
	.incdir	"f:\git\goldmomo_endlos\assembler_files\ucore\hcore\"
	.include	"include/uctrl_port.i"
	.include	"include/preformance_counter.i"
	.incdir	"f:\git\goldmomo_endlos\assembler_files\ucore\"
	.include	"include/ucore_ctrl.i"

;*********************************************************************
;* start
;*********************************************************************	
	
	.def	spLast		$d4000000
	.def	stackSize	1048576	;4 MegaByte per core
	
	.def	screenWidth	360
	.def	screenHeight	225
	
	org	hcoreStartPc
	
	nop
	nop
	nop
	nop
	
startHCore

	move	hold,stackSize
	moveh	hold,last,>>stackSize
	or	r0,last,0
	nop
	
	gssr	hold,0			;get core number
	fead	last,r0	
	nop
	mul	r0			;stackOffset = core number (0..n) * stackSize
	
	;set stack offset
	
	move	hold,spLast
	moveh	hold,last,>>spLast
	sub	r127,last,r0		;stackPointer (r127) = spLast - stackOffset
	
	;set other cores (not core 0) to sleep
	
	gssr	hold,0
	cmpeq	last,0
	br.tc	stop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
	;setup video
	
	gpc	r126,2
	br	setupVideo
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;
	
	move	r16,0
	
loop	
	;wait vblank

	gpc	r126,2
	;br	waitBlank
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;switch frames
	
	gpc	r126,2
	br	switchFrames
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot	
	
	;clear screen
	
	; move	hold,drawFrame
	; moveh	hold,last,>>drawFrame
	; ld	r1,last,0		;dest
	
	; move	hold,10125		;count
	; or	r2,last,0
	; gpc	r126,2
	; br 	fill8
	; move	r0,$fff			;value          ;delay slot
	; nop	;delay slot
	; nop	;delay slot
	; nop	;delay slot
	
	;enable measure 
	
	.execm	enablePerformanceCounter(unused)
	
	gpc	r126,2
	br	drawBG
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
		
	;dont measure this
	
	.execm	disablePerformanceCounter(unused)
	
	;******** performance 1 *********

	;push data to stack
	
	sub	r127,r127,6
	nop
	gssr	hold,pfcInstCacheHitCnt		;cache hit cnt
	st	last,r127,0
	gssr	hold,pfcFetchReloadCycleCnt	;fetch reload cycle cnt
	st	last,r127,2
	gssr	hold,pfcScoreStallCnt		;score stall cnt
	st	last,r127,4
	
	;draw string (counter)

	move	hold,font1Bit		
        moveh	hold,last,>>font1Bit  
        add	r0,last,0		;r0 = src
        
	move	hold,drawFrame
	moveh	hold,last,>>drawFrame
	ld	r1,last,0		;r1 = dest
	
       
        move	hold,$ffff		
        moveh	hold,last,$ffff	
        or	r3,last,0		;r3 = color
	
	move	hold,drawChar1Bit		
        moveh	hold,last,>>drawChar1Bit  
        add	r6,last,0		;r6 = draw function
	
	move	hold,performance1String		
        moveh	hold,last,>>performance1String  
        add	r7,last,0		;r7 = text address
	
	gpc	r126,2
	br	drawString	
	move	r4,10			;r4 = x			delay slot
	move	r5,26			;r5 = y			delay slot
	move	r2,720			;r2 = dest width	delay slot
	nop				;			delay slot
	
	;restore stack
	
	add r127,r127,6
	nop
	nop
	
	;******** performance 2 *********

	;push data to stack
	
	sub	r127,r127,4
	nop
	gssr	hold,pfcExecuteStallCnt		;exec stall cnt
	st	last,r127,0
	gssr	hold,pfcDispatchMergeStallCnt	;dsipatch merge stall cnt
	st	last,r127,2
	
	
	;draw string (counter)

	move	hold,font1Bit		
        moveh	hold,last,>>font1Bit  
        add	r0,last,0		;r0 = src
        
	move	hold,drawFrame
	moveh	hold,last,>>drawFrame
	ld	r1,last,0		;r1 = dest
	
       
        move	hold,$ffff		
        moveh	hold,last,$ffff	
        or	r3,last,0		;r3 = color
	
	move	hold,drawChar1Bit		
        moveh	hold,last,>>drawChar1Bit  
        add	r6,last,0		;r6 = draw function
	
	move	hold,performance2String		
        moveh	hold,last,>>performance2String  
        add	r7,last,0		;r7 = text address
	
	gpc	r126,2
	br	drawString	
	move	r4,10			;r4 = x			delay slot
	move	r5,43			;r5 = y			delay slot
	move	r2,720			;r2 = dest width	delay slot
	nop				;			delay slot
	
	;restore stack
	
	add r127,r127,4
	
	
	

	;
	
	br	loop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
stop	br	stop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
;******************************
;*
;* fill8 (8 longs)
;*	
;* r0 = value
;* r1 = address
;* r2 = count
;*
	
fill8
	nop

fill8Loop
	nop
	sub	r2,r2,1
	st	r0,r1,0
	st	r0,r1,2
	st	r0,r1,4
	st	r0,r1,6
	st	r0,r1,8
	cmpeq	r2,0
	br.tc	fill8Loop
	st	r0,r1,10	;delay slot
	st	r0,r1,12	;delay slot
	st	r0,r1,14	;delay slot
	add	r1,r1,16	;delay slot

	jmp	r126,0
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot	
	
;******************************
;*
;* drawString
;*
;* r0 = source (752bit width)
;* r1 = dest
;* r2 = dest width
;* r3 = color
;* r4 = x start
;* r5 = y start
;* r6 = drawFunction
;* r7 = text address
;*	

drawString

	.def	drawStringStackUse	16

	;prolog

	sub	r127,r127,drawStringStackUse	;sp-
	nop
	nop
	st	r16,r127,0	
	st	r17,r127,2	
	st	r18,r127,4
	st	r19,r127,6
	st	r20,r127,8
	st	r21,r127,10
	st	r22,r127,12	
	st	r126,r127,14
	
	;
	
	or	r16,r0,r0			;store source
	or	r17,r1,r1			;store dest
	or	r18,r2,r2			;store width
	or	r19,r3,r3			;store color
	or	r20,r6,r6			;store draw function
	or	r21,r7,r7			;store text address
	
	add	r22,r127,drawStringStackUse	;stack last
	
	;x & y is pixelwise
	
	fead	r2,r5		;	y * width
	add	r4,r4,r4	;x*2
	mul	hold		;
	add	r17,last,r17	;dest+=
	add	r17,last,r4	;dest+=x
	
	;char loop
	
textLoop
	
	ld	r4,r21,0	;load word
	add	r21,r21,2	;next address
	
	cmpeq	r4,37		;%
	br.ts	textProcessCmd
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	cmpeq	r4,0		;end
	br.ts	textLoopEnd
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;no cmd and not end so draw char
	
	gpc	r126,2
	br	textDrawChar
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	br	textLoop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
textLoopEnd	
	
	;epilog
	
	ld	r16,r127,0	;
	ld	r17,r127,2	;
	ld	r18,r127,4
	ld	r19,r127,6
	ld	r20,r127,8	
	ld	r21,r127,10	
	ld	r22,r127,12	
	ld	r126,r127,14
	
	nop	
	jmp	r126,0
	add	r127,r127,drawStringStackUse	;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
; 
; r4 = char
;

textDrawChar

	sub	r127,r127,2	;sp--
	nop
	nop
	st	r126,r127,0	;store lr

	;draw
	
	sub	hold,r4,32	;char offset
	and	r4,last,$ff	;secure and
	gpc	r126,2
	jmp	r20,0		;draw function
	or	r0,r16,r16	;source       delay slot
	or	r1,r17,r17	;dest         delay slot
	or	r2,r18,r18	;width        delay slot
	or	r3,r19,r19	;color        delay slot
	
	;
	
	ld	r126,r127,0	;get lr
	add	r127,r127,2	;sp++
	
	jmp	r126,0
	add	r17,r17,16	;destination + 8 pixel
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
; r22 = stack	
	
textProcessCmd

	ld	r4,r21,0	;load cmd
	add	r21,r21,2
	
	cmpeq	r0,120	;x -> hex		
	br	textProcessCmdHex
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	br	textLoop	;unknown
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
;	
	
textProcessCmdHex

	ld	r9,r22,0	;value from stack
	add	r22,r22,2	;goto next (up)
	
	move	r10,7
	
textProcessCmdHexLoop

	lsr	r4,r9,28	;use upper 4 bit	;delay slot
	gpc	r126,2	
	br	textDrawChar
	cmple	r4,9                                    ;delay slot
	add.ts	r4,r4,48	;16+32  0..9            ;delay slot
	add.tc	r4,r4,55	;23+32  A..F            ;delay slot
	nop						;delay slot

	cmpeq	r10,0
	br.tc	textProcessCmdHexLoop
	sub	r10,r10,1	;delay slot
	lsl	r9,r9,4		;delay slot
	nop			;delay slot
	nop			;delay slot
	
	br	textLoop	;go back
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
;******************************
;*
;* drawChar1Bit
;*
;* r0 = source (752bit width)
;* r1 = dest
;* r2 = dest width
;* r3 = color
;* r4 = char offset
;*

drawChar1Bit

	move	hold,$8000
	or	r8,last,0	;shift mask
	
	and	hold,r4,1	;even/odd
	cmpeq	last,0
	lsr.tc	r8,r8,8		;shift mask >> 8 if odd
	
	lsr	hold,r4,1	;offset / 2
	add	r0,last,r0	;to source address
	nop	
	move	r4,7
	ldw	r5,r0,0		;load 16 bit
	
lineLoop

	or	r7,r8,r8	;reset msk
	ldw	r6,r0,47	;next line load 16 bit
	nop
	
	lsr	hold,r7,0
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,0		;color to dest if bit set
	
	lsr	hold,r7,1
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,2		;color to dest if bit set
	
	lsr	hold,r7,2
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,4		;color to dest if bit set
	
	lsr	hold,r7,3
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,6		;color to dest if bit set
	
	lsr	hold,r7,4
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,8		;color to dest if bit set
	
	lsr	hold,r7,5
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,10	;color to dest if bit set
	
	lsr	hold,r7,6
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,12	;color to dest if bit set
	
	lsr	hold,r7,7
	and	hold,last,r5
	cmpeq	last,0
	st.tc	r3,r1,14	;color to dest if bit set
	
	;next line
	
	or	r7,r8,r8	;reset msk
	add	r1,r1,r2     	;delay slot
	ldw	r5,r0,94	;preload next line load 16 bit (would be thrown away, at last access)
	
	lsr	hold,r7,0
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,0		;color to dest if bit set
	
	lsr	hold,r7,1
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,2		;color to dest if bit set
	
	lsr	hold,r7,2
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,4		;color to dest if bit set
	
	lsr	hold,r7,3
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,6		;color to dest if bit set
	
	lsr	hold,r7,4
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,8		;color to dest if bit set
	
	lsr	hold,r7,5
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,10	;color to dest if bit set
	
	lsr	hold,r7,6
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,12	;color to dest if bit set
	
	lsr	hold,r7,7
	and	hold,last,r6
	cmpeq	last,0
	st.tc	r3,r1,14	;color to dest if bit set
	
	cmpeq	r4,0
	br.tc	lineLoop
	add	r0,r0,94	;delay slot
	sub	r4,r4,1		;delay slot
	add	r1,r1,r2     	;delay slot
	nop			;delay slot
	             	
	;epilog
		
	jmp	r126,0
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
;******************************
;*
;* setupVideo
;*

setupVideo

	move	r3,0
	move	hold,video320x225x32
	moveh	hold,last,>>video320x225x32	
	or	r0,last,0			;source data
	move	hold,UcVideoRHCountWVideoOn	;dest start
	or	r1,last,0
	
	
setupVideoLoop
	
	ldw	r2,r0,r3			;get data[r3]
	.execm	ucpSetParam(0,r1)		;param 0 = address ($8010 ++)
	.execm	ucpSetParam(1,r2)		;param 1 = value
	.execm	ucpCall(upfCallSubUCtrlWrite)	;set uctrl register
	cmpeq	r3,26
	br.tc	setupVideoLoop
	add	r3,r3,1
	add	r1,r1,1
	nop	;delay slot
	nop	;delay slot
	
	jmp	r126,0
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
video320x225x32	
		
	word	$00010097
	word	$017f017d
	word	$071f076f
	word	$0002001e
	word	$03a203a3
	word	$00000040
	word	$00000000
	word	$00000000
	word	$00080170
	word	$001e03a2
	word	$00040000
	word	$d0000000
	word	$00000000
	word	$00000000
	
;******************************
;*
;* waitBlank
;*	

waitBlank

	;emulator test

	.execm	ucpSetParam(0,UuLedR)	;param 0 = UuLedr / isEmu
	.execm	ucpCallReturn(t0,upfCallSubUCtrlRead)	;get uctrl register
	nop
	
	and	hold,t0,1
	cmpeq	last,0			
	br.tc	waitBlankEnd		;t0 <> 0 (emulator) -> waitBlankEnd 
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;sw test

	.execm	ucpSetParam(0,UcRSwichtWLedG)	;param 0 = UcRSwichtWLedG 
	.execm	ucpCallReturn(t0,upfCallSubUCtrlRead)	;get uctrl register
	nop
	
	and	hold,t0,1
	cmpeq	last,1			
	br.ts	waitBlankEnd		;t0(0) == 1 (SW0 is set) -> waitBlankEnd 
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	move	hold,UcVideoHITVCOUNT
	or	t0,last,0		;hitv count
	move	t1,930			;line
	
	.execm	ucpSetParam(0,t0)		;param 0 = address (hitv count)
	.execm	ucpSetParam(1,t1)		;param 1 = 930
	.execm	ucpCall(upfCallSubUCtrlWrite)	;set uctrl register
	
	move	hold,UcVideoRPVcountHit
	or	t0,last,0		;vcount hit adr
	
	.execm	ucpSetParam(0,t0)		;param 0 = address (vcount hit)
	
waitBlankLoop

	.execm	ucpCallReturn(t0,upfCallSubUCtrlRead)	;set uctrl register
	nop
	and	hold,t0,1
	cmpeq	last,1		;vcount hit?
	br.tc	waitBlankLoop	;no do again
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
waitBlankEnd

	jmp	r126,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
;
;	
	
drawBG	

	;prolog
	
	sub	r127,r127,2	;sp-
	nop
	nop
	st	r126,r127,0	
	
	;
	
	move	hold,texture512x512x8888
	moveh	hold,last,>>texture512x512x8888
	or	r31,last,0		;r31 = source
	
	move	hold,drawFrame
	moveh	hold,last,>>drawFrame
	ld	r32,last,0		;r32 = dest
	
	move	r33,512			;r33 = source width (in pixel)
	move	r34,360			;r34 = dest width (in pixel)
	move	r35,0		;y_start
	move	r36,0		;u = 0
	move	r37,0		;v = 0
	
	move	hold,0
	moveh	r38,last,$0	;su = 0001 0000
	
	;move	hold,512
	;moveh	r39,last,$0	;sv = 0001 0000
	
	;move	hold,512
	;moveh	r40,last,$0	;du = $0001 0000
	
	move	hold,zx
	moveh	hold,last,>>zx
	or	r0,last,0
	
	move	hold,zy
	moveh	hold,last,>>zy
	or	r1,last,0
	
	move	hold,$ffff
	or	r2,last,0
	ld	r39,r0,0
	ld	r40,r1,0
	
	nop
	add	r39,r39,64
	add	r40,r40,64
	nop
	
	and	r39,r39,r2
	and	r40,r40,r2
	
	nop
	st	r39,r0,0
	st	r40,r1,0
	
	;
	
	move	hold,$0
	moveh	r41,last,$0	;dv = $0001 0000	
	
drawLoop	
	
	or	r16,r32,r32	;dest adr
	or	r17,r34,r34	;dest width
	or	r18,r31,r31	;source adr
	or	r19,r33,r33	;source width
	move	r20,0		;x = 0
	move	r21,0		;length
	or	r22,r35,r35	;y
	
	;
	
	gpc	r126,2
	br	drawScanline
	or	r23,r36,r36	;u	delay slot
	or	r24,r37,r37	;v	delay slot
	or	r25,r40,r40	;du	delay slot
	or	r26,r41,r41	;dv	delay slot
	
	;

	cmpeq	r35,223
	br.tc	drawLoop
	add	r35,r35,1	;delay slot
	add	r36,r36,r38	;u=u+su			;delay slot
	add	r37,r37,r39	;v=v+sv			;delay slot		
	nop			;delay slot
		
	;epilog

	ld	r126,r127,0	;get lr
	add	r127,r127,2	;sp++
	
	jmp	r126,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	

;******************************
;*
;* drawScanline
;*
;* r16 = dest adr	-
;* r17 = dest width 	-
;* r18 = source adr	-
;* r19 = source width	-
;* r20 = x
;* r21 = length
;* r22 = y		-
;* r23 = u 16:16	-
;* r24 = v 16:16	-
;* r25 = du 16:16	-
;* r26 = dv 16:16	-
;*

	.macro	drawScanLineGetSourcePixel(_result,_u,_v,_du,_dv,_source_adr,_source_width_x2)
	
	lsl	hold,_u,7			;      u << 7   
	lsr	hold,last,23			;rx = (u << 7)>>7 >> 16
	lsl	_result,last,1			;rx *= 2 (addr to long)
	lsl	hold,_v,7			;      v << 7
	lsr	hold,last,23			;ry = (v << 7)>>7 >> 16
	fead	last,_source_width_x2		;ry * source Width (x2)
	add	_u,_u,_du			;u = u + du
	mul	hold				;  		  [	ry * source Width (x2)			]
	add	hold,last,_result		;  		  [			  + rx (x2)		]
	ld	_result,last,_source_adr	;_result	ld[ 				+sourceAdr 	]
	add	_v,_v,_dv			;v = v + dv
	
	.endmacro

drawScanline

	  ; bmData += x_start + y * visibleWidth;   // calc dest start position
	  
	add	r17,r17,r17	;r17 = dest width * 2
	add	r19,r19,r19	;r19 = source width * 2
	add	r20,r20,r20	;r20 = x * 2
	
	fead	r22,r17		;			y * visible width 
	nop
	mul	hold
	add	hold,last,r16	;	    dest_adr + 
	add	r16,last,r20	;dest_adr =                                 + x 
	
	
	;r15  = count - 1
	
	move	r27,21
		
drawScanlineX8

	.execm	drawScanLineGetSourcePixel(r0,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r1,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r2,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r3,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r4,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r5,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r6,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r7,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r8,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r9,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r10,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r11,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r12,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r13,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r14,r23,r24,r25,r26,r18,r19)
	.execm	drawScanLineGetSourcePixel(r15,r23,r24,r25,r26,r18,r19)
	
	st	r0,r16,0
	st	r1,r16,2
	st	r2,r16,4
	st	r3,r16,6
	st	r4,r16,8
	st	r5,r16,10
	st	r6,r16,12	;delay slot
	st	r7,r16,14	;delay slot
	st	r8,r16,16
	st	r9,r16,18
	st	r10,r16,20
	st	r11,r16,22
	st	r12,r16,24
	st	r13,r16,26
	
	cmpeq	r27,0
	br.tc	drawScanlineX8
	st	r14,r16,28	;delay slot
	st	r15,r16,30	;delay slot
	add	r16,r16,32	;delay slot
	sub	r27,r27,1	;delay slot
	
	jmp	r126,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
	; unsafe private void drawScanline(UInt32* bmData, UInt32* smData, UInt32 x_start, UInt32 x_end, UInt32 y, UInt32 u, UInt32 v, UInt32 du, UInt32 dv)  // hard
        ; {
            ; bmData += x_start + y * visibleWidth;   // calc dest start position

            ; for (UInt32 x = x_start; x < x_end; x++)
            ; {
                ; UInt32 rx = u >> 16;
                ; UInt32 ry = v >> 16;

                ; rx &= (UInt32)0x1ff;
                ; ry &= (UInt32)0x1ff;

                ; *bmData++ = (UInt32)smData[rx + (ry * sourceWidth)];

                ; u += du;
                ; v += dv;
            ; }	

zx	word	$1000
zy	word	$1000
	    
;******************************
;*
;* switchFrames
;*	

switchFrames

	move	hold,displayFrame
	moveh	hold,last,>>displayFrame
	or	t0,last,0
	
	move	hold,drawFrame
	moveh	hold,last,>>drawFrame
	or	t1,last,0
	nop
	
	ld	t2,t0,0
	ld	t3,t1,0
	
	;t2 = display
	;t3 = draw
	
	move	hold,UcVideoMEMStartAdrLow
	or	t4,last,0
	move	hold,UcVideoMEMStartAdrHigh
	or	t5,last,0
	
	or	t6,t3,t3	;low
		
	.execm	ucpSetParam(0,t4)		;param 0 = address (start address low)
	.execm	ucpSetParam(1,t6)		;param 1 = value
	.execm	ucpCall(upfCallSubUCtrlWrite)	;set uctrl register
	
	lsr	t6,t6,16	;high
	move	hold,UcVideoMEMLatch
	or	t4,last,0
	
	.execm	ucpSetParam(0,t5)		;param 0 = address (start address high)
	.execm	ucpSetParam(1,t6)		;param 1 = value
	.execm	ucpCall(upfCallSubUCtrlWrite)	;set uctrl register
	
	.execm	ucpSetParam(0,t4)		;param 0 = address (latch out)
	.execm	ucpCallReturn(t4,upfCallSubUCtrlWrite)	;set uctrl register
	
	nop
	fead	t4,t4	;sync
	
	jmp	r126,0
	st	t3,t0,0	;delay slot
	st	t2,t1,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
displayFrame	word	frame0
drawFrame	word	frame1	
	
performance1String
	.stringz	"IHIT %x IRLC %x SCOS %x"
	
performance2String
	.stringz	"EXES %x DPMS %x"	
	