; FILE: Source:modules/Fix13.ASM          REV: 16 --- Fix KS1.3 2meg chip bug.

; NOTE: As of BlizKick 1.10 this patch is obsolete! Functionality of this
;       patch is now built in.

;
; Example BlizKick Module
; ~~~~~~~~~~~~~~~~~~~~~~~
; This code shows how to create simple BlizKick "patch" Module.
; Should be quite self-explonary...
;
; !PATCHING CODE NEED NOT BE FULLY PC-RELATIVE!
;
; !PATCH CODE ITSELF NEED TO BE FULLY PC-RELATIVE!
;  (as ROM is...)
;
; Written by Harry Sintonen.
; This source code is Public Domain.
;

	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! ;-)

	lea	($C,a0),a5		; a5=ptr to rom version (buf)
	lea	(_error1,pc),a2
	cmp.w	#34,(a5)		; Requires KS V34
	bne.b	.exit

	lea	(_error2,pc),a2
	move.w	#1024,d0		; Scan first 1024 words:
	moveq	#2,d7			; Set counter
.find	subq.w	#2,d0
	beq.b	.exit
	addq.l	#2,a0			; Looking for:
	cmp.l	#$B7FC0008,(a0)		; "cmpa.l #512*1024,a3"
	beq.b	.found1
.cont1	cmp.l	#$43F90020,(a0)		; "lea $200000,a1"
	beq.b	.found2
	bra.b	.find

.found1	tst.w	(4,a0)
	bne.b	.find
	cmp.b	#$62,(6,a0)		; "bhi.b xxx" 
	bne.b	.cont1

	move.w	#$0020,(2,a0)		; Set new max chip size
	subq.l	#1,d7			; Decr counter
	bra.b	.find

.found2	cmp.l	#$00004BFA,(4,a0)	; "lea (xxx,pc),a5"
	bne.b	.find

	move.w	#$47F9,(a0)		; Fix "lea $200000,a3"
	move.w	#$240-$218,(12,a0)	; Set new bra.w offset
	subq.l	#1,d7			; Decr counter
	bra.b	.find


.exit	tst.l	d7			; Valid counter?
	bne.b	.fail
	moveq	#1,d0			; Return success
	rts

.fail	move.l	a2,a0			; a0=fmt
	move.l	a5,a1			; a1=array
	jsr	(a4)			; Call _Printf
	moveq	#0,d0			; Return fail
	rts

_error1	dc.b	'Fix13: This patch requires KS1.3 (V34) ROM! (you tried V%d.%d)',10,0
_error2	dc.b	'Fix13: Couldn''t patch!',10,0


	SECTION	VERSION,DATA

	dc.b	'$VER: Fix13_PATCH 1.1 (22.3.97)',0
