.TITL GRSDNT.A
;
	.BEXT BREAKENTRY, INTERRUPTENTRY, SWAPOUTFILE
	.BEXT SWAPINFILE, VCALL,INLD, OUTLD
	.BEXT LOCALSTORAGE,Callswat
;
	.SREL
BREAKENTRY:	BRENT
INTERRUPTENTRY: IENT
SWAPOUTFILE:	OF
SWAPINFILE:	IF
LOCALSTORAGE: LSTRG
Callswat:	CENT
;
;
	.NREL
;
TRAPPC:	527
INTPC:	500
N4:	4
N100:	100
N1002:	1002	; signal for COMMANDER
KBAD2:	177036	; KEYBOARD CONTAINING SHIFT(LEFT)
KBAD3:	177037	; KEYBOARD WORD
KBLK:	521	; DISK CONTROL WORD
KEYUP:	1	; NON-ZERO IF SWAT KEY WAS UP 30 MILLISECONDS AGO
N3:	3
N2:	2
;
PC: 	700	; standard register area
AC0:	701
AC1:	702
AC2:	703
AC3:	704
CARRY:	705
BOOTFLAG: 706	; 0 ==> SWATEE created by booting
; Here is the message area. Swat knows its format
; and finds it by knowing where BRENT is.
;
SMSG:	JSR 0,3
OF:	-1	; file id for SWATEE (BRENT-#43)
	-1
	-1
	-1
	-1
IVO:	2	; internal version no., see SWAPPER.(BRENT-#36)
WHY:	.BLK 1	; (BRENT-#35)
LSTRG:	.BLK 1	; AREA FOR STRING CONSTANTS
;
	.BLK 1	; JUNK
RMSG:	JSR 0,3	; RECEIVED MESSAGE
IF:	-1	; file id for SWAT (BRENT-#31)
	-1
	-1
	-1
	-1
SIG:	.BLK 1	; 0=>EXECUTE CODE;1=>CALL SUB;2=>DISPLAY
		; 3,5=> restart; 4=> directed swap
CODE:		; BEGINNING OF CODE IF SIG = 0
CLVC:		; CALL DESCRIP. IF SIG = 1
FID:	.BLK 23	; FID IF SIG = 3 or 4

BRENT:	DIR	; Breaks enter here
	STA 0,@AC0
	STA 3,@AC3
	SUBCL 0,0
	STA 0,@CARRY
	LDA 3,@TRAPPC
	SUBZL 0,0
	SUB 0,3
	SUB 0,0
	JMP COMON
IENT:	STA 0,@AC0	; TIMER INTERRUPT ENTERS HERE
	STA 3,@AC3
	LDA 0,@KBAD3
	LDA 3,N4
	AND 3,0,SZR	; IS SWAT KEY DOWN?
	JMP RET1	; NO, RETURN
	LDA 0,KEYUP
	MOV 0,0,SNR	; HEAVY FINGERS?
	JMP RET	; YES IGNORE THEM
	LDA 0,@KBAD2
	LDA 3,N100
	AND 3,0,SZR	; IS LEFT SHIFT KEY DOWN TOO?
	JMP SWT	; NO, INVOKE SWAT
	SUB 0,0	; NO, HE HAS JUST PRESSED THE TWO MAGIC KEYS
	STA 0,KEYUP	; RECORD IT
	STA 0,@N1002	; notify command processor
	EIR
	JMP @353	; ABORT SUBSYSTEM
SWT:	SUBCL 0,0
	STA 0,@CARRY
	LDA 3,@INTPC
	SUBZL 0,0
	JMP COMON
RET1:	STA 0,KEYUP	; RECORD UPNESS
RET:	LDA 0,@AC0
	LDA 3,@AC3
	BRI

CENT:	DIR	; EXPLICIT CALL ENTERS HERE
 	STA 0,@AC0
	SUBCL 0,0
 	STA 0,@CARRY
 	INC 3,3
 	LDA 0,N2
 	JMP COMON

CRET:	DIR		; CALL RETURNING, SEND VALUE
	STA 0,@AC0
	LDA 0,N3
	STA 0,WHY
	JMP COMON

COMON:	; AC0 = WHY WE ARE HERE
	;	0 ==> BREAK
	;	1 ==> INTERRUPT
	;	2 ==> CALL
	;	3 ==> RETURN FROM CALL
	; CARRY SAVED
	; AC3 = ADDRESS TO RESTART LATER
	; OTHER REGISTERS UNTOUCHED
	STA 1,@AC1
	STA 2,@AC2
	STA 0,WHY
	STA 3,@PC
SWPOUT:	; RETURN FROM CALL MERGES HERE.
	; MESSAGE VECTOR IS READY TO GO
	LDA 0,@KBLK	; WAIT FOR DISK TO QUIET
	MOV 0,0,SZR
	JMP .-2

	LDA 0,N3
	SIO	; Turn off EtherNet

	LDA 0,@ACTIVE
	SUB 1,1
	STA 1,@ACTIVE
	EIR		; Flush pending interrupts into WW
	DIR
	STA 0,@ACTIVE

	SUB 0,0		; signal that SWATEE is
	STA 0,@BOOTFLAG ; complete

	LDA 0,M5
	STA 0,SIG	; signal resume if booted

	LDA 0,@SOF
	LDA 1,@SIF
	JSRII outld
	    2
	MOV 0,0,SNR	; ARE WE WAKING UP?
	JMP WAKE	;YES
SWPIN:	LDA 0,@SIF
	LDA 1,@SOF
	JSRII inld
	    2
SIF:	SWAPINFILE
SOF:	SWAPOUTFILE
outld:	OUTLD
inld:	INLD
; CONTROL NEVER RETURNS HERE
WW:	452
ACTIVE:	453
WAKE:
; Throw away pending interrupts
	LDA 0,@WW
	LDA 1,@ACTIVE
	SUB 2,2
	STA 2,@ACTIVE
	EIR
	DIR
	STA 0,@WW
	STA 1,@ACTIVE

; SIG = 0  ==> EXECUTE CODE simulating broken instruction
; SIG = 1  ==> CALL SUBROUTINE
; SIG = 2  ==> SIT STILL, DISPLAY SCREEN
; SIG = 3  ==> resume at PC, reset swap-out file id.
; SIG = 4  ==> swap out on designated file and invoke Swat
; SIG = 5  ==> resume at PC
	LDA 0,SIG
	MOV 0,0,SZR	; RESTART?
	JMP L12345	; NO
	LDA 0,@ECRY	; Yes, restore registers
	MOVR 0,0
	LDA 0,@EAC0
	LDA 1,@EAC1
	LDA 2,@EAC2
	LDA 3,@EAC3
	JMP CODE	; Execute message blindly, it
			; will enable interrupts.
L12345:	SUBZL 1,1
	SUB# 0,1,SZR	; Call?
	JMP L2345	; No
	LDA 2,@EAC2	; SET STACK POINTER
	LDA 3,@SIF	; AC3 = IF
	LDA 0,DCLVC
	ADD 3,0		; locate call descriptor
	JSRII vcall
	    1
	JMP CRET
vcall:	VCALL

L2345:	LDA 1,M2
	SUB# 0,1,SZR	; display  ?
	JMP L345	; No

USCRN:	LDA 0,@KB3
	LDA 3,M4
	AND 3,0,SZR	; IS SWAT KEY DOWN?
	JMP USCRN
	JMP SWPIN	; back to swat

; SIG is 3, 4 or 5
L345:	LDA 1,M3
	SUB# 0,1,SNR
	JMP L34		; RESUME
	LDA 1,M4
	SUB# 0,1,SZR
	JMP L5

L34:	LDA 1,@SIF	; Message tells the fid to
	LDA 3,DCLVC	; swap out on
	ADD 1,3		;  AC3 = ptr to fid
	LDA 2,@SOF
	LDA 1,0,3
	STA 1,0,2
	LDA 1,1,3
	STA 1,1,2
	LDA 1,2,3
	STA 1,2,2
	LDA 1,4,3
	STA 1,4,3	; Swatee's fid copied
	LDA 1,M3
	SUB# 0,1,SZR	; SIG = 3
	JMP SWPOUT	; No. Invoke Swat
			; Yes. SIG=4. RESUME
L5:	LDA 0,@EPC
	STA 0,@M500
	LDA 0,@ECRY	;  restore registers
	MOVR 0,0
	LDA 0,@EAC0
	LDA 1,@EAC1
	LDA 2,@EAC2
	LDA 3,@EAC3
	BRI		; restart


;
M2:	2
M3:	3
M4:	4
M5:	5
M500:	500
KB3:	177037
EPC: 	700	; standard register area
EAC0:	701
EAC1:	702
EAC2:	703
EAC3:	704
ECRY:	705
DCLVC:	CLVC-IF
BRK:	77400
.END