;
; +++++++++++++++++++++++
; +++ KERMIT Base I/O +++
; +++++++++++++++++++++++
;
;==================== Begin of I/O =====================;
; COMM I/O						;
;=======================================================;
;
; Get character if new one ready
; EXIT	Accu holds character
;
.auxchr:
	call	auxrdy		; Look for any
	jr	z,.auxchr	; .. wait for any
	call	auxchr		; .. get it
	ret
;
; Put a character to COMM port
; ENTRY	Accu holds character
;
prtout:
IF	(IOport EQ _PORT) OR (IOport EQ _INTRPT) OR (IOport EQ _QUEUE)

	ld	b,a
@xmwt:
	call	outrdy		; Wait for ready
	jr	z,@xmwt
	ld	a,b
	out	(datprt),a	; Output
	ret
;
; Check COMM ready for data
; EXIT	Return Zero reset if port ready for data
;
outrdy:
	call	cliost		; Reset SIO
	in	a,(comprt)	; Get state
	and	rcvrdy		; Test bit
	ret
;
; Clear the COMM status register
;
cliost:
	ld	a,siores
	out	(comprt),a	; Simple reset
	ret

ELSE
IF	IOport	EQ	_BDOS

	push	de
	ld	e,a
	ld	c,auxout
	call	BDOS		; Output via BDOS
	pop	de
	ret

ENDIF;.._BDOS
ENDIF;.._PORT OR .._INTRPT

IF	IOport	EQ	_PORT

; Check COMM input state
; EXIT	Zero reset on character from port
;
auxrdy:
	call	cliost
	in	a,(comprt)	; Get status
	and	rxcav		; Get bit
	ret
;
; Receive COMM character
; EXIT	Accu holds character from port
;
auxchr:
	in	a,(datprt)	; Get it
	ret

ELSE
IF	IOport	EQ	_BDOS

; Check COMM input state
; EXIT	Zero reset on character from port
;
auxrdy:
	ld	c,auxist
	call	BDOS		; Get status
	and	00000001b	; Get bit
	ret
;
; Receive COMM character
; EXIT	Accu holds character from port
;
auxchr:
	ld	c,auxin
	call	BDOS		; Get character
	ret
ELSE
IF	IOport	EQ	_INTRPT

; Check COMM input state
; EXIT	Zero reset on character from port
;
auxrdy:
	call	userf		; Get status
SAist:
	dw	$
	ld	a,0
	adc	a,0		; .. add carry
	or	a		; .. for fixing flag
	ret
;
; Receive COMM character
; EXIT	Accu holds character from port
;
auxchr:
	call	userf		; Get character
SAinp:
	dw	$
	ret

ELSE
IF	IOport	EQ	_QUEUE

; Check COMM input state
; EXIT	Zero reset on character from port
;
auxrdy:
	call	userf		; Get status
SAist:
	dw	$
	ld	a,0
	adc	a,0		; .. add carry
	or	a		; .. for fixing flag
	ret
;
; Receive COMM character
; EXIT	Accu holds character from port
;
auxchr:
	call	userf		; Get character
SAinp:
	dw	$
	ret

ENDIF;.._QUEUE
ENDIF;.._INTRPT
ENDIF;.._BDOS
ENDIF;.._PORT

;=======================================================;
; Character I/O						;
;=======================================================;
;
; Test Character available from keyboard
; EXIT	Zero Flag set if no Character available
;
conrdy:
	ld	c,consta	; Is a char on the console?
	call	BDOS
	or	a
	ret
;
; Put Character to Console
; ENTRY	Accu holds Character
;
putcon:
	ld	c,conout
	ld	e,a		; Get character
	call	BDOS
	ret
;
; Get Character from Keyboard
; EXIT	Accu holds Character
;
getcon:
	ld	c,conin		; Get the char.
	call	BDOS
	ret
;
; Get character from console
; EXIT	Accu holds character
;
dircon:
	ld	a,0fdh
	jr	condir		; Get directly
;
; Get anything from Console
; EXIT	Zero Flag set Indicates Nothing
;
contst:
	ld	a,0ffh
;
; Put Character to Console the direct way
; ENTRY	Accu holds Character
;
condir:
	ld	c,dconio
	ld	e,a		; Get character
	call	BDOS
	or	a
	ret
;
; Print a string on console
; ENTRY	Reg DE holds string addresse ended with '$'
;
prtstr:
	ld	c,prstr
	call	BDOS		; Print
	ret
;
; Put a Character to Printer
; ENTRY	Reg E holds Character
;
putlst:
	res	7,e		; .. no MSB
	ld	a,e
	cp	' '		; Test printable
	jr	nc,..putlst
	add	a,'@'
	push	af
	ld	e,'^'
	call	..putlst	; Map control
	pop	af
	ld	e,a
..putlst:
	ld	c,lstout
	call	BDOS		; Char to printer
	ret
;
;=======================================================;
; File I/O						;
;=======================================================;
;
; Open Existing File
; ENTRY	Reg DE points to FCB
; EXIT	Zero flag set on file not found
;
open:
	ld	c,openf		; See if the file exists.
	jr	dofile
;
; Create a New File
; ENTRY	Reg DE points to FCB
; EXIT	Zero flag set on no directory space
;
create:
	ld	c,makef		; Now create it.
dofile:
	call	chk.dsk		; Check legal disk
	push	de
	pop	ix
	xor	a
	ld	(ix+_EX),a	; Zero the extent.
	ld	(ix+_EG),a	; Must be zero for MAKEF or OPENF.
	ld	(ix+_CR),a	; Zero the current record.
	call	BDOS
	cp	cpmerr
	ret
;
; Set file attribute
; ENTRY	Reg DE points to FCB
;
attrbut:
	ld	c,setfat
	call	BDOS		; Set ..
	ret
;
; Find Size of a File
; ENTRY	Reg DE points to FCB
; EXIT	RRN of FCB holds Size
;
filsiz:
	ld	c,cflsz		; Get file-size
	call	BDOS
	ret
;
; Delete File
; ENTRY	Reg DE points to FCB
; EXIT	Accu holds error code
;
delete:
	ld	c,delf		; Delete the file if it exists.
	call	chk.dsk
	call	BDOS
	ret
;
; Close a File
; ENTRY	Reg DE points to FCB
; EXIT	Accu holds error code
;
close:
	ld	c,closf		; Close up the file.
	call	BDOS
	ret
;
; Search for First FCB Entry
; FCB in Location <cmfcb>
;
srcfxx:
	ld	de,(cmfcb)	; Get FCB
	jr	sr1
;
; Search for First FCB Entry
; EXIT	Accu holds Error Code
;
srcfrs:
	ld	de,fcb
	call	chk.dsk		; Check valid drive
sr1:
	ld	c,sfirst	; Search first
	call	BDOS
	ret
;
; Search for Next FCB Entry
; EXIT	Accu holds Error Code
;
srcnxt:
	ld	c,snext		; Search next
	call	BDOS
	ret
;
; Read a Random Record from Disk
; ENTRY	Reg DE points to FCB
; EXIT	Accu holds Error Code
; 	Zero Flag set on Success
redrnd:
	ld	c,rrand		; Read a record.
	jr	sec.IO
;
; Write a Sequential Record to Disk
; ENTRY	Reg DE points to FCB
; EXIT	Accu holds Error Code
; 	Zero Flag set on Success
;
wrtseq:
	ld	c,writef	; The write code.
	jr	sec.IO
;
; Read a Sequential Record from Disk
; ENTRY	Reg DE points to FCB
; EXIT	Accu holds Error Code
; 	Zero Flag set on Success
;
redseq:
	ld	c,readf		; Read a record.
sec.IO:
	call	BDOS
	or	a		; 00H => I/O O.K
	ret
;
;=======================================================;
; System Management Calls				;
;=======================================================;
;
; Get disk free space
; ENTRY	Accu holds drive
; EXIT 	Last defined DMA buffer holds free records
;
spcget:
	ld	e,a		; Set drive
	ld	c,getspc
	call	BDOS		; Get space
	ret
;
; Init the BDOS Disk System
;
inidsk:
	ld	c,inbdos
	call	BDOS		; Do the init
	ret
;
; Reset one drive
; ENTRY	Accu holds drive
;
resdsk:
	ld	h,1		; Init bit
	or	a
	jr	z,res..
..res:
	add	hl,hl		; Shift bit
	dec	a
	jr	nz,..res
res..:
	ld	c,indsk
	call	BDOS		; Reset
	ret
;
; Check selected drive for FCB OK
; ENTRY	Reg DE points to FCB
; EXIT	Restart KERMIT on error
;
chk.dsk:
	push	bc		; Save BDOS code
	push	de		; .. and FCB
	ld	a,(de)		; Get drive
	or	a		; Check current
	jr	nz,..ck
	call	getdrv		; Get actual
	inc	a
..ck:
	dec	a
	push	af
	call	resdsk		; Reset this drive
	pop	af
	call	logdrv		; Log it
	pop	de
	pop	bc
	ret	nc		; Ok
	call	crlf
	ld	de,cmer05	; Give error
	jp	..reent		; .. reenter
;
; Fetch Current Drive
; EXIT	Accu holds Drive
;
getdrv:
	ld	c,rddrv		; Get our logged in drive
	call	BDOS
	ret
;
; Log a Drive
; ENTRY	Accu holds Drive to be Logged
; EXIT	Carry set on disk not inserted
;
logdrv:
	push	af
	ld	a,0ffh
	call	errmod		; Turn off BDOS errors
	pop	af
	ld	e,a		; Get drive
	ld	c,logdsk
	call	BDOS		; And "LOG" it
	push	af
	ld	a,0
	call	errmod		; Turn on BDOS errors again
	pop	af
	rra			; Set condition
	ret
;
; Fetch Current User
; EXIT	Accu holds user 0..15
; 	Reg HL holds ASCII number (L holds tens)
;
getusr:
	ld	a,0ffh
	call	logusr		; Get current user
	ld	l,' '
	ld	h,a
	cp	10		; Test range
	jp	c,usrl10
	push	af
	sub	10
	ld	h,a		; Fix user
	ld	l,'1'
	pop	af
usrl10:
	ld	de,3000h
	add	hl,de		; Make ASCII
	ret
;
; Log a User
; ENTRY	Accu holds User to be Logged
;
logusr:
	ld	e,a		; Get user
	ld	c,usrcod
	call	BDOS		; And "LOG" it
	ret
;
; Set DMA Buffer for File Transfer
;
dmaset:
	ld	c,setdma	; Init DMA addr
	ld	de,buff
	call	BDOS
	ret
;
; Set BDOS error mode
; ENTRY	Accu holds error code
;
errmod:
	ld	e,a
	ld	c,errcod
	call	BDOS		; Set code
	ret
;
; Set/Get state of console
; ENTRY	Reg DE holds new mode (-1 is get)
; EXIT	Reg HL holds current mode on get
;
conmod:
	ld	c,consmod
	call	BDOS		; Do the mode
	ret
;
; Get or Put System Control Block (SCB) data
; ENTRY	Reg DE points to contol block
;
scb:
	ld	c,scbfun
	call	BDOS		; Get or Put
	ret
;
; Get CP/M version
; EXIT	Accu holds version
;
getcpm:
	ld	c,verfun
	call	BDOS		; Fetch version
	ret
;
; Get special JOYCE version
; EXIT	Accu holds version
;
usbios	equ	3*(userfn-1)
;
getams:
	ld	hl,(warm+1)	; Get BIOS base
	ld	de,usbios
	add	hl,de		; Point to special
	ld	(userf+1),hl	; Save as JMP address
userf:	jp	$-$


