* Rob Northen Copylock Backup/Restore program  v 1.00
*
* Makes backups of Copylock Keydisks, then writes them back to other
* keydisks modifying the magic number accordingly. This allows you
* to get a fully 100% version of a Copylock protected disk from a
* non-working copy !!!
*
* Coded by: Absu of CYNIX
*
* Version 1.00 : 20/01/92 - 21/01/92
*
buffer		equ $100000		;Address for 800K disk buffer

		opt s-
		move.l sp,a0
		move.l 4(a0),a0
		move.l $c(a0),d0
		add.l $14(a0),d0
		add.l $1c(a0),d0
		move.l d0,-(sp)
		pea (a0)
		move.w #0,-(sp)
		move.w #$4a,-(sp)
		trap #1
		add.l #12,sp
		clr.l -(sp)
		move.w #$20,-(sp)
		trap #1
		addq.l #6,sp
		move.l d0,oldsp
		move.w $43e.w,old43e
		move.w #$ffff,$43e.w
		move.w #18,-(sp)
		move.w #4,-(sp)
		move.w #3,-(sp)
		trap #13
		addq.l #6,sp
		move.w #4,-(sp)
		trap #14
		addq.l #2,sp
		move.w d0,oldrez
		move.w #1,-(sp)
		pea -1
		pea -1
		move.w #5,-(sp)
		trap #14
		add.l #12,sp
		move.w #16,-(sp)
		move.w #11,-(sp)
		trap #13
		addq.l #4,sp
loop1		bsr title
		bsr check_ram
		lea buffer,a0
		move.l a0,a1
		add.l #$ca000,a1
.1		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		clr.l (a0)+
		cmp.l a1,a0
		bne.s .1
		lea text2(pc),a0
		bsr print
		bsr c_on
loop2		bsr get_key
		cmp.b #'1',d0
		beq backup
		cmp.b #'2',d0
		beq restore
		cmp.b #'3',d0
		beq instruct
		cmp.b #'4',d0
		bne.s loop2
exit		bsr c_off
		move.w oldrez,-(sp)
		pea -1
		pea -1
		move.w #5,-(sp)
		trap #14
		add.l #12,sp
		move.w #8,-(sp)
		move.w #4,-(sp)
		move.w #3,-(sp)
		trap #13
		addq.l #6,sp
		move.w old43e,$43e.w
		move.l oldsp,-(sp)
		move.w #$20,-(sp)
		trap #1
		addq.l #6,sp
		clr.w -(sp)
		trap #1

backup		bsr title
		lea text7(pc),a0
		bsr print
		bsr get_key
		bsr c_off
		move.w #0,sides
		moveq #4,d0
		moveq #0,d1
		moveq #10,d2
		moveq #0,d3
		lea buffer,a0
		bsr disk_routs
		tst.l d0
		bne.s .1
		move.w #1,sides
		lea text8(pc),a0
		bsr print
		moveq #0,d1
		lea buffer,a5
		move.w #'00',d7
		move.w #79,d6
.2		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #4,d0
		move.l #10,d2
		moveq #0,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		beq.s .3
.7		lea text14(pc),a0
		bsr print
		bsr get_key
		bra loop1
.3		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .4
		move.b #'0',d7
		add.w #$100,d7
.4		dbf d6,.2
.1		lea text9(pc),a0
		bsr print
		moveq #0,d0
		moveq #0,d1
		moveq #6,d2
		moveq #0,d3
		lea (buffer+$64000),a0
		bsr disk_routs
		moveq #0,d0
		moveq #6,d1
		moveq #4,d2
		moveq #0,d3
		lea (buffer+$64c00),a0
		bsr disk_routs
		moveq #10,d1
		lea (buffer+$65400),a5
		move.w #'01',d7
		move.w #78,d6
.5		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #0,d0
		move.l #10,d2
		moveq #0,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		bne .7
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .6
		move.b #'0',d7
		add.w #$100,d7
.6		dbf d6,.5
		bsr c_on
		lea text10(pc),a0
		bsr print
		bsr get_key
		bsr c_off
		tst.w sides
		beq .8
		lea text11(pc),a0
		bsr print
		moveq #0,d1
		lea buffer,a5
		move.w #'00',d7
		move.w #79,d6
.9		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #4,d0
		move.l #10,d2
		moveq #3,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		moveq #4,d0
		move.l #10,d2
		moveq #1,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		beq.s .11
.10		lea text15(pc),a0
		bsr print
		bsr get_key
		bra loop1
.11		moveq #4,d0
		move.l #10,d2
		moveq #0,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		tst.l d0
		bne.s .10
		move.l a5,a4
		move.w #$4ff,d5
.13		cmp.l (a0)+,(a4)+
		bne .14
		dbf d5,.13
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .12
		move.b #'0',d7
		add.w #$100,d7
.12		dbf d6,.9
.8		lea text12(pc),a0
		bsr print
		moveq #0,d1
		lea (buffer+$64000),a5
		move.w #'00',d7
		move.w #79,d6
.15		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #0,d0
		move.l #10,d2
		moveq #3,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		moveq #0,d0
		move.l #10,d2
		moveq #1,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		bne .10
		moveq #0,d0
		move.l #10,d2
		moveq #0,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		tst.l d0
		bne .10
		move.l a5,a4
		move.w #$4ff,d5
.17		cmp.l (a0)+,(a4)+
		bne.s .14
		dbf d5,.17
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .16
		move.b #'0',d7
		add.w #$100,d7
.16		dbf d6,.15
		lea text13(pc),a0
		bsr print
		bsr get_key
		bra loop1
.14		lea text16(pc),a0
		bsr print
		bsr get_key
		bra loop1

restore		bsr title
		lea text17(pc),a0
		bsr print
		bsr get_key
		bsr c_off
		move.w #0,sides
		moveq #4,d0
		moveq #0,d1
		moveq #10,d2
		moveq #0,d3
		lea buffer,a0
		bsr disk_routs
		tst.l d0
		bne.s .1
		move.w #1,sides
		lea text8(pc),a0
		bsr print
		moveq #0,d1
		lea buffer,a5
		move.w #'00',d7
		move.w #79,d6
.2		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #4,d0
		move.l #10,d2
		moveq #0,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		beq.s .3
.7		lea text14(pc),a0
		bsr print
		bsr get_key
		bra loop1
.3		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .4
		move.b #'0',d7
		add.w #$100,d7
.4		dbf d6,.2
.1		lea text9(pc),a0
		bsr print
		moveq #0,d0
		moveq #0,d1
		moveq #6,d2
		moveq #0,d3
		lea (buffer+$64000),a0
		bsr disk_routs
		moveq #0,d0
		moveq #6,d1
		moveq #4,d2
		moveq #0,d3
		lea (buffer+$64c00),a0
		bsr disk_routs
		moveq #10,d1
		lea (buffer+$65400),a5
		move.w #'01',d7
		move.w #78,d6
.5		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #0,d0
		move.l #10,d2
		moveq #0,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		bne .7
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .6
		move.b #'0',d7
		add.w #$100,d7
.6		dbf d6,.5
		bsr c_on
		lea text18(pc),a0
		bsr print
		bsr get_key
		bsr c_off
		tst.w sides
		beq .8
		lea text11(pc),a0
		bsr print
		moveq #0,d1
		lea buffer,a5
		move.w #'00',d7
		move.w #79,d6
.9		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #4,d0
		move.l #10,d2
		moveq #3,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		moveq #4,d0
		move.l #10,d2
		moveq #1,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		beq.s .11
.10		lea text15(pc),a0
		bsr print
		bsr get_key
		bra loop1
.11		moveq #4,d0
		move.l #10,d2
		moveq #0,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		tst.l d0
		bne.s .10
		move.l a5,a4
		move.w #$4ff,d5
.13		cmp.l (a0)+,(a4)+
		bne .14
		dbf d5,.13
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .12
		move.b #'0',d7
		add.w #$100,d7
.12		dbf d6,.9
.8		lea text12(pc),a0
		bsr print
		move.w #'00',track+4
		lea track(pc),a0
		bsr print
		moveq #0,d0
		moveq #0,d1
		moveq #5,d2
		moveq #1,d3
		lea (buffer+$64000),a0
		bsr disk_routs
		tst.l d0
		bne .10
		moveq #0,d0
		moveq #6,d1
		moveq #4,d2
		moveq #1,d3
		lea (buffer+$64c00),a0
		bsr disk_routs
		tst.l d0
		bne .10
		bsr magic
		moveq #10,d1
		lea (buffer+$65400),a5
		move.w #'01',d7
		move.w #78,d6
.15		move.w d7,track+4
		lea track(pc),a0
		bsr print
		moveq #0,d0
		move.l #10,d2
		moveq #3,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		moveq #0,d0
		move.l #10,d2
		moveq #1,d3
		move.l a5,a0
		bsr disk_routs
		tst.l d0
		bne .10
		moveq #0,d0
		move.l #10,d2
		moveq #0,d3
		lea (buffer+$c8000),a0
		bsr disk_routs
		tst.l d0
		bne .10
		move.l a5,a4
		move.w #$4ff,d5
.17		cmp.l (a0)+,(a4)+
		bne.s .14
		dbf d5,.17
		lea $1400(a5),a5
		add.l #10,d1
		addq.b #1,d7
		cmp.b #':',d7
		bne.s .16
		move.b #'0',d7
		add.w #$100,d7
.16		dbf d6,.15
		lea text19(pc),a0
		bsr print
		bsr internal
		lea text21(pc),a0
		tst.l d0
		bne.s .18
		lea text20(pc),a0
.18		bsr print
		bsr get_key
		bra loop1
		bsr get_key
		bra loop1
.14		lea text16(pc),a0
		bsr print
		bsr get_key
		bra loop1

instruct	lea text4(pc),a0
		bsr print_help
		lea text6(pc),a0
		bsr print_help
		bra loop1

print_help	move.l a0,a6
		bsr title
		move.l a6,a0
		bsr print
		lea text5(pc),a0
		bsr print
		bsr get_key
		rts

magic		move.l #$0e0e0505,$ffff8800.w
		lea (buffer+$64a00),a0
		lea $50(a0),a6
		move.l a0,d7
		move.w #$86,$ffff8606.w
		moveq #0,d1
		bsr fdc_com
		move.w #$80,$ffff8606.w
		moveq #$13,d1		
		bsr fdc_com
		bsr fdc_wait
		move.b d7,$ffff860d.w
		lsr.l #8,d7
		move.b d7,$ffff860b.w
		lsr.l #8,d7
		move.b d7,$ffff8609.w
		move.w #$190,$ffff8606.w
		move.w #$90,$ffff8606.w
		move.w #$190,$ffff8606.w
		moveq #1,d1
		bsr fdc_com
		move.w #$184,$ffff8606.w
		moveq #6,d1
		bsr fdc_com
		move.w #$180,$ffff8606.w
		add.l #$50,a1
		move.w sr,-(sp)
		move.w #$2700,sr
		move.w #$a2,$ffff8604.w
		clr.l -(sp)
.1		move.b $ffff8609.w,1(a7)
		move.b $ffff860b.w,2(a7)
		move.b $ffff860d.w,3(a7)
		cmp.l (a7),a6
		bgt.s .1
		move.w #$d0,$ffff8604.w
		addq.l #4,sp
		move.w (sp)+,sr
		rts
fdc_com		bsr.s .1
		move.w d1,$ffff8604.w
.1		moveq #$20,d2
.2		dbf d2,.2
		rts

fdc_wait	move.l #$40000,d1
.1		btst #5,$fffffa01.w
		beq.s .2
		subq.l #1,d1
		bpl.s .1
.2		rts

check_ram	cmp.l #$80000,$42e.w
		bgt.s .1
		lea text3(pc),a0
		bsr print
		bsr get_key
		lea exit(pc),a0
		move.l a0,(sp)
.1		rts

title		bsr c_off
		lea text1(pc),a0
		bsr print
		bsr c_on
		rts

print		pea (a0)
		move.w #9,-(sp)
		trap #1
		addq.l #6,sp
		rts

get_key		move.w #7,-(sp)
		trap #1
		addq.l #2,sp
		rts

c_off		lea off,a0
		bsr print
		rts

c_on		lea on,a0
		bsr print
		rts

disk_routs	movem.l d5-d7/a2-a6,-(sp)
		bsr rob_routs
		movem.l (sp)+,d5-d7/a2-a6
		rts
rob_routs	incbin disk_io.bin
internal	incbin internal.bin
		rts
oldrez		dc.w 0
old43e		dc.w 0
oldsp		dc.l 0
sides		dc.w 0
track		dc.b 27,'D',27,'D'
		dc.l 0
on		dc.b 27,'e',0,0
off		dc.b 27,'f',0,0
text1		dc.b 27,'E',27,'w',27,'p'
		dc.b "                                                                                ",10,13
		dc.b "                  Rob Northen Copylock Backup/Restore Utility                   ",10,13
		dc.b "                                                                                ",10,13
		dc.b "                      Copyright (c) 1992 CYNIX Productions                      ",10,13
		dc.b "                                                                                ",10,13
		dc.b "                       Version 1.00  20/01/92 - 21/01/92                        ",10,13
		dc.b "                                                                                ",10,13
		dc.b "                      Coded by Absu (For Cynix use Only!)                       ",10,13
		dc.b "                                                                                ",10,13
		dc.b 27,'q'
		dc.b 10,10,13,0
text2		dc.b "Options:",10,10,13
		dc.b "1. Backup Copylock Keydisk",10,13
		dc.b "2. Restore backup to new Keydisk",10,13
		dc.b "3. Instructions",10,13
		dc.b "4. Quit to desktop",10,10,13
		dc.b "Please select 1-4 ?",0
text3		dc.b "Sorry, but you need at least 1 Meg of Ram to run this program......",0
text4		dc.b 27,'p'," Option 1: Backup Copylock Keydisk ",27,'q'
		dc.b "                              ",27,'p'," Help Screen 1 ",27,'q',10,10,13
		dc.b "This option will produce a non-working copy of the Copylock Keydisk, but all",10,13
		dc.b "the necessary data for the program will be 100% intact. This is the direct",10,13
		dc.b "equivalent of a Fast-Copy, with 'Best Match' used on the error track.",10,13
		dc.b "This option should be used by original suppliers who do not have access to any",10,13
		dc.b "form of hardware copier, and so usually just send non-working copies of",10,13
		dc.b "Copylock protected disks to be hacked.",10,13
		dc.b "It can also be used to prepare a Copylock Keydisk for uploading to a BBS,",10,13
		dc.b "as some archive programs cannot read all the data on the error track correctly.",10,13
		dc.b "The source disk will be checked for single or double sided and the destination",10,13
		dc.b "disk will always be formatted and verified to make sure it is 100%.",0
text5		dc.b 10,10,13,"Press any key to continue....",0
text6		dc.b 27,'p'," Option 2: Restore backup to new Keydisk ",27,'q'
		dc.b "                        ",27,'p'," Help Screen 2 ",27,'q',10,10,13
		dc.b "This option will allow you to restore a previously backed-up (non-working)",10,13
		dc.b "copy of a game onto a new Keydisk, giving a 100% working version of the game!",10,13
		dc.b "You will need a working Copylock Keydisk to write the backup onto, so just",10,13
		dc.b "use an old original, or a hardware copied backup of an old game.",10,13
		dc.b "The new Keydisk will be formatted as required (except for track 0), so all you",10,13
		dc.b "really have to hardware copy onto the new Keydisk is track 0, side 0 from a",10,13
		dc.b "working Copylock Keydisk.",10,13
		dc.b "The correct magic number will be read from the backup copy, and the new Keydisk",10,13
		dc.b "magic number will be modifed accordingly.",10,13
		dc.b "You should then have a fully working 100% copy of the game to hack !!!",0
text7		dc.b "Insert the Original Copylock Keydisk in Drive A, and press a key....",10,13,0
text8		dc.b 10,13,"Reading: Side - 1, Track -   ",0
text9		dc.b 10,13,"Reading: Side - 0, Track - 00",0
text10		dc.b 10,10,13,"Insert a blank disk into Drive A, and press a key....",10,13,0
text11		dc.b 10,13,"Writing: Side - 1, Track -   ",0
text12		dc.b 10,13,"Writing: Side - 0, Track -   ",0
text13		dc.b 10,10,13,"Backup complete, press any key to continue....",0
text14		dc.b 10,10,13,"Error while Reading, press any key to continue....",0
text15		dc.b 10,10,13,"Error while Writing, press any key to continue....",0
text16		dc.b 10,10,13,"Error while Verifying, press any key to continue....",0
text17		dc.b "Insert the backup (or Fast-Copied) disk in Drive A, and press a key....",10,13,0
text18		dc.b 10,10,13,"Insert the New Keydisk in Drive A, and press a key....",10,13,0
text19		dc.b 10,10,13,"Checking Keydisk for magic number: ",0
text20		dc.b "Not Found!",10,10,13,"Are you sure that was a working Keydisk ???",0
text21		dc.b "Found!",10,10,13,"Keydisk now restored, press any key to continue....",0
	even
		dc.w 0
