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

version=	"2.40"
*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=	$80000; max. 512K

	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	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

; 64'er 3/94
; 2000 REM JULIANISCH -> KALENDER
; 2005 J=INT((4*JD-1)/146097)
; 2010 JD=4*JD-1-146097*J
; 2020 T=INT(JD/4)
; 2030 JD=INT((4*T+3)/1461)
; 2040 T=4*T+3-1461*JD
; 2050 T=INT((T+4)/4)
; 2060 M=INT((5*T-3)/153)
; 2070 T=5*T-3-153*M
; 2080 T=INT((T+5)/5)
; 2090 J=100*J+JD
; 2100 IFM<10THENM=INT(M+3):GOTO2120
; 2110 M=INT(M-9):J=J+1
; 2120 RETURN
jd	equr	d2
j	equr	d3
cj	equr	d4
t	equr	d5
m	equr	d6
*c	set	146097; 189*773
date	add.l	#722391,jd; 1-Jan-78
	move.l	jd,j
	lsl.l	#2,j
	subq	#1,j
*	divu.l	#c,j
	divu	#773,j
	and.l	#$ffff,j
	divu	#189,j
	and.l	#$ffff,j

	move.l	j,cj
*	mulu.l	#c,cj
	mulu	#773,cj
	mulu	#189,cj
	lsl.l	#2,jd
	subq	#1,jd
	sub.l	cj,jd

	move.l	jd,t
	lsr.l	#2,t

c	set	1461
	move.l	t,jd
	lsl.l	#2,jd
	addq	#3,jd
	divu	#c,jd

	move.l	jd,cj
	mulu	#c,cj
	lsl.l	#2,t
	addq	#3,t
	sub.l	cj,t

	lsr.l	#2,t
	addq	#1,t

c	set	153
	move.l	t,m
	mulu	#5,m
	subq	#3,m
	divu	#c,m

	move.l	m,cj
	mulu	#c,cj
	mulu	#5,t
	subq	#3,t
	sub.l	cj,t

	divu	#5,t
	addq	#1,t

	mulu	#100,j
	add.l	jd,j

	cmp.b	#10,m
	bge.s	ge10
	addq	#3,m
	bra.s	lt10
ge10	sub.b	#9,m
	addq	#1,j
lt10	rts

DIGITS	macro
	move.b	d0,d1
	and.b	#$f,d1
	move.b	d1,\1
	lsr.b	#4,d0
	move.b	d0,\2
	endm

time	move.l	#vector,d1
	jsr	datestamp(a6)
	move.l	vector(pc),jd
	bsr	date
	move.b	j,d0	; years
	sub	#1980,d0; since 1980
	DIGITS	start_emu+$87,start_emu+$8f
	move.b	m,d0	; months
	DIGITS	start_emu+$77,start_emu+$7f
	move.b	t,d0	; days
	DIGITS	start_emu+$67,start_emu+$6f
	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
