home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archimedes / arbuff.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  6KB  |  291 lines

  1. /* > c.buffer
  2.     (c) D.R.McAuley 1987
  3. */
  4.  
  5. #include "arthur.h"
  6. #include <stdio.h>
  7. #include "tty.h"
  8.  
  9. static reg_set rs;
  10. reg_set *regptr = &rs;
  11.  
  12. #define interupts_off() swix(OS_IntOff, regptr)
  13. #define interupts_on() swix(OS_IntOn, regptr)
  14.  
  15. #define ctrlS 19
  16. #define ctrlQ 20
  17.  
  18. #define BUFFSIZE 0x4000
  19. #define BUFFLWM  0x0400
  20. #define BUFFHWM  0x3C00
  21.  
  22. /* #define OUTPUTTOO */
  23. #define XONXOFF
  24.  
  25. #ifdef OUTPUTTOO
  26. static char rsbufout[BUFFSIZE];
  27. static char *headout = rsbufout;
  28. static char *tailout = rsbufout;
  29. static int  countout = 0;
  30. #endif
  31.  
  32. static char rsbufinn[BUFFSIZE];
  33. static char *headinn = rsbufinn;
  34. static char *tailinn = rsbufinn;
  35. static int  countinn = 0;
  36.  
  37.  
  38. static char kbbufinn[BUFFSIZE];
  39. static char *kbheadinn = kbbufinn;
  40. static char *kbtailinn = kbbufinn;
  41. static int  kbcountinn = 0;
  42.  
  43. static struct {
  44.         unsigned count:8;
  45.         unsigned on:1;
  46.         unsigned run:1;
  47. } x;
  48.  
  49. void (*keypoller)();
  50.  
  51.  
  52. /* code */
  53.  
  54. static f_ptr tmp;
  55. f_ptr define_keypoller(void (*proc)())
  56. {
  57.     tmp = keypoller;
  58.     keypoller = proc;
  59.     return tmp;
  60. }
  61.  
  62. void set_xon_xoff(int state) { 
  63.         x.count = 0;
  64.         x.on = state;
  65.         x.run = state;
  66. }
  67.  
  68. int rsgetcount (void) { return(0); }
  69. int kbgetcount (void) { return(0); }
  70.  
  71. #ifdef OUTPUTTOO
  72. int _rsinsertout(char ch)
  73. {       int wasdead = (countout == 0);
  74.         if (countout == BUFFSIZE) return (1);
  75.         if (ch != 0) {
  76.             (countout)++; *headout = ch;
  77.             if (++(headout) == (rsbufout + BUFFSIZE)) headout = rsbufout;
  78.         }
  79.         if (wasdead) inter();
  80.         return (0);
  81. }
  82.  
  83. int _rsremoveout(int examine) 
  84. {       char ch;
  85.         if (countout == 0) return(256);
  86.         ch = *tailout;
  87.         if (!examine) {
  88.             countout--;
  89.             if (++(tailout) == (rsbufout + BUFFSIZE)) tailout = rsbufout;
  90.         }
  91.         return (ch);
  92. }
  93.  
  94. int _rscountnpout(int purge, int countt)
  95. {       if (purge) {
  96.             countout = 0;
  97.             headout = tailout = rsbufout;
  98.             return (0);
  99.         }
  100.         if (countt) return(BUFFSIZE-countout); else return(countout);
  101. }
  102. #endif
  103.  
  104. int _rsinsertinn(char ch)
  105. {
  106.         if (countinn == BUFFSIZE) return (1);
  107.         if (ch != 0) {
  108.             (countinn)++; *headinn = ch;
  109.             if (++(headinn) == (rsbufinn + BUFFSIZE)) headinn = rsbufinn;
  110.         }
  111.         return (0);
  112. }
  113.  
  114. int _rsremoveinn(int examine) 
  115. {       char ch;
  116.         if (countinn == 0) return(256);
  117.         ch = *tailinn;
  118.         if (!examine) {
  119.             countinn--;
  120.             if (++(tailinn) == (rsbufinn + BUFFSIZE)) tailinn = rsbufinn;
  121.         }
  122.         return (ch);
  123. }
  124.  
  125. int _rscountnpinn(int purge, int countt)
  126. {       if (purge) {
  127.             countinn = 0;
  128.             headinn = tailinn = rsbufinn;
  129.             return (0);
  130.         }
  131.         if (countt) return(BUFFSIZE-countinn); else return(countinn);
  132. }
  133.  
  134.  
  135. static int kbcalled = 0;
  136. int _kbinsertinn(char ch)
  137. {
  138. kbcalled++;
  139.         if (kbcountinn == BUFFSIZE) return (1);
  140.         if (ch != 0) {
  141.             (kbcountinn)++; *kbheadinn = ch;
  142.             if (++(kbheadinn) == (kbbufinn + BUFFSIZE)) kbheadinn = kbbufinn;
  143.         }
  144.         return (0);
  145. }
  146.  
  147. int _kbremoveinn(int examine) 
  148. {       char ch;
  149.         if (kbcountinn == 0) return(256);
  150.         ch = *kbtailinn;
  151.         if (!examine) {
  152.             kbcountinn--;
  153.             if (++(kbtailinn) == (kbbufinn + BUFFSIZE)) kbtailinn = kbbufinn;
  154.         }
  155.         return (ch);
  156. }
  157.  
  158. int _kbcountnpinn(int purge, int countt)
  159. {       if (purge) {
  160.             kbcountinn = 0;
  161.             kbheadinn = kbtailinn = kbbufinn;
  162.             return (0);
  163.         }
  164.         if (countt) return(BUFFSIZE-kbcountinn); else return(kbcountinn);
  165. }
  166.  
  167.  
  168. void rsinsert(int ch, int b)
  169. {       interupts_off();
  170.         if (b == 1) _rsinsertinn((char) ch);
  171. #ifdef OUTPUTTOO
  172.         else _rsinsertout((char) ch);
  173. #endif
  174.         interupts_on();
  175. }
  176.  
  177. int rsremove(int b)
  178. {       int ch = 0;
  179.         if (b == 1 ) {
  180.             if (countinn != 0) {
  181.                 interupts_off();
  182.                 ch = _rsremoveinn(FALSE);
  183.                 interupts_on();
  184.             }
  185. #ifdef OUTPUTTOO
  186.         } else {
  187.             if (countout != 0) {
  188.                 interupts_off();
  189.                 ch = _rsremoveout(FALSE);
  190.                 interupts_on();
  191.             }
  192. #endif
  193.         }
  194.         return(ch);
  195. }
  196.  
  197. void rspurge(int b)
  198. {       interupts_off();
  199.         if (b == 1) _rscountnpinn(TRUE, 0);
  200. #ifdef OUTPUTTOO
  201.         else _rscountnpout(TRUE, 0);
  202. #endif
  203.         interupts_on();
  204. }
  205.  
  206. int rscount(int spaces, int b)
  207. {       int cnt;
  208.         interupts_off();
  209.         if (b ==1 ) cnt = _rscountnpinn(FALSE, spaces);
  210. #ifdef OUTPUTTOO
  211.         else cnt = _rscountnpout(FALSE, spaces);
  212. #endif
  213.         interupts_on();
  214.         return(cnt);
  215. }
  216.  
  217.  
  218. void kbinsert(int ch, int b)
  219. {       interupts_off();
  220.         if (b == 0) _kbinsertinn((char) ch);
  221.         interupts_on();
  222. }
  223.  
  224. int kbremove(int b)
  225. {       int ch = 0;
  226.         if (b == 0 ) {
  227.             if (kbcountinn != 0) {
  228.                 interupts_off();
  229.                 ch = _kbremoveinn(FALSE);
  230.                 interupts_on();
  231.             }
  232.         }
  233.         return(ch);
  234. }
  235.  
  236. void kbpurge(int b)
  237. {       interupts_off();
  238.         if (b == 0) _kbcountnpinn(TRUE, 0);
  239.         interupts_on();
  240. }
  241.  
  242. int kbcount(int spaces, int b)
  243. {       int cnt;
  244.         interupts_off();
  245.         if (b == 0 ) cnt = _kbcountnpinn(FALSE, spaces);
  246.         interupts_on();
  247.         return(cnt);
  248. }
  249.  
  250.  
  251. int rsgetch(void)
  252. {       int ch;
  253.         int loop = 0;
  254.         x.count++;
  255.         while (TRUE) {
  256. #ifdef XONXOFF
  257.             if (x.count > 16) {
  258.                 if ((countinn >= BUFFHWM) && x.run && x.on) {
  259.                     sendchar(ctrlS);
  260.                     x.run = FALSE;
  261.                 } else if ((countinn <= BUFFLWM) && (!x.run) && x.on) {
  262.                     sendchar(ctrlQ);
  263.                     x.run = TRUE;
  264.                 }
  265.                 x.count = 0;
  266.                 (*keypoller)();
  267.             }
  268. #endif
  269.             
  270.             if ((ch = pollch()) != NOCHAR) return(ch);
  271.  
  272.             if (loop++ == 16) { loop = 0; (*keypoller)(); }
  273.         }
  274. }
  275.  
  276. int pollch(void) { return(rsremove(1) & 0x7F); }
  277. /*
  278. int pollch(void)
  279. {       mosbyte2(145, 1);
  280.         return(regs.r[2] & 0x7F);
  281. }
  282. */
  283.  
  284. #ifdef OUTPUTTOO
  285. void sendchar(int ch) { rsinsert(ch, 2); }
  286. #else
  287. void sendchar(int ch) { mosbyte3(138, 2, ch); }
  288. #endif
  289.  
  290. void inter(void) { mosbyte3(156, 0x20, 0x9F); } /* Enable interupts poo */
  291.