; Graz, February 1994

*** StartUp by POSEIDON of ALCATRAZ ***
*** Thanks to MIKE of SPREADPOINT   ***
*** Works on A500 upto A4000/40     ***
*** Written using DEVPAC            ***
	
	Include	CustomRegisterAGA.s

*** Exec ***

LVO_SuperVisor=-30
FindTask=-294
GetMsg=-372
WaitPort=-384
LVO_OldOpenLibrary=-408
LVO_CloseLibrary=-414

*** Grafics ***

ActiView=34
CopInit	=38
LVO_LoadView=-222
LVO_WaitTOF=-270

*** Use this const. for center your screen correct ***

SCR_X=40	PlaneWidth in bytes ( must be a multiple of 4 )
SCR_Y=256	PlaneHeight in lines ( must be multiple of 2 )
PLN=4		# of Planes

*** Writing this const. in the same named registers ***

DIW_START=($29-(SCR_Y-256)/2)*256+$81-(SCR_X*8-320)/2
DIW_STOP=($29+(SCR_Y-256)/2)*256+$c1+(SCR_X*8-320)/2
DDF_START=((($81-(SCR_X*8-320)/2)-17)/2)&$fff8
DDF_STOP=DDF_START+(SCR_X*4-8)

*** This const. gives you the first line of your screen ***
*** very useful for Copper Wait ***

COP0=$29-(SCR_Y-256)/2

*** Some often used macros ***

*** OpenLibrary	Name,Base,Error ***

M_OpenLib 	MACRO
		move.l	4.w,a6
		lea	\1,a1
		jsr	LVO_OldOpenLibrary(a6)
		move.l	d0,\2
		beq	\3
		ENDM

*** CloseLibrary Base ***

M_CloseLib	MACRO
		move.l	4.w,a6
		move.l	\1,a1
		jsr	LVO_CloseLibrary(a6)
		ENDM

*** Try to use Sections for Code and Datas, so there
*** will never be any problem with the cache !!! 

	Section	Programm,CODE

Start	move.l	4.w,a6			WB-startup ( no fucking GURU !!! )
	sub.l	a1,a1
	jsr	FindTask(a6)
	move.l	d0,a4
	tst.l	$ac(a4)
	bne.s	fromCLI

	lea	$5c(a4),a0		get WorkbenchMessage
	jsr	WaitPort(a6)
	lea	$5c(a4),a0
	jsr	GetMsg(a6)

fromCLI	M_OpenLib GfxName,GfxBase,Err
	
	move.l	GfxBase,a6		store old view
	move.l	ActiView(a6),OldView

	move.l	#0,a1			set view to default
	jsr	LVO_LoadView(a6)
	jsr	LVO_WaitTOF(a6)
	jsr	LVO_WaitTOF(a6)		

	lea	$dff000,a5
	move.w 	#$7fff,d0

	move.w	INTENAR(a5),-(a7)	clear all interrupts
	or.w	#$8000,(a7)
	move.w	d0,INTENA(a5)

	move.w  INTREQR(a5),-(a7)
	or.w	#$8000,(a7)
	move.w	d0,INTREQ(a5)

	move.w	DMACONR(a5),-(a7)	clear all DMA channels
	or.w	#$8000,(a7)
	move.w	d0,DMACON(a5)

	clr.l	MyVBR			use VBR register 
	move.l	4.w,a6
	move.w	296(a6),d0			
	btst	#0,d0			if 60010 or higher processor
	beq.s	noVBR

	lea	SuperV,a5		goto supervisormode
	jsr	LVO_SuperVisor(a6)
	bra.s	noVBR	

SuperV	movec	VBR,d0			and get VBR register
	move.l	d0,MyVBR
	rte

noVBR	lea	$dff000,a5
	cmp.b	#$fc,LISAID(a5)		test AGA
	bne.s	NotAGA
	move.w	#$c00,BPLCON3(a5)	reset new BPLCON register to default
	clr.w	FMODE(a5)		reset fetchmode to 16 bit
	
NotAGA	bsr.w	InitInterrupt			
	
*** Init. here your CopperList ***

	lea	$dff000,a5
	move.l	#NewCopperList,COP1LCH(a5)	init. new CopperList
	clr.w	COPJMP1(a5)
	move.w	#DIW_START,DIWSTRT(a5)		set DisplayWindow
	move.w	#DIW_STOP,DIWSTOP(a5)
	move.w	#DDF_START,DDFSTRT(a5)		set DataFetch
	move.w	#DDF_STOP,DDFSTOP(a5)
	move.w	#PLN*$1000+$200,BPLCON0(a5)	init. BitPlaneControl
	clr.l	BPLCON1(a5)

	move.w	#$c020,INTENA(a5)	switch on e.g. Level3Interrupt
	move.w	#$83c0,DMACON(a5)	???

Main	
	
*** Insert here your MainProgramm ***
	
	btst	#6,$bfe001
	bne.s	Main

Exit	bsr.w	DeinitInterrupt
	lea	$dff000,a5	
	move.w	(a7)+,DMACON(a5)	restore DMA
	move.w	(a7)+,INTREQ(a5)	restore interrupts
	move.w	(a7)+,INTENA(a5)
	move.l	GfxBase,a6		restore view
	move.l	OldView,a1
	jsr	LVO_LoadView(a6)
	jsr	LVO_WaitTOF(a6)
	jsr	LVO_WaitTOF(a6)
	move.l	CopInit(a6),COP1LCH(a5)

	M_CloseLib GfxBase
Err	moveq	#0,d0
	rts

*** Use the VBR !!! e.g. Level3Interrupt ***

InitInterrupt
	move.l	MyVBR,a0
	move.l 	$6c(a0),OldInt
	move.l	#NewInterrupt,$6c(a0)
	rts

DeinitInterrupt
	move.l	MyVBR,a0
	move.l 	OldInt,$6c(a0)
	rts

NewInterrupt
	movem.l	d0-d7/a0-a6,-(sp)

*** Insert here your own Interrupt routine ***

	movem.l	(sp)+,d0-d7/a0-a6
	move.w	#$0020,INTREQ+$dff000
	rte

*** Remeber the cache !?! ***

	Section	Variables,DATA
	
GfxName dc.b	"graphics.library",0
	even
GfxBase	dc.l	0
OldView	dc.l	0
MyVBR	dc.l	0
OldInt	dc.l	0

*** Sections for ChipMemory ***

	Section	GFX,DATA_C

NewCopperList	
	dc.l	-2	create your own copperlist

	Section PLANES&BUFFER,BSS_C

Plane	ds.b	SCR_X*SCR_Y*PLN
