home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8911.zip / GEHANI.LST < prev    next >
File List  |  1989-10-04  |  5KB  |  232 lines

  1. _CONCURRENT C FOR REAL-TIME PROGRAMMING_
  2. by N.H. Gehani and W.D. Roome
  3.  
  4. [LISTING ONE]
  5.  
  6.  
  7. process spec ttyOutput()
  8. {
  9.     trans void  put(int nc, char *pc);
  10.     trans async ready(), start(), stop();
  11. };
  12.  
  13. process spec ttyInput()
  14. {
  15.     trans void  put(char c);
  16.     trans int   get(int wait);
  17. };
  18.  
  19. process spec ttyReader()
  20. {
  21.     trans async ready();
  22.     trans char  get();
  23. };
  24.  
  25. process spec ttyLine();
  26.  
  27. process ttyInput  ttyIn;
  28. process ttyOutput ttyOut;
  29. process ttyReader ttyRdr;
  30.  
  31.  
  32. [LISTIN╟ TWO]
  33.  
  34. #include "concurrentc.h"
  35. #include "tty.h"
  36.  
  37. void ttyReply(msg, reply)
  38.     char *msg, *reply;
  39. {
  40.     int c;
  41.     char *nasty = "\r\nWAKE UP!\r\n";
  42.  
  43.     while (1) {
  44.         ttyOut.put(strlen(msg), msg);
  45.         c = within 30 ? ttyIn.get(1) : -1;
  46.         if (c != -1)
  47.             break;
  48.         ttyOut.put(strlen(nasty), nasty);
  49.     }
  50.     while (c != '\n') {
  51.         *reply++ = c;
  52.         c = ttyIn.get(1);
  53.     }
  54.     *reply = '\0';
  55. }
  56.  
  57.  
  58. [LISTIN╟ THREE]
  59.  
  60. #include "concurrentc.h"
  61. #include "tty.h"
  62.  
  63. void ttyInit()
  64. {
  65.     ttyOut = create ttyOutput() priority(1);
  66.     c_associate(ttyOut.ready, 1/*DUM*//*PSoutput-ready-interruptPS*/);
  67.     ttyRdr = create ttyReader() priority(2);
  68.     c_associate(ttyRdr.ready, 2/*DUM*//*PSinput-ready-interruptPS*/);
  69.     ttyIn = create ttyInput();
  70.     create ttyLine();
  71. }
  72.  
  73.  
  74. [LISTIN╟ FOUR]
  75.  
  76. #define M_RdrBuff 2048
  77. #define M_OutBuff 2048
  78. #define M_InBuff  1024
  79. #define M_LineLen 1024
  80.  
  81. #define StopChar  0x13  /* ctl-S */
  82. #define StartChar 0x11  /* ctl-Q */
  83. #define EraseChar '\b'  /* backspace */
  84. #define KillChar  0x03  /* ctl-C */
  85.  
  86.  
  87.  
  88. [LISTIN╟ FIVE]
  89.  
  90. #include "concurrentc.h"
  91. #include "tty.h"
  92. #include "defs.h"
  93.  
  94. process body ttyReader()
  95. {
  96.     int  n = 0, in = 0, out = 0;
  97.     char c, buff[M_RdrBuff];
  98.  
  99.     while (1) {
  100.         select {
  101.             accept ready();
  102.             c = uartGetChar();
  103.             if (c == StartChar) {
  104.                 ttyOut.start();
  105.             } else if (c == StopChar) {
  106.                 ttyOut.stop();
  107.             } else if (n < M_RdrBuff) {
  108.                 buff[in] = c;
  109.                 in = (in + 1) % M_RdrBuff;
  110.                 n++;
  111.             }
  112.           or (n > 0):
  113.             accept get()
  114.                 treturn buff[out];
  115.             out = (out + 1) % M_RdrBuff;
  116.             n--;
  117.         }
  118.     }
  119. }
  120.  
  121. [LISTIN╟ SIX]
  122.  
  123. #include "concurrentc.h"
  124. #include "tty.h"
  125. #include "defs.h"
  126.  
  127. process body ttyLine()
  128. {
  129.     int n = 0, putline = 0, i;
  130.     char c, buff[M_LineLen];
  131.  
  132.     while (1) {
  133.         switch (c = ttyRdr.get()) {
  134.             case EraseChar:
  135.                 ttyOut.put(3, "\b \b");
  136.                 if (n > 0)
  137.                     --n;
  138.                 break;
  139.             case KillChar:
  140.                if (n > 0)
  141.                    ttyOut.put(2, "\r\n");
  142.                n = 0;
  143.                break;è            case '\r':
  144.             case '\n':
  145.                 ttyOut.put(2, "\r\n");
  146.                 buff[n++] = '\n';
  147.                 putline = 1;
  148.                 break;
  149.             default:
  150.                 ttyOut.put(1, &c);
  151.                 buff[n++] = c;
  152.                 if (n >= M_LineLen)
  153.                     putline = 1;
  154.                 break;
  155.         }
  156.         if (putline) {
  157.             for (i = 0; i < n; i++)
  158.                 ttyIn.put(buff[i]);
  159.             putline = n = 0;
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. [LISTIN╟ SEVEN]è
  166. #include "concurrentc.h"
  167. #include "tty.h"
  168. #include "defs.h"
  169.  
  170. process body ttyInput()
  171. {
  172.     int n = 0, in = 0, out = 0;
  173.     char buff[M_InBuff];
  174.  
  175.     while (1) {
  176.         select {
  177.           (n < M_InBuff):
  178.             accept put(c)
  179.                 buff[in] = c;
  180.             in = (in + 1) % M_RdrBuff;
  181.             n++;
  182.           or (n > 0):
  183.             accept get(wait)
  184.                 treturn buff[out];
  185.             out = (out + 1) % M_RdrBuff;
  186.             n--;
  187.           or (n == 0):
  188.             accept get(wait) suchthat (!wait)
  189.                 treturn -1;
  190.         }
  191.     }
  192. }
  193.  
  194.  
  195. [LISTIN╟ EIGHT]
  196.  
  197. #include "concurrentc.h"
  198. #include "tty.h"
  199. #include "defs.h"
  200.  
  201. process body ttyOutput()
  202. {
  203.     int n = 0, in = 0, out = 0;
  204.     int running = 1, ttyrdy = 1, i;
  205.     char buff[M_OutBuff];
  206.  
  207.     while (1) {
  208.         select {
  209.             accept start()
  210.                 running = 1;
  211.           or accept stop()
  212.                 running = 0;
  213.           or accept ready()
  214.                 ttyrdy = 1;
  215.           or accept put(nc, pc) suchthat (n+nc <= M_OutBuff) {
  216.                 for (i = 0; i < nc; i++) {
  217.                     buff[in] = pc[i];
  218.                     in = (in + 1) % M_OutBuff;
  219.                 }
  220.                 n += nc;
  221.             }
  222.         }
  223.         if (ttyrdy && running && n > 0) {
  224.             uartPutChar(buff[out]);
  225.             out = (out + 1) % M_OutBuff;
  226.             n--;
  227.             ttyrdy = 0;
  228.         }
  229.     }
  230. }
  231.  
  232.