; © Stefan Haubenthal 1992-94

*	include	prog:asm/include/wb.i
*	incdir	prog:asm/include
*	include	easystart.i

version=	"2.02"
allocabs=	-$0cc
freemem=	-$0d2
oldopenlibrary=	-$198
closelibrary=	-$19e
open=		-$01e
read=		-$02a
copymemquick=	-$276
mode_oldfile=	1005

rom=		$40000
romsize=	$30000
emu=		$70000

	move.l	4.w,a6
	move.l	#romsize,d0
	lea	rom,a1
	jsr	allocabs(a6)
	tst.l	d0
	beq	exit
	move.l	#emusize,d0
	lea	emu,a1
	jsr	allocabs(a6)
	tst.l	d0
	beq	exit
	lea	dosname(pc),a1
	jsr	oldopenlibrary(a6)
	move.l	d0,a6
	move.l	#romname,d1
	move.l	#mode_oldfile,d2
	jsr	open(a6)
	beq.s	exit2
	move.l	d0,d1
	move.l	#rom,d2
	move.l	#romsize,d3
	jsr	read(a6)
	bsr.s	patch
	move.l	4.w,a6
	lea	start_emu,a0
	lea	emu,a1
	move.l	#emusize,d0
	jsr	copymemquick(a6)
	lea	emu,a0
	move.l	#$4e714e71,$a0(a0)
	move.l	#$4e714e71,$b2(a0)
	move.l	#$4e714e71,$b6(a0)
	jmp	emu+4
; goodbye AmigaDOS!
exit2	move.l	a6,a1
	move.l	4.w,a6
	jsr	closelibrary(a6)
	move.l	#emusize,d0
	lea	emu,a1
	jsr	freemem(a6)
exit	moveq	#20,d0
	rts

patch	lea	reloc,a0
	sub.l	a1,a1
next_reloc	moveq	#0,d0
	cmp	#0,(a0)
	add	(a0)+,a1
	bne.s	not_packed
	move.b	(a0)+,d0
	move.b	(a0)+,d1
next_packed	add	d1,a1
not_packed	sub.b	#($fc0000-rom)>>16,(a1,d2.l)
	dbra	d0,next_packed
	cmp.l	#diff,a0
	blo.s	next_reloc
next_diff	move.l	(a0)+,a1
	move	(a0)+,d0
	move.b	d0,(a1,d2.l)
	cmp.l	#end_diff,a0
	blo.s	next_diff
	rts

	dc.b	"$VER: ST4Amiga V"
	dc.l	version
	dc.b	0
dosname	dc.b	"dos.library",0
romname	dc.b	"TOS.IMG",0

	data
; { delta_offset.w
; | 0.w number-1.b delta_offset.b }
reloc	include	reloc.asm
; { offset.l data.w }
diff	include	diff.asm
end_diff

	code
start_emu	include	emu.asm
emusize=	*-start_emu
