home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / midi / k1patche / k1patche.sha / io401.c next >
Encoding:
C/C++ Source or Header  |  1994-03-16  |  2.0 KB  |  92 lines

  1. /*********************************************************************/
  2. /* Copyright 1989 by H. Edward Hall                                  */
  3. /*                                                                   */
  4. /* Permission is hereby granted to copy and to give away this        */
  5. /* software as long as this notice is preserved in its entirety.     */
  6. /* You may modify the software or use it for any purpose just as     */
  7. /* long as you preserve this notice and do not sell the software.    */
  8. /* No warranty of any kind is made for this software.  The holder    */
  9. /* of this copyright reserves the right to enhance and/or sell       */
  10. /* this software with no obligation to provide updates or further    */
  11. /* free copies; if this should happen you may still use this version */
  12. /* of the software under the terms you received it with.             */
  13. /*********************************************************************/
  14.  
  15. #ifdef TURBOC
  16. #include <dos.h>
  17. #endif TURBOC
  18. #include "mpu401.h"
  19.  
  20. #define NBUFFERED 1024
  21.  
  22. /*
  23.  * Tickle the MPU's registers.
  24.  */
  25. /*
  26.  * The following should be about 3000 for an AT, 1000 for a PC;
  27.  */
  28. #define DELAYFACT 3000
  29.  
  30. static unsigned char databuf[NBUFFERED];
  31. static int nbuffered = 0;
  32. int
  33. putcmd(cmd)
  34. unsigned char cmd;
  35. {
  36.     register int x;
  37.  
  38.     x = 0;
  39.     while ((inportb(STATUS_PORT)&DRR) != 0)
  40.         if (x++ > DELAYFACT)
  41.             return -1;
  42.  
  43.     disable();
  44.     outportb(STATUS_PORT, cmd);
  45.     for (;;)
  46.     {
  47.         x = 0;
  48.         while ((inportb(STATUS_PORT)&RDY) != 0)
  49.             if (x++ > DELAYFACT)
  50.             {
  51.                 enable();
  52.                 return -1;
  53.             }
  54.         x = inportb(DATA_PORT)&0xff;
  55.         if (x != ACK)
  56.             databuf[nbuffered++] = (unsigned char)x;
  57.         else
  58.             break;
  59.     }
  60.     enable();
  61.     return ACK;
  62. }
  63.  
  64. int
  65. getdata()
  66. {
  67.     register int x;
  68.  
  69.     if (nbuffered > 0)
  70.         return (int)databuf[--nbuffered];
  71.     x = 0;
  72.     while ((inportb(STATUS_PORT)&RDY) != 0)
  73.         if (x++ > DELAYFACT)
  74.             return -1;
  75.     return (inportb(DATA_PORT)&0xff);
  76. }
  77.  
  78. int
  79. putdata(val)
  80. unsigned char val;
  81. {
  82.     register int x;
  83.  
  84.     x = 0;
  85.     while ((inportb(STATUS_PORT)&DRR) != 0)
  86.         if (x++ > DELAYFACT)
  87.             return -1;
  88.  
  89.     outportb(DATA_PORT, val);
  90.     return (int)val;
  91. }
  92.