	INCLUDE "exec/execbase.i"
	INCLUDE "hardware/dmabits.i"

	XREF _SysBase
	XREF _LVOSupervisor
	XREF _intena
	XREF _dmacon

	section text,code

	XDEF _loader
	XDEF _loaderend

_loader:
	move.l sp,a3
	move.l _SysBase(a4),a6
	lea start_super(pc),a5
	jmp	_LVOSupervisor(a6)

start_super:
	move.w	#$2700,sr

	; the BSD kernel wants values into the following registers:
	; a0:  fastmem-start
	; d0:  fastmem-size
	; d1:  chipmem-size
	; d3:  Amiga specific flags
	; d4:  E clock frequency
	; d5:  AttnFlags (cpuid)
	; d7:  boothowto
	; a4:  esym location
	; a2:  Inhibit sync flags
	; All other registers zeroed for possible future requirements.

	lea  stack(pc),sp		; make sure we have a good stack ??
	move.l	4(a3),a1		; loaded kernel
	move.l	8(a3),d2		; length of loaded kernel
	move.l	12(a3),d6   	; entry point
	move.l	16(a3),a0		; fastmem-start
	move.l	20(a3),d0		; fastmem-size
	move.l	24(a3),d1		; chipmem-size
	move.l	28(a3),d7		; boothowto
	move.l	32(a3),a4		; esym
	move.l	36(a3),d5		; cpuid
	move.l	40(a3),d4		; E clock frequency
	move.l	44(a3),d3		; Amiga flags
	move.l  48(a3),a2       ; Inhibit sync flags
	move.l  52(a3),a3		; target
	add.l   a3,d6           ; relocate entry point
	move.l  d6,-(sp)        ; push to stack for RTS

	btst	#AFB_68040,AttnFlags+1(a6)  ; AFB_68040,SysBase->AttnFlags
	beq	not040

; Turn off 68040 MMU

	sub.l a5,a5
	movec a5,TC
	movec a5,URP
	movec a5,SRP
	movec a5,ITT0
	movec a5,ITT1
	movec a5,DTT0
	movec a5,DTT1

	cpusha bc              ; push caches
	bra	nott

not040:

	lea	zero(pc),a5
	pmove	(a5),tc			; Turn off MMU
	lea	nullrp(pc),a5
	pmove	(a5),crp		; Turn off MMU some more
	pmove	(a5),srp		; Really, really, turn off MMU

; Turn off 68030 TT registers

	btst	#AFB_68030,AttnFlags+1(a6) ; AFB_68030,SysBase->AttnFlags
	beq	nott				; Skip TT registers if not 68030

	lea	zero(pc),a5
	pmove (a5),tt0
	pmove (a5),tt1

nott:
	move.w	#DMAF_MASTER,_dmacon	; disable DMA
	moveq #0,d6
	movec.l d6,cacr                 ; disable caches

L0:
	move.b	(a1)+,(a3)+
	subq.l	#1,d2
	bne.s	L0
	moveq	#0,d6			; (might make future compatibility
	move.l	d6,a5
	move.l	d6,a6
	move.l	d6,a1			;  would have known contents)
	move.l	d6,a3

	rts				        ; enter kernel at address on stack ***


nullrp:
	dc.l $7fff0001
zero:
	dc.l 0
	ds.l 64
stack:
_loaderend:

	end

