; FILE: Source:modules/PrepareEmul.ASM          REV: 2 --- PrepareEmul for BlizKick

;
; PrepareEmul patch for BlizKick ("patch" Module)
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Written by Harry Sintonen.
; This source code is Public Domain.
;
; Original PrepareEmul belongs to The ShapeShifter package
; © Copyright 1993-1995 Christian Bauer. This patch doesn't really
; base on PrepareEmul program, but ShapeShifter's documentation...
;

	incdir	"include:"
	include	"blizkickmodule.i"	; Some required...

	SECTION	PATCH,CODE
_DUMMY_LABEL
	BK_PTC

; Code is run with following incoming parameters:
;
; a0=ptr to ROM start (buffer)	eg. $1DE087B8
; a1=ptr to ROM start (ROM)	eg. $00F80000 (do *not* access!)
; d0=ROM lenght in bytes	eg. $00080000
; a2=ptr to _FindResident routine (will search ROM buffer for resident tag):
;    CALL: jsr (a2)
;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to resident name
;     OUT: d0=ptr to resident (buf) or NULL
; a3=ptr to _InstallModule routine (can be used to plant a "module"):
;    CALL: jsr (a3)
;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to module, d6=dosbase
;     OUT: d0=success
; a4=ptr to _Printf routine (will dump some silly things (errormsg?) to stdout ;-)
;    CALL: jsr (a4)
;      IN: a0=FmtString, a1=Array (may be 0), d6=dosbase
;     OUT: -
; d6=dosbase, a6=execbase
;
; Code should return:
;
; d0=true if succeeded, false if failed.
; d1-d7/a0-a6 can be trashed. a7 *must* be preserved! ;-)

NEWSTART	EQU	$2000

	moveq	#0,d7
	lsr.l	#1+5,d0			; d0=1st 1/32th of the rom (words)
	move.w	#$41F8,d1		; cmp preload
.find	cmp.w	(a0)+,d1
	dbeq	d0,.find
	tst.w	d0
	bmi.b	.done
	cmp.w	#$0400,(a0)
	beq.b	.is
	cmp.w	#$1000,(a0)
	bne.b	.find
.is	move.w	#NEWSTART,(a0)
	addq.l	#1,d7
	bra.b	.find

.done	move.l	d7,d0
	beq.b	.failed
	rts

.failed	lea	(.errmsg,pc),a0
	jsr	(a4)
	moveq	#0,d0
	rts


.errmsg	dc.b	'This ROM isn''t compatible with PrepareEmul patch!',10,0


	SECTION	VERSION,DATA

	dc.b	'$VER: PrepareEmul_PATCH 1.0 (11.9.96)',0

