;DSK:<FOONEX>DEVICE.MAC;2 18-Sep-80 15:13:54, Edit by DANG
; Added versatec device VTC
;<134-TENEX>DEVICE.MAC;9    21-Oct-79 18:57:09    EDIT BY PETERS
;removed sri oddball device ifdefs
;<134-TENEX>DEVICE.MAC;8    23-Mar-79 17:51:44    EDIT BY PETERS
;<134-TENEX>DEVICE.MAC;7    26-Jul-77 15:29:22    EDIT BY DANG
; Made MTBs be MTAs again (device code 600002)
;<134-TENEX>DEVICE.MAC;6    22-Mar-77 23:13:12    EDIT BY LYNCH
; TOOK OUT MTAN STUFF 
;<134-TENEX>DEVICE.MAC;4    16-Feb-77 11:03:13    EDIT BY UNTULIS
;ADDED MTB TO DEVICE TABLE 
;<134-TENEX>DEVICE.MAC;2     4-FEB-76 15:19:51    EDIT BY UNTULIS
;ADDED SRI-AI DEVICES
;<135-TENEX>DEVICE.MAC;13    12-DEC-75 10:49:21    EDIT BY PLUMMER
; INSERT MISSING CLOSE PAREN IN DEV(LPT) IFNDEF LPTSPL
;<134-TENEX>DEVICE.MAC;12    28-APR-75 12:12:37    EDIT BY CLEMENTS
;<134-TENEX>DEVICE.MAC;11    28-APR-75 11:31:22    EDIT BY CLEMENTS
;<134-TENEX>DEVICE.MAC;10    24-APR-75 14:14:18    EDIT BY CLEMENTS
;<TENEX-132>DEVICE.MAC;9     9-NOV-73 19:28:44	EDIT BY CLEMENTS
;<TENEX-132>DEVICE.MAC;8    19-JAN-73 10:16:26	EDIT BY TOMLINSON
; ADD DAC ADC
;<FILESYSTEM>DEVICE.MAC;7    29-JUN-72  9:53:26	EDIT BY TOMLINSON

	SEARCH STENEX,PROLOG
	TITLE	DEVICE
	SUBTTL	R.S.Tomlinson	22 MAR 72	2034:
	USE	SWAPPC

EXTERN	CPOPJ,SKPRET,CHKDEV
EXTERN	FORKX
INTERN	NDEV

; Initial device tables used to initialize the working device tables

DEFINE	DV(NAME,UNITS,TYPE,DISP,CHAR,MODES)<
	XWD <SIXBIT /   NAME/>,UNITS
	EXTERN DISP
	XWD 0,DISP
	XWD CHAR+TYPE,MODES
IFE UNITS,<NDEV==NDEV+1>
NDEV==NDEV+UNITS>

NDEV==2	; Leaves room to patch 2 devices

INIDVT:		DV(DSK,0,0,DSKDTB,720000,100001)
IFDEF DPKN,<	DV(DPK,DPKN,0,DSKDTB,762000,100001)>
IFDEF MTAN,<	DV(MTA,MTAN,2,MTADTB,643000,100000)>
IFDEF DTAN,<	DV(DTA,DTAN,3,DTADTB,742000,100001)>
IFDEF PTRN,<	DV(PTR,0,4,PTRDTB,240000,14403)>
IFDEF PTPN,<	DV(PTP,0,5,PTPDTB,440000,14403)>
IFDEF LPTSPL,<	DV(LPT,0,7,SPLDTB,400000,100001)
 IFDEF LPTDVF,<	DV(LP1,0,7,LPTDTB,400000,100001)>>
IFNDEF LPTSPL,<
 IFDEF LPTDVF,<	DV(LPT,0,7,LPTDTB,400000,1)>>
		DV(TTY,NLINES,12,TTYDTB,640000,401)
		DV(NIL,0,15,NILDTB,600000,177777)
IFDEF NETN,<	DV(NET,0,16,NETDTB,700000,341)>
IFDEF PLTN,<	DV(PLT,0,17,PLTDTB,440000,1)>
IFDEF VTCN,<	DV(VTC,0,20,VTCDTB,440000,100001)>
IFDEF CHAOS,<	DV(CHA,0,21,CHADTB,700000,341)>
INIDVE:	BLOCK 6

LS(DEVLCK)	; Lock for assigning devices
LS(DEVNAM,NDEV)	; Sixbit device name
LS(DEVCHR,NDEV)	; Device characteristics
LS(DEVDSP,NDEV)	; Lh ==> ofn if any, rh ==> dispatch table address
LS(DEVUNT,NDEV)	; Lh ==> job of assigner, rh ==> unit number

; Initialize device tables
DEVINI::SETOM DEVLCK
	MOVSI C,-NDEV
	MOVSI D,INIDVT-INIDVE
INIDVL:	HRRZ A,INIDVT(D)	; Get number of units
	PUSH P,A		; Save
INIDV0:	MOVE A,INIDVT+1(D)	; Get initial dispatch address
	MOVEM A,DEVDSP(C)
	MOVE A,INIDVT+2(D)	; Get device characteristics
	MOVEM A,DEVCHR(C)
	MOVE A,INIDVT(D)	; Get name and units
	HLLZM A,DEVNAM(C)	; Save fixed part of name
	TRNN A,777777		; Multiple units?
	JRST INIDV1		; No.
	SOS B,(P)		; Yes, get real unit number
	HRRZS A
	SUBI A,1(B)
	HRROM A,DEVUNT(C)	; Save in devunt
	PUSHJ P,NUMSIX		; Convert number to sixbit string
	HLRM A,DEVNAM(C)	; Save in rh of name
	AOBJN C,.+1
	SKIPLE (P)		; Any more units of this kind?
	JRST INIDV0		; Yes.
	JRST INIDV2		; No.

INIDV1:	SETOM DEVUNT(C)		; No units, no job assigned
	AOBJN C,.+1
INIDV2:	ADD D,[XWD 3,3]		; Setp to next entry
	POP P,A
	JUMPL D,INIDVL		; Loop until done
	JUMPGE C,CPOPJ
	SETZM DEVNAM(C)		; Clear unused entries
	SETZM DEVCHR(C)
	SETZM DEVUNT(C)
	SETZM DEVDSP(C)
	AOBJN C,.-4
	POPJ P,


; Device lookup

DEVLUK::MOVE B,1(A)
	TLNN B,774000
	JRST DEVLK5		; Null name not ok
	ANDCMI B,377
	CAMN B,[ASCIZ /CTY/]
	JRST [	MOVEI A,CTYLIN
		JRST DEVLK4]
	CAME B,[ASCIZ /TTY/]
	JRST DEVLK0
	MOVE B,JOBNO
	HLRZ A,JOBPT##(B)
	CAIN A,777777
	JRST DEVLK5
DEVLK4:	HRLI A,600012
	PUSHJ P,CHKDEV
	JRST DEVLK5
	JRST DEVLK3

DEVLK0:	PUSHJ P,ASCSIX		; Convert to sixbit
	JRST DEVLK5		; Non-sixbit or too many
	MOVSI B,-NDEV
DEVLK1:	CAME A,DEVNAM(B)
	AOBJN B,DEVLK1
	JUMPGE B,DEVLK5
DEVLK3:	MOVE DEV,DEVDSP(B)
	HLRZ A,DEVUNT(B)
	CAIE A,777777
	CAMN A,JOBNO
	JRST DEVLK7
	MOVEI A,GJFX29
	POPJ P,

DEVLK7:	MOVE A,DEVCHR(B)
	HRL DEV,DEVUNT(B)
	TLNE A,(1B7)
	TLNE A,(1B8)
	JRST SKPRET
	MOVEI A,GJFX28
	POPJ P,

DEVLK5:	MOVEI A,GJFX16
	POPJ P,

; Convert number to sixbit characters

NUMSIX:	PUSH P,C
	PUSH P,B
	PUSH P,D
	MOVE C,[POINT 6,D]
	MOVEI D,0
	ANDI A,777
	PUSHJ P,NUMSI1
	MOVE A,D
	POP P,D
	POP P,B
	POP P,C
	POPJ P,

NUMSI1:	IDIVI A,8
	HRLM B,(P)
	SKIPE A
	PUSHJ P,NUMSI1
	HLRZ A,(P)
	ADDI A,20
	IDPB A,C
	POPJ P,


; Routine to convert ascii to sixbit
; Call:	A	; Lookup pointer
;	PUSHJ P,ASCSIX	; For six characters
; Or
;	PUSHJ P,ASC3SX	; For three characters

ASCSIX::SKIPA C,[6]
ASC3SX::MOVEI C,3
	HRLI A,(<POINT 7,0,35>)
	PUSH P,A
	SETZB A,B
	PUSH P,D
ASCSX1:	ILDB D,-1(P)
	JUMPE D,ASCSX3		; Null, done
	SOJL C,ASCSXR		; Too many characters, error
	SUBI D,40
	JUMPL D,ASCSXR		; Not sixbit
	CAIL D,100
	JRST ASCSXR
	ROTC A,6
	IOR A,D
	IORI B,77
	JRST ASCSX1

ASCSX4:	ROTC A,6
ASCSX3:	TLNN A,770000
	JUMPN A,ASCSX4
	AOS -2(P)
ASCSXR:	POP P,D
	POP P,C
	POPJ P,

	END
