	FIELD1	SETIDL
/IDLE RANDOM NUMBER GENERATOR
	EXTERN	#IDLE

	JA	#RET
	TEXT	+SETIDL+
SXR,	SETX	XR
	SETB	BP
BP,	F 0.0
XR,	ORG	.+3
EXP,	0
IDLAD,	ADDR	#IDLE-1		/ Pointer to idle routine in FRTS
POINT,
JOB,	ADDR	DOIT		/ Address of routine to run
	ORG	XR+10
K0100,	100
K0003,	3
ROTWRD, 0017
	#LBL=.
	COMMON	NUMCOM
NUMBER,	ORG	.+3

	COMMON	TXTCOM
RTEXT,	ORG	.+1147
LINES,	ORG	.+0044
ASCVAR,	ORG	.+0003
TXTLOC,	ORG	.+0003
DATA,	ORG	.+0352
	ORG	#LBL
SEEDPT,	ADDR	NUMBER
	0
DATHAK,	7777
	FLDA	ASCVAR
DATABL,	1763-1		/ Address of Direct Access table entry 8 in FRTS
COUNT,	0		/ Idle loop count
COUNT2,	7774		/ Outer counter
RESET,	7774		/ Outer counter value
X4=14			/ Autoindex reg
	ORG	10*3+BP
	FNOP
	JA	SXR
	0
RET,	JA	.

	BASE	0
#RET,	STARTD
	FLDA	10*3
	FSTA	RET
	SETB	BP
	BASE	BP
	STARTF
	TRAP4	SET8	/GO TO 8 MODE STUFF.
	JA	RET	/DONE. ON IDLE QUEUE.

SET8,	0
/
/ Horrible hackery. This sets up the FRTS direct access table
/ to allow us to open the ADVENT.DA file without having to
/ rebuild it each time. Open it using USR, and this lets us
/ go use it later. (DEFINE FILE always creates a new one.)
	TAD	DATABL
	DCAZ	X4
SCDF,	CDF	0
	TAD	DATHAK
	DCA%	X4
	TAD	DATHAK+1
	DCA%	X4
	TAD	DATHAK+2
	DCA%	X4
	TAD	CDFIDL
	SZA CLA
	JMP	SFIELD	/ALREADY DONE
	TAD	IDLAD	/FIELD OF IDLE ROUTINE
	JMS	MKCDF
	DCA	CDFIDL
	TAD	IDLAD+1	/ADDRESS OF #IDLE
	DCAZ	X4
CDFIDL,	0		/GETS CDF
	TAD	SKPINS	/SKP INSTRUCTION
	DCAZ%	X4	/STORE AT #IDLE
	TAD	JOB+1	/ADDRESS OF IDLE ROUTINE
	DCAZ%	X4	/STORE AT #IDLE+1
	TAD	JOB
	JMS	MKCDF
	IAC		/ CDF -> CIF CDF
	DCAZ%	X4	/STORE AT #IDLE+2
SFIELD,	CIF CDF
	JMP%	SET8

/
/ Blinky lights
/
SKPINS,
DOIT,	SKP		/ Entry point, one-time use constant
	TAD	ROTWRD	/GET WORD TO DISPLAY
STALL,	ISZ	COUNT
	JMP	STALL
	ISZ	COUNT2
	JMP	STALL
FLOP,	RAL		/Shift over
	DCA	ROTWRD	/Save it
	TAD	RESET	/Reset outer counter
	DCA	COUNT2
	SNL
	JMP	DONE
	TAD	ROTWRD
FLIP,	RAR		/Rotate back
	DCA	ROTWRD
	TAD	FLOP
	DCA	COUNT	/Save old rotate
	TAD	FLIP	/And flip 'em around
	DCA	FLOP
	TAD	COUNT
	DCA	FLIP
	DCA	COUNT
DONE,	TAD	SEEDPT
	JMS	MKCDF
	DCA	.+1
	0
	TAD	SEEDPT+1
	DCA	EXP
	CLL IAC
	TAD	EXP
	DCA	POINT
	TAD%	POINT
	TAD	K0100
	SPA
	JMP	OVRFLO
	DCA%	POINT
	JMP	DOIT+1

OVRFLO,	CLA CLL
	DCA%	POINT
	TAD%	EXP
	IAC
	AND	K0003
	DCA%	EXP
	JMP	DOIT+1
/ Change field number into CDF instruction
MKCDF,	0
	CLL RTL
	RAL		/INTO BITS 6-8
	TAD	SCDF	/CDF TO #IDLE
	JMP%	MKCDF

	END
