;*---------------------------------------------------------------------------
;  :Program.	gods.asm
;  :Contents.	Slave for "Gods"
;  :Author.	BJ
;  :History.	20.05.96
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;---------------------------------------------------------------------------*

	INCDIR	Includes:
	INCLUDE	whdload.i

	OUTPUT	wart:gods/gods.slave
	BOPT	O+ OG+			;enable optimizing
	BOPT	w4-			;disable 64k warnings

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

.base		SLAVE_HEADER		;ws_Security + ws_ID
		dc.w	1		;ws_Version
		dc.w	WHDLF_Disk	;ws_flags
		dc.l	$ff000		;ws_BaseMemSize			;$bc000
		dc.l	$f2800		;ws_ExecInstall
		dc.w	.Start-.base	;ws_GameLoader
		dc.w	0		;ws_CurrentDir
		dc.w	0		;ws_DontCache

;======================================================================
.Start	;	A0 = resident loader
;======================================================================

		lea	(_resload,pc),a1
		move.l	a0,(a1)			;save for later using

		move.l	#CACRF_EnableI,d0	;enable instruction cache
		move.l	d0,d1			;mask
		jsr	(resload_SetCACR,a0)

		move.l	#$80400,$2c	;exp-mem at
		move.b	#1,$1c
		lea	($1000),a5
		move.l	a5,$84

		move.l	#$600,d0	;offset
		move.l	#$a00,d1	;size
		moveq	#1,d2		;disk
		move.l	a5,a0		;data
		bsr	_LoadDisk

		patch	$e2(a5),_loader
		patch	$d6(a5),_1
		jmp	(a5)

_1		move.l	#$d200,d0
		patch	$a00+$1bc,_loader
		patch	$a00+$1b0,_2
		rts

_2		move.l	#$a280,d0
		patch	$a00+$1db5a,_loader
		rts

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

_loader		btst	#0,d3			;read or write ?
		bne	_savehighs		;only highscores

		moveq	#1,d0
		cmp.l	d0,d1
		bne	.0
		cmp.l	d0,d2
		beq	_loadhighs
.0
		movem.l	d1-d3/a0-a1,-(a7)

		cmp.w	#$28a,d2
		bne	.22
		move.w	#$26e,d2	;let's strip the diskimage a bit (disk1)
.22
		cmp.l	#$7ba80,a0
		bne	.1
		lea	(.disk),a1
		move.w	#2,(a1)		;from this point only disk 2
.1
		mulu	#512,d1		;start on disk
		move.l	d1,d0		;offset
		mulu	#512,d2		;amount of blocks
		move.l	d2,d1		;size
		moveq	#0,d2
		move.w	(.disk),d2	;disk
		bsr	_LoadDisk

		bsr	_kinit
		
		movem.l	(a7)+,d1-d3/a0-a1
		moveq	#0,d0
		rts

.disk		dc.w	1

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

_loadhighs
	IFD wmode
		move.l	#$b4863d88,$60
		move.l	#$5dc,$b04c
		clr.l	$d158
		clr.l	$f6bc
		clr.l	$107f0
	ENDC
		movem.l	d1/a0-a2,-(a7)
		move.l	a0,a1			;address
		lea	(_savename),a0
		move.l	(_resload),a2
		jsr	(resload_LoadFile,a2)
		tst.l	d0
		bne	.end
		movem.l	(a7),d1/a0-a2
		moveq	#512/4-1,d0
.clr		clr.l	(a0)+
		dbf	d0,.clr
.end		movem.l	(a7)+,d1/a0-a2
		moveq	#0,d0
		rts

_savehighs	movem.l	d1-d2/a0-a2,-(a7)
		mulu.w	#512,d2
		move.l	d2,d0
		move.l	a0,a1
		lea	(_savename),a0
		move.l	(_resload),a2
		jsr	(resload_SaveFile,a2)
		movem.l	(a7)+,d1-d2/a0-a2
		moveq	#0,d0
		rts

_savename	dc.b	"highs",0

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

_kinit		movem.l	a0-a1,-(a7)
		lea	(_keyboard,pc),a1
		cmp.l	$68,a1
		beq	.q
		lea	(_realint68,pc),a0
		move.l	$68,(a0)
		move.l	a1,$68
.q		movem.l	(a7)+,a0-a1
		rts

_realint68	dc.l	0

_keyboard	move.l	d0,-(a7)
		move.b	$bfec01,d0
		ror.b	#1,d0
		not.b	d0

; RAW-Key-Codes:
;	ESC	$45
;	DEL	$46
;	F1..F10	$50..$59
;	HELP	$5f

		cmp.b	#$58,d0
		bne	.1
		move.l	(a7)+,d0
		bra	_debug			;coredump & quit
.1
		cmp.b	#$59,d0
		beq	_exit			;exit

		move.l	(a7)+,d0
		move.l	(_realint68),-(a7)	;enter orginal rou.
		rts

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

_resload	dc.l	0		;address of resident loader

;--------------------------------
; IN:	d0=offset d1=size d2=disk a0=dest
; OUT:	d0=success

_LoadDisk	movem.l	d0-d2/a0-a2,-(a7)
		move.l	(_resload),a2
		jsr	(resload_DiskLoad,a2)
		tst.l	d0
		beq	.err
		movem.l	(a7)+,d0-d2/a0-a2
		moveq	#-1,d0
		rts
		
.err		move.l	d1,-(a7)
		movem.l	(4,a7),d0-d2/a0-a2
		move.l	d2,(4+5*4,a7)		;disk number
		move.l	(a7),(4+4*4,a7)		;doserror
		add.w	#4+4*4,a7
		move.l	#TDREASON_DISKLOAD,-(a7)
		move.l	(_resload),-(a7)
		add.l	#resload_Abort,(a7)
		rts

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

_exit		move.l	#TDREASON_OK,-(a7)
		bra	_end
_debug		move.l	#TDREASON_DEBUG,-(a7)
_end		move.l	(_resload,pc),-(a7)
		add.l	#resload_Abort,(a7)
		rts

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

	END
