;*** COPY utility for R-Type 3" disc format***


init_scr_pack	EQU #BBFF
draw_char		EQU #BB5A
change_mode		EQU #BC0E
keyboard_init	EQU #BB00
wait_key		EQU #BB18

trackA_buffer	EQU #4000
trackB_buffer	EQU #6000

;entry point
.Main

	call init_scr_pack
	ld A,2
	call change_mode
	
	call keyboard_init
	
	ld HL,start_text
	call print_txt
	
	call input

	di
	
	;jr $

	call FDC_MotorOn

;init loop	
	xor A
	ld (CurrentTrack),A
	
	ld A,(DriveB)
	ld (current_drive),A
	call FDC_Track0

	ld A,(DriveA)
	ld (current_drive),A	
	call FDC_Track0
	
	ld IY,track_list

;get tracks to process	
	ld B,(IY)
	inc IY
	
.MainLoop
	push BC
	push IY
	
	ld A,(DriveA)
	ld (current_drive),A
	

	
	ld A,(CurrentTrack)
	push IY

	ei
	call convert_trackno
	ld HL,track_text
	call print_txt
	ld HL,format_text
	call print_txt
	di
	
	pop IY
	
;db #ED,#FF	

;first of all , format track on drive A
	ld A,(IY)
	ld IXL,A
	ld A,(IY+1)
	ld IXH,A
	inc IY
	inc IY
	
	ld A,(CurrentTrack)
	
	call TrackFormat

		push AF
		call show_report
		pop AF
	
	;check errors here ! (whot? i am too lazy , yeah !)	
	call nc,error
	
	ld E,25
	call DSK_wait
	
;read track on drive B now
	ld A,(DriveB)
	ld (current_drive),A

	push IY
	ei
	ld HL,read_text
	call print_txt
	di	
	pop IY
	
;get data specs address	
	ld A,(IY)
	ld IXL,A
	ld A,(IY+1)
	ld IXH,A
	
	ld A,(CurrentTrack)
	
	ld HL,trackA_buffer
	call TrackRead
	
		push AF
		call show_report
		pop AF

	call nc,error
	
	ld E,25
	call DSK_wait

;write track on drive A

	ld A,(DriveA)
	ld (current_drive),A
	
	push IY
	ei
	ld HL,write_text
	call print_txt
	di	
	pop IY
	
;get data specs address	
	ld A,(IY)
	ld IXL,A
	ld A,(IY+1)
	ld IXH,A
	
	ld A,(CurrentTrack)
	
	ld HL,trackA_buffer
	call TrackWrite
	
		push AF
		call show_report
		pop AF

	call nc,error
	
	ld E,25
	call DSK_wait

;check written datas

	push IY
	ei
	ld HL,check_text
	call print_txt
	di	
	pop IY

	ld A,(IY)
	ld IXL,A
	ld A,(IY+1)
	ld IXH,A
	
	ld A,(CurrentTrack)
	
	ld HL,trackA_buffer
	call TrackCompare
	call nz,error
	
	ld E,25
	call DSK_wait

	ld HL,CurrentTrack
	inc (HL)
	
	inc IY
	inc IY
	
	pop HL	;just to remove IY from stack
	
	pop BC
	;djnz MainLoop
	dec B
	jp nz,MainLoop
	
	
	call FDC_MotorOff

	ld HL,job_done
	call print_txt

	call input
	jp main

.error
	call FDC_RetryTrack
	ei
	ld HL,error_text
	call print_txt
.error_badkey	
	call input
	di
	cp "R"
	jr z,error_retry
	cp "I"
	ret z
	cp "C"
	jr nz,error_badkey
	pop HL
	pop IY
	pop BC
	jp main
	
.error_retry
	call error_print
	pop HL
	pop IY
	pop BC
	jp MainLoop
	
	
.error_print
	call draw_char
	ld A,13
	call draw_char
	ld A,10
	call draw_char
	ret
	
.print_txt
	ld A,(HL)
	bit 7,A
	push AF
	and 127
	call draw_char
	pop AF
	ret nz
	inc HL
	jr print_txt	
	
.input	
	call keyboard_init	;reinit keyboard to avoid ghost keys
	call wait_key
	and #DF	;force upper key
	ret
	
.convert_trackno
	ld C,0
.convert_trackno_loop
	cp 10
	jr c,convert_trackno_stop
	inc C
	sub 10
	jr convert_trackno_loop
.convert_trackno_stop
	ld HL,track_count+1
	add 48
	ld (HL),A
	dec HL
	ld A,48
	add C
	ld (track_count),A
	ret
	
.ST0_text
	db "ST0 12345678 "
.ST1_text
	db "ST1 12345678 "
.ST2_text
	db "ST2 12345678 ",10,13+128
.show_report
push BC
push DE
push HL
push AF
	ld A,(st0)
	ld BC,ST0_text+4
	call FONT_GetBinFromByte
	ld A,(st1)
	ld BC,ST1_text+4
	call FONT_GetBinFromByte
	ld A,(st2)
	ld BC,ST2_text+4
	call FONT_GetBinFromByte
	
	ld HL,ST0_text
	call print_txt
pop AF
pop HL
pop DE
pop BC	
	ret
	
.csector_text
	str "#00 "
	
.error_text
	db 13,10
	str "Error ! (R)etry , (I)gnore or (C)ancel ? "
.start_text
	db "COPYCAT V1.0",13,10,"Insert original disc in B: and target in A:",13,10
	STR "and press any key to start"
	
.track_text
	db 10,13,"processing track "
.track_count	
	db "00"
	db "...",13,10+128
.format_text
	str "Format..."
.read_text
	str "Read..."
.write_text
	str "Write..."
.check_text
	db "Check...",13,10+128
.job_done
	str "Job done, press any key to restart..."


;**********************	
;*** FORMAT A TRACK ***
;**********************
;A = track to format
;IX = sectors	
	
.TrackFormat

;wait for drive
	push AF
	call FDC_DriveWaitReady
	pop AF

	call FDC_track
	ld E,10
	call DSK_wait
	
	call FDC_FormatTrack
	
	ret

;*********************
;*** READ A TRACK ****
;*********************
; A = track
; IX points to track info (first sector,last sector,dw data count)
; HL = buffer
.TrackRead
	push AF
	call FDC_DriveWaitReady
	pop AF

	call FDC_track
	
	ld E,10
	call DSK_wait
	
; HL = address to write data
; DE = data count (bytes)
; B = first sector C = last

	;ld B,(IX+0)
	;ld C,(IX+1)
	;ld E,(IX+2)
	;ld D,(IX+3)
	;call FDC_read
	ld DE,512
	ld B,(IX+0)
.TrackReadLoop
	push HL
	push DE
	push BC
	
	push BC
	push HL
	push DE
	ld A,B
	ld BC,csector_text+1
	call FONT_GetHexFromByte
	ld HL,csector_text
	call print_txt
	pop DE
	pop HL
	pop BC
	di
	
	ld C,B
	call FDC_read
	
	call show_report
	
	pop BC
	pop DE
	pop HL
	ret nc
	add HL,DE
	inc B
	ld A,(IX+1)
	cp B
	jr nc,TrackReadLoop
	
	ret

;*********************
;*** WRITE A TRACK ***
;*********************
;A = track
;IX points to track info (first sector,last sector,dw data count)
;HL = buffer
.TrackWrite
	push AF
	call FDC_DriveWaitReady
	pop AF

	call FDC_track
	
	ld E,10
	call DSK_wait
	
; HL = address to write data
; DE = data count (bytes)
; B = first sector C = last

	;ld B,(IX+0)
	;ld C,(IX+1)
	;ld E,(IX+2)
	;ld D,(IX+3)
	;call FDC_write
	ld DE,512
	ld B,(IX+0)
.TrackWriteLoop
	push HL
	push DE
	push BC
	ld C,B
	call FDC_write
	
	call show_report
	di
	
	pop BC
	pop DE
	pop HL
	ret nc
	add HL,DE
	inc B
	ld A,(IX+1)
	cp B
	jr nc,TrackWriteLoop
	
	
	ret
;*****************************************	
;*** COMPARE WRITTEN TRACK WITH BUFFER ***
;*****************************************
;A = track
;IX points to track info (first sector,last sector,dw data count)
.TrackCompare
	ld HL,trackB_buffer
	call TrackRead
	
	ld HL,trackA_buffer
	ld DE,trackB_buffer
	ld BC,512*10
	cpir
	
	;ret z
	
	;jr $
	ret
	
	
	
	
read "copycat_tracks.asm"
read "format.asm"

;get binary string from byte
; BC = target
.FONT_GetBinFromByte
	ld L,C
	ld H,B
	ld B,8
	
.FONT_GetBinFromByte_loop
	rl A
	;jr nc,FONT_GetBinFromByteNoBit
	push AF
	ld A,0
	rla 
	add 48
	
	
	ld (HL),A
	pop AF
	inc HL
	djnz FONT_GetBinFromByte_loop
	
	ret
	
	

;get hex string from byte
;A=digit
;BC = target
.FONT_GetHexFromByte
	push AF
	srl A
	srl A
	srl A
	srl A
	ld E,A
	ld D,0
	ld HL,hex_string
	add HL,DE
	ld A,(HL)
	ld (BC),A
	inc BC
	pop AF
	and 15
	ld E,A
	ld HL,hex_string
	add HL,DE
	ld A,(HL)
	ld (BC),A
	ret

.hex_string
	db "0123456789ABCDEF"

.FATAL_Error
	jr $


.DriveA
	db 0
.DriveB
	db 1
;.CurrentDrive
;	db 0
.CurrentTrack
	db 0