;intro final loader

nolist

org #9000
run start

write "bin/faceA/b_9000.exe"

intro_entry EQU #4809

amsdos_version	EQU 1	;only AMSDOS

;file system locations - required !
FS_Dir_Location		EQU 	directory
FS_DeexoBuffer		EQU	free_space+2
FS_Minimal_System	EQU	1

.start
	di	;not int !
	ld SP,stack_pos
;xor A	;for debug only
	;save current drive
	ld (save_drive),A
	ld (current_drive),A

	;install jumper for game at #0
	ld HL,restart_code
	ld DE,0
	ld BC,restart_code_size
	ldir

;hide screen
	ld BC,#BC01
	xor A
	out (C),C
	inc B
	out (C),A

;black border
	ld BC,#7F54
	ld A,16
	out (C),A
	out (C),C

;setup disc drive
	call FDCON


;check for 128k
	call check_ram
	jr c,Is128K

	ld A,#C0
	call set_bank
	ld DE,only128k
	ld HL,FS_Dir_Location
	call FS_GetFile
	jp nc,FATAL_Error
	ld HL,free_space
	call FS_LoadFile

	;shut down dirve
	call FDCOFF

	ld HL,free_space
	ld DE,#4000
	call deexo
	jp #4000

	

.Is128K

;load game module first
	ld A,#C7
	call set_bank

	ld DE,game
	ld HL,FS_Dir_Location
	call FS_GetFile
	jp nc,FATAL_Error
	ld HL,#4000
	call FS_LoadFile

;check if key pressed , if yes launch directly game
	call Check_key
	jr z,no_key_pressed

;check SPECY keys
	ld IX,keymap
	bit 4,(IX+7)		;S
	jr nz,no_speecy_title
	bit 3,(IX+3)		;P
	jr nz,no_speecy_title
	bit 6,(IX+7)		;C
	jr nz,no_speecy_title
	bit 3,(IX+5)		;Y
	jr nz,no_speecy_title

	ld A,#C0
	call set_bank
	ld DE,speccy
	ld HL,FS_Dir_Location
	call FS_GetFile
	jp nc,FATAL_Error
	ld HL,free_space
	call FS_LoadFile
	ld HL,free_space
	ld DE,#4000
	call deexo

;show green screen until keys released
	ld BC,#7F10
	ld A,#52
	out (C),C
	out (C),A
.speecy_title_wait_loop
	call Check_key
	jr nz,speecy_title_wait_loop

	jp #4000

.no_speecy_title

	rst 0	;launch game
.no_key_pressed



;load #C4 module
	ld A,#C4
	call set_bank

	ld DE,intro_c4
	ld HL,FS_Dir_Location
	call FS_GetFile
	jp nc,FATAL_Error
	ld HL,#4000
	call FS_LoadFile

;load #C5 module
	;ld A,#C5
	;call set_bank

	;ld DE,intro_c5
	;ld HL,FS_Dir_Location
	;call FS_GetFile
	;jp nc,FATAL_Error
	;ld HL,#4000
	;call FS_LoadFile

;load main module and uncompress
	ld A,#C0
	call set_bank

	ld DE,intro_main
	ld HL,FS_Dir_Location
	call FS_GetFile
	jp nc,FATAL_Error
	ld HL,free_space
	call FS_LoadFile

;shut down dirve
	call FDCOFF

;uncompress main
	ld HL,free_space
	ld DE,#400
	call deexo

;jump to intro	
	jp intro_entry
	
.set_bank
	ld B,#7F
	out (C),A
	ret


.intro_main
	db "INTRO   RAW"
.intro_c4
	db "DATA.C4 BIN"
.intro_c5
	db "DATA.C5 BIN"
.game
	db "MAIN    EXE"
.speccy
	db "SPECCY  RAW"
.only128k
	db "128KONLYRAW"

.restart_code
	ld SP,#C000
	ld BC,#7FC1
	out (C),C
save_drive	EQU $+1
	ld A,0
	jp #C000
restart_code_size	EQU $-restart_code

; keyboard funcs

.Scan_key
        ld hl,keymap    ;3
        ld bc,#f782     ;3
        out (c),c       ;4
        ld bc,#f40e     ;3
        ld e,b          ;1
        out (c),c       ;4
        ld bc,#f6c0     ;3
        ld d,b          ;1
        out (c),c       ;4
        ld c,0          ;2
        out (c),c       ;4
        ld bc,#f792     ;3
        out (c),c       ;4
        ld a,#40        ;2
        ld c,#4a        ;2 44
.scan_loop    
	ld b,d          ;1
        out (c),a       ;4 select line
        ld b,e          ;1
        ini             ;5 read bits and write into KEYMAP
        inc a           ;1
        cp c            ;1
        jr c,scan_loop       ;2/3 9*16+1*15=159
        ld bc,#f782     ;3
        out (c),c       ;4
        ret

.keymap
	ds 10,0

.Check_key
	call Scan_key

	ld HL,keymap
	ld B,10
	ld C,0

.Check_key_loop
	ld A,(HL)
	xor #FF
	or C
	ld C,A
	inc HL
	djnz Check_key_loop

	ld A,C
	or A
	ret

;check if 128K , return carry if yes
.check_ram
	ld BC,#7FC4
	ld HL,#4000
	ld D,4
.check_ram_loopA
	out (C),C
	ld (HL),C
	inc C
	dec D
	jr nz,check_ram_loopA

	ld DE,#0400
.check_ram_loopB
	dec C
	out (C),C
	ld A,(#4000)
	cp C
	jr nz,check_ram_module_fail
	inc E
	dec D
	jr nz,check_ram_loopB

	ld A,#C0
	out (C),A

	scf

	ret
	
.check_ram_module_fail
	or A
	ret

;disk code
read "FS_code.asm"

;case of fatal error
.FATAL_Error
	jr $
;transform a byte to 2 char in hex
.FONT_GetHexFromByte
	ret
.directory
	ds 1024,0
;stack here
	ds 128,0
.stack_pos

.free_space
list
;/end
