; TURGEN SYSTEM
; Plugin: Standard
; Cassette boot initialization routine skeleton
; ATASM
;
; This auxiliary routine allows conversion of monolithic binary files
; to cassette boot files. 
; 
; As monolithic binary files slightly differ from cassette boot files,
; the following strategy is executed.

; 1. As boot file cannot exceed 32 KB, it is loaded to a fixed address,
;    low enough to ensure that the trailing bytes of the last record
;    will not overlap with system ROM.
;
; 2. When this routine takes control, we do the following:
;    - Stop motor
;    - Store RUN address to RUNAD (736,737) and DOSVEC
;    - Reset stack 
;    - Reset COLDST flag
;    - Set disk boot succeeded flag
;    - Move program move routine to address 1024
;    - Jump to the move routine that moves the only DATA
;      segment of the program to the address where it is supposed
;      to be
;    - Run the program using address in the RUNAD vector


;Symbolics
            MOVE_RTN_ADDR=1024
            MOVE_RTN_ENTRY=1026

;Mainline code

            *=6144
            
BOOTHEAD   .BYTE 0                    ;Unused
BH_NBLK    .BYTE 0                    ;Number of blocks. TS fills this field
BH_LADDR   .WORD 6144                 ;Load address
BH_IADDR   .BYTE <BH_RTS,>BH_RTS      ;Initialization address

BOOTROUT   LDA  #60                   ;Motor off
           STA  54018
           
XRUN1      LDA  #0                    ;Store run address. TS modifies opcodes
           STA  736
           STA  10                    ;Also fake DOSVEC
XRUN2      LDA  #0
           STA  737
           STA  11                    ;Also fake DOSVEC 
           
           LDX  #255                  ;Reset stack pointer
           TXS
           
           LDA  #0                    ;Reset COLDST
           STA  580
           LDA  #1                    ;Set disk boot succeeded flag
           STA  9
           
BOOTMOVE   LDX  #(MOVE_END-MOVE_START)

BM_LOOP    LDA  MOVE_START-1,X        ;Move move routine
           STA  MOVE_RTN_ADDR-1,X
           DEX
           BNE  BM_LOOP
            
           JMP  MOVE_RTN_ENTRY        ;Execute the move routine
           
BH_RTS     RTS                        

; Move routine
           .IF MOVE_UP=1
MOVE_START .INCBIN tape_boot_move_up.bin
           .ELSE
MOVE_START .INCBIN tape_boot_move_down.bin
           .ENDIF
           JMP  (736)
MOVE_END              
           
           