    ORG $FF0000
;   ORG $E00000 <-- this works too, btw :P 

;;; MACROS AND EQUATES !!

    include "./macros.asm"

WaitYM macro

    btst.b #7, ($a04000)
    bne.s *-8
    endm

FMNoteOn macro chan, freq
    WaitYM
    move.b #chan&%011+$a4, ($A04000+(chan>>2)*2)
    move.b #freq>>8, ($A04001+(chan>>2)*2)
    WaitYM
    move.b #chan&%011+$A0, ($A04000+(chan>>2)*2)
    move.b #freq&$00FF, ($A04001+(chan>>2)*2)
    WaitYM
    move.b #$28, ($A04000)
    move.b #chan&%00000111, ($A04001+(chan>>2)*2)
    WaitYM
    move.b #(chan&%011)|%11110000, ($A04001+(chan>>2)*2)
    endm

LoadFM macro source, chan
    move.l a0, -(a7)
    move.l d0, -(a7)
    WaitYM
    move.b #(chan&%011)+$B0, ($A04000+(chan>>2)*2)
    lea (\source), a0
    move.l d1, -(a7)
    move.b (a0)+, ($A04001+(chan>>2)*2)
    move.b #(chan&%011)+$30, d0
    moveq #$1b, d1
    WAITYM                              ;10 bytes
    move.b d0, ($A04000+(chan>>2)*2)    ;06 bytes
    move.b (a0)+, ($A04001+(chan>>2)*2) ;06 bytes
    addq.b #4, d0                       ;02 bytes
    dbf d1, *-24                ;*-26
    move.l (a7)+, d1
    move.l (a7)+, d0
    move.l (a7)+, a0
    endm

FMsetParam macro port, chan, data
    WaitYM
    move.b #port, ($A04000+(chan>>2)*2)
    move.b #data, ($A04001+(chan>>2)*2)
    endm

FMwriteParam2 macro port, chan, data
    WaitYM
    move.b #port, ($A04000+(chan>>2)*2)
    move.b \data, ($A04001+(chan>>2)*2)
    endm


EntryPoint:

; Do something here ;D

    z80reset
    z80request
;    lea (z80wave), a0
;    lea ($A00000), a1
;    move.l #z80wave_end-z80wave, d0

;@copyz80
;    move.b (a0)+, (a1)+
;    dbf d0, @copyz80
;
;    z80reset
;    z80release

    moveq #0, d6

    LoadFM fm, 0
    FMSetParam $B4, 0, $80    

    LoadFM fm, 1
    FMSetParam $B5, 1, $80    
;
    LoadFM fm, 2
    FMSetParam $B6, 2, $80    

   LoadFM fm, 4
   FMSetParam $B4, 4, $80    

   LoadFM fm, 5
   FMSetParam $B5, 5, $80    

    moveq #0, d4
    moveq #0, d3
    moveq #0, d2
@Loopers
    FMSetParam $2B, 0, $FF
    FMSetParam $2C, 0, $FF
    moveq #0, d5
Init:
    moveq #0, d4
    btst #0, d6
    bne.w @fm0
    FMSetParam $B4, 0, $40    
    FMSetParam $B5, 1, $40    
    FMSetParam $B6, 2, $40    
    FMSetParam $B4, 4, $40    
    FMSetParam $B5, 5, $40    
    bset #0, d6
    bra.w @fm1
@fm0:
    FMSetParam $B4, 0, $80    
    FMSetParam $B5, 1, $80    
    FMSetParam $B6, 2, $80    
    FMSetParam $B4, 4, $80    
    FMSetParam $B5, 5, $80    
    bclr #0, d6
@fm1:
    fmnoteon 0, $2269
    fmnoteon 1, $2269
    fmnoteon 2, $2269
    fmnoteon 4, $2269
    fmnoteon 5, $2269

    lea (sample), a1
    move.l #sample_end-sample-200, d0     
@Samples
    WaitYM
    move.b #$2A, ($A04000)
    move.b (a1)+, ($A04001)
    move.l #43, d5
    nop
    dbf d5, *-2
    addq.l #1, d2
    cmp.l #(sample_end-sample)*1/20, d2
    bne.w @carryon
    moveq #0, d2

 

    FMWriteParam2 $40, 0, d4
    FMWriteParam2 $41, 1, d4
    FMWriteParam2 $42, 2, d4
    FMWriteParam2 $40, 4, d4
    FMWriteParam2 $41, 5, d4
    FMWriteParam2 $42, 6, d4

    FMWriteParam2 $44, 0, d4
    FMWriteParam2 $45, 1, d4
    FMWriteParam2 $46, 2, d4
    FMWriteParam2 $44, 4, d4
    FMWriteParam2 $45, 5, d4
    FMWriteParam2 $46, 6, d4

    FMWriteParam2 $48, 0, d4
    FMWriteParam2 $49, 1, d4
    FMWriteParam2 $4a, 2, d4
    FMWriteParam2 $48, 4, d4
    FMWriteParam2 $49, 5, d4
    FMWriteParam2 $4a, 6, d4

    FMWriteParam2 $4c, 0, d4
    FMWriteParam2 $4d, 1, d4
    FMWriteParam2 $4e, 2, d4
    FMWriteParam2 $4c, 4, d4
    FMWriteParam2 $4d, 5, d4
    FMWriteParam2 $4e, 6, d4

    btst #0, d6
    addq #1, d4

@carryon
    dbf d0, @Samples
    
    bra Init

TestString:
    dc.b "Testing", 0
    even
;;; SUBROUTINES!!
    include "./subs.asm"


fm:
    incbin "./a.eif"

    even

;    hex 1301700000131124001b1f1f1f0d1f190d0a0b11069b1f0f7f00000000

    even


sample: 
    incbin "./sample.snd"
sample_end:
    even
;;; Z80
;z80wave:
;    incbin "./z80wave.bin"
;z80wave_end:
 
