home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ddkx86v5.zip / DDKX86 / SRC / VDH / VDHMODE.C < prev    next >
C/C++ Source or Header  |  1995-04-14  |  79KB  |  1,917 lines

  1. /*DDK*************************************************************************/
  2. /*                                                                           */
  3. /* COPYRIGHT (C) Microsoft Corporation, 1989                                 */
  4. /* COPYRIGHT    Copyright (C) 1995 IBM Corporation                           */
  5. /*                                                                           */
  6. /*    The following IBM OS/2 WARP source code is provided to you solely for  */
  7. /*    the purpose of assisting you in your development of OS/2 WARP device   */
  8. /*    drivers. You may use this code in accordance with the IBM License      */
  9. /*    Agreement provided in the IBM Device Driver Source Kit for OS/2. This  */
  10. /*    Copyright statement may not be removed.                                */
  11. /*                                                                           */
  12. /*****************************************************************************/
  13. /****************************************************************************
  14.  *
  15.  * SOURCE FILE NAME = VDHMODE.c
  16.  *
  17.  * DESCRIPTIVE NAME = Base video device handlers - Mode, CLUT, Palette  
  18.  *
  19.  *
  20.  * VERSION      V2.0
  21.  *
  22.  * DATE         
  23.  *
  24.  * DESCRIPTION  This source file contains VDH entry points
  25.  *              which get/set the video mode.
  26.  *
  27.  *              NOTE: These routines execute as ring 2 conforming
  28.  *
  29.  * FUNCTIONS    GetMode,         SetMode                             
  30.  *              SetHWMode, SetEnvMode, GetModeIndex                  
  31.  *
  32.  * NOTES        NONE
  33.  *
  34.  * STRUCTURES   NONE
  35.  *
  36.  * EXTERNAL REFERENCES
  37.  *
  38.  *              NONE
  39.  *
  40.  * EXTERNAL FUNCTIONS  SaveRegs, RestoreRegs        
  41.  *                     AccessHardware, AccessCLUT   
  42.  *                     PhysToUVirt, FreePhysToUVirt 
  43.  *
  44. */
  45.  
  46. /*
  47. **  Include files                                                             
  48. */
  49.  
  50. #define  INCL_BASE                     /* ALL of OS/2 Base                  */
  51. #define  INCL_OS2STD                   /* Needed for NULL definition in     */
  52.                                        /* OS2STD.H                          */
  53. #include <os2.h>
  54. #include "vdhctl.h"                    /* Conditional compilation control   */
  55. #include "vdh.h"                       /* Type definitions                  */
  56.  
  57. /*
  58. **  Externally defined global variables                                       
  59. */
  60.  
  61. extern VIDEOMODE Modes[];              /* Supported modes                   */
  62. extern MEMORYMAPS MemoryMaps[];        /* Memory map info for each mode     */
  63. extern UCHAR READABLE;
  64. extern ULONG PartialSaveSize;          /* Room required to save entire PVB  
  65.                                           in the popup mode                 */
  66. extern USHORT VGA_PRESENT;             /* TRUE if VGA VDH has been          */
  67.                                        /* installed                         */
  68. extern USHORT(APIENTRY *ChainedCallVectorTable[MaxFn])();/*             @T24*/
  69. extern VIDEOHARDWARE VideoHardware;    /*                               @T39*/
  70. extern USHORT OEMFlags;                /* @MS27 */
  71. extern USHORT SVGAPresent;             /* @drw */
  72.  
  73. /*
  74. **  Parameters to ring 2 routines                                             
  75. */
  76.  
  77. extern CLUTDATA far ColorCLUT;
  78. extern CLUTDATA far MonoCLUT;
  79. extern CLUTDATA far SumCLUT;
  80. extern CLUTDATA far ColorCLUTLow;      /* @MS13 */
  81. extern CLUTDATA far ColorCLUT256;      /* @MS13 */
  82. extern CLUTDATA far SumCLUTLow;        /* @MS13 */
  83. extern CLUTDATA far SumCLUT256;        /* @MS13 */
  84. extern USHORT far ColorCLUTCount;      /* @MS13 */
  85. extern USHORT far ColorCLUTLowCount;   /* @MS13 */
  86. extern USHORT far ColorCLUT256Count;   /* @MS13 */
  87. extern USHORT far MonoCLUTCount;       /* @MS13 */
  88. extern USHORT far SumCLUTCount;        /* @MS13 */
  89. extern USHORT far SumCLUTLowCount;     /* @MS13 */
  90. extern USHORT far SumCLUT256Count;     /* @MS13 */
  91. extern CLUTDATA far *LCLUT;            /* @BB9, @S40F*/
  92.  
  93. /*****************************************************************************
  94.  *                                                                          
  95.  *  SUBROUTINE NAME: GetMode                                                
  96.  *                                                                          
  97.  *  DESCRIPTIVE NAME: Get current video mode setting                        
  98.  *                                                                          
  99.  *  FUNCTION: GetMode is called by BVS to get the current mode setting.     
  100.  *            If the request specifies hardware and the hardware is         
  101.  *            readable, the actual hardware setting will be read and        
  102.  *            returned.  Otherwise the returned information will be         
  103.  *            taken from the environment buffer, if it has been passed.     
  104.  *                                                                          
  105.  *  ENTRY POINT: GetMode                                                    
  106.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 271 )       
  107.  *                                                                          
  108.  *  INPUT: (Passed on stack)                                                
  109.  *             FAR *Environment ( Environment buffer for the session )      
  110.  *             FAR *ParmBlock                                               
  111.  *                     USHORT Length = length of this packet                
  112.  *                     USHORT Flags  = 0 - Environment buffer only          
  113.  *                                     1 - Hardware also                    
  114.  *                     VIOMODEINFO FAR *ModeDataPTR                         
  115.  *             ULONG Function ( Call vector table entry = 271 )             
  116.  *         (Referenced)                                                     
  117.  *             Modes[] (global data - table of supported video modes )      
  118.  *                                                                          
  119.  *  EXIT-NORMAL: AX = 0                                                     
  120.  *               Current mode setting is returned                           
  121.  *                                                                          
  122.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                                
  123.  *                                                                          
  124.  *  EFFECTS: If hardware specified and hardware is readable, the            
  125.  *           environment buffer is updated, if passed.                      
  126.  *           If the length of ModeData does not exactly fit a               
  127.  *           parameter, the length is adjusted and returned.                
  128.  *                                                                          
  129.  *  INTERNAL REFERENCES:                                                    
  130.  *    ROUTINES: NONE                                                        
  131.  *                                                                          
  132.  *  EXTERNAL REFERENCES:                                                    
  133.  *    ROUTINES: SaveRestoreHW, SetEnvMode                                   
  134.  *                                                                          
  135.  ****************************************************************************/
  136.  
  137.  
  138. USHORT EXPENTRY GetMode(Environment,ParmBlock,Function)
  139.  
  140.   ENVIRONMENT far *Environment;
  141.   VDH_MODE    far *ParmBlock;
  142.   ULONG           Function;
  143.  
  144. {
  145.   USHORT rc,ModeIndex,EnvBufferPassed;
  146.   UCHAR i,MapMask,RegValue;
  147.   USHORT NumBitplanes;
  148.   REGDATA RegData;
  149.   REGADDRESS RegAddress;
  150.   ENVIRONMENT far *TempEnv;
  151.   SEL Selector;
  152.   UCHAR TempChar;                      /*                              @TB21*/
  153.   VIDEOMODE far *pCurMode;             /* Reduce level of indirection,   @S8*/
  154.   VIOMODEINFO far *pReqMode;           /* Reduce level of indirection,   @S8*/
  155.   unsigned register ReqModeLen;        /* Eliminate segment loading,     @S8*/
  156.   int SumCLUT_found,ii,j;              /*                               @BB9*/
  157.  
  158.   rc = ERROR_VIO_INVALID_PARMS;        /* Initialize to error               */
  159.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  160.  
  161.   if ((Function == FnGetMode) &&       /* Valid function request            */
  162.      (ParmBlock->Length >= sizeof(VDH_MODE)) && /* Valid packet length      */
  163.      (ParmBlock->Flags <= 1) &&        /* Valid flags                       */
  164.      (EnvBufferPassed || (ParmBlock->Flags&UPDATE_HARDWARE)))
  165.   {
  166.  
  167.     rc = NO_ERROR;                     /* Initialize no error               */
  168.     ReqModeLen = (pReqMode = ParmBlock->ModeDataPTR)->cb;/*              @S8*/
  169.  
  170.     if (ReqModeLen > sizeof(VIOMODEINFO))
  171.     {                                  /*                               @S25*/
  172.       ReqModeLen = MinDLen_Mode_Vres;  /*                               @S25*/
  173.     }                                  /*                               @S25*/
  174.  
  175.     if (ReqModeLen < Mode_Return_Length)/*                               @@A*/
  176.       rc = ERROR_VIO_INVALID_LENGTH;   /*                                @@A*/
  177.  
  178.     else
  179.  
  180.       if (ReqModeLen == Mode_Return_Length)/*                            @@A*/
  181.  
  182.         ReqModeLen = sizeof(VIOMODEINFO);/*                              @@A*/
  183.  
  184.       else
  185.       {                                /*                                @@A*/
  186.  
  187.         /*
  188.         **  If requesting application is running in foreground,                    
  189.         **    - Return hardware value if hardware can be read                      
  190.         **    - Otherwise return environment buffer value                          
  191.         **    ( report error if write-only hardware and no env buffer passed )
  192.         **  If requesting application is running in background,                    
  193.         **    - Return environment buffer value                                    
  194.         **     ( report error if environment buffer was not passed )               
  195.         */
  196.                                                                            
  197.  
  198.         if ((ParmBlock->Flags&UPDATE_HARDWARE) && READABLE)
  199.         {
  200.  
  201. #if      VDHVGA                        /* Read/write hardware               */
  202.                                        /* @MS00 */
  203.  
  204.           /*
  205.           **  Allocate temporary storage to temporary environment buffer                
  206.           */
  207.  
  208.           if (!(rc = DosAllocSeg(sizeof(ENVIRONMENT), (PSEL)&Selector, 0)))
  209.           {
  210.             TempEnv = (ENVIRONMENT far *)MakeFarPTR(Selector, 0);
  211.  
  212.             /*
  213.             **  Read all registers directly from the hardware                             
  214.             */
  215.  
  216.             TempEnv->VideoEnable = 1;  /* Assume video ON               @T34*/
  217.             SaveRestoreHW(TempEnv, GET);
  218.             ModeIndex = GetModeIndex(TempEnv);/* Get mode index + 1         */
  219.  
  220.             if (!ModeIndex--)
  221.               rc = ERROR_VIO_MODE;     /* Somebody corrupted the registers  */
  222.  
  223.             if (!rc && EnvBufferPassed)
  224.  
  225.               /*
  226.               **  Update environment buffer with current mode setting                       
  227.               */
  228.  
  229.               SetEnvMode((UCHAR)ModeIndex, Environment, TempEnv, 0);/*  @T70*/
  230.  
  231.             /*
  232.             **  Deallocate temporary storage to temporary environment buffer              
  233.             */
  234.  
  235.             DosFreeSeg(Selector);
  236.           } 
  237. #endif             /*     VDHVGA @MS00 */
  238.         } 
  239.  
  240.         else
  241.         {
  242.  
  243.           /*
  244.           **  Requesting application is running in the "background"                     
  245.           */
  246.  
  247.           if (EnvBufferPassed)
  248.  
  249.             /*
  250.             **  Retrieve mode setting from environment buffer                             
  251.             */
  252.  
  253.             ModeIndex = Environment->ModeIndex;
  254.  
  255.           else
  256.             rc = ERROR_VIO_INVALID_PARMS;/* Not FG OR write-only, no env    */
  257.                                          /* buffer                          */
  258.         } 
  259.  
  260. #if      VDH8514A                        /* @MS00 */
  261.  
  262.         /*
  263.         **  If the current mode in the environment buffer is an 8514/A native         
  264.         **  mode, return the appropriate information.  Otherwise call GetMode         
  265.         **  in VDHVGA.DLL                                                             
  266.         **                                                                            
  267.         */
  268.  
  269.         if (!Environment->NATIVE_MODE)
  270.           rc = ChainedVDHGetMode((ENVIRONMENT far *)&Environment->VGAEnvironment,
  271.                                   ParmBlock, Function);         /* not      */
  272.                                                                 /* native   */
  273.  
  274.         else
  275.  
  276.           /* Native mode - return the mode data                             */
  277.  
  278. #endif    
  279.  
  280.           if (!rc)
  281.           {
  282.             pCurMode = (VIDEOMODE far *)&Modes[ModeIndex].cb;/*          @S8*/
  283.  
  284.             /*
  285.             **  Mode type:
  286.             **  xxxxxxxb b = 0 Monochrome,  b = 1 Other
  287.             **  xxxxxxbx b = 0 Text,        b = 1 Graphics
  288.             **  xxxxxbxx b = 0 Color burst, b = 1 Color burst disabled
  289.             **  xxxxbxxx b = 0 Normal mode, b = 1 Advanced function ( native )
  290.             */
  291.  
  292.             pReqMode->fbType = pCurMode->fbType;
  293.  
  294.             if (EnvBufferPassed && !(ParmBlock->Flags&UPDATE_HARDWARE))
  295.             {
  296.  
  297. #if      VDHVGA                        /* Read/write hardware               */
  298.                                        /* @MS00 */
  299.  
  300.               if ((Environment->ModeData.fbType&NOT_MONO) /* @MS13 */
  301.                  && !(Environment->ModeData.fbType&GRAPHICS))
  302.               {                        /* @MS13 */
  303.                 /*
  304.                 ** We are looking for any monochrome CLUT, not a particular
  305.                 ** one. Thecharacteristic of a monochrome palette is the same
  306.                 ** intensity for red, green, and blue in each row.
  307.                 */
  308.                 register irow = 0;     /* @MS13 */
  309.  
  310.  
  311.                 while ((irow < 0xFF+1) /* @MS13 */
  312.                    && (Environment->LookupTable[irow].Red ==   /* @MS13 */
  313.                       Environment->LookupTable[irow].Green)    /* @MS13 */
  314.                    && (Environment->LookupTable[irow].Red ==   /* @MS13 */
  315.                       Environment->LookupTable[irow].Blue)     /* @MS13 */
  316.                    )
  317.                   irow++;                                      /* @MS13 */
  318.  
  319.                 /*
  320.                 **  If loop completed, assume black & white
  321.                 */
  322.                 if (irow == 0xFF+1)
  323.                   Environment->ModeData.fbType |= NO_CLR_BRST; /* @MS13 */
  324.               }                                                /* @MS13 */
  325. #endif    
  326.               pReqMode->fbType = Environment->ModeData.fbType;
  327.             } 
  328.  
  329.             if (ReqModeLen < MinDLen_Mode_Color)
  330.               ReqModeLen = MinDLen_Mode_Type;
  331.  
  332.             else
  333.             {
  334.  
  335.               /*
  336.               **  Color ( power of 2 )                                                      
  337.               */
  338.  
  339.               pReqMode->color = pCurMode->color;
  340.  
  341.               if (ReqModeLen < MinDLen_Mode_Column)
  342.                 ReqModeLen = MinDLen_Mode_Color;
  343.  
  344.               else
  345.               {
  346.  
  347.                 /*
  348.                 **  Number of text columns: 40 or 80                                          
  349.                 */
  350.  
  351.                 pReqMode->col = pCurMode->col;
  352.  
  353.                 if (ReqModeLen < MinDLen_Mode_Row)
  354.                   ReqModeLen = MinDLen_Mode_Column;
  355.  
  356.                 else
  357.                 {
  358.  
  359.                   /*
  360.                   **  Number of text rows                                                       
  361.                   */
  362.  
  363.                   pReqMode->row = pCurMode->row;
  364.  
  365.                   if (EnvBufferPassed && !(ParmBlock->Flags&UPDATE_HARDWARE))
  366.                     pReqMode->row = Environment->ModeData.row;
  367.  
  368.                   if (ReqModeLen < MinDLen_Mode_Hres)
  369.                     ReqModeLen = MinDLen_Mode_Row;
  370.  
  371.                   else
  372.                   {
  373.  
  374.                     /*
  375.                     **  Horizontal resolution                                                     
  376.                     */
  377.  
  378.                     pReqMode->hres = pCurMode->hres;
  379.  
  380.                     if (ReqModeLen < MinDLen_Mode_Vres)
  381.                       ReqModeLen = MinDLen_Mode_Hres;
  382.  
  383.                     else
  384.                     {
  385.  
  386.                       /*
  387.                       **  Vertical resolution                                                       
  388.                       */
  389.  
  390.                       pReqMode->vres = pCurMode->vres;
  391.  
  392.                       if (ReqModeLen < MinDLen_Mode_FormatID)
  393.                         ReqModeLen = MinDLen_Mode_Vres;
  394.  
  395.                       else
  396.                       {
  397.  
  398.                         /*
  399.                         **  Attribute format ID                                                       
  400.                         */
  401.  
  402.                         if (EnvBufferPassed)/*                           @P1*/
  403.                           pReqMode->fmt_ID = Environment->ModeData.fmt_ID;
  404.  
  405.                         else           /*                                @P1*/
  406.                           pReqMode->fmt_ID = pCurMode->fmt_ID;
  407.  
  408.                         if (ReqModeLen < MinDLen_Mode_Attrib)
  409.                           ReqModeLen = MinDLen_Mode_FormatID;
  410.  
  411.                         else
  412.                         {
  413.  
  414.                           /*
  415.                           **  Attribute                                                                 
  416.                           */
  417.  
  418.                           if (EnvBufferPassed)/*                         @P1*/
  419.                             pReqMode->attrib = Environment->ModeData.attrib;
  420.  
  421.                           else         /*                                @P1*/
  422.                             pReqMode->attrib = pCurMode->attrib;
  423.  
  424.                           if (ReqModeLen < MinDLen_Mode_BufStart)
  425.                             ReqModeLen = MinDLen_Mode_Attrib;
  426.  
  427.                           else
  428.                           {
  429.  
  430.                             /*
  431.                             **  Physical display buffer start address                                     
  432.                             */
  433.  
  434. #if      VDH8514A                      /* @MS00 */
  435.                             pReqMode->BufferAddress = UNKNOWN;
  436.  
  437. #else
  438.                             pReqMode->BufferAddress = MemoryMaps
  439.                                [pCurMode->MemMap].Start.FullAddress;
  440. #endif    
  441.  
  442.                             if (ReqModeLen < MinDLen_Mode_BufLen)
  443.                               ReqModeLen = MinDLen_Mode_BufStart;
  444.  
  445.                             else
  446.                             {
  447.  
  448.                               /*
  449.                               **  Physical display buffer length                                            
  450.                               */
  451.  
  452. #if      VDH8514A                      /* @MS00 */
  453.                               pReqMode->BufferLength = UNKNOWN;
  454.  
  455. #else
  456.                               pReqMode->BufferLength = pReqMode->fbType
  457.                                  &GRAPHICS?MemoryMaps[pCurMode->MemMap].
  458.                                  TotalSize:2*pReqMode->col *pReqMode->row;
  459.  
  460.                               if (pCurMode->MemMap == MemMap_LoRes)
  461.                                 pReqMode->BufferLength = 16000;/*      @@TB9*/
  462. #endif    
  463.  
  464.                               if (ReqModeLen < MinDLen_Mode_FullBufSz)
  465.                                 ReqModeLen = MinDLen_Mode_BufLen;
  466.  
  467.                               else
  468.                               {
  469.  
  470.                                 /*
  471.                                 **  Full physical display buffer size                                         
  472.                                 */
  473.  
  474.                                 pReqMode->FullBufferSize = 
  475.                                    pReqMode->BufferLength;
  476.  
  477.                                 /*
  478.                                 **  Add size of font buffer only if text mode
  479.                                 */
  480.  
  481.                                 if (!(pCurMode->fbType&GRAPHICS))
  482.                                   pReqMode->FullBufferSize = 
  483.                                      pReqMode->BufferLength+ 
  484.                                      (ULONG)256*(ULONG)
  485.                                            (Environment->ModeData.vres/
  486.                                            Environment->ModeData.row);
  487.  
  488.                                 if (pReqMode->attrib == WorldAttrCount)
  489.                                   pReqMode->FullBufferSize *= WorldAttrMult;
  490.  
  491.                                 if (ReqModeLen < MinDLen_Mode_PartBufSz)
  492.                                   ReqModeLen = MinDLen_Mode_FullBufSz;
  493.  
  494.                                 else
  495.                                 {
  496.  
  497.                                   /*
  498.                                   **  Full physical display buffer size of
  499.                                   **  popup mode
  500.                                   */
  501.  
  502.                                   pReqMode->PartialBufferSize = 
  503.                                      PartialSaveSize;
  504.  
  505.                                   if (pReqMode->attrib == WorldAttrCount)
  506.                                     pReqMode->PartialBufferSize *= 
  507.                                        WorldAttrMult;
  508.  
  509.                                   if (ReqModeLen < MinDLen_Mode_ExtData)
  510.                                     ReqModeLen = MinDLen_Mode_PartBufSz;
  511.  
  512.                                   else
  513.                                   {
  514.  
  515.                                     /*
  516.                                     **  Pointer to extended mode data area
  517.                                     */
  518.  
  519.                                     pReqMode->ExtDataArea = (UCHAR far *)NULL;
  520.                                     ReqModeLen = MinDLen_Mode_ExtData;
  521.                                   } 
  522.                                 }      /* else get PartialBufferSize        */
  523.                               }        /* else get FullBufferSize           */
  524.                             }          /* else get BufferLength             */
  525.                           }            /* else get BufferAddress            */
  526.                         }              /* else get Attribute                */
  527.                       }                /* else get Format ID                */
  528.                     }                  /* else get Vres                     */
  529.                   }                    /* else get Hres                     */
  530.                 }                      /* else get rows                     */
  531.               }                        /* else get columns                  */
  532.             }                          /* else get color                    */
  533.           }                            /* if !rc                            */
  534.       }                                /* else good mode length          @@A*/
  535.   }                                    /* if good parameters                */
  536.  
  537.   if (!rc)
  538.   {                                    /*                                @S8*/
  539.     pReqMode->cb = ReqModeLen;         /*                                @S8*/
  540.   }                                    /*                                @S8*/
  541.  
  542.  
  543.   return (rc);
  544.  
  545. /*****************************************************************************
  546.  *                                                                          
  547.  *  SUBROUTINE NAME: SetMode                                                
  548.  *                                                                          
  549.  *  DESCRIPTIVE NAME: Set video mode                                        
  550.  *                                                                          
  551.  *  FUNCTION: SetMode is called by BVS to set the video mode.               
  552.  *            If the request specifies hardware, the hardware and the       
  553.  *            environment buffer, if passed, will be updated.               
  554.  *            Otherwise just the environment buffer, if passed, will        
  555.  *            be updated.                                                   
  556.  *                                                                          
  557.  *  ENTRY POINT: SetMode                                                    
  558.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 272 )       
  559.  *                                                                          
  560.  *  INPUT: (Passed on stack)                                                
  561.  *             FAR *Environment ( Environment buffer for the session )      
  562.  *             FAR *ParmBlock                                               
  563.  *                     USHORT Length = length of this packet                
  564.  *                     USHORT Flags  = 0 - Environment buffer only          
  565.  *                                     1 - Hardware also                    
  566.  *                     VIOMODEINFO FAR *ModeDataPTR                         
  567.  *             ULONG Function ( Call vector table entry = 272 )             
  568.  *         (Referenced)                                                     
  569.  *             Modes[] (global data - table of supported video modes )      
  570.  *                                                                          
  571.  *  EXIT-NORMAL: AX = 0                                                     
  572.  *               Video mode is set                                          
  573.  *                                                                          
  574.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                                
  575.  *                                                                          
  576.  *  INTERNAL REFERENCES:                                                    
  577.  *    ROUTINES: NONE                                                        
  578.  *                                                                          
  579.  *  EXTERNAL REFERENCES:                                                    
  580.  *    ROUTINES: SetHWMode                                                   
  581.  *                                                                          
  582.  ****************************************************************************/
  583.  
  584.  
  585. USHORT EXPENTRY SetMode(Environment,ParmBlock,Function)
  586.  
  587.   ENVIRONMENT far *Environment;
  588.   VDH_MODE    far *ParmBlock;
  589.   ULONG           Function;
  590.  
  591. {
  592.   USHORT rc,i,tempflags,MODE_FOUND,EnvBufferPassed;
  593.   UCHAR FONT_AVAILABLE;                /*                              @@TB1*/
  594.   VIDEOMODE far *pCurMode;             /* Reduce level of indirection,   @S8*/
  595.   VIOMODEINFO far *pReqMode;           /* Reduce level of indirection,   @S8*/
  596.   unsigned register ReqModeLen;        /* Eliminate segment loading,     @S8*/
  597.   USHORT ChainedXGAMode132 = (ParmBlock->Flags & 0x4000);       /*            */
  598.   ParmBlock->Flags &= ~0x4000;                                  /*            */
  599.  
  600.   FONT_AVAILABLE = TRUE;               /*                              @@TB1*/
  601.   rc = ERROR_VIO_INVALID_PARMS;        /* Initialize to error               */
  602.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  603.  
  604.   if ((Function == FnSetMode) &&       /* Valid function request            */
  605.      (ParmBlock->Flags <= 0x07) &&     /* Valid flags                       */
  606.      (EnvBufferPassed || (ParmBlock->Flags&UPDATE_HARDWARE)))
  607.   {
  608.     ReqModeLen = (pReqMode = ParmBlock->ModeDataPTR)->cb;/*              @S8*/
  609.     rc = ERROR_VIO_INVALID_LENGTH;     /*             @@TB1INITIALIZETOERROR*/
  610.  
  611.     if ((ParmBlock->Length >= sizeof(VDH_MODE)) && /* @@TB1VALIDPACKETLENGTH*/
  612.        (ReqModeLen >= MinDLen_Mode_Type))          /* @@TB1VALIDMODELENGTH  */
  613.     {
  614.  
  615.       /*
  616.       **  Find the mode based upon matching specified parameters with entry in      
  617.       **  the table of supported modes.  If a partial parameter list is             
  618.       **  provided, use the highest resolution mode that matches the provided       
  619.       **  parameters.                                                               
  620.       */
  621.                                                                             
  622.       rc = ERROR_VIO_MODE;             /* Initialize to error               */
  623.       tempflags = ParmBlock->Flags;
  624.  
  625.       if (pReqMode->fbType&NO_CLR_BRST)
  626.       {
  627.         tempflags |= NO_CLR_BRST;      /* Indicate B/W Mode                 */
  628.       } 
  629.  
  630. #if      1                      /* @drw **************** added SVGA support */
  631.                                 /* Flags bit 2 is 'no validation' flag      */
  632.                                 /* must have SVGAPresent                    */
  633.                                 /* must have an environment to update       */
  634.                                 /* must be text mode                        */
  635.                                 /* must have at least up to 'row' data      */
  636.  
  637.       if (SVGAPresent && EnvBufferPassed && (ParmBlock->Flags&0x04) && 
  638.       (ReqModeLen >= MinDLen_Mode_Row) && !(pReqMode->fbType&GRAPHICS))
  639.       {
  640.         Environment->ModeIndex = 0;
  641. #if     VDHVGA
  642.         Environment->SVGAMode |= MODE_SVGA_ENVFLAG;    /*            */
  643. #endif
  644.         Environment->ModeData.cb = ReqModeLen;
  645.         Environment->ModeData.fbType = pReqMode->fbType;
  646.  
  647.         if (ReqModeLen >= MinDLen_Mode_Color)
  648.           Environment->ModeData.color = pReqMode->color;
  649.  
  650.         if (ReqModeLen >= MinDLen_Mode_Column)
  651.           Environment->ModeData.col = pReqMode->col;
  652.  
  653.         if (ReqModeLen >= MinDLen_Mode_Row)
  654.           Environment->ModeData.row = pReqMode->row;
  655.  
  656.         if (ReqModeLen >= MinDLen_Mode_Hres)
  657.           Environment->ModeData.hres = pReqMode->hres;
  658.  
  659.         if (ReqModeLen >= MinDLen_Mode_Vres)
  660.           Environment->ModeData.vres = pReqMode->vres;
  661.  
  662.         if (ReqModeLen >= MinDLen_Mode_FormatID)
  663.           Environment->ModeData.fmt_ID = pReqMode->fmt_ID;
  664.  
  665.         Environment->ModeData.BufferAddress = (UCHAR FAR *)0x0b8000L;
  666.         Environment->ModeData.BufferLength = 2*pReqMode->col *pReqMode->row;
  667.         Environment->ModeData.ExtDataArea = NULL;
  668.         Environment->ScrollRect.Right = pReqMode->col-1;
  669.         Environment->ScrollRect.Bottom = pReqMode->row-1;
  670.         return (NO_ERROR);
  671.       }                   
  672. #if     VDHVGA
  673.       else
  674.         Environment->SVGAMode &= ~MODE_SVGA_ENVFLAG;            /*           */
  675. #endif
  676. #endif                           /* @drw **************** end SVGA support  */
  677.  
  678.       for (i = 0, MODE_FOUND = FALSE; (i < NUM_MODES) && !MODE_FOUND; i++)
  679.       {                                /* @MS12 */
  680.         pCurMode = (VIDEOMODE far *)&Modes[i].cb;/*                      @S8*/
  681.  
  682.         /*
  683.         **  Verify that the mode is valid by finding it in the mode table             
  684.         */
  685.  
  686. #if      VDHCGA                        /* @MS00 */
  687.         /*
  688.         ** Ignore Color Burst on DSM                               
  689.         */
  690.  
  691.         if ((pCurMode->fbType == pReqMode->fbType)                 /* @MS14 */
  692.         || ((OEMFlags&DSM_MONITOR)                                 /* @MS14 */
  693.         && (pCurMode->fbType == (pReqMode->fbType&~NO_CLR_BRST)))) /* @MS14 */
  694.  
  695. #else                                                              /* @MS00 */
  696.  
  697.           if (pCurMode->fbType == (pReqMode->fbType&~NO_CLR_BRST)) /* @MS14 */
  698. #endif    
  699.  
  700.              /*
  701.              **  Type matches - If color specified, check for match. Otherwise
  702.              **               default color, col, row, hres, vres to this mode
  703.              */
  704.  
  705.              if (ReqModeLen < MinDLen_Mode_Color)
  706.                 MODE_FOUND = TRUE;       /* Found a matching mode           */
  707.  
  708.             else
  709.  
  710.               if (pReqMode->color == pCurMode->color)
  711.  
  712.                 /*
  713.                 **  Color matches - If col specified, check for match. 
  714.                 **      Otherwiswe default col, row, hres, vres to this mode
  715.                 */
  716.  
  717.                 if (ReqModeLen < MinDLen_Mode_Column)
  718.                   MODE_FOUND = TRUE;   /* Found a matching mode             */
  719.  
  720.                 else
  721.  
  722.                   if ((pReqMode->col == pCurMode->col) || (pReqMode->fbType
  723.                      &GRAPHICS))
  724.  
  725.                     /*
  726.                     **  col matches - If row specified, check for match. 
  727.                     **      Otherwise default row, hres and vres to this mode
  728.                     */      
  729.  
  730.                     if (ReqModeLen < MinDLen_Mode_Hres)
  731.                       MODE_FOUND = TRUE;/* Found a matching mode            */
  732.  
  733.                     else
  734.  
  735.                       if (pReqMode->hres == pCurMode->hres)
  736.  
  737.                         /*
  738.                         **  hres matches - If vres specified, check for match.
  739.                         **                 Otherwise default vres to this mode
  740.                         */
  741.  
  742.                         if (ReqModeLen < MinDLen_Mode_Vres)
  743.                           MODE_FOUND = TRUE;/* Found a matching mode        */
  744.  
  745.                         else
  746.  
  747.                           if (pReqMode->vres == pCurMode->vres)
  748.                             MODE_FOUND = TRUE;/* Found a matching mode      */
  749.  
  750. /*
  751. ** If the everything matches so far, call findfont if the rows don't match  
  752. ** and not graphics mode.  If no font found, set a temporary error code.    
  753. ** If no font found and buffer not large enough, reset !MODE_FOUND true.    
  754. */
  755.  
  756.         goto search_part1;
  757.  
  758. search_part2:                          /* MS?? - END */
  759.  
  760.         if (MODE_FOUND && (ReqModeLen >= MinDLen_Mode_Row))
  761.           if (pReqMode->row > 0)
  762.           {                            /*                               @B22*/
  763.  
  764.             if (!(pReqMode->fbType&GRAPHICS) && (pReqMode->row != 
  765.                pCurMode->row))
  766.             {
  767.  
  768. #if      FONT_SUPPORT                  /* @MS00 */
  769.               FONT_AVAILABLE = FindFont(i, pReqMode->row, Environment);
  770.  
  771.               if (!FONT_AVAILABLE || (0x2000 < (pReqMode->row *pReqMode->col)))
  772. #endif    
  773.                    MODE_FOUND = FALSE;
  774.             } 
  775.  
  776.             else
  777.               FONT_AVAILABLE = TRUE;
  778.           }                            /*                               @B22*/
  779.  
  780.           else
  781.             MODE_FOUND = FALSE;        /*                               @B22*/
  782.             goto search_end;
  783.  
  784. search_part1:
  785.  
  786. #if      VDHVGA                        /* @MS00 */
  787.  
  788.         if (MODE_FOUND)
  789.         {                              /*                               @S39*/
  790.  
  791.           /*
  792.           **  Make sure PDP does not exist for modes not valid with PDP
  793.           */
  794.  
  795.           /*          ** The logic of this terrible if statement should be:
  796.           ** If (current mode not supported on plasma display and
  797.           **     display is plasma) or 
  798.           ** if (mode has 132 columns and either
  799.           **     ((IBM_132 mode index used on non-IBM hardware) or
  800.           **     (OEM_132 index used on non-oem hardware)) true or
  801.           ** if (if CGA or MPA mode conflict with VGA)
  802.           **    reset mode found to false.                                      
  803.           */
  804.           if (((pCurMode->Flags&NOT_PLASMA)/*                           @S39*/
  805.              && (VideoHardware.display == PlasmaDisplay)) ||           /*@T39,@S39*/
  806.  
  807.              (
  808.               (pCurMode->col == 132) &&                         /*@B726706 */ 
  809.               ((
  810.                 (i < ModeIndex_OEM_132) &&                      /*@B726706 */
  811.                 (!(Environment->Comp_Reg = VideoHardware.Reg_132) ||  /*           */
  812.                 ((Environment->XGA_PRESENT) && !(ChainedXGAMode132))) /*           */
  813.                ) || 
  814.                (
  815.                 (i >= ModeIndex_OEM_132) && 
  816.                 !(OEMFlags&OEM_132_COLUMNS)
  817.                )                                                /*@B726706 */
  818.              ))                                                 /*@B726706 */
  819.  
  820.              /*
  821.              **  Make sure CGA does not conflict with this VGA mode 
  822.              */
  823.  
  824.              || ((pCurMode->fbType&NOT_MONO)/*                          @S39*/
  825.              && (VideoHardware.fVideoType&CGA_BIT))/*                   @S39*/
  826.  
  827.              /*
  828.              **  Make sure MPA does not conflict with this VGA mode                      
  829.              */
  830.  
  831.              || (!(pCurMode->fbType&NOT_MONO)/*                         @S39*/
  832.              && (VideoHardware.fVideoType&MPA_BIT)))
  833.           {                            /*                               @S39*/
  834.             MODE_FOUND = FALSE;
  835.           }                            /*                               @S39*/
  836.  
  837.         }                              /*                               @S39*/
  838. #endif    
  839.  
  840. #if      VDHEGA                        /* @MS00 */
  841.  
  842.  
  843.           /*
  844.           **  EVEN THOUGH THE SPECIFIED MODE WAS FOUND IN THE MODE TABLE, MAKE          
  845.           **  SURE THAT IT IS VALID WITH THE ATTATCHED MONITOR.  MAKE SURE              
  846.           **  MONOCHROME MONITOR EXISTS FOR MONOCHROME MODES 7,F,10                     
  847.           */
  848.  
  849.           if ((!(pReqMode->fbType&NOT_MONO) && 
  850.           (VideoHardware.display != Mono5151)) ||                /* @T39  */
  851.  
  852.           /*
  853.           **  Make sure monochrome monitor doesn't exist for color modes                
  854.           */
  855.  
  856.           ((pReqMode->fbType&NOT_MONO) && 
  857.           (VideoHardware.display == Mono5151)) ||                /* @T39  */
  858.           ((pCurMode->Flags&EGA_GT_64K) &&                       /* @MS15 */
  859.           (VideoHardware.memory <= 64L*1024L)) ||                /* @MS15 */
  860.  
  861.           /*
  862.           **  Make sure normal color monitor doesn't exist for enhanced modes           
  863.           */
  864.  
  865.           ((pCurMode->Flags&ENHANCED_MONITOR) && 
  866.           (VideoHardware.display != EnColor5154))                /*  @T39*/
  867.             )
  868.  
  869.             MODE_FOUND = FALSE;
  870. #endif    
  871.  
  872.         /*
  873.         **  Make sure disable color burst is not specified on non-applicable
  874.         **  modes
  875.         */
  876.  
  877.         if (MODE_FOUND && ((pReqMode->fbType&NO_CLR_BRST) && 
  878.                           (pCurMode->Flags & IGNORE_CLR_BRST)))
  879.  
  880.            MODE_FOUND = FALSE;          /* END OF                       @T37*/
  881.  
  882.         /*
  883.         **  Check for modes prohibited by OEM flags                                   
  884.         */
  885.  
  886. #if      VDHCGA                        /* @MS19 - BEGIN */
  887.  
  888.         if (MODE_FOUND)
  889.  
  890.           if ((!(OEMFlags&IDC_CGA)
  891.              && (pCurMode->Flags & IDC_PLASMA_ONLY))
  892.              || ((pCurMode->Flags & VDU_DSM_ONLY)
  893.              && (!(OEMFlags & VDU_CGA)
  894.              || !(OEMFlags & DSM_MONITOR) )))
  895.  
  896.             MODE_FOUND = FALSE;
  897.  
  898. #endif    
  899.  
  900. #if      VDHVGA
  901.  
  902.         if (MODE_FOUND)
  903.  
  904.           if (((OEMFlags & STARLIGHT_VGA) && !(pCurMode->fbType & GRAPHICS) &&
  905.              (pCurMode->col == 40) && (pCurMode->hres == 360)) || 
  906.              ((i == ModeIndex_VGC2E) && !(OEMFlags & STARDUST_VGA)))
  907.           {
  908.             MODE_FOUND = FALSE;
  909.           } 
  910.  
  911. #endif                                 /* VDHVGA */
  912.         goto search_part2;
  913.  
  914. /* End of mode table search                                                 */
  915.  
  916. search_end:                            /* @MS19 - END */
  917.         ;
  918.       }                                /* End of For that searches the Mode */
  919.                                        /* table                             */
  920.  
  921.       /*
  922.       **  Verify that the format ID and Attribute count are default or@P1   
  923.       **  ARE THE WORLD FORMAT (LVB FORMAT 70, 3 LVB ATTRIBUTES)            
  924.       */
  925.  
  926.       if (MODE_FOUND)
  927.       {                                /*                                @P1*/
  928.  
  929.         if (ReqModeLen >= MinDLen_Mode_FormatID)
  930.         {                              /*                                @P1*/
  931.  
  932.           if ((pReqMode->fmt_ID != DefaultFormat) && /*                  @P1*/
  933.              (pReqMode->fmt_ID != WorldFormat))
  934.           {                            /*                                @P1*/
  935.             MODE_FOUND = FALSE;        /*                                @P1*/
  936.           }                            /*                                @P1*/
  937.  
  938.           else
  939.           {                            /*                                @P1*/
  940.  
  941.             if (ReqModeLen >= MinDLen_Mode_Attrib)
  942.             {                          /*                                @P1*/
  943.  
  944.               if (((pReqMode->attrib != DefaultAttrCount) && 
  945.                  (pReqMode->attrib != WorldAttrCount) && 
  946.                  (pReqMode->attrib != 0)) ||                       /*  @TB15*/
  947.                  ((pReqMode->attrib != WorldAttrCount) && 
  948.                  (pReqMode->fmt_ID == WorldFormat)))
  949.               {
  950.                 MODE_FOUND = FALSE;    /*                                @P1*/
  951.               }                        /*                                @P1*/
  952.             }                          /*                                @P1*/
  953.           }                            /*                                @P1*/
  954.         }                              /*                                @P1*/
  955.       }                                /*                                @P1*/
  956.  
  957. #if      VDH8514A                      /* @MS00 */
  958.  
  959.         /*
  960.         **  If the specified mode is native to the 8514/A is an 8514/A native          
  961.         **  mode, return the appropriate information.  Otherwise call GetMode          
  962.         **  in VDHVGA.DLL                                                              
  963.         */
  964.  
  965.         if (!MODE_FOUND)
  966.         {
  967.  
  968.           if (VGA_PRESENT)
  969.           {
  970.             tempflags = Environment->NATIVE_MODE;/* Save AFMode flag    @T58*/
  971.             i = (USHORT)Environment->ModeData.fbType;/* Save mode type  @T58*/
  972.             Environment->ModeData.fbType &= ~NATIVE;/* Clear native bit     
  973.                                                                         @T58*/
  974.             Environment->NATIVE_MODE = FALSE;/* Must leave native mode      */
  975.  
  976.             if (ParmBlock->Flags&UPDATE_HARDWARE)
  977.             {
  978.               LeaveNativeMode();
  979.             } 
  980.  
  981.             if ((rc = ChainedVDHSetMode(           /*                   @T58*/
  982.                (ENVIRONMENT far *)&Environment->VGAEnvironment, 
  983.                ParmBlock, Function)))
  984.             {
  985.  
  986.               /* Reset back to AF mode if setmode fails on VGA              */
  987.  
  988.               Environment->NATIVE_MODE = tempflags;/* Restore native mode   */
  989.                                                    /*                   @T58*/
  990.               Environment->ModeData.fbType = (UCHAR)i;/* Restore mode type  
  991.                                                                         @T58*/
  992.  
  993.               if ((ParmBlock->Flags&UPDATE_HARDWARE) && tempflags)
  994.               {                        /*                               @T58*/
  995.                 SetHWMode(Environment->ModeIndex, ParmBlock->Flags, 
  996.                    Environment);
  997.               } 
  998.             } 
  999.           } 
  1000.         } 
  1001.  
  1002.         else
  1003.         {
  1004.           Environment->NATIVE_MODE = TRUE;/* Must set a Native mode         */
  1005.  
  1006. #else
  1007.  
  1008.           if (MODE_FOUND)
  1009.           {
  1010. #endif    
  1011.  
  1012.             /*
  1013.             **  Parameters are valid
  1014.             **  - Update environment buffer if it was passed         
  1015.             **  - Update hardware if in foreground                   
  1016.             */
  1017.  
  1018.             if (EnvBufferPassed)
  1019.             {
  1020.  
  1021.               /*
  1022.               **  Put all of the caller's parameters into the 
  1023.               **  environment buffer Fill in missing
  1024.               **  parameters with defaults in the mode table                     
  1025.               */
  1026.  
  1027.               Environment->ModeIndex = (UCHAR)i-1;/* Remember actual mode   */
  1028.                                                   /* being set              */
  1029.               Environment->ModeData.cb = ReqModeLen;
  1030.               Environment->ModeData.fbType = pReqMode->fbType;
  1031.  
  1032.               if (ReqModeLen >= MinDLen_Mode_Color)
  1033.                 Environment->ModeData.color = pReqMode->color;
  1034.  
  1035.               else
  1036.                 Environment->ModeData.color = pCurMode->color;
  1037.  
  1038.               if (ReqModeLen >= MinDLen_Mode_Column)
  1039.                 Environment->ModeData.col = pReqMode->col;
  1040.  
  1041.               else
  1042.                 Environment->ModeData.col = pCurMode->col;
  1043.  
  1044.               if (ReqModeLen >= MinDLen_Mode_Row)
  1045.                 Environment->ModeData.row = pReqMode->row;
  1046.  
  1047.               else
  1048.                 Environment->ModeData.row = pCurMode->row;
  1049.  
  1050.               if (ReqModeLen >= MinDLen_Mode_Hres)
  1051.                 Environment->ModeData.hres = pReqMode->hres;
  1052.  
  1053.               else
  1054.                 Environment->ModeData.hres = pCurMode->hres;
  1055.  
  1056.               if (ReqModeLen >= MinDLen_Mode_Vres)
  1057.                 Environment->ModeData.vres = pReqMode->vres;
  1058.  
  1059.               else
  1060.                 Environment->ModeData.vres = pCurMode->vres;
  1061.  
  1062.               if (ReqModeLen >= MinDLen_Mode_FormatID)
  1063.                 Environment->ModeData.fmt_ID = pReqMode->fmt_ID;
  1064.  
  1065.               else
  1066.                 Environment->ModeData.fmt_ID = pCurMode->fmt_ID;
  1067.  
  1068.               /*
  1069.               ** always set World Attribute count if World Format
  1070.               ** was specified
  1071.               */
  1072.  
  1073.               if (Environment->ModeData.fmt_ID == WorldFormat)/*         @P1*/
  1074.                 Environment->ModeData.attrib = WorldAttrCount;/*         @P1*/
  1075.  
  1076.               else
  1077.                 Environment->ModeData.attrib = pCurMode->attrib;
  1078.  
  1079.               Environment->ModeData.BufferAddress = 
  1080.                  MemoryMaps [pCurMode->MemMap].Start.FullAddress;
  1081.  
  1082.               Environment->ModeData.BufferLength = 
  1083.                  Environment->ModeData.fbType & GRAPHICS
  1084.                  ? MemoryMaps[pCurMode->MemMap].TotalSize
  1085.                  : 2 * Environment->ModeData.col * Environment->ModeData.row;
  1086.  
  1087.               if (pCurMode->MemMap == MemMap_LoRes)
  1088.                 Environment->ModeData.BufferLength = 16000;/*          @@TB9*/
  1089.  
  1090.               Environment->ModeData.ExtDataArea = (UCHAR far *)NULL;
  1091.             } 
  1092.  
  1093. #if      FONT_SUPPORT                  /* @MS00 */
  1094.  
  1095.             /*
  1096.             **  Setup the font to be loaded for text modes                                
  1097.             */
  1098.  
  1099.             if (!(pReqMode->fbType&GRAPHICS))
  1100.             {
  1101.  
  1102.               if (EnvBufferPassed)
  1103.               {
  1104.                 FindFont(Environment->ModeIndex, Environment->ModeData.row, 
  1105.                    Environment);
  1106.               } 
  1107.  
  1108.               else
  1109.               {
  1110.                 FindFont((UCHAR)i-1, Modes[(UCHAR)i-1].row, Environment);
  1111.               } 
  1112.             } 
  1113. #endif    
  1114.  
  1115.             /*
  1116.             **  If Flags = UPDATE_HARDWARE, SetHWMode will set the hardware               
  1117.             **  registers If environment buffer is passed, SetHWMode will                 
  1118.             **  shadow the registers                                                      
  1119.             */
  1120.  
  1121.             rc = SetHWMode((UCHAR)(i-1), tempflags, Environment);/*     @T53*/
  1122.  
  1123.           } 
  1124.  
  1125.           if (!FONT_AVAILABLE)         /*                              @@TB1*/
  1126.             rc = ERROR_VIO_FONT;       /* end check for invalid length      */
  1127.         }                              /*                              @@TB1*/
  1128.     }                                  /* end check for invalid parms  @@TB1*/
  1129.  
  1130.     if (rc == NO_ERROR)
  1131.     {                                  /*                                @P1*/
  1132.  
  1133.       if (EnvBufferPassed)
  1134.       {
  1135.         Environment->ScrollRect.Right = Environment->ModeData.col-1;/*   @P1*/
  1136.         Environment->ScrollRect.Bottom = Environment->ModeData.row-1;/*  @P1*/
  1137.  
  1138.         if (Environment->ModeData.fmt_ID == WorldFormat)/*               @P1*/
  1139.           Environment->AttrBufSize = 3;/*                                @P1*/
  1140.  
  1141.         else                           /*                                @P1*/
  1142.           Environment->AttrBufSize = 1;/*                                @P1*/
  1143.       }                                /*                                @P1*/
  1144.     }                                  /*                                @P1*/
  1145.  
  1146.     return (rc);
  1147.   } 
  1148.  
  1149. /*****************************************************************************
  1150.  *                                                                             
  1151.  *  SUBROUTINE NAME: SetHWMode                                                 
  1152.  *                                                                             
  1153.  *  DESCRIPTIVE NAME: Set the hardware mode                                    
  1154.  *                                                                             
  1155.  *  FUNCTION: SetHWMode is called to set the hardware to the supported         
  1156.  *            video mode. If Flags = UPDATE_HARDWARE, the registers            
  1157.  *            are set.  If environment buffer is passed, the registers         
  1158.  *            are shadowed.                                                    
  1159.  *                                                                             
  1160.  *  ENTRY POINT: SetHWMode                                                     
  1161.  *    LINKAGE:   CALL FAR                                                      
  1162.  *                                                                             
  1163.  *  INPUT: (Passed on stack)                                                   
  1164.  *             UCHAR Mode ( Index in table of supported modes )                
  1165.  *         (Referenced)                                                        
  1166.  *             Modes[] (global data - table of supported video modes )         
  1167.  *             Fonts[] (global data - table of ROM font areas )                
  1168.  *                                                                             
  1169.  *  EXIT-NORMAL: Video mode is set on hardware                                 
  1170.  *                                                                             
  1171.  *  EFFECTS: NONE                                                              
  1172.  *                                                                             
  1173.  *  INTERNAL REFERENCES:                                                       
  1174.  *    ROUTINES: SetDefaultCLUT, PhysToUVirt, FreePhysToUVirt                   
  1175.  *                                                                             
  1176.  *  EXTERNAL REFERENCES:                                                       
  1177.  *    ROUTINES: AccessHardware, AccessRegister                                 
  1178.  *                                                                             
  1179.  *****************************************************************************/
  1180.  
  1181.  
  1182.   USHORT PASCAL near SetHWMode(Mode, Flags, Environment)/*         @B15,@T53*/
  1183.  
  1184.   UCHAR Mode;
  1185.   USHORT Flags;
  1186.   ENVIRONMENT far *Environment;
  1187.   {
  1188.  
  1189.     USHORT EnvBufferPassed,Ind,ColorMode,VideoEnable,VideoOn,/*         @C21*/
  1190.            i,j,k,m,rc;                                       /*         @T53*/
  1191.     REGADDRESS RegAddress;
  1192.     REGDATA RegData;
  1193.  
  1194. #if      VDHVGA                        /* @MS00 */
  1195.     CLUTDATA far *CLUT;
  1196.     REGDATA  ClutParms;
  1197.     USHORT   CLUTCount;                /* @MS13 */
  1198. #endif    
  1199.  
  1200.     EnvBufferPassed = SEG(Environment);/* Non-zero = TRUE                   */
  1201.     ColorMode = Modes[Mode].fbType&NOT_MONO;/* Needed by AccessVideoEnable  */
  1202.                                             /* on non-VGA                   */
  1203.  
  1204. #if      !(VDH8514A)                   /* @MS00 */
  1205.  
  1206. /*
  1207. **  Turn the video signal off to reduce snow                                  
  1208. */
  1209.  
  1210.     if (Flags&UPDATE_HARDWARE)
  1211.     {
  1212.  
  1213.       if (EnvBufferPassed)             /*                               @C21*/
  1214.         VideoOn = Environment->VideoEnable;/*                           @C21*/
  1215.  
  1216.       else                             /*                               @C21*/
  1217.  
  1218. #if      VDHVGA                      /*                         @C21,@MS00*/
  1219.   AccessVideoEnable(ColorMode, GET, &VideoOn);/*                        @C21*/
  1220.  
  1221.   #else                                /*                         @C21,@MS00*/
  1222.   VideoOn = 1;                         /*                               @C21*/
  1223.  
  1224. #endif    
  1225.  
  1226.       VideoEnable = 0;                 /* Turn video off                    */
  1227.       AccessVideoEnable(ColorMode, SET, &VideoEnable);/* ring 2 callgate    */
  1228.     } 
  1229. #endif    
  1230.  
  1231. /*
  1232. **  IF NOT GRAPHICS, SEARCH THRU MODE TABLE TO FIND CRTCTLREGS_CMD WITH CURSOR
  1233. **  START/END DATA AND CHANGE THEM TO BE THE 2ND TO LAST AND LAST PEL IN THE  
  1234. **  ROW          INDEX 0XB -> END   0XA -> START                              
  1235. */
  1236.  
  1237.     if (!(Environment->ModeData.fbType&GRAPHICS))
  1238.     {                                                             /*    @C12*/
  1239.  
  1240.       for (i = 0; Modes[Mode].ModeRegs[i].Command != CRTCtlRegs_CMD || 
  1241.          Modes[Mode].ModeRegs[i].RegData.NumEntries < 0x0C; i++);
  1242.                                                                   /*    @C12*/
  1243.       m = Environment->ModeData.vres/Environment->ModeData.row;   /*    @C12*/
  1244.       Modes[Mode].ModeRegs[i].RegData.DataArea[0x0B] = (UCHAR)--m;/*    @C12*/
  1245.       Modes[Mode].ModeRegs[i].RegData.DataArea[0x0A] = (UCHAR)--m;/*    @C12*/
  1246.  
  1247. #if      !(VDHVGA      || VDH8514A)    /* set start line shadow for non-VGA 
  1248.                                                               @C24,B10,@MS00*/
  1249. Environment->EGAShadowTopScanLine = m; /*                               @C24*/
  1250. #endif    
  1251.     }                                  /*                           @C12,B10*/
  1252.  
  1253. #if      VDHCGA                        /* @MS16 - BEGIN */
  1254.  
  1255.       if (OEMFlags&IDC_CGA)
  1256.       {
  1257.         /*
  1258.         ** If setting 640x400 graphics mode, enable extended mode bit.
  1259.         ** For all other CGA modes, keep it clear.
  1260.         */
  1261.  
  1262.         Environment->Hardware.IDCExtendedMode = (Mode == ModeIndex_DMP40)
  1263.         ? 0x01 : 0x00;
  1264.  
  1265.         if (Flags&UPDATE_HARDWARE)
  1266.         {
  1267.           UCHAR byt;
  1268.  
  1269.           RegAddress.DataPort = IDCExtendedModeReg;
  1270.           AccessRegister(&RegAddress, GET, &byt);
  1271.           byt &= ~0x01;
  1272.           byt |= Environment->Hardware.IDCExtendedMode;
  1273.           AccessRegister(&RegAddress, SET, &byt);
  1274.         } 
  1275.       } 
  1276. #endif    
  1277.  
  1278. #if      VDHVGA
  1279.  
  1280.     if (OEMFlags&STARDUST_VGA)
  1281.     {
  1282.       /*
  1283.       ** Clear STARDUST's extended graphics register variables in
  1284.       ** environment. The corresponding hardware registers are
  1285.       ** automatically cleared when the sequencer registers are accessed
  1286.       */
  1287.  
  1288.       for (i = 0x00; i < 0x07; i++)
  1289.         Environment->Hardware.ExtVGARegs[i] = 0x00;
  1290.  
  1291.         /*
  1292.         ** Set 6-bit DAC mode 
  1293.         */
  1294.         Environment->Hardware.DACCmd &= 0x00;
  1295.  
  1296.       if (Flags&UPDATE_HARDWARE)
  1297.       {
  1298.         UCHAR byt;
  1299.  
  1300.         /*
  1301.         ** Set HW DAC if foreground 
  1302.         */
  1303.         RegAddress.DataPort = DACCmdReg;
  1304.         AccessRegister(&RegAddress, GET, &byt);
  1305.         byt &= ~0x02;
  1306.         AccessRegister(&RegAddress, SET, &byt);
  1307.  
  1308.         /*
  1309.         ** Unlock extended VGA graphics regs 
  1310.         */
  1311.         RegAddress.AddressPort = GraphAddressPort;
  1312.         RegAddress.DataPort = GraphDataPort;
  1313.         RegAddress.Flags = NONE;
  1314.         RegData.DataArea = &byt;
  1315.         RegData.FirstEntry = VGA_ENV_REG;
  1316.         RegData.NumEntries = 1;
  1317.         byt = VGA_UNLOCK;
  1318.         AccessHardware(&RegAddress, BYTES, NONE, SET, &RegData);
  1319.       } 
  1320.     } 
  1321. #endif
  1322.  
  1323.     for (i = 0,                        /* JumpCmd= FALSE,                 */
  1324.     Ind = Mode; Modes[Ind].ModeRegs[i].Command; i++)
  1325.     {                                  /* @MS17 */
  1326.  
  1327.       switch (Modes[Ind].ModeRegs[i].Command)
  1328.       {
  1329.  
  1330. #if      !(VDH8514A)                   /* @MS00 */
  1331.  
  1332. #if      VDHEGA        || VDHVGA     /* @MS00 */
  1333.  
  1334. /*
  1335. **  Set the attribute registers                                               
  1336. */
  1337.         case  Attributes_CMD :
  1338.  
  1339.           if (Flags&UPDATE_HARDWARE)
  1340.           {
  1341.             RegAddress.AddressPort = AttAddressPort;
  1342.             RegAddress.DataPort = AttDataWritePort;
  1343.             RegAddress.ColorAdjust = NONE;
  1344.             RegAddress.Flags = Attributes_CMD;
  1345.           } 
  1346.  
  1347. /*
  1348. **  Enter Attribute registers into environment buffer                         
  1349. */
  1350.  
  1351.           if (EnvBufferPassed)
  1352.  
  1353.             for (m = 0, 
  1354.                  k = Modes[Ind].ModeRegs[i].RegData.FirstEntry;
  1355.                  m < Modes[Ind].ModeRegs[i].RegData.NumEntries; 
  1356.                  k++, m++)
  1357.  
  1358.                 Environment->Hardware.Attributes.All[k] = 
  1359.                 Modes[Ind].ModeRegs[i].RegData.DataArea[k];
  1360.  
  1361.           break;
  1362.  
  1363. /*
  1364. **  Set the sequencer registers                                               
  1365. */
  1366.  
  1367.         case  Sequencers_CMD :
  1368.  
  1369.           if (Flags&UPDATE_HARDWARE)
  1370.           {
  1371.             RegAddress.AddressPort = SeqAddressPort;
  1372.             RegAddress.DataPort = SeqDataPort;
  1373.             RegAddress.ColorAdjust = NONE;
  1374.             RegAddress.Flags = Sequencers_CMD;
  1375.           } 
  1376.  
  1377. /*
  1378. **  Enter Sequencer registers into environment buffer                         
  1379. */
  1380.  
  1381.           if (EnvBufferPassed)
  1382.  
  1383.             for (m = 0, 
  1384.                  k = Modes[Ind].ModeRegs[i].RegData.FirstEntry;
  1385.                  m < Modes[Ind].ModeRegs[i].RegData.NumEntries; 
  1386.                  k++, m++)
  1387.  
  1388.               Environment->Hardware.Sequencers.All[k] = 
  1389.               Modes[Ind].ModeRegs[i].RegData.DataArea[k];
  1390.  
  1391.           break;
  1392.  
  1393. /*
  1394. **  Set the graphics registers                                                
  1395. */
  1396.  
  1397.         case  Graphics_CMD :
  1398.  
  1399.           if (Flags&UPDATE_HARDWARE)
  1400.           {
  1401.             RegAddress.AddressPort = GraphAddressPort;
  1402.             RegAddress.DataPort = GraphDataPort;
  1403.             RegAddress.ColorAdjust = NONE;
  1404.             RegAddress.Flags = NONE;
  1405.           } 
  1406.  
  1407. /*
  1408. **  Enter Graphics registers into environment buffer                          
  1409. */
  1410.  
  1411.           if (EnvBufferPassed)
  1412.  
  1413.             for (m = 0, 
  1414.                  k = Modes[Ind].ModeRegs[i].RegData.FirstEntry;
  1415.                  m < Modes[Ind].ModeRegs[i].RegData.NumEntries; 
  1416.                  k++, m++)
  1417.  
  1418. #if      VDHVGA
  1419.  
  1420.               if (k >= 0x40)
  1421.                      Environment->Hardware.ExtVGARegs[k-0x40] =
  1422.                        Modes[Ind].ModeRegs[i].RegData.DataArea[m];
  1423.  
  1424.               else
  1425. #endif
  1426.                      Environment->Hardware.GraphicsRegs[k] = 
  1427.                        Modes[Ind].ModeRegs[i].RegData.DataArea[k];
  1428.           break;
  1429. #endif    
  1430.  
  1431. /*
  1432. **  Set the CRT registers                                                     
  1433. */
  1434.  
  1435.         case  CRTCtlRegs_CMD :
  1436.  
  1437.           if (Flags&UPDATE_HARDWARE)
  1438.           {
  1439.  
  1440. /*
  1441. **  If current cols = 132 then ensure proper register setup for 132 cols      
  1442. */
  1443.  
  1444. #if      VDHVGA                      /*@D1085,@MS00                        */
  1445.  
  1446.             if (Modes[Mode].ModeRegs[i].RegData.DataArea[0x01] == 131)
  1447.             {                          /*@D1085                             */
  1448.  
  1449. //                        if (!SVGAPresent)
  1450. //                        {                        /* @drw */
  1451.  
  1452.                 if (Environment->XGA_PRESENT == FALSE)  
  1453.                 {                      /*@D1452                             */
  1454.                   SET132(Environment->Comp_Reg, 1);/*@D1085                 */
  1455.                 }                      /* endif@D1452                       */
  1456. //                        }                        /* endif                             */
  1457.             }                          /* endif@D1085                       */
  1458. #endif    
  1459.             RegAddress.AddressPort = CRTAddressPort;
  1460.             RegAddress.DataPort = CRTDataPort;
  1461.             RegAddress.ColorAdjust = ColorAdjustment;
  1462.             RegAddress.Flags = NONE;
  1463.           } 
  1464.  
  1465. /*
  1466. **  Enter CRT registers into environment buffer                               
  1467. */
  1468.  
  1469.           if (EnvBufferPassed)
  1470.  
  1471.             for (m = 0, 
  1472.                  k = Modes[Ind].ModeRegs[i].RegData.FirstEntry;
  1473.                  m < Modes[Ind].ModeRegs[i].RegData.NumEntries; 
  1474.                  k++, m++)
  1475.  
  1476.                Environment->Hardware.CRTCtlRegs.All[k] =
  1477.                  Modes[Ind].ModeRegs[i].RegData.DataArea[k];
  1478.  
  1479.           break;
  1480. #endif    
  1481.  
  1482. /*
  1483. **  Set the specified register                                                
  1484. **  (Port address is in 'FirstEntry' field of RegData                         
  1485. */
  1486.  
  1487.         case  RegOutput_CMD :
  1488.  
  1489.           if (Flags&UPDATE_HARDWARE)
  1490.             RegAddress.DataPort = Modes[Ind].ModeRegs[i].RegData.FirstEntry;
  1491.  
  1492. #if      !(VDH8514A)                   /* @MS00 */
  1493.  
  1494.           if (EnvBufferPassed)
  1495.  
  1496.             switch (Modes[Ind].ModeRegs[i].RegData.FirstEntry)
  1497.             {
  1498.  
  1499. #if      VDHEGA        || VDHVGA     /* @MS00 */
  1500.  
  1501.               case  MiscOutputRegWrite:
  1502.                 Environment->Hardware.MiscOutputReg = 
  1503.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1504.                 break;
  1505. #endif    
  1506.  
  1507. #if      VDHEGA                      /* @MS00 */
  1508.  
  1509.               case  Graphics1PosReg:
  1510.                 Environment->Hardware.Graphics1Position = 
  1511.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1512.                 break;
  1513.  
  1514.               case  Graphics2PosReg :
  1515.                 Environment->Hardware.Graphics2Position = 
  1516.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1517.                 break;
  1518.  
  1519. #endif                               /* VDHEGA */
  1520.  
  1521. #if      VDHCGA
  1522.               case  CGAModeCtlReg :
  1523.                 Environment->Hardware.CGAModeControl = 
  1524.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1525.                 break;
  1526.  
  1527.               case  CGAColorSelReg :
  1528.                 Environment->Hardware.CGAColorSelect = 
  1529.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1530.                 break;
  1531.  
  1532. #endif    
  1533.  
  1534. #if      VDHMPA                      /* @MS00 */
  1535.  
  1536.               case  CRTCtlPort1 :
  1537.                 Environment->Hardware.MPAModeControl = 
  1538.                 *Modes[Ind].ModeRegs[i].RegData.DataArea;
  1539.                 break;
  1540.  
  1541. #endif    
  1542.             }                          /* End of inner Switch structure     */
  1543. #endif    
  1544.               break;
  1545.  
  1546. /*
  1547. **  Jump to a command under a different mode                                  
  1548. */
  1549.  
  1550.       }                                /* End of outer Switch structure     */
  1551.  
  1552.       if (Flags&UPDATE_HARDWARE)
  1553.       {
  1554.         if (Modes[Ind].ModeRegs[i].Command == RegOutput_CMD)
  1555.         {
  1556.  
  1557.           if (Modes[Ind].ModeRegs[i].RegData.NumEntries == SETWORD)
  1558.           {                            /*                               @@B1*/
  1559.             AccessRegister(&RegAddress, SETWORD, 
  1560.             Modes[Ind].ModeRegs[i].RegData.DataArea);      /*           @@B1*/
  1561.           } 
  1562.  
  1563.           else
  1564.           {                            /*                               @@B1*/
  1565.             AccessRegister(&RegAddress, SET, 
  1566.             Modes[Ind].ModeRegs[i].RegData.DataArea);       /*          @@B1*/
  1567.           }                            /*                               @@B1*/
  1568.  
  1569. #if      !(VDH8514A)                   /* @MS00 */
  1570.         } 
  1571.  
  1572.         else
  1573.         {
  1574.           AccessHardware(&RegAddress, BYTES, ColorMode, SET, 
  1575.           &Modes[Ind].ModeRegs[i].RegData);
  1576.  
  1577. #if      VDHEGA        || VDHVGA     /* @MS00 */
  1578.  
  1579. /*
  1580. **  Setting attribute registers on a non-VGA turns video on again             
  1581. **  Turn the video off once again to reduce further snow                      
  1582. */
  1583.  
  1584.           if (Modes[Ind].ModeRegs[i].Command == Attributes_CMD)
  1585.             AccessVideoEnable(ColorMode, SET, &VideoEnable);/* callgate     */
  1586. #endif    
  1587. #endif    
  1588.         } 
  1589.       } 
  1590.     } 
  1591.  
  1592. #if      VDHVGA                        /* @MS00 */
  1593.     CLUT = &MonoCLUT;                  /* Preset Mono CLUT as default for   */
  1594.                                        /* Mono modes                        */
  1595.     CLUTCount = MonoCLUTCount;         /* set size of this CLUT       @MS13 */
  1596.  
  1597.     if (Modes[Mode].fbType&NOT_MONO)
  1598.     {
  1599.       CLUT = &SumCLUT;                 /* Preset Summed CLUT as default for */
  1600.                                        /* Color modes @MS13                 */
  1601.       CLUTCount = SumCLUTCount;        /* set size of this CLUT      @MS13  */
  1602.  
  1603.       if (Modes[Mode].color == 8)
  1604.       {                                /* 256 color ?                @MS13  */
  1605.         CLUT = &SumCLUT256;            /* @MS13 */
  1606.         CLUTCount = SumCLUT256Count;   /* @MS13 */
  1607.       }                                /* @MS13 */
  1608.  
  1609.       else
  1610.  
  1611.         if (Modes[Mode].vres == 200)
  1612.         {                              /* low res ?                 @MS13  */
  1613.           CLUT = &SumCLUTLow;          /* @MS13 */
  1614.           CLUTCount = SumCLUTLowCount; /* @MS13 */
  1615.         }                              /* @MS13 */
  1616.  
  1617.       if ((!(VideoHardware.fVideoType&MONO_DISPLAYS))/* Not MONO displays   
  1618.                                                                    @T39,@T73*/
  1619.          && !(Flags&NO_CLR_BRST))
  1620.       {                                /* Not a B/W mode                    */
  1621.         CLUT = &ColorCLUT;             /* Use Color CLUT only for true      
  1622.                                           Color modes                       */
  1623.         CLUTCount = ColorCLUTCount;    /* set size of this CLUT     @MS13   */
  1624.  
  1625.         if (Modes[Mode].color == 8)
  1626.         {                              /* 256 color ?               @MS13   */
  1627.           CLUT = &ColorCLUT256;        /* @MS13 */
  1628.           CLUTCount = ColorCLUT256Count; /* @MS13 */
  1629.         }                              /* @MS13 */
  1630.  
  1631.         else
  1632.  
  1633.           if (Modes[Mode].vres == 200)
  1634.           {                            /* low res ?                  @MS13   */
  1635.             CLUT = &ColorCLUTLow;      /* @MS13 */
  1636.             CLUTCount = ColorCLUTLowCount; /* @MS13 */
  1637.           }                            /* @MS13 */
  1638.       } 
  1639.  
  1640.       else
  1641.       {                                /* set NO_CLR_BRST when using summed 
  1642.                                           color lookup table                */
  1643.  
  1644.         if (!(Environment->ModeData.fbType&GRAPHICS))
  1645.           Environment->ModeData.fbType |= NO_CLR_BRST;/*                @BB8*/
  1646.       } 
  1647.     } 
  1648.  
  1649.     if (Flags&UPDATE_HARDWARE)
  1650.     {
  1651.       ClutParms.DataArea = (UCHAR far *)CLUT;
  1652.       ClutParms.NumEntries = CLUTCount; /* @MS13 */
  1653.                                                                    /* @MS13 */
  1654.  
  1655.       for (i = 0; i < 0xFF+1; i += CLUTCount)
  1656.       {
  1657.         ClutParms.FirstEntry = i;
  1658.         AccessCLUT(SET, (CLUTDATA far *far *)&ClutParms);
  1659.       } 
  1660.  
  1661.       SetTextGrays(Environment);       /* @MS13 */
  1662.     } 
  1663.  
  1664. /*
  1665. **  Enter color lookup table into environment buffer                          
  1666. */
  1667.  
  1668.     if (EnvBufferPassed)
  1669.     {
  1670.  
  1671.       for (i = 0x00; i < 0xFF+1; )
  1672.       {
  1673.  
  1674.         for (j = 0x00; j < 0x3F+1; i++, j++)
  1675.         {
  1676.           Environment->LookupTable[i] = CLUT[j];
  1677.         } 
  1678.       } 
  1679.     } 
  1680.  
  1681. #endif    
  1682.  
  1683.     rc = NO_ERROR;                     /*                               @T53*/
  1684.  
  1685. #if      FONT_SUPPORT                  /* @MS00 */
  1686.  
  1687. /*
  1688. **  Load the appropriate font                                                 
  1689. */
  1690.  
  1691.     if (Flags&UPDATE_HARDWARE)
  1692.       rc = SetHWFont(Environment, Environment->ActiveFontPTR, 
  1693.            (UCHAR far *) PHYSICAL_FONT_BUFFER);        /*               @T53*/
  1694.  
  1695. #endif    
  1696.  
  1697. #if      !(VDH8514A)                   /* @MS00 */
  1698.  
  1699. /*
  1700. **  Turn the video signal back on                                             
  1701. */
  1702.  
  1703.     if ((Flags&UPDATE_HARDWARE) && VideoOn)
  1704.     {                                  /*                               @C21*/
  1705.       VideoEnable = 1;                 /* Turn video on                     */
  1706.       AccessVideoEnable(ColorMode, SET, &VideoEnable);/* ring 2 callgate    */
  1707.     } 
  1708.  
  1709. #endif    
  1710.  
  1711.     return (rc);                       /*                               @T53*/
  1712.   } 
  1713.  
  1714. /*****************************************************************************
  1715.  *                                                                          
  1716.  *  SUBROUTINE NAME: SetEnvMode                                             
  1717.  *                                                                          
  1718.  *  DESCRIPTIVE NAME: Set the envrionment mode                              
  1719.  *                                                                          
  1720.  *  FUNCTION: SetEnvMode is called to update the environment buffer         
  1721.  *            with the current video mode.                                  
  1722.  *                                                                          
  1723.  *  ENTRY POINT: SetEnvMode                                                 
  1724.  *    LINKAGE:   CALL FAR                                                   
  1725.  *                                                                          
  1726.  *  INPUT: (Passed on stack)                                                
  1727.  *             UCHAR Mode ( Index in table of supported modes )             
  1728.  *             FAR * Environment ( far pointer to environment buffer )      
  1729.  *             FAR * HWEnvironment (pointer to hardware state buffer)       
  1730.  *         (Referenced)                                                     
  1731.  *             Modes[] (global data - table of supported video modes )      
  1732.  *                                                                          
  1733.  *  EXIT-NORMAL: Environment buffer is updated to reflect HW state          
  1734.  *                                                                          
  1735.  *  EFFECTS: NONE                                                           
  1736.  *                                                                          
  1737.  *  INTERNAL REFERENCES:                                                    
  1738.  *    ROUTINES: NONE                                                        
  1739.  *                                                                          
  1740.  *  EXTERNAL REFERENCES:                                                    
  1741.  *    ROUTINES: NONE                                                        
  1742.  *                                                                          
  1743.  ****************************************************************************/
  1744.  
  1745.  
  1746.   void PASCAL near SetEnvMode(Mode, Environment, HWEnvironment, GetMode)
  1747.                                                               
  1748.   UCHAR Mode;
  1749.   ENVIRONMENT far *Environment;
  1750.   ENVIRONMENT far *HWEnvironment;
  1751.   USHORT GetMode;                      /*                               @T70*/
  1752.  
  1753.   {
  1754.     int i;
  1755.     UCHAR far *Source;
  1756.     UCHAR far *Destination;
  1757.     VIDEOMODE far *pCurMode;           /* Reduce level of indirection,   @S8*/
  1758.  
  1759. #if      !(VDH8514A)                   /* @MS00 */
  1760.  
  1761. /*
  1762. **  Update environment buffer with current hardware state                     
  1763. */
  1764.  
  1765.     if (HWEnvironment)
  1766.     {
  1767.       Source = (UCHAR far *)&HWEnvironment->Hardware;
  1768.       Destination = (UCHAR far *)&Environment->Hardware;
  1769.  
  1770.       for (i = 0; i < sizeof(HWREGS); i++)
  1771.       {
  1772.         Destination[i] = Source[i];
  1773.       } 
  1774.     } 
  1775.  
  1776. #endif    
  1777.  
  1778.     if (GetMode)
  1779.     {                                  /*                               @T70*/
  1780.  
  1781.       pCurMode = (VIDEOMODE far *)&Modes[Environment->ModeIndex = Mode].cb;
  1782.       Environment->ModeData.cb = pCurMode->cb;
  1783.       Environment->ModeData.fbType = pCurMode->fbType;
  1784.       Environment->ModeData.color = pCurMode->color;
  1785.       Environment->ModeData.col = pCurMode->col;
  1786.       Environment->ModeData.row = pCurMode->row;
  1787.       Environment->ModeData.hres = pCurMode->hres;
  1788.       Environment->ModeData.vres = pCurMode->vres;
  1789.       Environment->ModeData.fmt_ID = pCurMode->fmt_ID;
  1790.       Environment->ModeData.attrib = pCurMode->attrib;
  1791.  
  1792.       Environment->ModeData.BufferAddress = 
  1793.         MemoryMaps[pCurMode->MemMap].Start.FullAddress;
  1794.  
  1795.       Environment->ModeData.BufferLength = 
  1796.         MemoryMaps[pCurMode->MemMap].TotalSize;
  1797.  
  1798.       if (pCurMode->MemMap == MemMap_LoRes)  /*                        @@TB9*/
  1799.         Environment->ModeData.BufferLength = 16000;
  1800.  
  1801.       Environment->ModeData.FullBufferSize = 
  1802.          Environment->ModeData.BufferLength;
  1803.  
  1804.       Environment->ModeData.PartialBufferSize = (ULONG)4000;
  1805.       Environment->ModeData.ExtDataArea = (UCHAR far *)NULL;
  1806.  
  1807. #if      FONT_SUPPORT                  /* @MS00 */
  1808.  
  1809.       if (!(pCurMode->fbType&GRAPHICS))
  1810.       {
  1811.         Environment->ModeData.FullBufferSize += (ULONG)256*(ULONG)
  1812.            (Environment->ModeData.vres/Environment->ModeData.row);
  1813.       } 
  1814.  
  1815.       Environment->ROMFontIndex = pCurMode->ROMFontIndex;
  1816.  
  1817. #endif    
  1818.     }                                  /*                               @T70*/
  1819.   } 
  1820.  
  1821. #if      VDHVGA                        /* Read/write hardware               */
  1822.                                         /* @MS00 */
  1823.  
  1824. /*****************************************************************************
  1825.  *                                                                             
  1826.  *  SUBROUTINE NAME: GetModeIndex                                              
  1827.  *                                                                             
  1828.  *  DESCRIPTIVE NAME: Determine video mode from registers                      
  1829.  *                                                                             
  1830.  *  FUNCTION: GetModeIndex returns the index in the table of modes             
  1831.  *            that corresponds to the current hardware setting.                
  1832.  *                                                                             
  1833.  *  ENTRY POINT: GetModeIndex                                                  
  1834.  *    LINKAGE:   CALL FAR                                                      
  1835.  *                                                                             
  1836.  *  INPUT: (Passed on stack)                                                   
  1837.  *             FAR * Environment ( Registers that have been read )             
  1838.  *         (Referenced)                                                        
  1839.  *             Modes[] ( table of supported video modes )                      
  1840.  *                                                                             
  1841.  *  EXIT-NORMAL: AX = mode table index + 1                                     
  1842.  *                                                                             
  1843.  *  EXIT-ERROR:  AX = 0                                                        
  1844.  *                                                                             
  1845.  *  EFFECTS: NONE                                                              
  1846.  *                                                                             
  1847.  *  INTERNAL REFERENCES:                                                       
  1848.  *    ROUTINES: NONE                                                           
  1849.  *                                                                             
  1850.  *  EXTERNAL REFERENCES:                                                       
  1851.  *    ROUTINES: AccessCLUT                                                     
  1852.  *                                                                             
  1853.  ****************************************************************************/
  1854.  
  1855.  
  1856.   USHORT PASCAL near GetModeIndex(Environment)/*                        @B15*/
  1857.  
  1858.   ENVIRONMENT far *Environment;
  1859.   {
  1860.     USHORT i,MODE_FOUND;
  1861.     VIDEOMODE far *pCurMode;           /* Reduce level of indirection,   @S8*/
  1862.     FarAddress PhysAddress1;           /*                               @C29*/
  1863.     VIDEO_BIOS far *BiosData;          /*                               @C29*/
  1864.  
  1865. /*
  1866. **  An examination of just 4 registers is all that is needed to determine   
  1867. **  which of the supported mode is currently set.  If an unknown mode is    
  1868. **  set, an error is returned.  The registers examined are:                 
  1869. **                  - Miscellaneous Output Register                         
  1870. **                  - Attribute Mode Control Register                       
  1871. **                  - Attribute Color Plane Enable Register                 
  1872. **                  - Sequencer Clocking Mode Register                      
  1873. */
  1874.                                                                             
  1875.  
  1876.     for (i = 0, MODE_FOUND = FALSE; (i < NUM_MODES) && !MODE_FOUND; i++)
  1877.     {                                  /* @MS12 */
  1878.  
  1879.       pCurMode = (VIDEOMODE far *)&Modes[i].cb;/*                        @S8*/
  1880.  
  1881.       if ((Environment->Hardware.MiscOutputReg == pCurMode->CompRegs[0]) && 
  1882.          ((Environment->Hardware.Attributes.Regs.ModeControl&~BLINK_BIT) == 
  1883.          (pCurMode->CompRegs[1]&~BLINK_BIT)) && 
  1884.          (Environment->Hardware.Attributes.Regs.ColorPlane == 
  1885.          pCurMode->CompRegs[2]) && 
  1886.          (Environment->Hardware.Sequencers.Regs.ClockingMode == 
  1887.          pCurMode->CompRegs[3]))
  1888.  
  1889.         MODE_FOUND = TRUE;
  1890.     } 
  1891.  
  1892. /*
  1893. **  If no mode is found, if Protect mode use modeindex from environment
  1894. **  ELSE GET MODE FROM BIOS DATA AREA                                     
  1895. */
  1896.  
  1897.     if (!MODE_FOUND)
  1898.     {                                  /*                          @C29START*/
  1899.       i = Environment->ModeIndex;
  1900.       i++;
  1901.     }                                  /*                            @C29END*/
  1902.  
  1903.     if ((i == ModeIndex_VGC7p+1) &&    /*                               @T71*/
  1904.        (VideoHardware.fVideoType&VGM_BIT))
  1905.     {                                  /*                               @T71*/
  1906.       i++;                             /* Return mono mode index on a mono  
  1907.                                           plasma display                @T71*/
  1908.     }                                  /*                               @T71*/
  1909.  
  1910.     return (i);
  1911.   } 
  1912.  
  1913. #endif    
  1914.  
  1915.  
  1916.