שתשת                                        ;----------------------------------------------------------------------------
;	mainloader. refer to the logbook textfile for info!
;	ps: ich trete dem coder so tief in den arsch das es aus den
;	ohren quillt. ich rule ich bin gott ;) ach was ich bin mehr als das!
;----------------------------------------------------------------------------
;	routine beginnt ab $a00
;----------------------------------------------------------------------------

secstart:
	bra.b	start

interruptroutine:
	move.w	#$0020,$dff09c

irqbitbuffer	=	*-3
	nop
	rte

;----------------------------------------------------------------------------
;	start
;----------------------------------------------------------------------------
start:
	lea	secstart(pc),a0
	lea	secstart(pc),a0		;steht hier 2 mal???
	lea	$dff000,a5
	move.w	#$7fff,$9a(a5)
	move.l	#interruptroutine,$6c.w
	move.w	#$c020,$9a(a5)

;----------------------------------------------------------------------------
;	memory geschichte...
;----------------------------------------------------------------------------
	lea	$70000,a0
	move.l	(a0)+,minlocmem		;null
	move.l	(a0)+,maxlocmem		;max chip mem
	move.l	(a0)+,allocmem		;allocatet mem
	move.l	(a0),availmem		;available mem
	move.w	#$0020,$dff1dc

;----------------------------------------------------------------------------
;	clear memory
;----------------------------------------------------------------------------
	lea	$2200.w,a0		;diese routine ende! naja nach meinem
	lea	$80000,a1		;optimize ist's kuerzer ;=)

.loop:
	clr.l	(a0)+
	cmp.l	a0,a1
	bne.b	.loop

;----------------------------------------------------------------------------
;	trap
;----------------------------------------------------------------------------
	move.l	#traproutine,$80.w	;$a00 auf trap setzten
	trap	#0

	lea	$800.w,sp

	bsr.w	initialisation		;nothing only rts

	lea	traphere(pc),a0		;initzialisierung dma/irq und cache
	jsr	(a0)
	move.w	d6,cachestatusbuffer

	moveq	#5,d0
	trap	#13

	bsr.w	firstloader		;von oben bis hier nix besonderes!

	move.l	#font,fontaddressbuffer

;----------------------------------------------------------------------------
;	memorystructure
;	address=$ff,$00,$00,$00,$00,$00,address
;----------------------------------------------------------------------------
	bsr.w	memoryhandling		;memory handling

	lea	markerminusbuffer(pc),a0
	move.b	#$ff,(a0)
	move.l	#$00002400,diskdmaaddressbuffer
	move.l	loaderstruct(pc),a1
	move.l	allocmembuffer(pc),4(a1)

	bsr.b	main			;main

	move.l	allocmemaddedbuffer(pc),a0
	addq.w	#2,a0
	move.l	6(a0),a0
	move.l	a0,loaderstruct
	add.w	#$0790,a0
	move.l	a0,lbl000f70
	add.w	#$01e4,a0
	move.l	a0,startjumpaddressbuffer
	move.l	#traphere,traphereaddressbuffer
	lea	start(pc),a0
	lea	checkall(pc),a1
	lea	checksuccess(pc),a2
	move.l	loaderstruct(pc),a3
	move.l	lbl000f70(pc),a4
	move.l	allocmembuffer(pc),a5
	move.l	#markerminusbuffer,d0
	move.l	traphereaddressbuffer(pc),d1
	move.l	fontaddressbuffer(pc),d2
	move.l	#diskdmaaddressbuffer,d3
	move.l	#copperlist1,d4
	move.l	allocmemaddedbuffer(pc),d5
	move.l	startjumpaddressbuffer(pc),a6
	move.w	cachestatusbuffer(pc),d6
	jmp	(a6)

;----------------------------------------------------------------------------
;	main routine
;----------------------------------------------------------------------------
main:
	move.l	loaderstruct(pc),a0	;file one
	move.b	#1,1(a0)		;trackstart
	move.b	#1,2(a0)		;headstart
	move.w	#1,controllbuffer

	moveq	#0,d0
	bsr.b	checkall		;clist/check/init

	tst.w	controllbuffer
	bpl.b	.exit

	bsr.w	memresetter

;----------------------------------------

	move.l	loaderstruct(pc),a0	;file two
	move.b	#79,1(a0)		;trackstart
	move.b	#1,2(a0)		;headstart
	move.w	#1,controllbuffer

	moveq	#0,d0
	bsr.b	checkall		;clist/check/init

	tst.w	controllbuffer
	bpl.b	.exit

	bsr.w	memresetter

;----------------------------------------

	move.l	loaderstruct(pc),a0	;file three
	move.b	#79,1(a0)		;trackstart
	clr.b	2(a0)			;headstart
	clr.w	controllbuffer

	moveq	#0,d0
	bsr.b	checkall		;clist/check/init

.exit:
	clr.w	controllbuffer
	rts

;----------------------------------------------------------------------------
;	trap
;----------------------------------------------------------------------------
traproutine:
	movem.l	(sp)+,d0-d2
	lea	$a00.w,sp
	movem.l	d0-d2,-(sp)
	rte

;----------------------------------------------------------------------------
;	loader
;----------------------------------------------------------------------------
firstloader:
	bsr.w	idgetter		;hole idword!
	bsr.w	ciainit			;normale cia inits!
	bsr.w	drivespresent		;na wieviele diskdrives hamwa?
	bra.w	idreader		;step zyl 2 und check sync und id

;----------------------------------------------------------------------------
;	clist/check/init
;----------------------------------------------------------------------------
checkall:
	lea	copperlist1(pc),a0
	move.l	a0,a1			;copy copperlist address
	lea	$dff000,a5
	move.w	#$05f0,$96(a5)

	cmp.w	#$ffff,d0
	bne.b	checksuccess

	lea	text5(pc),a1
	bsr.w	errortyper
	bsr.w	mouseloop
	bra.w	oldcopperlist

;----------------------------------------------------------------------------
;	checking was successfull
;----------------------------------------------------------------------------
checksuccess:
	move.l	a1,copperlistbuffer
	move.l	a0,d7
	beq.b	.nocopper

	move.l	a0,$80(a5)
	clr.w	$88(a5)

.nocopper:
	move.b	d0,lbb000fb8
	move.w	d0,d2
	move.l	loaderstruct(pc),a0
	asl.w	#4,d0
	move.w	d0,d1
	move.l	8(a0,d1.w),d0		;track length $1a00
	move.b	0(a0,d1.w),disknumberbuffer
	move.b	1(a0,d1.w),tracknumberbuffer
	move.b	2(a0,d1.w),headnumberbuffer
	move.l	4(a0,d1.w),filelengthbuffer

	move.l	filelengthbuffer(pc),-(sp)
	move.l	8(a0,d1.w),tracklengthstruct

	bsr.w	idgetter
	bsr.b	haufenmuellroutine

	move.l	(sp)+,a0
	cmp.l	#$5450574d,(a0)		;"TPWM" kontrolle ob richtiges teil.
	bne.b	.nope

	move.l	loaderstruct(pc),a1
	moveq	#0,d0
	move.b	lbb000fb8(pc),d0
	asl.w	#4,d0
	move.b	3(a1,d0.w),d0
	and.w	#255,d0
	asl.w	#2,d0
	move.l	lbl000f70(pc),a1
	move.l	0(a1,d0.w),a5
	move.l	a0,a6

	bsr.w	lbc000f36
	bra.b	newtrap

.nope:
	bsr.w	maskdrive

;----------------------------------------------------------------------------
;	trap
;----------------------------------------------------------------------------
newtrap:
	moveq	#5,d0
	trap	#13
	rts

;----------------------------------------------------------------------------
;	memory comparison
;----------------------------------------------------------------------------
haufenmuellroutine:
	movem.l	d0-d7/a0-a6,-(sp)
	moveq	#0,d2
	moveq	#0,d3
	moveq	#0,d4
	move.b	disknumberbuffer(pc),d2
	move.b	tracknumberbuffer(pc),d3
	move.b	headnumberbuffer(pc),d4
	move.l	tracklengthstruct(pc),d5
	divu	#$1a00,d5

.loop:
	move.l	allocmemaddedbuffer(pc),a0
	move.w	(a0)+,d0
	subq.w	#1,d0

.memaddloop:
	tst.b	0(a0)			;1st byte minus? je nach anzahl
	bmi.b	.loopend		;des freien memorys

	cmp.b	1(a0),d2
	bne.b	.loopend

	cmp.b	2(a0),d3
	bne.b	.loopend

	cmp.b	3(a0),d4
	bne.b	.loopend

	subq.w	#1,d5
	beq.b	.decrementmemory

	eor.b	#1,d4
	bne.b	.loop

	addq.w	#1,d3
	bra.b	.loop

.decrementmemory:
	movem.l	(sp)+,d0-d7/a0-a6
	bra.b	.continue

.loopend:
	add.w	#10,a0
	dbra	d0,.memaddloop

;----------------------------------------

	movem.l	(sp)+,d0-d7/a0-a6
	bsr.w	ciainit

	move.b	disknumberbuffer(pc),d0
	lea	idnumberdrivesbuffer(pc),a0
	moveq	#3,d1

.wherediskforid:
	cmp.b	0(a0,d1.w),d0
	beq.b	.updatedrivewithid
	dbra	d1,.wherediskforid

;----------------------------------------

	lea	text2(pc),a1
	bsr.w	errortyper
	bsr.w	motorsave
	bsr.w	oldcopperlist
	bra.b	.aehmsumthingwrong

;----------------------------------------

.updatedrivewithid:
	move.w	d1,drivesetupbuffer

.aehmsumthingwrong:
	moveq	#6,d0
	trap	#13

	bsr.w	drivesetup

	move.b	tracknumberbuffer(pc),d0
	bsr.w	stepdirection

	move.b	headnumberbuffer(pc),d0
	move.w	drivesetupbuffer(pc),d1
	lea	currentheadbuffer(pc),a0
	move.b	d0,0(a0,d1.w)

	bsr.w	changeheadside
	bsr.w	diskfuckupremoved
	bsr.w	motoron
	bsr.w	lbc00043a
	bsr.w	driveinit

	moveq	#7,d0
	trap	#13

;----------------------------------------

.continue:
	movem.l	d0-d7/a0-a6,-(sp)
	move.l	loaderstruct(pc),a0
	moveq	#0,d0
	move.b	lbb000fb8(pc),d0
	lsl.w	#4,d0
	add.w	d0,a0
	move.l	4(a0),a2

	moveq	#0,d0
	moveq	#0,d1
	moveq	#0,d2
	move.b	disknumberbuffer(pc),d0
	move.b	1(a0),d1
	move.b	2(a0),d2
	move.l	8(a0),d4

.mainloop:
	move.l	allocmemaddedbuffer(pc),a1
	move.w	(a1)+,d3
	subq.w	#1,d3
	lea	markerminusbuffer(pc),a4

.memadderloop:
	tst.b	0(a1)
	bmi.b	.memadder

	cmp.b	1(a1),d0
	bne.b	.memadder

	cmp.b	2(a1),d1
	bne.b	.memadder

	cmp.b	3(a1),d2
	bne.b	.memadder

	moveq	#0,d3
	move.b	lbb000fb8(pc),d3
	move.b	0(a4,d3.w),4(a1)
	move.b	d3,5(a1)
	move.l	6(a1),a3
	move.w	#$067f,d3

.clear:
	move.l	(a3)+,(a2)+
	dbra	d3,.clear

	sub.l	#lbw001000,d4
	bmi.b	.exit
	beq.b	.exit

	eor.b	#1,d2
	bne.b	.mainloop

	addq.w	#1,d1
	bra.b	.mainloop

.memadder:
	add.w	#10,a1
	dbra	d3,.memadderloop
	bra.b	.mainloop

.exit:
	movem.l	(sp)+,d0-d7/a0-a6
	rts

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc00043a:
	moveq	#0,d0
	lea	currenttrackbuffer(pc),a0
	move.b	0(a0,d5.w),d0
	move.w	d0,trackstodobuffer
	lea	currentheadbuffer(pc),a0
	move.b	0(a0,d5.w),headbackupbuffer
	move.b	#3,replacediskbuffer

lbc00045e:
	clr.b	trackrereadbuffer

	movem.l	d0-d7/a0-a6,-(sp)
	move.l	allocmemaddedbuffer(pc),a0
	move.w	(a0)+,d0
	subq.w	#1,d0
	moveq	#0,d2
	moveq	#0,d3
	move.w	trackstodobuffer(pc),d1
	move.b	headbackupbuffer(pc),d2
	move.b	disknumberbuffer(pc),d3
	move.b	lbb000fb8(pc),d5
	sub.l	a1,a1

lbc000490:
	tst.b	0(a0)
	bmi.b	lbc0004b8

	cmp.b	2(a0),d1
	bne.b	lbc0004b8

	cmp.b	3(a0),d2
	bne.b	lbc0004b8

	cmp.b	1(a0),d3
	bne.b	lbc0004b8

	movem.l	(sp)+,d0-d7/a0-a6
	bra.w	lbc00067e

lbc0004b8:
	add.w	#10,a0
	dbra	d0,lbc000490

;----------------------------------------

	move.l	allocmemaddedbuffer(pc),a0
	move.w	(a0)+,d0
	subq.w	#1,d0
lbc0004ca:
	tst.b	0(a0)
	bmi.b	lbc00052a

	add.w	#10,a0
	dbra	d0,lbc0004ca

;----------------------------------------

	move.l	allocmemaddedbuffer(pc),a0
	move.w	(a0)+,d0
	subq.w	#1,d0
	moveq	#0,d2
	moveq	#0,d3
	move.w	trackstodobuffer(pc),d1
	move.b	headbackupbuffer(pc),d2
	move.b	disknumberbuffer(pc),d3
	move.b	lbb000fb8(pc),d5
	moveq	#-1,d4

lbc000502:
	tst.b	0(a0)
	bmi.b	lbc00052a

	cmp.b	4(a0),d4
	bls.b	lbc000520

	cmp.b	5(a0),d5
	beq.b	lbc000520

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

lbc000520:
	add.w	#10,a0
	dbra	d0,lbc000502

;----------------------------------------

	move.l	a1,a0

lbc00052a:
	move.l	6(a0),filelengthbuffer
	move.b	#1,0(a0)
	move.b	d3,1(a0)
	move.b	d1,2(a0)
	move.b	d2,3(a0)
	move.b	#255,4(a0)
	move.b	d5,5(a0)
	movem.l	(sp)+,d0-d7/a0-a6

;----------------------------------------------------------------------------
;	loader?
;----------------------------------------------------------------------------
loader:
	lea	$dff000,a5
	move.w	#$1002,$9a(a5)
	move.w	#$8210,$96(a5)
	lea	currentheadbuffer(pc),a0
	move.b	headbackupbuffer(pc),0(a0,d5.w)
	bsr.w	changeheadside

	move.w	#$7700,$9e(a5)
	clr.w	$24(a5)
	move.w	#$4489,$7e(a5)
	move.w	#$9500,$9e(a5)
	move.l	diskdmaaddressbuffer(pc),$20(a5)
	move.w	#$9a20,$24(a5)
	move.w	#$9a20,$24(a5)
	move.w	#$03e7,d0

lbc0005a0:
	bset	#0,$bfee01

lbc0005a8:
	btst	#1,$1f(a5)
	bne.b	lbc0005d4

	btst	#0,$bfed01
	beq.b	lbc0005a8

	subq.w	#1,d0
	bpl.b	lbc0005a0

lbc0005be:
	tst.w	controllbuffer
	beq.w	replacedisk

	move.w	#$ffff,controllbuffer
	bra.w	lbc00067e

lbc0005d4:
	move.w	#$1002,$9c(a5)
	move.w	#$4000,$24(a5)

	move.l	filelengthbuffer(pc),a3
	move.l	diskdmaaddressbuffer(pc),a1
	lea	-2(a1),a1

lbc0005ec:
	addq.l	#2,a1
	cmp.w	#$4489,(a1)
	beq.b	lbc0005ec

	move.w	(a1)+,d2		;idword
	move.w	(a1)+,d0
	and.w	#$5555,d0
	asl.w	#1,d0
	move.w	(a1)+,d1
	and.w	#$5555,d1
	or.w	d1,d0			;tracknumber

	lea	currenttrackbuffer(pc),a0
	cmp.b	0(a0,d5.w),d0
	beq.b	lbc000626

	bsr.w	tracknull
	bsr.w	diskfuckupremoved
	bsr.w	ciainit
	bsr.w	motoron
	bsr.w	changeheadside
	bra.b	lbc00062c

lbc000626:
	cmp.w	trackstodobuffer(pc),d0
	beq.b	lbc000638

lbc00062c:
	move.w	trackstodobuffer(pc),d0
	bsr.w	stepdirection
	bra.w	loader

lbc000638:
	cmp.w	idwordbuffer(pc),d2
	bne.w	diskchange

	move.w	#1663,d7		;1663 longs ?
	moveq	#0,d3

lbc000648:
	move.l	(a1)+,d0
	and.l	#$55555555,d0
	asl.l	#1,d0
	move.l	(a1)+,d1
	and.l	#$55555555,d1
	or.l	d1,d0
	add.l	d0,d3
	move.l	d0,(a3)+		;copy data to mem
	dbra	d7,lbc000648

	move.l	(a1)+,d0
	and.l	#$55555555,d0
	asl.l	#1,d0
	move.l	(a1)+,d1
	and.l	#$55555555,d1
	or.l	d1,d0
	cmp.l	d0,d3
	bne.b	rereadtrack

lbc00067e:
	sub.l	#lbw001000,tracklengthstruct
	beq.b	lbc0006ba
	bmi.b	lbc0006ba

	add.l	#lbw001000,filelengthbuffer
	lea	currentheadbuffer(pc),a0
	eor.b	#1,headbackupbuffer
	eor.b	#1,0(a0,d5.w)
	bne.w	lbc00045e

	bsr.w	stepinside

	addq.w	#1,trackstodobuffer
	bra.w	lbc00045e

lbc0006ba:
	rts

;----------------------------------------------------------------------------
;	re-read track
;----------------------------------------------------------------------------
rereadtrack:
	addq.b	#1,trackrereadbuffer
	cmp.b	#3,trackrereadbuffer
	bne.w	loader

	subq.b	#1,replacediskbuffer
	bmi.w	lbc0005be

	lea	currenttrackbuffer(pc),a0
	move.b	0(a0,d5.w),-(sp)

	bsr.w	tracknull
	bsr.w	diskfuckupremoved
	bsr.w	motoron
	bsr.w	changeheadside

	move.b	(sp)+,d0
	bsr.w	stepdirection

	clr.b	trackrereadbuffer
	bra.w	loader

;----------------------------------------------------------------------------
;	somethings wrong with the disk. replace it.
;----------------------------------------------------------------------------
replacedisk:
	move.w	#$4000,$24(a5)
	btst	#2,$bfe001
	beq.b	driveready

	bsr.w	ciainit

	lea	text1(pc),a1
	bsr.w	errortyper
	bsr.w	mouseloop
	bsr.w	oldcopperlist
	bsr.w	drivesetup
	bsr.w	tracknull
	bsr.w	idreader
	bsr.w	drivesetup
	bsr.w	motoron
	bne.b	driveready
	bsr.w	changeheadside

	move.w	trackstodobuffer(pc),d0
	bsr.w	stepdirection

	move.b	#3,replacediskbuffer
	clr.b	trackrereadbuffer
	bra.w	loader

;----------------------------------------------------------------------------
;	disk changed
;----------------------------------------------------------------------------
diskchange:
	move.w	#$4000,$24(a5)
	btst	#2,$bfe001
	beq.b	driveready
	bra.b	information

;----------------------------------------------------------------------------
;	check if drive was ready
;----------------------------------------------------------------------------
driveready:
	btst	#5,$bfe001
	beq.w	rereadtrack

;----------------------------------------------------------------------------
;	information
;----------------------------------------------------------------------------
information:
	bsr.w	driveinit
	bsr.w	delay
	bsr.w	drivesetup

	lea	text2(pc),a1
	bsr.w	errortyper
	bsr.w	motorsave
	bsr.w	oldcopperlist
	bsr.w	delay
	bsr.w	drivesetup
	bsr.w	diskfuckupremoved
	bsr.w	motoron
	bne.b	driveready
	bsr.w	delay
	bra.w	loader

;----------------------------------------------------------------------------
;	idgetter
;----------------------------------------------------------------------------
idgetter:
	movem.l	d0/a0,-(sp)
	moveq	#0,d0
	move.b	disknumberbuffer(pc),d0
	subq.w	#1,d0
	lea	idtable(pc),a0
	add.w	d0,d0
	move.w	0(a0,d0.w),idwordbuffer
	movem.l	(sp)+,d0/a0
	rts

;----------------------------------------------------------------------------
;	select which direction we want to step
;----------------------------------------------------------------------------
stepdirection:
	lea	currenttrackbuffer(pc),a0
	cmp.b	0(a0,d5.w),d0
	beq.b	.exit
	blt.b	.otherside

	bsr.b	stepinside
	bra.b	stepdirection

.otherside:
	bsr.b	stepoutside
	bra.b	stepdirection

.exit:
	rts

;----------------------------------------------------------------------------
;	track null
;----------------------------------------------------------------------------
tracknull:
	bsr.w	driveinit
	bsr.w	drivesetup

	move.b	drivestatusbuffer(pc),d0
	bset	#1,d0
	move.b	d0,drivestatusbuffer

	bsr.b	ciairqreg

.loop:
	btst	#4,$bfe001
	beq.b	.nullreached

	bsr.b	stephead
	bsr.b	ciairqreg
	bra.b	.loop

.nullreached:
	lea	currenttrackbuffer(pc),a0
	clr.b	0(a0,d5.w)
	rts

;----------------------------------------------------------------------------
;	step head
;----------------------------------------------------------------------------
stephead:
	move.b	drivestatusbuffer(pc),d7
	bclr	#0,d7
	move.b	d7,$bfd100
	nop
	move.b	drivestatusbuffer(pc),$bfd100
	bra.b	ciairqreg

;----------------------------------------------------------------------------
;	change the direction to step
;----------------------------------------------------------------------------
stepoutside:
	move.b	drivestatusbuffer(pc),d7
	bset	#1,d7
	move.b	d7,drivestatusbuffer
	lea	currenttrackbuffer(pc),a0
	subq.b	#1,0(a0,d5.w)
	bra.b	stephead

;----------------------------------------------------------------------------
;	change the direction to step
;----------------------------------------------------------------------------
stepinside:
	move.b	drivestatusbuffer(pc),d7
	bclr	#1,d7
	move.b	d7,drivestatusbuffer
	lea	currenttrackbuffer(pc),a0
	addq.b	#1,0(a0,d5.w)
	bra.b	stephead

;----------------------------------------------------------------------------
;	irq of the damn cia
;----------------------------------------------------------------------------
ciairqreg:
	bset	#0,$bfee01

.ready:
	btst	#0,$bfed01
	beq.b	.ready
	rts

;----------------------------------------------------------------------------
;	disk removed whil he wanted to reaf
;----------------------------------------------------------------------------
diskfuckupremoved:
	bsr.w	drivesetup

	btst	#2,$bfe001
	bne.b	.allok

	lea	text2(pc),a1

	bsr.w	errortyper
	bsr.w	motorsave
	bsr.w	oldcopperlist

.allok:
	bra.w	drivesetup

;----------------------------------------------------------------------------
;	turn motor on
;----------------------------------------------------------------------------
motoron:
	move.b	#%01111001,$bfd100
	bclr	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	move.b	#$8c,$bfe401
	move.b	#$8a,$bfe501
	moveq	#$28,d0

.loop:
	bset	#0,$bfee01

.wait:
	btst	#0,$bfed01
	beq.b	.goon

	subq.w	#1,d0
	beq.b	.motoron
	bra.b	.loop

.goon:
	btst	#5,$bfe001
	bne.b	.wait

	move.b	#$50,$bfe401
	move.b	#8,$bfe501
	moveq	#0,d0
	rts

.motoron:
	move.b	#$50,$bfe401
	move.b	#8,$bfe501
	moveq	#1,d0
	rts

;----------------------------------------------------------------------------
;	drive init motor off select etc...
;----------------------------------------------------------------------------
driveinit:
	move.b	#%11111001,$bfd100
	bset	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	or.b	#%11111000,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	rts

;----------------------------------------------------------------------------
;	select drive and turn shit on
;----------------------------------------------------------------------------
driveselect:
	moveq	#0,d5
	move.w	d0,drivesetupbuffer
	move.w	d0,d5
	addq.w	#3,d0
	move.b	drivestatusbuffer(pc),d7
	or.b	#%01111001,d7
	and.b	#%11111001,d7
	bclr	d0,d7
	move.b	d7,drivestatusbuffer
	move.b	d7,$bfd100
	rts

;----------------------------------------------------------------------------
;	drivesetup
;----------------------------------------------------------------------------
drivesetup:
	move.w	drivesetupbuffer(pc),d0
	bra.b	driveselect

;----------------------------------------------------------------------------
;	drive mask
;----------------------------------------------------------------------------
maskdrive:
	or.b	#%01111000,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	rts

;----------------------------------------------------------------------------
;	change heads
;----------------------------------------------------------------------------
changeheadside:
	lea	currentheadbuffer(pc),a0
	move.b	0(a0,d5.w),d0
	tst.b	d0
	beq.b	.upperhead

	bset	#2,drivestatusbuffer
	bra.b	.headchanged

.upperhead:
	bclr	#2,drivestatusbuffer

.headchanged:
	move.b	drivestatusbuffer(pc),$bfd100
	move.b	#$8c,$bfe401
	move.b	#$8a,$bfe501
	bset	#0,$bfee01

.ready:
	btst	#0,$bfed01
	beq.b	.ready

	bset	#0,$bfee01

.realyready:
	btst	#0,$bfed01
	beq.b	.realyready

	move.b	#$50,$bfe401
	move.b	#8,$bfe501
	rts

;----------------------------------------------------------------------------
;	cia init
;----------------------------------------------------------------------------
ciainit:
	lea	$dff000,a5
	move.b	$bfe201,d0
	and.b	#$c3,d0
	move.b	d0,$bfe201
	move.b	#%11111001,$bfd100
	move.b	#$ff,$bfd300
	move.b	$bfee01,d0
	and.b	#$c0,d0
	or.b	#8,d0
	move.b	d0,$bfee01
	move.b	#$7f,$bfed01
	move.b	#$50,$bfe401
	move.b	#8,$bfe501
	bra.w	ciairqreg

;----------------------------------------------------------------------------
;	motor save
;----------------------------------------------------------------------------
motorsave:
	move.w	drivesetupbuffer(pc),d0
	lea	currenttrackbuffer(pc),a0
	move.b	0(a0,d0.w),-(sp)
	lea	currentheadbuffer(pc),a0
	move.b	0(a0,d0.w),-(sp)
	bsr.w	idreader

	clr.l	lbl000fa0

lbc000a66:
	move.w	#3,drivesetupbuffer
	lea	lbl000fa0(pc),a0
	lea	driveavailflagbuffer(pc),a1

lbc000a76:
	move.w	drivesetupbuffer(pc),d5
	bset	#7,drivestatusbuffer
	tst.b	0(a0,d5.w)
	beq.b	lbc000a98

	bclr	#7,drivestatusbuffer
	move.b	#%01111001,$bfd100

lbc000a98:
	bsr.w	drivesetup

	tst.b	0(a1,d5.w)
	beq.b	lbc000ae8

	btst	#2,$bfe001
	bne.b	immediatehalt

	move.b	#%01111001,$bfd100
	bclr	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	st	0(a0,d5.w)
	bra.b	lbc000ae8

;----------------------------------------------------------------------------
;	halt all
;----------------------------------------------------------------------------
immediatehalt:
	move.b	#%11111001,$bfd100
	bset	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	clr.b	0(a0,d5.w)
lbc000ae8:
	subq.w	#1,drivesetupbuffer
	bpl.b	lbc000a76

	move.w	#3,drivesetupbuffer
lbc000af8:
	move.w	drivesetupbuffer(pc),d0
	lea	lbl000fa0(pc),a0
	tst.b	0(a0,d0.w)
	beq.b	lbc000b58

	bclr	#7,drivestatusbuffer
	move.b	#%01111001,$bfd100
	bsr.w	drivesetup

	lea	driveavailflagbuffer(pc),a0
	tst.b	0(a0,d5.w)
	beq.b	lbc000b58

	btst	#5,$bfe001
	bne.b	lbc000b58

	bsr.b	delay
	bsr.w	tracknull
	bsr.w	diskfuckupremoved
	bsr.w	trackload

	lea	idnumberdrivesbuffer(pc),a0
	tst.b	0(a0,d5.w)
	beq.b	lbc000b58

	lea	lbl000fa0(pc),a0
	clr.b	0(a0,d5.w)
lbc000b58:
	subq.w	#1,drivesetupbuffer
	bpl.b	lbc000af8

	bsr.b	delay

	moveq	#3,d0
	lea	idnumberdrivesbuffer(pc),a0
	move.b	disknumberbuffer(pc),d1
lbc000b6e:
	cmp.b	0(a0,d0.w),d1
	beq.b	lbc000b7e
	dbra	d0,lbc000b6e

	bra.w	lbc000a66

lbc000b7e:
	move.w	d0,-(sp)
	move.w	#3,drivesetupbuffer
lbc000b88:
	bsr.w	drivesetup
	bsr.w	driveinit

	subq.w	#1,drivesetupbuffer
	bpl.b	lbc000b88

	move.w	(sp)+,d5
	move.w	d5,drivesetupbuffer
	lea	currentheadbuffer(pc),a0
	move.b	(sp)+,0(a0,d5.w)
	move.b	(sp)+,d0
	bsr.w	stepdirection

	bsr.w	changeheadside
	bsr.w	oldcopperlist
	rts

;----------------------------------------------------------------------------
;	delay
;----------------------------------------------------------------------------
delay:
	moveq	#50,d0

.loop:
	bsr.w	ciairqreg
	dbra	d0,.loop
	rts

;----------------------------------------------------------------------------
;	read tracks with right id's
;----------------------------------------------------------------------------
idreader:
	clr.w	drivesetupbuffer
	clr.l	idnumberdrivesbuffer

.loop:
	bsr.b	trackload

	movem.l	d0-d7/a0-a6,-(sp)
	lea	idtable(pc),a2
	move.l	diskdmaaddressbuffer(pc),a3
	move.w	(a3),d3
	clr.l	(a3)

	moveq	#1,d2

.lastid:
	cmp.w	(a2)+,d3
	bne.b	.notfound

	lea	idnumberdrivesbuffer(pc),a0
	move.w	drivesetupbuffer(pc),d5
	move.b	d2,0(a0,d5.w)
	bra.b	.exit

.notfound:
	addq.w	#1,d2
	cmp.w	#4,d2
	bls.b	.lastid

.exit:
	movem.l	(sp)+,d0-d7/a0-a6
	addq.w	#1,drivesetupbuffer
	cmp.w	#4,drivesetupbuffer
	blt.b	.loop

	clr.w	drivesetupbuffer
	move.l	idnumberdrivesbuffer(pc),d0
	and.l	driveavailflagbuffer(pc),d0
	move.l	d0,idnumberdrivesbuffer
	rts

;----------------------------------------------------------------------------
;	load simple track!
;----------------------------------------------------------------------------
trackload:
	move.w	drivesetupbuffer(pc),d5
	lea	driveavailflagbuffer(pc),a0
	tst.b	0(a0,d5.w)
	beq.w	.exit			;ups keine laufwerke?

	bsr.w	drivesetup		;sette laufwerk neu, mann wie oft noch!

	lea	currenttrackbuffer(pc),a0
	tst.b	0(a0,d5.w)
	bne.b	.stepme

	moveq	#2,d0			;step nach zylinder 2
	bsr.w	stepdirection
	bra.b	.stepfar

.stepme:
	bsr.w	stepinside

	moveq	#5,d0

.loop:
	bsr.w	ciairqreg
	dbra	d0,.loop

	bsr.w	stepoutside

.stepfar:
	btst	#2,$bfe001
	beq.w	.exit

	move.b	#%01111001,$bfd100
	bclr	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100

	move.l	#$f0000,d0

.spindizzy:
	subq.l	#1,d0
	beq.w	.exit

	btst	#5,$bfe001
	bne.b	.spindizzy

	move.l	diskdmaaddressbuffer(pc),a0
	clr.l	(a0)+
	clr.l	(a0)+
	move.w	#$1002,$9a(a5)
	move.w	#$8210,$96(a5)

	bsr.w	changeheadside		;upperside

	move.w	#$7700,$9e(a5)
	clr.w	$24(a5)
	move.w	#$4489,$7e(a5)
	move.w	#$9500,$9e(a5)
	move.l	diskdmaaddressbuffer(pc),$20(a5)
	move.w	#$8010,$24(a5)
	move.w	#$8010,$24(a5)

	move.l	#$f0000,d0

.dmairqwait:
	subq.l	#1,d0
	beq.b	.exit

	btst	#1,$1f(a5)
	beq.b	.dmairqwait

	move.w	#$1002,$9c(a5)
	move.w	#$4000,$24(a5)
	move.l	diskdmaaddressbuffer(pc),a1
	lea	-2(a1),a1

.syncfind:
	addq.l	#2,a1
	cmp.w	#$4489,(a1)
	beq.b	.syncfind

	lea	idnumberdrivesbuffer(pc),a0
	move.w	(a1),d0
	cmp.w	idwordbuffer(pc),d0
	bne.b	.exit

	move.b	disknumberbuffer,0(a0,d5.w)

.exit:
	bra.w	driveinit

;----------------------------------------------------------------------------
;	check how many drives we've got
;----------------------------------------------------------------------------
drivespresent:
	moveq	#0,d0			;drive null
	bsr.w	driveselect		;waehle laufwerk
	bsr.w	tracknull

	move.w	#1,drivesetupbuffer	;step
	move.l	#$ff000000,driveavailflagbuffer

lbc000d4a:
	bsr.w	drivesetup		;setze step

	move.b	#%01111001,$bfd100
	bclr	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100
	move.b	#%11111001,$bfd100
	bset	#7,drivestatusbuffer
	move.b	drivestatusbuffer(pc),$bfd100

	bsr.w	maskdrive

	moveq	#0,d2
	moveq	#31,d6

lbc000d8a:
	bsr.w	drivesetup
	bsr.w	ciairqreg

	move.b	$bfe001,d1
	and.b	#%00100000,d1
	ror.b	#5,d1
	lsl.l	#1,d2
	or.b	d1,d2

	bsr.w	maskdrive
	bsr.w	ciairqreg
	dbra	d6,lbc000d8a

	tst.l	d2
	bne.b	lbc000dc8

	bsr.w	drivesetup
	bsr.w	tracknull		;sind wir auf null. mann der suckt!

	move.w	drivesetupbuffer(pc),d0
	lea	driveavailflagbuffer(pc),a0
	st	0(a0,d0.w)

lbc000dc8:
	addq.w	#1,drivesetupbuffer
	cmp.w	#4,drivesetupbuffer
	blt.w	lbc000d4a
	rts

;----------------------------------------------------------------------------
;	mouse
;----------------------------------------------------------------------------
mouseloop:
	btst	#6,$bfe001
	bne.b	mouseloop
	rts

;----------------------------------------------------------------------------
;	print error to screen
;----------------------------------------------------------------------------
errortyper:
	bsr.w	changedisknumber

	lea	$dff000,a5
	move.l	d1,-(sp)

	moveq	#7,d1
	move.w	#$0144,d0

.loop:
	clr.l	0(a5,d0.w)
	addq.w	#8,d0
	dbra	d1,.loop

	move.l	(sp)+,d1
	move.w	#$05f0,$96(a5)

.waitblit1:
	btst	#14,$2(a5)
	bne.b	.waitblit1

	move.l	diskdmaaddressbuffer(pc),$54(a5)
	clr.w	$74(a5)
	clr.w	$66(a5)
	move.w	#$0100,$40(a5)
	clr.w	$42(a5)
	move.w	#$8640,$96(a5)
	move.w	#$0294,$58(a5)

.waitblit2:
	btst	#14,$2(a5)
	bne.b	.waitblit2

	move.l	diskdmaaddressbuffer(pc),a0
	lea	$20(a0),a0
	bsr.b	texttyper

	lea	copperlist2(pc),a0
	move.w	diskdmaaddressbuffer(pc),$2(a0)
	move.w	planeaddressbuffer(pc),d0
	add.w	#$0020,d0
	move.w	d0,$6(a0)
	clr.w	$108(a5)

	bsr.b	verticalblank

	move.w	#$6281,$8e(a5)
	move.w	#$6ac1,$90(a5)
	move.w	#$0038,$92(a5)
	move.w	#$00d0,$94(a5)
	clr.w	$102(a5)
	move.l	a0,$80(a5)
	clr.w	$88(a5)
	move.w	#$0fdf,$182(a5)
	move.w	#$8380,$96(a5)
	rts

;----------------------------------------------------------------------------
;	vertical blank check
;----------------------------------------------------------------------------
verticalblank:
	move.l	4(a5),d0
	and.l	#$1ff00,d0
	cmp.l	#$2900,d0
	bne.b	verticalblank
	rts

;----------------------------------------------------------------------------
;	replace old copperlist
;----------------------------------------------------------------------------
oldcopperlist:
	bsr.b	verticalblank

	move.w	#$05f0,$96(a5)
	move.w	#$87f0,$96(a5)
	move.l	copperlistbuffer(pc),$80(a5)
	clr.w	$88(a5)
	rts

;----------------------------------------------------------------------------
;	text typer
;----------------------------------------------------------------------------
texttyper:
	moveq	#0,d0
	move.b	(a1)+,d0
	bmi.b	.end

	bsr.b	typefont

	lea	1(a0),a0
	bra.b	texttyper

.end:
	rts

;----------------------------------------------------------------------------
;	type font
;----------------------------------------------------------------------------
typefont:
	cmp.b	#$20,d0
	beq.b	.space

	move.l	fontaddressbuffer(pc),a2
	lea	0(a2,d0.w),a2
	move.b	(a2),(a0)
	move.b	$2c(a2),$28(a0)
	move.b	$58(a2),$50(a0)
	move.b	$84(a2),$78(a0)
	move.b	$b0(a2),$a0(a0)
	move.b	$dc(a2),$c8(a0)
	move.b	$108(a2),$f0(a0)
	move.b	$134(a2),$118(a0)
	rts

.space:
	clr.b	(a0)
	clr.b	$28(a0)
	clr.b	$50(a0)
	clr.b	$78(a0)
	clr.b	$a0(a0)
	clr.b	$c8(a0)
	clr.b	$f0(a0)
	clr.b	$118(a0)
	rts

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc000f36:
	move.l	(a6)+,d0
	move.l	(a6)+,d7
	move.l	a5,a4
	add.l	d7,a4
lbc000f3e:
	move.b	(a6)+,d0
	moveq	#7,d7
lbc000f42:
	add.b	d0,d0
	blo.b	lbc000f54

	move.b	(a6)+,(a5)+
lbc000f48:
	cmp.l	a5,a4
	bls.b	lbc000f52

	dbra	d7,lbc000f42

	bra.b	lbc000f3e

lbc000f52:
	rts

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc000f54:
	move.b	(a6)+,d1
	moveq	#0,d2
	move.b	d1,d2
	asl.w	#4,d2
	move.b	(a6)+,d2
	and.w	#15,d1
	addq.w	#2,d1
	move.l	a5,a3
	sub.w	d2,a3
lbc000f68:
	move.b	(a3)+,(a5)+
	dbra	d1,lbc000f68
	bra.b	lbc000f48

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbl000f70:
	dc.l	$00000000
loaderstruct:
	dc.l	$00001978,$01010100,$00000000,$00001a00,$00000000
	;	addr, disknr, track, head, filelen, tracklen, ?
filelengthbuffer:
	dc.l	$00000000
tracklengthstruct:
	dc.l	$00000000
copperlistbuffer:
	dc.l	$00000000
diskdmaaddressbuffer:
	dc.w	$0000
planeaddressbuffer:
	dc.w	$2400
driveavailflagbuffer:
	dc.l	$00000000
idnumberdrivesbuffer:
	dc.l	$00000000
lbl000fa0:
	dc.l	$00000000
currentheadbuffer:
	dc.l	$00000000
currenttrackbuffer:
	dc.l	$00000000
cachestatusbuffer:
	dc.w	$0000
idwordbuffer:
	dc.w	$0000
drivesetupbuffer:
	dc.w	$0000
trackrereadbuffer:
	dc.b	$00
replacediskbuffer:
	dc.b	$00
drivestatusbuffer:
	dc.b	$00
disknumberbuffer:
	dc.b	$01
headnumberbuffer:
	dc.b	$00
tracknumberbuffer:
	dc.b	$00
lbb000fb8:
	dc.b	$00
headbackupbuffer:
	dc.b	$00
maxlocmem:
	dc.l	$00000000
minlocmem:
	dc.l	$00000000
availmem:
	dc.l	$00000000
allocmem:
	dc.l	$00000000
allocmemaddedbuffer:
	dc.l	$00000000
allocmembuffer:
	dc.l	$00000000
fontaddressbuffer:
	dc.l	$00000000
traphereaddressbuffer:
	dc.l	$00000000
startjumpaddressbuffer:
	dc.l	$00000000
trackstodobuffer:
	dc.w	$0000
controllbuffer:
	dc.w	$0000
markerminusbuffer:
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000
lbw001000:
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000

;----------------------------------------------------------------------------
;	memory handling
;----------------------------------------------------------------------------
memoryhandling:
	tst.l	minlocmem
	beq.b	memorymanaging

	tst.l	allocmem
	bne.b	memorymanaging

	move.l	minlocmem(pc),allocmem
	move.l	maxlocmem(pc),availmem
	clr.l	minlocmem
	clr.l	maxlocmem
	bra.b	memorymanaging

;----------------------------------------------------------------------------
;	no memory
;----------------------------------------------------------------------------
memoryfail:
	lea	text4(pc),a1
	bsr.w	errortyper

.loop:
	nop
	bra.b	.loop

;----------------------------------------------------------------------------
;	memory managing
;----------------------------------------------------------------------------
memorymanaging:
	move.l	availmem(pc),d0
	sub.l	allocmem(pc),d0

	cmp.l	#$40000,d0		;$40000 avail
	blo.b	memoryfail		;nope

	move.l	allocmem(pc),d0
	move.l	d0,allocmembuffer

	add.l	#$17400,d0
	move.l	d0,allocmemaddedbuffer

	move.l	d0,a0
	sub.l	availmem(pc),d0
	neg.l	d0
	move.l	d0,d1
	move.l	maxlocmem(pc),d2
	sub.l	minlocmem(pc),d2
	add.l	d2,d0
	divu	#$1a0a,d0
	move.w	d0,(a0)+
	move.l	a0,a1
	move.w	d0,d3
	mulu	#10,d3
	add.l	d3,a1
	subq.w	#1,d0
lbc0010e8:
	move.b	#$ff,(a0)
	clr.b	1(a0)
	clr.b	2(a0)
	clr.b	3(a0)
	clr.b	4(a0)
	clr.b	5(a0)
	move.l	a1,6(a0)
	sub.l	#lbw001000,d1
	cmp.l	#lbw001000,d1
	bhs.b	lbc00111e

	move.l	minlocmem(pc),a1
	move.l	d2,d1
	bra.b	lbc001122

lbc00111e:
	add.w	#$1a00,a1
lbc001122:
	add.w	#10,a0
	dbra	d0,lbc0010e8
	rts

;----------------------------------------------------------------------------
;	reset memory structures
;----------------------------------------------------------------------------
memresetter:
	movem.l	d0/a0,-(sp)
	move.l	allocmemaddedbuffer(pc),a0
	move.w	(a0)+,d0
	subq.w	#1,d0

.loop:
	move.b	#$ff,(a0)
	clr.b	1(a0)
	clr.b	2(a0)
	clr.b	3(a0)
	clr.b	4(a0)
	clr.b	5(a0)
	add.w	#10,a0
	dbra	d0,.loop
	movem.l	(sp)+,d0/a0
	rts

;----------------------------------------------------------------------------
;	insert disk x, where x is one of the game disks
;----------------------------------------------------------------------------
changedisknumber:
	move.l	d0,-(sp)
	moveq	#0,d0
	move.b	disknumberbuffer(pc),d0
	add.b	#$1a,d0
	move.b	d0,text3
	move.l	(sp)+,d0
	rts

;----------------------------------------------------------------------------
;	id table? ich idiot hab's fuern text gehalten ;)
;----------------------------------------------------------------------------
idtable:
	dc.w	$2aa9,$2aa4,$2a92,$2a4a

;----------------------------------------------------------------------------
;	text ausgaben
;----------------------------------------------------------------------------
text1:
	dc.b	$12,$04,$11,$08,$0e,$14,$12,$20,$03,$08,$12,$0a,$04,$11
	dc.b	$11,$0e,$11,$24,$02,$0b,$08,$02,$0a,$20,$0c,$0e,$14,$12
	dc.b	$04,$20,$13,$0e,$20,$11,$04,$13,$11,$18,$24
	dc.b	$ff			;endsign

text2:
	dc.b	$20,$20,$20,$0f,$0b,$04,$00,$12,$04,$20,$08,$0d,$12,$04
	dc.b	$11,$13,$20,$03,$08,$12,$0a,$20

text3:
	dc.b	$1b,$20,$08,$0d,$20,$00,$0d,$18,$20,$03,$11,$08,$15,$04
	dc.b	$ff			;endsign

text4:
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$0e
	dc.b	$14,$13,$20,$0e,$05,$20,$0c,$04,$0c,$0e,$11,$18
	dc.b	$ff			;endsign

text5:
	dc.b	$12,$00,$15,$08,$0d,$06,$20,$05,$00,$08,$0b,$04,$03,$24
	dc.b	$20,$02,$0b,$08,$02,$0a,$20,$0c,$0e,$14,$12,$04,$20,$13
	dc.b	$0e,$20,$02,$0e,$0d,$13,$08,$0d,$14,$04,$24,$ff,$00,$96
	dc.b	$02,$00,$01,$80,$00,$00
	dc.b	$ff,$ff,$ff,$fe		;endsign, to much bullshit!

;----------------------------------------------------------------------------
;	copperlists
;----------------------------------------------------------------------------
copperlist1:
	dc.w	$00e0,$0000,$00e2,$0000,$0100,$1200,$0100,$0200,$0180
	dc.w	$0000,$ffff,$fffe

copperlist2:
	dc.w	$00e0,$0000,$00e2,$0000,$0100,$1200,$6007,$fffe,$0180
	dc.w	$0088,$6107,$fffe,$0180,$0044,$6a07,$fffe,$0100,$0200
	dc.w	$0180,$0088,$6b07,$fffe,$0180,$0000,$ffff,$fffe

	dc.b	'special coverdisk-version21.2.1994'
	dc.b	'rauser computer advertising gmbh'

;----------------------------------------------------------------------------
;	initialization
;----------------------------------------------------------------------------
initialisation:
	lea	$2200,a0
	move.w	#127,d0
.loop:
	clr.l	(a0)+
	dbra	d0,.loop

	lea	$2200,a0
	move.w	#$0003,$6(a0)
	move.b	#$05,$12(a0)
	move.w	#$00d9,$16(a0)
	move.w	#$0348,$18(a0)
	move.b	#$05,$1a(a0)
	move.w	#$0389,$1e(a0)
	move.w	#$0508,$20(a0)
	move.b	#$05,$22(a0)
	move.w	#$0429,$26(a0)
	move.w	#$03b8,$28(a0)
	move.b	#$05,$2a(a0)
	move.w	#$0429,$2e(a0)
	move.w	#$01e8,$30(a0)
	move.b	#$03,$3c(a0)
	clr.b	$3d(a0)
	move.b	#$04,$3f(a0)
	clr.b	$3e(a0)
	clr.w	$0c(a0)
	move.w	#$ffff,$56(a0)
	move.b	#$ff,$40(a0)
	move.b	#$01,$41(a0)
	move.w	#$0001,$42(a0)
	rts

;----------------------------------------------------------------------------
;	fucking lame font
;----------------------------------------------------------------------------
font:
	dc.w	$7cfc,$7cfc,$fefe,$7c66,$7e0e,$c6c0,$c6fc,$7cfc,$7cfc
	dc.w	$3e7e,$66c6,$c6c6,$c6fe,$3c18,$3c3c,$1c7e,$1c7e,$3c3c
	dc.w	$0000,$1800,$0003,$3000,$c6c6,$c6c6,$c0c0,$c666,$1806
	dc.w	$c6c0,$eec6,$c6c6,$c6c6,$6018,$66c6,$c6c6,$c606,$6638
	dc.w	$6666,$3c60,$3066,$6666,$0000,$3c18,$0006,$1800,$c6c6
	dc.w	$c0c6,$c0c0,$c066,$1806,$ccc0,$fec6,$c6c6,$c6c6,$6018
	dc.w	$66c6,$c66c,$c60c,$6618,$0606,$6c7c,$6006,$6666,$0000
	dc.w	$3c18,$000c,$0c00,$fefc,$c0c6,$f8f8,$ce7e,$1806,$f8c0
	dc.w	$d6c6,$c6fc,$c6fc,$3c18,$66c6,$d638,$7e18,$6618,$1c1c
	dc.w	$cc06,$7c0c,$3c3e,$0000,$1800,$7e18,$0600,$c6c6,$c0c6
	dc.w	$c0c0,$c666,$1866,$ccc0,$c6c6,$c6c0,$dec6,$0618,$66c6
	dc.w	$fe6c,$0630,$6618,$3006,$fe06,$6618,$6606,$0000,$1800
	dc.w	$0030,$0c00,$c6c6,$c6c6,$c0c0,$c666,$1866,$c6c0,$c6c6
	dc.w	$c6c0,$cec6,$0618,$667c,$eec6,$0660,$6618,$6066,$0c66
	dc.w	$6618,$660c,$1818,$0018,$0060,$1800,$c6fc,$7cfc,$fec0
	dc.w	$7e66,$7e3c,$c6fe,$c6c6,$7cc0,$7ec6,$7c18,$3c38,$c6c6
	dc.w	$fcfe,$3c7e,$7e3c,$1e3c,$3c18,$3c38,$1818,$1818,$00c0
	dc.w	$3000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0030,$0000,$0000,$0000,$0000

;----------------------------------------------------------------------------
;	trap here
;----------------------------------------------------------------------------
traphere:
	lea	trapcode(pc),a0
	move.l	a0,$80.w
	trap	#0
	rts

;----------------------------------------------------------------------------
;	trap code
;----------------------------------------------------------------------------
trapcode:
	move.w	#$7fff,d0
	move.w	d0,$dff096
	move.w	d0,$dff09a
	move.w	d0,$dff09c

	bsr.b	cachehandling
	move.w	d0,d6

	move.w	#$0000,sr
	moveq	#8,d0
	trap	#13
	rts

;----------------------------------------------------------------------------
;	cache handling
;----------------------------------------------------------------------------
cachehandling:
	move.l	$10.w,-(sp)
	lea	lbc00151e(pc),a2
	move.l	a2,$10.w
	move.l	sp,a1

	moveq	#0,d0
	lea	lbc00152c(pc),a0
	movec	d0,vbr

	moveq	#10,d0
	move.l	#irqbitbuffer,d1
	movec	d1,cacr
	nop
	movec	cacr,d1

	moveq	#20,d0
	lea	jumptable1(pc),a0
	btst	#9,d1
	beq.b	lbc00150a

	moveq	#30,d0
	lea	jumptable2(pc),a0
	bra.b	lbc001518

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc00150a:
	btst	#0,d1
	bne.b	lbc001518

	moveq	#$28,d0
	lea	jumptable3(pc),a0
	cpusha	bc

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc001518:
	moveq	#0,d1
	movec	d1,cacr

;----------------------------------------------------------------------------
;
;----------------------------------------------------------------------------
lbc00151e:
	move.l	a1,sp
	move.l	a0,$b4.w
	move.l	(sp)+,$10.w
	rts

	dc.w	0

lbc00152c:
	rte

	dc.w	0

;----------------------------------------------------------------------------
;	jumptable
;----------------------------------------------------------------------------
jumptable1:
	move.w	.jumplist1(pc,d0.w*2),d0
	jmp	jumptable1(pc,d0.w)

.jumplist1:
	dc.w	lbc00159c-jumptable1
	dc.w	lbc00161c-jumptable1
	dc.w	lbc00159c-jumptable1
	dc.w	lbc0015fe-jumptable1
	dc.w	lbc00159c-jumptable1
	dc.w	lbc0015b0-jumptable1
	dc.w	lbc00159c-jumptable1
	dc.w	lbc00159c-jumptable1
	dc.w	lbc001642-jumptable1
	dc.w	lbc001662-jumptable1
	dc.w	lbc001642-jumptable1
	dc.w	lbc00168e-jumptable1

;----------------------------------------------------------------------------
;	jumptable
;----------------------------------------------------------------------------
jumptable2:
	move.w	.jumplist2(pc,d0.w*2),d0
	jmp	jumptable2(pc,d0.w)

.jumplist2:
	dc.w	lbc0015e0-jumptable2
	dc.w	lbc00161c-jumptable2
	dc.w	lbc0015c2-jumptable2
	dc.w	lbc0015fe-jumptable2
	dc.w	lbc00159e-jumptable2
	dc.w	lbc0015b0-jumptable2
	dc.w	lbc00159c-jumptable2
	dc.w	lbc00159e-jumptable2
	dc.w	lbc00164a-jumptable2
	dc.w	lbc001662-jumptable2
	dc.w	lbc001674-jumptable2
	dc.w	lbc00168e-jumptable2

;----------------------------------------------------------------------------
;	jumptable
;----------------------------------------------------------------------------
jumptable3:
	move.w	.jumplist3(pc,d0.w*2),d0
	jmp	jumptable3(pc,d0.w)

.jumplist3:
	dc.w	lbc0015ee-jumptable3
	dc.w	lbc00162a-jumptable3
	dc.w	lbc0015d0-jumptable3
	dc.w	lbc00160c-jumptable3
	dc.w	lbc0015ac-jumptable3
	dc.w	lbc0015be-jumptable3
	dc.w	lbc00163a-jumptable3
	dc.w	lbc00163e-jumptable3
	dc.w	lbc001656-jumptable3
	dc.w	lbc00166a-jumptable3
	dc.w	lbc001680-jumptable3
	dc.w	lbc001690-jumptable3

lbc00159c:
	rte

lbc00159e:
	movec	cacr,d0
	or.w	#$800,d0
	movec	d0,cacr
	rte

lbc0015ac:
	cpusha	dc
	rte

lbc0015b0:
	movec	cacr,d0
	or.w	#8,d0
	movec	d0,cacr
	rte

lbc0015be:
	cpusha	bc
	rte

lbc0015c2:
	movec	cacr,d0
	and.w	#$feff,d0
	movec	d0,cacr
	rte

lbc0015d0:
	movec	cacr,d0
	bclr	#$1f,d0
	movec	d0,cacr
	cpusha	dc
	rte

lbc0015e0:
	movec	cacr,d0
	or.w	#$3100,d0
	movec	d0,cacr
	rte

lbc0015ee:
	movec	cacr,d0
	bset	#$1f,d0
	movec	d0,cacr
	cpusha	dc
	rte

lbc0015fe:
	movec	cacr,d0
	and.w	#$fffe,d0
	movec	d0,cacr
	rte

lbc00160c:
	movec	cacr,d0
	and.w	#$7fff,d0
	movec	d0,cacr
	cpusha	dc
	rte

lbc00161c:
	movec	cacr,d0
	or.w	#1,d0
	movec	d0,cacr
	rte

lbc00162a:
	movec	cacr,d0
	or.w	#$8000,d0
	movec	d0,cacr
	cpusha	ic
	rte

lbc00163a:
	cpusha	dc
	rte

lbc00163e:
	cpusha	ic
	rte

lbc001642:
	moveq	#1,d0
	movec	d0,cacr
	rte

lbc00164a:
	move.l	#$3111,d0
	movec	d0,cacr
	rte

lbc001656:
	move.l	#$80008000,d0
	movec	d0,cacr
	rte

lbc001662:
	moveq	#0,d0
	movec	d0,cacr
	rte

lbc00166a:
	moveq	#0,d0
	movec	d0,cacr
	cpusha	dc
	rte

lbc001674:
	move.l	#$2111,d0
	movec	d0,cacr
	rte

lbc001680:
	move.l	#$8000,d0
	movec	d0,cacr
	cpusha	dc
	rte

lbc00168e:
	rte

lbc001690:
	cpusha	bc
	rte

	or.b	#0,d0
	bset	d3,-(a3)
	sub.w	0(a0),a4
	and.l	#$203c0000,(a0)
	or.b	d0,d0
	movec	d0,cacr
	cpusha	dc
	rte

	rte

	cpusha	bc
	rte

;----------------------------------------------------------------------------
;	vermutlicher weise reloctable oder nur dreck!
;----------------------------------------------------------------------------
	dc.l	$000003ec,$00000098,$00000000,$0000116c,$000010be
	dc.l	$000010b2,$00001088,$00001082,$0000107c,$00001074
	dc.l	$00001068,$0000105e,$00000f74,$00000dd2,$00000dca
	dc.l	$00000d7a,$00000d74,$00000d60,$00000d5a,$00000d46
	dc.l	$00000d3c,$00000d24,$00000d1c,$00000c82,$00000c2c
	dc.l	$00000c26,$00000c20,$00000c1a,$00000c12,$00000c0a
	dc.l	$00000bd8,$00000bcc,$00000bc6,$00000b9c,$00000b92
	dc.l	$00000b84,$00000b5a,$00000b0c,$00000af4,$00000aea
	dc.l	$00000ad8,$00000aba,$00000a8c,$00000a7e,$00000a6a
	dc.l	$00000a62,$000009a0,$00000996,$00000978,$00000962
	dc.l	$0000094a,$00000938,$00000928,$000008b4,$00000864
	dc.l	$0000084c,$00000802,$000007cc,$000007c2,$000007ba
	dc.l	$00000754,$0000074e,$000006f8,$000006d0,$000006c6
	dc.l	$000006be,$000006b2,$0000069e,$00000692,$00000684
	dc.l	$0000063a,$000005cc,$000005c0,$0000056a,$0000052e
	dc.l	$000004fc,$000004f6,$000004f0,$000004ea,$000004dc
	dc.l	$000004c2,$0000048a,$00000710
