; FILE: Source:modules/RebootFix.ASM          REV: 7 --- Fix problem with multisync reboot

;
; RebootFix module for BlizKick
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Written by Harry Sintonen.
; This source code is Public Domain.
;
;

	incdir	"include:"
	include	"blizkickmodule.i"


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

	cmp.w	#37,($C,a0)		;requires V37+ rom image
	bhs.b	.cont
	moveq	#0,d0
	rts

.cont	moveq	#0,d7

	move.l	a0,a1
	move.l	#$397C0200,d1
	move.w	#512-1,d2
.findl	subq.w	#1,d2
	beq.b	.badrom
	addq.l	#2,a1
	cmp.l	(a1),d1
	bne.b	.findl
	cmp.w	#$0100,(4,a1)
	bne.b	.findl

	move.w	#$4EB9,(a1)+		;create jsr
	move.l	a1,a5
	move.l	#_earlycode_RT,(a5)	;add RT!!

	move.l	d0,d5
	lea	(_rebootfix_module,pc),a1
	jsr	(a3)			;call _InstallModule
	move.l	d0,d1
	move.l	d5,d0
	tst.l	d1
	beq.b	.nomod

	lea	(_name,pc),a1
	jsr	(a2)			;call _FindResident
	tst.l	d0
	beq.b	.nomod
	add.l	d0,(a5)			;add RT!

	moveq	#1,d7
.nomod
.badrom
	move.l	d7,d0
	rts

	CNOP	0,4
_rebootfix_module
 BK_MOD BKMF_SingleMode,_end,(0)<<24+37<<16+NT_UNKNOWN<<8+(256-128),_name,_idstr,_init

; Singlemode on,
; NEVER INIT module, requires KS V37.x or better, module type NT_UNKNOWN, priority -128.

_init	rts

_earlycode_RT	EQU	*-_rebootfix_module-bkm_ResTag
	move.w	#$0200,($100,a4)	; Colour burst on!

	move.l	d0,-(sp)
	moveq	#3,d0
	swap	d0			; some delay
.loop	move.w	#$0020,($1DC,a4)	; set PAL
	subq.l	#1,d0
	bne.b	.loop
	move.l	(sp)+,d0
	rts

_name
_idstr	dc.b	'RebootFix patch',0
	CNOP	0,2
_end

	SECTION	VERSION,DATA

	dc.b	'$VER: RebootFix_PATCH 1.2 (5.12.97)',0

