.titl SpruceMeasureMl

; See also SpruceMeasure.bcpl, for explanation of intent

; InitMeasure(measurePtr, measureTableSize, lvLocals-1)
;	meaurePtr is a block big enough for expected events; lvLocals-1 precedes block of
;	5 locals to be monitored
; TickMeasure()
;	called by Measure. Should also be called from clock interrupt

; interpretation of indices: (for locations, add 100 or 200)
;0:	malfunction
;1:	proceedCode
;2:	nonF
;3:	coldStart
;4:	consecutiveErrors
;5:	PageSync (0 = waiting for 0, 2 = have 0, 1 = waiting for 1, 3 = have 1)
;6:	signalBand
;7:	nVisibleBands
;10:	Orbit Command Table pointer
;11:	nPagesPrinted
;12:	ROS Status, word 0 [only updated while NOT imaging]
;13:	ROS Status, word 8 [only updated while NOT imaging]
;14:	Orbit Status [updated after reading word 8, above, or between bands while imaging]
;15:	Command Table Pointer [updated only while imaging, indicates execution of band-triggered cmds]
;16:	Shit Register, sometimes updated during imaging
;17:	sign+band if count-H is changing, last band otherwise
;20:	-1 if Attempt to feed a sheet is being made from emulator -- not imaging
;21:	-1 if band pages are being read from disk.
;22:	-1 if font pages are being read from disk.
;23:	Random interesting event (when band # itself is interesting, etc.)

; Stack Offsets
savedPC=1
temp=2
extraArguments=3

; R-Register identifiers
rArg1 = 520	;r52
rProgResult = 660 ;r66
rShit = 540	;r54
rnBandsM1 = 500	;r50

STORER=70000

	.srel
	.ent TickMeasure, InitMeasure, measureTable, measuring
TickMeasure:	.TickMeasure
; Measure: .Measure
InitMeasure: .InitMeasure
measureTable: 0
measuring: 0
.save3: save3

	.nrel

	.bext	signalBand, nVisibleBands, nPagesPrinted, Func, Debug, DebugSystem
	0;	the EMM, WSD, JC memorial extra word

.measuring: measuring
IsMeasuring: 20000
.DebugSystem: DebugSystem

.InitMeasure:;(measurePtr, measureSize, lvLocals-1)
	inc	3,3
	sta	3,save3
	sta	0,evPtr
	add	0,1
	lda	0,@.DebugSystem
	lda	3,IsMeasuring
	and#	3,0,snr
	 jmp	 @save3
	sub	0,0
	sta	0,@evPtr
	lda	0,C2mmP10
	sub	0,1
	sta	1,evMax
	lda	1,3,2
	sta	1,locSrcM1
	lda	0,C125252	;initial values, to force initial event
	lda	1,C140mmM1	;end address
	lda	3,CM40Pmm	;neg wd. ct
	BLKS
	mknil	1,1
	sta	1,@.measuring
	jmp	@save3

; Move selected locals to test area
.TickMeasure:
	isz	entCt
	77400	;reentered! WHY?
	sta	3,save3
	sta	2,save2
	lda	0,locSrcM1
	lda 	1,C104	; x determined by end of locals area in 100 (current vals) block
	lda	3,CM5	; x as above
	BLT		; 61005
; Move selected statics and other fixed memory locations to test area
	lda	0,@.signalBand
	sta	0,106
	lda	0,@.nVisibleBands
	sta	0,107
	lda	0,@.nPagesPrinted
	sta	0,111
	lda	3, @.A720
	sta	3,110
	sz	3,3
	jmp	imaging
; Not imaging -- read ROS status words 0 and 8, Orbit status
	mknil	0,0
	sta	0,130	;Band # in progress
	sub	1,1
	sta	1,117	;Count-H is not changing
	sta	1,firstChange
	jsr	GetStatus
	sta	0,112	;ROS Status word 0
	lda	1, CWord8
	jsr	GetStatus
	sta	0,113	;ROS Status word 8
	lda	0,orbStat
	jmp	record


.signalBand: signalBand
.nVisibleBands: nVisibleBands
.nPagesPrinted: nPagesPrinted
.Func: Func

; constants based on mm, the number of events being monitored
; MM=24 2-24-79
CM1mm: -124
CMmm: -24
C140mmM1: 163
Cmm: 24
CMmmP1: -25
CM40Pmm: -64
C4: 4
C2mmP10: 60
CImagePage: 14
CSign: 100000

; other constants
CESize: 5
C77: 77
CfRosStatus: 16
CWord8: 20000
CM5: -5
C104: 104
C125252: 125252
COrbBits: 54220
entCt: -1
orbStat: 0

.A720: 720
.A115: 115
.A130: 130

evPtr: 0
evMax: 0
locSrcM1: 0
save3: 0
save2: 0
firstChange: 0

; Imaging -- Record progress
imaging:
	lda	0,0,3	;Orbit function directive
	lda	1,CImagePage	;if not really imaging, in the middle of utility op,
	se	0,1	;just ignore the whole thing
	 jmp	 forgetIt
	lda	0,.A115
	STORER+rArg1; 115 ← Arg1
	STORER+rShit ;116 ← Shit
	lda	0,.A130
	STORER+rnBandsM1 ;130 ← NBandsM1
	STORER+rProgResult ; 131 ← ProgResult
	lda	0,firstChange
	sz	0,0
	 jmp	 noChg
	lda	0,130
	lda	1,170
	sta	0,170
	slt	0,1
	 jmp	 noChg
	isz	123		;random interesting event
	dsz	firstChange
noChg:
	lda	3,110
	lda	0,11,3

; Create an event record, off 3
record:	lda	1,COrbBits
	and	1,0
	sta	0,114	;Selected Orbit status when reading word 8, or at end of most recent band
	lda	0,131
	lda	1,171
	sta	0,171
	lda	3,CSign
	se	1,0
	add	3,0
	sta	0,117		;neg if count-H is changing, else band #
forgetIt:
	lda	3,evPtr
	RCLK
	sta	0,1,3
	sta	1,2,3
	lda	0,130
	sta	0,3,3
	lda	2,CMmmP1

; Differences inner loop
recInc:	inc	2,2,snr
	 jmp	recEnter
	lda	0,124,2	; 100 to 1mm-1
	lda	1,164,2	; 140 to 140mm-1
	sne	0,1
	 jmp	recInc
recEntry:
	sta	0, 5, 3
	lda	0,Cmm
	add	2,0
	sta	0,4,3
	inc	3,3
	inc	3,3
	jmp	recInc

; ac3 = evPtr+2*#entries
recEnter:
	lda	1,evPtr
	sub	1,3,snr
	 jmp	 noEntry
; ac3 = 2*#entries
	lda	0,C4
	add	0,3
; ac3 = 4+2*#entries (size of new entry block)
	sta	3,@evPtr
	add	1,3
; ac3 = ptr to next entry (don't update if no room)
	lda	0,evMax
	sub	3,0
	movl	0,0,snc
	sta	3,evPtr
	sub	0,0
	sta	0,@evPtr
noEntry:
; copy new into old
	lda	0,C77
	lda	1,C140mmM1
	lda	3,CMmm
	BLT
; All done
	lda	3,save3
	sta	2,save3	;0, not in measure routine any more
	lda	2,save2
	dsz	entCt
	jmp	1,3

GetStatus:
	lda	0,@.Debug
	sz	0,0
	jmp	0,3
	sta	3,.sv3
	lda	0,COrbitStatus
	jsr	doit
.func:	0
.fwd:	0
	0
	0
	0
	0
	0
	0
.result:
	0
.orbStat:
	0
	0
	0
	0
	0
	0
	0
	0
	0
	0
	0
doit:	skeven	3,3		;double-word boundary!
	inc	3,3
	sta	0,0,3
	sta	1,1,3
	sta	3,@.AA720
	lda	0,COrbitStart
	SIO
	lda	0,@.AA720
	sz	0,0
	jmp	.-2
	lda	0,.orbStat-.func,3
	sta	0,orbStat
	lda	0,.result-.func,3
	jmp	@.sv3


.Debug: Debug
.sv3: 0
COrbitStatus: 16
COrbitStart: 4
.AA720: 720
	.end

;; DCS, February 19, 1979  2:24 PM, created
;; February 21, 1979  11:34 AM, working on it
;; February 22, 1979  5:32 PM, substantial progress
;; February 24, 1979  3:32 PM, almost done
;; February 24, 1979  4:31 PM, ready for test
;; February 24, 1979  4:47 PM, syntax, addressing problems
;; February 24, 1979  5:21 PM, adjust some of the measurees
;; February 25, 1979  12:15 PM, major modification, move Measure(----,----) to DoverPrint
;; February 26, 1979  9:11 AM, try to find cause of occasional trap
;; February 27, 1979  9:19 PM, local DoFunc (GetStatus), to eliminate Func table enclobberment!
;; March 2, 1979  9:09 AM, fix raunchy bug preventing operation with Debug false
;; March 3, 1979  4:18 PM, wrong status location argument to GetStatus
;; March 4, 1979  7:13 AM, even-word alignment for orbit table
;; March 5, 1979  7:31 AM, record orbit status only when selected bits change, record random
;;	interesting event (23) when band # first gets smaller, after imaging begins
;; March 5, 1979  8:25 AM, repair orbit status fetch
;; March 12, 1979  10:10 PM, named SpruceMeasureMl, mods to Init for permanent use, by Swinehart
;;