* MAPUX v0.0 Kernel [skin-of-teeth]
* (c) Martin Young '95

	; These are imported to this module
	XREF	_fs_init
	XREF	isr_install
	XREF	SysCall
	XREF	_renderClear
	XREF	_Screen

	XDEF	_SStack

	MACHINE	68020

	INCLUDE	"sys/hardware.i"

	CODE

	; For starters we need a user stack (small)
	move.l	#UStack,sp

	; Disable all interrupts
	move.w	#$7fff,INTREQ		; Clear all pending interrupts
	move.w	#$7fff,INTENA		; Disable all interrupts & master
	move.w	#$7fff,DMACON		; Turn off all DMA

	; Install the new interrupt handlers
	jsr	isr_install		; Inserts vectors

	; Clear the bitplane data
	jsr	_renderClear

	; Copy bitplane pointers into copper list
	move.l	#Copper,a0		; Location of copper list
	move.l	#_Screen,d0		; Location of bitplane data
	move.w	d0,58(a0)		; BPL1PTL
	swap	d0
	move.w	d0,54(a0)		; BPL1PTH
	swap	d0

	; Set copper list program counter
	move.w	#$01ff,DMACON		; Switch off all DMA
	move.l	#Copper,COP1LC		; Where the copper list is
	move.w	#$8380,DMACON		; Re-enable master, copper and bitplane DMA
	move.w	d0,COPJMP1		; Strobe COPJMP1

	; Enter supervisor mode
	move.l	#InSupx,$80		; Re-route trap #0
	trap	#0
InSupx:	move.l	#_SStack,d0
	movec	d0,msp			; Set the master stack pointer
	move.l	#IStack,d0
	movec	d0,isp			; Set the interrupt stack pointer
	move.l	#UStack,a0
	move.l	a0,usp			; And reset the user stack too
	move.l	#SysCall,$80		; Trap #0 becomes unix system call


	; This is unpleasant: plug in the keyboard
	move.b	#$0,CIAA_CRA
	move.b	#$0,CIAA_CRB		; Turn off the B bits
	move.b	#$7f,CIAA_ICR		; Clear interrupts on CIA-A
	move.b	#$88,CIAA_ICR		; Enable serial interrupts at CIA-A
	move.w	#$c008,INTENA		; Enable CIA-A interrupts & master
	move.b	#$20,CIAA_CRA		; Seems like a nice number


	; And now - I must go down to the C again
	jsr	_fs_init		; guess what?
they_say:	
	jmp	they_say		; Bowie


	DATA

	; Copper list
Copper: dc.l	$2b01fffe		; wait for x=0, y=43
	dc.l	$01800000		; two colours
	dc.l	$01820fff
	dc.l	$01000200		; BPLCON0
	dc.l	$01040000		; BPLCON2
	dc.l	$008e2c81		; DIWSTRT
	dc.l	$00902cc1		; DIWSTOP
	dc.l	$01e42100		; DIWHIGH
	dc.l	$0092003c		; DDFSTRT
	dc.l	$009400d4		; DDFSTOP
	dc.l	$01020000		; BPLCON1
	dc.l	$01080000		; BPL1MOD
	dc.l	$010a0000		; BPL2MOD
	dc.l	$00e00000		; BPL1PTH
	dc.l	$00e20000		; BPL1PTL
	dc.l	$2c01fffe		; Wait for x=0, y=44
	dc.l	$01009200		; BPLCON0
	dc.l	$ffe1fffe		; Wait for x=224, y=255
	dc.l	$2c01fffe		; Wait for x=0, y=44
	dc.l	$01000200		; BPLCON0
	dc.l	$00880000		; Strobe COPJMP1
	dc.l	$fffffffe		; Wait forever
EndCopper
CopperLen	equ EndCopper-Copper


	; Stacks (longword alligned)
	CNOP	0,4
	ds.w	256	; 8Kb - should be enough
UStack:	ds.w	1	; Stack might be full decending
	ds.w	256	; 8Kb - should be enough
_SStack:	ds.w	1	; Stack might be full decending
	ds.w	256	; 8Kb - should be enough
IStack:	ds.w	1	; Stack might be full decending

	END

