
org  #100

run #100
;score read/load

nolist

.read_score

	ld DE,scores
	ld HL,score_table
	call CAT_Update

	di

	ld A,1
	call motor_state

	call FDC_calibrate
	call FDC_write

	xor A
	call motor_state

	ei

	rst 0

.test_done
	jr test_done

	ret

.MOTOR_state

	ld BC,#FA7E
	out (C),A

	ld C,1 ;wait 0.5 sec
	call CR_Wait

	ret

;write score sector
.FDC_write
	ld A,(score_track)
	call FDC_track

;send command

        ld A,%01000101
        call FDC_send ;code
        ld A,(drive) 
        call FDC_send ;idd
	LD   A,(score_track)
        call FDC_send ;track
        xor A
        call FDC_send ;adt
        ld A,(score_sector)
	call FDC_send ;ns
	ld A,2	
        call FDC_send ;ls
        ld A,(score_sector)
	add 3
        call FDC_send ;fp
        ld   A,#52                    
        call FDC_send ;GAP
        ld   A,#FF
        call FDC_send ;done

;write data
	ld BC,#FB7E
	ld HL,score_table
	ld DE,2048 ;score_table_size

.FDC_write_loop1
        in   A,(C)      
        jp   P,FDC_write_loop1

        and  %00100000  
        jr   Z,FDC_write_done
;
        inc  C
	ld A,(HL)
        out  (C),A
        inc  HL
;
        dec  C
 	dec de
 	ld a,e
 	or d

        jr nz,FDC_write_loop1

.FDC_write_loop2
        in   A,(C)                
 	jp   P,FDC_write_loop2
        and  %00100000  
        jr   Z,FDC_write_done
;
        inc  C	
        out (C),C
        dec  C
        jr FDC_write_loop2

.FDC_write_done

	jr FDC_getresult

;read score sector
.FDC_read
	ld A,(score_track)
	call FDC_track

;send command

        ld A,%01000110
        call FDC_send ;code
        ld A,(drive) 
        call FDC_send ;idd
	LD   A,(score_track)
        call FDC_send ;track
        xor A
        call FDC_send ;adt
        ld A,(score_sector)
	call FDC_send ;ns
	ld A,2	
        call FDC_send ;ls
        ld A,(score_sector)
        call FDC_send ;fp
        ld   A,#52                    
        call FDC_send ;GAP
        ld   A,#FF
        call FDC_send ;done

;read data
	ld BC,#FB7E
	ld HL,score_table
	ld DE,score_table_size

.FDC_read_loop1
        in   A,(C)      
        jp   P,FDC_read_loop1

        and  %00100000  
        jr   Z,FDC_read_done
;
        inc  C
        in   A,(C)
        ld   (HL),A
        inc  HL
;
        dec  C
 	dec de
 	ld a,e
 	or d

        jr nz,FDC_read_loop1

.FDC_read_loop2
        in   A,(C)                
 	jp   P,FDC_read_loop2
        and  %00100000  
        jr   Z,FDC_read_done
;
        inc  C
        in   A,(C)
        dec  C
        jr FDC_read_loop2

.FDC_read_done

	jr FDC_getresult	

;get operation result
;return carry if success

.FDC_getresult
        call FDC_get
        ld (st0),A
        call FDC_get
        ld (st1),A
        call FDC_get
        ld (st2),A
;ignore other parms
        call FDC_get
        call FDC_get
        call FDC_get
        call FDC_get

	ld A,(st0)
	and %10011000
	jr nz,FDC_error

	ld A,(st1)
	and %00110111
	jr nz,FDC_error

	ld A,(st2)
	and %00110000
	jr nz,FDC_error

	scf
	ret

.FDC_error
	or A
	ret




	
;goto track A 
.FDC_track
        push AF
        ld   A,%00001111
        call FDC_send
        ld A,(drive)
        call FDC_send
        pop AF
        call FDC_send
        call FDC_waitdone
	ret

;goto track 0 to recalibrate head
.FDC_calibrate         
	ld   A,%00000111
   	call FDC_send
  	ld a,(drive)
        call FDC_send
        call FDC_waitdone
        ret

	
;wait X seconds contained in C
CR_Wait:
	ld B,#F5
	ld E,5 ;minimal wait is 0.1 sec

;1sec loop
;wait start of VBL
.CR_waitLoop
	in A,(C)
	rra
	jr nc,CR_waitLoop

;wait end of VBL
.CR_waitCounterLoop
	in A,(C)
	rra
	jr c,CR_waitCounterLoop


	dec E
	jr nz,CR_waitLoop



;1 sec loop
	ld E,50
	dec C
	jr nz,CR_waitLoop
;C secs loop
	ret

;get ST0 status to see if operation ended
.FDC_waitdone
        ld   A,%00001000
        call FDC_send

        call FDC_get
        ld   (st0),A
        call FDC_get
        xor  A
        ld   (st1),A                   
        ld   (st2),A
        ld   A,(st0)

        bit  5,A                      
        jr z,FDC_waitdone 

	ret
	

;send data to FDC
.FDC_send
        ex af,af'
 	ld   BC,#FB7E
.FDC_sendwait 	
	in   A,(C)
        jp   P,FDC_sendwait
        ex af,af'
 	inc C
 	out  (C),A
        ret  

;get data from FDC
.FDC_get
 	ld   BC,#FB7E
.FDC_getwait
	in   A,(C)
 	jp   P,FDC_getwait
 	inc C
        in   A,(C)
        ret

.drive		db 0
.head		db 0
.score_track	db 0
.score_sector	db #C1
.st0		db 0
.st1		db 0
.st2		db 0

xpos		EQU 10
ypos		EQU 9

let s_prior	= 35
let yoffset = 0

;draw order in mode 1
; 0 , nfiles/2
; 1 , nfiles/2+1
; 2 , nfiles/2+2

;9 entries so
; 0 , 5
; 1 , 6
; 2 , 7
; 3 , 8
; 4

;31 / 32

read "update_cat.asm"

.scores
	incbin "bin/faceA/hiscore.dat"

;write "bin/cat_art.bin"

id_offset	EQU 65

.score_table
;line 0 , set mode 1 and border 0
	db 00		
	db 00		;1 = 1 sure to be first entry
	db 04,01	;2 = 3 mode 1
	db #1D,0,0	;3 = 6 border 0,0
	db 0		;1 = 7 no char out !
	db #17		;1 = 8 last char of name
	db 0,0,#15	;3 = 11 extension
	ds 20,0

;line 1
	incbin "bin/amsdos_entry.bin"

;line 1 , set ink 0,0
	db 00
	db id_offset+31,#06	;2 = 2 ,line order and 6 to allow out
	db #1C,00,00,00	;4 = 6 ink 0,0
	db #15		;1 = 7 no out !
	db 0		;1 = 8 last char of name
	db 0,0,0	;3 = 11 extension
	ds 20,0
;line 2 , set ink 1,10
	db 00
	db id_offset+32,#06	;2 = 2 
	db #1C,1,10,10	;4 = 6
	db #15		;1 = 7
	db 0		;1 = 8
	db 0,0,0	;3 = 11
	ds 20,0
;line 3 , set ink 2,20
	db 00
	db id_offset+1,#06	;2 = 2 
	db #1C,2,20,20	;4 = 6
	db #15		;1 = 7
	db 0		;1 = 8
	db 0,0,0	;3 = 11
	ds 20,0

;line 4 , set ink 3,10,20
	db 00
	db id_offset+33,#06	;2 = 2 
	db #1C,3,10,20	;4 = 6
	db #15		;1 = 7
	db 0		;1 = 8
	db 0,0,0	;3 = 11
	ds 20,0

;line 5 , hidden text
	db 00
	db id_offset+2,#06	;2 = 2
	db #1F,xpos,0	;3 = 5
	db #0F,0	;2 = 7
	db #17		;1 = 8
	db "RR"		;2 = 10
	db #15		;1 = 1
	ds 20,0

;line 6 
	db 0
	db id_offset+34,#06	;2 = 2
	db "LLRRL"	;5 = 7
	db #17		;1 = 8
	db "LR"		;2 = 10
	db #15		;1 = 11
	ds 20,0

;line 7
	db 0
	db id_offset+3,#06	;2 = 2
	db "LRL X"	;5 = 7
	db #17		;1 = 8
	db #0E,0		;2 = 10
	db #15
	ds 20,0


;line 8 , ranking
	db 00
	db id_offset+35,#06		;2 = 2
	db #1F,xpos+5,ypos-3	;3 = 5
	db #0F,2		;2 = 7
	db #17			;1 = 8 skip  point
	db "R "			;2 = 10
	db #15			;1 = 11
	ds 20,0

;line 9 , ranking part 2
	db 00
	db id_offset+4,#06		;2 = 2
	db "A N K "		;6 = 8
	db #08			;1 = 9
	db "I"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;line 10 , ranking part 3
	db 00
	db id_offset+36,#06		;2 = 2
	db " N G"		;4 = 6
	db #15			;1 = 7
	db 0,0,0,0		;4 = 11
	ds 20,0

;********************
;*** score line 1 ***
;********************

;line 11 , locate posx,y , set pen for score
	db 00
	db id_offset+5,#06			;2 = 2
	db #1F,xpos,ypos		;4 = 5
	db #0F,3			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 12 , No.X
	db 00
	db id_offset+37,#06			;2 = 2
	db "No.1  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 13 , score
	db 0
	db id_offset+6,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 14 , adjust for name
	db 0
	db id_offset+38,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0			;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 15 , name
	db 0
	db id_offset+7,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 2 ***
;********************

;line 16 , locate posx,y , set pen for score
	db 00
	db id_offset+39,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+1	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 17 , No.X
	db 00
	db id_offset+8,#06			;2 = 2
	db "No.2  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 18 , score
	db 0
	db id_offset+40,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 19 , adjust for name
	db 0
	db id_offset+9,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 20 , name
	db 0
	db id_offset+41,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 3 ***
;********************

;line 21 , locate posx,y , set pen for score
	db 00
	db id_offset+10,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+2	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 22 , No.X
	db 00
	db id_offset+42,#06			;2 = 2
	db "No.3  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 23 , score
	db 0
	db id_offset+11,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 24 , adjust for name
	db 0
	db id_offset+43,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 25 , name
	db 0
	db id_offset+12,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 4 ***
;********************

;line 26 , locate posx,y , set pen for score
	db 00
	db id_offset+44,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+3	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 27 , No.X
	db 00
	db id_offset+13,#06			;2 = 2
	db "No.4  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 28 , score
	db 0
	db id_offset+45,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 29 , adjust for name
	db 0
	db id_offset+14,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 30 , name
	db 0
	db id_offset+46,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 5 ***
;********************

;line 31 , locate posx,y , set pen for score
	db 00
	db id_offset+15,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+4	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 32 , No.X
	db 00
	db id_offset+47,#06			;2 = 2
	db "No.5  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 33 , score
	db 0
	db id_offset+16,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 34 , adjust for name
	db 0
	db id_offset+48,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 35 , name
	db 0
	db id_offset+17,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 6 ***
;********************

;line 36 , locate posx,y , set pen for score
	db 00
	db id_offset+49,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+5	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 37 , No.X
	db 00
	db id_offset+18,#06			;2 = 2
	db "No.6  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 38 , score
	db 0
	db id_offset+50,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 39 , adjust for name
	db 0
	db id_offset+19,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 40 , name
	db 0
	db id_offset+51,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;********************
;*** score line 7 ***
;********************

;line 41 , locate posx,y , set pen for score
	db 00
	db id_offset+20,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+6	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 42 , No.X
	db 00
	db id_offset+52,#06			;2 = 2
	db "No.7  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 43 , score
	db 0
	db id_offset+21,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 44 , adjust for name
	db 0
	db id_offset+53,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 45 , name
	db 0
	db id_offset+22,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0
;********************
;*** score line 8 ***
;********************

;line 46 , locate posx,y , set pen for score
	db 00
	db id_offset+54,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+7	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 47 , No.X
	db 00
	db id_offset+23,#06			;2 = 2
	db "No.8  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 48 , score
	db 0
	db id_offset+55,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 49 , adjust for name
	db 0
	db id_offset+24,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 50 , name
	db 0
	db id_offset+56,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0
;********************
;*** score line 9 ***
;********************

;line 51 , locate posx,y , set pen for score
	db 00
	db id_offset+25,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+8	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 52 , No.X
	db 00
	db id_offset+57,#06			;2 = 2
	db "No.9  "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 53 , score
	db 0
	db id_offset+26,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 54 , adjust for name
	db 0
	db id_offset+58,#06		;2 = 2
	db 32,32,#15		;4 = 6
	db 0,0,0		;2 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 55 , name
	db 0
	db id_offset+27,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0
;********************
;*** score line 10 ***
;********************

;line 56 , locate posx,y , set pen for score
	db 00
	db id_offset+59,#06			;2 = 2
	db #1F,xpos,ypos+yoffset+9	;4 = 5
	db #0F,1			;2 = 7
	db #15				;1 = 8
	db 32,32,32			;3 = 11
	ds 20,0
;line 57 , No.X
	db 00
	db id_offset+28,#06			;2 = 2
	db "No.10 "			;6 = 8
	db #08			  	;1 = 9
	db " "				;1 = 10
	db #15				;1 = 11
	ds 20,0
	
;line 58 , score
	db 0
	db id_offset+60,#06	;2 = 2
	db "123456"		;6 = 8	6 usefull chars
	db #08			;1 = 9
	db 32,#15
	ds 20,0

;line 59 , adjust for name
	db 0
	db id_offset+29,#06		;2 = 2
	db 32,32,#15		;3 = 5
	db 0,0,0		;3 = 8
	db 0,0,0		;3 = 11
	ds 20,0

;line 60 , name
	db 0
	db id_offset+61,#06		;2 = 2
	db "ABCDEF"		;6 = 8
	db #08			;1 = 9
	db "G"			;1 = 10
	db #15			;1 = 11
	ds 20,0

;line 61 run disc part 1
	db 0
	db id_offset+30,#06		;2 = 2 
	db #1F,1,ypos+14	;3 = 5
	db "RU"			;2 = 7
	db #17			;1 = 8
	db "N",34		;2 = 10
	db #15
	ds 20,0
;line 62 part 2
	db 0
	db id_offset+62,#06		;2 = 2
	db "DISC"		;4 = 6
	db #0B			;1 = 7
	db #17			;1 = 8
	db #0D,#15		;2 = 10
	db 0
	ds 20,0
score_table_size EQU $-score_table
