;----------------------------------------------------------------
;
; NSDOSIO.ASM
;    Console I/O routines for North Star DOS on an IMSAI SIO-2
;    serial board.
;
;    09/23/2019, Mike Douglas
;
;----------------------------------------------------------------

; SIO-2 equates

CSRA	equ	03h		;port a control/status register
DRA	equ	02h		;port a data register
SIOCTL	equ	08h		;interrupt control register

RDRF	equ	02h		;receive data bit mask
TDRE	equ	01h		;transmit ready bit mask

; These four jumps at 200Dh are console I/O jumps in NS DOS

	org	200Dh
	jmp	cOut		;character out
	jmp	cIn		;character in
	jmp	tInit		;initialization
	jmp	contC		;control C routine
	
; The acutal I/O routines are at 2900h or 2A00h for single
;   and double density DOS, respectively

	org	2900h		;for single density NSDOS
;	org	2A00h		;for double density NSDOS.

; tInit - initialize the 8251 console serial port

tInit	xra	a
	out	SIOCTL		;no interrupts
	
	out	CSRA		;account for possible power up into
	out	CSRA
	out	CSRA

	mvi	a,40h		;reset 8251
	out	CSRA

	mvi	a,4Eh		;8N1, /16
	out	CSRA
	
	mvi	a,37h		;reset errors, enable xmit, rcv
	out	CSRa

	ret

; cIn - wait for next character received and return in A

cIn	in	CSRA		;A=port a status
	ani	RDRF		;read data present?
	jz	cIn		;loop if not

	in	DRA		;A=data from serial port
	ani	07Fh		;strip parity
	ret

; cOut - output the character passed in B

cOut	in	CSRA		;A=port a status
	ani	TDRE		;OK to send?
	jz	cOut		;loop if not

	mov	a,b		;A=character to send
	out	DRA		;send it
	ret
	
; contC - see if a character has been typed, and if so, is it
;   a ctrl-c. If so, return Z true, else return Z false.

contC	in	CSRA		;A=port a status
	ani	RDRF		;read data present?
	xri	RDRF		;not zero if no character	
	rnz

	in	DRA		;A=data from serial port
	ani	7Fh		;strip parity
	cpi	03h		;ctrl-c?
	ret
	
	end
	

