	; Mr Do's Castle - high score saver
	; ---------------------------------
	
	; The 2k x 8 SRAM at location E1 on the board
	; needs to be replace with an NVRAM 
	; eg. M48Z02 or DS1220
	;
	; Assembled with TASM
	; tasm -80 -b -fFF dc_saver.asm


	; ------------------------------
	; patches are for A rom ( 'set1' & 'set2' using the MAME names )
	; and C roms ( 'older'  )


	; Patch to A1 (set1 & set2) - same locations for the C1 version

	;offet old new 
        ;0037:  C9 FF checksum fix A1,  $13 for C1

        ;0126:  81 80 start RAM test $100 higher

        ;0129:  17 18 length RAM test $100 less
        
        ;013C:  81 80 start RAM checking $100 higher
        
        ;013F:  17 18 length RAM checking $100 less

        ;0198:  1F FF make clear memory skip the
        ;0199:  00 17 

        ;019A:  C3 75 jump to clear upper part of memory ( 7DB0 'SET1/2' 78B0 'OLDER' )
        ;019B:  B0 ED  
        ;019C:  7D B0

        ;086F:  C3 21 jump to clear high-scores checking
        ;0870:  B2 20 and integrity of scores test
        ;0871:  7D 80 ( 7DB2 'SET1/2' 78B2 'OLDER' )

	;0878:  00 01 make the high score start at 100 not 10000
	;087B:  01 00	 


	; Patches to A4 - $7dd0 code for both 'A' versions
	;            C4 - $78b0 code for 'C' version
	;
	; A4 
	;1fff:  FF 6E checksum fix ( set1 & set 2 )
	;
	; C4
	;1c07:  FF 28 checksum fix

	
	; ===============================================================


	; -----------------------------------------------------------------------------------
	; -----------------------------------------------------------------------------------
	.org $7db0 ; for the 'A' 'set1' and 'set2' version
	;.org $7890  ; # for the 'older' 'C' version


	jr mem_clr  
	jr Check_and_Reset

	; second part of clearing memory 
	; revectored from $19A leave a hole 

; -----------------------------------------------------------------------------------
mem_clr:
; -----------------------------------------------------------------------------------
	ld      (hl), l
	ldir    
	ld      hl, $8070
	ld      bc, $178F
	ld      de, $8071
	ld      (hl), 0
	ldir    
	jp      $19D ; continue power on
	

; -----------------------------------------------------------------------------------
Check_and_Reset:
; -----------------------------------------------------------------------------------


	; check for joystick UP after 
	; initial power up test have finished
	
	halt ; wait a couple IRQ's for the switch status to be updated
	halt
	

	ld a, ( $95d3 )
	and $2 	; joystick UP
	jp nz, ok_contin

	; clear the screen & sprites
	call $2ae
	call $2a1
      
	; say hi do you want to clear the scores ???

	ld hl, msg1
	call $2df ; print a string

	ld hl, msg2
	call $2df ; print a string

	ld hl, msg2a
	call $2df ; print a string

	ld hl, msg3
	call $2df ; print a string

	ld c, 0

wait_something:

	halt	; do this once per frame

	ld a, ( $95d5 ) ; inputs
	ld b,a
	and $1 ; p1 push button 
	jp z, ok_contin

	ld a,b
	and $88
	jr nz, ok_not_1p2p

	inc c
	ld a, c
	cp 120  ; approx 2 seconds
	jr nz, ok_not_1p2p

	; clear screen
	call $2ae
	ld hl, msg4
	call $2df ; print a string

wait_4_rel:
	
	ld a, ( $95d5 ) ; inputs
	and $88
	cp $88
	jr nz, wait_4_rel
	
	; kill the scores !!

	jp rst_the_scores

ok_not_1p2p:

	jr wait_something



msg1:
	
	.dw $b110
	.db $1F
	.db $0e
	.db "CLEAR SCORES ?"
msg2:

	.dw $b0ae
	.db $1C
	.db 22
        .db "HOLD ",1,"P & ",2,"P START FOR"

msg2a:

	.dw $b0ed 
	.db $1C
	.db 18
        .db 2," SECONDS TO CLEAR"

msg3:	
	.dw $b0ca
	.db $1F
	.db $15
	.db "PUSH BUTTON TO CANCEL"

msg4:
	.dw $b0ae
	.db $1F
	.db 21
        .db "RELEASE START BUTTONS"


ok_contin:

;       check to see if any initials are corrupt
;  	simple way to see if the score table needs
;	re-initializing if things went horribly wrong 



	ld b,10
	ld hl, $8023
	ld de, $5
nxt_one:
	ld c, 3  
nxt_init:
	ld a, ( hl )
	cp $20 ; space
	jr c, rst_the_scores
	cp $5b ; 'Z'+1
	jr nc, rst_the_scores 
	
	inc hl
	dec c
	jr nz, nxt_init
	add hl, de ; next initials

	djnz nxt_one
	jr all_good

rst_the_scores:

	; just clear it out !
	ld hl, $8020
	jp $872

all_good:

	; carry on with system startup
	jp $88c


	.end
