 	
	org	hcoreStartPc
	
	
	
	.def	spLast		$d4000000
	.def	stackSize	1048576	;4 MegaByte per core
	
startHCore

	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

	;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	

	;prepare parallel execution
	
	sub	r127,r127,2		;
	move	hold,$00ff
	moveh	hold,last,$00ff
	st	last,r127,0		;store color on stack
	
	spa	r127,0			;hand_over_address = sp
	
	move	hold,parallelFunction
	moveh	hold,last,>>parallelFunction
	or	r0,last,0
	spa	last,1			;jump_address	   = parallelFunction
	
	move	hold,31
	spa	last,2			;max_cores 	   = use as max 31 cores
	
	gpc	r126,2			;return address
	
	rake			;allocate and jump parrallel
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot

	add	r127,r127,2	;stack pop
	
	
	;do more thinks after this
	;...
	;...
	;...
	
hend	br	hend
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;----------------------------------------------------------------------------
	;- parallel
	;----------------------------------------------------------------------------
	
parallelFunction

	gpa	r0,0		;hand_over_address
	gpa	r1,2		;max_cores working here
	gpa	r2,3		;core number of working cores
	
	rqld	r0,0		;hand over address is stack from core 0
	ld	r0		;get color
	
	fead	r2,2880
	nop
	mul	r2		;core number * 2lines
	
	move	hold,frame
	moveh	hold,last,>>frame
	add	hold,last,r2	;frame + (core number * 2lines)
	add	r1,last,16	;8 pixel to right
	
	nop
	st	r0,r1,0

	;...
	;...
	;...
	;...
	
	jmp	r126,0		;return
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot