;---------------------------------------------------------------------
; midiio.asm
;
; (C) 1991 Steve Simpson
;
; Date: 910925
;
; Handles MIDI input and output.
;
;
; Assemble:  Manx Aztec v5.0d
;            as -l midiio 
;---------------------------------------------------------------------

 SECTION    CODE

 NOLIST
 INCLUDE "exec/types.i"
 INCLUDE "asmsupp.i"
 INCLUDE "hardware/custom.i" 
 INCLUDE "hardware/intbits.i"
 LIST


 SECTION   DATA

TBE            equ      13           ;Transmit Buffer Empty bit
RBE            equ      14           ;Receive Bffer Empty bit
CUSTOM_CHIPS   equ      $dff000      ;custom chips base address


 SECTION   CODE
;---------------------------------------------------------------------
; external definitions and references
;---------------------------------------------------------------------
 XREF   MIDISend
 XREF   MIDIRead


;--------------------------------------------------------------------
; MIDISend
;
; Sends a byte stream out onto the MIDI network.
;
; registers:	input:	a0	 - pointer to buffer
;			d0:0-15  - number bytes to send
;--------------------------------------------------------------------
MIDISend:
   movem.l      a1-a2,-(sp)

   lea          CUSTOM_CHIPS,a1  ;load custom chip base

   sub.l        #1,d0            ;(D0)-1 bits to send (count relative 0)
   move.l       a0,a2            ;working buffer pointer

;
;the MIDI transmit data loop
;
ms_outloop:

;
;wait until the transmit buffer is empty
;
   move.w	#TBE,d1
   jsr		WaitMIDIPort

;
;transmit one byte 
;
   move.b       (a2)+,d1         ;data byte into D1
   andi.w       #$ff,d1          ;clear high byte
   ori.w        #$100,d1         ;set stop bit
   move.w       d1,serdat(a1)    ;send byte

;
;keep going until D0=0
;
   dbra         d0,ms_outloop    ;keep going for (D0)-1 times


   movem.l      (sp)+,a1-a2
   rts                           ;return to caller



;--------------------------------------------------------------------
; MIDIRead
;
; Gets a byte stream from the MIDI network.
;
; registers:	input:	a0	- pointer to data buffer
;			d0:0-15	- number of bytes to send
;--------------------------------------------------------------------
MIDIRead:
   movem.l      a1-a2,-(sp)

   lea          CUSTOM_CHIPS,a1  ;load custom chip base

   sub.l        #1,d0            ;(D0)-1 bits to send (count relative 0)
   move.l       a0,a2            ;working buffer pointer

;
;clear RBF bit in INTREQ and SERDATR registers
;
   move.w	#INTF_RBF,intreq(a1)

;
;the receive MIDI data loop
;
mr_inloop:

;
;wait until there is some data in the UART
;
   move.w	#RBE,d1
   jsr		WaitMIDIPort

;
;now get the data from the UART SERDATR register
;
   move.w	serdatr(a1),d1		 ;get the byte from MIDI
   move.b       d1,(a2)+		 ;save byte in buffer

;
;clear RBF bit in INTREQ and SERDATR registers
;
   move.w       #INTF_RBF,intreq(a1)

;
;if DO<>0 then keep going
;
   dbra         d0,mr_inloop     ;keep going for (D0)-1 times


   movem.l      (sp)+,a1-a2
   rts                           ;return to caller


;---------------------------------------------------------------------
; WaitMIDIPort
;
; registers:	input:	a1 - custom chips base address
;			d1 - bit number to wait on
;---------------------------------------------------------------------
WaitMIDIPort:

wmp_loop:
   btst.b	d1,serdatr(a1)		;test the bit
   beq		wmp_loop

   rts

   
   END
