;*---------------------------------------------------------------------------
;  :Program.	9finger.asm
;  :Contents.	Slave for "9 finger's" from Spaceballs
;  :Author.	WEPL
;  :History.	13.11.96 - 24.11.96
;		28.11.96 trackloader delay changed ('60)
;		16.06.97 soundtracker playback fixed
;			 slave version 2
;		21.06.97 rts on stfix added
;		24.06.97 keyboard routine fixed
;		15.08.97 update for key managment
;		30.08.97 keyboard external
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;---------------------------------------------------------------------------*

	INCDIR	Includes:
	INCLUDE	whdload.i

	OUTPUT	"dwart:9 finger's/9finger.slave"
	BOPT	O+ OG+			;enable optimizing
	BOPT	w4-			;disable 64k warnings
	SUPER

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

.base		SLAVE_HEADER		;ws_Security + ws_ID
		dc.w	4		;ws_Version
		dc.w	WHDLF_Disk|WHDLF_NoError ;ws_flags
		dc.l	$fb000		;ws_BaseMemSize
		dc.l	$400		;ws_ExecInstall
		dc.w	_Start-.base	;ws_GameLoader
		dc.w	0		;ws_CurrentDir
		dc.w	0		;ws_DontCache
_keydebug	dc.b	$58		;ws_keydebug = F9
_keyexit	dc.b	$59		;ws_keyexit = F10

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

	DOSCMD	"WDate >T:date"
		dc.b	"$VER: "
	INCBIN	"T:date"
		dc.b	0,0

;======================================================================
_Start	;	A0 = resident loader
;======================================================================

		lea	(_resload,pc),a1
		move.l	a0,(a1)			;save for later use
		
		move.l	#CACRF_EnableI,d0	;enable instruction cache
		move.l	d0,d1    		;mask
		jsr	(resload_SetCACR,a0)

	;install keyboard quitter
		bsr	_SetupKeyboard

 IFEQ 1
	;bootblock
		lea	$1000,a6		;A6 bootblock address
		
		moveq	#0,d0			;offset
		move.l	#2*512,d1		;size
		moveq	#1,d2			;disk
		lea	-12(a6),a0
		bsr	_LoadDisk
		
		moveq	#0,d0
		lea	$80000,a0

		nops	2,$3e(a6)		;exp mem check
		
		patch	$198(a6),_trackload
		patch	$150(a6),_trackload_bad
		
		patch	$148(a6),_1
		jmp	$26(a6)
 ENDC
 
;---------------

_start		lea	$80000,a6		;exp mem
		
		move.l	a6,a0
		moveq	#1,d0
		moveq	#16,d1
		bsr	_trackload
		
		lea	$7fff0,a0
		move.l	a6,(a0)+		;exp mem
		clr.l	(a0)+			;???
		move.w	#-1,(a0)+		;???
		
		lea	$1e00,a7
		
;---------------

_1		jsr	$80008			;decrunch
		lea	$2000,a6		;address of main exe

	;fix soundtracker playback
		move.l	a6,a0
		move.l	a6,a1
		add.l	#11*512*16,a1
		bsr	_stfix

		and.w	#~INTF_PORTS,$38b6(a6)	;preserve int on

		patch	$5f8(a6),_trackload	;loader
		patch	$4f2(a6),_disk2		;disk 2 check and wait
		patch	$430(a6),_restart

	;	bsr	waitvb
	;	move.w	#$8000,_custom+vposw	;force LOF

		jmp	(a6)

_restart	lea	_disk,a0
		subq.w	#1,(a0)
		move.w	#$7fff-INTF_PORTS,_custom+intena
		move.w	#4*50,d0		;wait n secs
.w		bsr	_waitvb
		dbf	d0,.w
	;	move.w	#$000,_custom+vposw	;set LOF
		bra	_start

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

_disk2		lea	_disk,a0
		addq.w	#1,(a0)
		rts

_disk		dc.w	1

;---------------
;	d0 = starttrack
;	d1 = amount tracks
;	a0 = address

;_trackload_bad	addq.w	#1,d1

_trackload	mulu	#11*512,d0		;offset
		mulu	#11*512,d1		;size
		move.w	(_disk),d2

		cmp.l	#200000,d1
		blo	.2
		lsr.l	#1,d1
		movem.l	d0-d1/a0-a2,-(a7)
		move.l	(_resload),a2
		jsr	(resload_DiskLoad,a2)
		movem.l	(a7)+,d0-d1/a0-a2
		add.l	d1,d0			;new offset
		add.l	d1,a0			;new address

		move.l	d1,d3
		divu	#11*512/18,d3		;wait ca. 18/50 secs per track
.w		bsr	_waitvb
		dbf	d3,.w

.2		movem.l	d0-d1/a0-a2,-(a7)
		move.l	(_resload),a2
		jsr	(resload_DiskLoad,a2)
		movem.l	(a7)+,d0-d1/a0-a2
		add.l	d1,a0
		
		rts

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

_waitvb		waitvb
		rts

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

_resload	dc.l	0		;address of resident loader

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

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

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

	INCDIR	Sources:whdload
	INCLUDE	keyboard.s
	INCLUDE	stfix.s

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

	END
