; ardump.asm (Action Replay Cartridge)
; dumps C64 cartridges
; Version 1.0
; machine language part


* = $0801

.INCLUDE SYSTEM.ASM			; KERNAL functions


_bas10		.byte <_bas20,>_bas20,$0a,$00,$99,$22
		.text "ACTION REPLAY DUMPER V1.0"
		.byte $22,$00

_bas20		.byte <_bas30,>_bas30,$14,$00,$85,$22,$11
		.text "ENTER FILENAME"
		.byte $22,$3b,$4e,$24,$00

_bas30		.byte <_basend,>_basend,$1e,$00,$9e,$20,$32,$31,$32,$31
_basend		.byte $00,$00,$00


		JSR  _copyfilename
		JSR  _promptSave
		JSR  _waitFire

		LDA  #$00
		STA  _teil

_copyL1	        LDA  _teil
		ASL
		ASL
		ASL
		STA  $de00
		JSR  _busywait

		LDA  #$80
		STA  _copyL2+2
		LDX  #$00
		LDY  #$00
_copyL2		LDA  $8000,Y
		STA  $2000,Y
		INY
		BNE  _copyL2
		INC  _copyL2+2
		INC  _copyL2+5
		INX
		CPX  #$20
		BNE  _copyL2

		LDA  _teil
		CMP  #$03
		BEQ  _doneit
		CLC
		ADC  #$01
		STA  _teil
		BNE  _copyL1

_doneit		JSR  _saveCart
		JSR  _promptDone
_the_end	JMP  _the_end
	
_teil		.byte $00


_copyfilename	LDY  #$02
		LDA  ($2d),Y
		STA  _namelen
		INY
		LDA  ($2d),Y
		STA  $fb
		INY
		LDA  ($2d),Y
		STA  $fc

		LDA  _namelen
		TAX
		LDY  #$00
_copyname	LDA  ($fb),Y
		STA  _filename,Y
		INY
		DEX
		BNE  _copyname

		RTS



_saveCart	JSR  CLRCHN
		JSR  CLALL
		LDA  #$01
		LDX  $ba		; FA - Use current Device Number
		TAY
		JSR  SETLFS		; SETLFS 1,8,1

		LDA  _namelen
		LDX  #<_filename
		LDY  #>_filename
		JSR  SETNAM
		LDX  #$00		; source memory $2000
		LDY  #$20
		STX  $fb
		STY  $fc
		LDA  #$fb
		LDX  #$00		; end of source memory $a000
		LDY  #$a0
		JSR  SAVE
		LDA  #$01
		JSR  CLOSE
		RTS

_namelen	.byte $00
_filename	.text "FILENAME GOES HERE"


_promptSave	LDY  #$00
_promptSaveL1	LDA  _save_text,Y
		BEQ  _promptSaveJ1
		JSR  CHROUT
		INY
		BNE  _promptSaveL1
_promptSaveJ1	RTS

_promptDone	LDY  #$00
_promptDoneL1	LDA  _done_text,Y
		BEQ  _promptDoneJ1
		JSR  CHROUT
		INY
		BNE  _promptDoneL1
_promptDoneJ1	RTS


_busywait	LDX  #$00
		LDY  #$00
		INC  $d020
_busywaitL1	DEY
		BNE  _busywaitL1
		DEX
		BNE  _busywaitL1
		RTS

_save_text	.byte $0d,$0d
		.text "PLEASE INSERT SAVE DISK"
		.byte $0d
		.text "AND PRESS <SPACE> KEY!"
		.byte 0

_done_text	.byte $0d,$0d
		.text "DONE!"
		.byte 0


_waitFire	LDA  $dc00		; wait for fire button up
		AND  $dc01
		AND  #$10
		BEQ  _waitFire
_waitFireL1	LDA  $dc00		; wait for fire button
		AND  $dc01
		AND  #$10
		BNE  _waitFireL1
		RTS

_end		.byte $00,$00,$00
