    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

FMNoteOn2 macro chan, freq
    move.w \freq, -(a7)
    WaitYM
    move.b #chan&%011+$a4, ($A04000+(chan>>2)*2)
    ror   #8, \freq
    move.b \freq, ($A04001+(chan>>2)*2)
    WaitYM
    move.b #0, \freq
    rol    #8, \freq
    move.b #chan&%011+$A0, ($A04000+(chan>>2)*2)
    move.b \freq, ($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)
    move.w (a7)+, \freq
    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

   FMSetParam $B4, 0, $00    
   FMSetParam $B5, 1, $00    
   FMSetParam $B6, 2, $00    
   FMSetParam $B4, 4, $00  
   FMSetParam $B5, 6, $00 

    moveq #0, d3
    moveq #0, d2
@Loopers
    FMSetParam $2B, 0, $FF
    FMSetParam $2C, 0, $f8
    moveq #0, d5
Init:
   moveq #0, d4
   moveq #0, d2
   moveq #5, d3
   moveq #0, d6
   moveq #0, d5
   moveq #0, d7
    lea (sample), a1
    move.l #sample_end-sample-1, d0     
@Samples
    WaitYM
    move.b #$2A, ($A04000)
    move.b (a1)+, ($A04001)
    move.l #43, d7
    nop
    dbf d7, *-2

    addq.w #1, d2
    cmp.w #2000, d2
    bne.w @carryon
; d3 how many l
; d4 how many r

    moveq #0, d2
@noreset:
    tst.b d5
    bne @d51
@d50:
    subq.b #1, d3
    addq.b #1, d4
    bra.s @d5not0
@d51:
    addq.b #1, d3
    subq.b #1, d4
@d5not0:
    
; PROCESS ALL CHANNELS

   ; for i = 1 to d3
   ; set l
   ; next i
   ; for i = 1 to d4
   ; set r
   ; next i 
    movem.l d3-d5/a5, -(a7)
    moveq #0, d5
    move.l #0, ($FFF000)
    move.l #0, ($FFF004)
    tst.b d3
    beq.s @ProcessR
    subq #1, d3
    lea ($FFF000), a5
@LoopL:
    or.b #$80, (a5)+    
    dbf d3, @loopL
@ProcessR:
    tst.b d4
    beq @NoR
    subq #1, d4 
    lea ($FFF000), a5
@LoopR:
    or.b #$40, (a5)+    
    dbf d4, @loopR
@NoR:
    lea ($FFF000), a5
    WaitYM
    move.b #$B4, ($A04000)
    move.b (a5)+, ($a04001)
    WaitYM
    move.b #$B5, ($A04000)
    move.b (a5)+, ($a04001)
    WaitYM
    move.b #$B6, ($A04000)
    move.b (a5)+, ($a04001)
    WaitYM
    move.b #$B4, ($A04002)
    move.b (a5)+, ($a04003)
    WaitYM
    move.b #$B6, ($A04002)
    move.b (a5)+, ($a04003)
    WaitYM
    movem.l (a7)+, d3-d5/a5   
    cmp.b #0, d3
    bne.s @d3not0
    move.b #$01, d5
    bra.s @carryon
@d3not0:
    cmp.b #0, d4
    bne.s @d4not0
    moveq #0, d5
@d4not0:
@carryon:
    dbf d0, @Samples
    
    bra Init

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


fm:
     incbin "./c.eif"

;    even
;    hex 1234173598206789027439087358927387258757587286197610762575
;    hex 1301700000131124001b1f1f1f0d1f190d0a0b11069b1f0f7f00000000

    even


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