; © Stefan Haubenthal 1992-95
*	opt	o+
*	include	prog:asm/include/wb.i
*	incdir	prog:asm/include
*	include	easystart.i
	incdir	1.02
*	incdir	2.06

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

io=		$18000;  32K
emu=		$20000
temp=		$23100;  8 bytes
buffer=		$24000
scr=		$30000
scrsize=	$8000; ± 32K
dsk=		$38004
dsksize=	$4000;   16K
rom=		$40000
romsize=	$40000; 256K/192K

	move.l	4.w,a6
	IFD	allocabs
	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.s	exit
	ENDC
	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
	bsr.s	time
	move.l	4.w,a6
	lea	start_emu,a0
	lea	emu,a1
	move.l	#emusize,d0
	jsr	copymemquick(a6)
	jmp	emu+4
; goodbye AmigaDOS!
exit2	move.l	a6,a1
	move.l	4.w,a6
	jsr	closelibrary(a6)
	IFD	allocabs
	move.l	#emusize,d0
	lea	emu,a1
	jsr	freemem(a6)
	ENDC
exit	moveq	#20,d0
	rts

patch	lea	reloc,a0
	sub.l	a1,a1
	move.b	rom+5,d3
	sub.b	#rom>>16,d3
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	d3,(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

DIGITS	macro
	move.b	d0,d2
	and.b	#$f,d2
	move.b	d2,\1
	lsr.b	#4,d0
	move.b	d0,\2
	endm
time	move.l	#vector,d1
	jsr	datestamp(a6)
	move.l	vector+4(pc),d0
	divu	#60,d0	; hours
	DIGITS	start_emu+$57,start_emu+$5f
	swap	d0	; minutes
	DIGITS	start_emu+$47,start_emu+$4f
	move.l	vector+8(pc),d0
	divu	#50,d0	; seconds
	DIGITS	start_emu+$37,start_emu+$3f
	rts

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

	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
temp1	EQU	temp
temp2	EQU	temp1+2
temp3	EQU	temp2+2
temp4	EQU	temp3+2
start_emu	include	emu.asm
emusize=	*-emu
