//   Analyze.bcpl 
// Needs GP,  Template,  SpruceMl (ML routine, included in package)
get "streams.d"

external [ Gets; Puts; keys; dsp;Wss; SetFilePos; Resets; Closes; Endofs; CreateDisplayStream;
	ShowDisplayStream; DoubleAdd; DoubleSub; Ws; MulFull; PutTemplate; OpenFile ]
static [ outStream; blockCt; f ]
structure [ left byte; right byte ]

let Analyze(v, switches) be [
	for i = 1 to 999 do [ switchon switches!i into [  case 0: break;  default: endcase ] ]
	// let name=vec 256; let sw=vec 128; SetupReadParam(name, sw)
	f = OpenFile("Analee", ksTypeReadOnly, wordItem) //!!
	if f eq 0 then [ PutTemplate(outStream,"$S not found*N","Analee"); finish ]
// measureTable address was put into location 721
	let p = vec 1; p!0 = 1; p!1 = (#76721) lshift 1
	SetFilePos(f, p); p!0 = 0; p!1 = (Gets(f)-#400) lshift 1; SetFilePos(f, p)
	outStream = OpenFile("Analyze.lst",ksTypeWriteOnly, charItem)
	Wss(outStream, "*N   Analyze of February 26, 1979*N")
// print entries
let time0 = vec 1; time0!0 = 0; time0!1 = 0
while valof [ blockCt = Gets(f); resultis blockCt ] do
	[
	let eventTime = vec 1
	eventTime!0 = Gets(f); eventTime!1 = Gets(f)
	let band = Gets(f)
	if time0!0 eq 0 then [ time0!0 = eventTime!0; time0!1 = eventTime!1 ]
	DoubleSub(eventTime, time0)
	let t1, t2 = vec 1, vec 1
	MulFull(((eventTime!1)&#7774) rshift 2,38,t1)
	MulFull((eventTime!0)*38, 1024, t2)
	DoubleAdd(t1,t2)
	for i = 1 by 2 to blockCt-4 do
		[
		let index, value = Gets(f), Gets(f)
		let s = selecton index into
			[
			case 0:	"malfunction"
			case 1: "proceedCode"
			case 2: "nonF"
			case 3: "coldStart"
			case 4: "consecutiveErrors"
			case 5: "page sync"
			case 6: "signalBand"
			case 7: "nVisibleBands"
			case #10: "Func"
			case #11: "nPagesPrinted"
			case #12: "ROS status wd. 0"
			case #13: "ROS status wd. 8"
			case #14:  "Orbit Status"
			case #15: "ROSCommand table"
			case #16: "Shit"
			case #17: valof [ let chg = value<0
					  value = value&#77777
					  resultis chg? "Count-H starts changing",
							"Count-H stops changing"
					]
			case #20: "FeedASheet state"
			case #21: "Reading bands"
			case #22: "Reading fonts"
			case #23: "Random Interesting Event"
			default: "[unused]"
			]
		PutTemplate(outStream,"$10EUD$6D$8UO    $S*N",t1,band,value,s)
		]
	]
	Closes(outStream)
    ]
// DCS, February 26, 1979  6:03 PM, analyze Dover Printer measurement results
// February 27, 1979  9:51 AM, ready to test table access
// February 27, 1979  2:32 PM, simple presentation to dsp works
// February 27, 1979  2:38 PM, output to file, reorg. presentation
// March 5, 1979  7:29 AM, better use of space
// March 5, 1979  5:06 PM, different count-H legend
//