org #400

run entry_point
write "bin/faceA/b_0400.exe"

amsdos_version	EQU 1	;only AMSDOS

;file system locations - required !
FS_Dir_Location		EQU 	#8000
FS_DeexoBuffer		EQU	#8400+2
FS_Minimal_System	EQU	1

snd_init	EQU #4000
snd_play	EQU snd_init+3
snd_stop	EQU snd_play+3

music_bank	EQU #C4

nolist

.entry_point
	di

	;xor A	
	ld (current_drive),A	;set current drive

	xor A
	call SetR6

	ld HL,#FBC9
	ld (#38),HL



	call intro_load_all

	;jp intro_done

	ld HL,palette
	call GFX_SetColors

	ld HL,crtc
	call GFX_SetCRTC

	call intro_splash_screen

	jp done

	;call FDCON
	;ld HL,fname

	;ld DE,FS_Dir_Location
	;ex DE,HL

	;call FS_GetFile
	;jr nc,done
	;ld HL,#C000
	;call FS_LoadFile

	;ld A,(current_drive)
	;call #C000


.done
	ld BC,#7F10
	ld A,R
	and 31
	or #40
	out (C),C
	out (C),A


	jr done

.intro_load_all
	call FDCON

;get music first
	ld HL,snd_module

	ld DE,FS_Dir_Location
	ex DE,HL

	call FS_GetFile
	jr nc,done
	ld HL,FS_DeexoBuffer
	call FS_LoadFile

	ld BC,#7F00+music_bank
	out (C),C

	ld HL,FS_DeexoBuffer
	ld DE,#4000
	call deexo

;load main module
	ld BC,#7FC1
	out (C),C

	ld HL,main_module
	ld DE,FS_Dir_Location
	ex DE,HL

	call FS_GetFile
	jr nc,done
	ld HL,#C000
	call FS_LoadFile

	ld BC,#7FC0
	out (C),C

	call FDCOFF
	ret

.intro_splash_screen

	ld BC,#7F8C
	out (C),C
	call draw_image

	ld BC,#7F00+music_bank
	out (C),C

	call snd_init

	ld A,44
	call SetR6

.intro_splash_loop	
	ld B,#F5
.intro_splash_vbl_loop	
	in A,(C)
	rra
	jr nc,intro_splash_vbl_loop

.intro_splash_vbl_counter_loop	
	in A,(C)
	rra
	jr c,intro_splash_vbl_counter_loop

	call snd_play

	call Scan_key
	call Check_key
	jr nz,intro_end

	jr intro_splash_loop

.intro_end
	call snd_stop
.intro_done
	ld BC,#7FC1
	out (C),C

	xor A
	call SetR6

	ld A,(current_drive)
	call #C000

	

.draw_image
	ld C,44 ;258/6
	ld DE,#8000
	ld HL,splash

.di_loop1
	ld B,46
	push BC
	push HL
.di_loop2
	push HL
	push BC
	ld BC,92

repeat 6
;line draw
	ld A,(HL)
	ld (DE),A
	inc HL
	inc DE
	ld A,(HL)
	ld (DE),A
	dec DE
	dec HL
	ld A,8
	add D
	ld D,A
	add HL,BC
rend
	pop BC
	call compute_adress
	pop HL
	inc HL
	inc HL

	dec B
	jp nz,di_loop2

	pop HL
	ld BC,552 ;larg*r9 768
	add HL,BC
	pop BC
	dec C
	jp nz,di_loop1


	ret	

.SetR6
	ld BC,#BC06
	out (C),C
	inc B
	out (C),A
	ret

.compute_adress
	ld A,D
	and %111
	ld D,A

	inc DE
	inc DE

	bit 3,D
	jr z,no_wrap

	ld A,#C0
	ld (cur_address),A
.no_wrap
	ld A,D
	and %111
	ld D,A
	ld A,(cur_address)
	or D
	ld D,A
	ret

cur_address db #80
	
.snd_module
	db "INTROSNDBIN"
.main_module
	db "MAIN    EXE"
.palette
	db 0, 1, 10, 3, 4, 13, 2, 11 ,5, 14, 20, 23, 6, 8, 17, 26
	db 0


;overscan CRTC config
;width = 46 , height = 43 , 6 lines char
;so 46*8,44*6 = 368*264 (mode 1)
;start page = #8000

.crtc
	db 0,63,1,46,2,49,4,51,5,0,6,0,7,46,8,0,9,5
	db 12,%00101100,13,0
	db #FF

read "FS_code.asm"



;case of fatal error
.FATAL_Error
	jr FATAL_Error
;transform a byte to 2 char in hex
.FONT_GetHexFromByte
	ret

;setup crtc
; HL = setup data (reg,data) ending with #FF
GFX_SetCRTC:

	ld B,#BC
	ld A,#FF
.CRTC_loop
	ld C,(HL)
	cp C
	ret z

	out (C),C
	inc B
	inc HL

	ld C,(HL)
	out (C),C
	dec B
	inc HL

	jr CRTC_loop
;setup palette for old
;use firmware colors definitions
;HL = color list + border
;if color = #FF -> end
GFX_SetColors:
	
	ld B,17
	xor A

	push HL
	pop IX

.Colors_loop	
	push BC
	push AF

	ld A,(IX)
	cp #FF
	jr z,GFX_SetColors_Quit
	
	and 31
	ld E,A
	ld D,0

	ld HL,GFX_color_table
	add HL,DE

	ld C,(HL)
	ld B,#7F

	pop AF

	out (C),A
	out (C),C

	pop BC

	inc A
	inc IX
	djnz Colors_loop
	ret
.GFX_SetColors_Quit
	pop AF
	pop BC
	ret
;table to convert from firmware to hardware colors
.GFX_color_table
;software color table
;only 27 colors
db #54 ;Black 	
db #44 ;Blue 
db #55 ;Bright Blue 
db #5C ;Red 	
db #58 ;Magenta 
db #5D ;Mauve 
db #4C ;Bright Red 
db #45 ;Purple 
db #4D ;Bright Magenta 
db #56 ;Green 
db #46 ;Cyan 
db #57 ;Sky Blue 
db #5E ;Yellow 
db #40 ;White	
db #5F ;Pastel Blue
db #4E ;Orange
db #47 ;Pink	
db #4F ;Pastel Magenta	
db #52 ;Bright Green 
db #42 ;Sea Green 
db #53 ;Bright Cyan
db #5A ;Lime
db #59 ;Pastel Green
db #5B ;Pastel Cyan 
db #4A ;Bright Yellow
db #43 ;Pastel Yellow 	
db #4B ;Bright White

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

;overscan image
.splash
incbin "gfx/splashscr.pm0"
list
;/EOF