
; DeliMIDI.s
; A quick hack in 1998 by Alastair M. Robinson to serve as an example of
; using the B5MIDI routines from machine code.  Note that _SysBase
; and _DOSBase are made externally visible - they're needed by the B5MIDI
; routine.  Your Machine Code program (or in this case, Delitracker) must
; open the dos.library and setup these two bases.

;
; This file was assembled with SNMA 2.02 and linked with B5MIDI.o and Amiga.lib
; using PhxLnk 4.31.
; (I'd have used PhxAss, but it can't cope with the naughty TAG_USER + "DT"
; definition.  SNMA treats the "DT" as a 16 bit number expressed in ASCII.)

        incdir  "text_include:"
        include "dos/dos.i"
        include "intuition/intuition.i"
        include "work:Music/Delitracker/Developer/include/misc/DevpacMacros.i"
        include "work:Music/Delitracker/Developer/include/misc/DeliPlayer.i"

;
;
        SECTION Player,Code
;
;

        PLAYERHEADER PlayerTagArray                     ; define start of header

        dc.b '$VER: DeliMIDI 1.0 (5 Jun 98)',0      ; for OS 2.0 version command
        even

delibase        dc.l 0

        XDEF    _SysBase
        XDEF    _DOSBase

_DOSBase       dc.l 0
_SysBase       dc.l 0



PlayerTagArray
        dc.l    DTP_Flags,PLYF_SONGEND|PLYF_ANYMEM
        dc.l    DTP_RequestDTVersion,16                 ; define all the tags
        dc.l    DTP_PlayerVersion,01<<16+00
        dc.l    DTP_PlayerName,PName                    ; for the player
        dc.l    DTP_Creator,CName
        dc.l    DTP_DeliBase,delibase
        dc.l    DTP_Check2,Chk                          ; omit any unused
        dc.l    DTP_InitPlayer,InitPlay
        dc.l    DTP_EndPlayer,EndPlay
        dc.l    DTP_Stop,Stop
        dc.l    DTP_StartInt,StartSnd
        dc.l    DTP_StopInt,StopSnd
        dc.l    TAG_DONE                                ; signify end of tags

*-----------------------------------------------------------------------*
;
; Playername / creatorname
;
*-----------------------------------------------------------------------*

PName   dc.b    'DeliMIDI',0
CName   dc.b    'Written by Alastair M. Robinson,',10
        dc.b    'as an example of using the B5MIDI',10
        dc.b    'routines from machine code!',0
        even


*-----------------------------------------------------------------------*
;
; Check if the module is a TestPlayer-Module (THIS ROUTINE MUST EXIST!!!)
;
*-----------------------------------------------------------------------*

Chk
        move.l  delibase,a0                     ; ^DeliBase
        move.l  dtg_ChkData(a0),a0              ; get module base from DT
        moveq   #0,d0                           ; clear register
        cmpi.l  #'MThd',(a0)                    ; supported type ?
        bne     .error                           ; no - signal false
        move.w  (8,a0),d1
        cmp.w   #2,d1
        bge     .error
        moveq   #0,d0
        rts                                     ; leave

.error
        moveq   #-1,d0
        rts

*-----------------------------------------------------------------------*
;
; Initialize the player
;
*-----------------------------------------------------------------------*

        XREF    _MIDI_Setup
        XREF    _InitMIDIFile

InitPlay
        move.l  delibase,a5
        move.l  (dtg_DOSBase,a5),_DOSBase
        move.l  4,_SysBase
        jsr     _MIDI_Setup
        tst.l   d0
        beq     .error

        pea     0                       ; No need to set a tempo.
        move.l  (dtg_ChkData,a5),-(a7)  ; Is this where you're supposed to get
        jsr     _InitMIDIFile           ; the module pointer?
        lea     (8,a7),a7
        rts

.error
        moveq   #0,d0
        rts


*-----------------------------------------------------------------------*
;
; Clean up the player
;
*-----------------------------------------------------------------------*

        XREF    _MIDI_Cleanup

EndPlay
        jmp     _MIDI_Cleanup


*-----------------------------------------------------------------------*
;
; Start interrupts - (Splitting hairs: B5MIDI isn't interrupt based!)
;
*-----------------------------------------------------------------------*

        XREF    _PlayMIDIFile
StartSnd
        pea     songendhandler
        jsr     _PlayMIDIFile
        lea     (4,a7),a7
        rts

songendhandler
        movem.l d1-d7/a1-a6,-(a7)

        move.l  delibase,a5
        move.l  (dtg_SongEnd,a5),a0
        jsr     (a0)            ; Tell DeliTracker it can now change the song
                                ; if it wants.

        pea     songendhandler
        jsr     _PlayMIDIFile   ; And set the MIDI file playing again.
        lea     (4,a7),a7

        movem.l (a7)+,d1-d7/a1-a6
        rts

*-----------------------------------------------------------------------*
;
; Stop Interrupts
;
*-----------------------------------------------------------------------*

        XREF    _StopMIDIFile
StopSnd
        jmp     _StopMIDIFile

*-----------------------------------------------------------------------*
;
; Stop
;
*-----------------------------------------------------------------------*

Stop
        jmp     _StopMIDIFile

        END

