// C U R S O R   
// errors --
//
// Stuff to keep a display going in the cursor so you can tell something is
// happening.
//
//CursorChar(c) -- puts character in upper left-hand corner
//CursorDigit(d) -- puts digit in upper right-hand corner
// or CursorDigit() -- increments it.
//CursorToggle(i) i=0,1,2,3 -- toggles one of 4 remaining areas
//

// outgoing procedures
external
	[
	CursorChar
	CursorDigit
	CursorToggle
	]

// outgoing statics
//external
//	[
//	]
//static
//	[
//	]

// incoming procedures
external
	[
//OS
	Zero
	]

// incoming statics
//external
//	[
//	]

// internal statics
static
	[
	cursorLastDigit
	]

// File-wide structure and manifest declarations.

manifest [
	cursorX=#426
	cursorY=#427
	cursorBits=#431
	]

// Procedures

let CursorChar(c) be
[
   Zero(cursorBits, 16)
   @cursorX=303-8
   @cursorY=404-8
   cursorLastDigit=0

   CursorPutUp(c, false)
]

and CursorDigit(d; numargs n) be
[
   if n eq 0 then d=cursorLastDigit+1
   d=d rem 10
   cursorLastDigit=d

   CursorPutUp(d, true)
]

and CursorToggle(i,newx;numargs n) be
[
   let mask=(i&1) ne 0? #377, #177400
   let base=((i gr 1)? 12, 8)+cursorBits
   for i=0 to 3 do base!i=base!i xor mask
   if n eq 2 then @cursorX=newx
]

and CursorPutUp(c, right) be
[
   let ix=selecton c into
	[
	case $F: 40
	case $S: 5*4
	case $C: 44
	case $P: 48
	case $K: 52
	default: c*4
	]

   let BM=table [
	#174; #42104; #42104; #42174;	//0
	#20; #30020; #10020; #10174;	//1
	#174; #42004; #4020; #20174;	//2
	#174; #2004; #36004; #2174;	//3
	#104; #42104; #76004; #2004;	//4
	#174; #40100; #76004; #2174;	//5, S
	#174; #40100; #76104; #42174;	//6
	#174; #2004; #4020; #20100;	//7
	#174; #42104; #76104; #42174;	//8
	#174; #42104; #76004; #2174;	//9
	#174; #40100; #74100; #40100;	//F
	#174; #40100; #40100; #40174;	//C
	#174; #42104; #76100; #40100;	//P
	#104; #44120; #60120; #44104;	//K
	]

   let map=BM+ix
   let m1=(right ne 0)? #377, #177400
   let m2=m1 xor -1

   for i=1 to 7 do
	[
	let w=map!(i/2)
	if (i&1) ne 0 then w=w lshift 8
	if right then w=w rshift 8
	(cursorBits-1)!i=((cursorBits-1)!i & m2)%(w&m1)
	]
]