;---------------------------------------------------------------------
; resource.asm
;
; (C) 1991 Steve Simpson
;
; Date: 910925
;
; Handles opening and freeing of the serial port resource for MIDI.
;
;
; Assemble:  Manx Aztec v5.0d
;            as -l resource
;---------------------------------------------------------------------
 SECTION    CODE

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


;---------------------------------------------------------------------
; external definitions and references
;---------------------------------------------------------------------
 XREF    _AbsExecBase        
 XREF    AllocMIDIResource
 XREF    FreeMIDIResource
 XREF    SetMIDIBaud

;OpenResource is a ROM routine in the exec library
 XLIB    OpenResource

;the routines to allocate and free the resource have no C interface
;and so can only be accessed from assembler
 XLIB    mr_AllocMiscResource
 XLIB    mr_FreeMiscResource



 SECTION    DATA

;this is the name of the resource we want to open
MiscName    MISCNAME
;this is our name
MyName:      dc.b  'MIDI-Serial Port Example',0
      dc.w   0

_AbsExecBase   equ   4     ;the only fixed address in the Amiga

MIDI_BAUD      equ   114         ;MIDI baud rate set to 31250
CUSTOM_CHIPS   equ   $dff000     ;custom chips base address



 SECTION    CODE

;--------------------------------------------------------------------
; AllocMIDIResource
;
; Tries to open the misc.resource and alocate the serial port for our
; exclusive use - as is required, so as to avoid conflicts with other
; programs trying to use the serial port
;
; registers:   entry:   none
;              exit:    d0 - pointer to struct MiscResource or NULL
;--------------------------------------------------------------------
AllocMIDIResource:
   movem.l     a1/a6,-(sp)

   lea.l       MiscName(pc),a1
   CALLSYS     OpenResource         ;open "misc.resource"
   move.l      d0,d7                ;save resource pointer
   bne.s       amr_1$               ;jump if resource ok
   bra.s       amr_fail             ;jump to flag failure

amr_1$:
   exg.l       d7,a6                ;resource pointer into A6

;
;have now got pointer to resource base
;call the resources library-like vectors (these have no C interface
;and so must be called from assembler)
;
   move.l      #MR_SERIALBITS,d0    ;we want serial bits
   lea.l       MyName(pc),a1        ;use our name
   LINKSYS     mr_AllocMiscResource,a6
   tst.l       d0
   bne.s       amr_fail             ;someone else has the serial port

   move.l      #MR_SERIALPORT,d0
   lea.l       MyName(pc),a1
   LINKSYS     mr_AllocMiscResource,a6
   tst.l       d0
   bne.s       amr_fail             ;someone else has the serial port

   move.l      a6,d0                ;resource pointer into D0 for return
   bra.s       amr_exit             ;return with pointer to MiscResource

amr_fail:
   moveq       #0,d0                ;flag failure - return 0

amr_exit:
   movem.l     (sp)+,a1/a6
   rts                               ;return to caller


;--------------------------------------------------------------------
; FreeMIDIResource
;
; Frees the misc.resource serial port allocated for MIDI
;
; registers:   entry:   a0 - pointer to previously allocated resource
;--------------------------------------------------------------------
FreeMIDIResource:
   movem.l   a6,-(sp)      ;save A6
   exg.l     a0,a6         ;resource pointer into A6

;
;free the serial resource
;
   move.l    #MR_SERIALPORT,d0
   LINKSYS   mr_FreeMiscResource,a6
   move.l    #MR_SERIALBITS,d0
   LINKSYS   mr_FreeMiscResource,a6

fmr_exit:
   move.l    (sp)+,a6      ;restore A6
   rts


;--------------------------------------------------------------------
; SetMIDIBaud
;
; Sets the MIDI (serial port) baud rate.
; The baud rate is set to 31250 - the value to set in the custom
; chip SERPER register is derived from the equation on p199 of
; "Amiga System Programmer's Guide"
;
; registers:   entry:   none
;              exit:    none
;--------------------------------------------------------------------
SetMIDIBaud:
   lea       CUSTOM_CHIPS,a0      ;load custom chip base
   move.w    #MIDI_BAUD,d0        ;load baud rate into register
   move.w    d0,serper(a0)        ;write baud rate to PAULA register
   move.w    #INTF_RBF,d0         ;RBF bit to clear
   move.w    d0,intreq(a0)        ;clear bit in INTREQ register

   rts               ;return to caller



   END
