	;; testThreadMig.asm
	;; two threads talking to one migratory thread
	;; the migratory thread also has two maps to remote and local memory each
main:
	SPAWNLBC movingThread, q0
	SPAWNLBC fixedThreads, q1
	SPAWNLBC fixedThreads, q2
	MAPQC    q10, q0, @q0
	MAPQC    q11, q0, @q1
	MAPQC    q12, q0, @q2
	MOVE     @q1, q10	; send fixed threads
	MOVE     @q2, q10
	PROCID   q10		; send my procID to the fixed thread
	MOVECL   50, q11	; map point for thread 1
	MOVE     @q0, q11	; send mobile thread to fixed threads
	MOVECL   0x40, q11	; set init memory value
	MOVECL   51, q12	; map point for thread 2
	MOVE     @q0, q12
	MOVECL   0x140, q12
	MOVE     q3, q20	; q20 has the mobile thread capability
	CONSUME  @q3		; wait for mobile thread to signal readiness
	PRINTS   "migrating..."
	HALT
movingThread:
	;; q50-9 reserved for incoming maps
	;; except for q0, which is my spawner's interface
	ALLOCATEC 2, q10	; q10, q11 hold local capabilities
	ALLOCATEC 2, q11
	MMS      q30, q31	; q30-33 reserved for local capability interaction
	MMS      q32, q33
	MOVE     @q10, q30
	MOVE     @q11, q32
	MOVECL   0, q30		; init two local capabilities
	MOVECL   0, q32
	MOVECL   0x10, q31
	MOVECL   0x20, q33
	MOVECL   1, q30
	MOVECL   1, q32
	MOVECL   0x11, q31
	MOVECL   0x21, q33
	;; in q0, I expect my two other capabilities
	MOVE     q0, q12	; q12, q13 hold the other thread capabilitie
	MOVE     q0, q13
	MOVE     q0, q14	; procid of caller is in q14
	;; q40-9 reserved for mappings to other queues
	MAPQC    q40, q1, @q12	; map back to other threads
	MAPQC    q41, q1, @q13
	MAPQC    q42, q3, @q14	; map back to caller
	PROCID   q42		; send procid to caller
	;; now, do a loop where I just continuously print out an exchange status
	MOVE     q50, q60	; mem capability from fixed thread 1 (q60)
	MOVE     q51, q61	; mem cap from fixed thread 2 (q61)
	;; establish exchanges to remote and local capabilities
	EXCH     q70, q71, q72	; remotes
	EXCH     q73, q74, q75
	EXCH     q76, q77, q78	; locals
	EXCH     q79, q80, q81
	MOVE     @q60, q70
	MOVE     @q61, q73
	MOVE     @q10, q76
	MOVE     @q11, q79
	;; wait for remotes to finish initialization
	CONSUME  q50
	CONSUME  q51
	;; signal the remotes to go
	MOVECL   0, q40		; send remotes their initial value
	MOVECL   0x100, q41
	;; now do my loop
	;; in this loop, I exchange with each of four memory locations
	;; print those values
	;; send data to my fixed threads
	;; and repeat
	;; on iteration 4, I tell my parent to migrate me
	MOVECL   0, q100	; my loop counter variable q100
	MOVECL   0x40, q101	; rc1 iv
	MOVECL   0x140, q102	; rc2 iv
	MOVECL   0x10, q103	; lc1 iv
	MOVECL   0x20, q104	; lc2 iv
	MOVE     q101, q71
	MOVE     q102, q74
	MOVE     q103, q77
	MOVE     q104, q80
	;; expectations--
	;; remote cap 1 returns 0x40 starting count
	;; remote cap 2 returns 0x140 starting count
	;; local cap 1 returns 0x10 starting count
	;; local cap 2 returns 0x20 starting count
	;; mobile thread 1 should start printing 0x50
	;; mobile thread 2 should start printing 0x150
testLoop:
	MOVECL   0, q70		; load up the addresses
	MOVECL   0, q73
	MOVECL   0, q76
	MOVECL   0, q79
	ADDC     @q100, 0x50, q105
	MOVE     q105, q40	; send shit on to the fixed threads to print
	ADDC     @q100, 0x150, q105
	MOVE     q105, q41
	PRINTSNNL   "iter "
	PRINTQ   @q100
	PRINTQX  @q72		; rc1
	PRINTQX  @q75		; rc2
	PRINTQX  @q78		; lc1
	PRINTQX  @q81		; lc2
	PRINTQX  q50		; rt1  starts at 0
	PRINTQX	 q51		; rt2  starts at 0x100
	ADDC     q72, 1, q71
	ADDC     q75, 1, q74
	ADDC     q78, 1, q77
	ADDC     q81, 1, q80
	SEQC     @q100, 4, q111
	BRZ      q111, jumpover
	MOVECL   8, q120
	PROCID   q121
	MIGRATE  q121, q120	; migrate to node 8
	MOVECL   0, q42		; signal parent to migrate me on the 5th iteration
jumpover:		
	SEQC     @q100, 15, q111
	BRZ      q111, jump2
	MOVECL   0, q120
	PROCID   q121
	MIGRATE  q121, q120	; migrate to node 0
jump2:	
	ADDC     q100, 1, q100
	SEQC     @q100, 20, q110
	BRZ      q110, testLoop	; do it 20 times
	PRINTS   "mobile thread done"
	HALT	
	;; fixed thread stuff...
fixedThreads:
	ALLOCATEC 2, q10	; q10 holds my local capability
	MOVE     q0, q5
	MAPQ     q20, q5, q0	; map to the fixed thread (q20 local, remote spec'd in q5 (it is 50 or 51))
	MOVE     q0, q70	; get my memory init value
	MMS      q30, q31
	MOVE     @q10, q30
	MOVECL   0, q30
	MOVE     @q70, q31
	ADDC     q70, 1, q70
	MOVECL   1, q30
	MOVE     @q70, q31
	MOVE     @q10, q20	; send my capability to the caller
	MOVECL   0, q20		; tell the caller to start its test run
	MOVE     q1, q60	; wait for my initial value
fixedLoop:	
	PRINTQX  q1		; wait for data from mobile thread
	MOVE     @q60,q20	; send a copy back to the mobile thread
	ADDC     q60,1,q60
	BR       fixedLoop
	HALT

