# analagous to unix schedule(), sorta 

#rm listing

#echo "
# Switch processes using interrupt gate with stack-switch, which requires
#as little as a single static TSS, the way I read it. We have a stock IRET
#(dismiss) frame (no error code, 32 bit) on the INTERRUPT stack, not
#sleepy's stack. Intel left me a user bit in a segment discriptor, and just
#for grins, that's the is-a-process bit in colorgOS. (It happens to be the
#1 meg bit.) Thus the GDT is your process list, and your process table
#(crib) maps to it 1:1 at a size scale of 8:64 per entry. We want the
#stack-switching for security and to allow stack-staining, i.e. reading a
#child routines stack frame."

Astash=$((0x6f00))
Bstash=$((0x6f04))
sleepy=$((0x6f08))
crib=$((0x7000))
maxprocess=$((0x6f10))

L switch # from sleepy to frisky
	= A to @ $Astash
	= 4 SP to A		# sleepy's CS
	downshift 3 to A 	
	= A to @ $sleepy
	upshift 6 to A		
	+ $crib to A
	pull @ 28 A		# leave an exit to enter later
	pull @ 32 A
	pull @ 36 A
	pull @ 40 A
	pull @ 44 A
	= DI to  @ 24 A
	= SI to  @ 20 A
	= BP to  @ 16 A
	= D  to  @ 12 A
	= C  to  @ 8 A
	= B  to  @ 4  A
	= @ $Astash to B
	= B to @  A				# sleepy is in his crib	
	= @ $sleepy to A
L switch_perdescriptor				# a loop. usually brief.
	-test @ $maxprocess to A
		when	not	sign	switch_dontring
	zero A			
L switch_dontring
	1+  A
	address  $(($permanent_gdt+4))  A *2^ 2 to B
	ANDtest `binary 10000 00000000 00000000` to B
		when	zero	gettin_frisky
	jump	switch_perdescriptor
L gettin_frisky
	pull A
	pull B
	pull C
	pull D
	pull BP
	pull SI
	pull DI
		dismiss				# in the out door.

