5-4
MIDI Port I/O Programming
Reading MIDI Data
When MIDI data is available from an external MIDI device, it can be read in from
the DSP data port. As mentioned before, there are two operational modes for
detecting the availability of in-bound MIDI data: polling mode or interrupt mode.
When in polling mode, in-bound MIDI data is available when bit 7 of the DSP
Read-Buffer Status port is set. Conversely, when bit 7 is clear, no MIDI data is
available.
In interrupt mode, an interrupt occurs when MIDI data is available. An interrupt
service routine has to be set up to retrieve the MIDI data. A read of the DSP
Read-Buffer Status port will clear the interrupt signal.
The sequence for reading MIDI data is identical in both polling and interrupt modes.
Below is an assembly code fragment that can be used in either a polling routine or an
interrupt service routine:
mov
dx,wSBCBaseAddx
; SBC base I/O address 2
x0h
add
dl,0Eh
; Read-Buffer Status port, 2
xEh
Busy:
in
al,dx
; Read Read-Buffer Status port
or
al,al
; Data available?
jns
Busy
; Bit 7 clear, try again
sub
dl,4
; Read Data port, 2
xAh
in
al,dx
; Read in-bound DSP data
The in-bound MIDI data is not tagged with timing information unless the MIDI time
stamp mode is used. Therefore, it is up to the application to handle the timing for the
MIDI data.
Under MIDI time stamp mode, in-bound MIDI data is tagged with 3 bytes of timing
information. Hence, 4 bytes of data are always available whenever there is any
in-bound MIDI data. The procedure of retrieving the MIDI data together with the
timing information is the same except you can now read 4 bytes at a time within the
interrupt service routine.
The MIDI time stamp is a 3-byte quality that measures time in units of milliseconds.
The packing format of time stamped in-bound MIDI data is as follows:
nTime.LowByte, nTime.MidByte, nTime.HighByte, bMidiData