**********************************************************
*                                                        *
* KeyHack:    A quick temporary Keyboard Hack...         *
*                                                        *
* ... to patch a RAM copy of the Amiga Kickstart OS      *
* to slow down the Keyboard Handshake, and thus make the *
* Amiga usable on those nasty Cherry keyboards when said *
* Amiga has been Turbocharged with a gofaster board      *
* like Commodore's wonderful A2620.                      *
*                                                        *
* Note: This hack is temporary until someone does a more *
* decent 'legal' patch to the Kickstart RAM image.       *
*                                                        *
* This patch was written, and is copyright by Jez San of *
* Argonaut Software, London.  All rights reserved.       *
*                                                        *
* This program, and its sourcecode is freely             *
* redistributable provided the author's credit is left   *
* intact.   You may upload this file anywhere, or put it *
* on a fish (or other animal) disk but don't make profit *
* from distributing it.  This is NOT PD!                 *
*                                                        *
* The author can be contacted on the following nets...   *
* BIX: jsan  PLINK: UK JEZ  CIS: 72247,3661  jsan@well   *
*                                                        *
* This version, written on 4th Feb 1989.                 *
*                                                        *
* To use KeyHack you must have previously loaded up a    *
* RAM copy of the Kickstart ROM somewhere in main memory *
*                                                        *
* example session :-                                     *
*                                                        *
* 1> SETCPU FASTROM                                      *
* 1> KEYHACK                                             *
*                                                        *
* KeyHack will take a few seconds to find the RAM KS     *
* image, and will return whether it finds it or not.     *
*                                                        *
**********************************************************

	output	KeyHack

_execbase	equ	$4
_LVOAllocMem	equ	$ffffff3a



* First start searching RAM from $000000 to $7fffff for a RAM image
* of the Kickstart ROM.

	sub.l	a0,a0			* Start at location RAM=$0
	move.l	#$7fffff/2,d0		* And end searching at $7fffff.
	move.w	#$0028,d1		* For speed purposes registerise things.

search1	cmp.w	(a0)+,d1		* My particular ROMcode of interest
	beq.s	foundit			* Okay, patch it
search2	subq.l	#1,d0			* Any more to do?
	bne.s	search1

* Not found, Return a 'tuff luck' error to that effect...

error	moveq	#-2,d0			* Set an Error Return Code perhaps
	rts				* return from whence thou came

* Found a potential bit of Kickstart ROM, now check more to be sure (to be sure).

foundit	cmp.l	#$00400e00,(a0)		* Just to make sure...?
	bne.s	search2			* Keep on lookin
	cmp.l	#$78004600,4(a0)	* And definite?
	bne.s	search2

* Right, almost definite this is the right bit to patch.
* Here goes... (holding breath).

	move.l	a0,-(sp)		* Save off the buggy ROM position

* Allocate a small amount of memory to put the patch into...
	move.l	_execbase,a6
	moveq	#0,d1			* Type of memory
	move.l	#patchend-patchstart,d0	* Length
	jsr	_LVOAllocmem(a6)	* Exec AllocMem call
	tst.l	d0
	bne.s	noerror

	move.l	(sp)+,a0
	bra	error

noerror	move.l	d0,a2			* Okay, now this is our patch address
	move.l	d0,a4			* Save it again for JMP modification purposes

* Now put our new ROM return address into our patch area

foundit2
	move.l	(sp)+,a0		* Restore ROM address

	addq.l	#6-2,a0			* Return address in RAMROM
	move.l	a0,patch2+2		* Where my patch jumps back into the ROM.

* Now copy our patch into its newfound home.

	lea	patchstart(pc),a3		* Source data
	move.w	#(patchend-patchstart)/4,d0	* Amount of Longwords to copy
copylp1	move.l	(a3)+,(a2)+		* Copy a LongWord
	dbra	d0,copylp1		* Continue...

* Right, now that my patch is ready configured, so patch the RAM KS image.

	subq.l	#2+4,a0			* Backup a bit
	move.w	#$4ef9,(a0)+		* Install JMP.L instruction
	move.l	a4,(a0)+		* Install address to Jump To.

byebye	moveq	#0,d0
	rts



patchstart

* Put this line back in if you wanna see flashes each time you press a key!

*	move.w	#$f00,$dff180		* Flash screen red each keypress

	ori.b	#$40,$0e00(a0)

	move.l	d0,-(sp)		* Save d0

	muls	#-1,d0			* A few extra microseconds delay
	muls	#-1,d0			* Each Muls is up to 10 microseconds
	muls	#-1,d0			* on a 14 mhz 68020 its about 4
	muls	#-1,d0			* The ROM KS code has an inherent
	muls	#-1,d0			* delay due to the code that actually
	muls	#-1,d0			* handles keyboard input, so these
	muls	#-1,d0			* are purely supplemental... For
	muls	#-1,d0			* best results, use a proper hardware
	muls	#-1,d0			* timing loop based on the CIA chip
	muls	#-1,d0			* or the Video scanline as a dependable
	muls	#-1,d0			* timing source.
	muls	#-1,d0
	muls	#-1,d0
	muls	#-1,d0
	muls	#-1,d0

	move.l	(sp)+,d0		* Restore character that was pressed

patch2	jmp	$fffffffe		* Dummy JMP to be patch RSN!

patchend	dc.l	0		* End of patch

