; ---------------------------------------------------------------------------
; Arkos player test using external YMZ
; ---------------------------------------------------------------------------
    LIST 

    include "firmware.i"

    ORG  $0100

; ---------------------------------------------------------------------------
start
    ; Get active disk drive and save it for after
    LD   HL,(AMSDOS_RESERVED_AREA)
    LD   A,(HL)                 ; Active disk drive for Amsdos (0 = A / 1 = B)
    LD   (disk_drive + 1),A
restart
    LD   C,$FF                  ; Disable ROMs
    LD   HL,main_program        ; HL = Start program address
    CALL MC_START_PROGRAM       ; Run program

main_program
    CALL TXT_VDU_DISABLE        ; Disable VDU
    CALL KL_ROM_WALK            ; Enable ROMS
    CALL TXT_VDU_ENABLE         ; Enable VDU

    ; Set active diskdrive for the Amsdos (the same used for load this program)
disk_drive
    LD   A,0
    LD   HL,(AMSDOS_RESERVED_AREA)
    LD   (HL),A

    CALL print_text

    ; load the song using the firmware
    LD   HL,song_filename       ; HL = address filename
    LD   B,10                   ; B = length filename
    CALL CAS_IN_OPEN
    LD   HL,$4000               ; HL = load address
    CALL CAS_IN_DIRECT
    CALL CAS_IN_CLOSE 

    ; Set a null interrupt handler
    DI
    LD   A,$FB                  ; EI (1)
    LD   HL,$4DED               ; RETI (4)
    LD   ($0038),A
    LD   ($0039),HL
    EI 

    ; Pen 1 White
    LD   A,1
    LD   BC,$7F00 + $4B
    OUT  (C),A
    OUT  (C),C

    ; Set CPC frequency in the YMZs
    LD   BC,$F880
    LD   HL,$7F01
    OUT  (C),H                  ; Set Timer 0 as clock for the YMZ
    OUT  (C),L                  ; 2 MHz clock for YMZ (CPC frequency)

    ; Select external YM
    XOR  A
    CALL PLY_SelectMusicAY

    ; Intialize player
    LD   DE,$4000               ; Song
    CALL PLY_Init

.infinite_loop
    CALL wait_refresh
    HALT
    HALT 
    HALT
    HALT

    ; Border raster
    LD   BC,$7F10
    OUT  (C),C
    LD   A,R
    AND  $1F
    OR   $40
    OUT  (C),A

    ; Play song
    CALL PLY_Play

    ; Border black
    LD   A,16
    LD   BC,$7F54
    OUT  (C),A
    OUT  (C),C

    JR   .infinite_loop

    CALL PLY_Stop           ; We never get here :P
    
; ---------------------------------------------------------------------------
wait_refresh
    LD   B,$F5
.loop_wait_refresh
    IN   A,(C)
    RRA
    JR   NC,.loop_wait_refresh
    RET

; ---------------------------------------------------------------------------
    include "fx_arkos_player.s"

; ---------------------------------------------------------------------------
song_filename
    DEFB "MUSICA.BIN"

; ---------------------------------------------------------------------------
; Print a string using the firmware
; ---------------------------------------------------------------------------
print_text
    LD   HL,message
    LD   A,(HL)
.loop_print_text
    CALL TXT_OUTPUT
    INC  HL
    LD   A,(HL)
.continue_print_text
    CP   $FF
    JR   NZ,.loop_print_text
    RET

message
    DEFB $1C,$00,$00,$00        ; INK 0,0,0     (Negro)
    DEFB $1C,$01,00,00          ; INK 1,26,26   (Blanco)
    DEFB $1D,$00,$00            ; BORDER 0,0
    DEFB $04,$00                ; MODE 1
    DEFB $1F,6,12               ; LOCATE x,y 40 - 22 / 2
    DEFB $0F,$01,"ULTRASYD!!!"
    DEFB $FF

; ---------------------------------------------------------------------------
    END
