; *** Stunt Track Racer HD Loader V1.0
; *** Written by Keith Krellwitz (kkrellwi@nmu.edu)
; *** and Jean-Francois Fabre (jffabre@ensica.fr)


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

	HD_PARAMS	"str.d","CON:20/20/400/200/Stunt Track Racer HD Loader/AUTO",STD_DISK_SIZE,1

_loader:

	Mac_printf	"Stunt Track Racer HD Loader v1.0"
	Mac_printf	"Coded by Abaddon/JOTD ©1997"

	JSRABS	LoadDisks
	JSRABS	TransfRoutines

	bsr	ReadSaves

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

	GO_SUPERVISOR
	SAVE_OSDATA	$80000

	GETUSRADDR RDBoot
	move.l	d0,a0
	jmp		(a0)


ReadSaves:
	GETUSRADDR	dirname	; because now user reloc section is trashed
					; in absolute mode to avoid relocation errors
	move.l	D0,A0
	GETUSRADDR	dirbuff
	move.l	d0,A1
	moveq.l	#0,D0
	move.l	#$200,D1
	JSRGEN	ReadFileHD
	tst.l	D0
	beq	rs_quit
	Mac_printf	"** Could not read save file"
rs_quit:
	rts

_UserPatchRoutines:

RDBoot:	
	; because the original version has got a different boot code!!

	lea		boot(pc),a0
	lea		$5082,a1
	move.l	#$2b,d0	
relocate:
	move.l	(a0)+,(a1)+
	dbf		d0,relocate

	move.l	#$0,d0	
	move.l	#$0,d3	
	move.l	#$16,d1	
	move.l	#$4c,d2	
	lea		$9448,a0
	JSRGEN	ReadRobSectors

	PATCHUSRJSR	$50b2.W,patch
	lea		$51b8.W,a7
	lea		$9448,a3
	JSRGEN	FlushCachesHard
	jmp		$5082.W

patch:
	STORE_REGS
	PATCHGENJMP $99b8,ReadRobSectors
	RESTORE_REGS
reloc:
	move.b	(a0)+,(a1)+
	dbf		d0,reloc
	PATCHUSRJMP	$957a,jumper
	JSRGEN	FlushCachesHard
	rts
jumper:
	PATCHUSRJMP	$104.W,jumper2
	lea		$3d80,a7
	JSRGEN	FlushCachesHard
	jmp		(a0)

jumper2:
	PATCHUSRJSR	$f04e,KbInt
	PATCHUSRJMP $62e86,ReadSectors		;probably could eliminate this the only call beside
										;the load/save game read an empty track

	move.l	#$4E714E71,$59856			;Bypass insert formatted disk

	JSRGEN	ResetSprites
	JSRGEN	FlushCachesHard
	jmp	$E700
KbInt:
	move.b	#$0,0(a0,d0.w)
	cmp.b	#$59,d0				; f10	
	bne	noquit
	JSRGEN	InGameExit
noquit:
	rts

ReadSectors:
	movem.l	D1-A6,-(sp)
	cmp.b	#$16,D1
	bne.b	nodir
	cmp.b	#1,D2
	beq.b	dir
nodir:
	cmp.b	#$17,D1
	bcs.b	nodat
	cmp.b	#$35,D1
	bcc.b	nodat
	cmp.b	#1,D2
	beq.b	dat
nodat:
	JSRGEN	ReadRobSectors
exit:
	movem.l	(sp)+,D1-A6
	rts

dir:
	tst.b	D3
	beq.b	readdir
	
	; write dir info
	; but cache it too not to have to read it
	; from HD on a load during game
	; (I don't care for $200 bytes more in the loader)

	lea	dirbuff(pc),A1
transf:
	move.w	#$7F,D0
copy:
	move.l	(A0)+,(A1)+
	dbf	D0,copy

	moveq.l	#0,D0
	bra	exit

readdir:
	move.l	A0,A1
	lea	dirbuff(pc),A0
	bra	transf


dat:
	lea	savnum(pc),A2
	move.b	D1,D2
	and.b	#$F,D2
	add.l	#'a',D2	; index 1
	move.b	D2,(A2)+
	lsr.b	#4,D1
	add.l	#'0',D1	; index 2
	move.b	D1,(A2)+

	moveq.l	#0,D0
	move.l	#$200,D1
	move.l	A0,A1
	lea	savname(pc),A0	; file name
	
	tst.b	D3		; write?
	beq.b	readdat

	; write data to disk

	JSRGEN	WriteFileHD
	tst.l	D0
	bne.b	exit

	; write directory information to disk

	lea	dirname(pc),A0
	lea	dirbuff(pc),A1
	moveq.l	#0,D0
	move.l	#$200,D1
	JSRGEN	WriteFileHD
	bra	exit	

readdat:
	JSRGEN	ReadFileHD
	bra	exit	

trainer:
	dc.l	0
_general_pbuffer
	dc.l	0
_user_pbuffer
	dc.l	0
dirname:
	dc.b	"str.dir",0
savname:
	dc.b	"str.s"
savnum:
	dc.b	"aa",0
	cnop	0,4
dirbuff:
	blk.l	$80,0

boot:
	incbin	"str.bin",b0
_EndUserPatchRoutines:

