 	
	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore"
	.include	"tools.i"
	
	org	hcoreStartPc
	
	.def	spLast		$d4000000
	.def	stackSize	1048576	;4 MegaByte per core
	
startHCore

	move	hold,totalPixelCount		;delay slot
	moveh	hold,last,>>totalPixelCount	;delay slot
	or	r16,last,0			;delay slot
	udivinit	r1,r0,4
	

	br	startHCore
	nop
	nop
	nop
	nop

	move	hold,stackSize
	moveh	hold,last,>>stackSize
	or	r0,last,0
	gssr	hold,0			;get core number		
	fead	last,r0	
	nop
	mul	r0			;stackOffset = core number (0..n) * stackSize

	;every core has counter for test
	
	move	r120,0
	
	;set stack offset
	
	move	hold,spLast
	moveh	hold,last,>>spLast
	sub	r127,last,r0		;stackPointer (r127) = spLast - stackOffset	
	
	;let only core 0 run, others are in idle state
	
	gssr	hold,0			;get core number
	cmpeq	last,0			;core 0?
	br.ts	startHCore0
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;other cores goto sleep
	
	gpc	r126,0	;gpc doesnt accept negative values (fix me)
	nop
	nop
	nop
	idle	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;lets go
	
startHCore0	
	nop
loop

	;prepare parallel execution
	
	;sub	r127,r127,2		;
	
	move	r0,31
	;.execm	doRake(r127,r0,parallelFunction)	;handover = sp, max_cores = r0, target = parallelFunction
	.execm	doRakeSync(r127,r0,parallelFunction)	;handover = sp, max_cores = r0, target = parallelFunction
	
	;add	r127,r127,2	;stack pop
	
	
	;do more thinks after this
	;...
	;...
	;...
	
	br	loop
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;----------------------------------------------------------------------------
	;- parallel
	;----------------------------------------------------------------------------
	
parallelFunction

	gpa	r13,0		;hand_over_address
	gpa	r1,2		;max_cores working here
	gpa	r12,3		;core number of working cores
	
	;rqld	r0,0		;hand over address is stack from core 0
	
	
	fead	r12,5760
	nop
	mul	r2		;core number * 4lines
	
	;store counter as color
	
	move	hold,frame
	moveh	hold,last,>>frame
	add	hold,last,r2	;frame + (core number * 4lines)
	add	hold,last,7200	;5 lines offset
	add	r1,last,16	;8 pixel to right
	nop
	
	st	r120,r1,0
	st	r120,r1,2
	st	r120,r1,4
	st	r120,r1,6
	st	r120,r1,8
	st	r120,r1,10
	st	r120,r1,12
	st	r120,r1,14
	
	move	hold,r1
	add	r1,last,1440	;next line
	nop
	
	st	r120,r1,0
	st	r120,r1,2
	st	r120,r1,4
	st	r120,r1,6
	st	r120,r1,8
	st	r120,r1,10
	st	r120,r1,12
	st	r120,r1,14
	
	
	add 	r120,r120,1
	
	.execm 	returnSync(r13,r12,r0)
	
	