* 	  		$VER: MagicHead 1.8 (05.06.93)			      	
*									      
*                          This is the  -Magic Head-   			      
*    Coded by Argon/Complex ^MoMP^   (Member of Mainzelmaennchen Productions) 
*
*                       for the  C & A  Assembling Tool
*

dmaconr=2
vposr=4
vhposr=6
dskdatr=8
joy0dat=$a
joy1dat=$c
clxdat=$e
adkconr=$10
intenar=$1c
intreqr=$1e
dskpt=$20
dskptl=$22
dsklen=$24
dskdat=$26
vposw=$2a
vhposw=$2c
copcon=$2e
bltcon0=$40
bltcon1=$42
bltafwm=$44
bltalwm=$46
bltcpt=$48
bltcptl=$4a
bltbpt=$4c
bltbptl=$4e
bltapt=$50
bltaptl=$52
bltdpt=$54
bltdptl=$56
bltsize=$58
bltcmod=$60
bltbmod=$62
bltamod=$64
bltdmod=$66
bltcdat=$70
bltbdat=$72
bltadat=$74
dsksync=$7e
cop1lc=$80
cop1lcl=$82
cop2lc=$84
cop2lcl=$86
cop1jmp=$88
cop2jmp=$8a
diwstrt=$8e
diwstop=$90
dffstrt=$92
dffstop=$94
dmacon=$96
clxcon=$98
intena=$9a
intreq=$9c
adkcon=$9e
bpl1pt=$e0
bpl2pt=$e4
bpl3pt=$e8
bpl4pt=$ec
bpl5pt=$f0
bpl6pt=$f4
bpl7pt=$f8
bpl8pt=$fc
bpl1ptl=$e2
bpl2ptl=$e6
bpl3ptl=$ea
bpl4ptl=$ee
bpl5ptl=$f2
bpl6ptl=$f6
bpl7ptl=$fa
bpl8ptl=$fe
bplcon0=$100
bplcon1=$102
bplcon2=$104
bplcon3=$106
bplmod1=$108
bplmod2=$10a
Beamcon0=$1dc
spr0data=$120
spr1data=$14c
spr2data=$154
spr3data=$15c
spr4data=$164
spr5data=$16c
spr6data=$174
spr7data=$17c
col0=$180
col1=$182
col2=$184
col3=$186
col4=$188
col5=$18a
col6=$18c
col7=$18e
col8=$190
col9=$192
col10=$194
col11=$196
col12=$198
col13=$19a
col14=$19c
col15=$19e
col16=$1a0
col17=$1a2
col18=$1a4
col19=$1a6
col20=$1a8
col21=$1aa
col22=$1ac
col23=$1ae
col24=$1b0
col25=$1b2
col26=$1b4
col27=$1b6
col28=$1b8
col29=$1ba
col30=$1bc
col31=$1be
htotal=$1c0
Hsstop=$1c2
hbstrt=$1c4
hbstop=$1c6
vtotal=$1c8
vsstop=$1ca
vbstart=$1cc
hsstart=$1de
vsstart=$1e0
hcenter=$1e2
diwhigh=$1e4
bltsizev=$05c
bltsizeh=$05e
burst=$1fc


;------------------- MACROS

waitm	macro
.SHL_wm\@: btst #6,$bfe001
	  bne.s .SHL_wm\@
	 endm
waitb	macro
.SHL_wb\@: btst #6,$2(a5)
	  bne.s .SHL_wb\@
	 endm
waitmr	macro
.SHL_wmr\@: btst #2,$16(a5)
	  bne.s .SHL_wmr\@
	 endm

	ifd debug
ComeBack macro
	btst	#7,$bfe001
	bne.s	.ncb\@
	trap	#0
.ncb\@:
	endm
SaveRegs macro
	move.l	a0,-(a7)
	move.l	$7c,a0
	move.l	#"REGS",(a0)+
	movem.l	d0-d7,(a0)
	lea	32(a0),a0
	move.l	(a7),(a0)+
	movem.l	a1-a7,(a0)
	move.l	(a7)+,a0
	endm
	endc

Use	macro
	incdir	"code:include/CAtool"
	include	\1
	endm

	IFD	watcher
	jmp	Magic_Startup
	include	"code:MagicWatcher.asm"
dothewatch macro
	movem.l	d0-a6,-(a7)
	lea	$dff000,a5

	move.l	#x_spr1,$120(a5)
	move.l	#x_spr2,$124(a5)
	move.l	#x_spr3,$128(a5)
	move.l	#x_spr4,$12c(a5)
	move.l	#x_spr5,$130(a5)
	move.l	#x_spr6,$134(a5)
	move.l	#x_spr7,$138(a5)
	move.l	#x_spr8,$13c(a5)
	
	move.l	#$0fff0fff,d0
	move.l	d0,$1a0(a5)
	move.l	d0,$1a4(a5)
	move.l	d0,$1a8(a5)
	move.l	d0,$1ac(a5)
	move.l	d0,$1b0(a5)
	move.l	d0,$1b4(a5)
	move.l	d0,$1b8(a5)
	move.l	d0,$1bc(a5)
	
	move.w	#$8020,dmacon(a5)
	
	jsr	X_SWrite

	 movem.l	(a7)+,d0-a6
	endm
	endc


;---------------------------------------
 
	section	MagicHead,code		;need not be in chipram

Magic_Startup:
	ifnd	keepsystem		;Variable vorbereiten
keepsystem set 0
	endc

	movem.l	d0/a0,-(a7)		;save initial values

	sub.l	a1,a1
	move.l  4.w,a6

	ifd	CPU
	ifeq	CPU-1
	move.w	296(a6),Magic_CPU	;AttnFlags
	endc
	endc

	jsr	-294(a6)		;FindTask
	move.l	d0,a4

	ifd	StackSize
	ifeq	Stacksize-1
	move.l	$84(a4),Magic_stacksize	;pr_StackSize
	endc
	endc

	tst.l	$ac(a4)			;pr_cli
	beq.s	fromWorkbench

	movem.l	(a7)+,d0/a0		;restore regs
	bra.s	fromcli		;and run the user prog

fromWorkbench
	lea	$5c(a4),a0		;pr_msgport
	move.l	4.w,a6
	jsr	-384(A6)		;wait for a message
	lea	$5c(a4),a0
	jsr	-372(A6)		;then get it
	move.l	d0,returnMsg		;save it for later reply

	IFD	WBcode
	 IFEQ	WBcode-1		;extra wbstartup-code ?
	  jsr	Magic_Wbcode
	 ENDC
  	ENDC

	movem.l	(a7)+,d0/a0		;restore
Fromcli:
	bsr.s	Magic_init		;do the inialization

	IFD	ReturnCode
	 IFEQ	ReturnCode-1
	  move.l	d0,-(a7)	;save it
	 ENDC
	ENDC

	tst.l	returnMsg
	beq.s	Magic_exit		;if it was a CLI

	move.l	4.w,a6
        jsr	-132(a6)		;forbid

	move.l	returnMsg(pc),a1
	jsr	-378(a6)		;replay message

Magic_exit
	ifd	ReturnCode
	ifeq	Returncode-1
	move.l	(a7)+,d0		;exit code
	endc
	endc
	rts
	even
returnMsg:	dc.l	0
	Ifd	stacksize
	ifeq	stacksize-1
Magic_stacksize: dc.l	0
	endc
	endc
wbview:  dc.l	0
gfxbase: dc.l	0
gfxname: dc.b	"graphics.library",0
	even
Magic_dma:	dc.w	0
Magic_irq:	dc.w	0
Magic_CPU:	dc.w	0
	cnop	0,8
;----------------------------------------
Magic_init:

	move.l	a4,a1
	moveq	#127,d0			; give me the best priprity
	jsr	-300(a6)		; SetTaskPri

	lea	gfxname,a1		; graphics name
	moveq	#0,d0			; any version
	jsr	-552(a6)
	tst.l	d0
	beq.w	Magic_error		; failed to open? Then quit
	move.l	d0,gfxbase
	
	move.l	d0,a6
	move.l	34(a6),wbview		; store old view

.loop	sub.l	a1,a1			; clear a1
	jsr 	-222(a6) 		; LoadView
	jsr	-270(a6)	 	; WaitTOF
	jsr	-270(a6) 		; WaitTOF
	nop
        cmp.l	#0,34(a6)   		; Any other view appeared?
	bne.s	.loop			; If so wipe it.
	
	ifd forcepal
	ifeq forcepal-1
	move.l	4.w,a6
	cmp.b	#60,530(a6)		;VBlankFrequency
	bne.s	.pal
	cmp.b	#60,531(a6)		;PowerSupplyFrequency
	bne.s	.pal
	move.w	#$32,$dff1dc		;Beamcon0 -> pal !
.pal:
	endc
	endc

	ifeq	keepsystem-0
	lea	$dff000,a5		;Not nice but sometimes necessary
	waitb
	move.w	dmaconr(a5),d0
	move.w	intenar(a5),d1
	move.w	#$7fff,intena(a5)	;kill it ! -snif 
	move.w	#$7fff,dmacon(a5)
	or.w	#$8000,d0
	move.w	d0,magic_dma
	or.w	#$8000,d1
	move.w	d1,magic_irq
	move.w	#$8240,dmacon(a5)
	endc

	ifeq	keepsystem-1
	jsr	Main			;Call the relevant code!
.relb:	btst	#6,$bfe001
	beq.s	.relb
	endc
	ifeq	keepsystem-0
	move.l	$80,-(a7)
	lea	Magic_visor(pc),a0
	move.l	a0,$80
	trap	#0			;Call by exception - the hard way.
	move.l	(a7)+,$80
	endc

	ifeq	keepsystem-0
	lea	$dff000,a5
	waitb
	move.w	magic_dma(pc),d0
	move.w	Magic_irq(pc),d1
	move.w	#$7fff,dmacon(a5)
	move.w	#$7fff,intena(a5)
	move.w	d0,dmacon(a5)
	move.w	d1,intena(a5)		;revive it 
	endc

	move.l	wbview(pc),a1		;Closedown again
	move.l	gfxbase(pc),a6
	jsr	-222(a6) 		;Load old view again

	move.l	38(a6),$dff080	 	;Restore old copper

	move.l	a6,a1
	move.l	4.w,a6
	jsr	-414(a6) 		;CloseLibrary

	rts				;quit to WB/CLI
Magic_error:
	moveq	#-1,d0			;set return flag ...
	rts  
Magic_visor:
	ifd	debug			;The master needs help ?
	move.l	$8,.Magic_oBusErr	;Store the old values
	move.l	$c,.Magic_oAdrErr	
	move.l	$10,.Magic_oIglErr
	move.l	$14,.Magic_oDbzErr
	move.l	$28,.Magic_oLnaErr
	move.l	$2c,.Magic_oLnfErr
	move.l	$6c,.Magic_ovbi		;Store old interrupt vectors
	move.l	$68,.Magic_oser
	move.l	$7c,a0
	move.l	#"-OK-",(a0)
	lea	.Magic_BusErr(pc),a0	;Set new handlers
	move.l	a0,$8
	lea	.Magic_AdrErr(pc),a0
	move.l	a0,$c
	lea	.Magic_IglErr(pc),a0
	move.l	a0,$10
	lea	.Magic_DbzErr(pc),a0
	move.l	a0,$14
	lea	.Magic_LnaErr(pc),a0
	move.l	a0,$28
	lea	.Magic_LnfErr(pc),a0
	move.l	a0,$2c
	lea	.Magic_Trap(pc),a0	;set trap handler
	move.l	a0,$80
	move.l	a7,.stack		;for reviving !
	lea	.Magic_vbi(pc),a0
	move.l	a0,$6c
	move.w	#$c020,$dff09a
	IFD watcher
	moveq	#0,d0			;SpriteWriter Position 
	moveq	#10,d1
	jsr	X_SetWriterPos
	endc
	jsr	Main			;no stack trouble please !
.relb2:	btst	#6,$bfe001
	beq.s	.relb2
	move.l	a0,.save	
	move.l	$7c,a0
	cmp.l	#"REGS",(a0)		;allready saved reg's ?
	beq.s	.return
	move.l	#"REGS",(a0)+		;no! then save them now !
	movem.l	d0-d7,(a0)
	lea	32(a0),a0
	move.l	.save(pc),(a0)+
	movem.l	a1-a7,(a0)
.return:nop
	move.l	.Magic_oBusErr(pc),$8	;restore the old values
	move.l	.Magic_oAdrErr(pc),$c
	move.l	.Magic_oIglErr(pc),$10
	move.l	.Magic_oDbzErr(pc),$14
	move.l	.Magic_oLnaErr(pc),$28
	move.l	.Magic_oLnfErr(pc),$2c
	move.l	.Magic_ovbi(pc),$6c	
	move.l	.Magic_oser(pc),$68
	nop
	rte     
.Magic_VBI:
	ComeBack
	ifd 	watcher
	dothewatch
	endc
	move.w	#$0020,$dff09c
	rte
.back:	move.l	.stack(pc),a7
	bra	.return
.Magic_BusErr:				;The Error handlers
	move.w	#$7fff,$dff09a
	move.l	a0,.save
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#1,(a0)+
.regs:
	movem.l	d0-d7,(a0)		;save the reg's
	lea	32(a0),a0
	move.l	.save(pc),(a0)+
	movem.l	a1-a7,(a0)
	bra	.back
.Magic_AdrErr:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#2,(a0)+
	bra	.regs
.Magic_IglErr:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#3,(a0)+
	bra	.regs
.Magic_DbzErr:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#4,(a0)+
	bra	.regs
.Magic_LnaErr:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#5,(a0)+
	bra	.regs
.Magic_LnfErr:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"ERR!",(a0)+
	move.w	#6,(a0)+
	bra	.regs
.Magic_Trap:
	move.w	#$7fff,$dff09a
	move.l	$7c,a0
	move.l	#"TRAP",(a0)+
	bra	.regs
    
	cnop	0,4
.stack:		dc.l	0
.save:		dc.l	0
.Magic_oBusErr:	dc.l	0
.Magic_oAdrErr:	dc.l	0
.Magic_oIglErr:	dc.l	0
.Magic_oDbzErr:	dc.l	0
.Magic_oLnaErr:	dc.l	0
.Magic_oLnfErr:	dc.l	0
.Magic_ovbi:	dc.l	0
.Magic_oser:	dc.l	0
	cnop	0,8
	endc
	ifnd	debug
	jsr	Main			;no stack trouble please !
.relb3:	btst	#6,$bfe001
	beq.s	.relb3
	nop
	rte         
	cnop	0,8
	endc
	
	IfD	watcher
	SECTION "b0",bss_c

x_spr1:		ds.w	8+(high*4)
x_spr2:		ds.w	8+(high*4)
x_spr3:		ds.w	8+(high*4)
x_spr4:		ds.w	8+(high*4)
x_spr5:		ds.w	8+(high*4)
x_spr6:		ds.w	8+(high*4)
x_spr7:		ds.w	8+(high*4)
x_spr8:		ds.w	8+(high*4)

	section	"ww",data_c
SW_font:
	incbin	"code:rawgfx/800*9*2font.raw"
	endc
	section	"x",code
