* Coder V0.8, by TIP of TNM in 1988
*
* Bootblock routine V1.0

* EXEC

AllocMem       = -198
DoIO           = -456
WaitIO         = -474
CopyMem        = -624

* Datenbereich

Interrupts = 0
DiskIO = 2
DiskBlock = $2000
DSKPORT = $bfd100

p:
Start:
	movem.l	a0-a6/d0-d7,-(a7)	;Alles zwischenspeichern

	lea	$dff000,a4		;Co-Chips
	lea	$70000,a5		;Datenfeld
	lea	DSKPORT,a6		;DISKPORT

	move.l	a1,DiskIO(a5)		;Disk IO Extra speichern

	move.w	$1c(a4),(a5)		;Interrupts speichern
	move.w	#$7fff,$9a(a4)		;Interrupts verbieten

	move.b	#$7d,(a6)		;Drive 1 Head 0 anwaehlen
	nop
	nop
	move.b	#%01101101,(a6)

Track0:	btst	#4,$bfe001-DSKPORT(a6)	;Auf Track 0 fahren
	beq.s	T0_End
	bset	#1,(a6)			;Track-1
	bclr	#0,(a6)
	bset	#0,(a6)
	move.w	#2500,d0
Wait:	dbf	d0,Wait
	bra.s	Track0
T0_End:
	move.w	#$8400,$9e(a4)		;ADKCON
	move.w	#$4489,$7e(a4)		;SYNC
	lea	DiskBlock(a5),a0
	move.l	a0,$20(a4)		;DSKPT

	move.b	$bfdd00-DSKPORT(a6),d0	;Auf Index Warten
WI:	move.b	$bfdd00-DSKPORT(a6),d0
	btst	#4,d0
	beq.s	WI

	move.w	#512*11+6+$8000,d0	;Laden starten
	move.w	d0,$24(a4)		;Uebertragung an
	move.w	d0,$24(a4)
	move.w	#2,$9c(a4)		;INTREQ

Wait1:	btst	#1,$1f(a4)		;Auf Track warten
	beq.s	Wait1

	clr.w	$24(a4)			;DISKDMA AUS
	move.w	#$400,$9e(a4)

	move.b	#$fd,(a6)		;Drive deselecten
	nop
	nop
	move.b	#%11101101,(a6)

	move.l	4.w,a6			;ReCodierter Track
	move.l	#512*11,d0		;Speicher
	moveq	#2,d1
	jsr	-198(a6)

	lea	Buffer(pc),a0		;In die neue TD routine
	move.l	d0,(a0)
	move.l	d0,a1			;Destination
	move.l	d0,a2

	move.l	#$55555555,d5		;Zur Platzersparnis !
	lea	DiskBlock+2(a5),a0	;Source

	move.w	#512*11/4-1,d7		;Anzahl der Langworte
RCode:
	move.l	(a0)+,d0		;2 Langworte recodieren
	move.l	(a0)+,d1
	and.l	d5,d0
	and.l	d5,d1
	lsl.l	#1,d0
	or.l	d1,d0
	move.l	d0,(a1)+
	dbf	d7,RCode
	clr.l	(a2)			;Pruefsumme:0

* Einbauroutine ***********************

	move.l	DIskIO(a5),a1		;Disk IO
	move.l	4.w,a6			;Ex-Base

	move.l	20(a1),a4		;TrackDisk device
	lea	-30(a4),a0		;Alte trackdisk Routine
	lea	OldJump(pc),a1
	moveq	#6,d0
	jsr	CopyMem(a6)

	move.w	#$4ef9,-30(a4)		;JMP

	move.l	#NewTDEnd-NewTD,d0	;Speicher fuer die neue DoIO rout
	clr.l	d1
	jsr	AllocMem(a6)

	move.l	d0,-28(a4)		;NewTD Jump

	lea	NewTD(pc),a0		;NewTD routine kopieren
	move.l	d0,a1
	move.l	#NewTDEnd-NewTD,d0
	jsr	CopyMem(a6)

* DISK CODED BY THE NEW MASTERS *******

	lea	$dff000,a4

	lea	GraphicsName(pc),a1
	jsr	-408(a6)
	move.l	d0,a6

	lea	TextAttribute(pc),a0
	lea	Font(pc),a1
	move.l	a1,(a0)
	jsr	-72(a6)			;Open Font
	move.l	d0,a0
	move.l	34(a0),a2

	lea	$50000,a3		;Memory Clearen
	move.w	#$1000,d0
CMem:	clr.l	(a3)+
	dbf	d0,CMem

	lea	Gelaber(pc),a0

	lea	$50001,a3		;Text in Grafik Printen
	moveq	#Gelaber2-Gelaber-1,d0
	bsr.L	PrintText
	lea	$50180,a3
	moveq	#Gelaber2End-Gelaber2-1,d0
	bsr.L	PrintText

	lea	NewCopperSabbel(pc),a0
	move.l	a0,$80(a4)		;Neue Copperliste setzen
	clr.w	$88(a4)
	move.w	#$8100,$96(a4)

	moveq	#14,d1
Dunkel:
	bsr.s	WaitTop
	sub.w	#$111,6(a0)
	subq.b	#1,10(a0)
	dbf	d1,Dunkel

	move.l	#$60000,d0		;Auf Maus warten oder weiter
WartFick:
	btst	#6,$bfe001
	beq.s	Endi
	subq.l	#1,d0
	bne.s	WartFick
Endi:
	moveq	#14,d1
Hell:
	bsr.s	WaitTop
	add.w	#$111,6(a0)
	addq.b	#1,10(a0)
	dbf	d1,Hell

	move.w	#$100,$96(a4)
	move.l	38(a6),$80(a4)		;Copperliste ausschalten
	clr.w	$88(a4)

	move.l	a6,a1			;Graphicslib schliessen
	move.l	4.w,a6
	jsr	-414(a6)

	move.w	(a5),d0			;Interrupts erlauben
	bset	#15,d0
	move.w	d0,$9a(a4)

	movem.l	(a7)+,a0-a6/d0-d7	;Alles zurueckholen

	lea	DosName(pc),a1		;Normale Dos-Routine
	jsr	-96(a6)
	move.l	d0,a0
	move.l	22(a0),a0
	clr.l	d0
	rts

WaitTop:
	moveq	#2,d2
WT_1:
	move.l	4(a4),d0
	lsr.l	#8,d0
	cmp.b	#1,d0
	bne.s	WT_1
WT_2:
	cmp.b	#2,6(a4)
	bne.s	WT_2
	dbf	d2,WT_1
	rts

NewCopperSabbel:
	dc.l	$01000000
	dc.l	$01800fff
	dc.l	$01820fff
	dc.l	$00e00005
	dc.l	$00e20000
	dc.l	$00920070
	dc.l	$009400a8
	dc.l	$8007fffe
	dc.l	$01001000
	dc.l	$c807fffe
	dc.l	$01000000
	dc.l	$fffffffe

TextAttribute:
	dc.l	0
	dc.w	8
	dc.b	0,0
PrintText:
	clr.l	d1
	move.b	(a0)+,d1
	sub.b	#32,d1
	add.l	a2,d1
	move.l	d1,a1
	moveq	#7,d2
PT_Luuple:
	move.b	(a1),(a3)
	move.b	(a1),16(a3)
	add.l	#32,a3
	add.l	#$c0,a1
	dbf	d2,PT_Luuple
	sub.l	#255,a3
	dbf	d0,PrintText
	rts

* Neue Begin IO routine C by TIP ******

NewTD:
	cmp.b	#2,29(a1)
	bne.L	OldJump
ReadIt:
	move.l	a1,-(a7)
	bsr.L	OldJump
	move.l	(a7)+,a1

	move.l	a1,-(a7)
	move.l	a6,-(a7)
	move.l	4.w,a6
	jsr	-474(a6)
	move.l	(a7)+,a6
	move.l	(a7)+,a1

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

	move.l	4(a1),a0
	move.l	a1,(a0)
	move.l	(a1),a0
	move.l	a1,4(a0)

	tst.b	31(a1)			;Fehlerflag gesetzt?
	bne.s	Back

	move.l	36(a1),d1		;Laenge
	beq.L	Error			;Laenge 0:

	clr.l	d0			;PPLLAATTZZ sparen

	move.l	40(a1),a2		;Buffer
	cmp.l	d0,a2
	beq.L	Error

	move.l	#1024,d3		;PLATZLI
	move.l	#512,d4			;PLLLLLLLLATZ

	move.l	44(a1),a0		;TrackOffset
	cmp.l	d0,a0
	bne.s	NoBootBlock		;Ab 1. Sector

	add.l	d3,a2			;Bootblock zur Bufferadresse addieren
	add.l	d3,a0			;Offset+Bootblock
	sub.l	d4,d1			;Laenge - Bootblock
	beq.s	Back			;Nur 1. Sector gelesen ?
	sub.l	d4,d1
	beq.s	Back			;Nur erste 2 Sektoren

NoBootBlock:
	cmp.l	d4,a0			;Start bei 2. Sektor ?
	bne.s	NoSecondSector
	add.l	d4,a0			;Offset+1 sektor
	add.l	d4,a2			;1 Sektor nicht invertieren
	sub.l	d4,d1			;Laenge-1 Sektor
	beq.s	Back			;Bloss 2. Sektor wurde gelesen
NoSecondSector:
	move.l	#512*11,d2		;Mal wieder Platz sparen
SearchBufferOffset:
	cmp.l	d2,a0
	blt.s	BufferOffsetFound
	sub.l	d2,a0
	bra.s	SearchBufferOffset

BufferOffsetFound:
	move.l	a0,d0			;Buffer Byte Zaehler Start
	add.l	Buffer(pc),a0		;Zu Bufferadresse addieren

	clr.l	d3			;Dest Byte Zaehler
INV_Loop:
	move.l	(a0)+,d4
	eor.l	d4,(a2)+
	addq.l	#4,d0
	cmp.l	d0,d2			;Uebertrag des Buffers
	bne.s	INV_Loop1
	clr.l	d0
	sub.l	d2,a0
INV_Loop1:
	addq.l	#4,d3
	cmp.l	d1,d3
	bne.s	INV_Loop
Back:
	movem.l	(a7)+,a0-a6/d0-d7	;Zurueck
	rts
Error:
	moveq	#50,d6
S_2:	move.w	#$ffff,d7
S_1:	move.w	d7,$dff180
	dbf	d7,S_1
	dbf	d6,S_2
	bra.s	Back

OldJump:	blk.b	6,0
Buffer:		dc.l	0
NewTDEnd:

***************************************
* Unterprogramme **********************
***************************************

* Daten

DosName:	dc.b	'dos.library',0
GraphicsName:	dc.b	'graphics.library',0
Gelaber:	dc.b	'DISK CODED BY'
Gelaber2:	dc.b	'THE NEW MASTERS'
Gelaber2End:
Font:		dc.b	'topaz.font',0
End:
