; *** Project-X Hard Disk Loader V1.3
; *** Written by Jean-François Fabre


	include	"/lib/libs.i"
	include	"/lib/macros.i"
	include	"/lib/refs.i"

_loader:
	move.l	D0,trainer
	move.l	D1,noosswap

	Mac_printf	"Project-X HD Loader V1.3"
	Mac_printf	"Programmed by Jean-François Fabre © 1997"

	tst.l	D0
	beq	skip$

;	NEWLINE
;	Mac_printf	"Trainer activated"
skip$
	JSRABS	LoadDisks
	JSRABS	TransfRoutines

	bsr	GetVersion

	; *** Tries to allocate memory for extension

	move.l	#MEMF_FAST,D1
	move.l	_SysBase,A6
	JSRLIB	AvailMem
	move.l	D0,D6

	cmp.l	#$200000,D6	; more than 2 mb of fastmem available
	bcs	lowmem$		; no

	move.l	#$100000,D0
	JSRABS	AllocExtMem
	move.l	D0,ExtBase
	bne	onemeg$

lowmem$
	move.l	#$80000,D0
	JSRABS	AllocExtMem
	move.l	D0,ExtBase
	beq	MemErr
	bra	cont$
onemeg$
	add.l	#$80000,D0
	move.l	D0,ExtBase2
	beq	MemErr	
cont$

	moveq.l	#0,D0
	move.l	#CACRF_CopyBack,D1
	JSRABS	Degrade

;	WAIT_LMB

	GO_SUPERVISOR
	SAVE_OSDATA	$80000

	; **** boot stuff and patch

	JSRGEN	InitTrackDisk
	lea	$70000,A3

	MOVE.L	A3,40(A1)		;2C: 234B0028
	MOVE.L	#$00001000,36(A1)	;30: 237C000010000024
	MOVE.L	#$0006E600,44(A1)	;38: 237C0006E600002C
	MOVE	#$0002,28(A1)		;40: 337C0002001C
	JSRGEN	TrackLoadFast
	
	move.l	#$400,$144(A3)
	move.l	ExtBase,$140(A3)
	move.l	ExtBase2,$13C(A3)
	move.l	#PatchLoader1,$138(A3)

	lea	$8A8(A3),A4
	cmp.l	#2,version
	bne	2$
	lea	$89A(A3),A4
2$
	move.w	#$4EF9,(A4)+
	move.l	#ReadSectors1,(A4)+


	move.w	#$4EF9,$E6(A3)
	move.l	#PatchLoader1,$E8(A3)
	
	lea	$80000,A7
	JMP	$70(A3)

PatchAfterATN:
	STORE_REGS

	nop
	nop
	nop

	move.l	ExtBase(pc),A0

	; *** different offsets depending on the version

	cmp.l	#1,version
	bne	nopkb_v1$

	; *** test quit key during game

	move.l	A0,A2
	add.l	#$1FEE6,A2
	cmp.w	#$1039,(A2)
	bne	kb1$

	move.w	#$4EB9,(A2)+
	move.l	#kbint,(A2)+

	; *** test quit key during menu
kb1$
	cmp.w	#$1039,$1D3E(A0)
	bne	nopkb_v1$

	move.w	#$4EB9,$1D3E(A0)
	move.l	#kbint,$1D40(A0)

nopkb_v1$
	cmp.l	#2,version
	bne	nopkb_v2$
	
	; *** test quit key during game

	move.l	A0,A2
	add.l	#$1FE90,A2
	cmp.w	#$1039,(A2)
	bne	kb2$

	move.w	#$4EB9,(A2)+
	move.l	#kbint,(A2)+

	; *** test quit key during menu

kb2$
	cmp.w	#$1039,$1CE8(A0)
	bne	nopkb_v2$

	move.w	#$4EB9,$1CE8(A0)
	move.l	#kbint,$1CEA(A0)

nopkb_v2$

	; *** common to both versions

	cmp.w	#$41F9,$408.W
	bne	noppr$

	move.w	#$4E75,$408.W
noppr$

	cmp.w	#$2E28,$1984.W
	bne	noplc$

	move.l	#$7E024E71,$1984.W

noplc$
	JSRGEN	ResetSprites
	JSRGEN	FlushCachesHard

	RESTORE_REGS
	movem.l	(A7)+,D2-D5/A2-A4
	tst.l	D0
	RTS



PatchLoader1:
	STORE_REGS

	move.l	ExtBase(pc),A0

	cmp.l	#2,version
	beq	v2$

	move.w	#$4EF9,$46B4(A0)
	move.l	#ReadSectors2,$46B6(A0)

	move.w	#$4EF9,$3CFE(A0)
	move.l	#PatchAfterATN,$3D00(A0)
	move.l	#$57CE,hiscore_offset
	bra	vcom$

v2$
	move.w	#$4EF9,$465E(A0)
	move.l	#ReadSectors2,$4660(A0)

	move.w	#$4EF9,$3CA8(A0)
	move.l	#PatchAfterATN,$3CAA(A0)

	move.l	#$5778,hiscore_offset

vcom$
	; *** read high scores

	move.l	noosswap(pc),D4
	bne	skip$

	move.l	hiscore_offset(pc),A1
	add.l	A0,A1
	lea	hiscname(pc),A0
	moveq.l	#0,D0
	move.l	#$A0,D1
	JSRGEN	ReadFileHD

skip$	
	RESTORE_REGS
	move.l	$7013C,D0
	jmp	(A0)

kbint:
	move.b	$BFEC01,D0
	move.l	D0,-(sp)
	not.b	D0
	ror.b	#1,D0
	cmp.b	#$5F,D0
	bne	noquit$
	JSRGEN	InGameExit
noquit$
	move.l	(sp)+,D0
	rts

ReadSectors1:
ReadSectors2:
	STORE_REGS

	cmp.b	#1,D3		; write?
	beq	write$

	tst.w	D1
	beq	exit$

	JSRGEN	ReadRobSectorsFast
exit$
	RESTORE_REGS
	moveq.l	#0,D0
	tst.l	D0
	rts

write$
	; *** read high scores

	move.l	ExtBase(pc),A1
	add.l	hiscore_offset(pc),A1
	cmp.l	A1,A0
	bne	exit$

	bsr	WriteScores

	bra	exit$

WriteScores:
	STORE_REGS
	tst.l	noosswap
	bne	skip$

	move.l	ExtBase(pc),A1
	add.l	hiscore_offset(pc),A1
	lea	hiscname(pc),A0
	moveq.l	#0,D0
	move.l	#$A0,D1
	JSRGEN	WriteFileHD
skip$
	RESTORE_REGS
	rts


GetVersion
	moveq.l	#0,D0
	JSRGEN	GetDiskPointer
	move.l	D0,A0
	move.l	$C(A0),D0

	cmp.l	#$2F09203C,D0
	bne	notv1$

	move.l	#1,version
	rts

notv1$
	cmp.l	#$33FC0020,D0
	bne	notv2$

	move.l	#2,version
	rts

notv2$
	Mac_printf	"** Unsupported Project-X version. Contact the author"
	JMPABS	CloseAll


MemErr:
	Mac_printf	"** Not enough memory to run Project-X"
	JMPABS	CloseAll

_user_pbuffer:
	dc.l	0
_general_pbuffer:
	dc.l	0
ExtBase:
	dc.l	0
ExtBase2:
	dc.l	0
trainer:
	dc.l	0
noosswap:
	dc.l	0
version:
	dc.l	0
hiscore_offset:
	dc.l	0
hiscname:
	dc.b	"px.high",0
	even

_UserPatchRoutines:
	nop
	nop
_EndUserPatchRoutines:

	HD_PARAMS	"ProjectX.disk","CON:20/20/350/200/Project-X HD Loader",STD_DISK_SIZE,4

