;*---------------------------------------------------------------------------
;  :Program.	oscar.asm
;  :Contents.	Slave for "Oscar"
;  :Author.	Wepl
;  :Version.	$Id: oscar.asm 1.3 1998/06/27 23:50:34 jah Exp jah $
;  :History.	20.05.96
;		16.06.97 updated for slave version 2
;		15.08.97 update for key managment
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;---------------------------------------------------------------------------*

	INCDIR	Includes:
	INCLUDE	whdload.i

	IFD BARFLY
	OUTPUT	"wart:oscar/oscar.slave"
	BOPT	O+ OG+				;enable optimizing
	BOPT	ODd- ODe-			;disable mul optimizing
	BOPT	w4-				;disable 64k warnings
	SUPER					;disable supervisor warnings
	ENDC

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

_base		SLAVE_HEADER			;ws_Security + ws_ID
		dc.w	4			;ws_Version
		dc.w	WHDLF_NoError		;ws_flags
_upchip		dc.l	$177000			;ws_BaseMemSize
		dc.l	$300			;ws_ExecInstall
		dc.w	_Start-_base		;ws_GameLoader
		dc.w	_dir-_base		;ws_CurrentDir
		dc.w	0			;ws_DontCache
_keydebug	dc.b	0			;ws_keydebug
_keyexit	dc.b	$59			;ws_keyexit = F10

_dir		dc.b	"data",0

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

	IFD BARFLY
		dc.b	"$VER: Oscar.Slave by Wepl "
	DOSCMD	"WDate >T:date"
	INCBIN	"T:date"
		dc.b	0
	ENDC
	EVEN

;======================================================================
_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)

		lea	_10,a0
		move.l	(.freemem),a1
		move.l	(_resload),a2
		jsr	(resload_LoadFileDecrunch,a2)
		lea	.freemem,a0
		add.l	d0,(a0)

		patch	$400+$8b4e,.allocmem	;emulate this
		skip	$2a8-$276,$400+$276	;disable some os-stuff
		patch	$400+$7b1a,_loader
		add.l	#$99fc+$400-$9b64,$838+$400	;copylock
		jmp	$43e			;start the dance

.allocmem	lea	.freemem,a0		;this emulates "exec.AllocMem"
		move.l	(a0),a1
		add.l	d0,(a0)
		move.l	(a0),d1
		cmp.l	(_upchip),d1
		blo	.1
	illegal					;if basemem must increased
.1		move.l	a1,a0
		addq.l	#7,d0
		lsr.l	#3,d0
.clr		clr.l	(a0)+
		clr.l	(a0)+
		subq.l	#1,d0
		bne	.clr
		move.l	a1,d0
		rts

.freemem	dc.l	$400			;start point of free memory

_10		dc.b	"exe",0
	EVEN

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

_loader		addq.l	#4,a0			;skip "df0:"
		move.l	a2,-(a7)
		move.l	(_resload),a2
		jsr	(resload_LoadFileDecrunch,a2)
		move.l	(a7)+,a2
		bsr	_kinit
		moveq	#0,d0			;return code
		rts

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

_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	(_keydebug),d0
		bne	.1
		move.l	(a7)+,d0
		move.w	(a7),(6,a7)		;sr
		move.l	(2,a7),(a7)		;pc
		clr.w	(4,a7)			;ext.l sr
		bra	_debug			;coredump & quit
.1
		cmp.b	(_keyexit),d0
		beq	_exit			;exit
		cmp.b	#$45,d0
		beq	_exit			;exit

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

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

_resload	dc.l	0			;address of resident loader

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

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

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

	END

