home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CPPCOM17.ZIP / COMPORTS.CPP < prev    next >
C/C++ Source or Header  |  1991-02-27  |  9KB  |  177 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7. ***************************************************************************/
  8.  
  9. // file comports.cpp defines various uart classes.
  10.  
  11. #include <stdlib.h>
  12.  
  13. #include "comports.hpp"
  14.  
  15. static const int insize = 1024;
  16. static const int outsize = 4096;
  17.  
  18.  
  19. #ifdef __TURBOC__
  20.  
  21. #define DEFINE_COMX(COM, x, driver)                     \
  22. CharQueue * COM##x::inq = new CharQueue(insize);        \
  23. CharQueue * COM##x::outq = new CharQueue(outsize);      \
  24. DRIVER COM##x::driver = driver##x;                      \
  25. COM##x * COM##x::this_ptr = NULL;                       \
  26. COM##x::COM##x()                                        \
  27. {                                                       \
  28.     base_address = io_address[##x - 1];                 \
  29.     this_ptr = this;                                    \
  30.     DoIfNoCarrier = DoNothing;                          \
  31.     if(RegisterDriver(##x, driver##x) != 0)             \
  32.     exit(1);                                           \
  33. }                                                       \
  34. COM##x::~COM##x()                                       \
  35. {                                                       \
  36.     if(RestoreDriver(##x) != 0)                         \
  37.     exit(1);                                           \
  38. }                                                       \
  39. void COM##x::SetDoIfNoCarrier(void (*f)())              \
  40. {                                                       \
  41.     DoIfNoCarrier = f;                                  \
  42. }                                                       \
  43. void COM##x::FlushOutput()                              \
  44.     { while (!outq->IsEmpty()) {;} }                    \
  45. void COM##x::SendChar(char ch)                          \
  46. {                                                       \
  47.     while(outq->IsFull())                               \
  48.     ;    /* wait til queue transmitted by driver */    \
  49.     outq->Add(ch);                                      \
  50.     SetIER_Transmit(true);                              \
  51. }                                                       \
  52. int COM##x::GetChar()                                   \
  53. {                                                       \
  54.     return inq->Get();                                  \
  55.     /* remember, empty charqueue flag is -1. */         \
  56. }                                                       \
  57. void interrupt driver##x(...)                           \
  58. {                                                       \
  59.     /* driver for comm interrupt. */                    \
  60.     COM##x * cptr = COM##x::this_ptr;                   \
  61.     com_interrupt_t i;                                  \
  62.     while((i = cptr->GetIntrType()) != NONE_PENDING)    \
  63.     {                                                   \
  64.     disable();  /* don't let another come in */        \
  65.     switch((int)i & 0xFF)                              \
  66.     {                                                  \
  67.         case (int)RECEIVE_READY:                       \
  68.         if(!COM##x::inq->IsFull())                    \
  69.             COM##x::inq->                             \
  70.             Add(cptr->ReceiveChar());                \
  71.                 break;                                  \
  72.             case (int)TRANSMIT_READY :                  \
  73.                 if(COM##x::outq->IsEmpty())             \
  74.                     cptr->SetIER_Transmit(false);       \
  75.                 else                                    \
  76.                     cptr->                              \
  77.                     TransmitChar(COM##x::outq->Get());  \
  78.              break;                                   \
  79.             case (int)NO_CARRIER:                       \
  80.                 (*cptr->DoIfNoCarrier)();               \
  81.              break;                                  \
  82.             default:                                    \
  83.             /* all others here. the function */         \
  84.             /* GetIntrType() should have already */     \
  85.             /* cleaned up the other interrupt flags. */ \
  86.                 break;                                  \
  87.         }                                               \
  88.         enable();  /* other interrupts now ok */        \
  89.     }                                                   \
  90.     outportb( 0x20, 0x20);                              \
  91. }                                                       
  92.                                                         
  93.  
  94.  
  95.  
  96. #else ifdef __ZTC__
  97.  
  98. #define DEFINE_COMX(COM, x, driver)                     \
  99. CharQueue * COM##x::inq = new CharQueue(insize);        \
  100. CharQueue * COM##x::outq = new CharQueue(outsize);      \
  101. DRIVER COM##x::driver = driver##x;                      \
  102. COM##x::COM##x()                                        \
  103. {                                                       \
  104.     base_address = io_address[##x - 1];                 \
  105.     this_ptr = this;                                    \
  106.     DoIfNoCarrier = DoNothing;                          \
  107.     if(RegisterDriver(##x, driver##x) != 0)             \
  108.         exit(1);                                        \
  109. }                                                       \
  110. COM##x::~COM##x()                                       \
  111. {                                                       \
  112.     if(RestoreDriver(##x) != 0)                         \
  113.         exit(1);                                        \
  114. }                                                       \
  115. void COM##x::SetDoIfNoCarrier(void (*f)())              \
  116. {                                                       \
  117.     DoIfNoCarrier = f;                                  \
  118. }                                                       \
  119. void COM##x::FlushOutput()                              \
  120.     { while (!outq->IsEmpty()) {;} }                    \
  121. void COM##x::SendChar(char ch)                          \
  122. {                                                       \
  123.     while(outq->IsFull())                               \
  124.         ;                                               \
  125.     outq->Add(ch);                                      \
  126.     SetIER_Transmit(true);                              \
  127. }                                                       \
  128. int COM##x::GetChar()                                   \
  129. {                                                       \
  130.     return inq->Get();                                  \
  131. }                                                       \
  132.                                                         \
  133. int driver##x(INT_DATA *pd)                             \
  134. {                                                       \
  135.     COM##x * cptr = COM##x::this_ptr;                   \
  136.     com_interrupt_t i;                                  \
  137.     while((i = cptr->GetIntrType()) != NONE_PENDING)    \
  138.     {                                                   \
  139.         disable();  /* don't let another come in */     \
  140.         switch((int)i & 0xFF)                           \
  141.         {                                               \
  142.             case (int)TRANSMIT_READY :                  \
  143.                 if(COM##x::outq->IsEmpty())             \
  144.                     cptr->SetIER_Transmit(false);       \
  145.                 else                                    \
  146.                     cptr->                              \
  147.                     TransmitChar(COM##x::outq->Get());  \
  148.               break;                                  \
  149.             case (int)RECEIVE_READY:                    \
  150.                 if(!COM##x::inq->IsFull())              \
  151.                     COM##x::inq->                       \
  152.                         Add(cptr->ReceiveChar());       \
  153.                 break;                                  \
  154.             case (int)NO_CARRIER:                       \
  155.                 (*cptr->DoIfNoCarrier)();               \
  156.              break;                                  \
  157.             default:                                    \
  158.                 break;                                  \
  159.         }                                               \
  160.         enable();  /* other interrupts now ok */        \
  161.     }                                                   \
  162.     outportb( 0x20, 0x20);                              \
  163.     return 1;                                           \
  164. }                                                       
  165.  
  166.  
  167. #endif
  168.  
  169. // To define up to four com ports, you should
  170. // declare one or more of the following statements:
  171.  
  172. DEFINE_COMX(COM, 1, driver)
  173. DEFINE_COMX(COM, 2, driver)
  174. DEFINE_COMX(COM, 3, driver)
  175. DEFINE_COMX(COM, 4, driver)
  176.  
  177.