.ENT Random,GetPinValue,SetPinValue,GetBusValue,SetBusValue,SetBusOpen,SetPinOpen
	.SREL
Random: .RANDOM
GetPinValue: .GPV
SetPinValue: .SPV
GetBusValue: .GBV
SetBusValue: .SBV
SetBusOpen: .SBO
SetPinOpen: .SPO

	.NREL
.RANDOM: LDA 0 CRAM
	LDA 1 C377
	ANDS 0,1
	MOVZL 1 1
	ADDZL 0 1
	MOVZL 1 1
	ADD 1 0
	LDA 1 RCONST
	ADD 1 0
	STA 0 CRAM
	JMP 1 3


CRAM: 0
C377: 377
RCONST: 13849.
c400: 400
cbit4: 4000
utilout: 177016
utilin: 177030
count: 0
sac2: 0
lowc: 6000
highc: 7000
offc: 2000
c2: 2


.SBO: sta 3 1 2
	sta 0 count
	lda 3 c2
	sne 3 0
	jmp fewargs ;2 pins in this bus (3  args)
	lda 3 3 2 ;displacement of args in callers frame
	add 2 3
	inc 3 3
	sta 1 1 3 ;store second arg
	lda 0 offc
	sta 2 sac2
oloop:	inc 3 3 ;pointer to arg
	lda 1 0 3	;pin number
oloopx: add 0 1
	sta 1 @utilout
	movl 1 2
	movor 2 2
	sta 2 @utilout
	sta 1 @utilout
	dsz count
	jmp oloop
	lda 2 sac2
	lda 3 1 2
	jmp 1 3

fewargs: lda 0 offc
	sta 2 sac2
	add 2 3 ;ac3 now points one below 3rd arg if there
	jmp oloopx


.GBV: sta 3 1 2
	sta 0 count
	lda 3 c2
	sne 3 0
	jmp gfargs
	lda 3 3 2 ;displacement of args in callers frame
	add 2 3
	inc 3 3
	sta 1 1 3 ;store second arg
	sub 0 0
	sta 2 sac2
gloop:	inc 3 3 ;pointer to arg
	lda 1 0 3	;pin number
gloopx: lda 2 c400
	add 2 1
	sta 1 @utilout
	movl 1 2
	movor 2 2
	sta 2 @utilout
	sta 1 @utilout
	lda 2 cbit4
	lda 1 @utilin
	and 1 2 szr
	movol 0 0 skp	;put 1 in value
	movzl 0 0	;put 0 in value
	dsz count
	jmp gloop
	lda 2 sac2
	lda 3 1 2
	jmp 1 3

gfargs: sub 0 0
	sta 2 sac2
	add 2 3
	jmp gloopx


.SPO:	lda 1 offc
	add 1 0
	sta 0 @utilout
	movl 0 1
	movor 1 1
	sta 1 @utilout
	sta 0 @utilout
	jmp 1 3

.GPV:	lda 1 c400 ;get pin value
	add 1 0
	sta 0 @utilout
	movl 0 1
	movor 1 1
	sta 1 @utilout
	sta 0 @utilout
	lda 1 cbit4
	lda 0 @utilin
	and 1 0 szr
	subzl 0 0 skp ;load one
	sub 0 0	;load zero
	jmp 1 3

.SPV: movr 1 1 snc
	jmp lowval
highval:	lda 1 highc
	jmp send
lowval: lda 1 lowc
send:	add 1 0
	movl 0 1
	movor 1 1
	sta 0 @utilout
	sta 1 @utilout
	sta 0 @utilout
	jmp 1 3


.SBV: sta 3 1 2  ;save return
	sta 0 count ;first arg
	lda 3 3 2 ;offset of args
	add 2 3 ;pointer to args
	sta 1 2 3 ;store second arg in 3rd word of arg block
	inc 3 3
	add 0 3 ;ac3 points to last arg
	lda 1 1 3 ;value
	sta 2 sac2
	sta 3 argp
	lda 3 lowc
sloop:	lda 0 @argp
	movr 1 1 snc
	mov 3 2 skp	;value is zero
	lda 2 highc
	add 2 0
	sta 0 @utilout
	movl 0 2
	movor 2 2
	sta 2 @utilout
	sta 0 @utilout
	dsz argp
	dsz count
	jmp sloop
	lda 2 sac2
	lda 3 1 2
	jmp 1 3

argp: 0


	.END