	.Z80
	TITLE	COLEX-880 series Slave circuit driver
	SUBTTL	Copyright (C) 1984, Colex Electronic Co. Ltd.
	NAME	('SCKTDR')
	INCLUDE	COPYRIGH
;****************************************************************
;*								*
;*		Slave Network Circuit Driver			*
;*								*
;*	This module receives & transmits packages between	*
;*	the master processor and the slave.			*
;*								*
;****************************************************************
;  Date		  Author		Modification
;03-Jul-84	R.J. Wellsted
;05-Jul-84	R.J. Wellsted	Added ?init? code to get node id
	PAGE

	INCLUDE	NETEQU

SCC0C	EQU	2		;scc control port

	PUBLIC	CKTIN@,CKTDR@	;init & driver entry

	EXTRN	WAIT,SIGNAL
	EXTRN	LNKPOL,UNLINK
	EXTRN	CKTAST

	COMMON	/?INIT?/
CKTIN@:	LD	A,(RXBUFS)	;get node no.
	LD	L,A		;make 16 bit
	LD	H,0
	LD	(CKTAST),HL	;save in circuit table
	RET

	CSEG
CKTDR@:	LD	A,C		;get function no.
	OR	A		;test for rx
	JR	Z,RXCKT		;skip if so
	DEC	A		;test for tx
	JR	Z,TXCKT		;skip if so
	LD	A,-1		;flag error
	RET			;done

RXCKT:	INC	DE
	INC	DE
	INC	DE		;adjust past linkage
	INC	DE
	LD	HL,RXMXLK	;point to mutual exclusion lock
	CALL	WAIT		;get it
	LD	(RXBUFP),DE	;save message buffer address
	LD	DE,WAIENQ	;point to poll node
	CALL	LNKPOL		;add to poll list
	CALL	ENQWAI		;pre-test status
	LD	HL,RXEVNT	;point to semaphore
	CALL	WAIT		;wait for a message
	LD	DE,WRXEOT	;point to poll node
	CALL	LNKPOL		;add to poll list
	CALL	EOTWRX		;pre-test
	LD	HL,RXEVNT	;point to semaphore
	CALL	WAIT		;wait for eot
RXEXIT:	LD	HL,RXMXLK	;point to mutual exclusion lock
	CALL	SIGNAL		;finished with it
	XOR	A		;no errors
	RET			;done

TXCKT:	INC	DE
	INC	DE
	INC	DE		;adjust pointer past linkage
	INC	DE
	LD	HL,TXMXLK	;point to mx lock
	CALL	WAIT		;get it
	LD	(TXBUFP),DE	;save message buffer address
	LD	A,SLVENQ	;load enq
	LD	(TXBUFS),A	;set status byte
	LD	HL,PREQ		;point to scc command
	LD	B,PREQL		;load scc command length
	LD	C,SCC0C		;point to scc control port
	OTIR			;send to scc
	LD	DE,WAIACK	;point to poll node
	CALL	LNKPOL		;link into poll list
	CALL	ACKWAI		;pre-test status
	LD	HL,TXEVNT	;point to semaphore
	CALL	WAIT		;wait for it
	LD	DE,WTXEOT	;point to poll node
	CALL	LNKPOL		;add to poll list
	CALL	EOTWTX		;pre-test status
	LD	HL,TXEVNT	;point to semphore
	CALL	WAIT		;wait for eot
TXEXIT:	LD	HL,TXMXLK	;point to mx lock
	CALL	SIGNAL		;release it
	XOR	A		;no errors
	RET			;done

WAIENQ:	DW	0		;forward linkage
	DW	0		;backward linkage
ENQWAI:	LD	HL,RXBUFS	;point to status byte
	LD	A,(HL)		;get it
	CP	MSTENQ		;master wants to xfer?
	RET	NZ		;done if not
	LD	(HL),SLVACK	;send an ack now
	LD	HL,RXEVNT	;point to event semaphore
	CALL	SIGNAL		;signal ready
	LD	HL,WAIENQ	;point to poll node
	CALL	UNLINK		;unlink it
	RET			;done

WRXEOT:	DW	0		;forward linkage
	DW	0		;backward linkage
EOTWRX:	LD	HL,RXBUFS	;point to status byte
	LD	A,(HL)		;get it
	CP	MSTEOT		;master finished ?
	RET	NZ		;done if not
	LD	(HL),SLVEOT	;flag eot
	LD	HL,RXEVNT	;point to semaphore
	CALL	SIGNAL		;signal ready
	LD	HL,WRXEOT	;point to poll node
	CALL	UNLINK		;remove from poll list
	RET			;done

WAIACK:	DW	0		;forward linkage
	DW	0		;backward linkage
ACKWAI:	LD	HL,TXBUFS	;point to status byte
	LD	A,(HL)		;get it
	CP	MSTACK		;master wants to xfer?
	RET	NZ		;done if not
	LD	HL,TXEVNT	;point to event semaphore
	CALL	SIGNAL		;signal ready
	LD	HL,WAIACK	;point to poll node
	CALL	UNLINK		;unlink it
	RET			;done

WTXEOT:	DW	0		;forward linkage
	DW	0		;backward linkage
EOTWTX:	LD	HL,TXBUFS	;point to status byte
	LD	A,(HL)		;get it
	CP	MSTEOT		;master finished ?
	RET	NZ		;done if not
	LD	(HL),SLVEOT	;flag eot
	LD	HL,TXEVNT	;point to semaphore
	CALL	SIGNAL		;signal ready
	LD	HL,WTXEOT	;point to poll node
	CALL	UNLINK		;remove from poll list
	RET			;done

	DSEG

RXMXLK:	DW	1		;semaphore count
	DW	$		;forward link
	DW	$-2		;backward link

RXEVNT:	DW	0		;semaphore count
	DW	$		;forward link
	DW	$-2		;backward link

TXMXLK:	DW	1		;semaphore count
	DW	$		;forward link
	DW	$-2		;backward link

TXEVNT:	DW	0		;semaphore count
	DW	$		;forward link
	DW	$-2		;backward link

PREQ:	DB	05,11101010B
	DB	05,11101000B
PREQL	EQU	$-PREQ

	END
