#include typedef unsigned char byte; int header[8] = {0xf0, 0x40, 0x00, 0x22, 0x00, 0x04, 0x00, 0x00}; int request[9] = {0xf0, 0x40, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0xf7}; int head; #define WAIT( port, flag ) while ( (inp(port)&flag) != 0 ) #define COMDPORT 0x331 /* MPU-401 Command Port on IBM */ #define STATPORT 0x331 /* MPU-401 Status Port on IBM */ #define DATAPORT 0x330 /* MPU-401 Data I/O Port on IBM */ #define DRR 0x40 /* Mask for Data Read Reg. Bit */ #define DSR 0x80 /* Mask for Data Set Ready Bit */ #define ACK 0xFE /* MPU-401 Acknowledge Response */ #define MASKFLIP 0xFF /* WAIT Function Bit Mask XOR */ #define MPURESET 0xFF /* MPU-401 Total Reset Command */ #define UARTMODE 0x3F /* MPU-401 "Dumb UART Mode" */ #define NOTEON1 0x90 /* MIDI Note On for Channel 1 */ #define VELOCITY 64 /* MIDI Medium Key Velocity */ #define NOTEOFF 0 /* 0 Velocity = Note Off */ #define FIRSTNOTE 36 /* First note synth can play */ #define LASTNOTE 96 /* Last note synth can play */ /*************************************************************************** * I N I T I A L I Z A T I O N * ***************************************************************************/ RstMPU() { int a; /* Clear Display */ outp( COMDPORT, MPURESET ); /* Send MPU-401 RESET Command */ a = inp( DATAPORT ); /* Dummy read to clear buffer */ WAIT( STATPORT, DRR ); /* wait for port ready */ outp( COMDPORT, UARTMODE ); /* Set MPU-401 "Dumb UART" Mode */ a = inp(DATAPORT); /* Dummy Read to clear buffer */ WAIT( STATPORT, DSR ); /* Wait for "UART" port ready - */ /* Really crucial! */ } /*************************************************************************** * M A I N P R O G R A M * ***************************************************************************/ snd (i) int i; { int a; outp (DATAPORT, i); a=inp(DATAPORT); WAIT(STATPORT, DRR); } int rcv() { int a; int i; loop: WAIT (STATPORT, DSR); i=inp (DATAPORT); if (head && ((byte)i==(byte)0xfe)) goto loop; return (i); } main(argc, argv) int argc; char *argv[]; { FILE *fp; int i; int cksum; int ch, j; RstMPU(); if (argc != 2) { printf ("Usage: upload \n"); exit(-1); } printf ("Upload to %s...", argv[1]); if ((fp = fopen(argv[1], "wb")) == NULL) { printf ("cannot open\n"); exit(-1); } for (i=0; i<9; i++) snd (request[i]); head = 1; cksum = 0; for (i=0; i<8; i++) { if ((j=rcv()) != (header[i])) printf ("yikes - 0x%x expected, got 0x%x\n", header[i], j); fputc (j, fp); cksum++; } head = 0; ch=rcv(); while ((ch & 0xff) != 0xf7) { cksum++; fputc(ch, fp); ch=rcv(); } if ((byte)ch != (byte)0xf7) printf ("Bum\n"); fputc (ch, fp); cksum++; printf ("\n Done - received %d bytes\n", cksum); fclose (fp); }