;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"

;*********************************************************************	
; sd header is placed here
;*********************************************************************	

	.include "boot_level_sdHeader.s"

;*********************************************************************
;* start at address 32 here
;*********************************************************************

start		
		
	gpci	r7,2
	br	setupVideo
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot


	;first copy rom to sram (172240 bytes (43060 32bits))
	
	movei	r0,0	;source 
	movei	r1,0
	moveih	r0,0
	moveih	r1,$e0	;p(0,0) = e000000
		
	movei	r2,$d0	;destination 
	movei	r3,$02
	moveih	r2,$78
	moveih	r3,$f0	;p(0,0) = f002 78d0 (360*225*2) (2 frambuffers)
	
	gpci	r7,2	;next 4 + X
	br	exCopy
	movei	r4,$33	;delay slot
	nop		;delay slot
	moveih	r4,$a8	;delay slot
	nop		;delay slot
	
	;draw loop
	
drawLoop

	;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

	st	r1,r3
	addi	r1,1
	nop
	st	r1,r4
	st	r2,r7		;latch out
	
	;draw background to drawFrame
	
	movei	r0,drawFrame	
	nop
	moveih	r0,>drawFrame	
	nop
	rqldi	r0,0
	rqldi	r0,1
	nop
	ld	r3
	ld	r2	;destination
	
	movei	r0,$d0	;source 
	movei	r1,$02
	moveih	r0,$78
	moveih	r1,$f0	;p(0,0) = f002 78d0 (360*225*2) (2 frambuffers)
	
	gpci	r7,2	;next 4 + X
	br	exCopy
	movei	r4,$33	;delay slot
	nop		;delay slot
	moveih	r4,$9e	;delay slot
	nop		;delay slot
	
	;calc scene
	
	gpci	r7,2	;next 4 + X
	br	calcScene
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;draw scene
	
	gpci	r7,2	;next 4 + X
	br	drawScene
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	;copy
	
	;end
	movei	r1,$ff
	nop
	moveih	r1,$0
	nop
	
waste2	
	movei	r0,$ff
	nop
	moveih	r0,$ff
	nop
	
waste

	subi	r0,1
	;brts	waste
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	subi	r1,1
	;brts	waste2
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot

	br drawLoop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
end	br	end
	nop
	nop
	nop
	nop
	
displayFrame	word	$f000	
	        word	$0000

drawFrame	word	$f001	
	        word	$3c68
			
;******************************
;* 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
	
	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
	
;******************************
;* exCopy
;*
;* r1:r0 source
;* r3:r2	dest
;* r4 count - 1 (32 bit copy per step)
;* 
		
exCopy	

	push	r5
	push	r6
	
exCopyLoop
	esadr	r1,r0
	erqldi	0
	erqldi	1
	
	eld	r5
	eld	r6
	
	esadr	r3,r2
	est	r5,0
	est	r6,1

	subi	r4,1
	brts	exCopyLoop
	addi	r0,2		;delay slot
	addtqi	r1,r1,0		;delay slot
	addi	r2,2		;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
	
	
;******************************
;* waitVPos
;*
;* r0 pos
;* 

waitVPos	
	
	push	r1
	push	r2
	
	movei	r1,UcVideoRVCountWHSYNC
	nop
	moveih	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
	
;******************************
;* drawScene
;*
;* 
;* 
	
drawScene	

	push	r7

	movei	r7,19	;ballcount
	nop
	
nextBall
	
	; get ball coordinates
	
	movei	r0,ballpos	
	add	r4,r7,r7		;r4 * 2
	moveih	r0,>ballpos	
	movei	r1,$68
	add	r0,r0,r4
	moveih	r1,1	;r1 = 360
	
	rqldi	r0,0
	rqldi	r0,1
	nop
	ld	r2	;x
	ld	r3	;y
	
	nop
	
	mul	r3,r3,r1	;y * 360
	gmulhi	r0		;r0 = upper mul value	
	or	r5,r2,r2
	movei	r6,0
	add	r5,r5,r3
	addt	r6,r6,r0
	
	;draw ball
	
	movei	r0,drawFrame	
	nop
	moveih	r0,>drawFrame	
	nop
	rqldi	r0,0
	rqldi	r0,1
	nop
	ld	r3
	ld	r2	;destination frame
	
	nop
	add	r2,r2,r5	;
	addt	r3,r3,r6	;add position

	movei	r0,$38	;source 
	movei	r1,$03
	moveih	r0,$b5
	moveih	r1,$f0	;p(0,0) = f003b538 
	
	; change source ball
	
	movei	r6,00
	or	r4,r7,r7
	moveih	r6,$4	;1024
	lsri	r4,2
	nop
	mul	r6,r6,r4
	gmulhi	r4
	nop
	add	r0,r0,r6
	addt	r1,r1,r4
	
	movei	r6,31
dyloop	
	movei	r4,31
dxloop	
	esadr	r1,r0	;source
	erqldi	0
	eld	r5
	nop
	extri	r5,15	;bit 15 = 0 -> transparent
	esadr	r3,r2		
	estts	r5,0	;store if t set
	
	subi	r4,1
	brts	dxloop
	addi	r0,1	;delay slot
	addtqi	r1,r1,0 ;delay slot
	addi	r2,1    ;delay slot
	addtqi	r3,r3,0 ;delay slot
		
	subi	r6,1
	brts	dyloop	
	addi	r2,164	;delay slot
	addtqi	r3,r3,0	;delay slot
	addi	r2,164	;delay slot
	addtqi	r3,r3,0	;delay slot	
	
	
	subi	r7,1
	brts	nextBall
	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
	
;******************************
;* calcScene
;*
;* 
;* 
	
calcScene

	movei	r4,cnt	
	movei	r5,usin
	moveih	r4,>cnt
	moveih	r5,>usin
		
		
	rqldi	r4,0
	nop
	nop
	ld	r4	;cnt
	
	movei	r0,19
	
calcLoop

	movei	r6,ballpos
	nop
	moveih	r6,>ballpos
	nop
	add	r6,r6,r0
	nop
	add	r6,r6,r0

	;r4 is cnt
	
	push	r0
	push	r1
	push	r2
		
	; get sin from cnt

	movei	r1,$7f
	or	r0,r4,r4
	nop
	muli	r0,3
	nop
	and	r0,r4,r1
	nop
	add	r1,r5,r0
	nop
	rqldi	r1,0
	nop
	nop
	ld	r2
	
	;get cos from cnt
	
	nop
	movei	r1,$7f
	or	r0,r4,r4
	nop
	muli	r0,5
	nop
	and	r0,r0,r1
	nop
	add	r1,r5,r0
	nop
	addi	r1,32
	nop
	rqldi	r1,0
	nop
	nop
	ld	r3
		
	;get pos	
		
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r0
	ld	r1
	
	lsri	r3,1
	or	r0,r2,r2	;x = sin(cnt)
	or	r1,r3,r3	;y = cos(cnt)
	addi	r0,32
	addi	r1,32
	
	st	r6,r0
	addi	r6,1
	nop
	st	r6,r1

	
	rqpop	
	rqpop	
	rqpop	
	pop	r2
	pop	r1
	pop	r0
	nop
	
	addi	r4,4
	
	subi	r0,1
	brts	calcLoop
	nop	;delay slot
	nop     ;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;cnt up
	
	
	movei	r4,cnt	
	nop
	moveih	r4,>cnt	
	nop
	rqldi	r4,0
	nop
	nop
	ld	r0
	nop
	addi	r0,1
	nop
	st	r4,r0
	
	
	jmpi	r7,0
	nop	;delay slot
	nop     ;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
ballpos	

	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	0
	word	192
	
	word	160
	word	0
	
	word	80
	word	80
	
	word	10
	word	10
	
	word	80
	word	80
	
	word	10
	word	10
	
cnt	word	0	
	
	
usin	word 127	;0..pi = 0..63
	word 133
	word 140
	word 146
	word 152
	word 158
	word 164
	word 170
	word 176
	word 182
	word 187
	word 193
	word 198
	word 203
	word 208
	word 213
	word 218
	word 222
	word 226
	word 230
	word 233
	word 237
	word 240
	word 243
	word 245
	word 248
	word 249
	word 251
	word 253
	word 254
	word 254
	word 255
	word 255
	word 255
	word 254
	word 254
	word 253
	word 251
	word 249
	word 248
	word 245
	word 243
	word 240
	word 237
	word 233
	word 230
	word 226
	word 222
	word 218
	word 213
	word 208
	word 203
	word 198
	word 193
	word 187
	word 182
	word 176
	word 170
	word 164
	word 158
	word 152
	word 146
	word 140
	word 133
	word 127
	word 121
	word 114
	word 108
	word 102
	word 96
	word 90
	word 84
	word 78
	word 72
	word 67
	word 61
	word 56
	word 51
	word 46
	word 41
	word 36
	word 32
	word 28
	word 24
	word 21
	word 17
	word 14
	word 11
	word 9
	word 6
	word 5
	word 3
	word 1
	word 0
	word 0
	word 0
	word 0
	word 0
	word 0
	word 0
	word 1
	word 3
	word 5
	word 6
	word 9
	word 11
	word 14
	word 17
	word 21
	word 24
	word 28
	word 32
	word 36
	word 41
	word 46
	word 51
	word 56
	word 61
	word 67
	word 72
	word 78
	word 84
	word 90
	word 96
	word 102
	word 108
	word 114
	word 121
	word 127
	word 133
	word 140
	word 146
	word 152
	word 158
	word 164
	word 170
	word 176
	word 182
	word 187
	word 193
	word 198
	word 203
	word 208
	word 213
	word 218
	word 222
	word 226
	word 230
	word 233
	word 237
	word 240
	word 243
	word 245
	word 248
	word 249
	word 251
	word 253
	word 254
	word 254
	word 255	
	
;******************************
;*
;* 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				1 (ON)
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1				152-1
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1				384-1
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1				382-1
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1				1824-1
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1				1904-1
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1				3-1
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1				31-1
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1				931-1
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1				932-1
	word	$0000	;1a LC ADDER LOW				$0000				$0000
	word	$0040	;1b LC ADDER HIGH				$0080				$0040	
	word	$0000	;1c LC ADDER LATCH				X				X
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000				$0000	
	word	$0000	;1e						X				X
	word	$0000	;1f						X				X
	word	$0008	;20 HLOADSTART 					8				8
	word	$00bc	;21 HLOADSTOP					360+8				180+8
	word	$001e	;22 VLOADSTART 					31-1				31-1
	word	$03a2	;23 VLOADSTOP 					931-1				931-1
	word	$0004	;24 VLOADNEXT					2				4
	word	$0000	;25 MEM_STARTADR_store low			$0000				$0000
	word	$f000	;26 MEM_STARTADR_store high			$f000				$f000
	word	$0000	;27 MEM_LINEOFFSET_store low			$0000				$0000
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000				$0000
	word	$0002	;29 MEM_MODE_store 				2				2 (X1R5G5B5X1R5G5B5)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X				X
