
Temp		= $100
Temp2		= $104
_MyWinRPort	= $108
_IntBase	= $10c
IT_Yps		= $110

	
boot:	dc.b	'DOS',0
	dc.l	0
	dc.l	$370

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

	move.l	4,a6			;alloc chipmem for bootblock
	move.l	#$480,d0
	move.l	#$10002,d1
	jsr	-198(a6)
	move.l	d0,Temp2

	move.l	#0,IT_Yps

	bsr.w	OpenWin
	lea	Info(pc),a0
	bsr.w	PrintLine
	add.l	#18,IT_Yps

	lea	Load(pc),a0
	bsr.w	PrintLine

	movem.l	(a7),d0-d7/a0-a6		;load simbug
	move.w	#2,$1c(a1)	;command
	move.l	#$c000,$24(a1)	;length
	move.l	Temp,$28(a1)	;load address
	move.l	#$800,$2c(a1)	;disk address
	jsr	-$1c8(a6)
	
	movem.l	(a7),d0-d7/a0-a6
	lea	ELoad(pc),a0
	tst.b	31(a1)
	bne.w	Fatal

	bsr.w	InitComms

	lea	Ins(pc),a0
	bsr.w	PrintLine
	
.loop	btst	#6,$bfe001
	bne.b	.loop

	lea	Load2(pc),a0
	bsr.w	PrintLine

	movem.l	(a7),d0-d7/a0-a6	;read new bootblock
	move.w	#2,$1c(a1)
	move.l	#$400,$24(a1)	;length
	move.l	Temp2,$28(a1)	;load address
	move.l	#$000,$2c(a1)	;disk address
	jsr	-$1c8(a6)

	lea	Exec(pc),a0
	bsr.w	PrintLine
			
	movem.l	(a7),d0-d7/a0-a6
	move.l	a0,Temp
	move.l	Temp2,a0
	pea	12(a0)
	move.l	Temp,a0
	rts
	
Fatal:	add.l	#18,IT_Yps
	bsr.w	PrintLine
.x	bra.s	.x

	*----------*

GetTopFast:
	move.l	4,a6
	move.l	322(a6),a0		; memlist
	
.loop	move.w	14(a0),d0
	and.w	#4,d0
	beq.b	.chip

.get	sub.l	#$c000,24(a0)	;upper
	sub.l	#$c000,28(a0)	;free
	move.l	24(a0),Temp
	rts
	
.chip	move.l	0(a0),a0
	tst.l	(a0)
	bne.b	.loop

	lea	GetTopChip(pc),a0
	sub.l	#$a000,2(a0)

	move.l	322(a6),a0		; memlist
	bra	.get
	
	*----------*
	
InitComms:
	bsr.b	Comms_ConfigMem
	
	bsr.w	GetTopChip
	move.l	Temp,a6
	move.l	a1,0(a6)		;chipmem display
	;move.l	#0,4(a6)		;chipmem backup
	;move.l	#0,264(a6)		;current VBR
	
	Lea	VecList(pc),a1
	Lea	0,a4
.setup	movem.w	(a1)+,d0/d1
	add.l	a6,d1
	move.l	d1,(a4,d0.w)
	tst.w	(a1)
	bpl.b	.setup

	rts	

	*--------------------------------------------*

Comms_ConfigMem:

	move.l	4,a6
	move.l	322(a6),a0		; memlist
	move.l	Temp,a1
	lea	$7b72(a1),a1
	move.l	a1,-(a7)
	
	move.w	#5,d3
			
.loop	move.l	20(a0),d1		; lower
	and.l	#$fffff000,d1
	move.l	d1,(a1)+
	move.l	24(a0),(a1)+	; upper

	sub.w	#1,d3
	beq.b	.max

	move.l	0(a0),a0
	tst.l	(a0)
	bne.b	.loop
	
.max	move.l	(a7)+,a1
	move.l	a1,a0
.chiploop
	tst.l	(a0)
	bne.b	.notchip
	
	;put chipmem first is memlist
	
	move.l	(a0),d0
	move.l	(a1),(a0)
	move.l	d0,(a1)
	lea	4(a0),a0
	lea	4(a1),a1
	move.l	(a0),d0
	move.l	(a1),(a0)
	move.l	d0,(a1)		
	rts	

.notchip
	lea	8(a0),a0
	add.w	#1,d3
	cmp.w	#5,d3
	bne.b	.chiploop

	rts
	
	*--------------------------------------------*

GetTopChip:	
	lea	$200000-$8000,a1
.loop	move.l	4,a6
	jsr	-534(a6)		;TypeOfMem
	tst.l	d0
	bne.b	.ok
	sub.l	#$5000,a1
	bra.b	.loop
.ok	rts
	
	*--------------------------------------------*
	
OpenWin:
	move.l	4,a6
	Lea	IntName(pc),A1
	jsr	-408(a6)		;OldOpenLibrary
	tst.l	d0
	Beq.b	.Err
	move.l	d0,_IntBase
	move.l	d0,a6

	lea	SimWindow(pc),a0
	move.l	a1,32(a0)
	jsr	-204(a6)		;OpenWindow
	move.l	d0,a0
	Move.l	$32(A0),_MyWinRPort

	move.l	Temp2,a1
	lea	$400(a1),a1
	moveq.l	#1,d0
	move.l	d0,d1
	move.l	d0,d2
	move.l	d0,d3
	jsr	-270(a6)		;SetPointer

.Err	moveq.l	#0,d0
	rts

	*-------------------------------*

PrintLine:
	lea	IText(pc),a1
	move.l	a0,12(a1)
	move.l	_MyWinRPort,a0
	move.l	#0,d0		
	move.l	IT_Yps,d1
	move.l	_IntBase,a6
	jsr	-216(a6)
	rts	
		
	*-------------------------------*

VecList:	dc.w	$08,24	;Bus Error
	dc.w	$0C,28	;Address Error
	dc.w	$10,32	;Illegal Instruction
	dc.w	$14,36	;Zero Divide
	dc.w	$18,40	;CHK/CHK2 Instruction
	dc.w	$1C,44	;cpTRAPcc, TRAPcc, TRAPV Instructions
	dc.w	$24,52	;Trace Exception
	dc.w	$28,56	;Line-A Emulator (1010)
	dc.w	$2C,60	;Line-F Emulator (1111) & Coprocessor Instructions
	dc.l	-1

	*-------------------------------*

IntName:
	dc.b	"intuition.library"
	even
SimWindow:
	dc.w	0,0		;window XY origin relative to TopLeft of screen
	dc.w	640,200		;window width and height
	dc.b	0,1		;detail and block pens
	dc.l	0		;IDCMP flags
	dc.l	$21040		;other window flags
	dc.l	0		;first gadget in gadget list
	dc.l	0		;custom CHECKMARK imagery
	dc.l	0		;window title
	dc.l	0		;custom screen pointer
	dc.l	0		;custom bitmap
	dc.w	5,5		;minimum width and height
	dc.w	-1,-1		;maximum width and height
	dc.w	1		;destination screen type

IText:	dc.b	1,0,1,0	;front and back text pens, drawmode and fill byte
	dc.w	12,8	;XY origin relative to container TopLeft
	dc.l	0	;font pointer or NULL for default
	dc.l	0	;pointer to text
	dc.l	0	;next IntuiText structure

Info:	dc.b	"Simbug Exception Handler v0.1 By Shagratt/LSD",0
Load:	dc.b	"Loading Simbug....",0
Ins:	dc.b	"Insert disk and press LMB",0
Load2:	dc.b	"Loading new bootblock..  ",0
Exec:	dc.b	"Executing new bootblock..",0
ELoad:	dc.b	"Error reading disk",0

	even
	ds.l	1000






