;options saving code
; no face checking so be sure face has been checked before !
.OptionsSave
;start drive and load dir
	;call FDCON
	
;check if file is in directory
	LD DE,hiscore_file
	ld HL,FS_Dir_Location
	call FS_GetFile
	jr nc,OptionSaveFileNotFound
	
; DE = size , BC = first track,sector
; TO
; HL = address with data to write
; DE = data count (bytes)
; B = first sector C = last

	ld A,5	;max tries
.OptionSaveGotoTrack
	ld (OptionSaveRetries),A
	ld A,B
	push BC
	
	call FDC_track
	
	pop BC

;check if we are on good track !
	cp B
	jr z,OptionSaveCorrectTrack
;uncorrect track , recalibrate and retry

	push BC
	call FDC_RetryTrack
	pop BC
;do retry	
	ld A,(OptionSaveRetries)
	dec A
	jr nz,OptionSaveGotoTrack
	jr OptionSaveFileNotFound	;no more tries so do not write !
	
	
.OptionSaveCorrectTrack		
	ld HL,hi_score_table
	ld B,C					;first sector = last sector
	
	call FDC_write
	
.OptionSaveFileNotFound
	;call FDCOFF
	
;now , update cat art
	ld HL,default_cat_art
	ld DE,#9000
	call deexo	;first , unpack cat art to buffer
	
;second , update cat art
	ld DE,hi_score_table
	ld HL,#9000
	call CAT_Update
	
	ld A,5	;5 tries max
	ld (OptionSaveRetries),A

.OptionSaveFileTrackDirLoop	
;third , writte it !
	xor A	;goto track 0
	call FDC_Track0
	or A
	jr z,OptionSaveDirTrackOk

	call FDC_RetryTrack
	ld A,(OptionSaveRetries)
	dec A
	ld (OptionSaveRetries),A
	jr nz,OptionSaveFileTrackDirLoop
	jr OptionSaveDirNoWrite

.OptionSaveDirTrackOk

.OptionSaveWriteDirLoop	
	;write it now
	; HL = address with data to write
	; DE = data count (bytes)
	; B = first sector C = last
	ld HL,#9000
	ld DE,2048	;4 sectors
	ld BC,#C1C4
	call FDC_Write
	
.OptionSaveDirNoWrite
	
	ret
.OptionSaveRetries
	db 0
read "update_cat.asm"