    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

@Endless

    move.l #5, d0
    lea (Address), a0
    jsr Receive
    lea (address), a0
    moveq.l #5, d4
    move.l #4, d0
    move.l #4, d1
@printheader
    move.b (a0)+, d2
    jsr Print_HEXByte
    addq.l #3, d0
    dbf d4, @printheader
    move.l (address), a1
    moveq.l #0, d0
    move.w (address+4), d0
    lea (address), a0
    jsr receive
    jsr $FF0000 ; Jump to RAM
    bra.s @Endless

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

    even
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;,
    ; DEM FACKIN' SUBROUTINES
    include "./subs.asm"
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;,
        
ROM_End: ; The End, you lost the game-

    align $40000
    incbin "wave.snd"


