;======================================================================
;
;	This is the RAM reboot code, which is called to reset the
;	system into the new ROM, which is of course now in some magic
;	and safe place.  This code assumes that the memory is going
;	to be visable and unmapped when called, and that after the
;	RESET instruction, it'll be visable again at least by the time
;	the OVL bit is reset.
;
;		a2 - tag
;		d2 - ROM jump address
;======================================================================

RAMBootCode:
	lea.l	TAGTC(a2),a0            ; Get TC register
	lea.l	TAGCRP(a2),a1           ; Get CRP register

	and.l	#$7fffffff,(a0)         ; Turn off MMU
	dc.w	$f010			; PMOVE (a0),TC
	dc.w	$4000
	or.l	#$80000000,(a0)         ; Prep for startup

	dc.w	$f011			; PMOVE (a1),CRP
	dc.w	$4c00

	move.l	#$0809,d1     ; Make sure inst cache is on + flushed.
	movec	d1,cacr

	move.b	#2,d0
	move.l	#1,d3			; First pass, no delay
	bra	CacheRun

RSTLoop:
	btst	#0,d0
	beq	ReAnim
BootOpt:
	move.l	#$00400000,d3
	reset				; Clobber the system

CacheRun:
	move.w	ANYCREG,d1		; Set a spell, take yer shoes off
	subq.l	#1,d3
	bne	CacheRun
	move.b	#3,$bfe201		; Turn off Amiga ROM Overlay
	move.b	#2,$bfe001

	subq.b	#1,d0			; Where do we go?
	btst	#0,d0
	beq	RSTLoop

	move.l	#3,d1			; Freeze the cache
	movec	d1,cacr
	bra	RSTLoop

	nop
	nop

ReAnim:
	move.l	#0,d1			; Turn all caches off
	movec	d1,cacr

	move.l	#5,d1			; Let 'em know we're here
1$	bchg	#1,$bfe001
	move.l	#80000,d0
2$	subq.l	#1,d0
	bne	2$
	dbra	d1,1$

	dc.w	$f010			; Turn the MMU on
	dc.w	$4000

;JumpOpt:
;	move.l	#$44223311,a1		 ; Get the ROM start
	move.l	d2,a1
	IFNE	DEBUG
	  lea.l   $f00000,a0
	  move.l  a1,(a0)+
	  move.l  -2(a1),(a0)+
	  move.l  2(a1),(a0)+
	ENDC
	jmp	(a1)                    ; Start up the ROM
	nop
	nop
	nop
RAMBootEnd:
