home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / pcw_c.zip / PCWKERN.C < prev    next >
C/C++ Source or Header  |  1991-08-17  |  13KB  |  321 lines

  1. /***********************************************************/
  2. /* File Id.                  Pckern.C                      */
  3. /* Author.                   Stan Milam.                   */
  4. /* Date Written.             01/29/89.                     */
  5. /* Date Modified.            05/26/89.                     */
  6. /*                                                         */
  7. /*           (c) Copyright 1989-90 by Stan Milam           */
  8. /*                                                         */
  9. /* Modification Notes:  Rewrote initialization logic.  Add-*/
  10. /* ed global variables, _monitor, _adaptor, _video_ram.    */
  11. /* Added function to check video state & return max row &  */
  12. /* columns.                                                */
  13. /*                                                         */
  14. /* Comments:  This file will be the kernal of the Pcscrn   */
  15. /* library functions.  It will contain code to deal with   */
  16. /* the video environment.  Most important is the start up  */
  17. /* initialization code.  This code will determine the      */
  18. /* video adaptors on board.                                */
  19. /***********************************************************/
  20.  
  21. #ifdef MSC
  22. #  include <conio.h>
  23. #  define MK_FP(seg,off) ((void far *)(((long)(seg)<<16) | (off)))
  24. #  define outportb outp
  25. #  define inportb inp
  26. #endif
  27.  
  28. #include <dos.h>
  29. #include <stdlib.h>
  30.  
  31. #define TRUE  1
  32. #define FALSE 0
  33.  
  34. #define COLORSEG  0XB800
  35. #define MONOSEG   0XB000
  36. #define MONO      0
  37. #define COLOR     1
  38.  
  39. #define MDA      1
  40. #define CGA      2
  41. #define EGA      3
  42. #define VGA      4
  43.  
  44. char PCWRKSTR[151];
  45.  
  46. void vgetmode(int *cols, int *mode, int *apage);
  47. void _pcw_exit(void);
  48.  
  49. /* Global Variables */
  50.  
  51. int      CheckSnow;                    /* Do we need to check for snow? */
  52. int      Vbump;                        /* Vertical increment in quick writes */
  53. int      _monitor;                     /* The kind of monitor we are using */
  54. int      _adaptor;                     /* The video adaptor we are using */
  55. int      _video_ram;                   /* How much video memory */
  56.  
  57. /*  Some pointers to low memory where vital info is to be found */
  58.  
  59. static char far *ega_byte;
  60. static int  far *regen_len;
  61. static char far *ega_rows;
  62. static char *_pcw_copyright;
  63.  
  64. /*  Values stored in this file to be used throughout the library */
  65.  
  66. static unsigned ScrnSeg;
  67. static int      VideoPage;             /* Active Video page for library */
  68. static int      SavedMode;             /* Video Mode when called */
  69. static char     Init_Called= 0;        /* Was initialization done? */
  70.  
  71. /***********************************************************/
  72. /* Functions in this file:                                 */
  73. /* pcwinit() - Initializes the library.                    */
  74. /* getpage() - Returns video page library function use.    */
  75. /* setpage() - Sets library video page                     */
  76. /* switchpage() - Uses BIOS to switch active video page    */
  77. /* getscrnseg() - Returns active screen segment.           */
  78. /* getpagesize()- Returns current size of video page       */
  79. /***********************************************************/
  80.  
  81.  
  82. /***********************************************************/
  83. /*                           PCWINIT                       */
  84. /*                                                         */
  85. /* This function must be called to use any of the quick    */
  86. /* screen writing functions or the window functions. This  */
  87. /* is because we have to determine some basic info about   */
  88. /* the video environment.                                  */
  89. /* If called with a zero value before any other PCW call   */
  90. /* the automatic exit will not be established.  Automatic  */
  91. /* exit is the default.                                    */
  92. /***********************************************************/
  93.  
  94. void pcwinit(int action) {
  95.  
  96.      union REGS regs;                       /* For video interrupts */
  97.  
  98.      Init_Called    = (char) TRUE;
  99.      ega_byte       = (char far *) MK_FP(0x0000, 0x0487);
  100.      regen_len      = (int  far *) MK_FP(0x0000, 0x044c);
  101.      ega_rows       = (char far *) MK_FP(0x0000, 0x0484);
  102.      _pcw_copyright = "PC Windows (c) Copyright 1989-90 by Stan Milam";
  103.  
  104.      regs.x.ax = 0x1a00;                         /* Check for VGA */
  105.      int86(0x10, ®s, ®s);                  /* Invoke BIOS */
  106.      if (regs.h.al == 0x1a) {                    /* If VGA BIOS supported */
  107.         _adaptor  = VGA;                         /* Tell the world about it */
  108.         CheckSnow = FALSE;                       /* Don't need this */
  109.         if (regs.h.bl >= 7) {                    /* Determine the monitor */
  110.            switch(regs.h.bl) {
  111.               case  7 :
  112.               case 11 :
  113.                    _monitor = MONO;
  114.                    ScrnSeg  = MONOSEG;
  115.                    break;
  116.               case  8 :
  117.               case 10 :
  118.               case 12 :
  119.                    _monitor = COLOR;
  120.                    ScrnSeg  = COLORSEG;
  121.                    break;
  122.            }
  123.         }
  124.         regs.x.ax = 0x1200;                      /* Get amount of video ram */
  125.         regs.h.bl = 0x10;
  126.         int86(0x10,®s,®s);                 /* Ask the BIOS */
  127.         switch(regs.h.bl) {
  128.           case 0 : _video_ram =  64; break;
  129.           case 1 : _video_ram = 128; break;
  130.           case 2 : _video_ram = 192; break;
  131.           case 3 : _video_ram = 256; break;
  132.         }
  133.      }
  134.  
  135.      if (!_adaptor) {                            /* If no VGA */
  136.         regs.x.ax = 0x1200;                      /* Check for EGA */
  137.         regs.h.bl = 0x10;
  138.         int86(0x10,®s,®s);                 /* EGA BIOS supported? */
  139.         if (regs.h.bl == 0x10) {                 /* Not EGA or VGA */
  140.            int86(0x11,®s,®s);              /* Get BIOS Equip List */
  141.            if ((regs.x.ax & 0x0030) == 0x0030) { /* Check for MONO */
  142.               ScrnSeg   = MONOSEG;
  143.               _adaptor  = MDA;
  144.               _monitor  = MONO;
  145.               _video_ram= 4;
  146.               CheckSnow = FALSE;
  147.            }
  148.            else {                                /* Must be CGA */
  149.               ScrnSeg   = COLORSEG;
  150.               _adaptor  = CGA;
  151.               _monitor  = COLOR;
  152.               CheckSnow = TRUE;
  153.               _video_ram= 16;
  154.            }
  155.         }
  156.         else {                                   /* Must be an EGA */
  157.            CheckSnow = FALSE;
  158.            _adaptor  = EGA;
  159.            switch(regs.h.bh) {                   /* Which Monitor? */
  160.              case 0 :                            /* Color */
  161.                 ScrnSeg = COLORSEG;
  162.                 _monitor= COLOR;
  163.                 break;
  164.              case 1 :                            /* Mono */
  165.                 ScrnSeg = MONOSEG;
  166.                 _monitor= MONO;
  167.                 break;
  168.            }
  169.            switch(regs.h.bl) {                   /* How Much memory? */
  170.              case 0 : _video_ram =  64; break;
  171.              case 1 : _video_ram = 128; break;
  172.              case 2 : _video_ram = 192; break;
  173.              case 3 : _video_ram = 256; break;
  174.            }
  175.         }
  176.      }
  177.      if (_adaptor > CGA) {                        /* Set alternate print */
  178.         regs.x.ax = 0x1200;                       /* screen to handle screen*/
  179.         regs.x.bx = 0x0020;                       /* sizes > 25 lines */
  180.         int86(0x10,®s,®s);
  181.      }
  182.      if (action) atexit(_pcw_exit);            /* Set up automatic exit */
  183.      vgetmode(&Vbump, &SavedMode, &VideoPage); /* Get initial info */
  184.      Vbump *= 2;                               /* Establish Vertical */
  185. }
  186.  
  187. /***********************************************************/
  188. /*                                                         */
  189. /* Now some miscellanious functions that will be called    */
  190. /* from the library functions to retrieve some of the      */
  191. /* values stored here in this file.                        */
  192. /*                                                         */
  193. /***********************************************************/
  194.  
  195. /***********************************************************/
  196. /*                      Chk_Video_State                    */
  197. /*                                                         */
  198. /* Determine if we are in a valid text mode to do screen   */
  199. /* writes.  Also return the number of colums & rows.       */
  200. /***********************************************************/
  201.  
  202.  
  203. int chk_video_state(int *max_rows, int *max_cols) {
  204.  
  205.    static int cols, mode, ap, rc;
  206.  
  207.    if (!Init_Called) pcwinit(1);
  208.    vgetmode(&cols, &mode, &ap);
  209.    Vbump = cols * 2;
  210.    rc = !((mode > 3 && mode < 7) || mode > 7);
  211.    if (_adaptor < EGA) *max_rows = 25;
  212.    else *max_rows = *ega_rows + 1;
  213.    *max_cols = cols;
  214.    return(rc);
  215. }
  216.  
  217.  
  218. /* Get the videopage the library is working in now  */
  219.  
  220. int getpage(void) {
  221.  
  222.     return(VideoPage);
  223. }
  224.  
  225. /* Get the Screen Memory Segment */
  226.  
  227. unsigned getscrnseg(void) {
  228.  
  229.    return(ScrnSeg);
  230. }
  231.  
  232. /* Return the pagesize to whoever wants to know */
  233.  
  234. int getpagesize(void) {
  235.  
  236.     return(*regen_len);
  237. }
  238.  
  239. /* Function to return Init_Called - Used to determine if   */
  240. /* initialization routine was called.  Library function    */
  241. /* will not work if it returns NULL                        */
  242.  
  243. int ispcwinit(void) {
  244.  
  245.     return((int) Init_Called);
  246. }
  247.  
  248. /***********************************************************/
  249. /*                         SetPage                         */
  250. /*                                                         */
  251. /* Set the video page for the library functions to work in.*/
  252. /* First edit to see if the specified page is legal.       */
  253. /* Return NULL if the specified page was illegal.          */
  254. /***********************************************************/
  255.  
  256. int setpage(int page) {
  257.  
  258.     int cols, mode, apage;
  259.  
  260.     if (!ispcwinit()) pcwinit(1);
  261.     if (page < 0 || _adaptor == MDA) return(0); /* No Mono here! */
  262.     vgetmode(&cols, &mode, &apage);         /* Get video Mode */
  263.     if (mode > 3) return(0);                /* Not a valid text mode */
  264.     if (mode < 2) {                         /* 40 column text mode */
  265.        if (_adaptor > CGA) {                /* When EGA we have... */
  266.           if (page > 15) return(0);         /* 15 pages-no more! */
  267.           else VideoPage = page;            /* Set the page */
  268.        }
  269.        else {                               /* Else we are CGA */
  270.           if (page > 7) return(0);          /* And we have 8 pages */
  271.           else VideoPage = page;
  272.        }
  273.     }
  274.     else {                                  /* Okay-80 column text mode */
  275.        if (_adaptor > CGA) {                /* And when its an EGA */
  276.           if (page > 7) return(0);          /* We have 8 pages */
  277.           else VideoPage = page;
  278.        }
  279.        else {                               /* And when its CGA */
  280.           if (page > 3) return(0);          /* 4 pages.         */
  281.           else VideoPage = page;
  282.        }
  283.     }
  284.     return(1);
  285. }
  286.  
  287. /***********************************************************/
  288. /*                         SwitchPage                      */
  289. /*                                                         */
  290. /* Use BIOS to switch the active display to another video  */
  291. /* page.  First edit to see if we can.                     */
  292. /***********************************************************/
  293.  
  294. void switchpage(int page) {
  295.  
  296.    union REGS regs;
  297.    int cols, mode, apage;
  298.  
  299.    if (!ispcwinit()) pcwinit(1);
  300.    if (page < 0 || _adaptor == MDA) return; /* No Mono here! */
  301.    vgetmode(&cols, &mode, &apage);          /* Get the video mode */
  302.    if (mode > 3) return;                    /* Not valid text mode */
  303.    if (mode < 2) {                          /* 40 Column text mode */
  304.       if (_adaptor > CGA) {                 /* If we are EGA we have... */
  305.          if (page > 15) return;             /* 15 page in 40 col text mode */
  306.       }
  307.       else                                  /* Otherwise...40 col CGA */
  308.          if (page > 7) return;              /* We have 8 pages */
  309.    }
  310.    else {                                   /* Otherwise...80 col text */
  311.       if (_adaptor > CGA) {                 /* If we are EGA... */
  312.          if (page > 7) return;              /* We have 8 pages to play with */
  313.       }
  314.       else                                  /* Otherwise...we are CGA and */
  315.          if (page > 3) return;              /* We have 4 */
  316.    }
  317.    regs.h.ah = 5;                           /* Call BIOS to switch */
  318.    regs.h.al = (char) page;                 /* To specified page */
  319.    int86(0x10, ®s, ®s);               /* BIOS Video */
  320. }
  321.