/ A-D CLOCKED, BUFFERED SAMPLING ROUTINE
/
/ VERSION 5A 4-27-77  PT
/
	ADSK=6534
	ADRB=6533
	ADST=6532
	ADLM=6531
	ADLE=6536
	ADCL=6530
	CLZE=6130
	ESF=4
	LINC=6141
	PDP=2
	SAM=100
	CLEN=6134
	FIELD1	SAMPLE
	0			/INTERRUPT TIME AD SAMPLER
	IFNSW	8 <
	JMS	LNCSAM		/INITIATE SAMPLE
NEXTCH,	ISZ	SAMINS		/UPDATE SAM INST FOR NEXT CHAN
	JMS	LNCSAM		/SAM AND INITIATE NEXT CHANNEL
	>
	TAD	SAMPTR		/SAVE THE OLD SAM BUFFER POINTER
	DCA	OLDPTR
	TAD	BUFFLD		/AND THE FIELD
	DCA	OLDFLD
	ISZ	SAMPTR		/BUMP BUFFER POINTER
	JMP	FLDOK		/FIELD IS OK
	TAD	BUFFLD		/BUMP FIELD
	TAD	L10
	DCA	BUFFLD
FLDOK,	ISZ	SAMCNT		/BUMP BUFFER COUNT
	JMP	BUFFLD		/NOT END OF BUFFER
	TAD	ARRAY+2		/RESET POINTER TO START OF BUFFER
	DCA	SAMPTR
	TAD	FLDBUF		/RESET BUFFER FIELD
	DCA	BUFFLD
	TAD	BUFSIZ		/RESET COUNT
	DCA	SAMCNT
BUFFLD,	HLT			/GET FIELD OF NEW ADB STOP CODE
	TAD%	SAMPTR		/IS THIS THE SAM STOP CODE ?
	TAD	M3776		/(ILLEGAL AS A SAMPLE)
	SZA CLA
	JMP	NOERR
	ISZ	TOOFAS		/SET TOO FAST SWITCH
SAMPLD,	CDF	10
	DCA%	XCLINT+1	/STOP SAMPLING
	JMP%	SAMPLE
NOERR,	CLL CMA RAR		/SET ADB STOP CODE
	DCA%	SAMPTR
OLDFLD,	HLT			/GET TO FIELD OF SAMPLE
	IFSW	8 <
	ADRB			/READ SAMPLE
	>
	IFNSW	8 <
	TAD	SAMTMP		/GET PREVIOUSLY READ SAMPLE
	>
	DCA%	OLDPTR		/INTO BUFFER
	ISZ	NPOINT+2	/ANY MORE SAMPLES
	SKP			/YES
	ISZ	NPOINT+1	/MORE THAN 7777 ?
	SKP			/YES
	JMP	SAMPLD		/NO
	ISZ	NCHANL+1	/ANY MORE CHANNELS TO SAMPLE ?
	JMP	NEXTCH		/YES GO START SAMPLING
	TAD	CSTART+2	/STARTING CHANNEL
	IFSW	8 <
	ADLM
	>
	IFNSW	8 <
	DCA	SAMINS
	JMS	LNCSAM		/SET CHANNEL TO START
				/IN CASE CLOCK INITIATED
	>
	TAD	NCHANL+2	/NUMBER OF CHANNELS
	DCA	NCHANL+1	/INTO COUNTER
	CDF	10
	JMP%	SAMPLE
	IFSW	8 <
NEXTCH,	ADST			/SAMPLE NEXT CHANNEL
	ADSK			/WAIT FOR SAMPLE
	JMP	.-1
	JMP	SAMPLE+1
	>
	IFNSW	8 <
LNCSAM,	0			/LINC SAM SUBROUTINE
	LINC
SAMINS,	SAM	0		/SAMPLE AND SELECT NEXT CHANNEL
	PDP
	DCA	SAMTMP		/SAVE IT
	JMP%	LNCSAM
	>
ADSETU,	0			/SET UP ROUTINE
	DCA	TOOFAS		/CLEAR TOO FAST SWITCH
	TAD	ARRAY+1		/GET FIELD OF BUFFER
	AND	L7
	CLL RTL
	RAL
	TAD	CDF0
	DCA	FLDBUF
	TAD	FLDBUF
	DCA	BUFFLD		/SAVE IN SAMPLER CODE
	TAD	ARRAY+2		/SET SAMPLER BUFFER POINTER
	IAC
	DCA	SAMPTR
	TAD	LENGTH+2	/SIZE OF BUFFER
	CLL RAL
	TAD	LENGTH+2	/TIMES THREE
	DCA	BUFSIZ		/SAVE IT
	TAD	BUFSIZ		/SET INITIAL COUNT
	IAC
	DCA	SAMCNT
	TAD	NCHANL+2	/SET CHANNEL COUNT
	DCA	NCHANL+1
	IFSW	8 <
	CLA CMA			/STOP THE CLOCK
	CLZE
	CLA
	ADCL			/CLEAR AD LOGIC JUST IN CASE
	TAD	L300		/SET AD ENABLE BITS
	ADLE
	TAD	CSTART+2	/STARTING CHANNEL NUMBER
	ADLM
	>
	IFNSW	8 <
	CLEN			/STOP THE CLOCK
	TAD	CSTART+2	/SET UP INITIAL SAM INSTRUCTION
	TAD	L100
	DCA	CSTART+2
	TAD	CSTART+2
	DCA	SAMST		/STARTING SAM
	TAD	SAMST		/ALSO INTERRUPT TIME SAM
	DCA	SAMINS
	TAD	L100		/SET FAST SAM BIT
	IOF			/TURN OFF INTERRUPTS IN LINC MODE
	LINC			/ENTER LINC MODE
	ESF
SAMST,	SAM	0		/SET INITIAL SAM CHANNEL
	PDP
	ION
	CLA
	>
	CIF CDF
	JMP%	ADSETU
BASEX,	FNOP
M3776,	-3776
L10,	10
SAMPTR,
ARRAY,	0;0;0
LENGTH,	0;0
BUFSIZ,	0
SAMCNT,
CSTART,	0
OLDPTR,	0;0
SAMTMP,
NCHANL,	0;0;0
NPOINT,	0;0;0
FLDBUF,
XCLINT,	ADDR	#CLINT
	IFSW	8 <
L300,	300
	>
	IFNSW	8 <
L100,	100
	>
SAMXR,	0;0
TOOFAS,	0
	ORG	10*3+BASEX
	0
	JA	NAME+3
	0
SAMRTN,	JA	.
CDF0,	CDF
L7,	7
	SECT	REALTM
	BASE	0
	STARTD
	FLDA	30		/GET RETURN ADDR
	FSTA	SAMRTN
	FLDA	0		/GET ARG POINTER
	BASE	BASEX
	SETB	BASEX
	SETX	SAMXR
	FSTA	NPOINT		/SAVE ARG POINTER
	FCLA
	EXTERN	#CLINT
	FSTA	#CLINT		/STOP ANY SAMPLING NOW!
	LDX	1,1
	FLDA%	NPOINT,1	/GET BUFFER ADDRESS
	FSTA	ARRAY
	FLDA%	NPOINT,1+	/GET ADDR OF LENGTH
	FSTA	LENGTH
	FLDA%	NPOINT,1+	/ADDR OFHANNEL START
	FSTA	CSTART
	FLDA%	NPOINT,1+	/ADDR OF # CHANNELS
	FSTA	NCHANL
	FLDA%	NPOINT,1+	/ADDR OF NUMBER OF POINTS
	FSTA	NPOINT
	FLDA	ARRAY		/CREATE SETX INS
	FADD	STXMJA
	FSTA	BUFSTX
	FSTA	ADBSTX		/AND SAVE IT IN TWO PLACES
	STARTF
	FLDA%	LENGTH		/INTEGERIZE AND NEGATE SOME ARGS
	FNEG
	ALN	0
	FSTA	LENGTH
	FNORM
	ATX	1		/SET BUFFER COUNT
	FLDA%	CSTART		/GET STARTING CHANNEL
	ALN	0
	FSTA	CSTART
	FLDA%	NCHANL
	FNEG
	ALN	0
	FSTA	NCHANL
	FLDA%	NPOINT
	FNEG
	ALN	0
	FSTA	NPOINT
	LDX	-1,2		/SET UP FOR BUFFER CLEAR
	FCLA
CLRBUF,	FSTA%	ARRAY,2+
	JXN	CLRBUF,1+
	TRAP4	ADSETU		/SET UP AD STUFF
	FLDA	LENGTH		/RE-GET BUFFER SIZE
	FNORM
	ATX	1		/BUT NOW ITS TIMES THREE
	FLDA	STPCOD		/STORE STOP CODES
	FSTA%	ARRAY		/INTO FIRST 3 WORD
	STARTD
	FLDA	SAMADR		/SET UP SAMPLER INTERRUPT HANDLER
	FSTA	#CLINT
	STARTF
	JA	SAMRTN		/RETURN
NAME,	TEXT	+RTMADB+
SAMADR,	ADDR	SAMPLE
STPCOD,	3776;3777;0
STXMJA,	1100-1030;0
L2047,	F	2047.
L511,	F	511.
	ENTRY	ADB
ADB,	BASE	0		/FETCH SAMPLE FROM BUFFER
	STARTD
	FLDA	30		/SAVE REUTRN
	FSTA	SAMRTN
	SETB	BASEX
	SETX	SAMXR
	BASE	BASEX
	JXN	SPEEDK,2	/CLOCK TOO FAST
	FLDA	ADBSTX		/SAVE OLD SETX
	FSTA	OLDSTX
	FADD	L1		/ADD ONE TO IT
	FSTA	ADBSTX		/AND SAVE IT BACK
	JXN	NORINQ,1+	/END OF BUFFER ?
	FLDA	BUFSTX		/YES, RESTART
	FSTA	ADBSTX
	FLDA	LENGTH		/RESET COUNT
	ATX	1
NORINQ,	STARTF
ADBSTX,	SETX	0		/SET XR0 TO NEXT SAMPLE
WAIT,	XTA	0		/GET THE NEXT SAMPLE
	FSUB	L2047		/IS IT THE STOP CODE
	JEQ	WAIT		/YES
	XTA	0		/NO, FETCH THE SAMPLE
	LDX	3776,0		/SET SAMPLE STOP CODE
OLDSTX,	SETX	0		/SET XR0 TO PREVIOUS STOP CODE
	LDX	0,0		/NOW ZERO IT
	JA	SAMRTN		/RETURN
SPEEDK,	EXTERN	#WRITO		/USE FORTRAN I/O
	TRAP3	#WRITO		/TO WRITE A MESSAGE
	JA	TTYUNT		/ON THE TTY
	JA	MESSAG
	EXTERN	#RENDO
	TRAP3	#RENDO		/CLOSE THE RECORD
	LDX	0,2		/KILL TOO FAST SWITCH
	JA	SAMRTN		/RETURN FROM ADB
TTYUNT,	F	0.
MESSAG,	TEXT	'(" SAMPLING TOO FAST")'
BUFSTX,	SETX	0
L1,	0;1
	END
