home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 242.lha / GenericLIBrarian / source / pc-mach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-07  |  7.2 KB  |  340 lines

  1. /*
  2.  * Glib - Generic LIBrarian and editor
  3.  *
  4.  * Machine dependent stuff for MIDI programs.
  5.  *
  6.  * This is for MS-DOS machines.  The screen operations should
  7.  * work okay, but the MIDI I/O needs to be worked on.  It may
  8.  * or may not be fast enough on some machines. 
  9.  */
  10.  
  11. #include "dos.h"
  12. #include "glib.h"
  13.  
  14. #define C86
  15. #ifdef C86
  16. #define inp inportb
  17. #define outp outportb
  18. #endif
  19.  
  20. int Rows = 24;
  21. int Cols = 80;
  22.  
  23. flushmidi()
  24. {
  25.         while ( statmidi() )
  26.                 getmidi();
  27. }
  28.  
  29. /* getmouse - get currect row and column of mouse */
  30. getmouse(amr,amc)
  31. int *amr;
  32. int *amc;
  33. {
  34.         *amr = -1;
  35.         *amc = -1;
  36. }
  37.  
  38. /* statmouse - return mouse button state (0=nothing pressed,1=left,2=right) */
  39. statmouse()
  40. {
  41.         return(-1);
  42. }
  43.  
  44. /* Return when either a console key or mouse button is pressed. */
  45. mouseorkey()
  46. {
  47.         return(getconsole());
  48. }
  49.  
  50. flushconsole()
  51. {
  52. }
  53.  
  54. statconsole()
  55. {
  56.         struct regval sreg, rreg;
  57.         sreg.ax = 0x0100;
  58.         sysint(0x16, &sreg, &rreg);
  59.         if ( rreg.ax != 0 )
  60.                 return(1);
  61.         else
  62.                 return(0);
  63. }
  64.  
  65. getconsole()
  66. {
  67.         struct regval sreg, rreg;
  68.         sreg.ax = 0x0000;
  69.         sysint(0x16, &sreg, &rreg);
  70.         return(rreg.ax & 0x7f);
  71. }
  72.  
  73. long milliclock()
  74. {
  75.         static int hour1 = -1, min1, sec1, hund1;
  76.         struct regval sreg, rreg;
  77.         int hours, mins, secs, hunds;
  78.         long mills;
  79.  
  80.         sreg.ax = 0x2c00;
  81.         sysint(0x21, &sreg, &rreg);
  82.         hours = (rreg.cx >> 8) & 0x7f;
  83.         mins = rreg.cx & 0x7f;
  84.         secs = (rreg.dx >> 8) & 0x7f;
  85.         hunds = rreg.dx & 0x7f ;
  86.         if ( hour1 < 0 ) {
  87.                 mills = 0L;
  88.                 hour1 = hours;
  89.                 min1 = mins;
  90.                 sec1 = secs;
  91.                 hund1 = hunds;
  92.         }
  93.         else {
  94.                 mills = 10L*(hunds-hund1)+1000L*(secs-sec1)
  95.                         +60000L*(mins-min1)+360000L*(hours-hour1);
  96.         }
  97.         return( mills );
  98. }
  99.  
  100. char *
  101. alloc(n)
  102. {
  103.         char *p;
  104.  
  105.         if ( (p=malloc((unsigned)n)) == (char *)NULL ) {
  106.                 printf("*** Whoops *** alloc has failed?!?  No more memory!\n");
  107.                 fflush(stdout);
  108.                 bye();
  109.         }
  110.         return(p);
  111. }
  112.  
  113. windinit()
  114. {
  115. }
  116.  
  117. windgoto(r,c)
  118. int r,c;
  119. {
  120.         struct regval sreg, rreg;
  121.         sreg.ax = 0x200;
  122.         sreg.bx = 0;
  123.         sreg.dx = (r<<8) | c ;
  124.         sysint(0x10, &sreg, &rreg);
  125. }
  126.  
  127. winderaserow(r)
  128. {
  129.         struct regval sreg, rreg;
  130.         sreg.ax = 0x0600;
  131.         sreg.bx = 0;
  132.         sreg.cx = (r<<8);       /* urow, lcol */
  133.         sreg.dx = (r<<8) | 79;
  134.         sysint(0x10, &sreg, &rreg);
  135. }
  136.  
  137. windexit(r)
  138. int r;
  139. {
  140.         /* windgoto(23,0);
  141.         windrefresh();
  142.         nocbreak();
  143.         nl();
  144.         echo();
  145.         endwin(); */
  146. }
  147.  
  148. windclear()
  149. {
  150.         struct regval sreg, rreg;
  151.         sreg.ax = 0x0600;
  152.         sreg.bx = 0;
  153.         sreg.cx = 0;    /* urow, lcol */
  154.         sreg.dx = (24<<8) | 79;
  155.         sysint(0x10, &sreg, &rreg);
  156. }
  157.  
  158. /* windgets - get a line of input from the console, handling backspaces */
  159. windgets(s)
  160. char *s;
  161. {
  162.         char *origs = s;
  163.         int c;
  164.  
  165.         while ( (c=getconsole()) != '\n' && c!='\r' && c!= EOF ) {
  166.                 if ( c == '\b' ) {
  167.                         if ( s > origs ) {
  168.                                 windstr("\b \b");
  169.                                 s--;
  170.                         }
  171.                 }
  172.                 else {
  173.                         windputc(c);
  174.                         *s++ = c;
  175.                 }
  176.                 windrefresh();
  177.         }
  178.         *s = '\0';
  179. }
  180.  
  181. windstr(s)
  182. char *s;
  183. {
  184.         int c;
  185.  
  186.         while ( (c=(*s++)) != '\0' )
  187.                 windputc(c);
  188. }
  189.  
  190. windputc(c)
  191. int c;
  192. {
  193.         putchar(c);
  194. }
  195.  
  196. windrefresh()
  197. {
  198. }
  199.  
  200. beep()
  201. {
  202.         putchar('\007');
  203. }
  204.  
  205. windhigh()
  206. {
  207. }
  208.  
  209. windnorm()
  210. {
  211. }
  212.  
  213. /****************
  214.  * openls(), nextls(), and closels() are used to scan the current directory.
  215.  ***************/
  216.  
  217. openls()
  218. {
  219. }
  220. char *
  221. nextls()
  222. {
  223.         return(NULL);
  224. }
  225. closels()
  226. {
  227. }
  228.  
  229. /*
  230.  * The following MPU code has been provided by Steve Frysinger (moss!spf).
  231.  */
  232.  
  233. #define STATUS_PORT     0x0331
  234. #define COMMAND_PORT    0x0331
  235. #define DATA_PORT       0x0330
  236. #define DATA_READY_MASK 0x40
  237. #define DATA_AVAIL_MASK 0x80
  238.  
  239. int send_command_4001(val)      /* Patterned after Voyetra's reset_4001() */
  240. unsigned val;
  241. {
  242.         unsigned x = 0;
  243.         int flag;
  244.         int ack_count,time_count;
  245.         int retval=1; /* Assume success */
  246.         inp(DATA_PORT);
  247.         for (time_count=5000,flag=1;time_count&&flag;time_count--)
  248.         {
  249.                 if (!(inp(STATUS_PORT)&DATA_READY_MASK)) flag=0;
  250.         }
  251.         if (flag)
  252.         {
  253.                 fprintf(stderr,"Command timeout waiting for port!\n");
  254.                 retval = -1;
  255.         }
  256.         else
  257.         {
  258.                 outp(COMMAND_PORT,val);
  259.                 for (time_count=10000,ack_count=5,flag=0;!flag;)
  260.                 {
  261.                         if ((inp(STATUS_PORT)&DATA_AVAIL_MASK))
  262.                         {
  263.                                 time_count--;
  264.                                 if (!time_count)
  265.                                 {
  266.                                         flag++;
  267.                                         fprintf(stderr,"Command timeout waiting for ACK.\n");
  268.                                         retval = -1;
  269.                                 }
  270.                         }
  271.                         else
  272.                         {
  273.                                 x = (unsigned)inp(DATA_PORT);
  274.                                 if (x == 0xfe)
  275.                                 {
  276.                                         flag++;
  277.                                         fprintf(stderr,"Got command acknowledgement\n");
  278.                                 }
  279.                                 else
  280.                                 {
  281.                                         ack_count--;
  282.                                         if (!ack_count)
  283.                                         {
  284.                                                 printf("Too many data bytes without ACK\n");
  285.                                                 retval = -1;
  286.                                         }
  287.                                 }
  288.                         }
  289.                 }
  290.         }
  291.         return(retval);
  292. } /* send_command_4001 */
  293.  
  294. getmidi()
  295. {
  296.         while (inp(STATUS_PORT) & DATA_AVAIL_MASK)
  297.                 ; /* Wait for data available */
  298.         return(inp(DATA_PORT));
  299. }
  300.  
  301. statmidi()
  302. {
  303.         if ( inp(STATUS_PORT) & DATA_AVAIL_MASK) /* data available ? */
  304.                 return(0);
  305.         else
  306.                 return(1);
  307. }
  308.  
  309. sendmidi(val)
  310. int val;
  311. {
  312.         int timeout = 10000;
  313.  
  314.         unsigned status;
  315.         status=inp(STATUS_PORT);
  316.         while (status & DATA_READY_MASK)
  317.                 printf("Busy; read %d ",getmidi());
  318.         /*
  319.         This printf lets me know what's holding up the transfer; I usually get
  320.         254 back, unless I press a synth key, when I get the key press parameters.
  321.         */
  322.         outp(DATA_PORT,val);
  323. }
  324.  
  325. hello()
  326. {
  327.         send_command_4001(0xff);        /* reset */
  328.         send_command_4001(0x96);        /* kill RT? */
  329.         send_command_4001(0x94);        /* ?? */
  330.         send_command_4001(0x90);        /* ?? */
  331.         send_command_4001(0x3f);        /* go into uart mode */
  332. }
  333.  
  334. bye()
  335. {
  336.         send_command_4001(0xff);        /* reset */
  337.         windexit(0);
  338. }
  339.  
  340.