
 IFND WHDLOAD_I
WHDLOAD=1

	IFND	EXEC_EXECBASE_I
	INCLUDE	exec/execbase.i
	ENDC
	IFND	EXEC_TYPES_I
	INCLUDE	exec/types.i
	ENDC
	IFND	HARDWARE_CIA_I
	INCLUDE	hardware/cia.i
	ENDC
	IFND	HARDWARE_CUSTOM_I
	INCLUDE	hardware/custom.i
	ENDC
	IFND	HARDWARE_INTBITS_I
	INCLUDE	hardware/intbits.i
	ENDC
	IFND	HARDWARE_DMABITS_I
	INCLUDE	hardware/dmabits.i
	ENDC

 BITDEF POTGO,OUTRY,15
 BITDEF POTGO,DATRY,14
 BITDEF POTGO,OUTRX,13
 BITDEF POTGO,DATRX,12
 BITDEF POTGO,OUTLY,11
 BITDEF POTGO,DATLY,10
 BITDEF POTGO,OUTLX,9
 BITDEF POTGO,DATLX,8
 BITDEF POTGO,START,0

_ciaa		= $bfe001
_ciab		= $bfd000
_custom		= $dff000

;======================================================================
;	misc
;======================================================================

SLAVE_DEFNAME	MACRO
	dc.b	"WHDLoad.Slave",0
		ENDM
SLAVE_SECURITY	MACRO
	moveq	#-1,d0
	rts
		ENDM
SLAVE_ID	MACRO
	dc.b	"WHDLOADS"
		ENDM
SLAVE_HEADER	MACRO
	SLAVE_SECURITY
	SLAVE_ID
		ENDM

;======================================================================
;	some useful macros
;======================================================================

****************************************************************
***** write opcode ILLEGAL to specified address
ill	MACRO
	IFEQ	NARG-1
		move.w	#$4afc,\1
	ELSE
		FAIL	arguments "ill"
	ENDC
	ENDM

****************************************************************
***** write opcode RTS to specified address
ret	MACRO
	IFEQ	NARG-1
		move.w	#$4e75,\1
	ELSE
		FAIL	arguments "ret"
	ENDC
	ENDM

****************************************************************
***** write \1 times opcode NOP starting at address \2
nops	MACRO
	IFEQ	NARG-2
		movem.l	d0/a0,-(a7)
		moveq	#\1-1,d0
		lea	\2,a0
.lp\@		move.w	#$4e71,(a0)+
		dbf	d0,.lp\@
		movem.l	(a7)+,d0/a0
	ELSE
		FAIL	arguments "nops"
	ENDC
	ENDM

****************************************************************
***** write opcode JMP \2 to address \1
patch	MACRO
	IFEQ	NARG-2
		move.l	a0,-(a7)
		lea	\1,a0
		move.w	#$4ef9,(a0)+
		pea	\2
		move.l	(a7)+,(a0)
		move.l	(a7)+,a0
	ELSE
		FAIL	arguments "patch"
	ENDC
	ENDM

****************************************************************
***** write opcode JSR \2 to address \1
patchs	MACRO
	IFEQ	NARG-2
		move.l	a0,-(a7)
		lea	\1,a0
		move.w	#$4eb9,(a0)+
		pea	\2
		move.l	(a7)+,(a0)
		move.l	(a7)+,a0
	ELSE
		FAIL	arguments "patchs"
	ENDC
	ENDM

****************************************************************
***** wait of vertical blank
waitvb	MACRO
	IFEQ	NARG
.loop1\@	btst	#0,(_custom+vposr+1)
		beq	.loop1\@
.loop2\@	btst	#0,(_custom+vposr+1)
		bne	.loop2\@
	ELSE
		FAIL	arguments "waitvb"
	ENDC
	ENDM

****************************************************************
***** wait for pressing any button
waitbutton	MACRO
	IFEQ	NARG
	
.down\@		waitvb
		btst	#CIAB_GAMEPORT0,(ciapra+_ciaa)		;LMB
		beq	.up\@
		btst	#POTGOB_DATLY-8,(potinp+_custom)	;RMB
		beq	.up\@
		btst	#CIAB_GAMEPORT1,(ciapra+_ciaa)		;FIRE
		bne	.down\@
.up\@		waitvb		;entprellen
		btst	#CIAB_GAMEPORT0,(ciapra+_ciaa)		;LMB
		beq	.up\@
		btst	#POTGOB_DATLY-8,(potinp+_custom)	;RMB
		beq	.up\@
		btst	#CIAB_GAMEPORT1,(ciapra+_ciaa)		;FIRE
		beq	.up\@
		waitvb		;entprellen
	ELSE
		FAIL	arguments "waitbutton"
	ENDC
	ENDM

****************************************************************
***** flash the screen and wait for LMB
BLITZ		MACRO
		move	#$4200,bplcon0+_custom
		move.w	d0,-(a7)
.lpbl\@		move.w	d0,$dff180
		subq.w	#1,d0
		btst	#6,$bfe001
		bne	.lpbl\@
		waitvb			;entprellen
.lp2bl\@	move.w	d0,$dff180
		subq.w	#1,d0
		btst	#6,$bfe001
		beq	.lp2bl\@
		move.w	(a7)+,d0
		ENDM

****************************************************************
***** install Vertical-Blank-Interrupt which quits on LMB pressed
QUITVBI		MACRO
		move.l	a0,-(a7)
		lea	.vbi,a0
		move.l	a0,$6c
		bra	.g
.vbi		btst	#6,$bfe001
		beq	.vbi+1		;create "address error"
		move.w	#INTF_VERTB,_custom+intreq
		rte
.g		move.w	#INTF_SETCLR|INTF_INTEN|INTF_VERTB,_custom+intena
		move.w	#INTF_VERTB,_custom+intreq
		move.l	(a7)+,a0
	ENDM

;======================================================================
;	return-values for termination (resload_Abort)
;======================================================================

TDREASON_OK		= -1	;normal termination
TDREASON_DOSREAD	= 1	;error with resload_ReadFile
TDREASON_DOSWRITE	= 2	;error with resload_SaveFile
TDREASON_EXCEPTION	= 3	;(private only WHDLoad)
TDREASON_RESET		= 4	;(private only WHDLoad)
TDREASON_DEBUG		= 5	;make coredump and quit
TDREASON_DOSLIST	= 6	;error with resload_ListFiles
TDREASON_DISKLOAD	= 7	;error with resload_DiskLoad
TDREASON_FALSEMODE	= 9999	;(private only WHDLoad)

;======================================================================
; Slave		Version 1
;======================================================================

	STRUCTURE	WhdloadSlave,0
		STRUCT	ws_Security,4	;moveq #-1,d0 + rts
		STRUCT	ws_ID,8		;"WHDLOADS"
		UWORD	ws_Version	;version of Whdload that is required
		UWORD	ws_Flags	;see below
		ULONG	ws_BaseMemSize	;size of mem required by game
					;(must multiple of $1000, max=$200000)
		ULONG	ws_ExecInstall	;address in BaseMem where space is for a fake ExecLibrary
					;installed by the Loader to survive a RESET
					;for example $400, required are at least 84 Bytes
		RPTR	ws_GameLoader	;start of slave-code
					;will called from WHDLoad after init (in SuperVisor)
				;don't forget : the slave must be 100.00% PC-RELATIVE
		RPTR	ws_CurrentDir	;directory in which the mainloader should search for files
		RPTR	ws_DontCache	;pattern string for files which must not cached

		LABEL	ws_SIZEOF

;Flags for ws_Flags
 BITDEF WHDL,Disk,0	;means only diskimages (result is a different PRELOAD)

;======================================================================
; the JMP-Tower in WHDLoad (similar to a library)
; resident Loader
;	in "WHDLoad"
;	can called in HARD-State only
;	structure is a JMP-Tower
;======================================================================

	STRUCTURE	ResidentLoader,0
		ULONG	resload_Install		;(privat ONLY from WHDLoad)
		ULONG	resload_Abort		;return to operating system
			; Übergabe :	(a7)	= ULONG success (one of TDREASON_xxx)
			;		(4,a7)	= ULONG primary error code (e.g. doserrorcode)
			;		(8,a7)	= ULONG secondary error code (e.g. object name)
			;this must called via JMP ! (not JSR)
			;all other routines via JSR !
		ULONG	resload_LoadFile	;load to BaseMem
			; Übergabe :	a0 = CPTR name of file
			;		a1 = APTR address
			; Rückgabe :	d0 = BOOL success (size of file)
			;		d1 = ULONG dos errcode (0 if all went ok)
		ULONG	resload_SaveFile	;save from BaseMem
			; Übergabe :	d0 = LONG length to save
			;		a0 = CPTR name of file
			;		a1 = APTR address
			; Rückgabe :	d0 = BOOL success
			;		d1 = ULONG dos errcode (0 if all went ok)
		ULONG	resload_SetCACR		;sets the CACR (also ok with 68000's and from user-state)
			; Übergabe :	d0 = ULONG new cacr
			;		d1 = ULONG mask (bits to change)
			; Rückgabe :	d0 = ULONG old cacr
		ULONG	resload_ListFiles	;list files in dir to buffer
			; Übergabe :	d0 = ULONG buffer size (a1)
			;		a0 = CPTR name of directory to scan (relative of course)
			;		a1 = APTR buffer (MUST lie inside Slave !!!)
			; Rückgabe :	d0 = ULONG amount of listed names
			;		d1 = ULONG dos errcode (0 if all went ok)
		ULONG	resload_Decrunch	;decrunch memory
			; Übergabe :	a0 = APTR source
			;		a1 = APTR destination (can be equal to a0)
			; Rückgabe :	d0 = BOOL success (size of file unpacked)
		ULONG	resload_LoadFileDecrunch
			; Übergabe :	a0 = CPTR name of file (anywhere)
			;		a1 = APTR address (MUST inside BaseMem !!!)
			; Rückgabe :	d0 = BOOL success (size of file)
			;		d1 = ULONG dos errcode (0 if all went ok)
		ULONG	resload_FlushCache	;flush all caches
			; Übergabe :	-
			; Rückgabe :	-
		ULONG	resload_GetFileSize
			; Übergabe :	a0 = CPTR name of file
			; Rückgabe :	d0 = LONG size of file or 0 if does'nt exist
		ULONG	resload_DiskLoad
			; Übergabe :	d0 = ULONG offset
			;		d1 = ULONG size
			;		d2 = ULONG disk number
			;		a0 = APTR destination
			; Rückgabe :	d0 = BOOL success
			;		d1 = ULONG dos errorcode (if failed)
		LABEL	resload_SIZEOF

resload_CheckFileExists = resload_GetFileSize

;======================================================================

 ENDC

