home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / include / serial.h < prev    next >
C/C++ Source or Header  |  1995-05-08  |  3KB  |  144 lines

  1. // ------ serial.h
  2.  
  3. #ifndef SERIAL_H
  4. #define SERIAL_H
  5.  
  6. #include <dos.h>
  7. #undef disable
  8.  
  9. typedef int bool;
  10. const int true = 1;
  11. const int false = 0;
  12.  
  13. const int systimer = 8;
  14.  
  15. class Timer {
  16.   int timer;
  17. public:
  18.   Timer()
  19.     { timer = -1; }
  20.   bool timed_out()
  21.     { return timer == 0; }
  22.   void set(int secs)
  23.     { timer=secs*182/10+1; }
  24.   void disable()
  25.     { timer = -1; }
  26.   bool running()
  27.     { return timer > 0; }
  28.   void countdown()
  29.     { --timer; }
  30.   bool disabled()
  31.     { return timer == -1; }
  32. };
  33.  
  34. const int xon  = 17;
  35. const int xoff = 19;
  36. const int PIC01 = 0x21; // 8259 Programmable Interrupt Ctrlr
  37. const int PIC00 = 0x20; // "      "              "       "     
  38. const int EOI   = 0x20; // End of Interrupt command
  39. // --------------- line status register values
  40. const int XmitDataReady = 0x20;
  41. // ------------ modem control register values
  42. const int DTR = 1;
  43. const int RTS = 2;
  44. const int OUT2 = 8;
  45. // ------------ modem status register values
  46. const int RLSD = 0x80;
  47. const int DSR = 0x20;
  48. const int CTS = 0x10;
  49. // ----------- interrupt enable register signals
  50. const int DataReady = 1;
  51. // ------------- serial input interrupt buffer
  52. const int BufSize = 1024;
  53. const int SafetyLevel = (BufSize/4);
  54. const int Threshold = (SafetyLevel*3);
  55.  
  56. // ----- com port initialization parameter byte
  57. union portinit   {
  58.     struct {
  59.         unsigned wordlen  : 2;
  60.         unsigned stopbits : 1;
  61.         unsigned parity   : 3;
  62.         unsigned brk      : 1;
  63.         unsigned divlatch : 1;
  64.     } initbits;
  65.     char initchar;
  66. };
  67.  
  68. struct CommParameters  {
  69.   int port;
  70.   int parity;
  71.   int stopbits;
  72.   int databits;
  73.   int baud;
  74. };
  75.  
  76. class CommPort {
  77.   portinit initcom;
  78.   char *mp_recvbuff;
  79.   bool xonxoff_enabled;
  80.   char *mp_nextin, *mp_nextout;
  81.   int buffer_count;
  82.   CommParameters commparms;
  83.   bool waiting_for_xon;
  84.   bool waiting_to_send_xon;
  85.   static CommPort *mp_CommPort;
  86.   int timeout;
  87.   static Timer serialtimer;
  88.  
  89.   int BasePort()
  90.     { return (0x3f8-((commparms.port-1)<<8)); }
  91.   int TxData()
  92.     { return BasePort(); }
  93.   int RxData()
  94.     { return BasePort(); }
  95.   int DivLSB()
  96.     { return BasePort(); }
  97.   int DivMSB()
  98.     { return BasePort()+1; }
  99.   int IntEnable()
  100.     { return BasePort()+1; }
  101.   int IntIdent()
  102.     { return BasePort()+2; }
  103.   int LineCtl()
  104.     { return BasePort()+3; }
  105.   int ModemCtl()
  106.     { return BasePort()+4; }
  107.   int LineStatus()
  108.     { return BasePort()+5; }
  109.   int ModemStatus()
  110.     { return BasePort()+6; }
  111.   int irq()
  112.     { return 4-(commparms.port-1); }
  113.   int vector()
  114.     { return 12-(commparms.port-1); }
  115.   int ComIRQ()
  116.     { return ~(1 << irq()); }
  117.   void CommInterrupt();
  118.   friend void interrupt newcomint(...);
  119.   friend void interrupt newtimer(...);
  120. public:
  121.   CommPort(const CommParameters& cp);
  122.   ~CommPort();
  123.   void Initialize();
  124.   int readcomm();
  125.   bool writecomm(int ch);
  126.   void clear_serial_queue();
  127.   bool carrier()
  128.     { return (inp(ModemStatus()) & RLSD) != 0; }
  129.   bool input_char_ready()
  130.     { return mp_nextin != mp_nextout; }
  131.   void SetTimeout(int to)
  132.     { timeout = to; }
  133.   const CommParameters& CommParms()
  134.     { return commparms; }
  135.   void EnableXonXoff()
  136.     { xonxoff_enabled = true; }
  137.   void DisableXonXoff()
  138.     { xonxoff_enabled = false; }
  139. };
  140.  
  141.  
  142. #endif
  143.  
  144.