*******************************************
* Bootblock to load "81MusicDisk1" loader *
*******************************************

SysBase		equ	4
Supervisor	equ	-30
AllocMem	equ	-198
AllocABS	equ	-204
CloseDevice	equ	-450
DoIO		equ	-456

a1save		equ	$7f400
LoadMemAddr	equ	$7f404

bbstart		dc.b	'DOS',0
		dc.l	$0
		dc.l	$00000370

		lea	bbmain(pc),a0		; Let's put the bootblock in
		move.l	#$7f000,a2		; a sensible place...
		move.l	#bbmainlen,d2		; ...on second thoughts,
bbrelocloop	move.b	(a0)+,(a2)+		; let's put it at $7f000
		dbf	d2,bbrelocloop
		jmp	$7f000

						; This next section runs from
						; $7f000, but should run at
						; any address (no abs addrs)
bbmain		move.l	a1,a1save		; We need to use this later!
		move.w	#$0080,$dff096		; Stop copper from mucking
						; with screen as we want to
		move.w	#0,$dff180		; set screen colour to black
		move.l	#$10000,a1		; Allocate from $10000-$7efff
		move.l	#$6f000,d0
		move.l	SysBase,a6
		jsr	AllocABS(a6)		; If alloc fails, reset Amiga
		tst.l	d0			; because it IS possible if
		beq	ChipFail		; nothing gets in the way!
		move.l	#$30000,d0		; Try to allocate more memory
		move.l	#0,d1			; (for loading tunes, etc):
		jsr	AllocMem(a6)		; This will be FAST (possibly
		tst.l	d0			; CHIP for machines with 1MB
		beq.s	LoadMemFail		; of the stuff - e.g. my main
		move.l	d0,LoadMemAddr		; 'workhorse' Amiga)
						; This means that the disk will
						; work with any Amiga with at
						; least 1Mb of memory. The
						; previous version needed FAST
						; mem, and so wouldn't work on
						; A500+ with even newer Agnus
						; (As seen in A3000).
		move.l	a1save,a1		; Let's load the loader
		move	#2,28(a1)
		move.l	#$40000,40(a1)		; address to load to
		move.l	#$07000,36(a1)		; length on disk
		move.l	#$00400,44(a1)		; position on disk
		jsr	DoIO(a6)
		jmp	$40000			; Now execute the menu/loader
						; (to decrunch it)

LoadMemFail	lea	FailCopPtrs(pc),a0
		lea	FailScrDat(pc),a1
		move.l	a1,d0
		move.w	d0,6(a0)
		swap	d0
		move.w	d0,2(a0)
		lea	FailCopperList(pc),a0
		move.l	a0,$dff080
		move.w	#$7fff,$dff09a
		move.w	#$7fff,$dff096
		move.w	#$8380,$dff096
HaltLoop	bra.s	HaltLoop

ChipFail	move.l	SysBase,a6		; Reset if CHIP mem alloc fails
		lea.l	MagicResetCode(pc),a5	; (routine from H/W Ref Man)
		jsr	Supervisor(a6)		; (second edition)

		CNOP	0,4
MagicResetCode	lea.l	2,a0
		RESET
		jmp	(a0)

FailCopperList	dc.w	$08e,$2c81,$090,$f4c1,$092,$0038,$094,$00d0
		dc.w	$100,$0200,$102,0,$108,0,$180,$008,$182,$fd0
		dc.w	$ac09,$fffe,$100,$1200
FailCopPtrs	dc.w	$0e0,0,$0e2,0
		dc.w	$b409,$fffe,$100,$0200
		dc.w	$ffff,$fffe

FailScrDat	incbin	'loadfail.raw'

						; Oh dear, empty space in the
						; bootblock!  Let's put some
						; junk in, to keep the bblock
						; snoopers amused!

spacefillertext	dc.b	"THIS BOOTBLOCK IS NOT A VIRUS - IT IS THE CODE USED "
		dc.b	"TO START THE MUSICDISK.     FIRST, I WOULD LIKE TO "
		dc.b	"INTRODUCE THE TEAM (IN A BOOTBLOCK?!?)     "
		dc.b	"WE'VE GOT AARDVARK ON BOTH KEYBOARDS (CODE & MUSIC), "
		dc.b	"POWERSLAVE ON THE SCREENS (GRAPHICS) AND ALSO ON "
		dc.b	"KEYBOARDS OF THE MUSICAL VARIETY, WITH THE MAD ONE "
		dc.b	"HELPING WITH MOST NON-CODE THINGS"
		dc.b	"        Coded by AARDVARK - 29/12/91         "
		dc.b	"81!81!81!81!81!81!81!"

fillersize	SET	1024-(*-bbstart)

		IFLT	fillersize
		FAIL		!!!	Bootblock to big for 1K space
		ELSE
		ds.b	fillersize
		ENDC

bbmainlen	equ	spacefillertext-bbmain