;*---------------------------------------------------------------------------
;  :Program.	oscar.asm
;  :Contents.	Slave for "Oscar"
;  :Author.	Wepl
;  :Version.	$Id: oscar.asm 1.5 1999/05/09 13:30:28 jah Exp jah $
;  :History.	20.05.96
;		16.06.97 updated for slave version 2
;		15.08.97 update for key managment
;		15.07.98 cache on 68040+ disabled
;		08.05.99 adapted for WHDLoad 10.0, access faults removed
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;---------------------------------------------------------------------------*

	INCDIR	Includes:
	INCLUDE	whdload.i

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

	STRUCTURE	globals,$100
		LONG	_resload
		LONG	_chipptr
		LABEL	gl_ENDOF

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

_base		SLAVE_HEADER			;ws_Security + ws_ID
		dc.w	10			;ws_Version
		dc.w	WHDLF_NoError		;ws_flags
_upchip		dc.l	$177000			;ws_BaseMemSize
		dc.l	0			;ws_ExecInstall
		dc.w	_Start-_base		;ws_GameLoader
		dc.w	_data-_base		;ws_CurrentDir
		dc.w	0			;ws_DontCache
_keydebug	dc.b	0			;ws_keydebug
_keyexit	dc.b	$59			;ws_keyexit = F10
_expmem		dc.l	0			;ws_ExpMem
		dc.w	_name-_base		;ws_name
		dc.w	_copy-_base		;ws_copy
		dc.w	_info-_base		;ws_info

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

	IFND	.passchk
	DOSCMD	"WDate  >T:date"
.passchk
	ENDC

_data		dc.b	"data",0
_name		dc.b	"Oscar",0
_copy		dc.b	"1993 Flair Software",0
_info		dc.b	"installed & fixed by Wepl",10
		dc.b	"version 1.5 "
		INCBIN	"T:date"
		dc.b	0
	EVEN

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

	;save resload base
		move.l	a0,(_resload)			;save
		move.l	a0,a5				;A5 = resload

	;set start address for emulated exec.AllocMem
		move.l	#$400,(_chipptr)

	;enable caches
		move.l	#WCPUF_NWA|WCPUF_Base_WT,d0
		move.l	#WCPUF_NWA|WCPUF_Base,d1
		jsr	(resload_SetCPU,a5)

	;load main
		lea	(_exe,pc),a0			;name
		move.l	(_chipptr),a1			;address
		move.l	a1,a4				;A4 = executable
		jsr	(resload_LoadFileDecrunch,a5)
	;relocate main
		move.l	a4,a0				;address
		sub.l	a1,a1				;taglist
		jsr	(resload_Relocate,a5)
		add.l	d0,(_chipptr)
	;fix empty dbf loops
		move.l	a4,a0				;start
		lea	(a0,d0.l),a1			;end
		lea	(gl_ENDOF),a2			;temp
		bsr	_dbffix
	;apply patches
		lea	(_pexe,pc),a0			;patchlist
		move.l	a4,a1				;address
		jsr	(resload_Patch,a5)

		lea	($99fc-$9b64,a4),a0
		move.l	a0,d0
		add.l	d0,($838,a4)			;copylock

	;start main
		jmp	($3e,a4)

_pexe		PL_START
		PL_P	$8b4e,_allocmem			;emulate
		PL_S	$276,$2a8-$276			;disable os-stuff
		PL_P	$7b1a,_loader
		PL_W	$1ce2,$e841			;lsr.w  -> asr.w
		PL_W	$1ce4,$c3fc			;mulu   -> muls
		PL_W	$1cfc,$d0c1			;adda.l -> adda.w
		PL_W	$1e0e,$e841			;lsr.w  -> asr.w
		PL_W	$1e10,$c3fc			;mulu   -> muls
		PL_W	$1e28,$d0c1			;adda.l -> adda.w
		PL_END

_allocmem	addq.l	#7,d0				;round up
		and.b	#$f8,d0

		move.l	(_chipptr),a1
		add.l	d0,(_chipptr)

		move.l	a1,a0
		lsr.l	#3,d0
.clr		clr.l	(a0)+
		clr.l	(a0)+
		subq.l	#1,d0
		bne	.clr
		move.l	a1,d0
		rts

_exe		dc.b	"exe",0

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

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

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

		INCLUDE	sources:whdload/dbffix.s

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

	END
