;Edits:
;  12 Nov. 1982 2:10 pm PST (Friday)
;  Adding a "Ready to accept feed" command at St21

.LOC 0
RESTART:
	JMP STARTUP;		POWER ON RESET
	JMP INTHANDLE;		external interrupt vector
	JMP TimerInterrupt;	This is the timer interrupt loc: forget it

;INITIALIZATION ROUTINE - THIS ROUTINE INITIALIZE REGISTERS , FLAGS &USART
STARTUP:
	DIF			;in case we need to initialize some variables.
	EXI			;enable external interrupts when we enable
	DTI
	LPI	Bank2
	STB	NTestSW
	Movt	Buttons		;this says "Print" switch is not depressed
	Mvi	DspOnes 00
	Mvi	DspTens 00
	Mvi	CPYSEL 00
	MVI	ROUT1 00
	MVI	WDTtimeout 00		; for watch dog timer hack
	MVI	ROUT0 084	;turn on the "OFF LINE" led
	MVIT	084
	OUTT	OUT0
	STB	LEDRTN		;set up to strobe the leds
	OUTT	OUT1;

	MVI	KBDREG 0C0; enable keyboard

	Lpi	Bank0			;set the reg bank to 0
	MVI	SC 04			; SET STATE CNTR eq 4
	MVI	FLG1  80		; SET PWRUP FLAG

inituart:
	mvi	uartstat 5Ax		;load a temp register with 5Ax 
	out	uartstat cmdUart	;store to Uart mode reg
	mvi	uartstat 35x		;load temp with 35x
	out	uartstat cmdUart	;store to Uart command reg
	inp	uartcom dataUart	;make a dummy read of Uart datareg
	CLR 
	Lpi	Bank1			;set page flop
	movt	MoreFlags		;clr large doc bit.
	movt	uartcom			;clr command reg
	movt	uartstat		;clr status reg
	movt	UFLAG			;clr UFLAG
	movt	UFLAG2			;clr UFLAG2
	MVI	ROUT2 00
	STB	WatchDT
	Movt	Rout3
	Lpi	Bank0
INIT0:    CLR
	movt	LFC			;CLEAR LF CNTR
	movt	WDT			;CLEAR WATCH DOG CNTR
	movt	FLG2			;RESET XC AND FC

INITIAL:
	CLR				;ENABLE FLAGS
	movt	EC			;CLEAR E CNTR
	movt	FC			;CLEAR F CNTR
	movt	MSC			;CLEAR MOTION SENSOR CNTR
	STB	07			;RESET FLG1 EXCEPT PWRUP
	AND	FLG1			;FLAG IF IT IS ON.
INITIAL1:
	CAL	RESETMC			;CLEAR M CNTR
INITIAL2:
	STB	MCEnable		;ENABLE M CNTR
	IOR	FLG1
	CLR
	movt	XC			;CLEAR X CNTR
	CLB	0
	AND	FLG2			;RESET X CNTR ENABLE FLAG
	MVI	CopyCnt 1		;SET PROG CNTReq1

; STATE 1 ROUTINE
;	THIS ROUTINE ACCOMPLISHES CYCLE UP & PREPARES
;	MACHINE FOR COPY CYCLING.

STATE1:	lcj	SC 1 STATE2;		IF: SC EQ 1
	MVIT	77			;RESET PWRUP AND PRINT FLAGS
	AND	FLG1
;debugging stuff follows
;;	Movr	MCL	;******debugging stuff
;;	Outt	Trace0
	
ST101:	lcj	MCL 0 ST12
	Lpi	Bank1			;SET MAIN DRIVE, MC=0 here
	stb	MAINDRVE		;SET FANS 
	IOR	Rout3
	stb	VACUUM
	IOR	Rout3
	stb	DevClutch		;turn on the developer clutch
	IOR	Rout3			; will be turned off at cycle down
	stb	DevBias			;turn on the developer bias
	IOR	Rout2			; will be turned off at cycle down
	Stb	RosPullin
	IOR	Rout2			;Open the ROS shutter
	Stb	RosHold			;keep it there with less current
	IOR	Rout2
	Clb	LaserBeam		;this is or'd with video from server
	And	Rout2			;or with a test pattern when off-line test
	Lpi	Bank0
ST12:	STB	MCenable		;ENAB. M CTR
	IOR	FLG1			;& DISABLE X CNTR
	CLB	0
	AND	FLG2
ST121:	lcj	MCL 86. ST122
ST123:	Lpi	Bank2
	Clb	Nchargeon		;MC = 86 here
	And	Rout1			;Turn on the corotrons.
	Lpi	Bank0
	CLB	2			;AND
	AND	FLG1			;COIN FLAG

ST122:	lcj	MCL 102. STATE2
	lcj	CPYSEL 0 ST102
	JMP	ST11			;here if display = 00 and MCL=102

ST102:	CAL	RESETMC			;RESET MC & SET
	MVI	SC 2			;  STATE COUNTER ← 2
	JMP	STATE2	

ST11:	MVI	SC 3			;here if display = 00 and MCL=102
	CAL	RESETMC			;CLEAR MC
	STB	MCEnable
	IOR	FLG1			; ENABLE MC
	Lpi	Bank2
	Stb	Nchargeon
	Ior	ROUT1			; Turn off the corotrons.
	Lpi	Bank0
	JMP	STATE2			; With SC = 3 => cycle down.
; STATE 2 ROUTINE 
;                   THIS ROUTINE CONTAINS MULTI COPY CYCLING PROCESS.
STATE2:
	lcj	SC 2 State3Jmp
	CLB	3		; RESET PRINT FLAG
	AND	FLG1
	lcj	MCH 0 ST21
	lcj	MCL 1 ST21
	Lpi	Bank1		;Here when MC = 1; It was zeroed in state
	movr	UFLAG		; One just before entering here first time.
	CPL
	bt	prntbit sync
	jmp	pagesync	;get to the next page
sync:	Lpi	Bank1
	STB	scansol		; Paper feeder
	IOR	Rout3
	Clb	RosPullin	; Turn off the high current to ROS
	And	Rout2		;  shutter, but leave shutter open.
	Lpi	Bank0
ST21:	LCJ	mch 0 ST12C	; read counter for sol reset
	LCJ	mcl 15 ST12C
	Lpi	Bank1
	clb	scansol
	AND	Rout3		;  RESET SCAN SOL & JAM LED

	movr	IN2			;We'll determine end of page for large
	bt	LargeDoc ST12C	; doc at misc222 and send ReadyToFeed there.

	mvi	uartstat ReadyToFeed
	stb	statbit		; Send Ready To Feed to the
	ior	Uflag		; host.

ST12C:	Lpi	Bank0
	LCJ	mch 1 st270
	LCJ	mcl 2C st270
	Lpi	Bank1		; Here if state counter = 12C
	Mvi	UartStat PGDel
	Stb	StatBit		; send Page Delivered to the server
	Ior	UFlag
	Lpi	Bank0

ST270:	LCJ	mch 1 ST27	; this value must be at least one 
	LCJ	mcl 59. ST27	;  greater than the value of st27
	Lpi	Bank1
	clb	psbit		; watch for extra pagesync's
	and	uflag		; clear page sync 1 shot
	movr	uflag		; test if multicopy if so 
	bt	multibit more	; then clear the multibit
	clb	prntbit		; otherwise clear the printbit
	and	uflag
	Lpi	Bank0
	jmp	ST27

more:	clb	multibit
	and	uflag
	Lpi	Bank0
ST27:	lcj	MCH 1 ST24Jmp
	lcj	MCL 24. ST24Jmp
	STB	fuser		; MC EQ 280 (256+24)
	Lpi	Bank1
	IOR	Rout3		; SET FUSER
	Lpi	Bank0
	STB	1
	IOR	FLG1		; ENABLE E CNTR
	STB	3
	IOR	FLG2		; SET 1 SHOT FLAG
	Jmp	St23;

State3Jmp:
	JMP       STATE3
ST24Jmp:
	Jmp	St24;
	
.LOC 0100
ST23:	Lpi	Bank2
	movr	CPYSEL		; has the selector display contents.
	CPL
	lpi	Bank0
	movt	Temp1
	Lpi	Bank1 
	movr	IN2			;Can only get one copy in large doc mode
	bt	LargeDoc setcoin	; while in self test.
	movr	uflag
	bt	multibit ST24
	bt	prntbit setcoin
	Lpi	Bank0
	movr	CopyCnt
	ADD	Temp1		; PC>eqSELSW
	bt	nc ST24		; IF: PC GE SEL SW

setcoin:Lpi	Bank0
	STB	2
	IOR	FLG1		; SET COIN FLAG

ST24:	Lpi	Bank0
	lcj	MCH 1 State3	;IF MC = 256
	MVIT	59.
	CMP	MCL 
	bt	nz State3	;ORIF MC GT 315 (256+59)
	movr	FLG1
	bt	2 ST25		; COIN FLAG CLEAR?
	jmp	ST26		; Yes, bypass home sw stuff.

ST25:	MVI	SC 3		;SET STATE Counter to 3
ST26:	CAL	RESETMC		;RESET MC
	JMP	State3

pagesync:Lpi	Bank1
	movr	uflag
	cpl
	bt	psbit pgsy1
	JMP	Sync		;get us back to the previous page
pgsy1:	mvi	uartstat pgsync	;put page sync stat in stat reg
	stb	statbit		;set send status flag
	IOR	UFLAG          
	stb	psbit		;set the page sync 1 shot flag
	ior	uflag
	Lpi	Bank0		;reset page flop
	jmp	sync
; STATE 3 ROUTINE IS FOR CYCLE OUT.
STATE3:	lcj	SC 3 STATE4    	; SCeq3
	Lpi	Bank1
	movr	Rout3;
	CPL
	Lpi	Bank0
	bt	mainDrve ST37
	movr	FLG1
	bt	3 ST31			; JMP IF PRINT FLAG
ST37:	lcj	MCH 2. STATE4
;	lcj	MCL 3. ST36     	; MCeq512+3eq515
	lcj	MCL 123. ST36     	; MCeq512+93eq?
	Lpi	Bank1
	clb	patternbit
	and	uflag2
	movr	uflag2
	cpl
	bt	pagedelbit ST371
	mvi	uartstat ready
	stb	statbit
	ior	uflag

ST371:	Lpi	Bank1
	Clb	maindrve		; RESET DRIVES FANS EXPOSE
	AND	Rout3
	Clb	vacuum
	and	Rout3
	Clb	DevClutch		;turn off the Developer clutch
	and	Rout3
	Clb	fuser			;Reset fuser
	AND	Rout3
	Clb	DevBias			;ditto
	and	Rout2
	Clb	ROSHold
	And	Rout2			;close the ROS Shutter
	Stb	LaserBeam
	IOR	Rout2			;Turn off the video.
	lpi	Bank2
	stb	Nchargeon		;turn off the corotrons.
	ior	Rout1
	lpi	Bank0


ST36:	;;;LCJ	MCL 33. STATE4		; MCeq512+33eq545
	LCJ	MCL 153. STATE4		; MCeq512+123.=635.
	MVI	SC 4			; SET STATE CNTReq4
	JMP	STATE4

ST31:	Lpi	Bank2
	movr	Rout1
	bt	Nchargeon ST32		; JMP IF CHARGE IS OFF
	Lpi	Bank0
	MVI	SC 1			;SET SCeq1
	CLB	2			;  AND RESET COIN FLAG
	AND	FLG1
	JMP	INITIAL1

ST32:	Lpi	Bank0
	CLR
	CMP	MCH			; TEST MCHeq0 (LESS THAN 256)
	bt	z ST35
	STB	0.			;TEST MCH GRTR 256
	CMP	MCH 
	bt	n ST33			; JMP IF GRTR 256
	MVIT	156.
	CMP	MCL 
	bt	n      ST33            ;JMP IF MC GRTR 256+156eq412
ST35:	LCJ	MCH 1. STATE4
	LCJ	MCL 155. STATE4		;IF MCeq256+155 (eq 411)  RESTART
ST34:	MVI	MCL 72.			; SET MC to 72
	MVI	MCH 0.
	MVI	SC 1.			; SET SCeq1
	Lpi	Bank2
	Clb	Nchargeon		;Turn on corotrons.
	And	Rout1
	Lpi	Bank0
	CLB	2              		; RESET COIN FLAG
	AND	FLG1
	JMP	INITIAL2
ST33:	LCJ	MCH 2. STATE4
	LCJ	MCL 175. STATE4
	JMP	ST34           		; IF MCeq567  JMP ST34
; STATE 4 ROUTINE - STATE COUNTER IS DECODED IF ITS NOT 4 PROGRAM
;                   IS VECTORED TO STATE 5 ROUTINE.THIS ROUTINE
;                   CONTAINS THE STANDBY STATE.
;
STATE4:	lcj	SC 04 STATE5
	CAL	RESETMC		; CLEAR MC
	Lpi	Bank2
	Clb	Nchargeon	; Turn on the Corotrons
	And	Rout1
	Lpi	Bank0
	movr	IN0
	bt	ZapTherm ST41D	;ZAP MODE?
	Lpi	Bank2		;Yes,
	Mvi	CPYSEL 0B1	; put an L1 on the display
;;	Cal	SelDsp
	Cal	DisplayIt
	Lpi	Bank0
	MVI	SC 5.		; SET SC eq 5
	JMP	INIT0

ST41D:	movr	FLG2
	CPL
	bt	2 ST41		; DIAG FLAG SET?
	MVI	SC 6		; Yes, SET SC eq 6.

ST41:	movr	FLG1
	CPL			; PRINT FLAG SET?
	bt	3 STATE5	; Yes, SET CHARGEOFF, SET SCeq1.
	MVI	SC 1		; & JMP TO INITIAL
	movr	FLG1		; TEST PWRUP FLAG
	bt	7 ST42		;  IF SET  DONT SET CHARGEOFF
	Lpi	Bank2
	Stb	Nchargeon
	Ior	Rout1
	Lpi	Bank0
ST42:	JMP	INITIAL

; STATE 5 ROUTINE - THIS ROUTINE CONSISTS OF FUSER & EXPOSURE
;  PREHEAT(ZAP).

STATE5:	lcj	SC 5 STATE6
	movr	IN0
	bt	zaptherm ST51	;ZAP MODE?
	cal	wrmup		;Yes.

wrmup2:	CLB	3.		; RESET PRINT FLAG
	AND	FLG1
	STB	0
	IOR	FLG2		; ENABLE X CNTR
	STB	fuser
	Lpi	Bank1
	IOR	Rout3		; SET FUSER
	Lpi	Bank0

ST53:	lcj	XC 60. ST54	; IF: X CNTR EQ 60
	MVI	XC 0.		;   SET X CNTR eq 0

ST54:
ST55:	lcj	MCH 038 ST56
	lcj	MCL 040 ST56
	CLB	MCEnable		;MC EQ 14400
	AND	FLG1
ST56:	JMP	STATE6		;  JMP TO STATE6

ST51:	CLB	0		; DISABLE X CNTR
	AND	FLG2		;  RESET JAM LED 
	Lpi	Bank1
	Clb	fuser		; FUSER & EXPOSURE
	AND	Rout3
	Lpi	Bank2
	MVI	CPYSEL Blank	;tell the display we're warmed up
;;	Cal	SelDsp		;output it
	Cal	DisplayIt
	Lpi	Bank0
	MVI	SC 4		; SET SCeq4
	JMP	STATE6
; STATE 6 ROUTINE - IF STATE COUNTER IS NOT 6, PROGRAM
;                   IS VECTORED TO MISC ROUTINE. THIS ROUTINE CONTAINS
;                   THE MACHINE DIAGNOSTICS.
;                   IN STATE 6; TEMP IS USED AS A TEMPORARY REGISTER 
;                   IN WHICH OUTPUT CONDITIONS ARE SET UP.
;
;
.loc	1FF
 STATE6:	lcj	SC 6 GMISC
	JMP	JAM;  Not part of the original diag. code. Need something
			;here for when machine goes west.
			
;	MVI	TEMP  08		; SET JAM LED
;	lcj	CPYSEL  00  ST601	;SEL SW EQ 00?
;	movr	LFC			; NOT 15 HZ?
;	bt	3 ST601		; RESET JAM LED
;	CLB	3
;	AND	TEMP
;ST601:	lcj	CPYSEL  001  ST61  ; SEL SW EQ 01?
;	movr	Buttons			; PRINT SW ON?
;	bt	NTestSW ST61		;RESET JAM LED
;	CLB	3
;	AND	TEMP
;ST61:	lcj	CPYSEL  09  ST63  ; ELSEIF: SEL SW EQ 09
;	movr	IN1
;	bt	1 ST62              ; IF: HOME SW OFF
;	CLB	3                   ;  RESET JAM LED
;	AND	TEMP
;ST62:	STB	1
;	IOR	TEMP               ; SET SCAN SOL
;
;ST63:	lcj	Temp1  03  ST65  ; ELSEIF: SEL SW EQ 03
;ST64:	STB	2                   ; SET BILLING
;	IOR	TEMP
;ST65:	lcj	Temp1  04  ST66  ; ELSEIF: SEL SW EQ 04
;	MVIT	90                  ; SET FUSER &
;	IOR	TEMP                 ;  FANS.
;ST66:	lcj	Temp1  05  ST68  ; ELSEIF: SEL SW EQ 05
;	movr	IN1                ; IF: ZAP EQ 0
;	bt	4 ST67
;	CLB	3                        ; RESET JAM LED
;	AND	TEMP               ; ELSE:
;ST67:	STB	4                       ;  SET FANS
;	IOR	TEMP               ; ENDIF
;ST68:	lcj	Temp1  06  ST690
;	movr	IN1           ; SEL SW EQ 06?
;	bt	5 ST69                  ; IF: 50HZ SELECTED
;	CLB	3                        ;  RESET JAM LED
;	AND	TEMP                ; ELSE:
;ST69:	STB	5                         ; SET EXPOSURE
;	IOR	TEMP
;ST690:	lcj	Temp1  07  ST691 ; ELSEIF: SEL SW EQ 07
;	MVIT	50                  ; TURN ON CHARGE
;	IOR	TEMP                  ; DRIVES & FANS
;ST691:	lcj	Temp1  08  ST692  ;ELSEIF: SEL SW EQ 08
;	MVIT	70                  ; TURN ON DRVS
;	IOR	TEMP                  ; FANS EXPO.& CHG.
;	movr	IN1
;	bt	3	ST692                  ; IF: MOTION SENSE OFF
;	CLB	3                         ; RESET JAM LED
;	AND	TEMP
;ST692:	lcj	Temp1 15 MISC	;IF SEL SW EQ 15
;	movr	Buttons
;	bt	NTestSW MISC		;IF PRINT SW ON
;	MVI	SC 1			;SET SC eq 1
;	STB	2			;SET DIAG FLAG
;	IOR	FLG2			; AND THEN
;	JMP	INITIAL
GMISC:					;MISC ROUTINE
MISC:	CAL	INPUT			; REFRESH INPUTS
	movr	IN0			;check for paper out
	cpl
	bt	addPaper Misca
	lpi	bank1
	mvi	uartcom OffLineStat	;Tell server we're off line
	stb	combit			;since we don't have the raven
	ior	uflag			;code for paper-out.
	stb	NoPaper
	ior	MoreFlags		;Remember that we ran out of paper
	Lpi	Bank2
	mvi	cpysel 0C4
;;	Cal	SelDsp			;put a C4 on the display.
	Cal	DisplayIt
	Cal	Stop			;stop the printer.
	Jmp	Miscb
Misca:	Lpi	Bank1
	movr	MoreFlags		;If NoPaper is set, we had set it earlier.
	cpl				;Now we have paper, so reset the flag
	bt	NoPaper Miscb		; and remove the "out of paper"
	clb	NoPaper			; code from the display.
	And	MoreFlags
	Lpi	Bank2
	mvi	CpySel 000		;put a couple 0's on the display.
	Cal	DisplayIt

Miscb:	Lpi	Bank0
	movr	IN1
	bt	ZCROSSING MISC12
	EIF				; ZERO CROSSING TRUE: SET INTERRUPT ENABLE.
	CAL	CHKPRNT			; CHECK FOR PRINT COMMAND
	Lpi	Bank1			;  from Uart.
	MOVR	UFLAG
	BT	PRNTBIT MISC101
MISC12:	lpi	Bank2
	Movr	Buttons
	bt	NTestSW	MISC11
MISC101:	Lpi	Bank0		;Here when Uart got a print command
	STB	3
	IOR	FLG1			; SET PRINT FLAG
	Lpi	Bank1
	movr	uflag
	bt	prntbit misc11
	stb	patternbit		;here if print bit was set by pushing
	ior	uflag2			;  Test switch.
MISC11:	Lpi	Bank2
	Stb	NtestSW;		set the bit, (remember, active low)
	ior	Buttons
	Lpi	Bank0

MISC13:	lcj	EC 117. MISC14
	STB	MotCounter		; E CNTR EQ 117: ENABLE 
	IOR	FLG1;			; MOT SENSE Counter
MISC14:	movr	FLG1
	CPL
	bt	2 MISC141		;Check for coincidence.
	lcj	EC 140. MISC141
	lpi	bank1
	movr	IN2			;If in large document mode, don't turn off yet.
	bt	LargeDoc Misc141
	lpi	bank2			;Here when EC EQ 140
	Stb	Nchargeon 		;turn off the corotrons.
	Ior	ROUT1
MISC141:
	Lpi	Bank0
	lcj	EC 247. MISC15
	Lpi	Bank1
	Movr	IN2			;Get Large Document Sensor.
	cpl
	bt	LargeDoc NotLDC		;Large Document here?
	lpi	Bank0			;We're going to freeze time here for awhile.
	clb	MCEnable		;Leave master counter here until end of page.
	and	flg1
	clb	ECEnable		;Turn off the e counter too.
	and	flg1
	jmp	misc14a;

NotLDC:	lpi	Bank0
	CLB	MotCounter		;Here when EC EQ 247
	AND	FLG1			; DISABLE MOT SENSE Counter
	Lpi	Bank1
	STB	billing
	IOR	ROUT1			; SET BILLING

misc14a:Lpi	Bank0

MISC15:	lcj	EC 255. MISC16
	Lpi	Bank1			;Here when EC = 255
	CLB	billing
	AND	ROUT3			; RESET BILLING
	Lpi	Bank0
	CLB	1
	AND	FLG1			; DISABLE EC
	MVI	EC 0			; RESET EC
	STB	1
	IOR	FLG2			; ENABLE FC

MISC16:	Lpi	Bank0
	movr	FLG1
	CPL
	bt	1 MISC17		; IF: EC INCR FLAG SET
	MVI	FC 0			; RESET FC

MISC17:
	lpi	Bank1
	movr	IN2
	Lpi	Bank0
	bt	LargeDoc misc17A

	lcj	FC 176. MISC18	;here if not large doc
	Lpi	Bank1
	CLB	fuser			; RESET FUSER only if not large doc.
	AND	ROUT3

	jmp	misc17B
misc17A:
	lcj	FC 255. MISC18	;here if large doc
misc17B:
;	Lpi	Bank1		;don't reset fuser here if large doc.
;	CLB	fuser			; RESET FUSER
;	AND	ROUT3

;	Lpi	Bank2
;	stb	Nchargeon		;turn off the corotrons--they may not
;	Ior	ROUT1			; have been turned off if large doc.
	lpi	Bank0
	mvi	FC 00			;make sure it's inited for next copy.
	clb	FCEnable		;we're done with fuser now.
	and	Flg2

;The motion sensor machinery is set up to clear the motion sensor counter on
; negative transitions of the motion sensor.
MISC18:	
	movr	FLG1			;get motion counter flag.
	CPL
	bt	MotCounter MISC21	;Is the motion sensor flag enabled?
	movr	IN1			;Yes.
	bt	MotionSense MISC19 	;Is the input High?
	movr	FLG1			;NO.
	bt	MotSenseDelayFlag MISC20;Was it ever high?

MISC22:	lcj	MSC 10. Misc222  
				;Here when jammed or end of large doc.

	Jmp	Jam
Misc222:
	Lpi	Bank1
	Movr	IN2
	cpl
;LargeDoc is active high
	bt	LargeDoc LF
;TrailEdge is active low
	bt	TrailEdge LF		;here if LargeDoc
	lpi	Bank0			;If no paper is fed the dispatch to Jam
	movr	Flg1				; doesn't happen.  (Because the mot.
	bt	MCEnable LF		; ctr. would be reset continually.
	lpi	Bank1

	STB	billing		;Here if no paper on the Trail Edge Sensor.
	IOR	ROUT1			;SET BILLING, will be cleared at 
	mvi	uartstat ReadyToFeed
	stb	StatBit		;Now that the large doc has finally passed,
	IOR	Uflag			; we are ready to feed another.

	lpi	Bank0			; MISC15.
	stb	MCEnable		;Restart the master counter from 247.
	ior	flg1
	stb	Bit0			;Bump the e ctr to avoid turning it
	add	EC			; off again at Misc141
	stb	ECEnable		;Restart the E counter.
	ior	Flg1

	CLB	MotCounter
	AND	FLG1			;DISABLE MOT SENSE Counter
	jmp	MISC21			; for next go-round.


MISC19:	STB	MotSenseDelayFlag	;Paper is in motion.
	IOR	FLG1			; SET MOTION Delay FLAG
	JMP	MISC22
MISC20:	CLB	MotSenseDelayFlag
	AND	FLG1			; RESET MOTION Sensor Delay FLAG.
MISC21:	MVI	MSC 0			; Clear MOT SEN CTR
; LINE FREQ ROUTINE - WATCH DOG TIMER FLAG IS CHECKED. IF ITS CLEAR
;                     THE PROGRAM IS VECTORED TO STATE 1 ROUTINE.
;
LF:	lpi	Bank0
	movr	WDT
	CPL
	bt 0	LFEND			; IF WDT FLAG IS SET
	CLB	0			;   RESET WDT FLAG.
	AND	WDT
	STB	0
	ADD	LFC			;  INCR LF CNTR
	Lpi	Bank1
	movr	IN2
	LPI	Bank0
	bt	SixtyHertzBit LF11	;Branch if 50 Hertz mode
	jmp 	LF1

LF11:	movr	LFC
	bt	0 LFEND			; LSB OF LFC#1 (/ BY 2)
LF2:	movr	FLG1
	CPL
	bt	MCEnable LF3		;IF MC ENABLED
	STB	0
	ADD	MCL			;INCR LSB OF MC
	bt	nc LF3			;CARRY IS SET?
	ADD	MCH			;INCR MSB OF MC

;The E counter is first enabled in State 2 when MC = 280

LF3:	movr	FLG1
	CPL
	bt	1 LF31			;EC ENABLED?
	STB	0				;Yes
	ADD	EC			; INCR EC
LF31:	movr	FLG2
	CPL
	bt	1 LF35			; FC ENABLED
	STB	0
	ADD	FC			; INCR FC
LF35:	movr	FLG2
	CPL
	bt	0 LF311			; IF: XC ENABLED
	STB	0
	ADD	XC			; INCR XC &
	JMP	LF4
LFEND:	JMP	STATE1	; JMP TO STATE1


LF311:	CLR
	movt	XC			;RESET XC
	nop				;enable the lcj @LF4 to get to Lf47
	nop

LF4:	lcj	SC 2. Lf47       	;SC EQ 2?
	lcj	MCH 1. Lf47
	lcj	MCL 23. Lf47		;jump if MC EQ 279


; PROGRAM COUNTER IS INCREMENTED TO BE COMPATABLE WITH BCD INPUT
; FROM SELECTOR SWITCH.
	MVIT	7
	ADD	CopyCnt                   ; INCR PC BY 7
	bt	ci Lf47			; IF: CI IS CLEAR
	MVIT	0FA			; FF - 6
	ADD	CopyCnt			; DEC. PC BY 6

LF47:	movr	FLG1
	CPL
	bt	MotCounter LFEndAgain	;MOT SENSOR ENABLED?
	STB	0
	ADD	MSC			;INCR MOT SEN CNTR
LFEndAgain:
	JMP	STATE1	; JMP TO STATE1
LF1:	lcj	LFC 4 lf11Jmp		; LFC EQ 4
	MVI	LFC  0FF		;SET LFC TO 255
	JMP	LF2			;& JMP TO LF2
lf11Jmp:	Jmp LF11

; SUBROUTINES
;
RESETMC:  CLR
         movt       MCL ;            RESET MC LSB
         movt       MCH ;            RESET MC MSB
         RFS                        ;RETURN
; JAM ROUTINE HAS ONLY ONE NORMAL EXIT:  POWER UP RESET.
; THE DIAGNOSTICS ROUTINE IS ACESSED VIA THE JAM ROUTINE.
;
JAM:	Lpi	Bank2
	CLR
	Movt	Rout0			;Clear all the outputs so machine
	Movt	Rout1			; doesn't burn up.
	Lpi	Bank1
	Movt	Rout2
	Movt	Rout3
	Lpi	Bank2
	Mvi	CPYSEL 0E2		;"Please clear jam" code
;;;;	Cal	Number			;make sure it gets output
	Cal	DisplayIt
;;	Cal	SelDsp			;make sure it gets output
	Lpi	Bank1
	movr	uflag2
	bt	pagedelbit sendjam
	clb	statbit
	and	uflag

jammed:
	Lpi	Bank1
	clb	patternbit
	and	uflag2
	Stb	laserBeam		;turn off laser
	Ior	Rout2
	Lpi	Bank0
;					DUE TO INTERUPT FAILURE
;					(WATCH DOG TIMER OVERFLOW)
	CAL	INPUT			; INPUT AND MASK SELSW TO
;					IN0  INPUT TO IN1
	movr	IN1
	CPL
	bt	ZCrossing j21		;IF ZERO CROSSING TRUE
	EIF				; THEN SET INTERRUPT ENABLE
j21:
	LPI	Bank2
	Movr	Buttons
	LPI	Bank0
	bt	NTestSW J2		; IF: PRINT SW OFF - JMP J2
	movr	MCL
	CMP	Temp1
	bt	z J3			; IF: SEL SW EQ MC (LSB)
	MVIT	0F0			; DECR MC BY  010 
	ADD	MCL
J3:	lcj	MCL 0F8 JAM		;MC EQ 0F8?
	lcj	Temp1 000 JAM		;SEL SW EQ 00?
	MVI	SC 6			;SET SC eq 6 &
	JMP	INIT0			; JMP TO STATE 6

;THIS IS ONE OF THE PLACES WE SEND STATUS. WE RESPOND
;TO READ STATUS REQUESTS HERE, AND ALSO SEND 'JAMMED'
;IN RESPONSE TO 'FEED' COMMANDS.
; WE DON'T CARE IF THERE WAS A RESPONSE PENDING WHEN 
;THE JAM OCCURRED -- IT IS STEPPED ON. 
J2:	MVI	MCL	038		;SET LSB OF MC eq  038 
	Lpi	Bank1
	movr	uflag
	cpl
	bt	combit jammed		;if combit is 
	mvit	readstat
	cmp	uartcom
	bt	z	sendjam
	mvit	feedbot
	cmp	uartcom
	bt z sendjam
	Lpi	Bank0
	JMP	jammed		; JMP TO jammed

sendjam:Lpi	Bank1
	mvi uartstat jamb
	out uartstat datauart
	clb prntbit
	and uflag
	clb combit
	and uflag
	Lpi	Bank0
	jmp jammed

;	SUBROUTINE "INPUT" loads temp1 with cpysel
;	MACHINE INPUTS THEN STORED IN IN1.

INPUT:
	LPI	Bank2
	Movr	CPYSEL
	LPI	Bank1
	INP	IN2	INPUT2	;get 60~ info
	LPI	Bank0
	Movt	Temp1
	INP	IN1 INPUT1
	INP	IN0	INPUT0
	RFS			; RETURN

wrmup:
	Lpi	Bank1
	movr	UFLAG
	bt	prntbit sendwrmup
	Lpi	Bank0
	RFS

sendwrmup:
	mvi	uartstat warmup
	stb	statbit
	ior	uflag
	clb	prntbit
	and	uflag
	Lpi	Bank0
	RFS