;
; INTERRUPT PROCESSING LOGIC
;
; 00.01 KRS 07/15/81 SYSTEM STATE FIXES
; 00.02 KRS 09/09/81 DIFLAG REMOVED, ADDED SAVES
; 00.03 KRS 10/07/81 DISTRIBUTION VERSION
; 00.04 SBW 06/03/82 DIST. VER. FIXED
;
	NAM	#QQ[]03
	ENT	#QQ[]IN,#QQ[]EX,#QQ[]RT,#QQ[]CK
;
	EXT	FSCHED,TIKCNT,TIMVAL,TIMVL2
	EXT	MAXMAX,INTSTK,SXSTAT,CURMAP,MAPMAP
	EXT	DEVIO,BNKSEL
;
#QQ[]RT:	DW	0		; HL AT INTERRUPT TIME
;
#QQ[]IN:	SHLD	INTRTN		; HL = INTERRUPT ADDRESS
	POP	H		; USER RETURN ADDRESS
	SHLD	RETSV
	PUSH	PSW
	POP	H
	SHLD	PSWSV		; TRUE PSW
	LXI	H,0
RETSV:	EQU	$-2
	PUSH	H		; RETURN ADDRESS RESTORED
	LXI	H,0
	DAD	SP
	SHLD	SPSAV		; TRUE USER STACK
	LHLD	INTSTK
	SPHL
	PUSH	D				;1=D
	PUSH	B				;2=B
	LXI	H,0
PSWSV:	EQU	$-2		; TRUE PSW
	PUSH	H				;3=PSW
	IFC	Z80		; 00.02
	PUSH	X		; 00.02
	PUSH	Y		; 00.02
	EXX			; 00.02
	PUSH	H		; 00.02
	PUSH	D		; 00.02
	PUSH	B		; 00.02
	EXX			; 00.02
	EXAF			; 00.02
	PUSH	PSW		; 00.02
	EXAF			; 00.02
	ENDC			; 00.02
	LXI	H,0
INTRTN:	EQU	$-2		; INTERRUPT ROUTINE ITSELF
	PCHL
;
;	EVERY TICK TELL THE SYSTEM
;
#QQ[]CK:	LXI	D,1		; IF CLOCK COME BACK HERE
	LHLD	TIMVAL
	DAD	D
	SHLD	TIMVAL
	IFC	Z80			; 00.02
	JRNC	NOCARY			; 00.02
	ELSEC				; 00.02
	JNC	NOCARY
	ENDC				; 00.02
	LHLD	TIMVL2
	INX	H
	SHLD	TIMVL2
NOCARY:	LXI	H,TIKCNT
	INR	M
;
#QQ[]EX:	LDA	SXSTAT		; GET HERE ON ANY INTERRUPT
	MOV	D,A			; 00.01
	ANI	80H
	IFC	Z80			; 00.02
	JRNZ	NOFORC			; 00.02
	ELSEC				; 00.02
	JNZ	NOFORC		; IN SYSTEM STATE!!!
	ENDC				; 00.02
	MOV	A,D
	ORI	80H
	STA	SXSTAT		; NOW IN SYSTEM STATE!	 00.01
	LDA	CURMAP
	PUSH	PSW
	LDA	MAPMAP			; 00.02
	CALL	BNKSEL
	CALL	DEVIO		; POLL DEVICE I/O
	POP	PSW
	CALL	BNKSEL
	LDA	SXSTAT					;00.01
	ANI	7FH					;00.01
	STA	SXSTAT		; AND NOW YOU DON'T!	 00.01
;
	LDA	TIKCNT
	MOV	B,A
	LDA	MAXMAX			; 00.02
	CMP	B
	IFC	Z80			; 00.02
	JRGE	NOFORC			; 00.02
	ELSEC				; 00.02
	JGE	NOFORC		; TIME NOT ELAPSED
	ENDC				; 00.02
	LHLD	SPSAV
	LXI	D,FSCHED	; PUSH SCHEDULER ADDRESS
	DCX	H		; ONTO USER'S STACK
	MOV	M,D		; IF HE DON'T HAVE
	DCX	H		; THE SPACE AVAILABLE
	MOV	M,E		; SHOULDN'T BE RUNNING
	SHLD	SPSAV		; WITH INTERRUPTS AT ALL
	LDA	SXSTAT					;00.01
	ORI	80H					;00.01
	STA	SXSTAT		; HERE WE GO AGAIN!	 00.01
;
NOFORC:
	IFC	Z80		; 00.02
	EXAF			; 00.02
	POP	PSW		; 00.02
	EXAF			; 00.02
	EXX			; 00.02
	POP	B		; 00.02
	POP	D		; 00.02
	POP	H		; 00.02
	EXX			; 00.02
	POP	Y		; 00.02
	POP	X		; 00.02
	ENDC			; 00.02
	POP	PSW				;2
	POP	B				;1
	POP	D				;0
	LXI	H,0
SPSAV:	EQU	$-2
	SPHL
	LHLD	#QQ[]RT
	EI
	IFC	Z80
	RETI
	ELSEC
	RET
	ENDC
;
	END
