***************************************************
* Crappy tests by il Profesore i.e Sven Karlsson  *
***************************************************
* Istari Software 1996                            *
***************************************************
* 
* the gpu/dsp is enough. I think the 68000 was a 
* huge mistake by Atari. Bypass and small caches (1k Unified)
* on the riscs would have been much better...
* Martin should have made the machine he wanted to,,,

	*some includes
	include	d:\testcode\jaguar.inc


	* warning! all gpu/dsp modules must be absolute and
	* inserted in the main module or they will be put in
	* the outfile...
	module	"dsp_boot",jag_dsp
		
	org	$f1b000
	dsp
	
	movei	#dsp_start,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop

dsp_stack
	movei	#i2s_handler,r29
	jump	t,(r29)
	nop
	nop
	nop
	nop

dsp_start
	*setup audio
	movei	#D_FLAGS,r0
	load	(r0),r1
	bset	#3,r1
	bset	#14,r1
	store	r1,(r0)		*turn off all irqs get to regbank 1
				*so we can set up interupts


	movei	#JOYSTICK,r8
	movei	#$100,r9
	storew	r9,(r8)		*fix mute	

	movei	#R_DAC,r8
	moveq	#0,r9
	store	r9,(r8)	
	addq	#4,r8
	store	r9,(r8)		*silence the DACs


	movei	#SCLK,r8
	moveq	#25,r9
	store	r9,(r8)		*set frequency	


	movei	#SMODE,r8
	moveq	#$15,r9
	store	r9,(r8)	

	* setup and move registers to alt reg bank
	* so we don't have to bother about them..
	movei	#sample,r0
	moveta	r0,r2
	moveta	r0,r4

	movei	#sample_end,r0
	moveta	r0,r5
		
	movei	#BG,r6		*for debugging
	moveta	r6,r6
	
	movei	#R_DAC,r0
	moveta	r0,r3

	movei	#dsp_stack,r0
	moveta	r0,r31

	movei	#D_FLAGS,r1
	load	(r1),r0
	bset	#5,r0
	bclr	#3,r0
	store	r0,(r1)		*lets go..

.for_ever
	movefa	r2,r2
	jr	t,.for_ever
	nop

i2s_handler
	*r0 and r1 are used by handler
	*r2 and r3 are pointers
	*this code should be fully software pipelined
	*but im only human as human league used to say...
	
	movei	#D_FLAGS,r30
	load	(r30),r0

	loadw	(r2),r1		*get and play sample
	bset	#14,r0
	bclr	#3,r0
	store	r1,(r3)
	
	addqt	#2,r2
	addqt	#4,r3
	cmp	r2,r5
	store	r1,(r3)
	jr	pl,.skip
	subqt	#4,r3
	move	r4,r2		*reset sample pointer
.skip
	load	(r31),r1
	addqt	#4,r31
	addqt	#2,r1
	bset	#10,r0
	jump	t,(r1)
	store	r0,(r30)


	module	"gpu_boot",jag_gpu
	org	$f03000
	gpu	*not needed because the module is in gpu_mem but
		*its a bit clearer
gpu_start
	movei	#cpu_int,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop		
	movei	#gpu_level_1,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop		
	movei	#gpu_level_2,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop		
	movei	#gpu_level_3,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop		
	movei	#gpu_level_4,r0
	jump	t,(r0)			
	nop
	nop
	nop
	nop		
gpu_b_stack
cpu_int



	movei	#G_FLAGS,r0
	load	(r0),r1
	bset	#3,r1
	bset	#14,r1
	store	r1,(r0)		*turn off all irqs get to regbank 1
	
	movei	#gpu_b_stack,r31 *set stack


	*init video in a nice way...
	movei	#CONFIG,r8
	loadw	(r8),r9
	btst	#4,r9
	jr	eq,.pal
	nop	
	movei	#1409,r0	*ntsc values
	movei	#823,r2
	movei	#241,r4
	movei	#266,r6
	jr	t,.skip_pal
	nop
.pal	
	movei	#1381,r0	*pal values
	movei	#843,r2
	movei	#287,r4
	movei	#322,r6
.skip_pal
	movei	#Screen_width,r9
	storew	r0,(r9)
	addqt	#2,r9
	storew	r4,(r9)
	addqt	#2,r9
	move	r0,r1
	sharq	#1,r1
	sub	r1,r2
	addqt	#4,r2
	subqt	#1,r1
	moveq	#1,r10
	shlq	#10,r10
	or	r10,r1
	movei	#HDE,r10
	storew	r1,(r9)	
	addqt	#2,r9
	storew	r1,(r10)	*HDE
	subqt	#2,r10
	storew	r2,(r10)	*HDB2
	subqt	#2,r10
	storew	r2,(r9)		*hdb
	addqt	#2,r9
	storew	r2,(r10)	*HDB1
	addqt	#VDB-HDB1,r10
	move	r6,r5
	sub	r4,r5

	storew	r5,(r9)		*vdb
	addqt	#2,r9
	storew	r5,(r10)
	addqt	#2,r10
	
	add	r4,r6	
	storew	r6,(r9)		*vde
	moveq	#1,r6
	neg	r6
	storew	r6,(r10)	*VDE




	* copy to dsp and run
	
	movei	#D_CTRL,r8
	moveq	#0,r9
	store	r9,(r8)	
	
	* set pc
	movei	#dsp_start,r9
	movei	#D_PC,r8
	store	r9,(r8)

	movei	#dsp_boot_start,r8
	movei	#((dsp_boot_end-dsp_boot_start)>>3)+1,r10
	movei	#$f1b000,r9
	

.copy_loop
	loadp	(r8),r11
	subq	#1,r10
	storep	r11,(r9)
	addqt	#8,r8
	jr	pl,.copy_loop
	addqt	#8,r9

	
	movei	#D_CTRL,r8
	moveq	#1,r9
	store	r9,(r8)	
	

	*setup object list
	
	movei	#.return,r22	*r31=return adr
	movei	#Build_list,r23
	*jump	t,(r23)
	nop
.return

	*movei	#$f03002,r0
	*movei	#gpu_level_0,r1
	*store	r1,(r0)

	*set irq 0

	*movei	#201,r1
	*movei	#VI,r0
	*storew	r1,(r0)		*give interupt at screen end


*Int_bit	equ	4
*Init_int
	*movei	#G_FLAGS,r1
	*load	(r1),r0
	*bset	#4,r0
	*bset	#Int_bit+1,r0
	*bset	#Int_bit+2,r0
	*bset	#Int_bit+3,r0
	*bset	#Int_bit+4,r0
	*bclr	#3,r0
	*store	r0,(r1)		*give me OP interupts 


	movei	#VMODE,r0
	movei	#$4c1,r1
	storew	r1,(r0)		*turn video on

	movei	#BORD1,r3
	
	movei	#BG,r2
	movei	#$8833,r1	*greyish in cry...
	movei	#0,r0

	storew	r1,(r2)
	storew	r0,(r3)
	addq	#2,r3		
	storew	r0,(r3)		*turn border black...

	movei	#BG,r2
.for2_ever
	addqt	#1,r1
	jr	t,.for2_ever
	storew	r1,(r2)
	nop


	movei	#BG,r2
	movei	#$34ff,r1	*blue
	storew	r1,(r2)



gpu_level_0
gpu_level_1
gpu_level_2
gpu_level_3
gpu_level_4
	

	*movei	#return,r22	*r22=return adr
	*movei	#Build_list,r23
	*jump	t,(r23)
	*nop
return
	*movei	#Screen_vde,r0
	*moveq	#1,r2
	*load	(r0),r1
	movei	#101,r1
	movei	#VI,r0
	*or	r2,r1
	movei	#G_FLAGS,r30
	store	r1,(r0)		*give interupt at screen end
	load	(r30),r0
	
	*bset	#Int_bit+5,r0
	*bset	#Int_bit+5+1,r0
	*bset	#Int_bit+5+2,r0
	*bset	#Int_bit+5+3,r0
	*bset	#Int_bit+5+4,r0

	load	(r31),r1
	*bset	#14,r0
	addqt	#4,r31
	addqt	#2,r1
	*bclr	#3,r0
	jump	t,(r1)		*jump
	*store	r0,(r30)	*set flags
	nop
Build_list
	*don't bother with branch objects and only handles bitmaps
	movei	#Object_list_space,r9
	movei	#.loop,r23
	movei	#Nbr_of_obj,r8
	move	r9,r10
	move	r9,r20
	loadw	(r8),r0		*nbr of objects
	addqt	#2,r8
	addqt	#16,r10		*skip to next bitmap
.loop	load	(r8),r1		*first longword
	moveq	#8,r2
	move	r10,r11
	neg	r2
	addqt	#2,r8
	and	r2,r11		*mask to phrase adr
	load	(r8),r3		*2nd lw

	move	r11,r12
	shrq	#11,r11
	shlq	#21,r12		*fix link adr

	addqt	#2,r8
	or	r11,r1
	load	(r8),r4		*3rd lw
	or	r12,r4
	store	r1,(r9)		*store 1 lw
	*now copy the rest
	addqt	#2,r9	
	load	(r8),r1		*4th lw
	store	r3,(r9)		*st 2nd lw	
	addqt	#2,r9
	subq	#1,r0		*counter
	store	r4,(r9)		*st 3rd lw	
	addqt	#2,r9	
	jump	pl,(r23)
	store	r1,(r9)		*st 4th lw	
	moveq	#4,r0		*stop object
	movei	#OLP,r21	*olp adr
	store	r0,(r9)		*store stop object
	jump	t,(r22)		*return
	store	r20,(r21)	*reset object pointer
	
	
	module	"jaguar_main_mem"
	* only this module will be put in the outfile

	org	$5000
	dc.b	'JAGR'
	dc.w	3
	dc.l	start_of_code
	dc.l	end_of_code-start_of_code
	dc.l	start_of_code
start_of_code
	* code that loads a gpu-rout into gpu ram and runs it....
	* the tired old 68000 will be put to sleep. rest in peace...
	dc.w	$46FC,$2700,$23FC,$0007 
	dc.w	$0007,$00F0,$210C,$23FC 
	dc.w	$0007,$0007,$00F1,$A10C 
	dc.w	$43F9,$00F0,$3000,$2449 
	dc.w	$207C
	dc.l	gpu_boot_start
	dc.w	$2008,$223C
	dc.l	gpu_boot_end
	dc.w	$9280 
	dc.w	$E481,$22D8,$51C9,$FFFC 
	dc.w	$23CA,$00F0,$2110,$23FC 
	dc.w	$0000,$0001,$00F0,$2114 
	dc.w	$4E72,$2700,$60FA
	phrase
gpu_boot_start
	imodule	"gpu_boot"	* this special directive will insert
				* a previously defined absolute module
				* in the current section. Handy for
				* gpu/dsp routs.. CodeAss makes your
				* life simple! 
gpu_boot_end
	phrase
dsp_boot_start
	imodule	"dsp_boot"
dsp_boot_end
Nbr_of_obj	
	dc.w	1
	phrase
Object_list
	/* the object list...
	   hmmm..this comment looks odd..
	   it looks like C...
	   
	   it is!
	*/   
	//  CodeAss handles C++ comments also..
	//  CodaAss: a little more power!
	* only a bitmap object
	dc.l	(jaguar_dat&(-8))<<8
	dc.l	64<<14+52<<3
	dc.l	1
	dc.l	16<<18+1<<15+4<<12+100
		   
	phrase	
jaguar_dat
	include	logo.s
	even
sample	incbin	sound.dat
	even
sample_end
end_of_code
	bss
	even
Screen_width	ds.w	1	* always use these values when programming and
Screen_height	ds.w	1	* you can run your routs on both ntsc and pal
Screen_hde	ds.w	1	*must not be seperated
Screen_hdb	ds.w	1	------""-----
Screen_vdb	ds.w	1	------""-----
Screen_vde	ds.w	1	------""-----
	phrase
Object_list_space
	nolist			*we dont want to see a 250k sample...
	end			*not needed since fileend is detected