home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / djasy10d.zip / ASYNC.C next >
C/C++ Source or Header  |  1992-09-11  |  3KB  |  177 lines

  1. /*  async.c -- dj's async interface, modified for two ports and
  2.     pointer-bashing protection by j. alan eldridge 09/04/92
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8.  
  9. #include "djasync.h"
  10.  
  11. #define SIGNATURE 0x4154
  12. #define VERSION 1
  13. #define OFFSET 0x104
  14.  
  15. #ifdef __GNUC__
  16. #define far
  17. #define peek(a,b) (*(unsigned short *)(0xe0000000 + (a)*16 + (b)))
  18. #define disable() asm("cli")
  19. #define enable() asm("sti")
  20. #endif
  21.  
  22. #ifdef __TURBOC__
  23. #include <conio.h>
  24. #endif
  25.  
  26. #define NO_INTR 1
  27. #define RDY_CNT 1
  28.  
  29. typedef struct {
  30.   short jmp_op;
  31.   short signature;
  32.   short version;
  33.   short buffer_start;
  34.   short buffer_end;
  35.   short getp;
  36.   short putp;
  37.   short iov;
  38.   short count;
  39.   short overflow;
  40.   short buffer_size;
  41.   short ovflushes;
  42. } ASYNC_STRUCT;
  43.  
  44. static ASYNC_STRUCT far *async[2];
  45. static int              iov[2];
  46.  
  47. #define com_rb(n)    iov[n]
  48. #define com_tb(n)    iov[n]
  49. #define com_ier(n)    iov[n]+1
  50. #define com_ifr(n)    iov[n]+2
  51. #define com_bfr(n)    iov[n]+3
  52. #define com_mcr(n)    iov[n]+4
  53. #define com_lsr(n)    iov[n]+5
  54. #define com_msr(n)    iov[n]+6
  55.  
  56. static char far *aptr(int port, short p)
  57. {
  58. #ifdef __GNUC__
  59.   return (char *)((unsigned)async[port] - OFFSET + p);
  60. #else
  61.   return (char far *)MK_FP(FP_SEG(async[port]), p);
  62. #endif
  63. }
  64.  
  65. static ASYNC_STRUCT far *getivec(int which)
  66. {
  67.   ASYNC_STRUCT far *a;
  68.   if (peek(0, which*4) != OFFSET)
  69.     return 0;
  70. #ifdef __GNUC__
  71.   a = (ASYNC_STRUCT *)(0xe0000000 + peek(0, which*4+2)*16 + peek(0, which*4));
  72. #else
  73.   a = (ASYNC_STRUCT far *)MK_FP(peek(0,which*4+2),peek(0,which*4));
  74. #endif
  75.   if (a->signature != SIGNATURE)
  76.     return 0;
  77.   if (a->version != VERSION)
  78.     return 0;
  79.   return a;
  80. }
  81.  
  82. int async_init(int port)
  83. {
  84.   async[port] = getivec(12-port);
  85.  
  86.   if (!async[port])
  87.   {
  88.     fprintf(stderr, "No async driver.\n");
  89.     return 0;
  90.   }
  91.   iov[port] = async[port]->iov;
  92.   outportb(com_ier(port), 0x0f);
  93.   outportb(com_bfr(port), 0x03);
  94.   outportb(com_mcr(port), 0x0b);
  95.   return 1;
  96. }
  97.  
  98. int async_cnt(int port)
  99. {
  100.     return async[port]->count;
  101. }
  102.  
  103. void async_flush(int port)
  104. {
  105.     disable();
  106.     async[port]->count = async[port]->overflow = 0;
  107.     async[port]->getp = async[port]->putp = async[port]->buffer_start;
  108.     enable();
  109. }
  110.  
  111. int async_overflow(int port)
  112. {
  113.     int ret;
  114.     
  115.     disable();
  116.     ret = async[port]->overflow;
  117.     async[port]->overflow = 0;
  118.     enable();
  119.  
  120.     return ret;
  121. }
  122.  
  123.  
  124. int async_tx(int port, char c)
  125. {
  126.   while (~inportb(com_lsr(port)) & 0x20);
  127.   outportb(com_tb(port), c);
  128.  
  129.   return 0;
  130. }
  131.  
  132. int async_ready(int port)
  133. {
  134.   int ret;
  135.   
  136.   disable();
  137. #if RDY_CNT
  138.   ret = async[port]->count;
  139. #else
  140.   ret = (async[port]->getp != async[port]->putp);
  141. #endif
  142.   enable();
  143.  
  144.   return ret;
  145. }
  146.  
  147. int async_rx(int port)
  148. {
  149.   char rv;
  150.  
  151.   while (!async_ready(port))
  152.       /* spin wheels */;
  153.  
  154.   disable();
  155.   rv = *aptr(port, async[port]->getp++);
  156.   async[port]->count--;
  157.   if (async[port]->getp >= async[port]->buffer_end)
  158.     async[port]->getp = async[port]->buffer_start;
  159.   enable();
  160.  
  161.   return rv;
  162. }
  163.  
  164. int kb_ready()
  165. {
  166.   return (peek(0x40,0x1a) != peek(0x40,0x1c));
  167. }
  168.  
  169. int kb_rx()
  170. {
  171. #ifdef __GNUC__
  172.   return getkey();
  173. #else
  174.   return getch();
  175. #endif
  176. }
  177.