        IFEQ    FW_VBL_MUSIC_IRQ
        fail    "FW_VBL_MUSIC_IRQ must be enabled"
        ENDC
        IFEQ    FW_DYNAMIC_MEMORY_SUPPORT
        fail    "FW_DYNAMIC_MEMORY_SUPPORT must be enabled"
        ENDC

        IF      (FW_MUSIC_PLAYER_CHOICE==6)
        IFND    FW_PRETRACKER_JITTER_BUFFER_SIZE
FW_PRETRACKER_JITTER_BUFFER_SIZE = 38*4 ; default is 37 insts + end of copperlist
        ENDC
        IFND    FW_PRETRACKER_NUM_JITTER_BUFFERS
FW_PRETRACKER_NUM_JITTER_BUFFERS = 50
        ENDC
        ENDC

fw_MusicAlloc:
        move.l  #pv_SIZEOF+sv_SIZEOF,d0
        bsr     fw_AllocFast
        move.l  a0,fw_PretrackerMyPlayer(a6)
        lea     pv_SIZEOF(a0),a0
        move.l  a0,fw_PretrackerMySong(a6)
        IF      (FW_MUSIC_PLAYER_CHOICE==6)
        move.l  #FW_PRETRACKER_NUM_JITTER_BUFFERS*FW_PRETRACKER_JITTER_BUFFER_SIZE,d0
        bsr     fw_AllocChip
        move.l  a0,fw_PretrackerCopperlistStart(a6)
        move.l  a0,fw_PretrackerCopperWritePtr(a6)
        lea     (FW_PRETRACKER_NUM_JITTER_BUFFERS-1)*FW_PRETRACKER_JITTER_BUFFER_SIZE(a0),a0
        move.l  a0,fw_PretrackerCopperReadPtr(a6)
        lea     FW_PRETRACKER_JITTER_BUFFER_SIZE(a0),a0
        move.l  a0,fw_PretrackerCopperlistEnd(a6)
        move.l  #ft_SIZEOF,d0
        bsr     fw_AllocFast
        move.l  a0,fw_PretrackerBgTask(a6)
        ENDC
        rts

fw_MusicInit:
        PUTMSG  10,<"%d: Pretracker song init %p %p">,fw_FrameCounter-2(a6),a0,a1
        move.l  a0,a2
        PUSHM   d4-d7/a1/a4-a6
        ;move.l fw_PretrackerMyPlayer(a6),a0 ; unused
        move.l  fw_PretrackerMySong(a6),a1
        PUTMSG  10,<"MyPlayer=%p MySong=%p Data=%p">,a0,a1,a2
        bsr     pre_SongInit
        POPM

        move.l  a1,d1
        bne.s   .noalloc
        PUTMSG  10,<"Allocating %ld bytes samples memory for Pretracker">,d0
        bsr     fw_AllocChip
        move.l  a0,fw_MusicSamples(a6)
        move.l  a0,a1
.noalloc

        PUTMSG  10,<"%d: Pretracker player init">,fw_FrameCounter-2(a6)
        move.l  fw_PretrackerMyPlayer(a6),a0
        move.l  fw_PretrackerMySong(a6),a2
        lea     fw_PretrackerProgress(a6),a3
        PUTMSG  10,<"Progress at %p">,a3
        clr.l   (a3)
        PUSHM   d4-d7/a4-a6
        bsr     pre_PlayerInit
        POPM

        IF      (FW_MUSIC_PLAYER_CHOICE==6)
        move.l  fw_PretrackerBgTask(a6),a1
        move.b  #20,LN_PRI(a1)
        lea     fw_PretrackerAheadTask(pc),a0
        bsr     fw_AddTask
        ENDC

        lea     fw_MusicPlay(pc),a0
        move.l  a0,fw_MusicTickRoutine(a6)

        PUTMSG  10,<"%d: Pretracker init done">,fw_FrameCounter-2(a6)
        rts

        IF      (FW_MUSIC_PLAYER_CHOICE==6)
fw_PretrackerAheadTask:
        move.l  fw_PretrackerCopperWritePtr(a6),a1
.loop
        move.l  a1,fw_PretrackerCopperWritePtr(a6)
        move.l  fw_PretrackerMyPlayer(a6),a0
        PUSHM   a1/a5/a6
        bsr     pre_PlayerTick
        POPM
        lea     FW_PRETRACKER_JITTER_BUFFER_SIZE(a1),a1
        cmp.l   fw_PretrackerCopperlistEnd(a6),a1
        bne.s   .nowrap
        move.l  fw_PretrackerCopperlistStart(a6),a1
.nowrap
.retry
        cmp.l   fw_PretrackerCopperReadPtr(a6),a1
        bne.s   .loop
        bsr     fw_Yield
        bra.s   .retry

fw_MusicPlay:
        move.l  fw_PretrackerCopperReadPtr(a6),a1
        lea     FW_PRETRACKER_JITTER_BUFFER_SIZE(a1),a1
        cmp.l   fw_PretrackerCopperlistEnd(a6),a1
        bne.s   .nowrap
        move.l  fw_PretrackerCopperlistStart(a6),a1
.nowrap
        move.l  a1,fw_PretrackerCopperlist(a6)

        cmp.l   fw_PretrackerCopperWritePtr(a6),a1
        bne.s   .good
        PUTMSG  10,<"Pretracker buffer underflow">
        rts
.good   move.l  a1,fw_PretrackerCopperReadPtr(a6)
        rts

fw_MusicStop:
        move.l  fw_PretrackerBgTask(a6),a1
        bsr     fw_RemTask
        clr.l   fw_MusicTickRoutine(a6)
        move.w  #DMAF_AUDIO,dmacon(a5)
        ; unsupported right now
        rts

        ELSE

fw_MusicPlay:
        PUSHM   d4-d7/a4-a6
        move.l  fw_PretrackerMyPlayer(a6),a0
        IF      (FW_MUSIC_PLAYER_CHOICE>=5)
        move.l  fw_PretrackerCopperlist(a6),a1
        ENDC
        bsr.s   pre_PlayerTick
        POPM
        rts

fw_MusicStop:
        clr.l   fw_MusicTickRoutine(a6)
        move.w  #DMAF_AUDIO,dmacon(a5)
        ; unsupported right now
        rts
        ENDC

;--------------------------------------------------------------------
; sets the position if supported
; d0.w = new position
fw_MusicSetPosition:
        move.l  fw_PretrackerMyPlayer(a6),a0
        move.w  d0,pv_curr_pat_pos_w(a0)
        rts

;--------------------------------------------------------------------
PRETRACKER_COPPER_OUTPUT = FW_MUSIC_PLAYER_CHOICE-4
PRETRACKER_DONT_TRASH_REGS = 0

        include "musicplayers/raspberry_casket.asm"
