;***************************
;*** FATAL ERROR HANDLER ***
;***************************
;
; no registers for parameters entry 
; all on stack
;
; order on stack -
; error string
; fonction string
.FATAL_Error
	di		;no int allowed
	call GFX_Cls
	
	ld HL,error_pal
	call GFX_SetColors
	
	ld HL,crtc_config_error
	call GFX_SetCRTC
	
	ld BC,#7F8D
	out (C),C
	
	ld DE,#C00D+#40
	ld HL,fatal_error_str
	call FONT_DrawString
	
;show error
	ld DE,#C105+#40
	pop HL		;get error on stack
	call FONT_DrawString
;show functioname
	ld DE,#C085+#40
	pop HL
	call FONT_DrawString
	
;FDC/FS report
	ld DE,#C18B+#80
	ld HL,fdcfs_report_str
	call FONT_DrawString
	
;setup ST registers
	ld A,(st0)
	ld BC,st_reg_strST0
	call FONT_GetBinFromByte
	ld A,(st1)
	ld BC,st_reg_strST1
	call FONT_GetBinFromByte
	ld A,(st2)
	ld BC,st_reg_strST2
	call FONT_GetBinFromByte
	ld A,(st3)
	ld BC,st_reg_strST3
	call FONT_GetBinFromByte
	
;ST report
	ld DE,#C205+#80
	ld HL,st_reg_str
	call FONT_DrawString
	ld DE,#C245+#80
	ld HL,st_reg_strB
	call FONT_DrawString

;last file
	ld HL,(FS_LastFile)
	ld DE,fatal_error_last_file
	ld B,11
.error_lastfile_loop
	ld A,(HL)
	sub 32
	ld (DE),A
	inc HL
	inc DE
	djnz error_lastfile_loop
	
;show last file	
	ld DE,#C345+#80
	ld HL,fatal_error_last_file_str
	call FONT_DrawString
	
;Track - head - sector - size
	ld A,(r_track)
	ld BC,error_str_track
	call FONT_GetHexFromByte
	
	ld A,(r_sector)
	ld BC,error_str_sector
	call FONT_GetHexFromByte
	
	ld A,(r_head)
	add 16
	ld (error_str_head),A
	
	ld A,(r_size)
	or 144
	ld (error_str_size),A
	
	ld DE,#C2C5+#80
	ld HL,error_str_thssz
	call FONT_DrawString

;reset	
	ld DE,#C780
	ld HL,error_key
	call FONT_DrawString
	
.FATAL_Error_wait_reset
	call KEY_Update
	ld HL,keymap
	ld BC,#0A00
	
.FATAL_Error_wait_reset_loop
	ld A,(HL)
	xor #FF
	or C
	ld C,A
	inc HL
	djnz FATAL_Error_wait_reset_loop
	
	ld A,C
	or A
	jr z,FATAL_Error_wait_reset
	
	ld HL,FATAL_resetcode
	ld DE,0
	ld BC,8
	ldir
	rst 0
	
.FATAL_resetcode	
	db #01,#89,#7F,#ED,#49,#C3,#91,#05
	
	
.FATAL_Error_done
	jr FATAL_Error_done
	
.crtc_config_error	
	db #01,#20,#02,#2A,#06,#20,#07,34,5,0,#0C,#30,#0D,#00,#FF
.error_pal
	db 1,14,13,26,3,20,10,2,1,25,16,18,9,15,4,0
	db 1
.fatal_error_last_file_str
	db "LAST FILE       "
.fatal_error_last_file
	ds 12,128

.error_str_thssz
	db "TR."
.error_str_track
	db "00   HD."
.error_str_head
	db "0   SC."
.error_str_sector
	db "00   SZ."
.error_str_size
	str "0"

.error_key
	str "PRESS ANY KEY TO RESTART SYSTEM!"

	
.fatal_error_str
	str "*** FATAL ERROR ***"
.fdcfs_report_str
	str "*** FDC/FS REPORT ***"
.st_reg_str
	db "ST0 "
.st_reg_strST0
	db "XXXXXXXX . ST1 "
.st_reg_strST1
	str "XXXXXXXX "	
	
st_reg_strB
	db "ST2 "
.st_reg_strST2
	db "XXXXXXXX . ST3 "
.st_reg_strST3
	str "XXXXXXXX "
