;*---------------------------------------------------------------------------
;  :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
;  :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	2		;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

;======================================================================
.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
		lea	(.int2),a0
		move.l	a0,$68				;set vector
		lea	(_ciaa),a1
	;	move.b	#~(CIAICRF_SETCLR),(ciaicr,a1)
		move.b	#CIAICRF_SETCLR|CIAICRF_SP,(ciaicr,a1)	;allow ints from keyboard
		tst.b	(ciaicr,a1)			;clear all intreq
		and.b	#~(CIACRAF_SPMODE),(ciacra,a1)	;input mode
		move.w	#INTF_PORTS,(intreq+_custom)
		move.w	#INTF_SETCLR|INTF_INTEN|INTF_PORTS,(intena+_custom)
		bra	.int2_after
.int2		movem.l	d0-d1/a1,-(a7)
		lea	(_ciaa),a1
		btst	#CIAICRB_SP,(ciaicr,a1)		;check int reason
		beq	.int2_exit
		move.b	(ciasdr,a1),d0			;read code
		clr.b	(ciasdr,a1)			;output LOW (handshake)
		or.b	#CIACRAF_SPMODE,(ciacra,a1)	;to output
		not.b	d0
		ror.b	#1,d0
		cmp.b	#$45,d0				;ESC
		beq	_exit
		cmp.b	#$58,d0				;F9
		bne	.int2_1
		movem.l	(a7)+,d0-d1/a1
		move.w	(a7),(6,a7)			;sr
		move.l	(2,a7),(a7)			;pc
		clr.w	(4,a7)				;ext.l sr
		bra	_debug
.int2_1		cmp.b	#$59,d0				;F10
		beq	_exit
		moveq	#2-1,d1				;wait because handshake min 75 µs
.int2_w1	move.b	(_custom+vhposr),d0
.int2_w2	move.b	(_custom+vhposr),d0		;one line is 63.5 µs
		beq	.int2_w2
		dbf	d1,.int2_w2
		move.b	#-1,(ciasdr,a1)			;output HIGH (handshake)
		and.b	#~(CIACRAF_SPMODE),(ciacra,a1)	;to input
.int2_exit	move.w	#INTF_PORTS,(intreq+_custom)
		movem.l	(a7)+,d0-d1/a1
		rte
.int2_after

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

 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
		movem.l	a0-a1,-(a7)
		move.l	a6,a0
		lea	$10000,a1
		bsr	_stfix_init
		movem.l	(a7)+,a0-a1

		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

		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
		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

;--------------------------------
; fix for old soundtracker playback routine
; bad stuff:
;		move.w	#$12c,d0	;303c 012c
;	_loop	dbf	d0,_loop	;51c8 fffe
;
; a0 = start	a1 = end

_stfix_init
.s0		cmp.l	#$303c012c,(a0)
		bne	.s1
		cmp.l	#$51c8fffe,(4,a0)
		bne	.s1
		move.w	#$4eb9,(a0)+
		pea	(_stfix,pc)
		move.l	(a7)+,(a0)+
.s1		addq.l	#2,a0
		cmp.l	a0,a1
		bhi	.s0
		rts

_stfix		move.l	d1,-(a7)
		moveq	#7,d1
.1		move.b	$dff006,d0
.2		cmp.b	$dff006,d0
		beq	.2
		dbf	d1,.1
		move.l	(a7)+,d1
		addq.l	#2,(a7)
		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

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

	END
