home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / turbbook.arc / IBMPC.C < prev    next >
Text File  |  1988-07-04  |  4KB  |  176 lines

  1. /* --------------- ibmpc.c -------------- */
  2.  
  3. /*
  4.  * Low-level functions addressing BIOS & PC Hardware
  5.  */
  6.  
  7. #pragma inline
  8. #include <dos.h>
  9. static union REGS rg;
  10.  
  11. /* ----------- position the cursor ------------- */
  12. void cursor(int x, int y)
  13. {
  14.     rg.x.ax = 0x0200;
  15.     rg.x.bx = 0;
  16.     rg.x.dx = ((y << 8) & 0xff00) + x;
  17.     int86(16, &rg, &rg);
  18. }
  19.  
  20. /* ------------ return the cursor position ------------- */
  21. void curr_cursor(int *x, int *y)
  22. {
  23.     rg.x.ax = 0x0300;
  24.     rg.x.bx = 0;
  25.     int86(16, &rg, &rg);
  26.     *x = rg.h.dl;
  27.     *y = rg.h.dh;
  28. }
  29.  
  30. /* ----------- set cursor type --------------- */
  31. void set_cursor_type(int t)
  32. {
  33.     rg.x.ax = 0x0100;
  34.     rg.x.bx = 0;
  35.     rg.x.cx = t;
  36.     int86(16, &rg, &rg);
  37. }
  38. /*page*/
  39. char attrib = 7;
  40.  
  41. /* ------------- clear the screen -------------- */
  42. void clear_screen()
  43. {
  44.     cursor(0, 0);
  45.     rg.h.al = ' ';
  46.     rg.h.ah = 9;
  47.     rg.x.bx = attrib;
  48.     rg.x.cx = 2000;
  49.     int86(16, &rg, &rg);
  50. }
  51.  
  52. /* ----------- return the video mode ------------ */
  53. int vmode()
  54. {
  55.     rg.h.ah = 15;
  56.     int86(16, &rg, &rg);
  57.     return rg.h.al;
  58. }
  59.  
  60. /* -------- test for scroll lock -------- */
  61. int scroll_lock()
  62. {
  63.     rg.x.ax = 0x0200;
  64.     int86(0x16, &rg, &rg);
  65.     return rg.h.al & 0x10;
  66. }
  67. /*page*/
  68. void (*helpfunc)();
  69. int helpkey = 0;
  70. int helping = 0;
  71.  
  72. /* ------------- get a keyboard character ---------------- */
  73. int get_char()
  74. {
  75.      int c;
  76.  
  77.     while (1)    {
  78.         rg.h.ah = 1;
  79.         int86(0x16, &rg, &rg);
  80.         if (rg.x.flags & 0x40)    {
  81.             int86(0x28, &rg, &rg);
  82.             continue;
  83.         }
  84.         rg.h.ah = 0;
  85.         int86(0x16, &rg, &rg);
  86.         if (rg.h.al == 0)
  87.             c = rg.h.ah | 128;
  88.         else
  89.             c = rg.h.al;
  90.         if (c == helpkey && helpfunc)    {
  91.             if (!helping)    {
  92.                 helping = 1;
  93.                 (*helpfunc)();
  94.                 helping = 0;
  95.                 continue;
  96.             }
  97.         }
  98.         break;
  99.     }
  100.     return c;
  101. }
  102. int EGA = 1;
  103. /*page*/
  104. /* --- insert a character and attribute into video RAM --- */
  105. void vpoke(unsigned vseg, unsigned adr, unsigned chr)
  106. {
  107.     if (vseg == 45056 || EGA)    /* monochrome mode */
  108.         poke(vseg, adr, chr);
  109.     else    {
  110.         _DI = adr;        /* offset of video character */
  111.         _ES = vseg;        /* video segment */
  112.         asm cld;
  113.         _BX = chr;        /* the attribute and character */
  114.         _DX = 986;        /* video status port */
  115.         /* ------ wait for video retrace to start ----- */
  116.         do
  117.             asm in  al,dx;
  118.         while (_AL & 1);
  119.         /* ------ wait for video retrace to stop ----- */
  120.         do
  121.             asm in  al,dx;
  122.         while (!(_AL & 1));
  123.         _AL = _BL;
  124.         asm stosb;        /* store character */
  125.         /* ------ wait for video retrace to start ----- */
  126.         do
  127.             asm in  al,dx;
  128.         while (_AL & 1);
  129.         /* ------ wait for video retrace to stop ----- */
  130.         do
  131.             asm in  al,dx;
  132.         while (!(_AL & 1));
  133.         _AL = _BH;
  134.         asm stosb;        /* store attribute */
  135.     }
  136. }
  137.  
  138. /*page*/
  139. /* ---- read a character and attribute from video RAM --- */
  140. int vpeek(unsigned vseg, unsigned adr)
  141. {
  142.     int ch, at;
  143.  
  144.     if (vseg == 45056 || EGA)            /* monochrome mode */
  145.         return peek(vseg, adr);
  146.     asm push ds;
  147.     _DX = 986;            /* video status port */
  148.     _DS = vseg;            /* video segment address */
  149.     _SI = adr;            /* video character offset */
  150.     asm cld;
  151.     /* ------ wait for video retrace to start ----- */
  152.     do
  153.         asm in  al,dx;
  154.     while (_AL & 1);
  155.     /* ------ wait for video retrace to stop ----- */
  156.     do
  157.         asm in  al,dx;
  158.     while (!(_AL & 1));
  159.     asm lodsb;            /* get the character */
  160.     _BL = _AL;
  161.     /* ------ wait for video retrace to start ----- */
  162.     do
  163.         asm in  al,dx;
  164.     while (_AL & 1);
  165.     /* ------ wait for video retrace to stop ----- */
  166.     do
  167.         asm in  al,dx;
  168.     while (!(_AL & 1));
  169.     asm lodsb;            /* get the attribute */
  170.     _BH = _AL;
  171.     _AX = _BX;
  172.     asm pop ds;
  173.     return _AX;
  174. }
  175.  
  176.