; Turrican 2 HD loader v1.1 by Kyzer/CSG

	include	jst.i

	HD_PARAMS "Turrican2.d",1076448,1	;  2048+(158*6800)
	Mac_printf "Turrican 2 HD Loader v1.1 © 1998-1999 Kyzer/CSG"
	bsr	loadhiscores
	JSRABS	LoadDisks
	moveq	#0,d0
	move.l	#CACRF_CopyBack,d1
	JSRABS	Degrade
	GO_SUPERVISOR
	SAVE_OSDATA	$80000

	moveq	#0,d0
	move.l	#2048,d1
	moveq	#0,d2
	move.l	#$60000,a0
	JSRGEN	ReadDiskPart	; D0=0 D1=LENGTH D2=OFFSET A0=LOADADDR

	PATCHUSRJSR $42(a0),intro
	PATCHUSRJMP $c0(a0),main
	PATCHUSRJMP $394(a0),rawloader

	JSRGEN	FlushCachesHard	

; loader @ $60000:
; on entry, d0 = pointer to set of memory locations that are above $80000,
; including fastmem.
; 0(d0).l = ptr to another location, or NULL for end of list
; 4(d0).l = size of this location, counting from the top of the node
; 8(d0).l = 0
	moveq	#0,d0		; 'we found no extension mem' lie
	jmp	(a0)

;--------------------------------------
; patch the call to run the intro
intro	btst.b	#7,$bfe001	; don't run intro if fire pressed
	beq.s	.done
	jsr	$3010a		; run the intro
.wait	cmp.w	#208,$30ae2	; wait for intro to end
	blt.s	.wait
.done	rts

rawloader
; get rawloader    D0=bytelength  D1=track  D2=trackoffset  A0=loadaddr
; to readdiskpart  D0=0  D1=bytelength  D2=diskfileoffset  A0=loadaddr
	exg.l	d0,d1		; d1 = bytelength, d0=track
	mulu	#6800,d0	; offset = (track-2)*6800 +2048 + trackoffset
	add.l	d0,d2		; = (track*6800)-(2*6800)+2048 + trackoffset
	sub.l	#11552,d2	; = track*6800 - 11552 + trackoffset
	moveq	#0,d0
	JSRGEN	ReadDiskPart	; D0=0 D1=LENGTH D2=OFFSET A0=LOADADDR
	JSRGEN	FlushCachesHard
	rts


main	; install patches on mainpart and run it

	PATCHUSRJMP $9e2.w,rawloader
	PATCHUSRJMP $be0.w,loadhi
	PATCHUSRJMP $c5e.w,savehi
	PATCHUSRJMP $dcc.w,decrunch
	PATCHUSRJMP $6d18.w,keyb

	JSRGEN	FlushCachesHard	
	jmp	$c0.w

;--------------------------------------

; ingame decruncher: full credit to WEPL who rewrote the decruncher

; What did he do? He changed the instructions which wrote into the operands
; of other instructions, to write to a scrap of mem instead. He then rewrote
; the usually modifed instructions to perform their task using this scrap of
; mem instead of themselves. This could be done with patches, but why not
; just modify the code in the assembler, and hold this decruncher in fastram?

decrunch incbin	t2decr.wepl



keyb	move.b	d0,$249.w
	cmp.b	#$59,d0	; F10 key
	beq.s	.die
	jmp	$6d36.w
.die	JSRGEN	InGameExit

; $1C1.b = NUMBER OF POWERLINES (0-99)
; $1D6.b = LEVEL COMPLETE TRUE/FALSE ???
; $1D7.b = CHEAT ON/OFF
; $23C.b = SOUND ON/OFF
; $5CE2.w = NEXT LEVEL (0=lev1, 1=lev2, 2=lev3, 3=lev4, 4=lev5, 5=intro)
; $5CE6.w = CURRENT SONG


loadhi	move.l	highbuf(pc),a0
	lea	$25e.w,a1
	tst.l	(a0)
	beq.s	.exit
	moveq	#400/4-1,d0
.copy	move.l	(a0)+,(a1)+
	dbra	d0,.copy
.exit	rts

	
savehi	lea	$25e.w,a0
	move.l	highbuf(pc),a1
	moveq	#400/4-1,d0
.cp	move.l	(a0)+,(a1)+
	dbra	d0,.cp
	lea	savehiscores(pc),a0
	JSRGEN	SetExitRoutine
	rts

;--------------------------------------

loadhiscores
	move.l	#400,d0
	JSRABS	AllocExtMem	; easy allocate 400 byte!
	lea	highbuf(pc),a0
	move.l	d0,(a0)

	bsr.s	__hiset
	JSRGEN	ReadUserFileHD
	rts

savehiscores
	bsr.s	__hiset
	JSRGEN	WriteUserFileHD
	rts

__hiset	moveq	#0,d0
	move.l	#400,d1
	lea	hiname(pc),a0
	move.l	highbuf(pc),a1
	rts

highbuf	dc.l	0
hiname	dc.b	'turrican2.hisc',0
