home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / SCEQUIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-13  |  9.1 KB  |  295 lines

  1. /**
  2. *
  3. * Name        scequip -- Obtain video hardware environment & settings
  4. *
  5. * Synopsis    model = scequip();
  6. *
  7. *        char model     IBM Model code:
  8. *                  IBM_PC (0xff) if IBM PC
  9. *                  IBM_XT (0xfe) if IBM PC-XT
  10. *                        or Portable PC
  11. *                  IBM_JR (0xfd) if IBM PCjr
  12. *                  IBM_AT (0xfc) if IBM PC-AT
  13. *                  IBM_CV (0xf9) if IBM PC Convertible
  14. *
  15. * Description    This function obtains information about the video
  16. *        adapters installed on this machine and their options.
  17. *        All of the results are available in global variables
  18. *        declared in BSCREEN.H, except for b_pcmodel, which is
  19. *        declared in BQUERY.H.
  20. *
  21. *        This function should need to be called only once during
  22. *        the execution of a program.  (If it is called again, it
  23. *        will examine the b_know_hw flag and skip most of its
  24. *        operations to save time.)
  25. *
  26. *        Use SCMODE and SCROWS to obtain video information which
  27. *        might be altered under software control (such as video
  28. *        mode or number of columns or lines).  Use SCNEWDEV or
  29. *        SCCHGDEV to switch to a given video adapter.
  30. *
  31. * Results    model        IBM Model code:
  32. *                  IBM_PC (0xff) if IBM PC
  33. *                  IBM_XT (0xfe) if IBM PC-XT
  34. *                        or Portable PC
  35. *                  IBM_JR (0xfd) if IBM PCjr
  36. *                  IBM_AT (0xfc) if IBM PC-AT
  37. *                  IBM_CV (0xf9) if IBM PC Convertible
  38. *        (Also sets various global variables in BSCREEN.H)
  39. *
  40. *
  41. * Version    3.0  (C)Copyright Blaise Computing Inc. 1986
  42. *
  43. * Version    3.02 March 24, 1987
  44. *        Removed a strong typing warning (no change in object
  45. *            code).
  46. *
  47. **/
  48.  
  49. #include <bscreen.h>
  50. #include <bquery.h>
  51.  
  52. #if LAT300
  53. #include <dos.h>
  54. #endif
  55. #if MSC300
  56. #include <conio.h>
  57. #endif
  58.  
  59. int b_know_hw = 0;          /* Flag stating whether we have yet     */
  60.                   /*     run EQINIT:  0 if no, 1 if yes   */
  61.  
  62. int b_mdpa = DONT_KNOW;       /* Monochrome Display & Printer Adapter */
  63.                   /*   DONT_KNOW, ABSENT, or MONO          */
  64.  
  65. int b_cga = DONT_KNOW;          /* Color/Graphics Monitor Adapter       */
  66.                   /*   DONT_KNOW, ABSENT, or COLOR          */
  67.  
  68. int b_ega = DONT_KNOW;          /* Enhanced Graphics Adapter          */
  69.                   /*   DONT_KNOW, ABSENT, MONO or COLOR   */
  70.  
  71. int b_pgc = DONT_KNOW;          /* Professional Graphics Controller     */
  72.                   /*   DONT_KNOW, ABSENT, COLOR or HIFUNC */
  73.  
  74. int b_mem_ega = 0;          /* Amount of memory installed on EGA in */
  75.                   /* 1024-byte units:  64, 128, 192, 256  */
  76.  
  77. unsigned b_sw_ega = 0x0ffff;  /* Switch settings on EGA           */
  78.                   /* Low-order bit set means SW1 off, etc.*/
  79.                   /* (Bit value 1 implies that          */
  80.                   /* corresponding switch is off.)          */
  81.  
  82. static int adap_present(int);          /* Internal function          */
  83. static int pgc_present(void);          /* Internal function          */
  84. static int pgc_emulating(void);       /* Internal function          */
  85.  
  86. char scequip()
  87. {
  88.     int        ax,bx,cx,dx,flags;     /* Registers for BIOS call      */
  89.     unsigned   color_or_mono,mem;
  90.     int        pgc_mode;
  91.  
  92.     if (b_know_hw)
  93.     return b_pcmodel;     /* No need to do this work again          */
  94.  
  95.     if (qymodel() == IBM_JR)          /* Fetch & save model code.     */
  96.     {                      /* This is a PCjr           */
  97.     b_mdpa = ABSENT;
  98.     b_cga  = ABSENT;
  99.     b_ega  = ABSENT;
  100.     b_pgc  = ABSENT;
  101.     }
  102.     else if (b_pcmodel == IBM_CV)
  103.     {                      /* IBM PC Convertible acts like */
  104.     b_mdpa = MONO;              /* both MDPA & CGA.          */
  105.     b_cga  = COLOR;
  106.     b_ega  = ABSENT;
  107.     b_pgc  = ABSENT;
  108.     }
  109.     else
  110.     {                      /* Neither PCjr nor Convertible */
  111.  
  112.     /* First test for the presence of an EGA:  attempt BIOS video     */
  113.     /* function 18, subfunction BL=0x10 ("Return EGA Information").   */
  114.     /* If any information returned is invalid, then the EGA must be   */
  115.     /* absent.                                  */
  116.  
  117.     ax = utbyword(18,0);
  118.     bx = (int) 0xff10;
  119.     cx = 0x000f;
  120.     bios(16,&ax,&bx,&cx,&dx,&flags);
  121.     color_or_mono = uthibyte(bx);
  122.     mem          = utlobyte(bx);
  123.     b_sw_ega      = utlobyte(cx);
  124.  
  125.     if (b_sw_ega >= 12 || color_or_mono > 1 || mem > 3)
  126.         b_ega     = ABSENT;       /* Invalid value(s) found       */
  127.     else
  128.     {                  /* EGA is present           */
  129.         b_ega     = (color_or_mono ? MONO : COLOR);
  130.         b_mem_ega = 64 * (mem + 1);
  131.     }
  132.  
  133.     /* If the EGA is present, the color_or_mono flag says whether     */
  134.     /* it is emulating the MDPA or the CGA; if so, then the emulated  */
  135.     /* board cannot itself be present, but the other might be.          */
  136.  
  137.     if (b_ega == MONO
  138.         || !adap_present(0))           /* Sense mono adapter  */
  139.         b_mdpa = ABSENT;
  140.     else
  141.         b_mdpa = MONO;
  142.  
  143.     /* Further, if we sense a color adapter, then exactly one of the  */
  144.     /* following conditions must be true:  EGA is in color mode       */
  145.     /* (detected above from color_or_mono flag); PGC is emulating the */
  146.     /* CGA; or a genuine CGA is present.                  */
  147.  
  148.     pgc_mode = DONT_KNOW;
  149.     if (b_ega == COLOR
  150.         || (!adap_present(1))           /* Sense color adapter */
  151.         || (pgc_mode = pgc_emulating()) == COLOR)
  152.         b_cga = ABSENT;
  153.     else
  154.         b_cga = COLOR;
  155.  
  156.     /* If the PGC is present but not emulating the CGA, then it must  */
  157.     /* be in its high-function graphics mode.                  */
  158.  
  159.     if (pgc_mode == COLOR)
  160.         b_pgc = COLOR;
  161.     else
  162.         b_pgc = (pgc_present() ? HIFUNC : ABSENT);
  163.     }
  164.  
  165.     b_know_hw = 1;
  166.     return b_pcmodel;
  167. }
  168.  
  169. /**
  170. *
  171. * Name        adap_present -- Test for Monochrome or Color/Graphics Adapter
  172. *                or emulator
  173. *
  174. * Synopsis    present = adap_present(adapter);
  175. *        int present        1 if present or being emulated,
  176. *                    0 if not.
  177. *        int adapter        0 if testing for Monochrome Adapter,
  178. *                    1 if Color/Graphics Adapter
  179. *
  180. * Description    This function tests for the presence of the IBM
  181. *        Monochrome Display and Printer Adapter or the IBM
  182. *        Color/Graphics Adapter or other device (such as the EGA
  183. *        or PGC) that may be emulating them.
  184. *
  185. * Method    Write a bogus value into the cursor position register of
  186. *        the CRT controller chip, then read it back again.  If
  187. *        the same value is found in the register, then the CRTC
  188. *        must be present; if the value is different, then it must
  189. *        be absent.
  190. *
  191. * Result    present         1 if present or being emulated,
  192. *                    0 if not.
  193. *
  194. **/
  195.  
  196. static int adap_present(adapter)
  197. int adapter;
  198. {
  199.     int save_cursor,crtc_there;
  200.     unsigned int port;          /* Address of the CRT controller chip   */
  201.  
  202.     port = (adapter ? 0x03d4 : 0x03b4);
  203.  
  204.     outp(port++,0xf);              /* Set cursor register          */
  205.  
  206.     save_cursor = inp(port);          /* Save previous contents       */
  207.  
  208.     outp(port,0x5a);              /* Set to column 90          */
  209.     crtc_there = (inp(port) == 0x5a); /* Read the value back again    */
  210.  
  211.     outp(port,save_cursor);          /* Restore previous contents    */
  212.     return crtc_there;
  213. }
  214.  
  215. /**
  216. *
  217. * Name        pgc_present -- Test for IBM Professional Graphics Controller
  218. *
  219. * Synopsis    present = pgc_present();
  220. *        int present        1 if present, 0 if not.
  221. *
  222. * Description    This function tests for the presence of the IBM
  223. *        Professional Graphics Controller.
  224. *
  225. * Method    Write a bogus value into the supposed PGC, then read it
  226. *        back again.  If the same value is found, then the PGC
  227. *        must be present; if the value is different, then it must
  228. *        be absent.
  229. *
  230. * Result    present         1 if present, 0 if not.
  231. *
  232. **/
  233.  
  234. static int pgc_present()
  235. {
  236.     static ADS pgc_ads = {0x03db,0xc600};
  237.     char       save,dummy;
  238.     ADS        save_ads,dummy_ads;
  239.  
  240.     utabsptr(&save,&save_ads);
  241.     utslmove(&pgc_ads,&save_ads,1);    /* Save previous contents     */
  242.  
  243.     dummy = 0x5a;
  244.     utabsptr(&dummy,&dummy_ads);
  245.     utslmove(&dummy_ads,&pgc_ads,1);    /* Store dummy value          */
  246.     utslmove(&pgc_ads,&dummy_ads,1);    /* Read it back again          */
  247.  
  248.     utslmove(&save_ads,&pgc_ads,1);    /* Restore previous contents  */
  249.     return (dummy == 0x5a);
  250. }
  251.  
  252. /**
  253. *
  254. * Name        pgc_emulating -- Test for whether IBM Professional
  255. *            Graphics Controller is emulating the IBM Color/Graphics
  256. *            Adapter
  257. *
  258. * Synopsis    emulating = pgc_emulating();
  259. *        int emulating        COLOR  if PGC emulating the CGA,
  260. *                    HIFUNC if not.
  261. *
  262. * Description    This function tests for whether the IBM Professional
  263. *        Graphics Controller is emulating the IBM Color/Graphics
  264. *        Adapter.
  265. *
  266. * Method    Write a bogus value into the PGC through a memory
  267. *        address, then read it back again through an input port.
  268. *        If the same value is found, then the PGC must be present
  269. *        and in CGA emulation mode; if the value is different,
  270. *        then (if the PGC is indeed present) it must be in
  271. *        high-function graphics mode.
  272. *
  273. * Result    present         COLOR  if PGC emulating the CGA,
  274. *                    HIFUNC if not.
  275. *
  276. **/
  277.  
  278. static int pgc_emulating()
  279. {
  280.     static ADS pgc_ads = {0x03d4,0xc600};
  281.     char       save,dummy;
  282.     ADS        save_ads,dummy_ads;
  283.  
  284.     utabsptr(&save,&save_ads);
  285.     utslmove(&pgc_ads,&save_ads,1);    /* Save previous contents     */
  286.  
  287.     dummy = 0x28;
  288.     utabsptr(&dummy,&dummy_ads);
  289.     utslmove(&dummy_ads,&pgc_ads,1);    /* Store dummy value          */
  290.     dummy = (char) inp(0x3d4);        /* Read it back again          */
  291.     utslmove(&save_ads,&pgc_ads,1);    /* Restore previous contents  */
  292.  
  293.     return (dummy == 0x28) ? COLOR : HIFUNC;
  294. }
  295.