
	;note this code need iram or sdCard startup 

;*********************************************************************	
; includes
;*********************************************************************

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"
	.include "include\ucore.i"
	
;*********************************************************************	
; start
;*********************************************************************
	
spacePilotCore	

	cli
		
	;setup video
		
	dexti	>setupVideo
	movei	r0,setupVideo
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	;setup audio
	
	dexti	>initAudio
	movei	r0,initAudio
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;setup irq vector
	
	dexti	>irq
	movei	r0,irq	
	nop
	setssr	r0,2
	
	;setup audio irq
	
	movei	r0,UcIrqMsk
	movei	r1,$6			;enable audio irq and timer 2
	moveih	r0,>UcIrqMsk
	nop
	st	r0,r1			;
	
	;set audio dac counter = 2 (96/3 = 32KHz)
	
	movei	r6,UcAudioDACBase
	nop
	moveih	r6,>UcAudioDACBase
	movei	r1,$2
	movei	r0,0
	moveih	r1,$2	;address 2
	
	
	stinc	r6,r0	;dac left high = 0
	nop
	stinc	r6,r0	;dac left low = 0
	nop
	stinc	r6,r0	;dac right high = 0
	nop
	stinc	r6,r0	;dac right low = 0	
	nop
	st	r6,r1	;dac address/ctrl = $0201	;dac counter = 2
		
	;init module
	
	dexti	>modChannels
	movei	r4,modChannels
	dexti	>modInit
	movei	r2,modInit
	gpci	r7,2		;next 4 + X
	jmpi	r2,0
	movei	r0,modData	;delay slot
	movei	r1,>>modData	;delay slot
	moveih	r0,>modData	;delay slot
	moveih	r1,>>>modData	;delay slot
	
	;start music
	
	dexti	>modStart
	movei	r1,modStart
	gpci	r7,2		;next 4 + X
	jmpi	r1,0		;start
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;enable irqs
	
	sei
		
	;*************************************** welcome start
		
welcomeStart		

	;reset data
	
	dexti	>resetData
	movei	r0,resetData
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	;set welcome stone count
	
	movei	r6,gfxObjectStoneCount
	movei	r0,gfxObjectStoneCountMax
	moveih	r6,>gfxObjectStoneCount
	nop
	st	r6,r0
	
	;reset PS2 buffer
	
	gpci	r7,2	;next 4 + X
	br	PS2Reset
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;*************************************** welcome loop
	
welcomeLoop

	;frame switch
	
	dexti	>switchAndClearFrame
	movei	r0,switchAndClearFrame
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;not in game mode
	
	dexti	>inGame
	movei	r1,inGame
	movei	r0,0
	nop
	st	r1,r0
	
	;draw object (stone)
	
	dexti	>drawStones
	movei	r0,drawStones
	gpci	r7,2
	jmpi	r0,0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	;move and check stones
	
	dexti	>moveAndCheckStones
	movei	r0,moveAndCheckStones
	gpci	r7,2
	jmpi	r0,0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop
	
	;draw welcome message
	
	dexti	>drawWelcomeMessage
	movei	r0,drawWelcomeMessage
	gpci	r7,2
	jmpi	r0,0	
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop
	
	;debug output
	
	dexti	>debugOutput
	movei	r0,debugOutput
	gpci	r7,2
	jmpi	r0,0	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;process keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;test space key
	
	gpci	r7,2		;next 4 + X
	br	PS2TestKey
	movei	r0,$29		;space key	delay slot	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,1		;space pressed?
	brtc	welcomeLoop	;no welcome again
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;***************************************
	
	;reset data
	
	dexti	>resetData
	movei	r0,resetData
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	;*************************************** game loop
	
gameLoop

	;switch and clear frames
	
	dexti	>switchAndClearFrame
	movei	r0,switchAndClearFrame
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;in game mode
	
	dexti	>inGame
	movei	r1,inGame
	movei	r0,1
	nop
	st	r1,r0

	;process keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;draw object (stone)
	
	dexti	>drawStones
	movei	r0,drawStones
	gpci	r7,2
	jmpi	r0,0	
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	;update ship coordinates
	
	dexti	>moveAnimShip
	movei	r0,moveAnimShip
	gpci	r7,2
	jmpi	r0,0	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;draw and test object (ship)
	
	dexti	>clipAndTestAndDrawObject
	movei	r0,clipAndTestAndDrawObject
	gpci	r7,2
	jmpi	r0,0				;return r1 != 0 collsion
	movei	r6,gfxObjectShip		;delay slot
	nop					;delay slot
	moveih	r6,>gfxObjectShip		;delay slot
	nop					;delay slot
	
	dexti	>checkAndDrawShipExplosion
	movei	r0,checkAndDrawShipExplosion
	gpci	r7,2
	jmpi	r0,0		
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	;play sound on collision
	
	dexti	>playCrashSound
	movei	r0,playCrashSound
	gpci	r7,3
	cmpeqi	r1,0				;r1 != 0 -> playCrashSound
	jmptc	r0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	
	;check and process collision
	
	dexti	>checkAndProcessCollision
	movei	r0,checkAndProcessCollision
	gpci	r7,2
	jmpi	r0,0	
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop
	
	push	r0	;store collsiom value
		
	;draw energie bar
	
	dexti	>drawEnergieBar
	movei	r0,drawEnergieBar
	gpci	r7,2
	jmpi	r0,0	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;update and draw status
	
	dexti	>updateAndDrawStatus
	movei	r0,updateAndDrawStatus
	gpci	r7,2
	jmpi	r0,0	
	movei	r2,1	;update timer delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;game over?
	
	rqpop
	nop
	nop
	pop	r0
	nop
	cmpeqi	r0,0	;game over energie out
	brts	gameOver
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
	;move and check stones
	
	dexti	>moveAndCheckStones
	movei	r0,moveAndCheckStones
	gpci	r7,2
	jmpi	r0,0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop
	
	;debug output
	
	dexti	>debugOutput
	movei	r0,debugOutput
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	br	gameLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;************************ game over
	
gameOver

	dexti	>switchAndClearFrame
	movei	r0,switchAndClearFrame
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;not in game mode
	
	dexti	>inGame
	movei	r1,inGame
	movei	r0,0
	nop
	st	r1,r0

	;draw object (stone)
	
	dexti	>drawStones
	movei	r0,drawStones
	gpci	r7,2
	jmpi	r0,0	
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	;draw energie bar
	
	dexti	>drawEnergieBar
	movei	r0,drawEnergieBar
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;update and draw status
	
	dexti	>updateAndDrawStatus
	movei	r0,updateAndDrawStatus
	gpci	r7,2
	jmpi	r0,0
	movei	r2,0		;dont update paly timer 	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;draw and test object (ship)
	
	dexti	>clipAndTestAndDrawObject
	movei	r0,clipAndTestAndDrawObject
	gpci	r7,2
	jmpi	r0,0				;return r1 != 0 collsion
	movei	r6,gfxObjectShip		;delay slot
	nop					;delay slot
	moveih	r6,>gfxObjectShip		;delay slot
	nop		
	
	dexti	>checkAndDrawShipExplosion
	movei	r0,checkAndDrawShipExplosion
	gpci	r7,2
	jmpi	r0,0	
	movei	r1,1				;do explode delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot	

	;move ship out of screen
	
	dexti	>moveShipOut
	movei	r0,moveShipOut
	gpci	r7,2
	jmpi	r0,0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot	
	
	;draw game over
	
	gpci	r7,2	;next 4 + X
	br	drawTextFormated
	movei	r0,stringGameOver	;text	delay slot	
	movei	r1,18			;x      delay slot
	moveih	r0,>stringGameOver	;       delay slot
	movei	r2,6			;y
		
		
	dexti	>frameCounter
	movei	r0,frameCounter
	nop
	rqldi	r0,0
	nop
	movei	r1,$8
	ld	r0
	nop
	and	r0,r0,r1
	nop
	
	cmpeqi	r0,0
	brtc	skipPressSpaceTextAtGameOver
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
		
	gpci	r7,2	;next 4 + X
	br	drawTextFormated
	movei	r0,stringGameOver2	;text	delay slot	
	movei	r1,11			;x      delay slot
	moveih	r0,>stringGameOver2	;       delay slot
	movei	r2,7			;y
	
skipPressSpaceTextAtGameOver	
	
	;move and check stones
	
	dexti	>moveAndCheckStones
	movei	r0,moveAndCheckStones
	gpci	r7,2
	jmpi	r0,0
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop
	
	;wait for space
	
waitForGameOverKeyPressed
	
	;process keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;test space key
	
	gpci	r7,2		;next 4 + X
	br	PS2TestKey
	movei	r0,$29		;space key	delay slot	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	cmpeqi	r0,1	;space pressed
	brtc	gameOver	;not pressed
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
waitForGameOverKeyRelease
	
	;process keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;test space key
	
	gpci	r7,2		;next 4 + X
	br	PS2TestKey
	movei	r0,$29		;space key	delay slot	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	cmpeqi	r0,1	;space pressed
	brts	waitForGameOverKeyRelease	;pressed
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
		
	;goto welcome start
	
	br	welcomeStart
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
end	br	end
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;*********************************************************************
;* functions (near)
;*********************************************************************
	
;******************************
;*
;* drawChar
;* 
;* r0 	 	value
;* r1		x 	
;* r2		y
;*
;* trash r0

drawChar

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
		
	getssr	r7,0	;get ucore status reg 0 (irq 0 = irq enabled)
	cli		;stop irqs
	push	r7
	
	
	;destination base (sram base 0)
	
	muli	r1,8	;x * 8
	
	dexti	>drawFrame
	movei	r4,drawFrame
	
	nop
	rqldi	r4,1	;low
	rqldi	r4,0	;high
	nop
	ld	r4	;low
	ld	r5	;high
	
	add	r4,r4,r1	;address+x
	addtqi	r5,r5,0
	
	dexti	$17
	movei	r1,$80	;(360*16)*16
	nop
	
	mul	r1,r1,r2	;*y
	gmulhi	r2
	
	add	r4,r4,r1	;address+(y*360+16)
	addt	r5,r5,r2
		
	;font source
	
	dexti	>fontAddress
	movei	r2,fontAddress
	
	nop
	rqldi	r2,1	;low
	rqldi	r2,0	;high
	nop
	ld	r2	;low
	ld	r3	;high
		
	muli	r0,8		;value * 8
	nop
	add	r2,r2,r0	;add to source 
	addtqi	r3,r3,0		;

	movei	r0,15		;16 [y]
dcYloop	
	
	esadr	r3,r2
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	addi	r2,8		
	addtqi	r3,r3,0	
	
	movei	r1,7		;8 [x]

dcXloop
	
	eld	r6
	esadr	r5,r4
	extri	r6,15		
	estts	r6,0
	
	subi	r1,1		;x-
	brts	dcXloop
	addi	r4,1	;delay slot
	addtqi	r5,r5,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
		
	movei	r1,$70
	nop
	moveih	r1,$1		;r1 = 360+16-8
	nop
	
	add	r4,r4,r1	;dest + 360+16-8
	addtqi	r5,r5,0		;
	
	movei	r1,$e8
	nop
	moveih	r1,$2		;r1 = 752-8
	nop	
	
	subi	r0,1		;y-
	brts	dcYloop
	add	r2,r2,r1	;source + 752-8		delay slot
	addtqi	r3,r3,0		;			delay slot
	nop			;delay slot
	nop			;delay slot
	
	;restore irq enable
	
	rqpop
	nop
	nop
	pop	r7
	nop
	setssr	r7,0	;write back
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r5
	
	rqpop
	rqpop
	nop
	pop	r4
	pop	r3
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;*********************************************************************
;* included functions
;*********************************************************************

	.include	"components\drawText.s"	
	.include	"components\ps2keyboard.s"
	.include	"components\i2c.s"
	
	;mod and audio
	
	.include	"components\audioChannel.s"
	.include	"components\modSample.s"
	.include	"components\modPattern.s"
	.include	"components\audioPeriode.s"
	.include	"components\mod.s"
	.include	"components\modEffects.s"
	.include	"components\modEEffects.s"
		
;*********************************************************************
;* functions
;*********************************************************************

;***********************************************************************
;* irq code start
;***********************************************************************
	
irq
	cli
	
	;save all register
	
	epushsadrl
	epushsadrh
	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;check type of irq
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0		;memory
	nop
	extri	r0,1		;timer 2
	gpci	r7,2		
	brts	irqTimer2	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0		;memory
	nop
	extri	r0,2		;dac
	gpci	r7,2		
	brts	irqDAC	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;restore all used register and go back
	
	rqpop		
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop		
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop		
	rqpop
	rqpop
	pop	r1
        pop	r0
	epopsadrh
	
	rqpop
	nop
	nop
	epopsadrl	;delay slot
	
	rti
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	sei		;delay slot
	
;***********************************************************************
;* irq timer 2
;***********************************************************************
		
irqTimer2

	push	r0
	push	r7
	
	;mod tick
	
	
	dexti	>modTick
	movei	r0,modTick
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;clear irq 2 timer
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	movei	r1,2		;timer 2
	ld	r2		;memory
	nop
	bic	r2,r2,r1
	nop
	st	r0,r2		;
	
	;back
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	jmpi	r7,0
	nop		;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;***********************************************************************
;* irq audio
;***********************************************************************
	
irqDAC
	;irq begin
	
	push	r7
	
	;push leds on
	
	movei	r0,UcRSwichtWLedG
	movei	r1,$ff
	moveih	r0,>UcRSwichtWLedG
	moveih	r1,$ff
	nop
	st	r0,r1	
		
	
	;copy 96 values from channel 0	

	movei	r7,channel0
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel0Buffer	;out buffer
	moveih	r7,>channel0		;data struct
	moveih	r6,>channel0Buffer
	
	;copy 96 values from channel 1	

	movei	r7,channel1
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel1Buffer	;out buffer
	moveih	r7,>channel1		;data struct
	moveih	r6,>channel1Buffer
	
	;copy 96 values from channel 2	

	movei	r7,channel2
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel2Buffer	;out buffer
	moveih	r7,>channel2		;data struct
	moveih	r6,>channel2Buffer
	
	;copy 96 values from channel 3	

	movei	r7,channel3
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel3Buffer	;out buffer
	moveih	r7,>channel3		;data struct
	moveih	r6,>channel3Buffer
	
	;copy 96 values from channel 4	

	movei	r7,channel4
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel4Buffer	;out buffer
	moveih	r7,>channel4		;data struct
	moveih	r6,>channel4Buffer
	
	;copy 96 values from channel 5	

	movei	r7,channel5
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel5Buffer	;out buffer
	moveih	r7,>channel5		;data struct
	moveih	r6,>channel5Buffer
	
	;push dac data
	
	movei	r1,channel0Buffer
	movei	r2,channel1Buffer
	movei	r3,channel2Buffer
	movei	r4,channel3Buffer
	movei	r5,channel4Buffer
	movei	r6,channel5Buffer
	moveih	r1,>channel0Buffer
	moveih	r2,>channel1Buffer
	moveih	r3,>channel2Buffer
	moveih	r4,>channel3Buffer
	moveih	r5,>channel4Buffer
	moveih	r6,>channel5Buffer
	
	movei	r0,95			;95 values to DAC
	
fillDACBuffer

	push	r1
	push	r2
	push	r3
	push	r4

	rqldi	r1,0
	rqldi	r2,0
	rqldi	r3,0
	ld	r1
	ld	r2
	ld	r3
	add	r1,r1,r2
	nop
	add	r1,r1,r3
	
	rqldi	r4,0
	rqldi	r5,0
	rqldi	r6,0
	ld	r4
	ld	r2
	ld	r3
	add	r4,r4,r2
	movei	r2,UcAudioDACBase
	add	r4,r4,r3			
	moveih	r2,>UcAudioDACBase		
	movei	r3,0			;r3 = zero
		
	stinc	r2,r1	;dac left high = value
	movei	r7,$1
	stinc	r2,r3	;dac left low = 0
	moveih	r7,$1
	stinc	r2,r4	;dac right high = value
	nop
	stinc	r2,r3	;dac right low = 0
	nop
	st	r2,r7	;dac address/ctrl = $0101	;ctrl = enable audio
	
	rqpop	
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	addi	r6,1
	addi	r5,1
	pop	r1

	subi	r0,1
	brts	fillDACBuffer
	addi	r1,1		;delay slot						
	addi	r4,1		;delay slot
	addi	r3,1		;delay slot
	addi	r2,1		;delay slot
	
	;push leds off
	
	movei	r7,UcRSwichtWLedG
	movei	r6,$0
	moveih	r7,>UcRSwichtWLedG
	nop
	st	r7,r6	
	
	;clear stored audio irq
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	movei	r1,4		;dac
	ld	r2		;memory
	nop
	bic	r2,r2,r1
	nop
	st	r0,r2		;
	
	;restore all used register and go back
	
	rqpop		
	nop
	nop	
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* initAudio
;*
;* return
;*
;* r0 = 0 (PASS)
;*

initAudio	

	push	r7
		
	movei	r6,audioDefault	
	movei	r5,2			;y cursor	
	moveih	r6,>audioDefault
	nop	
		
initAudioI2CWriteLoop
	
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r2		;address
	ld	r3		;data
	
	cmpeqi	r2,$ff		;stop at sentinel ($ff)
	brts	initAudioI2CWriteFinish
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
			
	gpci	r7,2		;next 4 + X
	br	i2cWM8731WriteWord	;r2 = address;	r3 = data; return r0 = result
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	br	initAudioI2CWriteLoop
	addi	r6,2		;next lut address	delay slot
	addi	r5,1		;next y counter		delay slot
	nop			;delay slot
	nop			;delay slot

	movei	r0,0
	
initAudioI2CWriteFinish	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* i2cWM8731WriteWord
;*
;* r2 = address
;* r3 = data
;* 
;* return:
;*
;* r0 = result
;*

i2cWM8731WriteWord

	push	r4
	push	r1
	push	r6
	push	r7
	
	;start
	
	dexti	>i2cSendStart
	movei	r4,i2cSendStart
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	movei	r6,UcI2CSoftPortBCtrl		;delay slot
	nop					;delay slot
	moveih	r6,>UcI2CSoftPortBCtrl		;I2C ports b	;delay slot
	nop					;delay slot
		
	;write id
	
	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	movei	r1,$34		;WRITE			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$01		;delay slot
	nop			;delay slot
	
	;write address
	
	add	r1,r2,r2	;adr*2
	extri	r3,8
	addtqi	r1,r1,0		;+ bit8 of data
	
	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$02		;delay slot
	nop			;delay slot

	;write data low

	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	or	r1,r3,r3	;WRITE		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$04		;delay slot
	nop			;delay slot
	
	;finished without fail
	
	movei	r0,0		;all fine
	
	;stop
	
i2cSend8WACKFailsWr

	dexti	>i2cSendStop
	movei	r4,i2cSendStop
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cWaitHalfPeriode
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	dexti	>i2cWaitHalfPeriode
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cWaitHalfPeriode
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cWaitHalfPeriode
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r1

	rqpop
	jmpi	r7,0
	nop			;delay slot
	pop	r4		;delay slot
	nop			;delay slot
	nop			;delay slot	


;******************************
;*
;* playCrashSound
;* 
	
playCrashSound

	push	r0
	push	r1
	push	r5
	push	r6
	push	r7

	;disable irq if enabled temporary

	getssr	r7,0	;get ucore status reg 0 (irq 0 = irq enabled)
	cli		;stop irqs
	push	r7
	
	;set sample (channel0)

	gpci	r7,2
	br	setCrashSample	
	dexti	>channel0	;delay slot
	movei	r6,channel0	;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;set sample (channel5)

	gpci	r7,2
	br	setCrashSample	
	dexti	>channel5	;delay slot
	movei	r6,channel5	;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;restore irq enable
	
	rqpop
	nop
	nop
	pop	r7
	nop
	setssr	r7,0	;write back
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r5
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* setCrashSample
;* 	
;* r6 = channel (struct)
;*
	
setCrashSample

	push	r7

	dexti	>channelSetSampleAddress
	movei	r5,channelSetSampleAddress
	gpci	r7,2
	jmpi	r5,0
	movei	r0,crashSampleData	;delay slot
	movei	r1,>>crashSampleData	;delay slot
	moveih	r0,>crashSampleData	;delay slot
	moveih	r1,>>>crashSampleData	;delay slot
	
	dexti	>channelSetRepeatStart
	movei	r5,channelSetRepeatStart
	gpci	r7,2
	jmpi	r5,0
	movei	r0,$87	;delay slot
	nop		;delay slot
	moveih	r0,$08	;delay slot
	nop		;delay slot
	
	dexti	>channelSetRepeatLength
	movei	r5,channelSetRepeatLength
	gpci	r7,2
	jmpi	r5,0
	movei	r0,$97	;delay slot
	nop		;delay slot
	moveih	r0,$08	;delay slot
	nop		;delay slot
	
	dexti	>channelSetSampleLength
	movei	r5,channelSetSampleLength
	gpci	r7,2
	jmpi	r5,0
	movei	r0,$b9	;delay slot
	nop		;delay slot
	moveih	r0,$08	;delay slot
	nop		;delay slot
	
	dexti	>channelSetSampleStart
	movei	r5,channelSetSampleStart
	gpci	r7,2
	jmpi	r5,0
	movei	r0,$0	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	dexti	>channelSetVolume
	movei	r5,channelSetVolume
	gpci	r7,2
	jmpi	r5,0
	movei	r0,$40	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	dexti	>channelSetPeriode
	movei	r5,channelSetPeriode
	gpci	r7,2
	jmpi	r5,0
	movei	r0,113	;B-3	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* moveShipOut
;* 

moveShipOut

	dexti	>gfxObjectShip
	movei	r6,gfxObjectShip
	nop
	
	rqldi	r6,1	;y
	nop
	nop
	ld	r0	;y
	
	addi	r6,1
	addi	r0,1
	movei	r1,230
	cmplosi	r0,230
	movets	r0,r0,r1

	jmpi	r7,0
	st	r6,r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

;******************************
;*
;* checkAndDrawShipExplosion (not optimized ..)
;* 
;* r1 	 	!= 0 draw explosion
;*

checkAndDrawShipExplosion

	push	r1
	push	r7
	
	cmpeqi	r1,0
	brts	noExplosionToDraw
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;test and increment anim count
	
	dexti	>gfxAnimCounterExplosion
	movei	r5,gfxAnimCounterExplosion
	
	nop
	rqldi	r5,0
	nop
	nop
	ld	r0
	movei	r1,$3
	addi	r0,1
	nop
	and	r0,r0,r1
	nop
	st	r5,r0
	
	;copy anim to object
	
	movei	r5,gfxObjectExplosionAnim
	muli	r0,4				
	moveih	r5,>gfxObjectExplosionAnim
	movei	r6,gfxObjectExplosion
	add	r5,r5,r0				;source
	moveih	r6,>gfxObjectExplosion			;destination
	rqldi	r5,0	;w
	rqldi	r5,1	;h
	rqldi	r5,2	;x
	ld	r0
	ld	r1
	ld	r2
	rqldi	r5,3	;y
	nop
	addi	r6,2
	ld	r3
	stinc	r6,r0
	nop
	stinc	r6,r1
	nop
	stinc	r6,r2
	nop
	stinc	r6,r3
	nop
	
	
	
	;copy x , y from real space ship
	
	movei	r6,gfxObjectShip
	movei	r5,gfxObjectExplosion
	moveih	r6,>gfxObjectShip
	moveih	r5,>gfxObjectExplosion
	
	rqldi	r6,0	;x
	rqldi	r6,1	;y
	nop
	ld	r0
	ld	r1
	
	subi	r0,6	;x adjust
	subi	r1,2	;y adjust
	
	stinc	r5,r0
	nop
	stinc	r5,r1
	
	;draw it
	
	gpci	r7,2
	br	clipAndDrawObject	;return r1 != 0 collsion
	movei	r6,gfxObjectExplosion		;delay slot
	nop					;delay slot
	moveih	r6,>gfxObjectExplosion		;delay slot
	nop	
	
	;epilog
	
noExplosionToDraw
	
	rqpop
	rqpop
	nop
	pop	r7
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot



;******************************
;*
;* drawWelcomeMessage
;*

drawWelcomeMessage

	push	r7

	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome1	;text	delay slot	
	movei	r1,5			;x      delay slot
	moveih	r0,>stringWelcome1		;       delay slot
	movei	r2,2			;y
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome2	;text	delay slot	
	movei	r1,9			;x      delay slot
	moveih	r0,>stringWelcome2		;       delay slot
	movei	r2,4			;y
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome2b	;text	delay slot	
	movei	r1,12			;x      delay slot
	moveih	r0,>stringWelcome2b		;       delay slot
	movei	r2,5			;y
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome3	;text	delay slot	
	movei	r1,11			;x      delay slot
	moveih	r0,>stringWelcome3		;       delay slot
	movei	r2,7			;y
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome4	;text	delay slot	
	movei	r1,11			;x      delay slot
	moveih	r0,>stringWelcome4		;       delay slot
	movei	r2,8			;y
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome5	;text	delay slot	
	movei	r1,11			;x      delay slot
	moveih	r0,>stringWelcome5		;       delay slot
	movei	r2,9			;y
	
	dexti	>frameCounter
	movei	r0,frameCounter
	nop
	rqldi	r0,0
	nop
	movei	r1,$8
	ld	r0
	nop
	and	r0,r0,r1
	nop
	
	cmpeqi	r0,0
	brtc	skipPressSpaceText
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringWelcome6	;text	delay slot	
	movei	r1,12			;x      delay slot
	moveih	r0,>stringWelcome6		;       delay slot
	movei	r2,11			;y
	
skipPressSpaceText	
	
	rqpop	
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

;******************************
;*
;* updateAndDrawStatus
;*
;* r2 = update playtimer (1 = yes, 0 is no)
;*

updateAndDrawStatus

	push	r7
	
	;update timer
	
	dexti	>playTimeCounter
	movei	r1,playTimeCounter
	
	nop
	rqldi	r1,0
	nop
	nop
	ld	r0
	nop
	add	r0,r0,r2
	nop
	st	r1,r0
	
	moveih	r0,0	;and $ff
	nop
	cmpeqi	r0,$ff
	brtc	noStoneIncrease
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;add one stone
	
	dexti	>gfxObjectStoneCount
	movei	r1,gfxObjectStoneCount
	
	nop
	rqldi	r1,0
	nop
	nop
	ld	r0
	nop
	cmpeqi	r0,gfxObjectStoneCountMax
	brts	noStoneIncrease
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r0,1
	nop
	st	r1,r0
	
noStoneIncrease	
	
	;draw status
	
	dexti	>playTimeCounter
	movei	r1,playTimeCounter
	
	nop
	rqldi	r1,0
	nop
	nop
	ld	r0
	nop
	movei	r2,$89	;pseudo div 60
	nop
	moveih	r2,$88
	nop
	mul	r0,r0,r2
	nop
	gmulhi	r0
	nop
	lsri	r0,5
	nop
	
	
	push	r0
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringTime	;text	delay slot	
	movei	r1,1		;x      delay slot
	moveih	r0,>stringTime	;       delay slot
	movei	r2,1		;y
	
	rqpop
	nop
	nop
	nop
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringPower	;text	delay slot	
	movei	r1,1		;x      delay slot
	moveih	r0,>stringPower	;       delay slot
	movei	r2,0		;y      delay slot	
	
	;meteorids
	
	dexti	>gfxObjectStoneCount
	movei	r1,gfxObjectStoneCount
	nop
	rqldi	r1,0
	nop
	nop
	ld	r0
	nop
	addi	r0,1
	nop
	
	push	r0
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2	;next 4 + X
	jmpi	r0,0
	movei	r0,stringMeteoroids	;text	delay slot	
	movei	r1,33			;x      delay slot
	moveih	r0,>stringMeteoroids	;       delay slot
	movei	r2,1			;y
	
	rqpop
	nop
	nop
	nop
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot


;******************************
;*
;* moveAndCheckStones
;*

moveAndCheckStones

	push	r7
	
	dexti	>gfxObjectStoneCount	
	movei	r3,gfxObjectStoneCount	
	
	nop
	rqldi	r3,0
	nop
	nop
	ld	r7		;r7 is stone counter
	nop
	
moveAndCheckStoneLoop	
	
	push	r7
	
	;
	
	dexti	>gfxObjectStones
	movei	r5,gfxObjectStones
	nop
	add	r5,r5,r7	;+ counter
	nop
	rqldi	r5,0
	nop
	movei	r6,8
	ld	r5		;stone address
	nop
	add	r6,r5,r6	;stone address + 8
	nop
	
	;r5 = stone low
	;r6 = stone high
	
		;										0		1	2	3	4		
		;	0	1	2	3	4	5	6	7		8		9	a	b	c			
		;	x	y	w	h	sx	sy	srclow	srchigh		yspeed	S1	cox	coy	colsize	yintern	
	
	;add yspeed to y intern
	
	rqldi	r6,0
	rqldi	r6,4
	nop
	ld	r0	;y speed
	ld	r1	;y intern
	addqi	r4,r6,4	;y intern
	add	r1,r1,r0
	nop
	st	r4,r1	;store to y intern
	
	;check of screen and recalc (r1 = yintern)
	
	dexti	$3
	movei	r0,$ff
	nop

	cmplos	r1,r0	;yintern < 256*4-1
	brts	inScreen
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;rand y = 0-[(0..255)+50]*4
	
	gpci	r7,2
	br	rand			;r0 = 0..65535
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,200
	lsri	r0,6	;
	movei	r2,0
	add	r0,r0,r1
	addqi	r4,r6,4	;y intern
	sub	r2,r2,r0
	nop
	st	r4,r2	;store to y intern
	
	;rand x
	
	gpci	r7,2
	br	rand			;r0 = 0..65535
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	or	r1,r0,r0
	lsri	r0,8	;0..255
	lsri	r1,10	;0..63
	nop
	add	r1,r1,r0	
	
	gpci	r7,2
	br	rand			;r0 = 0..65535
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	lsri	r0,11	;0..31
	nop
	add	r1,r1,r0	;r1 = new x value
	nop
	st	r5,r1
	
	;rand yspeed 
	
	gpci	r7,2
	br	rand			;r0 = 0..65535
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	lsri	r0,13	;0..7
	nop
	addi	r0,4	;start at one pixel
	nop
	st	r6,r0	;yspeed
	
	;.stop
	
inScreen	
	
	;convert y intern to y
	
	rqldi	r6,4
	nop
	nop
	ld	r0
	addqi	r4,r5,1	;y address
	asri	r0,2	;/4
	nop
	st	r4,r0
	
	;
	
	rqpop
	nop
	nop
	pop	r7
	nop
		
	
	subi	r7,1
	brts	moveAndCheckStoneLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* checkAndProcessCollision
;*
;* r1 != 0 collision detected
;*
;* return 
;*
;* r0 = 0 game over
;*

checkAndProcessCollision	
	
	push r7
	
	movei	r2,1
	movei	r3,0
	cmpeqi	r1,0	
	movets	r4,r3,r2
	
	;sub form energie bar
	
	dexti	>Energie
	movei	r6,Energie	
	nop
	
	rqldi	r6,0
	nop
	nop
	ld	r2
	nop
	cmpeqi	r2,0
	brts	checkAndProcessEnergieOutDetected
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	sub	r2,r2,r4
	nop
	st	r6,r2
		
	movei	r0,1	;no collision 
	
checkAndProcessCollisionEnd
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
checkAndProcessEnergieOutDetected

	br	checkAndProcessCollisionEnd
	movei	r0,0	;collision  delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* drawStones
;*

drawStones

	push	r7

	dexti	>gfxObjectStoneCount	
	movei	r3,gfxObjectStoneCount		
	nop
	
	rqldi	r3,0
	nop
	nop
	ld	r3
	nop

stoneLoop	
	
	push	r3
	
	dexti	>gfxObjectStones
	movei	r6,gfxObjectStones
	
	nop
	add	r6,r6,r3
	nop
	rqldi	r6,0
	nop
	nop
	ld	r6	;
	
	;
	
	gpci	r7,2
	br	clipAndDrawObject
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	rqpop
	nop
	nop
	pop	r3
	nop
	
	subi	r3,1
	brts	stoneLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* drawEnergieBar
;*
	
drawEnergieBar	

	push	r7

	movei	r3,drawFrame
	movei	r6,Energie
	moveih	r3,>drawFrame
	moveih	r6,>Energie
	
	rqldi	r3,0	;high
	rqldi	r3,1	;low
	movei	r4,$8
	ld	r3	;high
	ld	r2	;low
			
	rqldi	r6,0
	nop
	moveih	r4,$9		;y offset (x = 7*8; y = 6)
	ld	r6		;energie value
	nop
	cmpeqi	r6,0		;no energie
	brts	drawEnergieBarExit
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	add	r2,r2,r4	;+xstart +ystart
	addtqi	r3,r3,0
			
	dexti	$1
	movei	r4,$78		;360 + 16
	
drawEnergieBarLoop	
	
	or	r1,r3,r3
	or	r0,r2,r2
	nop
	
	;load first to prevent latency
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	esadr	r1,r0
	erqldi	0
	add	r0,r0,r4
	addtqi	r1,r1,0
	nop
	
	or	r1,r3,r3
	or	r0,r2,r2
	nop
	
	;store 
	
	movei	r7,$0f
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
	
	movei	r7,$3f
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
	
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
	
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
	
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
	
	movei	r7,$0f
	esadr	r1,r0
	eld	r5
	add	r0,r0,r4
	or	r5,r5,r7	;no t out
	addtqi	r1,r1,0
	est	r5,0
		
	addi	r2,1
	addtqi	r3,r3,0
	
	
	subi	r6,1
	brts	drawEnergieBarLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

drawEnergieBarExit	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* checkCollision (spheric collision detection, not used yet)
;*
;* r5 = object a
;* r6 = object b
;*
;* return
;*
;* r4 = 1 if collosion is detcted else 0
;*

checkCollision	

	push	r1
	push	r2
	push	r3
	
	;obj a
	
	rqldi	r6,0	;x
	rqldi	r6,1	;y
	addi	r6,8	;next 8
	ld	r0
	ld	r1
	rqldi	r6,1
	rqldi	r6,2
	rqldi	r6,3
	ld	r2	;cx
	ld	r3	;cy
	ld	r4	;radius
	
	add	r2,r2,r0	;total x
	add	r3,r3,r1	;total y
	
	push	r2
	push	r3
	push	r4
	
	;obj b
	
	rqldi	r5,0	;x
	rqldi	r5,1	;y
	addi	r5,8	;next 8
	ld	r0
	ld	r1
	rqldi	r5,1
	rqldi	r5,2
	rqldi	r5,3
	ld	r4	;cx
	ld	r3	;cy
	ld	r2	;radius
	
	add	r0,r4,r0	;total x
	add	r1,r3,r1	;total y
	
	rqpop
	rqpop
	rqpop
	pop	r5
	pop	r4
	pop	r3
	
	;obja 	r0 = x  r1 = y r2 = r
	;objb   r3 = x  r4 = y r5 = r
	
	;test collision
	
	movei	r6,0		
	add	r2,r2,r5	;ra + rb
	sub	r0,r0,r3	;dx
	sub	r1,r1,r4	;dy
	
	sub	r3,r6,r0	;-dx
	sub	r4,r6,r1	;-dy
	cmplosi	r0,0		;dx<0?
	movets	r0,r3,r0	;dx = -dx else dx = dx (abs)
	cmplosi	r1,0		;dy<0?
	movets	r1,r4,r1	;dy = -dy else dy = dy (abs)
	
	;overflow prevention (dx or dy > 128 is never a collision)
	
	cmploi	r0,128
	brtc	checkCollisionOverflow
	movei	r4,0	;mark as no collision
	nop		;delay slot	
	nop		;delay slot	
	nop		;delay slot	
	
	cmploi	r1,128
	brtc	checkCollisionOverflow
	movei	r4,0	;mark as no collision
	nop		;delay slot	
	nop		;delay slot	
	nop		;delay slot	
	
	;calculate difference
	
	mul	r0,r0,r0	;dx*dx	(note: unsigned mul)
	mul	r1,r1,r1	;dy*dy
	mul	r2,r2,r2	;r*r
	add	r1,r0,r1	;dx*dx + dy*dy
	
	movei	r3,1		;
	movei	r4,0
	cmplo	r2,r1		;dx*dx + dy*dy < r*r
	movets	r4,r4,r3	;r4 = 1 if collosion is detcted


checkCollisionOverflow
	
	rqpop	
	rqpop	
	rqpop	
	pop     r3
	pop     r2
	pop     r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	
;******************************
;*	
;* debugOutput
;*

debugOutput
	
	push	r7

	dexti	>UcRSwichtWLedG
	movei	r0,UcRSwichtWLedG
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,0
	brtc	debugModeDisabled
	movei	r0,UcVideoRVCountWHSYNC		;		delay slot
	nop					;		delay slot
	moveih	r0,>UcVideoRVCountWHSYNC	;vblank address	delay slot
	nop					;		delay slot
	
	rqldi	r0,0	;vcount
	nop
	nop
	ld	r0
	nop
	subi	r0,30
	nop
	lsri	r0,2	;/4
	
	dexti	1
	movei	r1,$78	;width
	
	movei	r3,drawFrame
	mul	r0,r1,r0	;r0 = low
	moveih	r3,>drawFrame
	gmulhi	r1		;r1 = high 
	
	rqldi	r3,0	;high
	rqldi	r3,1	;low
	nop
	ld	r3	;high
	ld	r2	;low
	
	nop
	add	r2,r2,r0	;low
	addt	r3,r3,r1	;high
	movei	r0,$e0
	addi	r2,8		;first 8 pixels are invisible
	addtqi	r3,r3,0
	moveih	r0,$ff
	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	
debugModeDisabled	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
;******************************
;*	
;* screenClipObject
;*
;* r6 = object in
;* (will ever store in object tmp)
;*
;* return r0 == 0 (out of view dont draw)
;*

screenClipObject

	push	r7
	
	;copy not calculated part to temp struct
	
	rqldi	r6,6	;source low
	rqldi	r6,7	;source high
	movei	r2,gfxObjectTemp
	ld	r0
	ld	r1
	moveih	r2,>gfxObjectTemp
	nop
	addi	r2,6
	nop
	st	r2,r0	;source low
	addi	r2,1
	nop
	st	r2,r1	;source high
		
	;************ x test
	
	rqldi	r6,0	
	rqldi	r6,2	
	rqldi	r6,4	
	ld	r3	;dest x
	ld	r4      ;width
	ld	r5      ;source x
	
	;clip left
	
	cmplosi	r3,0	;r3<0
	brtc	screenClipXLowInRange
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	add	r4,r4,r3	;width + (-x) 
	sub	r5,r5,r3	;source - (-x)
	movei	r3,0		;x = 0
	
	;width <= 7 (<8) -> out of view
	
	cmplosi	r4,8
	brts	screenClipOutOfView
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
screenClipXLowInRange

	;clip right
	
	movei	r0,$78
	nop
	moveih	r0,1	;360+16
	nop
	
	add	r1,r3,r4	;r1 = width + x
	nop
	cmplo	r1,r0		;width + x < 360+16
	brts	screenClipXHighInScreen
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	sub	r4,r0,r3	;width = 360+16 - x
	nop
	
	cmplosi	r4,8		;
	brts	screenClipOutOfView
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
screenClipXHighInScreen	
	
	;store back (x, width, source x)

	dexti	>gfxObjectTemp
	movei	r0,gfxObjectTemp	
	nop
	
	st	r0,r3
	addi	r0,2
	nop
	st	r0,r4
	addi	r0,2
	nop
	st	r0,r5
	
	;************ y test
	
	rqldi	r6,1	
	rqldi	r6,3	
	rqldi	r6,5	
	ld	r3	;dest y
	ld	r4      ;hight
	ld	r5      ;source y
	
	;clip up
	
	cmplosi	r3,0	;r3<0
	brtc	screenClipYLowInRange
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	add	r4,r4,r3	;height + (-y) 
	sub	r5,r5,r3	;source - (-y)
	movei	r3,0		;y = 0
		
	cmplosi	r4,1		;y < 1
	brts	screenClipOutOfView
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

screenClipYLowInRange	

	;clip down
	
	movei	r0,$e0	;total_height = 224
	nop
	
	add	r1,r3,r4	;r1 = height + y
	nop
	cmplo	r1,r0		;height + y < total_height
	brts	screenClipYHighInScreen
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	sub	r4,r0,r3	;height = total_height+16 - y
	nop
	
	cmplosi	r4,1		;
	brts	screenClipOutOfView
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
screenClipYHighInScreen

	;store back (y, hight, source y)

	dexti	>gfxObjectTemp
	movei	r0,gfxObjectTemp	
	nop
	
	addi	r0,1
	nop
	st	r0,r3
	addi	r0,2
	nop
	st	r0,r4
	addi	r0,2
	nop
	st	r0,r5
	
	;all in view
	
	movei	r0,1

	
screenClipEnd
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
screenClipOutOfView

	br	screenClipEnd
	movei	r0,0	;out of view delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	
;******************************
;*
;* moveAnimShip
;*
;* reads from keyboard / move ship / anim ship
;*
	
moveAnimShip	

	push	r7
	
	;check keys and set shipMoveStore
	
	movei	r4,0		;OURL
	
	;left
	
	dexti	>PS2TestKey
	movei	r0,PS2TestKey
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,251		;left arrow	delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	add	r4,r4,r0	;if pressed
	
	;right
	
	dexti	>PS2TestKey
	movei	r0,PS2TestKey
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,$04		;right arrow	delay slot
	nop			;delay slot
	moveih	r0,1				;delay slot
	nop			;delay slot
	
	muli	r0,2
	nop
	add	r4,r4,r0	;if pressed
	
	;up
	
	dexti	>PS2TestKey
	movei	r0,PS2TestKey
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,$05		;up arrow	delay slot
	nop			;delay slot
	moveih	r0,1				;delay slot
	nop			;delay slot
	
	muli	r0,8
	nop
	add	r4,r4,r0	;if pressed
	
	;down
	
	dexti	>PS2TestKey
	movei	r0,PS2TestKey
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,$02		;down arrow	delay slot
	nop			;delay slot
	moveih	r0,1			;delay slot
	nop			;delay slot
	
	muli	r0,4
	nop
	add	r4,r4,r0	;if pressed
	
	;store for later processing
	
	dexti	>shipMoveStore
	movei	r3,shipMoveStore
	nop
	st	r3,r4
	
	;now real update ship coords

	movei	r3,shipMoveStore
	movei	r4,gfxObjectShip
	moveih	r3,>shipMoveStore
	moveih	r4,>gfxObjectShip
	
	rqldi	r3,0	
	rqldi	r4,0
	rqldi	r4,1
	ld	r0	;ship OURL
	ld	r1	;x
	ld	r2	;y
	
	extri	r0,0
	brtc	shipNotLeft
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	subi	r1,1

shipNotLeft	

	extri	r0,1
	brtc	shipNotRight
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1

shipNotRight	

	extri	r0,2
	brtc	shipNotDown
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r2,1

shipNotDown	

	extri	r0,3
	brtc	shipNotUp
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	subi	r2,1

shipNotUp	

	;check x y ranges
	
	movei	r3,0
	cmplosi	r1,0		;x<0
	movets	r1,r3,r1	
	
	movei	r3,0
	cmplosi	r2,0		;y<0
	movets	r2,r3,r2
	
	movei	r3,$50		;360+8-40=335=$150
	nop
	moveih	r3,$1
	nop
	cmplo	r1,r3		;x=>335
	movets	r1,r1,r3
	
	
	movei	r3,$b5		;225-44=$b5
	nop
	cmplo	r2,r3		;x=>181
	movets	r2,r2,r3


	;store back
	
	st	r4,r1	;x
	addi	r4,1	
	nop
	st	r4,r2	;y
	
	;animate (timer/counter updates)
	
	dexti	>gfxAnimCounterShip
	movei	r3,gfxAnimCounterShip
	nop
	rqldi	r3,0
	nop
	nop
	ld	r1	;anim counter   0..2
	nop
	
	cmpeqi	r1,2	;anim overflow
	brtc	noAnimUpdate
	addi	r1,1	;delay slot
	nop             ;delay slot
	nop             ;delay slot
	nop             ;delay slot
		
	movei	r1,0	;anim counter to 0
	
noAnimUpdate	
	nop
	st	r3,r1	;store anim counter
	
	;update ship object struct with correct source x,y,w,h from animation counter
	
	movei	r2,gfxObjectShip
	movei	r3,gfxAnimCounterShip
	movei	r5,shipMoveStore
	movei	r4,gfxObjectShipAnimNormal
	moveih	r2,>gfxObjectShip
	moveih	r3,>gfxAnimCounterShip
	moveih	r5,>shipMoveStore
	moveih	r4,>gfxObjectShipAnimNormal
	
	
	;check left right up down anim
	
	rqldi	r5,0
	nop
	nop
	ld	r5	;UDRL
	
	;normal left right
	;UD = 0 	 
	;L  = 12
	;R  = 24
	
	movei	r1,0	;normal
	movei	r0,12	;left
	extri	r5,0	;left	?
	movets	r6,r0,r1
	
	movei	r0,24	;right
	extri	r5,1	;right	?
	movets	r6,r0,r6
	
	;	
	
	rqldi	r3,0
	addi	r2,2	;w/h/x/y offset
	nop
	ld	r0	;animcounter
	nop
	muli	r0,4	;4 word per data
	nop
	add	r0,r0,r6	;left right offset
	nop
	add	r4,r4,r0	;add to anim offset
	nop
	rqldi	r4,0
	rqldi	r4,1
	rqldi	r4,2
	ld	r0	;w
	ld	r1	;h
	ld	r5	;x
	
	rqldi	r4,3
	nop
	nop
	ld	r6	;y
	
	
	st	r2,r0
	addi	r2,1
	nop
	
	st	r2,r1
	addi	r2,1
	nop
	
	st	r2,r5
	addi	r2,1
	nop
	
	st	r2,r6
	
	;
	
	rqpop	
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
			
;******************************
;*
;* clipAndDrawObject
;*
;* r6 = object
;*

clipAndDrawObject

	push	r7
	
	;srceen clip object
	
	gpci	r7,2
	br	screenClipObject
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	cmpeqi	r0,0	;out of view
	brts	skipObjectDraw
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;temp object (is generated by clipping)
	
	dexti	>gfxObjectTemp
	movei	r6,gfxObjectTemp				
	nop
	
	;start calculate and draw
	
	;dest address
	
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r0		;x
	ld	r1		;y
	
	movei	r2,$78
	movei	r3,drawFrame
	moveih	r2,$1	
	moveih	r3,>drawFrame
	
	mul	r1,r1,r2	;y * (360+16)
	gmulhi	r2		;r2:r1
	add	r1,r1,r0
	addtqi	r2,r2,0		;r2:r1 = y * (360+16) +x
	
	rqldi	r3,1		;draw frame low
	rqldi	r3,0		;draw frame  high
	nop
	ld	r0		;low
	ld	r3		;high
		
	add	r1,r1,r0
	addt	r2,r2,r3	;r2:r1 + dest frame
	nop
	
	push	r1
	push	r2
	
	;source address
	
	rqldi	r6,6
	rqldi	r6,7
	nop
	ld	r1		;low source addr
	ld	r0		;high source addr
	
	rqldi	r6,4		;source x
	rqldi	r6,5		;source y
	nop
	ld	r2		;x
	ld	r3		;y
	
	movei	r4,0
	nop
	moveih	r4,1
	nop
	mul	r3,r3,r4
	nop
	add	r3,r3,r2	;+x
	gmulhi	r5
	
	add	r1,r1,r3
	addt	r0,r0,r5
	nop
	
	push	r1		;source adr high
	push	r0		;source adr low
		
	;source / dest offset
	
	rqldi	r6,3
	rqldi	r6,2
	nop
	ld	r3		;height
	ld	r2		;width
	
	
	movei	r5,$78	
	movei	r4,$00
	moveih	r5,$01		;dest width = 360+16 = 376
	moveih	r4,$01		;source width = 256
	
	lsri	r2,3		;width / 8
	nop
	muli	r2,8		;*8 (witdh is only setable by mult of 8)
	nop
	sub	r1,r5,r2	;total dest width - object width
	sub	r0,r4,r2	;total source width - object width
	
	
	push	r1		;dest offset
	push	r0		;source offset
	
	;setup width and height	
	
	
	lsri	r2,3		;width / 8
	nop			;
	subi	r2,1		;width / 8 -1
	subi	r3,1		;height -1
	
	push	r2		;width
	push	r3		;height
	
	;draw it
	
	gpci	r7,2
	br	copyMasked
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
		
	;
	
skipObjectDraw
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* clipAndTestAndDrawObject
;*
;* r6 = object
;*

clipAndTestAndDrawObject

	push	r7
	
	;srceen clip object
	
	gpci	r7,2
	br	screenClipObject
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	cmpeqi	r0,0	;out of view
	brts	skipClipAndTestAndDrawObject
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;temp object (is generated by clipping)
	
	dexti	>gfxObjectTemp	
	movei	r6,gfxObjectTemp			
	nop
	
	;start calculate and draw
	
	;dest address
	
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r0		;x
	ld	r1		;y
	
	movei	r2,$78
	movei	r3,drawFrame
	moveih	r2,$1	
	moveih	r3,>drawFrame
	
	mul	r1,r1,r2	;y * (360+16)
	gmulhi	r2		;r2:r1
	add	r1,r1,r0
	addtqi	r2,r2,0		;r2:r1 = y * (360+16) +x
	
	rqldi	r3,1		;draw frame low
	rqldi	r3,0		;draw frame  high
	nop
	ld	r0		;low
	ld	r3		;high
		
	add	r1,r1,r0
	addt	r2,r2,r3	;r2:r1 + dest frame
	nop
	
	push	r1
	push	r2
	
	;source address
	
	rqldi	r6,6
	rqldi	r6,7
	nop
	ld	r1		;low source addr
	ld	r0		;high source addr
	
	rqldi	r6,4		;source x
	rqldi	r6,5		;source y
	nop
	ld	r2		;x
	ld	r3		;y
	
	movei	r4,0
	nop
	moveih	r4,1
	nop
	mul	r3,r3,r4
	nop
	add	r3,r3,r2	;+x
	gmulhi	r5
	
	add	r1,r1,r3
	addt	r0,r0,r5
	nop
	
	push	r1		;source adr high
	push	r0		;source adr low
		
	;source / dest offset
	
	rqldi	r6,3
	rqldi	r6,2
	nop
	ld	r3		;height
	ld	r2		;width
	
	
	movei	r5,$78	
	movei	r4,$00
	moveih	r5,$01		;dest width = 360+16 = 376
	moveih	r4,$01		;source width = 256
	
	lsri	r2,3		;width / 8
	nop
	muli	r2,8		;*8 (witdh is only setable by mult of 8)
	nop
	sub	r1,r5,r2	;total dest width - object width
	sub	r0,r4,r2	;total source width - object width
	
	
	push	r1		;dest offset
	push	r0		;source offset
	
	;setup width and height	
	
	
	lsri	r2,3		;width / 8
	nop			;
	subi	r2,1		;width / 8 -1
	subi	r3,1		;height -1
	
	push	r2		;width
	push	r3		;height
	
	;draw it
	
	gpci	r7,2
	br	copyMaskedAndTest
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
		
	;
	
skipClipAndTestAndDrawObject
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* copyMasked
;*	
;* sp+0 height-1
;* sp+1 width/8-1
;* sp+2 source offset
;* sp+3 dest offset
;* sp+4 source adr high
;* sp+5 source adr low
;* sp+6 dest adr high
;* sp+7 dest adr low
;*	

copyMasked

	push	r7
	getsp	r7,0
	nop
	addi	r7,1	;go to data offset
	nop
	
	rqldi	r7,4	
	rqldi	r7,5
	rqldi	r7,6
	ld	r4	;source low
	ld	r3	;source high
	ld	r6	;dest high
	rqldi	r7,7
	nop
	nop
	ld	r5	;dest low
	
	
copyMaskedy	

	rqldi	r7,1	;width
	nop
	nop
	ld	r2	;
	
copyMasked8x	

	esadr	r4,r3
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	esadr	r6,r5
	
	eld	r0
	eld	r1
	extri	r0,15
	estts	r0,0
	extri	r1,15
	estts	r1,1
	
	eld	r0
	eld	r1
	extri	r0,15
	estts	r0,2
	extri	r1,15
	estts	r1,3
	
	eld	r0
	eld	r1
	extri	r0,15
	estts	r0,4
	extri	r1,15
	estts	r1,5
	
	eld	r0
	eld	r1
	extri	r0,15
	estts	r0,6
	extri	r1,15
	estts	r1,7
	
	subi	r2,1
	brts	copyMasked8x
	addi	r3,8		;delay slot
	addtqi	r4,r4,0		;delay slot
	addi	r5,8		;delay slot
	addtqi	r6,r6,0		;delay slot
	
	;
	
	rqldi	r7,2	;source offset
	rqldi	r7,3	;dest offset
	rqldi	r7,0	;hight
	ld	r0
	ld	r1
	ld	r2
	
	add	r3,r3,r0	;add source x offset	
	addtqi	r4,r4,0		
	
	subi	r2,1		;height --
	brts	copyMaskedy
	st	r7,r2		;store to hight			delay slot
	add	r5,r5,r1	;add dest x offset		delay slot
	addtqi	r6,r6,0		;delay slot
	nop			;delay slot
	
	;clear stack
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* copyMaskedAndTest
;*	
;* sp+0 height-1
;* sp+1 width/8-1
;* sp+2 source offset
;* sp+3 dest offset
;* sp+4 source adr high
;* sp+5 source adr low
;* sp+6 dest adr high
;* sp+7 dest adr low
;*
;* return
;*
;* r1 = is dest and source pixel of (not 0 if there is a pixel to pixel collision)
;*	

copyMaskedAndTest

	push	r7
	getsp	r7,0
	movei	r1,0	;pixel to pixel collision
	addi	r7,1	;go to data offset
	nop
	
	rqldi	r7,4	
	rqldi	r7,5
	rqldi	r7,6
	ld	r4	;source low
	ld	r3	;source high
	ld	r6	;dest high
	rqldi	r7,7
	nop
	nop
	ld	r5	;dest low
	
	
copyMaskedAndTesty	

	rqldi	r7,1	;width
	nop
	nop
	ld	r2	;
	
copyMaskedAndTest8x	

	esadr	r4,r3
	erqldi	0
	esadr	r6,r5
	erqldi	0
	esadr	r4,r3
	erqldi	1
	esadr	r6,r5
	erqldi	1
	esadr	r4,r3
	erqldi	2
	esadr	r6,r5
	erqldi	2
	esadr	r4,r3
	erqldi	3
	esadr	r6,r5
	erqldi	3
	esadr	r4,r3
	erqldi	4
	esadr	r6,r5
	erqldi	4
	esadr	r4,r3
	erqldi	5
	esadr	r6,r5
	erqldi	5
	esadr	r4,r3
	erqldi	6
	esadr	r6,r5
	erqldi	6
	esadr	r4,r3
	erqldi	7
	esadr	r6,r5
	erqldi	7
	

	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,0
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data

	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,1
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,2
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,3
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,4
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,5
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,6
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	
	eld	r0		;bob
	nop
	extri	r0,15
	estts	r0,7
	eld	r0		;back
	nop
	orts	r1,r0		;or bg if bob has valid data
	
	dexti	$40
	movei	r0,$00
	nop
	and	r1,r1,r0	;only $40 is stones
	
	subi	r2,1
	brts	copyMaskedAndTest8x
	addi	r3,8		;delay slot
	addtqi	r4,r4,0		;delay slot
	addi	r5,8		;delay slot
	addtqi	r6,r6,0		;delay slot
	
	;
	
	rqldi	r7,2	;source offset
	rqldi	r7,0	;hight
	nop
	ld	r0
	ld	r2
	
	add	r3,r3,r0	;add source x offset	
	addtqi	r4,r4,0	

	rqldi	r7,3	;dest offset
	
	subi	r2,1	;hight --
	brts	copyMaskedAndTesty
	ld	r0		;dest offset			delay slot
	st	r7,r2		;store to hight			delay slot
	add	r5,r5,r0	;add dest x offset		delay slot
	addtqi	r6,r6,0		;delay slot
	
	
	;
	
	add	r1,r1,r1	;push out msb
	
	;clear stack
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	
;******************************
;*
;* switchAndClearFrame
;*	
;*

switchAndClearFrame	
	
	push	r7
	
	;wait vpos (930)

	gpci	r7,2	;next 4 + X
	br	waitVPos
	movei	r0,$a1	;delay slot
	nop		;delay slot
	moveih	r0,$3	;delay slot
	nop		;delay slot
	
	;switch frames
	
	gpci	r7,2	;next 4 + X
	br	switchFrames
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;set display address to video
	
	movei	r0,displayFrame	
	movei	r1,UcVideoMEMStartAdrLow
	movei	r2,UcVideoMEMLatch
	moveih	r0,>displayFrame
	moveih	r1,>UcVideoMEMStartAdrLow	;low memstart
	moveih	r2,>UcVideoMEMLatch		;latch out
	
	rqldi	r0,1
	rqldi	r0,0
	nop
	ld	r3
	ld	r4
	
	
	addi	r3,8	;offset
	nop

	st	r1,r3
	addi	r1,1
	nop
	st	r1,r4
	st	r2,r7		;latch out
	
	;clear draw frame
	
	gpci	r7,2	
	br	drawBackground
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* extCopy8
;*
;* r1:r0 source
;* r3:r2	dest
;* r4 count - 1 (8 words per count)
;* 
		
extCopy8	

	push	r5
	push	r6
	
extCopy8Loop
	esadr	r1,r0
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	esadr	r3,r2
	eld	r5
	eld	r6
	est	r5,0
	est	r6,1
	eld	r5
	eld	r6
	est	r5,2
	est	r6,3
	eld	r5
	eld	r6
	est	r5,4
	est	r6,5
	eld	r5
	eld	r6
	est	r5,6
	est	r6,7
	

	subi	r4,1
	brts	extCopy8Loop
	addi	r0,8		;delay slot
	addtqi	r1,r1,0		;delay slot
	addi	r2,8		;delay slot
	addtqi	r3,r3,0		;delay slot
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r6	;delay slot
	pop	r7     	;delay slot
	nop		;delay slot
	nop		;delay slot
			
;******************************
;*
;* switchFrames
;*

switchFrames

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5

	movei	r0,displayFrame
	movei	r1,drawFrame
	moveih	r0,>displayFrame	
	moveih	r1,>drawFrame
	
	rqldi	r0,0
	rqldi	r0,1
	nop
	ld	r2
	ld	r3
	
	rqldi	r1,0
	rqldi	r1,1
	nop
	ld	r4
	ld	r5

	st	r0,r4
	addi	r0,1
	nop
	st	r0,r5
	
	st	r1,r2
	addi	r1,1
	nop
	st	r1,r3
	
	;increment frame counter
	
	dexti	>frameCounter
	movei	r0,frameCounter
	nop
	rqldi	r0,0
	nop
	nop
	ld	r1
	nop
	addi	r1,1
	nop
	st	r0,r1
	
	rqpop
	rqpop
	rqpop
	pop	r5
	pop	r4
	pop	r3
	
	rqpop
	rqpop
	rqpop
	pop	r2
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
			
;******************************
;*
;* waitVPos
;*
;* r0 pos
;* 

waitVPos	
	
	push	r1
	push	r2
	
	;check emulator to prevent face freez .....
	
	dexti	>UuLedR
	movei	r1,UuLedR	;emulator
	nop
	rqldi	r1,0
	nop
	nop
	ld	r2
	nop
	cmpeqi	r2,1
	brts	waitVEmulator
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
waitVRealHardWare
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount		
	nop
		
waitV	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmpeq	r0,r2
	brtc	waitV
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
waitVEmulator
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount	
	nop	
		
waitVE	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmplo	r0,r2
	brts	waitVE
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;delay slot
	nop		;delay slot
	nop		;delay slot	
		
;******************************
;*
;* rand (optimzed)
;*
;* return r0 = result 
;*

rand

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5

	movei	r5,randSeed
	movei	r0,$39
	moveih	r5,>randSeed
	movei	r1,$6d
	movei	r2,$c6
	rqldi	r5,0
	rqldi	r5,1	
	moveih	r0,$30		;r0 = 12345
	ld	r3		;low seed
	ld	r4		;high seed
	
	moveih	r1,$4e
	moveih	r2,$41		;r2:r1 = $41c6 4e6d = 1103515245

	mul	r3,r3,r0	;r3 = seed * 12345 , low
	gmulhi	r4		;r0 = ... high

	add	r3,r3,r1	;
	addt	r4,r4,r2	;

	st	r5,r3		;low seed
	addi	r5,1
	or	r0,r3,r3	;result = high seed
	st	r5,r4		;high seed
	
	rqpop	
	rqpop
	rqpop
	pop	r5
	pop	r4
	pop	r3
	
	rqpop	
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
		
;******************************
;*
;* resetData
;* 

resetData

	;clear zoom
	
	dexti	>backGroundVariableValues	
	movei	r2,backGroundVariableValues	;u, v, su, sv, ...
	movei	r0,0
	dexti	2
	movei	r1,$01
	ssto	cZoomLow
	stwo	r2,r0
	ssto	cZoom
	stwo	r2,r1

	;clear stone count
	
	movei	r6,gfxObjectStoneCount
	movei	r0,0
	moveih	r6,>gfxObjectStoneCount
	nop
	st	r6,r0

	;clear stones
	
	movei	r3,gfxObjectStoneCountMax	
	
clearStonesLoop	
	
	dexti	>gfxObjectStones
	movei	r6,gfxObjectStones
	nop
	
	add	r6,r6,r3
	nop
	rqldi	r6,0
	nop
	movei	r0,$ff
	ld	r5			;ptr to stone
	moveih	r0,3			;1024
	addi	r5,12			;yintern
	
	nop
	st	r5,r0			;rest yintern to start value	
	
	subi	r3,1
	brts	clearStonesLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;reset energie
	
	movei	r6,Energie
	movei	r0,$2c
	moveih	r6,>Energie
	moveih	r0,$1		;300
	nop
	st	r6,r0
	
	;reset ship
	
	movei	r6,gfxObjectShip
	movei	r0,168
	moveih	r6,>gfxObjectShip
	movei	r1,182
	st	r6,r0	;x
	addi	r6,1
	nop
	st	r6,r1	;y
	
	;reset timer
	
	movei	r6,playTimeCounter
	movei	r0,0
	moveih	r6,>playTimeCounter
	nop
	st	r6,r0
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* definition of background picture data
;*

	.def	uLow         	0
	.def	uHigh         	1
	.def	vLow         	2
	.def	vHigh         	3
	.def	suLow        	4
	.def	suHigh        	5
	.def	svLow        	6
	.def	svHigh        	7
	.def	duLow        	8
	.def	duHigh        	9
	.def	dvLow        	10
	.def	dvHigh        	11
	
	.def	cOffsetX	0
	.def	cOffsetY        1
	.def	cCenterX        2
	.def	cCenterY        3
	.def	cAngle          4
	.def	cZoom	        5
	.def	cosycLow	6
	.def	cosycHigh	7
	.def	cosxcLow 	8
	.def	cosxcHigh	9
	.def	sinycLow	10
	.def	sinycHigh	11
	.def	sinxcLow	12
	.def	sinxcHigh	13	
	.def	cZoomLow	14
	.def	cOffsetLow	14

;******************************
;*
;* drawBackground
;*

drawBackground

	push	r7
	
	;update background angle, zoom ...
	
	dexti	>backGroundVariableValues	
	movei	r2,backGroundVariableValues	;u, v, su, sv, ...
	nop
	
	dexti	>inGame
	movei	r0,inGame
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0
	nop
	cmpeqi	r0,0	;not in game so np zoom
	brts	drawBackgroundNoZoom
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;zoom in
	
	rqldi	r2,cZoomLow
	rqldi	r2,cZoom
	rqldi	r3,0
	ld	r0	;low
	ld	r1	;high
	dexti	$04		;delay slot	
	subi	r0,$0
	subtqi	r1,r1,0
	ssto	cZoomLow
	stwo	r2,r0
	ssto	cZoom
	stwo	r2,r1
	
	;

drawBackgroundNoZoom
	
	rqldi	r2,cOffsetX
	rqldi	r2,cOffsetLow
	nop
	ld	r0
	ld	r1
	
	
	dexti	$10
	subi	r1,0
	subtqi	r0,r0,0
	
	ssto	cOffsetLow
	stwo	r2,r1
	ssto	cOffsetX
	stwo	r2,r0
	
	
	;recalc du ....
	
	gpci	r7,2
	br	calcBackgroundValues
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;***********************************************************************
	;* per screen
	;***********************************************************************
	
	dexti	>backGroundObjectValues	
	movei	r2,backGroundObjectValues	;u, v, su, sv, ...
	nop
	
	;set du
	
	rqldi	r2,duLow
	rqldi	r2,duHigh
	nop
	ld	r0
	ld	r1
	
	movei	r6,l2dduHigh
	nop
	setssr	r6,5		;write sel
	setssr	r1,6		;write data
	
	movei	r6,l2dduLow
	nop
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
	
	;set dv
	
	rqldi	r2,dvLow
	rqldi	r2,dvHigh
	nop
	ld	r0
	ld	r1
	
	movei	r6,l2ddvHigh
	nop
	setssr	r6,5		;write sel
	setssr	r1,6		;write data
	
	movei	r6,l2ddvLow
	nop
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
				
	;set xmask, ymask, xwidth, adr

	movei	r6,l2dxmask
	dexti	1	
	movei	r0,$ff		;xmask
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
	
	movei	r6,l2dymask
	dexti	1	
	movei	r0,$ff		;ymask
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
	
	movei	r6,l2dxwidth
	dexti	2	
	movei	r0,$00		;xwidth
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
	
	;
	
	movei	r0,$e0
	movei	r1,$04
	moveih	r0,$26
	moveih	r1,$d0		;r1:r0 = $d004 26e0 (source)
	
	movei	r6,l2dsadrLow
	nop
	setssr	r6,5		;write sel
	setssr	r0,6		;write data
	
	movei	r6,l2dsadrHigh
	nop
	setssr	r6,5		;write sel
	setssr	r1,6		;write data
	
	;
	
	movei	r3,0		;y
	
backGroundYLoop
	
	;set u (x)
	
	rqldi	r2,uLow
	rqldi	r2,uHigh
	movei	r6,l2duHigh
	ld	r0
	ld	r1
	
	movei	r5,l2duLow
	setssr	r6,5		;write sel
	setssr	r1,6		;write data
	setssr	r5,5		;write sel
	setssr	r0,6		;write data

	;set v 
	
	rqldi	r2,vLow
	rqldi	r2,vHigh
	nop
	ld	r0
	ld	r1
	
	movei	r6,l2dvHigh
	movei	r5,l2dvLow
	setssr	r6,5		;write sel
	setssr	r1,6		;write data
	setssr	r5,5		;write sel
	setssr	r0,6		;write data
	
	;v += sv;
	
	rqldi	r2,vLow
	rqldi	r2,vHigh
	rqldi	r2,svLow
	ld	r0		;vLow
	ld	r1		;vHigh
	ld	r5		;svLow
	rqldi	r2,svHigh
	nop
	nop
	ld	r6		;svHigh
	
	add	r0,r0,r5
	addt	r1,r1,r6
	ssto	vLow
	stwo	r2,r0
	ssto	vHigh
	stwo	r2,r1
		
	;u = +su
	
	rqldi	r2,uLow
	rqldi	r2,uHigh
	rqldi	r2,suLow
	ld	r0		;uLow
	ld	r1		;uHigh
	ld	r5		;suLow
	rqldi	r2,suHigh
	nop
	nop
	ld	r6		;suHigh
	
	add	r0,r0,r5
	addt	r1,r1,r6
	ssto	uLow
	stwo	r2,r0
	ssto	uHigh
	stwo	r2,r1
	
	;push pipeline (after du,dv)
	
	exl2den
	exl2den
	exl2den
	
	;draw scanline
		
	movei	r1,drawFrame
	nop
	moveih	r1,>drawFrame
	nop
	rqldi	r1,1	;low
	rqldi	r1,0	;high
	nop
	ld	r5
	ld	r6
	nop

	
	
	or	r0,r3,r3	;r0 = y
	dexti	1
	muli	r0,$78		;r0 = r0 * 376
	gmulhi	r1		;r1 (high part)

	gpci	r7,2
	br	drawScanLine24
	movei	r4,15		;16 * 24 = 384 pixel	delay slot
	add	r5,r5,r0	;dest + y		;delay slot
	addt	r6,r6,r1	;high partnop		;delay slot
	nop			;delay slot

	;
	
	cmpeqi	r3,224
	brtc	backGroundYLoop
	addi	r3,1		;y++ delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;
	
	rqpop	
	nop
	nop
	pop	r7
	nop

	nop
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* calcBackgroundValues
;*
	
calcBackgroundValues	

	push	r7
	
	dexti	>backGroundVariableValues
	movei	r5,backGroundVariableValues	;source
	
	dexti	>backGroundObjectValues
	movei	r6,backGroundObjectValues	;dest
	
	;get sin,cos form angle
	
	dexti	$1
	movei	r2,$ff
	rqldi	r5,cAngle
	dexti	>sinTable
	movei	r1,sinTable
	ld	r0		;angle
	nop
	and	r0,r0,r2	;only 0..511
	nop
	rqld	r1,r0		;sinTable[r]
	addi	r0,128
	nop
	ld	r4		;r4 = sin
	rqld	r1,r0		;sinTable[r+128]
	rqldi	r5,cZoom
	nop
	ld	r3		;r3 = cos
	ld	r2		;r2 = zoom
	
	;multiply with zoom
	
	nop
	muls	r0,r4,r2	;sin * zoom	
	gmulhi	r4		;get upper part r4 = ssin (high)
	muls	r1,r3,r2	;cos * zoom
	gmulhi	r3		;get upper part r3 = scos (high)
		
	;asr 8	r4:r0	r3:r1

	lsri	r0,8
	lsri	r1,8
	dexti	$20		;*$100 =  10 0000 [000] = 
	mulqi	r2,r4,0		;r4 = AAAA AAAA BBBB BBBB ->  BBBB BBBB 0000 0000
	nop
	or	r0,r0,r2
	dexti	$20
	mulqi	r2,r3,0
	nop
	or	r1,r1,r2
	asri	r4,8
	asri	r3,8
		
	;store su,sv,du,dv
		
	ssto	duLow
	stwo	r6,r0		;du = ssin
	ssto	duHigh
	stwo	r6,r4	
	
	ssto	dvLow
	stwo	r6,r1		;dv = scos
	ssto	dvHigh
	stwo	r6,r3

	
	ssto	suLow
	stwo	r6,r1		;su = scos
	ssto	suHigh
	stwo	r6,r3	
	
	movei	r7,0
	movei	r2,0
	
	sub	r7,r7,r0
	subt	r2,r2,r4	
	
	ssto	svLow
	stwo	r6,r7		;sv = -ssin
	ssto	svHigh
	stwo	r6,r2
	
	;calculate offset for center position
	;

	;scos = r3:r1
	;ssin = r4:r0
	
	;Int32 cosy = (Int32)((ncos * centerY));
	
	push	r0
	push	r1
	push	r3
	push	r4
	
	
	gpci	r7,2
	br	muls32x16	;r4:r3 = r1:r0 * r2
	rqldi	r5,cCenterY	
	or	r0,r1,r1	;r0 = cos low
	or	r1,r3,r3	;r1 = cos high
	ld	r2		;r2 = centerY
	ssto	cosycLow
	stwo	r5,r3
	ssto	cosycHigh
	stwo	r5,r4
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r1
	rqpop
	nop
	nop
	pop	r0
	nop
	
        ;Int32 cosx = (Int32)((ncos * centerX));
	
	push	r0
	push	r1
	push	r3
	push	r4
	
	gpci	r7,2
	br	muls32x16	;r4:r3 = r1:r0 * r2
	rqldi	r5,cCenterX	
	or	r0,r1,r1	;r0 = cos low
	or	r1,r3,r3	;r1 = cos high
	ld	r2		;r2 = centerX
	ssto	cosxcLow
	stwo	r5,r3
	ssto	cosxcHigh
	stwo	r5,r4
	
	;dump
	;.stop
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r1
	rqpop
	nop
	nop
	pop	r0
	nop
	
        ;Int32 siny = (Int32)((nsin * centerY));
	
	push	r0
	push	r1
	push	r3
	push	r4
	
	gpci	r7,2
	br	muls32x16	;r4:r3 = r1:r0 * r2
	rqldi	r5,cCenterY	
	or	r0,r0,r0	;r0 = sin low
	or	r1,r4,r4	;r1 = sin high
	ld	r2		;r2 = centerY
	ssto	sinycLow
	stwo	r5,r3
	ssto	sinycHigh
	stwo	r5,r4
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r1
	rqpop
	nop
	nop
	pop	r0
	nop
	
        ;Int32 sinx = (Int32)((nsin * centerX));
	
	push	r0
	push	r1
	push	r3
	push	r4
	
	gpci	r7,2
	br	muls32x16	;r4:r3 = r1:r0 * r2
	rqldi	r5,cCenterX	
	or	r0,r0,r0	;r0 = sin low
	or	r1,r4,r4	;r1 = sin high
	ld	r2		;r2 = centerX
	ssto	sinxcLow
	stwo	r5,r3
	ssto	sinxcHigh
	stwo	r5,r4
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r1
	rqpop
	nop
	nop
	pop	r0
	nop

        ;u = (UInt32)(((cosyc + sinxc)) + (offsetTextX << 16));
	
	rqldi	r5,cosycHigh
	rqldi	r5,cosycLow
	rqldi	r5,sinxcHigh
	ld	r0		
	ld	r1		;cosyc = r0:r1
	ld	r2
	rqldi	r5,sinxcLow
	rqldi	r5,cOffsetX
	nop
	ld	r3		;sinxc  = r2:r3
	ld	r4		;offset = r4
	
	;dump
	;.stop
	
	add	r1,r1,r3
	addt	r0,r0,r2	;cosyc + sinxc
	nop
	add	r0,r0,r4	;+ offset X
	
	;set u
	
	ssto	uLow
	stwo	r6,r1
	ssto	uHigh
	stwo	r6,r0
	
	;v = (UInt32)(((cosx - siny)) + (offsetTextY << 16));
	
	rqldi	r5,cosxcHigh
	rqldi	r5,cosxcLow
	rqldi	r5,sinycHigh
	ld	r0		
	ld	r1		;cosxc = r0:r1
	ld	r2
	rqldi	r5,sinycLow
	rqldi	r5,cOffsetY
	nop
	ld	r3		;sinyc  = r2:r3
	ld	r4		;offset = r4
	
	sub	r1,r1,r3
	subt	r0,r0,r2	;cosyc + sinxc
	nop
	add	r0,r0,r4	;+ offset Y
	
	;set u
		
	ssto	vLow
	stwo	r6,r1
	ssto	vHigh
	stwo	r6,r0
	
	;
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	;
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

;******************************
;*
;* r4:r3 = r1:r0 * r2
;*
	
muls32x16	

	nop
	mul	r3,r0,r2
	gmulhi	r4
	mul	r1,r1,r2
	movei	r2,0
	add	r4,r4,r1
	jmpi	r7,0
	sub	r0,r2,r0
	nop
	add	r4,r4,r0	
	nop	
	
;******************************
;*
;* drawScanLine24
;*
;* r4 	pixel count / 24 - 1
;* r5 	dest low
;* r6 	dest high
;*
;* note: you have to setup l2dData before 	

drawScanLine24

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;per 24 pixel (dest)
	
scanloop	
	exl2den
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2denerqld 
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2denerqld 
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2denerqld  
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2denerqld  	
	exl2denerqld	
	exl2denerqld    
	exl2denerqld  
	exl2denerqld    
	exl2denerqld	
	exl2denerqld    
	exl2denerqld 	
	exl2derqld    
	esadr	r6,r5
	addi	r5,8
	addtqi	r6,r6,0
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,4
	est	r1,5
	est	r2,6
	est	r3,7
	esadr	r6,r5
	addi	r5,8
	addtqi	r6,r6,0
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,4
	est	r1,5
	est	r2,6
	est	r3,7
	esadr	r6,r5
	addi	r5,8
	addtqi	r6,r6,0
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	subi	r4,1
	brts	scanloop
	est	r0,4		;delay slot
	est	r1,5            ;delay slot
	est	r2,6           ;delay slot
	est	r3,7            ;delay slot
	
	;
	
	rqpop	
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r5
	rqpop	
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2

	rqpop	
	rqpop
	jmpi	r7,0
	pop	r1		;delay slot
	pop	r0		;delay slot
	nop			;delay slot
	nop			;delay slot
		
	
;******************************
;*
;* setupVideo
;*

setupVideo

	movei	r0,videoDefault		;source
	movei	r1,UcTimer2Ctrl		;dest-1
	moveih	r0,>videoDefault
	moveih	r1,>UcTimer2Ctrl			
	movei	r2,26			;27-1
	
setupVideoLoop

	rqldi	r0,0
	addi	r0,1
	addi	r1,1
	ld	r3
	subi	r2,1
	brts	setupVideoLoop
	st	r1,r3	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;*********************************************************************
;* data
;*********************************************************************
	
videoDefault		;AD NAME					NEW				DEFAULT
	
	word	$0001	;10 VIDEO_ON					1 (ON)
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1
	word	$0000	;1a LC ADDER LOW				$0000
	word	$0040	;1b LC ADDER HIGH				$0040	
	word	$0000	;1c LC ADDER LATCH				X
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000	
	word	$0000	;1e						X
	word	$0000	;1f						X
	word	$0008	;20 HLOADSTART 					8
	word	$00bc	;21 HLOADSTOP					180+8
	word	$001e	;22 VLOADSTART 					31-1
	word	$03a2	;23 VLOADSTOP 					931-1
	word	$0004	;24 VLOADNEXT					4
	word	$0008	;25 MEM_STARTADR_store low			$0000
	word	$d000	;26 MEM_STARTADR_store high			$f000
	word	$0010	;27 MEM_LINEOFFSET_store low			$0000
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000
	word	$0003	;29 MEM_MODE_store 				2 (X4R4G4B4*2)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X
				
;*********************************************************************
;* data
;*********************************************************************

stringWelcome1
	.string	"goldmomo presents on goldmomo_endlos"
	word	0
	
stringWelcome2
	.string	"-= SPACE PILOT OF DEATH =-"
	word	0
	
stringWelcome2b
	.string	"(you can`t survive)"
	word	0

stringWelcome3
	.string	"PRG by goldmomo"
	word	0	
	
stringWelcome4
	.string	"GFX by arboris"
	word	0	

stringWelcome5
	.string	"SFX by revisq/anadune"
	word	0		
	
stringWelcome6
	.string	"Press space to start"
	word	0	

stringPower		
	.string	"Power"	
	word	0
	
stringTime		
	.string	"Time %us"	
	word	0
	
stringMeteoroids		
	.string	"Meteoroids %u"	
	word	0
	
stringGameOver
	.string	"GAME OVER"	
	word	0

stringGameOver2
	.string	"Press space to restart"	
	word	0
	
;**********************************************	

backGroundObjectValues
	
	word	0	;uLow         	0		
	word	0	;uHigh         	1
	word	0	;vLow         	2		
	word	0	;vHigh         	3
	word	0	;suLow        	4		
	word	0	;suHigh        	5
	word	32767	;svLow        	6		
	word	0	;svHigh        	7
	word	32767	;duLow        	8		
	word	0	;duHigh        	9
	word	0	;dvLow        	10		
	word	0	;dvHigh        	11
	
backGroundVariableValues	
	
	word	255	;offset X
	word	255	;offset Y
	word	-180	;center X
	word	-112	;center Y
	word	0	;angle
	word	$0201	;zoom	
	word	0	;cosyc 	low
	word	0	;	high
	word	0	;cosxc 	low 
	word	0	;     	high
	word	0	;sinyc 	low
	word	0	;    	high
	word	0	;sinxc 	low
	word	0	;     	high
	word	0	;zoom low
	word	0	;offset low

;*	
	
inGame	
	word	0
	
frameCounter
	word	0
	
playTimeCounter
	word	0
	
randSeed
	word	$dead
	word	$beef

	;Energie bar
	
Energie
	word	300

	;GFX  / ctrl

gfxObjectTemp

	.space	8	;for clip to draw object

;**********************

	.def	gfxObjectStoneCountMax	63	;+1

	
gfxObjectStoneCount

	word	63	;n+1
	
gfxObjectStones

	word	gfxObjectStone1
	word	gfxObjectStone2
	word	gfxObjectStone3
	word	gfxObjectStone4
	word	gfxObjectStone5
	word	gfxObjectStone6
	word	gfxObjectStone7
	word	gfxObjectStone8
	word	gfxObjectStone9
	word	gfxObjectStoneA
	word	gfxObjectStoneB
	word	gfxObjectStoneC
	word	gfxObjectStoneD
	word	gfxObjectStoneE
	word	gfxObjectStoneF
	word	gfxObjectStoneG
	word	gfxObjectStoneH
	word	gfxObjectStoneI
	word	gfxObjectStoneJ
	word	gfxObjectStoneK
	word	gfxObjectStoneL
	word	gfxObjectStoneM
	word	gfxObjectStoneN
	word	gfxObjectStoneO
	word	gfxObjectStoneP
	word	gfxObjectStoneQ
	word	gfxObjectStoneR
	word	gfxObjectStoneS
	word	gfxObjectStoneT
	word	gfxObjectStoneU
	word	gfxObjectStoneV
	word	gfxObjectStoneW
	word	gfxObjectStone21
	word	gfxObjectStone22
	word	gfxObjectStone23
	word	gfxObjectStone24
	word	gfxObjectStone25
	word	gfxObjectStone26
	word	gfxObjectStone27
	word	gfxObjectStone28
	word	gfxObjectStone29
	word	gfxObjectStone2A
	word	gfxObjectStone2B
	word	gfxObjectStone2C
	word	gfxObjectStone2D
	word	gfxObjectStone2E
	word	gfxObjectStone2F
	word	gfxObjectStone2G
	word	gfxObjectStone2H
	word	gfxObjectStone2I
	word	gfxObjectStone2J
	word	gfxObjectStone2K
	word	gfxObjectStone2L
	word	gfxObjectStone2M
	word	gfxObjectStone2N
	word	gfxObjectStone2O
	word	gfxObjectStone2P
	word	gfxObjectStone2Q
	word	gfxObjectStone2R
	word	gfxObjectStone2S
	word	gfxObjectStone2T
	word	gfxObjectStone2U
	word	gfxObjectStone2V
	word	gfxObjectStone2W
	
			
;
;	$d000 0000 	frame buffer 0		(360+16)*226 (1 line space)
;	$d001 4bf0	frame buffer 1		(360+16)*226 (1 line space)
;	$d002 97e0	font colored		24064/2
;	$d002 c6e0	bobs			180224/2
;	$d004 26e0	galaxy			524288/2
;		
		;										0		1	2	3	4		
		;	0	1	2	3	4	5	6	7		8		9	a	b	c			
		;	x	y	w	h	sx	sy	srclow	srchigh		yspeed	S1	cox	coy	colsize	yintern
		                                                                                                                        
gfxObjectStone1		.data	4,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		28,	21,	25,	1024		
gfxObjectStone2		.data	273,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone3		.data	82,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		10,	10,	9,	1024
gfxObjectStone4		.data	200,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone5		.data	186,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		24,		20,	15,	14,	1024
gfxObjectStone6		.data	109,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		24,		9,	6,	6,	1024
gfxObjectStone7		.data	53,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		24,		33,	22,	21,	1024
gfxObjectStone8		.data	324,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		24,		5,	13,	9,	1024
gfxObjectStone9		.data	304,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStoneA		.data	184,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStoneB		.data	314,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		10,	10,	9,	1024
gfxObjectStoneC		.data	209,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStoneD		.data	98,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		24,		20,	15,	14,	1024
gfxObjectStoneE		.data	17,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		24,		9,	6,	5,	1024
gfxObjectStoneF		.data	168,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		24,		33,	22,	21,	1024
gfxObjectStoneG		.data	71,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		24,		5,	13,	9,	1024
gfxObjectStoneH		.data	156,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		3,		28,	21,	25,	1024
gfxObjectStoneI		.data	115,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStoneJ		.data	351,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		2,		10,	10,	9,	1024
gfxObjectStoneK		.data	253,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStoneL		.data	107,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		3,		20,	15,	14,	1024
gfxObjectStoneM		.data	8,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		4,		9,	6,	6,	1024
gfxObjectStoneN		.data	132,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		2,		33,	22,	21,	1024
gfxObjectStoneO		.data	282,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		2,		5,	13,	9,	1024
gfxObjectStoneP		.data	4,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStoneQ		.data	334,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStoneR		.data	172,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		3,		10,	10,	9,	1024
gfxObjectStoneS		.data	255,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStoneT		.data	7,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		3,		20,	15,	14,	1024
gfxObjectStoneU		.data	301,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		4,		9,	6,	5,	1024
gfxObjectStoneV		.data	19,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		3,		33,	22,	21,	1024
gfxObjectStoneW		.data	24,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		3,		5,	13,	9,	1024
gfxObjectStone21	.data	4,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		28,	21,	25,	1024		
gfxObjectStone22	.data	273,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone23	.data	82,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		10,	10,	9,	1024
gfxObjectStone24	.data	200,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone25	.data	186,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		24,		20,	15,	14,	1024
gfxObjectStone26	.data	109,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		24,		9,	6,	6,	1024
gfxObjectStone27	.data	53,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		24,		33,	22,	21,	1024
gfxObjectStone28	.data	324,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		24,		5,	13,	9,	1024
gfxObjectStone29	.data	304,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone2A	.data	184,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone2B	.data	314,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		24,		10,	10,	9,	1024
gfxObjectStone2C	.data	209,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		24,		28,	21,	25,	1024
gfxObjectStone2D	.data	98,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		24,		20,	15,	14,	1024
gfxObjectStone2E	.data	17,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		24,		9,	6,	5,	1024
gfxObjectStone2F	.data	168,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		24,		33,	22,	21,	1024
gfxObjectStone2G	.data	71,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		24,		5,	13,	9,	1024
gfxObjectStone2H	.data	156,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		3,		28,	21,	25,	1024
gfxObjectStone2I	.data	115,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStone2J	.data	351,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		2,		10,	10,	9,	1024
gfxObjectStone2K	.data	253,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStone2L	.data	107,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		3,		20,	15,	14,	1024
gfxObjectStone2M	.data	8,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		4,		9,	6,	6,	1024
gfxObjectStone2N	.data	132,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		2,		33,	22,	21,	1024
gfxObjectStone2O	.data	282,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		2,		5,	13,	9,	1024
gfxObjectStone2P	.data	4,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStone2Q	.data	334,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStone2R	.data	172,	-60,	24,	21,	94,	316,	$c6e0,	$d002,		3,		10,	10,	9,	1024
gfxObjectStone2S	.data	255,	-60,	56,	53,	2,	246,	$c6e0,	$d002,		2,		28,	21,	25,	1024
gfxObjectStone2T	.data	7,	-60,	32,	31,	64,	262,	$c6e0,	$d002,		3,		20,	15,	14,	1024
gfxObjectStone2U	.data	301,	-60,	16,	12,	105,	270,	$c6e0,	$d002,		4,		9,	6,	5,	1024
gfxObjectStone2V	.data	19,	-60,	56,	42,	0,	306,	$c6e0,	$d002,		3,		33,	22,	21,	1024
gfxObjectStone2W	.data	24,	-60,	24,	26,	63,	315,	$c6e0,	$d002,		3,		5,	13,	9,	1024
	
	;**********************
	
gfxObjectShip
	word	168	;0 dest pos x
	word	182	;1 dest pos y
	word	40	;2 width
	word	40	;3 height
	word	41	;4 source x
	word	3	;5 source y
	word	$c6e0	;6 source low
	word	$d002	;7 source high
	word	0	;8 0 no speed
	word	20	;9 1 collision offset x
	word	23	;a 2 collision offset y
	word	18	;b 3 collision size
	
gfxObjectShipAnimNormal

	;	w	h	x	y
	.data	40,	36,	41,	3	
	.data	40,	43,	41,	43	
	.data	40,	40,	41,	89
	
gfxObjectShipAnimLeft

	;	w	h	x	y
	.data	40,	36,	0,	3	
	.data	40,	43,	0,	43	
	.data	40,	40,	0,	89	
	
gfxObjectShipAnimRight

	;	w	h	x	y
	.data	40,	36,	85,	3	
	.data	40,	43,	85,	43	
	.data	40,	40,	85,	89

gfxAnimCounterShip

	word	0	
	
	;**********************
	
gfxObjectExplosion
	word	168	;0 dest pos x
	word	182	;1 dest pos y
	word	48	;2 width
	word	48	;3 height
	word	2	;4 source x
	word	195	;5 source y
	word	$c6e0	;6 source low
	word	$d002	;7 source high
	word	0	;8 0 no speed
	word	0	;9 1 collision offset x
	word	0	;a 2 collision offset y
	word	22	;b 3 collision size	
	
gfxObjectExplosionAnim

	;	w	h	x	y
	.data	48,	49,	0,	195	
	.data	56,	49,	53,	195	
	.data	56,	49,	124,	195
	.data	56,	49,	192,	196
	
gfxAnimCounterExplosion

	word	0		
	
	;**********************
	
	
shipMoveStore	

	word	0	;UDRL	

	
displayFrame	

	.data	$d000,$0000

drawFrame	

	.data	$d001,$4bf0	
			
fontAddress

	.data	$d002,$97e0	
	
;*********************************************************************
;* channel data
;*********************************************************************

channel0	
	.space	32	;channel 0
channel1	                    
	.space	32	;channel 1       	
channel2		                              
	.space	32	;channel 2
channel3		                              
	.space	32	;channel 3	
channel4	                          
	.space	32	;channel 4  		
channel5	                            
	.space	32	;channel 5 		
	
modChannels

	word	channel1
	word	channel2
	word	channel3
	word	channel4
	
channel0Buffer
	.space	96

channel1Buffer
	.space	96

channel2Buffer
	.space	96
	
channel3Buffer
	.space	96
		
channel4Buffer
	.space	96

channel5Buffer
	.space	96	
	
;*****	
	
audioDefault
	
	;
				;A =  8 7654 3210
	.data   $9,$0000	;9 =  0 0000 0000 	(inactivate interface)
	.data	$0,$0097	;0 =  0 1001 0111	;left line in mute
	.data	$1,$0097	;1 =  0 1001 0111	;right line in mute
	.data	$2,$006e	;2 =  0 0111 1001	;+6db left out
	.data	$3,$006e	;3 =  0 0111 1001	;0db right out
	.data	$4,$0012	;4 =  0 0001 0010	;0=0 mic boost off;
							;1=1 enable line input mute
							;2=1 line input to adc
							;3=0 disable bypass
							;4=1 DAC select
							;5=0 side tone disable
							;76=0 -6db sidetone						
	.data	$5,$0000	;5 =  0 0000 0000	;DAC soft Mute off, no Filer
	.data	$6,$0067	;6 =  0 0110 0111 	;DAC Power, Output Power, Device Power on
	.data	$7,$0009	;7 =  0 0000 1001 	;left justified, 24 bit
	.data	$8,$001e	;8 =  0 0001 1110	;0=0 normal mode
							;1=1 bosr (384/192)
							;5432=0111 96khz
							;78=00 no clk dividing	
	.data   $9,$0001	;9 =  0 0000 0001 	(activate interface)
	.data	$ff,$0000	;stop	
	
;**********************************************	
	
sinTable	;512*1.25

    .data	0,402,804,1206,1608,2009,2411,2811,3212,3612,4011,4410,4808,5205,5602,5998,6393,6787,7179,7571,7962,8351,8740,9126,9512,9896,10279,10660,11039,11417,11793,12167,12540,12910,13279,13645,14010,14372,14733,15091,15446,15800,16151,16500,16846,17189,17531,17869,18205
    .data	18538,18868,19195,19520,19841,20160,20475,20787,21097,21403,21706,22005,22302,22595,22884,23170,23453,23732,24007,24279,24547,24812,25073,25330,25583,25832,26078,26319,26557,26790,27020,27245,27467,27684,27897,28106,28310,28511,28707,28898,29086,29269,29447,29621
    .data	29791,29956,30117,30273,30425,30572,30714,30852,30985,31114,31237,31357,31471,31581,31685,31785,31881,31971,32057,32138,32214,32285,32351,32413,32469,32521,32568,32610,32647,32679,32706,32728,32745,32758,32765,32767,32765,32758,32745,32728,32706,32679,32647,32610
    .data	32568,32521,32469,32413,32351,32285,32214,32138,32057,31971,31881,31785,31685,31581,31471,31357,31237,31114,30985,30852,30714,30572,30425,30273,30117,29956,29791,29621,29447,29269,29086,28898,28707,28511,28310,28106,27897,27684,27467,27245,27020,26790,26557,26319
    .data	26078,25832,25583,25330,25073,24812,24547,24279,24007,23732,23453,23170,22884,22595,22302,22005,21706,21403,21097,20787,20475,20160,19841,19520,19195,18868,18538,18205,17869,17531,17189,16846,16500,16151,15800,15446,15091,14733,14372,14010,13645,13279,12910,12540
    .data	12167,11793,11417,11039,10660,10279,9896,9512,9126,8740,8351,7962,7571,7179,6787,6393,5998,5602,5205,4808,4410,4011,3612,3212,2811,2411,2009,1608,1206,804,402,0,-402,-804,-1206,-1608,-2009,-2411,-2811,-3212,-3612,-4011,-4410,-4808,-5205,-5602,-5998,-6393,-6787
    .data	-7179,-7571,-7962,-8351,-8740,-9126,-9512,-9896,-10279,-10660,-11039,-11417,-11793,-12167,-12540,-12910,-13279,-13645,-14010,-14372,-14733,-15091,-15446,-15800,-16151,-16500,-16846,-17189,-17531,-17869,-18205,-18538,-18868,-19195,-19520,-19841,-20160,-20475,-20787
    .data	-21097,-21403,-21706,-22005,-22302,-22595,-22884,-23170,-23453,-23732,-24007,-24279,-24547,-24812,-25073,-25330,-25583,-25832,-26078,-26319,-26557,-26790,-27020,-27245,-27467,-27684,-27897,-28106,-28310,-28511,-28707,-28898,-29086,-29269,-29447,-29621,-29791,-29956
    .data	-30117,-30273,-30425,-30572,-30714,-30852,-30985,-31114,-31237,-31357,-31471,-31581,-31685,-31785,-31881,-31971,-32057,-32138,-32214,-32285,-32351,-32413,-32469,-32521,-32568,-32610,-32647,-32679,-32706,-32728,-32745,-32758,-32765,-32768,-32765,-32758,-32745,-32728
    .data	-32706,-32679,-32647,-32610,-32568,-32521,-32469,-32413,-32351,-32285,-32214,-32138,-32057,-31971,-31881,-31785,-31685,-31581,-31471,-31357,-31237,-31114,-30985,-30852,-30714,-30572,-30425,-30273,-30117,-29956,-29791,-29621,-29447,-29269,-29086,-28898,-28707,-28511
    .data	-28310,-28106,-27897,-27684,-27467,-27245,-27020,-26790,-26557,-26319,-26078,-25832,-25583,-25330,-25073,-24812,-24547,-24279,-24007,-23732,-23453,-23170,-22884,-22595,-22302,-22005,-21706,-21403,-21097,-20787,-20475,-20160,-19841,-19520,-19195,-18868,-18538,-18205
    .data	-17869,-17531,-17189,-16846,-16500,-16151,-15800,-15446,-15091,-14733,-14372,-14010,-13645,-13279,-12910,-12540,-12167,-11793,-11417,-11039,-10660,-10279,-9896,-9512,-9126,-8740,-8351,-7962,-7571,-7179,-6787,-6393,-5998,-5602,-5205,-4808,-4410,-4011,-3612,-3212,-2811
    .data	-2411,-2009,-1608,-1206,-804,-402,0,402,804,1206,1608,2009,2411,2811,3212,3612,4011,4410,4808,5205,5602,5998,6393,6787,7179,7571,7962,8351,8740,9126,9512,9896,10279,10660,11039,11417,11793,12167,12540,12910,13279,13645,14010,14372,14733,15091,15446,15800,16151,16500
    .data	16846,17189,17531,17869,18205,18538,18868,19195,19520,19841,20160,20475,20787,21097,21403,21706,22005,22302,22595,22884,23170,23453,23732,24007,24279,24547,24812,25073,25330,25583,25832,26078,26319,26557,26790,27020,27245,27467,27684,27897,28106,28310,28511,28707
    .data	28898,29086,29269,29447,29621,29791,29956,30117,30273,30425,30572,30714,30852,30985,31114,31237,31357,31471,31581,31685,31785,31881,31971,32057,32138,32214,32285,32351,32413,32469,32521,32568,32610,32647,32679,32706,32728,32745,32758,32765
    
		
				