home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tc-book.zip / IBMPC.C < prev    next >
Text File  |  1987-08-20  |  4KB  |  175 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. /*page*/
  103. /* --- insert a character and attribute into video RAM --- */
  104. void vpoke(unsigned vseg, unsigned adr, unsigned chr)
  105. {
  106.     if (vseg == 45056)            /* monochrome mode */
  107.         poke(vseg, adr, chr);
  108.     else    {
  109.         _DI = adr;        /* offset of video character */
  110.         _ES = vseg;        /* video segment */
  111.         asm cld;
  112.         _BX = chr;        /* the attribute and character */
  113.         _DX = 986;        /* video status port */
  114.         /* ------ wait for video retrace to start ----- */
  115.         do
  116.             asm in  al,dx;
  117.         while (_AL & 1);
  118.         /* ------ wait for video retrace to stop ----- */
  119.         do
  120.             asm in  al,dx;
  121.         while (!(_AL & 1));
  122.         _AL = _BL;
  123.         asm stosb;        /* store character */
  124.         /* ------ wait for video retrace to start ----- */
  125.         do
  126.             asm in  al,dx;
  127.         while (_AL & 1);
  128.         /* ------ wait for video retrace to stop ----- */
  129.         do
  130.             asm in  al,dx;
  131.         while (!(_AL & 1));
  132.         _AL = _BH;
  133.         asm stosb;        /* store attribute */
  134.     }
  135. }
  136.  
  137. /*page*/
  138. /* ---- read a character and attribute from video RAM --- */
  139. int vpeek(unsigned vseg, unsigned adr)
  140. {
  141.     int ch, at;
  142.  
  143.     if (vseg == 45056)                /* monochrome mode */
  144.         return peek(vseg, adr);
  145.     asm push ds;
  146.     _DX = 986;            /* video status port */
  147.     _DS = vseg;            /* video segment address */
  148.     _SI = adr;            /* video character offset */
  149.     asm cld;
  150.     /* ------ wait for video retrace to start ----- */
  151.     do
  152.         asm in  al,dx;
  153.     while (_AL & 1);
  154.     /* ------ wait for video retrace to stop ----- */
  155.     do
  156.         asm in  al,dx;
  157.     while (!(_AL & 1));
  158.     asm lodsb;            /* get the character */
  159.     _BL = _AL;
  160.     /* ------ wait for video retrace to start ----- */
  161.     do
  162.         asm in  al,dx;
  163.     while (_AL & 1);
  164.     /* ------ wait for video retrace to stop ----- */
  165.     do
  166.         asm in  al,dx;
  167.     while (!(_AL & 1));
  168.     asm lodsb;            /* get the attribute */
  169.     _BH = _AL;
  170.     _AX = _BX;
  171.     asm pop ds;
  172.     return _AX;
  173. }
  174.  
  175.