***************************************************************************
*                        _____  _____       _____                         *
*                     ___\_  /__\  _/______ \   /___                      *
*                     \   /    / \____    /   __/   \                     *
*                     /___\____\ ___ /   /____\_____/                     *
*                      dip/a!       /   /                                 *
*                   .---.----(*( RESISTANCE )*)---.---.                   *
*                   `-./                           \.-'                   *
*                                                                         *
*              APANO SIN - CRACKED BY STYLE/RSE in August 2ooo            *
*                     ORIGINAL SUPPLIED BY 4PLAY/RSE                      *
*                            DEDICATED TO JACKY                           *
*                                                                         * 
***************************************************************************

DEBUG		MACRO
		MOVE.W	#000,$DFF106
		MOVE.W	#\1,$DFF180
		ENDM
	
CHECK		= 0		; set to 1 to test the trackloader
PATCHLOADER	= 1		; set to 0 to use original loader

LOADEROFFSET	= 1166
LOADERLENGTH	= 16
WRITELOADER	= 0		; set to 1 to save loader to disk
DRIVE		= 1		; which drive to use for saving?

ID_MAINDISK	= $0A9ADE7E	; "ASD0"
ID_LEVELDISK1	= $0A9A9E76	; "ASL1"
ID_LEVELDISK2	= $0A9A9E6E	; "ASL2"
ID_LEVELDISK3	= $0A9A9E66	; "ASL3"
ID_FINALDISK	= $0A9AD66E	; "ASE2"

MFMBUFFER1	= $79000+8192	; 1st (Loader is at $79000!)
MFMBUFFER2	= $79000	; 2nd


		IFNE	WRITELOADER
		AUTO	WSDRIVE\LOADER1\LOADEROFFSET\LOADERLENGTH\
		ENDC

	IFNE	CHECK
CHECKLOADER
	move.w #$4000,$dff09a	


.0	moveq	#0,d1	
	move.l	#1,d2
	move.l	#0,d3
	move.l	#$c8,d4
	lea	$61000,a0
	;bsr	LOADER1

	LEA	$80000,A0
	LEA	NAME,A1
	BSR	LOADER2
	

.end	bsr MOTOR_OFF
	move.w #$c000,$dff09a
	RTS
NAME	dc.l	$826A5CC2
	ENDC



***************************************************
*** PATCH FOR THE 1st LOADER (TRAILER DISK)	***
***************************************************

LOADER1	move.l	a0,-(a7)
	lea	REGS(pc),a0
	movem.l	d0-d7/a0-a7,(a0)
	move.l	(a7)+,a0
	move.l	a1,-(a7)
	lea	REGS+8*4(pc),a1
	move.l	a0,(a1)
	addq.l	#4,7*4(a1)
	move.l	(a7)+,a1

	movem.l	d0-a6,-(a7)
	lea	FLAG(pc),a6
	cmp.l	#"JCKY",(a6)
	beq.b	.noInit
	move.l	#"JCKY",(a6)

	bsr	INIT_DISK

.noInit	moveq	#15-1,d1		; max. # of files 
	lea	TABLE(pc),a1
.search	cmp.w	(a1)+,d4
	beq.b	.found
	addq.l	#6,a1
	dbf	d1,.search
	bra.b	.error
	
.found	move.w	(a1)+,d0		; start block
	move.l	(a1)+,d1		; length in bytes
	moveq	#1,d5			; load from disk '1'
	bsr	BLOCK_LOAD
	bsr	MOTOR_OFF
	tst.w	d7
	beq.b	.ok
	
.error	moveq	#"1",d1
	bra	SHOW_DEBUGSCREEN
	;move.w	#0,$dff106
	;move.w	$dff006,$dff180
	;bra.b	.error

.ok	IFNE	PATCHLOADER
	cmp.w	#1162,d0
	bne.b	.out


; last file from disk 1 has been loaded, now patch the
; fucking shit...
 
	move.l	60(a7),a0		; get return address
	move.w	#$4eb9,d0		; "jsr"

	move.l	a0,a1
	add.l	#$396-$2f6,a1
	lea	LOADER2(pc),a2
	move.w	d0,(a1)+
	move.l	a2,(a1)
	
	add.l	#$4aa-$2f6,a0
	lea	PATCH(pc),a1
	move.w	d0,(a0)+
	move.l	a1,(a0)
	ENDC


.out	movem.l	(a7)+,d0-a6
	rts

FLAG	dc.l	0

; a1: address to copy patch to

PATCH	movem.l	d0-a6,-(a7)
	move.l	60(a7),a0		; get return address

	addq.l	#4,a0
	move.l	a0,a1
	add.w	2(a0),a1
	addq.w	#2,a1
	move.l	(a1),a1			; a1 = destination

	lea	MFMBUFFER(pc),a0
	move.l	#$79000,(a0)

	move.w	#$4ef9,(a1)+		; use Game Loader MFM Buffer for
	move.l	#$79000+13000,(a1)	; for our Loader
			
	lea	LOADER2(pc),a0		; Loader is just behind
	lea	$79000+13000,a1		; our MFM buffer
	move.w	#LOADEREND-LOADER2-1,d7
.copy	move.b	(a0)+,(a1)+
	dbf	d7,.copy

	lea	$42.w,a0
	lea	$79000+13000+(LOADER2\.FAKE-LOADER2),a1	; points to rts
	;lea	fake(pc),a1
	move.l	a1,(a0)
	
	
	movem.l	(a7)+,d0-a6
	rts	

TABLE	; Format: ID.w, StartBlock.w, LengthInBytes.l
	dc.w	$c8			; $01,$00,$c8
	dc.w	0003
	dc.l	20000

	dc.w	$f5			; $13,$2f,$f5
	dc.w	0043
	dc.l	24500

	dc.w	$295			; $09,$10,$295
	dc.w	0091
	dc.l	66100

	dc.w	$172			; $1a,$04,$172
	dc.w	0221
	dc.l	37000

	dc.w	$106			; $1f,$3b,$106
	dc.w	0294
	dc.l	26200

	dc.w	$6fe			; $24,$06,$6fe
	dc.w	0346
	dc.l	179200

	dc.w	$232			; $55,$01,$232
	dc.w	0696
	dc.l	56200

	dc.w	$3e8			; $40,$20,$3e8
	dc.w	0806
	dc.l	100000

	dc.w	$124			; $50,$18,$124
	dc.w	1002
	dc.l	29200

	dc.w	$16			; $8a,$1d,$16
	dc.w	1060
	dc.l	2200

	dc.w	$109			; $8b,$25,$109
	dc.w	1065
	dc.l	26500

	dc.w	$12			; $62,$1c,$12
	dc.w	1117
	dc.l	1800

	dc.w	$30			; $5f,$0f,$30
	dc.w	1121
	dc.l	4800

	DC.W	$9c			; $17,$25,$9c
	dc.w	1131
	dc.l	15600

	dc.w	$13			; $8b,$10,$13
	dc.w	1162
	dc.l	1900



; LOADER:	BLOCK 1166



***************************************************
*** PATCH FOR THE 2nd LOADER (MAIN DISK)	***
***************************************************

DELAY		MACRO
		movem.l	d0/d7,-(a7)
		move.w	#\1,d7
		bsr	WAITbeam
		movem.l	(a7)+,d0/d7
		ENDM

; a0: Load Address
; a1: File name

LOADER2	move.l	a0,-(a7)
	lea	REGS(pc),a0
	movem.l	d0-d7/a0-a7,(a0)
	move.l	(a7)+,a0
	move.l	a1,-(a7)
	lea	REGS+8*4(pc),a1
	move.l	a0,(a1)
	addq.l	#4,7*4(a1)
	move.l	(a7)+,a1


	move.w	FIRSTCALL(pc),d0
	bpl.b	.go

	movem.l	d0-a6,-(a7)
	move.l	#ID_MAINDISK,d1
	bsr	.DISKSWAP
	lea	DIRTABLES(pc),a0
	lea	DIR_MAINDISK(pc),a1
	lea	DIR_LEVELDISK1(pc),a2
	lea	DIR_LEVELDISK2(pc),a3
	lea	DIR_LEVELDISK3(pc),a4
	lea	DIR_FINALDISK(pc),a5
	move.l	a1,(a0)+
	move.l	a2,(a0)+
	move.l	a3,(a0)+
	move.l	a4,(a0)+
	move.l	a5,(a0)
	lea	FIRSTCALL(pc),a0
	addq.w	#1,(a0)
	movem.l	(a7)+,d0-a6

.go	lea	.ADDY(pc),a2
	move.l	a0,(a2)
	move.l	a0,d0
	tst.w	(a2)
	bmi.w	.DISKSWAP
	btst	#0,d0
	beq.b	.load

	move.l	(a1),d0

	moveq	#5-1,d7
	lea	.disks(pc),a2
.fix	cmp.l	(a2)+,d1
	beq.b	.swapem
	addq.l	#4,a2
	dbf	d7,.fix
	bra.b	.noswap

.swapem	movem.l	d0-d6/a0-a6,-(a7)
	bsr	.DISKSWAP
	movem.l	(a7)+,d0-d6/a0-a6
	tst.l	d7
	bne.b	.swapem

.noswap	lea	DIRTABLES(pc),a0
	move.l	.DISK(pc),d2
	and.l	#$ff,d2
	;sub.b	#"0",d2
	subq.w	#2,d2
	lsl.w	#2,d2
	move.l	(a0,d2.w),a0
.len	cmp.l	#-1,(a0)
	beq.w	.notfound
	cmp.l	(a0)+,d0
	beq.b	.yep
	addq.l	#6,a0
	bra.b	.len
.yep	addq.w	#2,a0			; skip start block
	move.l	(a0),d3			; return file length in d3
	rts	

.load	moveq	#5-1,d7
	lea	.disks(pc),a2
.CHECK	cmp.l	(a2)+,d1
	beq.w	.SWAP
	addq.l	#4,a2
	dbf	d7,.CHECK

.DISKOK	move.l	(a1),d0			; get file name

	move.l	d0,d6			; save Filename for debugging

	lea	DIRTABLES(pc),a1
	move.l	.DISK(pc),d2
	and.l	#$ff,d2
	;sub.b	#"0",d2
	subq.w	#2,d2
	lsl.w	#2,d2
	move.l	(a1,d2.w),a1
.search	cmp.l	#-1,(a1)
	beq.b	.notfound
	cmp.l	(a1)+,d0
	beq.b	.found
	addq.l	#6,a1
	bra.b	.search

.found	move.w	(a1)+,d0		; start block
	move.l	(a1)+,d1		; length in bytes
	move.l	.DISK(pc),d5		; get current disk
	bsr	BLOCK_LOAD
;	bsr	MOTOR_OFF
	tst.w	d7
	bne.b	.notfound

.noCRM	cmp.l	#$CA3A2EF2,d6		; decruncher loaded?
	bne.b	.nodec
	lea	DECRUNCH(pc),a1		; yep, install CRM patch
	lea	CRUNCHERADDRESS(pc),a2
	move.w	#$4ef9,(a0)+
	move.l	a1,(a0)+
	move.l	a0,(a2)

.nodec
.ok	moveq	#0,d7
.fake	rts

.notfound
	move.l	d6,d0
	move.l	.DISK(pc),d1
	add.b	#"0",d1
	cmp.l	#-1,(a1)
	bne.b	.loader
	moveq	#11,d7
.loader	bra	SHOW_DEBUGSCREEN

;.dead	move.w	#0,$dff106
;	move.w	#$00F,$dff180
;	bra.b	.dead

.SWAP	movem.l	d0-d6/a0-a6,-(a7)
	bsr.b	.DISKSWAP
	movem.l	(a7)+,d0-d6/a0-a6
	tst.l	d7
	bne.b	.SWAP
	bra.w	.DISKOK

.DISK	dc.b	"PR01"
.ADDY	dc.l	0

; d1: Disk ID

.DISKSWAP
	lea	.DISKS(pc),a1
	moveq	#5-1,d7
.IDloop	cmp.l	(a1)+,d1
	beq.b	.IDok
	addq.l	#4,a1
	dbf	d7,.IDloop
	bra.w	.escape
	
.IDok	move.l	(a1),d5			; get disk number
	lea	.DISK(pc),a0
	cmp.b	(a0),d5
	beq.w	.escape			; disk is already in drive!


; load bootblock to check Disk ID
.wait	DELAY	2*50

	moveq	#0,d0			; start block
	moveq	#3*4,d1			; length in bytes
	lea	.BUF(pc),a0		; load address
	bsr	BLOCK_LOAD
	bsr	MOTOR_OFF

	cmp.l	#1,d5			; Trailer Disk?
	bne.b	.std

	tst.w	d7			; yes, slightly other
	bne.b	.wait			; disk swap!
	bra.b	.escape

.std	tst.w	d7
	beq.b	.escape
	moveq	#-1,d7
	bra.b	.out
	

.escape	moveq	#0,d7
	lea	.DISK(pc),a0
	move.l	d5,(a0)
.out	rts
	
.BUF	ds.b	20

.DISKS	dc.l	ID_MAINDISK,02
	dc.l	ID_LEVELDISK1,03
	dc.l	ID_LEVELDISK2,04
	dc.l	ID_LEVELDISK3,05
	dc.l	ID_FINALDISK,06

CRUNCHERADDRESS	dc.l	0		; address of original game decruncher

; A0: SOURCE
; A1: DEST
DECRUNCH
	cmp.l	#"CrM!",(a0)
	beq.b	NormalDecrunch_DC
	move.w	4(a0),d7		; original code
	move.b	(a0),d7
	move.l	CRUNCHERADDRESS(pc),a2
	jmp	(a2)


**-----------------------------------------------------------
** Jump here to decrunch some data without any overlap checks
** The Regs have to loaded with:
** a0: Adr of Source (with Header)
** a1: Adr of Dest
**-----------------------------------------------------------
NormalDecrunch_DC
	movem.l	d0-d7/a0-a6,-(sp)
	cmp.l	#"CrM!",(a0)+
	bne.s	.NotCrunched
	tst.w	(a0)+		;skip MinSecDist
	move.l	(a0)+,d1	;OrgLen
	move.l	(a0)+,d2	;CrLen
	move.l	a0,a2
	bsr.s	FastDecruncher_DC
.NotCrunched:
	movem.l	(sp)+,d0-d7/a0-a6
	rts
**-------------------------------------------------------------------
** This is the pure Decrunch-Routine
** The Registers have to be loaded with the following values:
** a1: Adr of Destination (normal)	** a2: Adr of Source (packed)
** d1: Len of Destination		** d2: Len of Source
**-------------------------------------------------------------------
FastDecruncher_DC
	move.l	a1,a5			;Decrunched Anfang (hier Ende des Decrunchens)
	add.l	d1,a1
	add.l	d2,a2
	move.w	-(a2),d0		;Anz Bits in letztem Wort
	move.l	-(a2),d6		;1.LW
	moveq	#16,d7			;Anz Bits
	sub.w	d0,d7			;Anz Bits, die rotiert werden müssen
	lsr.l	d7,d6			;1.Bits an Anfang bringen
	move.w	d0,d7			;Anz Bits, die noch im Wort sind
	moveq	#16,d3
	moveq	#0,d4
.DecrLoop:
	cmp.l	a5,a1
	ble.w	.DecrEnd			;a1=a5: fertig (a1<a5: eigentlich Fehler)

	bsr.s	.BitTest
	bcc.s	.InsertSeq		;1.Bit 0: Sequenz
	moveq	#0,d4
** einzelne Bytes einfügen **
.InsertBytes:
	moveq	#8,d1
	bsr.w	.GetBits
	move.b	d0,-(a1)
	dbf	d4,.InsertBytes
	bra.s	.DecrLoop
*------------
.SpecialInsert:
	moveq	#14,d4
	moveq	#5,d1
	bsr.s	.BitTest
	bcs.s	.IB1
	moveq	#14,d1
.IB1:	bsr.s	.GetBits
	add.w	d0,d4
	bra.s	.InsertBytes
*------------
.InsertSeq:
** Anzahl der gleichen Bits holen **
	bsr.s	.BitTest
	bcs.s	.AB1
	moveq	#1,d1			;Maske: 0 (1 AB)
	moveq	#1,d4			;normal: Summe 1
	bra.s	.ABGet
.AB1:
	bsr.s	.BitTest
	bcs.s	.AB2
	moveq	#2,d1			;Maske: 01 (2 ABs)
	moveq	#3,d4			;ab hier: Summe mindestens 3
	bra.s	.ABGet
.AB2:
	bsr.s	.BitTest
	bcs.s	.AB3
	moveq	#4,d1			;Maske: 011 (4 ABs)
	moveq	#7,d4			;hier: Summe 11
	bra.s	.ABGet
.AB3:
	moveq	#8,d1			;Maske: 111 (8 ABs)
	moveq	#$17,d4			;hier: Summe 11
.ABGet:
	bsr.s	.GetBits
	add.w	d0,d4			;d0: Länge der Sequenz - 1
	cmp.w	#22,d4
	beq.s	.SpecialInsert
	blt.s	.Cont
	subq.w	#1,d4
.Cont:
** SequenzAnbstand holen **
	bsr.s	.BitTest
	bcs.s	.DB1
	moveq	#9,d1			;Maske: 0 (9 DBs)
	moveq	#$20,d2
	bra.s	.DBGet
.DB1:
	bsr.s	.BitTest
	bcs.s	.DB2
	moveq	#5,d1			;Maske: 01 (5 DBs)
	moveq	#0,d2
	bra.s	.DBGet
.DB2:
	moveq	#14,d1			;Maske: 11 (12 DBs)
	move.w	#$220,d2
.DBGet:
	bsr.s	.GetBits
	add.w	d2,d0
	lea	0(a1,d0.w),a3		;a3 auf Anf zu kopierender Seq setzten
.InsSeqLoop:
	move.b	-(a3),-(a1)		;Byte kopieren
	dbf	d4,.InsSeqLoop

	bra.w	.DecrLoop
*------------
.BitTest:
	subq.w	#1,d7
	bne.s	.BTNoLoop
	moveq	#16,d7			;hier kein add notwendig: d7 vorher 0
	move.w	d6,d0
	lsr.l	#1,d6			;Bit rausschieben und Flags setzen
	swap	d6			;ror.l	#16,d6
	move.w	-(a2),d6		;nächstes Wort holen
	swap	d6			;rol.l	#16,d6
	lsr.w	#1,d0			;Bit rausschieben und Flags setzen
	rts
.BTNoLoop:
	lsr.l	#1,d6			;Bit rausschieben und Flags setzen
	rts
*----------
.GetBits:				;d1:AnzBits->d0:Bits
	move.w	d6,d0			;d6:Akt Wort
	lsr.l	d1,d6			;nächste Bits nach vorne bringen
	sub.w	d1,d7			;d7:Anz Bits, die noch im Wort sind
	bgt.s	.GBNoLoop
;	add.w	#16,d7			;BitCounter korrigieren
	add.w	d3,d7			;BitCounter korrigieren
	ror.l	d7,d6			;restliche Bits re rausschieben
	move.w	-(a2),d6		;nächstes Wort holen
	rol.l	d7,d6			;und zurückrotieren
.GBNoLoop:
	add.w	d1,d1			;*2 (in Tab sind Ws)
	and.w	.AndData-2(pc,d1.w),d0	;unerwünschte Bits rausschmeißen
	rts
*----------
.AndData:
	dc.w	%1,%11,%111,%1111,%11111,%111111,%1111111
	dc.w	%11111111,%111111111,%1111111111
	dc.w	%11111111111,%111111111111
	dc.w	%1111111111111,%11111111111111
*-----------
.DecrEnd:
	rts		;a5: Start of decrunched Data


FIRSTCALL	dc.w	-2

*******************************************
*** Wait Beam Delay			***
*******************************************

; needs: # of raster to wait in d7

WAITbeam
	move.l	d0,-(a7)
.loop	move.l	$dff004,d0
	and.l	#$1ff00,d0
	cmp.l	#$12f*256,d0
	bne.s	.loop

.wait	move.l	$dff004,d0
	and.l	#$1ff00,d0
	cmp.l	#$12e*256,d0
	bne.s	.wait
	dbf	d7,.loop
	move.l	(a7)+,d0
	rts

DIRTABLES
	dc.l	0	; MAINDISK
	dc.l	0	; LEVEL DISK 1
	dc.l	0	; LEVEL DISK 2
	dc.l	0	; LEVEL DISK 3
	dc.l	0	; FINAL DISK

	; Format: NAME.l, StartBlock.w, LengthInBytes.l

*******************************************
*** 		MAIN DISK		***
*******************************************

DIR_MAINDISK	INCLUDE	DH1:!MAINDISK/FILES_FINAL/!DIR.s
		;INCLUDE	DH1:!MAINDISK/DIR.S
		dc.l	-1			; end of file table!
		
*******************************************
*** 		LEVEL DISK 1		***
*******************************************

DIR_LEVELDISK1	INCLUDE	DH1:!LEVELDISK1/DIR.S
		dc.l	-1			; end of file table
	
*******************************************
*** 		LEVEL DISK 2		***
*******************************************

DIR_LEVELDISK2	INCLUDE	DH1:!LEVELDISK2/DIR.S
		dc.l	-1			; end of file table!

*******************************************
*** 		LEVEL DISK 3		***
*******************************************

DIR_LEVELDISK3	INCLUDE	DH1:!LEVELDISK3/FILES_FINAL/!DIR.s
		;INCLUDE	DH1:!LEVELDISK3/DIR.S
		dc.l	-1			; end of file table!

*******************************************
*** 		FINAL DISK		***
*******************************************

DIR_FINALDISK	INCLUDE	WORK:!FINALDISK/FILES_CRM/!DIR.S
		;INCLUDE	DH1:!FINALDISK/DIR.S
		dc.l	-1			; end of file table!



*****************************************************************************

     *     System Independant Multi-Disk Block Loader V1.1 - 26/4/94   *
     *-----------------------------------------------------------------*

                    * By PHIL RUSTON AKA:PHIL!94/LSD *

* You MUST call "INIT_DISK" before loading for the 1st ever time.

* To LOAD A FILE Set:-

* A6 - Destination Address.
* D5 - Disk / Drive Number to load from. (see below).
* D6 - Bytes to load.
* D7 - Starting Block.

* Then call: "BLOCK_LOAD" - All registers preserved except D7

* D7 returns as 00 is load OK. Else one of the following errors:

* 01 = No speed signal from motor   (disk ejected whilst loading)
* 02 = No DMA transfer time out     (no sync / ''            '' )
* 03 = Disk removed from drive      (whilst loading)
* 04 = Can't find that disk block   (disk corrupt)
* 05 = Wrong track marker ID        ('')
* 06 = Checksum error on disk block ('')
* 07 = Block number req'd too big   (you've (in)directly asked for >$6df)
* 08 = Disk requester               (Disk specifified in D5 isnt inserted)
* 09 = No disk in that drive        (Direct drive access failed)
* 0A = That drive is not connected  (''                      '')

* When setting D5, if you want to look for a 'named' disk (see docs on
* naming disks) then just set the Longword as $0-$7f as normal in D5. 
* If you  want to directly access a DRIVE (df0-df3) set the drive number
* to access in D5 and SET the HIGH word to $FFFF. See docs for more info.

* Call "MOTOR_OFF" when finished loading for a while!!

*******************************************************************************

;	section loadcode,code

**************
* Initialize *
**************

INIT_DISK
	movem.l a0-a6/d0-d7,-(a7)
	lea	$dff000,a0
	lea	$bfd000,a1		
	lea	Variables(pc),a2
	clr.l	attempts(a2)
	clr.l	tracksides(a2)
	clr.l	track_in(a2)
	moveq	#-1,d7
	move.l	d7,disks_in(a2)
	move.b	d7,track_in(a2)
	bsr	motor_off

	
	bset	#3,drives_avail(a2)	;df0: assumed always available
	moveq	#4,d1

ID_drives
	move.b	#$7f,$100(a1)	;what externals are connected?
	bclr	d1,$100(a1)		
	move.w	#100,d7
	bsr	cia_wait
	move.b	#$ff,$100(a1)		
	bclr	d1,$100(a1)
	moveq	#$1f,d7
	moveq	#0,d0
df1idloop
	lsl.l	#1,d0
	bclr	d1,$100(a1)
	btst	#5,$1001(a1)
	beq.s	nxtbit
	ori.w	#1,d0
nxtbit	bset	d1,$100(a1)
	dbf	d7,df1idloop
	not.l	d0

	cmpi.l	#-1,d0		
	bne.s	no_drive		
	bset	d1,drives_avail(a2)

no_drive
	addq.w	#1,d1
	cmpi.w	#7,d1
	bne.s	ID_drives
	bsr	motor_off

ident	moveq	#0,d0		;now identify any disks present.
	moveq	#3,d1		
id_dsk_lp
	btst	d1,drives_avail(a2)
	beq.s	no_drv

	move.b	d0,use_drive(a2)
	move.b	#$ff,$100(a1)
	bclr	d1,$100(a1)
	bsr	stepin
	bsr	initialize_drive
	btst	#2,$1001(a1)
	beq.s	no_drv
	move.b	#1,start_motor(a2)
	bsr	identify_disk

no_drv	bsr	motor_off
	addq.w	#1,d1
	addq.w	#1,d0
	cmpi.w	#4,d0
	bne.s	id_dsk_lp

	movem.l	(a7)+,a0-a6/d0-d7
	rts


******************
* Construct file *
******************

; d0: Start block
; d1: Size
; a0: Load Address

BLOCK_LOAD
	movem.l	d0-d6/a0-a6,-(a7)

	bsr	preserve_system
	
	and.l	#$ffff,d0
	move.l	d0,d7
	move.l	d1,d6
	move.l	a0,a6

	lea	$dff000,a0
	lea	$bfd000,a1
	lea	Variables(pc),a2
	clr.b	error(a2)
	clr.b	start_motor(a2)
	move.l	d7,d4
	subq.l	#1,d6
	
	tst.l	d5			;Disk or Drive access?
	bpl.s	find_disk

*****************************************************************************

	moveq	#3,d7		;Direct drive access but is that
	add.w	d5,d7		;drive attatched?
	btst	d7,drives_avail(a2)
	bne.s	drive_ok
	move.b	#$a,error(a2)	;Drive not connected - error $a
	bra	load_done
drive_ok	
	cmp.b	use_drive(a2),d5	;same drive as before?
	beq	loader
	bsr	motor_off
	move.b	d5,use_drive(a2)
	moveq	#3,d0
	add.w	d5,d0
	bclr	d0,$100(a1)
	move.b	#1,start_motor(a2)
	bsr	wait1msec
	btst	#2,$1001(a1)
	bne.s	disk_now
	bsr	stepcheck
	btst	#2,$1001(a1)
	bne.s	disk_now2
	move.b	#$9,error(a2)
	bra	load_done

disk_now2
	bsr	initialize_drive
	bsr	identify_disk		
disk_now
	bra	loader

*****************************************************************************

find_disk
	moveq	#0,d0		;Name disk access - is the required
fdisk_lp
	cmp.b	disks_in(a2,d0.w),d5	;disk already in a drive?
	beq	found_disk
	addq.w	#1,d0
	cmpi.w	#4,d0
	bne.s	fdisk_lp
				
disk_gone
	bsr	motor_off		;no drive contains req'd disk
	moveq	#0,d0		;so switch 'em off and see if
	moveq	#3,d1		;any drive has had a disk replaced.
fdisk_lp2
	btst	d1,drives_avail(a2)
	beq.s	dsk_n_rem
	move.b	#$ff,$100(a1)
	bclr	d1,$100(a1)
	bsr	wait1msec
	btst	#2,$1001(a1)		;disk replaced / ejected?
	bne.s	dsk_n_rem
	move.b	d0,use_drive(a2)
	bsr	stepcheck
	btst	#2,$1001(a1)
	beq.s	dsk_n_rem
	move.b	#1,start_motor(a2)
	bsr	initialize_drive
	bsr	identify_disk		;here's a new disk to identify
	cmp.b	disks_in(a2,d0.w),d5
	beq.s	loader
	bsr	motor_off

dsk_n_rem
	addq.w	#1,d1
	addq.w	#1,d0
	cmpi.w	#4,d0
	bne.s	fdisk_lp2
	bsr	motor_off
	move.b	#$08,error(a2)	;disk request - error 8.
	bra	load_done

found_disk
	
	cmp.b	use_drive(a2),d0	;is this a load from the same
	beq	loader		;drive as the last load?
	bsr	motor_off
	move.b	d0,use_drive(a2)
	bclr	d1,$100(a1)
	move.b	#1,start_motor(a2)
	bsr	wait1msec	
same_drv
	;btst	#2,$1001(a1)		;this drive had the required disk
	;bne	loader			;but has it been ejected?
	bra	loader
	move.b	#$ff,disks_in(a2,d0.w)
	bra	disk_gone		;yep - so look else where
	
Loader	move.l	d4,d0
Nxt_block
	bsr	get_block
	tst.b	error(a2)
	bne.s	Load_done
	tst.l	d6			;Byte countdown ended?
	bmi.s	load_done
	addq.w	#1,d0
	bra.s	Nxt_block

load_done
	move.b	#$ff,track_in(a2)
	tst.b	error(a2)
	beq.s	no_error
	bsr	motor_off
	move.w	#1000,d7
	bsr	cia_wait
no_error
	moveq	#0,d7
	move.b	error(a2),d7

	bsr	restore_system
	
	movem.l (a7)+,d0-d6/a0-a6
	rts

*************
* Shut down *
*************

MOTOR_OFF
	movem.l a1/a2/d7,-(a7)
	lea	$bfd000,a1
	lea	Variables(pc),a2
	move.b	#$ff,$100(a1)
	move.b	#$87,$100(a1)
	bsr	wait1msec
	move.b	#$ff,$100(a1)
	move.b	#$ff,use_drive(a2)
	moveq	#100,d7
	bsr	cia_wait
	movem.l (a7)+,a1/a2/d7
	rts

*****************************************************************************

*************
* Get Block *
*************

* D0 will equal block number required ($0 - $6df)

Get_block
	cmp.w	#$6df,d0		;check block is range
	bls.s	blockinrange
	move.b	#$7,error(a2)
	bra	No_reload

Blockinrange

	move.w	#$0503,attempts(a2)
	move.l	d0,d1		
	divu	#11,d1		;what track/side is that on?
	cmp.b	track_in(a2),d1	
	beq.s	Trksde_in		;no need to load!
	
Reload	bsr	Fetch_trackside		;loads MFM track required
	tst.b	error(a2)
	bne	No_reload

Trksde_in
	clr.b	error(a2)		;find a sector
	swap	d1			;get sector number lo.
	move.l	MFMbuffer(pc),a3
	lea	$31fe(a3),a5
find_sync
	move.w	#$4489,d7
fs_loop	cmp.w	(a3)+,d7
	beq.s	found_sync
	cmp.l	a5,a3
	bls.s	fs_loop
	move.b	#4,error(a2)		;couldnt find that sector-error 4
	bra	MFM_error

found_sync	

	cmp.w	(a3),d7		;2 syncs?
	beq.s	syncstart
	subq.w	#2,a3

syncstart
	lea	$2a(a3),a4		;Check header checksum.
	bsr	decode_lw		
	move.l	$2(a3),d3
	move.l	$6(a3),d4
	andi.l	#$55555555,d3
	andi.l	#$55555555,d4
	eor.l	d4,d3
	cmp.l	d3,d5
	bne.s	Not_sector

	lea	$2(a3),a4		;Track mark = Head position?
	bsr	decode_lw
	move.l	d5,d4		
	swap	d4
	moveq	#0,d7
	move.b	use_drive(a2),d7
	cmp.b	tracksides(a2,d7.w),d4
	beq.s	Track_mark_ok
	move.b	#5,error(a2)		;wrong track mark - error 5
	bra	MFM_error

Track_mark_ok

	lsr.w	#8,d5		;Is this the sector required?
	cmp.b	d5,d1
	beq.s	Found_block
	
Not_sector

	add.w	#$430,a3      	;loop until find correct sector
	bra.s	find_sync

Found_Block
	
	moveq	#0,d2		;Transfer converted MFM to dest.
	move.l	#$55555555,d7
	lea	$3a(a3),a4		
	lea	$23a(a3),a5
	move.l	d6,d1		;back up pointers in case of error.
	move.l	a6,a3		
	moveq	#$7f,d3

Conv_lw	move.l	(a4)+,d5
	move.l	(a5)+,d4
	and.l	d7,d5
	and.l	d7,d4
	eor.l	d5,d2		;update checksum
	eor.l	d4,d2
	add.l	d5,d5
	or.l	d4,d5
	subq.l	#4,d6
	bmi.s	lst_bytes
	move.l	d5,(a6)+
nxt_lw	dbf	d3,Conv_lw
	bra.s	blk_done

Lst_bytes
	move.w	d6,d4
	addq.w	#4,d4		;last bytes
	bmi.s	nxt_lw

Lb_loop	rol.l	#8,d5
	move.b	d5,(a6)+
	dbf	d4,Lb_loop
	bra.s	nxt_lw
		
blk_done
	sub.w	#$208,a4		;get data checksum
	bsr	decode_lw		
	cmp.l	d5,d2
	beq.s	no_reload
	move.b	#6,error(a2)		;checksum error - error 6
	move.l	a3,a6		;restore pointers
	move.l	d1,d6

MFM_error
	move.l	d0,d1		
	divu	#11,d1		
	subq.b	#1,attempts(a2)
	bne	Reload
	bsr	initialize_Drive	;reset heads
	move.b	#5,attempts(a2)
	subq.b	#1,attempts+1(a2)
	bne	Reload

No_reload
	rts

*****************************************************************************

* MFM Track loader!
* D1 will equal trackside 0 - 159

Fetch_trackside
	
		
	clr.b	error(a2)		
	tst.b	start_motor(a2)	;motor on already?
	beq.s	motor_ok

	clr.b	start_motor(a2)
	move.b	#$7f,$100(a1)
	moveq	#3,d7
	add.b	use_drive(a2),d7
	bclr	d7,$100(a1)
	move.w	#200,d2		;wait for correct motor speed.
wdskrdy	moveq	#10,d7
	bsr	cia_wait		
	btst	#5,$1001(a1)		
	dbeq	d2,wdskrdy
	tst.w	d2
	bpl.s	motor_ok
	move.b	#1,error(a2)		;no speed signal - error 1
	btst	#2,$1001(a1)				
	bne.s	disk_ok
	move.b	#3,error(a2)		;disk removed - error 3
disk_ok	bra	trackload_end

motor_ok
	move.w	d1,d3
	lsr.b	#1,d3		;select disk side to load from.
	bcc.s	head1
	bclr	#2,$100(a1)
	bra.s	checkhead
head1	bset	#2,$100(a1)

checkhead
	moveq	#0,d7
	move.b	use_drive(a2),d7
	move.b	tracksides(a2,d7.w),d2	;ensure head is over correct
	lsr.b	#1,d2		;track
	cmp.b	d2,d3
	beq.s	headok
	bls.s	seekout
	bsr	stepin
	bra.s	checkhead 
seekout	bsr	stepout	
	bra.s	checkhead

headok	moveq	#0,d7
	move.b	use_drive(a2),d7
	move.b	d1,tracksides(a2,d7.w)
	move.w	#$2,$9c(a0)		;clear disk block irq
	moveq	#18,d7		;settle wait.
	bsr	cia_wait

	move.l	MFMBUFFER(pc),$20(a0)
	move.w	#$4000,$24(a0)	

	move.w	#%1000001000010000,dmacon(a0)
	move.w	#%0111111111111111,adkcon(a0)
	move.w	#%1000010100000000,adkcon(a0)	; Sync on, 2ms

	;move.w	#$8010,$96(a0)	;enable dma
	;move.w	#$6800,$9e(a0)	
	;move.w	#$9500,$9e(a0)
	move.w	#$4489,$7e(a0)	;sync
	move.w	#$9900,$24(a0)	
	move.w	#$9900,$24(a0)	

	move.w	#200,d2
waitdirq
	moveq	#10,d7
	bsr	cia_wait		
	btst	#1,$1f(a0)		
	dbne	d2,waitdirq
	tst.w	d2
	bpl.s	read_ok
	move.b	#2,error(a2)		;No DMA finish-Time Out-error 2

read_ok	move.b	d1,track_in(a2)
	move.w	#$4000,$24(a0)	
	move.w	#$10,$96(a0)		;disable dma

Trackload_end

	rts

*******************************************************************************

decode_lw
	move.l	#$55555555,d7	;decode two consec mfm longwords
	move.l	(a4),d5		;into d5
	move.l	$4(a4),d4	
	and.l	d7,d5	
	and.l	d7,d4
	add.l	d5,d5
	or.l	d4,d5		
	rts

stepout	moveq	#0,d7
	move.b	use_drive(a2),d7
	subq.b	#2,tracksides(a2,d7.w)
	bset	#1,$100(a1)
	bra.s	step

stepin	moveq	#0,d7
	move.b	use_drive(a2),d7
	addq.b	#2,tracksides(a2,d7.w)
	bclr	#1,$100(a1)
step	bsr.s	shortwait
	bclr	#0,$100(a1)
	bsr.s	shortwait
	bset	#0,$100(a1)
	moveq	#4,d7
	bsr	cia_wait
	rts

shortwait
	nop
	nop
	nop	
	nop	
	rts

initialize_drive

	btst	#$4,$1001(a1)		
	beq.s	gottrack0
	bsr.s	stepout
	bra.s	initialize_drive

gottrack0	
	moveq	#0,d7
	move.b	use_drive(a2),d7
	clr.b	tracksides(a2,d7.w)
	move.w	#20,d7
	bsr	cia_wait
	rts

Identify_disk

	movem.l a6/d0-d1/d4-d7,-(a7)
	lea	ID_buffer(pc),a6
	moveq	#$c,d6
	moveq	#$0,d0
	bsr	get_block
	moveq	#0,d6
	move.b	use_drive(a2),d6
	tst.b	error(a2)
	bne.s	not_sys

	move.w	ID_buffer+8(pc),d7
	cmpi.w	#"PR",d7
	beq.s	sys_disk
not_sys	moveq	#-1,d7
	bra.s	what_disk

sys_disk
	move.w	ID_buffer+$a(pc),d7
	sub.b	#"0",d7
what_disk
	move.b	d7,disks_in(a2,d6.w)
	move.b	#$ff,track_in(a2)
	movem.l	(a7)+,a6/d0-d1/d4-d7
	rts

stepcheck
	moveq	#0,d7
	move.b	use_drive(a2),d7
	btst	#1,tracksides(a2,d7.w)
	bne.s	h_odd
	bsr	stepin
	bra.s	stepdone
h_odd	bsr	stepout
stepdone
	rts

;wait1msec
; 	moveq	#1,d7
;
;cia_wait
;	move.b	#$08,$f00(a1)	;set one shot / stop timer.
;	move.b	#$cc,$600(a1)	;set timer lo
;	move.b	#$02,$700(a1)	;set timer hi (starts counter)
;ciawlp2	btst	#0,$f00(a1)		;wait for ciab timer b to timeout
;	bne.s	ciawlp2
;	subq.w	#1,d7
;	bne.s	cia_wait		;d7 = milliseconds to wait.
;	rts

;wait1msec
;	moveq	#1,d7
;cia_wait
;	move.b	#$08,$e00(a1)	; set one shot / stop timer
;	move.b	#$cc,$400(a1)	; set timer lo
;	move.b	#$02,$500(a1)	; set timer hi (starts counter)
;.loop	btst	#0,$d00(a1)	; wait for ciab timer a to timeout
;	bne.b	.loop
;	subq.w	#1,d7
;	bne.b	cia_wait
;	rts



;wait1msec
;	moveq	#10,d7
;	bra.w	WAITbeam
;cia_wait
;	;moveq	#64,d7		; standard
;	bra	WAITbeam

	

		
wait1msec	move.l	d6,-(a7)
		moveq	#15,d6
.wait2		move.b	$dff006,d7
.wait		cmp.b	$dff006,d7
		bne.b	.not1
		bra.b	.wait
.not1		subq.b	#1,d6
		bne.b	.wait2
		move.l	(a7)+,d6
		rts

cia_wait	movem.l	d6/a0,-(a7)
		lea	.fucktab(pc),a0
.fuck		cmp.w	(a0)+,d7
		beq.b	.bla
		addq.w	#2,a0
		bra.b	.fuck	

.bla		move.w	(a0),d6
		;move.b	#150,d6
.wait2		move.b	$dff006,d7
.wait		cmp.b	$dff006,d7
		bne.b	.not1
		bra.b	.wait
.not1		subq.b	#1,d6
		bne.b	.wait2
		movem.l	(a7)+,d6/a0
		rts

.fucktab	dc.w	4,50
		dc.w	10,60
		dc.w	18,80
		dc.w	20,85
		dc.w	100,100
		dc.w	1000,150
		

MFMBUFFER	dc.l	MFMBUFFER1

*******************************************************************************

Attempts	= $0
Error		= $2
Start_motor	= $3
Disks_in	= $4
Tracksides	= $8
Track_in	= $c
Drives_avail	= $d
Use_drive	= $e

variables	dcb.l 4,$0

ID_buffer dcb.l 3,$0

;	section chipstuff,data_c

;MFMbuffer	dcb.w $1910,$0000

*******************************************************************************

	ds.b	100-20



*****************************************************************************
*		    SYSTEM PRESERVE, RESTORE & KILL ROUTINES		    *
*****************************************************************************
preserve_system	movem.l	a6/a4/d0,-(sp)
		lea	custom,a6
		lea	loader_info_buffer(pc),a4
		move	dmaconr(a6),old_dmacon(a4)
		move	intreqr(a6),old_intreq(a4)
		move	intenar(a6),old_intena(a4)
		move	adkconr(a6),old_adkcon(a4)
		move	#%1000000000000000,d0
		or	d0,old_dmacon(a4)
		or	d0,old_intreq(a4)
		or	d0,old_intena(a4)
		or	d0,old_adkcon(a4)
		movem.l	(sp)+,a6/a4/d0
		rts

restore_system	movem.l	a6/a4,-(sp)
		lea	custom,a6
		lea	loader_info_buffer(pc),a4
		move	#%0111111111111111,intena(a6)
		move	old_dmacon(a4),dmacon(a6)
		move	old_intreq(a4),intreq(a6)
		move	old_adkcon(a4),adkcon(a6)
		move	old_intena(a4),intena(a6)
		movem.l	(sp)+,a6/a4
		rts

sys_vars	RSRESET
old_dmacon	rs.w	1
old_intreq	rs.w	1
old_adkcon	rs.w	1
old_intena	rs.w	1
size		rs.b	0
loader_info_buffer	ds.b	size

custom	= $dff000
vhposr	= $06
dmacon	= $96
intena	= $9A
intreq	= $9C
adkcon	= $9e
dmaconr	= $02
intenar	= $1C
intreqr	= $1E
adkconr	= $10

dsksync	= $7e
dskpt	= $20
dsklen	= $24





INTENASET	= %1100000000000000
;		   ab-------cdefg--
;	a: SET/CLR Bit
;	b: Master Bit
;	c: Blitter Int
;	d: Vert Blank Int
;	e: Copper Int
;	f: IO Ports/Timers
;	g: Software Int

DMASET		= %1000001111000000
;		   a----bcdefghi--j
;	a: SET/CLR Bit
;	b: Blitter Priority
;	c: Enable DMA
;	d: Bit Plane DMA
;	e: Copper DMA
;	f: Blitter DMA
;	g: Sprite DMA
;	h: Disk DMA
;	i..j: Audio Channel 0-3

SCREEN	= $60000

; d0: FILENAME
; d1: DISK NUMBER (ASCII)
; d7: ERROR CODE FROM TRACKLOADER

SHOW_DEBUGSCREEN
	movem.l	d0-a6,-(a7)

	bsr	preserve_system

	lea	$dff000,a6
	move.w	#$7FFF,d2
	move.w	d2,$9A(a6)		; Disable Interrupts
;	bsr.w	WaitRASTER
	move.w	d2,$96(a6)		; Clear all DMA channels
	move.w	d2,$9C(a6)		; Clear all INT requests
	move.w	#INTENASET!$C000,$9A(a6); set Interrupts+ BIT 14/15
	move.w	#DMASET!$8200,$96(a6)	; set DMA	+ BIT 09/15


; clear screen
	lea	SCREEN,a0
	move.w	#80*256/4-1,d6
	moveq	#0,d2
.cls	move.l	d2,(a0)+
	dbf	d6,.cls

	bsr.b	.MakeDebugText
	

	lea	cop(pc),a0
	move.l	a0,$80(a6)
	move.w	#0,$88(a6)


.dead	;btst	#2,$16(a6)
	bra.b	.dead

	bsr	restore_system

	movem.l	(a7)+,d0-a6
	rts

.MakeDebugText

; get current disk
	lea	DISKID_TEXT(pc),a0
	lea	DISKNAMES(pc),a1
	move.b	d1,(a0)+
	subq.b	#1,d1
	sub.b	#"0",d1
	and.w	#$ff,d1
	lsl.w	#4,d1
	lea	(a1,d1.w),a1
	moveq	#16-1,d6
.copy	move.b	(a1)+,(a0)+
	dbf	d6,.copy	

; convert filename to Ascii
	move.l	d0,d6
	lea	FILENAME_TEXT(pc),a1
	bsr	Convert
	addq.l	#2,a1
	move.l	d6,d0
	swap	d0
	move	d0,d1
	swap	d0
	eor	d1,d0
	swap	d0
	neg.b	d0
	rol.l	#7,d0

	moveq	#4-1,d6
.check	cmp.b	#" ",d0
	blt.b	.bug
	cmp.b	#$7e,d0
	bgt.b	.bug
	ror.l	#8,d0
	dbf	d6,.check
	move.l	d0,(a1)
	bra.b	.nobug
	
.bug	move.l	#"BUG!",(a1)	
.nobug

; fill in Error description
	move.l	d7,d0
	add.b	#"0",d0
	lea	ERRORNUMBER(pc),a0
	move.b	d0,(a0)
	subq.w	#1,d7
	lea	ERRORs(pc),a0
	lsl.w	#5,d7
	lea	(a0,d7.w),a0
	lea	ERRORTEXT(pc),a1
	moveq	#32/4-1,d7
.fill	move.l	(a0)+,(a1)+
	dbf	d7,.fill

; convert registers to Ascii
	lea	REGS(pc),a0
	lea	REGS_ASCII(pc),a1
	moveq	#16-1,d7
.loop	move.l	(a0)+,d0
	bsr.w	Convert
	dbf	d7,.loop


; write data registers
	lea	REGS_TEXT(pc),a0
	lea	REGS_ASCII(pc),a1
	moveq	#8-1,d7
.Dregs	move.l	(a1)+,3(a0)
	move.l	(a1)+,3+4(a0)
	lea	24(a0),a0
	dbf	d7,.dregs

; write address registers
	lea	REGS_TEXT(pc),a0
	moveq	#8-1,d7
.Aregs	move.l	(a1)+,15(a0)
	move.l	(a1)+,15+4(a0)
	lea	24(a0),a0
	dbf	d7,.Aregs


	lea	DEBUG_TEXT(pc),a0
	lea	SCREEN,a1
	bsr.w	PRINTTEXT


	lea	REGS_TEXT(pc),a0
	moveq	#57-1,d7
	moveq	#" ",d0
.clear	move.b	d0,(a0)+
	dbf	d7,.clear
	clr.b	(a0)
	moveq	#'"',d0
	move.b	d0,-2(a0)
	move.b	d0,-19(a0)		

	lea	SCREEN,a4
	lea	6*10*80+23(a4),a4
;	lea	13*10*80(a4),a4
	lea	REGS+8*4(pc),a5
	move.w	#" >",d4
	moveq	#8-1,d5
.loop2	lea	REGS_TEXT(pc),a1
	lea	40-1(a1),a2
	move.w	d4,(a1)+
	;subq.l	#1,a1
	move.l	(a5)+,a3
	
	moveq	#4-1,d7
.conv	move.l	(a3)+,d0
	move.l	d0,d3
	bsr.b	CONVERT
	addq.l	#1,a1

	move.l	d3,d0
	rol.w	#8,d0
	swap	d0
	rol.w	#8,d0
	moveq	#4-1,d6
	
.conv2	cmp.b	#" ",d0
	blt.b	.illegal
	cmp.b	#$7e,d0
	bgt.b	.illegal
	move.b	d0,(a2)+
	bra.b	.legal
.illegal
	move.b	#'.',(a2)+
.legal
	
	ror.l	#8,d0
	dbf	d6,.conv2
	dbf	d7,.conv
	lea	REGS_TEXT(pc),a0
	move.l	a4,a1
	bsr.b	PRINTTEXT
	lea	80*10(a4),a4
;	add.l	#"A1= "-"A0= ",d4
	dbf	d5,.loop2
	rts

CONVERT	moveq	#8-1,d1
.loop	rol.l	#4,d0
	move.b	d0,d2
	and.b	#$f,d2
	add.b	#"0",d2
	cmp.b	#"9",d2
	ble.b	.yo
	add.b	#7,d2
.yo	move.b	d2,(a1)+
	dbf	d1,.loop
	rts
	
; a0: Text
; a1: Screen

PRINTTEXT
	movem.l	d0-a6,-(a7)
	moveq	#0,d0			; x pos
	moveq	#0,d1			; y pos
.write	moveq	#0,d2
	move.b	(a0)+,d2
	beq.b	.end
	cmp.b	#10,d2
	bne.b	.nonew
	moveq	#0,d0
	add.w	#80*10,d1
	add.w	d1,d0
	bra.b	.write
.nonew	sub.w	#" ",d2
	lsl.w	#3,d2
	lea	FONT(pc),a2
	lea	(a2,d2.w),a2
	lea	(a1,d0.w),a3

	moveq	#8-1,d7
.char	move.b	(a2)+,(a3)
	lea	80(a3),a3
	dbf	d7,.char
	addq.w	#1,d0
	bra.b	.write
.end	movem.l	(a7)+,d0-a6
	rts


*******************************************
*** Wait Vertical Blank			***
*******************************************

WAITraster
	move.l	d0,-(a7)
loop_WR	move.l	$dff004,d0		; wait for Beam pos $xxx
	and.l	#$1ff00,d0
	cmp.l	#$12f*256,d0
	bne.s	loop_WR
	move.l	(a7)+,d0
	rts

REGS		dcb.l	16
REGS_ASCII	ds.b	16*8
DEBUG_TEXT	dc.b	"             Apano Sin - cracked by sTYLE/RESISTANCE IN AUGUST 2ooo!",10,10
		dc.b	"Sorry, could not load File: "
FILENAME_TEXT	dc.b	"00000000 (0000)",10
		dc.b	"Current Disk-ID: "
DISKID_TEXT	dc.b	"03 - LEVEL DISK 1",10
		dc.b	"ERRORCODE "
ERRORNUMBER	dc.b	"0 - "
ERRORTEXT	dc.b	"                                ",10,10
REGS_TEXT	dc.b	"D0=xxxxxxxx A0=xxxxxxxx",10
		dc.b	"D1=xxxxxxxx A1=xxxxxxxx",10
		dc.b	"D2=xxxxxxxx A2=xxxxxxxx",10
		dc.b	"D3=xxxxxxxx A3=xxxxxxxx",10
		dc.b	"D4=xxxxxxxx A4=xxxxxxxx",10
		dc.b	"D5=xxxxxxxx A5=xxxxxxxx",10
		dc.b	"D6=xxxxxxxx A6=xxxxxxxx",10
		dc.b	"D7=xxxxxxxx A7=xxxxxxxx",10,10

		dc.b	"MINIDEBUG V1.o by sTYLE/RSE!",10,10
		dc.b	"SPECIAL GREETS TO: NATAS (you know why!)  Adkins  4Play  Ixxy  Zoltrix",10
		dc.b	"Galahad  Thaler  Saint  and all forgotten ones.",10,10
		dc.b	"This crack is dedicated to Jacky! Maybe you'll be mine one fine day...",10
		dc.b	"'It took me long to recognize, what's behind those empty eyes...'",10,10
		dc.b	"Contact me for any reason at: sTYLe_xTREK(at)GMX.de",10
		dc.b	"FAITH IS LAME!!",10,0
		

DISKNAMES
.disk1		dc.b	" - TRAILER DISK "
.disk2		dc.b	" - MAIN DISK    "
.disk3		dc.b	" - LEVEL DISK 1 "
.disk4		dc.b	" - LEVEL DISK 2 "
.disk5		dc.b	" - LEVEL DISK 3 "
.disk6		dc.b	" - FINAL DISK   "

ERRORS
.error1		dc.b	"No speed signal from motor      "
.error2		dc.b	"No DMA transfer time out        "
.error3		dc.b	"Disk removed from drive         "
.error4		dc.b	"Can't find that disk block      "
.error5		dc.b	"Wrong track marker ID           "
.error6		dc.b	"Checksum error on disk block    "
.error7		dc.b	"Block number out of range       "
.error8		dc.b	"Disk requested not found        "
.error9		dc.b	"No disk in drive                "
.error10	dc.b	"That drive is not connected     "
.error11	dc.b	"File not found in directory tab!"	
		CNOP	0,2

	

FONT		INCBIN	SOURCES:FONTS/FONT8/PAT.fnt


cop	dc.w	$1fc,0			; burst off 
	dc.w	$100,$0200		; no dma
	dc.w	$08e,$2981		; Window start
	dc.w	$090,$29d1		; Window stop
	dc.w	$092,$3c		; Data fetch start = xx1/2-8.5
	dc.w	$094,$d4		; Data fetch stop  = PLwdt*4-8+DDstart
	dc.w	$096,$20		; disable Sprite DMA
	dc.w	$108,$00		; Mod 1 odd
	dc.w	$10a,$00		; Mod 2 even
	dc.w	$104,$00		; Bplcon2
	dc.w	$102,$00		; Bplcon1
p1	dc.w	$e0,$6,$e2,0
	dc.w	$100,$9200		; Bplcon0
	dc.w	$180,0,$182,$fff
	dc.l	-2			; End of Clist


LOADEREND

	PRINTT
	PRINTT
	PRINTT	"LOADERSIZE:"
	PRINTV	LOADEREND-LOADER1

	IFLT	(LOADERLENGTH*512)-(LOADEREND-LOADER1)
	PRINTT
	PRINTT	"ATTENTION!!! LOADERLENGTH TOO SMALL!!!"
	PRINTT
	ENDC
	
