
	org	hcoreStartPc

	nop
	nop
	nop
	nop	;preload

	gssr	r127,0		;get core number
	cmpeq	last,0
	br.tc	notCoreZero
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	
	
	;
	
loop	
	
	move	hold,color
	moveh	hold,last,>>color
	or	r31,last,0	;source
	
	move	hold,$0b4a
	moveh	hold,last,$d000
	or	r32,last,0	;dest
	
	move	r33,0			;r33 = source width (in pixel)
	move	r34,180			;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 
	
	move	hold,0
	moveh	r39,last,$0	;sv 
	
	move	hold,0
	moveh	r40,last,$0	;du 
	
	move	hold,$0
	moveh	r41,last,$0	;dv 
	;
	
	
	nop
	
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,100
	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

	
	br	loop
	nop
	nop
	nop
	nop
	
notCoreZero	br	notCoreZero
	nop
	nop
	nop
	nop
	

color		
	word	$0fff0f0f
	word	$00ff001f
	word	$0fff0f2f
	word	$01fe013e
	word	$0fff0f4f
	word	$02fd025d
	word	$0fff0f6f
	word	$03fc037c
	
;******************************
;*
;* 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)		]
	ldw	_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	r15,44
		
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)
	
	stw	r0,r16,0
	stw	r1,r16,1
	stw	r2,r16,2
	stw	r3,r16,3
	stw	r4,r16,4
	stw	r5,r16,5
	
	cmpeq	r15,0
	br.tc	drawScanlineX8
	stw	r6,r16,6	;delay slot
	stw	r7,r16,7	;delay slot
	add	r16,r16,8	;delay slot
	sub	r15,r15,1	;delay slot
	
	jmp	r126,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	
	
	
	
	
	