
;/* Jimmy`s fantastic Adventure..
  * -----------------------------
  * Hard-disk installable with vectorbase AGA Fix, cpu cache fixes, etc.
  * Read the source!
  */

_custom			=	$DFF000
intena			=	$9A
color			=	$180
dmacon			=	$96
fmode			=	$1fc
bplcon3			=	$106

AbsExecBase		=	$4
MaxLocMem		=	$3E
MaxExtMem		=	$4E
AttnFlags		=	$128

tv_UserIntVects		=	$100
tv_TrapInstrVects	=	$80

_LVOSupervisor		=	-30
_LVOOldOpenLibrary	=	-408
_LVOFindTask		=	$FFFFFEDA
_LVOWaitPort		=	$FFFFFE80
_LVOGetMsg		=	$FFFFFE8C
_LVOReplyMsg		=	$FFFFFE86
_LVOForbid		=	$FFFFFF7C
_LVOPermit		=	$FFFFFF76
_LVOOpen		=	$FFFFFFE2
_LVORead		=	$FFFFFFD6
_LVOClose		=	$FFFFFFDC
		
pr_CLI			=	$AC
pr_MsgPort		=	$5C

MODE_OLDFILE		=	$3ED

CACRF_CopyBack		=	$80000000	* Enable Copyback mode (`040+)
CACRF_EnableE		=	$40000000	* Enable External Caches
CACRF_WriteAllocate	=	$00002000	* Enable Write Allocate (`030+)
CACRF_DBE		=	$00001000	* Data Burst (`030+)
CACRF_ClearD		=	$00000800	* Flush Data Cache (`030+)
CACRF_FreezeD		=	$00000200	* Freeze Data Cache (`030+)
CACRF_EnableD		=	$00000100	* Enable Data Cache (`030+)
CACRF_IBE		=	$00000010	* Inst Burst (`030+)
CACRF_ClearI		=	$00000008	* Flush Inst Cache (`020+)
CACRF_FreezeI		=	$00000002	* Freeze Inst Cache (`020+)
CACRF_EnableI		=	$00000001	* Enable Inst Cache (`020+)

* Enable Instruction-cache()
* Enable Data-cache()
* Enable Write-allocate()

		section	fastram,code_f

*-------------- handle a workbench startup..

HD_Install:	move.l	d0,d7			;save CLI params len
		move.l	a0,a5			;save CLI params ptr
		suba.l	a1,a1			;clear a1
		move.l	4.w,a6			;get execbase
		jsr	_LVOFindTask(a6)	;find *THIS* task
		move.l	d0,a4			;get task ptr
		move.l	d7,d0			;restore CLI params len
		move.l	a5,a0			;restore CLI params ptr
		tst.l	pr_CLI(a4)		;did we originate from CLI?
		bne.w	ProgStart		;yes...call our program

		lea	pr_MsgPort(a4),a0	;from wb.. handle wb_startup
		move.l	a0,a4
		jsr	_LVOWaitPort(a6)	;wait for a message
		move.l	a4,a0
		jsr	_LVOGetMsg(a6)		;then get it
		move.l	d0,-(sp)
		bsr.w	ProgStart

		move.l	d0,d7			;save CLI Return code
		move.l	4.w,a6
		jsr	_LVOForbid(a6)
		move.l	(sp)+,a1
		jsr	_LVOReplyMsg(a6)
		move.l	d7,d0			;exit code
		rts

		cnop	0,4
RealLoadDOSFile:
		move.l	a1,a4
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2	* open mode
		move.l	_DOSBase,a6
		jsr	_LVOOpen(a6)		* open file
		move.l	d0,d6			* save filehandle
		beq.s	err			* open error result?
		move.l	d6,d1			* filehandle
		move.l	a4,d2			* buffer
		move.l	#901120,d3		* l = length read
		jsr	_LVORead(a6)		* call Read/Write
		move.l	d0,d2			* save read length
		beq.s	err
		move.l	d6,d1			* filebuffer
		jsr	_LVOClose(a6)		* Close File
		moveq	#0,d0
		rts
err:		moveq	#-1,d0
		rts

bad_file:	move.w	#$f00,$dff180		* red flash of screen..

		moveq	#5,d0			* exit to dos.. (warn)
		rts

_DOSBase	dc.l	0
		even
disk1.txt:	dc.b	'jimmy:jimmy1',0

*-------------- disk trackload block load 'simualtor'.. from image of disk
*		stored in public memory..

		cnop	0,4
LoadBlock:	movem.l	d1-d3/a0-a2,-(sp)	* Preserved trashed registers
		lea	disk1,a1
continue:	mulu	#512,d0			* Calculate StartBlock
		adda.l	d0,a1			* Get address for copy start
		mulu	#512,d1			* Calculate ByteSize to copy
		move.l	d1,d0
bytecopy:	move.b	(a1)+,(a0)+		* Copy x no.of bytes
		subq.l	#1,d1			* Subtract from bytesize
		bne.s	bytecopy		* Keep copying until all done!
		movem.l	(sp)+,d1-d3/a0-a2	* Restore trashed registers
		tst.l	d0
		rts

		cnop	0,4
ProgStart	move.l	4.w,a6
		lea	DosName(pc),a1		* lib name in a1
  		jsr	_LVOOldOpenLibrary(a6)	* try to open library
  		move.l	d0,_DOSBase		* store lib base
  		beq.w	bad_file		* cleanup and quit if fail

*-------------- load disk 1 mirror into free public ram..

		lea	disk1.txt(pc),a0
		lea	disk1,a1
		bsr	RealLoadDOSFile
		tst.l	d0
		bne.w	bad_file

*-------------- now take over machine now..

		move.l	4.w,a6
		lea	Caches(pc),a5
		jmp	_LVOSupervisor(a6)	* enter supervise mode!

*-------------- turn off all dma, interrupts, etc.		

Caches:		move.w	#$7FFF,$dff096		* no dmacon
		move.w	#$7FFF,$dff09a		* no intena
		move.w	#$7FFF,$dff09c		* no intreq

*-------------- patch AGA hardware to reset to defaults (null)

		move.w	#$0,_custom+fmode	* no AGA 64-bit fetchmodes..
		move.w	#$0,_custom+bplcon3	* no extra bitplane control

*-------------- turn OFF cpu caches.. (sob!)

		move.l	#CACRF_FreezeI+CACRF_FreezeD+CACRF_WriteAllocate,d0
		movec	d0,cacr

*-------------- reset vectorbase to zeropage 
		moveq	#0,d0
		movec	d0,vbr

*-------------- patch games 'hardware disk' loader to use FASTRAM loader..

		move.w	#$4ef9,fast1+$17d8+0
		move.l	#LoadBlock,fast1+$17d8+2

*-------------- do the game`s standard relocation bits here..

		move.l	(_LargePublBuffer,pc),a1
		move.l	(_LargeChipBuffer,pc),a4
		move.l	a1,a5
		lea	36(a1),a1
		move.l	a1,d1
		move.l	($14,a5),d0
		lsl.l	#2,d0
		add.l	d0,a5
		lea	40(a5),a5

		move.l	(a5),d7
		addq.l	#8,a5
		subq.l	#1,d7
fix_relocations	move.l	(a5),d0
		add.l	d1,(a1,d0.w)
		addq.l	#4,a5
		dbra	d7,fix_relocations

		move.l	(a5),d7
		addq.l	#8,a5
		move.l	a4,d1
		subq.l	#1,d7
fix_relocs2	move.l	(a5),d0
		add.l	d1,(a1,d0.w)
		addq.l	#4,a5
		dbra	d7,fix_relocs2

*-------------- if you hold right mouse button down during loading we activate
*		melon`s built in lives trainer mode..

		btst	#10,$dff016		* RIGHT MOUSE BUTTON PRESSED?
		bne.s	lives_trainer_on

		move.w	#$00f,$dff180		* show we enabled the cheat!

		move.w	#1,$6ac4(a1)		* activate "Melon Design`s"
						* built-in CHEAT-MODE for
						* Infinite lives trainer..
lives_trainer_on:

		jmp	(a1)			* jump into game..

bad_error	move.w	#$F00,($DFF180).l
		bra.w	bad_error

GfxName		dc.b	'graphics.library',0,0
DosName		dc.b	'dos.library',0
_LargePublBuffer	dc.l	fast1
_LargeChipBuffer	dc.l	chip1

		section	chippy,code_c		* CHIPRAM
chip1		incbin	work:resource/jimmy_chip.bin	* game main data
		section	public,code		* PUBLIC
fast1		incbin	work:resource/jimmy_public2.exe	* game main code
		section	pubdisk,bss		* PUBLIC
disk1:		ds.b	901120			* storage for disk image..

