    include header.asm
    include "./receive.asm"
    
CodeStart:

; Initialize VDP ETc
    move #$2700, sr ; disable interrupts
    
    move.b  ($A10001), d0           ; Disable TMSS if needed
    and.b   #$0F, d0
    beq.s   @NoTMSS
    move.l  #"SEGA", ($A14000)        ; SAYYYYGAAAAAAAAAAAAAAAAAAAAAAH
@NoTMSS:


    lea     (VDPControl), a0           ; Init VDP
    move.w  #$8014, (a0)              ; No IRQ4, no HV latch
    move.w  #$8174, (a0)              ; Disable display
    move.w  #$8230, (a0)              ; Scroll A: $C000
    move.w  #$8407, (a0)              ; Scroll B: $E000
    move.w  #$8578, (a0)              ; Sprites: $F000
    move.w  #$8700, (a0)              ; Background: pal 0, color 0
    move.w  #$8B00, (a0)              ; No IRQ2, full scroll
    move.w  #$8C81, (a0)              ; H40, no S/H, no interlace
    move.w  #$8D3E, (a0)              ; HScroll: $F800
    move.w  #$8F02, (a0)              ; Autoincrement: 2 bytes
    move.w  #$9001, (a0)              ; Scroll size: 64x32
    move.w  #$9100, (a0)              ; Hide window plane
    move.w  #$9200, (a0)              ;  "     "      "
        
    moveq   #0, d0                  ; Clear VRAM, necessary to do for real hardware
    move.l  #$40000000, (a0)
    lea     (VDPData), a1
    move.w  #$800-1, d1
@ClearVRAM:
    rept 8
    move.l  d0, (a1)
    endr
    dbf     d1, @ClearVRAM

    move.l  #$C0000000, (a0)        ; Clear CRAM
    moveq   #8-1, d1
@ClearCRAM:
    rept 4
    move.l  d0, (a1)
    endr
    dbf     d1, @ClearCRAM

    move.l  #$40000010, (a0)        ; Clear VSRAM
    move.l  d0, (a1)
    
    ;Init Joypads;
    move.b #$40, ($A10009)
    move.b #$40, ($A1000B)
    ;;;;;;;;;;;;;;
   
    lea (Font), a6
    lea (RLEData), a5
    bsr RunLengthDecoding
  
    lea (RLEData), a0
    moveq #0, d0
    move.w #128, d1
    bsr Load_Tiles
    
    lea (IntroText1), a0
    moveq #0, d0
    moveq #0, d1
    bsr Print_String
    
    move.w #$4E73, (RAM_HINT)
    move.w #$4E73, (RAM_VINT)
	
    move.l #$C0020000, (VDPControl)
    move.w #$0EEE, (VDPData)
    
    move.w  #$8174, ($C00004)       ; Enable display, interrupts, all the good shit
    move.w 	#$8B08, (VDPControl)

	move #$2000, sr                    ; DEM INTERRUPTS   

address equ $FF0000


z80request macro
    move.w #$100, ($A11100)
@waitz80:
    btst.b #0, ($A11100)
    bne.s @waitz80
    endm

z80release macro
    move.w #$000, ($A11100)
    endm

z80reset macro
    move.w #$000, ($A11200)
    rept $10
    nop
    endr
    move.w #$100, ($A11200)
    endm
    
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, 5, $00  
   FMSetParam $B6, 6, $C0 

    moveq #0, d3
    moveq #0, d2
@Loopers
    FMSetParam $2B, 0, $FF
    FMSetParam $2C, 0, $00
Init:
    lea (sample), a1
    move.l #sample_end-sample-1, d0   
	WaitYM
    move.b #$2A, ($A04000)
@Samples
    move.b (a1)+, ($A04001)
    dbf d0, @Samples
    
    bra Init

;;; 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:
 


IntroText1: dc.b 'Start transfer.exe and send a program!', 0

    even
    
    
ROM_End: ; The End, you lost the game-


