*
* By			: Solo/Genetic
* Version               : 1.1
* Release date prog     : 
* misc                  : AGA startup-code
*                       : shuts down the system
*			: handles the cache
*			: resets the display etc..
*
* Call ZooRoPa          : +31-(0)53-4328902 (jep Genetic Whq)
* or e-mail		: solognt@worldonline.nl
*

	incdir	dh1:adventure/

	section	dingges,code

;blitter stuff
LF_A		= $f0
LF_AN		= $0f
LF_B		= $cc
LF_C		= $aa
LF_CN		= $55

CACRF_EnableI	= (1<<0)	;Enable instruction cache
CACRF_EnableD	= (0<<8)	;68030 Enable data cache
CACRF_DBE	= (0<<12)	;68030 Data burst enable
CACRF_CopyBack	= (0<<31)	;Master enable for copyback caches


WaitBlit: macro
	btst	#14,$dff002		;Blitter status
	bne.s	*-8
	btst	#14,$dff002		;Blitter status
	bne.s	*-8
	endm


;	include dh1:routines/startup.s	;pour les icons

;************ SHUT DOWN SYSTEM ***************

START:
	lea	SPbase(pc),a0
	move.l	SP,(a0)

	move.l	4.w,a6

	clr.l	d0
	lea	dosname(pc),a1
	jsr	-552(a6)	; openlib
	lea	DosBase(pc),a0
	move.l	d0,(a0)		; save pointer to dosbase
	beq.w	errordos

	clr.l	d0
	lea	gfxname(pc),a1
	jsr	-552(a6)	; openlib
	lea	gfxbase(pc),a0
	move.l	d0,(a0)		; save pointer to gfxbase
	beq.w	errorgfx
	movea.l	d0,a6

	lea	OldView(pc),a0
	move.l  $22(a6),(a0)	; actiview save old view
	sub.l	a1,a1		; clears full long-word
	jsr	-222(a6)	; LoadView Open a NULL view (resets display
				; on any Amiga)
	jsr	-270(a6)	; WaitTOF Wait twice so that an interlace
	jsr	-270(a6)	; WaitTOF display can reset.

	bsr.w	getVBR		; VBRbase en cache stuff

	move.l	4.w,a6
	cmp.b	#50,530(a6)	; is vblank rate pal?
	beq.b	pal		; yup.
	lea	ntsc(pc),a0
	st	(a0)		; set NTSC flag.
pal:
	lea	$dff000,a6
	move.w	$7c(a6),d0	; AGA register...
	cmpi.b	#$f8,d0		; are we AGA?
	bne.w	not_aga		; no aga so bye bye...

	lea	AGA(pc),a0
	st	(a0)		; set the AGA flag.
	move.w	#0,$1fc(a6)	; reset AGA sprites to normal mode
	move.w	#%0000110000000000,$106(a6)
;not_aga:
	move.l	4.w,a6
	jsr	-132(a6)	;forbit

	lea	$dff000,a6
	
	lea	intena(pc),a0
	move.w	$01c(a6),d0
	or.w	#$C000,d0
	move.w	d0,(a0)
	lea	dmacon(pc),a0
	move.w	$002(a6),(a0)
	or.w	#$8000,(a0)
	lea	adkcon(pc),a0
	move.w	$010(a6),(a0)
	or.w	#$8000,(a0)

	lea	$dff000,a6
	move.w	#$7fff,$09a(a6)
	move.w	#$7fff-$20,$096(a6)
		; 5432109876543210
	move.w	#%1000001111010000,$096(a6)

	move.w	#$7fff,$09c(a6)
	lea	irqbase(pc),a0
	move.l	VBRbase(pc),a1
	move.l	$6c(a1),(a0)


;****************** BEGIN ********************

	bsr	TheDemoCode

;*********** RESTORE SYSTEM ******************

	move.l	VBRbase(pc),a1
	move.l	irqbase(pc),$6c(a1)

	lea	$dff000,a6
	move.l	#$7fff7fff,$9a(a6)	; kill all ints
	move.w	#$7fff,$96(a6)		; kill all dma
	move.w	dmacon(pc),$096(a6)
	move.w	intena(pc),$09A(a6)
	move.w	adkcon(pc),$09E(a6)

	move.l	4.w,a6
	jsr	-138(a6)	;permit


;********** RESTORE COPPERLIST **************

not_aga:
	bsr.b	Restorecache

	move.l	gfxbase(pc),a6	; gfx base
	move.l	OldView(pc),a1	; old WorkBench view
	jsr	-222(a6)	; LoadView Restore the view

	move.l	$4.w,a6
	move.l	gfxbase(pc),a1
	move.l	$26(a1),$dff080
	jsr	-414(a6)	;close lib
errorgfx:
	move.l	$4.w,a6
	move.l	DosBase(pc),a1
	jsr	-414(a6)	;close lib
errordos:
	move.l	SPbase(pc),SP

	moveq.l	#0,d0		;no error code and ready..
	rts

;*************** RESTORE CACHE ***************

Restorecache:				;restore the cache bits
	tst.l	oldcachebits
	beq.s	noturbo
	move.l	4.w,a6
	move.l	oldcachebits(pc),d0
	move.l	#-1,d1		;cache mask
	jsr	-648(a6)	;cache control
noturbo:
	rts

;*************** GET VBR BASE ****************
getVBR:				;get the VBR base and set the cache bits..
	MOVE.L	4.w,A6
	SUB.L	A1,A1
	MOVE.W	$128(A6),D0	;processor flags
	AND.W	#$007F,D0
	BEQ.S	MC68000

	lea	cpu(pc),a0
	clr.w	(a0)
	and.w	#$f,d0		;alleen processor flags
	btst	#2,d0		;MC68020??
	bne.s	noMC68020
	lea	cpu(pc),a0
	move.w	#-1,(a0)
noMC68020:
	move.l	4.w,a6
	cmp.w	#37,(a6)	;check if kick 2.0+
	blo.s	nokick20

	jsr	-636(a6)	;clear data and instr cache

	move.l	#1<<13|CACRF_EnableI|CACRF_EnableD|CACRF_DBE|CACRF_CopyBack,d0	;cache bits
	move.l	#-1,d1		;cache mask
	jsr	-648(a6)	;cache control
	move.l	d0,oldcachebits
nokick20:
	JSR	-120(A6)	; Disable
	JSR	-150(A6)	; SuperState
	MOVEC	VBR,A1		; get the Vector base offset
	lea	cpu(pc),a0	; so we know where the interupts are.
	tst.w	(a0)
;	bne.w	MC68020
;MC68020:
	JSR	-156(A6)	;UserState
	JSR	-126(A6)	;Enable
MC68000:
	lea	VBRbase(pc),a0
	MOVE.L	A1,(a0)
	RTS

oldcachebits	dc.l	0
cpu:		dc.w	0

;*********************************************

dosname:
	dc.b	'dos.library',0
gfxname:
	dc.b	'graphics.library',0
	cnop	0,4

DosBase:	ds.l	1
gfxbase:	ds.l	1
irqbase:	ds.l	1
VBRbase:	ds.l	1
OldView:	ds.l	1
SPbase:		ds.l	1
intena:		ds.w	1
dmacon:		ds.w	1
adkcon:		ds.w	1
ntsc:		dc.w	0
AGA:		dc.w	0

nextpart:	dc.w	0

;************************ YOUR DEMO STUFF DOWN HERE ****************************

mainIRQ:
	movem.l	d0-a6,-(sp)

	movem.l	(sp)+,d0-a6
	move.w	#$0020,$dff09c
	rte


TheDemoCode:
	move.l	VBRbase(pc),a1
	move.l	#mainIRQ,$6c(A1)
	move.w	#$c020,$dff09a

waitmouse:
	btst	#10,$dff016
	bne.s	waitmouse

	rts
