;
; +++++++++++++++++++++++++++++++
; +++ KERMIT System interface +++
; +++++++++++++++++++++++++++++++
;
IF	GERMAN
lp	equ	'('
rp	equ	')'
ELSE
lp	equ	'['
rp	equ	']'
ENDIF;GERMAN

versio:
	db	'KERMIT-80 V3.9F'
	db	' ',lp,'PCW8256/8512 OS 1.'
OSver:
	db	'x'

IF	GERMAN
IF	IOport	EQ	_PORT
	db	' - Direkte serielle I/O'
ELSE
IF	(IOport EQ _INTRPT) OR (IOport EQ _QUEUE)
	db	' - Interrupt I/O'
ENDIF;.._INTRPT
ENDIF;.._PORT
ELSE
IF	IOport	EQ	_PORT
	db	' - Direct serial I/O'
ELSE
IF	(IOport EQ _INTRPT) OR (IOport EQ _QUEUE)
	db	' - Interrupt I/O'
ENDIF;.._INTRPT
ENDIF;.._PORT
ENDIF;GERMAN
	db	rp,eot

errver:
IF	GERMAN
	db	'Dieses KERMIT ist angepasst an den JOYCE '
	db	'(AMSTRAD PCW) Rechner und funktioniert nur '
	db	'auf dieser Maschine.',cr,lf,eot
ELSE
	db	'This KERMIT is designed for the JOYCE '
	db	'(AMSTRAD PCW) machine and runs on no other '
	db	'one.',cr,lf,eot
ENDIF;GERMAN
drvtbl:				; Legal drives for PCW
	ds	'P'-'A'+1
drvlen:
	db	0
;
; Escape definitions

_under	macro
	db	esc,'r'
	endm

_nunder	macro
	db	esc,'u'
	endm

_invers	macro
	db	esc,'p'
	endm

_normal	macro
	db	esc,'q'
	endm

_clline	macro
	db	esc,'M'
	endm

_clscr	macro
	db	esc,'E'
	endm

_clreol	macro
	db	esc,'K'
	endm

_home	macro
	db	esc,'H'
	endm

_nocurs	macro
	db	esc,'f'
	endm

_curson	macro
	db	esc,'e'
	endm

_window	macro	top,left,height,width
	db	esc,'X',top+32,left+32,height-1+32,width-1+32
	endm

_goto	macro	x,y
	db	esc,'Y',y+32,x+32
	endm

;
; Get installed drives
;
getdrives:
	ld	de,BIOSPB
	ld	c,bioscl
	call	BDOS		; Get address
	ld	de,drvtbl
	ld	bc,256*('P'-'A'+1)
	ld	ix,drvlen
	ld	(ix),0
drvsetup:
	ld	a,(hl)
	inc	hl
	or	(hl)		; Test defined
	inc	hl
	jr	z,SkpDrv
	inc	(ix)		; Bump count
	ld	a,'A'
	add	a,c
	ld	(de),a		; Save drive
	inc	de
SkpDrv:
	inc	c
	djnz	drvsetup
	ret
;
BIOSPB:
	db	drvtab,0
	dw	0,0,0
;
; Get system environment
; EXIT	Zero flag set if ok
;
getenv:
	call	getams		; Get special validation
	dw	cdvers
	cp	amstra		; .. compare
	ret	nz
	ld	a,b
	cp	_MAJOR		; Test major OS
	ret	nz
	ld	a,c
IF	(IOport EQ _INTRPT) OR (IOport EQ _QUEUE)
	ld	de,SAinp.a
	ld	hl,SAist.a
	cp	_MINORa		; Find minor OS
	jr	z,gotOSa
	cp	_MINORb
	ret	nz
	ld	de,SAinp.b
	ld	hl,SAist.b
gotOSa:
	ld	(SAinp),de	; .. set vectors
	ld	(SAist),hl
ENDIF;IOport EQ _INTRPT OR _QUEUE
	add	a,'0'
	ld	(OSver),a
	xor	a
	ret
;
; Perform system specific initialization
;
illini:
	ld	de,errsio
	call	prtstr		; Tell error
	jp	warm		; Bye, bye
sysini:
	call	userf		; Get info about SIO
	dw	cdinfo
	ld	a,c
	cp	0ffh		; FFH is fitted state
	jr	nz,illini
	call	userf		; Get SIO parameters
	dw	cdpara
	ld	(SAmode),a	; .. save parameters
	ld	(SAset),de
	ld	(SAbits),hl
	ld	h,8		; Force eight data bits
	ld	l,8
IF	IOport EQ _PORT
	ld	a,PollMod	; Set mode
ELSE
	ld	a,IntMod	; Set mode
ENDIF;IOport EQ _PORT
	call	SA.Init		; And set it really
	ld	a,dtron		; Set DTR on
	call	SA.Init
	ld	a,TRUE
	ld	(DTR),a		; .. set flag
	ld	a,rtson
	call	SA.Init		; And RTS, too
IF	IOport	EQ	_BDOS
	ld	de,scb.i.g
	call	scb		; Get vector
	ld	(scb.i.s+2),hl	; Save it
	ld	de,scb.i.p
	call	scb		; Force AUXIN to SIO
	ld	de,scb.o.g
	call	scb		; Get vector
	ld	(scb.o.s+2),hl	; Save it
	ld	de,scb.o.p
	call	scb		; Force AUXOUT to SIO
ENDIF;IOport EQ _BDOS
	call	getdrives
	ret
errsio:
IF	GERMAN
	db	'JOYCE (AMSTRAD PCW) ben|tigt zus{tliche '
	db	'serielle Schnittstelle',cr,lf,eot
ELSE
	db	'JOYCE (AMSTRAD PCW) requires additional '
	db	'serial interface',cr,lf,eot
ENDIF;GERMAN
;
; Set system baud rate
; ENTRY	Reg H holds encoded receiver    baud rate
; 	Reg L holds encoded transmitter baud rate
;
sysbds:
	call	userf		; Ok, program it
	dw	cdbaud
	ret
;
; Get SIO values
; EXIT	Accu and reg B hold receiver encoded baud rate
;	Reg C holds transmitter      encoded baud rate
;	Reg D holds number of stop bits
;	Reg E holds parity
;	Reg H holds receiver    data bits
;	Reg L holds transmitter data bits
;
sysbdg:
	call	userf		; Get rate
	dw	cdpara
	ld	a,b
	ret
;
; Execute SIO exit
;
sysexit:
	ld	a,rtsoff	; Turn off RTS
	call	SA.Init
	ld	a,dtroff	; And DTR
	call	SA.Init
	ld	a,FALSE
	ld	(DTR),a
	ret
;
; Execute SIO reset
;
sysreset:
	ld	a,(SAmode)	; .. get parameters
	ld	de,(SAset)
	ld	hl,(SAbits)
	call	userf		; Reset SIO except Baud rate
	dw	cdinit
	ret
;
SAmode:
	db	0
SAset:
	dw	0
SAbits:
	dw	0
;
; Do SIO break
;
sysbrk:
	ld	a,reg5
	out	(comprt),a	; Set WR5
	ld	a,comwrd+break
	out	(comprt),a	; Give a BREAK
	ld	a,reg5
	out	(comprt),a
	ld	a,30		; Delay 300msec
	call	delay
	ld	a,comwrd
	out	(comprt),a
	ld	a,reg0
	out	(comprt),a	; Reset WR0
	jp	rskp		; And return
;
; Drop DTR a bit - about 2.5 sec
;
DTR.Drop:
	ld	a,(DTR)		; Test DTR off
	or	a
	ret	z		; .. yeap
	call	.DTR.Off	; Turn it off
	ld	a,250		; Delay 2500msec -> 2.5sec
	call	delay
	call	.DTR.On		; Turn it on again
	ret
;
; Turn on DTR if not yet on
;
DTR.On:
	ld	a,(DTR)		; Get state
	or	a		; Test ON
	ret	nz		; .. yeap
	cpl
	ld	(DTR),a
.DTR.Off:
	ld	a,dtron		; Turn it on
	call	SA.Init
	ret
;
; Turn off DTR if not yet off
;
DTR.Off:
	ld	a,(DTR)		; Get state
	or	a		; Test OFF
	ret	z		; .. yeap
	cpl
	ld	(DTR),a
.DTR.On:
	ld	a,dtroff	; Turn it off
	call	SA.Init
	ret
DTR:
	db	FALSE
;
; Delay routine.
; ENTRY	Accu holds time (hundredths of seconds)
;
; (CPUSPD should be set to the system clock rate,
; in units of 100KHz: for the PCW that's 40 for 4.0 MHz. 
;
delay:
	ld	c,cpuspd	; Number of times to wait 1000 T-states to
delay2:				;  make .01 second delay
	ld	b,70		; Number of times to execute inner loop to
				;  make 1000 T-state delay
delay3:
	dec	b		; 4 T-states (* 70 * cpuspd)
	jp	nz,delay3	; 10 T-states (* 70 * cpuspd)
	dec	c		; 4 T-states (* cpuspd)
	jp	nz,delay2	; 10 T-states (* cpuspd)
				; total delay: ((14 * 70) + 14) * cpuspd
				;  = 1001 * cpuspd
	dec	a		; 4 T-states
	jp	nz,delay	; 10 T-states
	ret			; grand total: ((1001 * cpuspd) + 14) * a
;
; Initilaize SIO channel
; ENTRY	Accu holds mode byte
;
SA.Init:
	ld	de,(SAset)	; Get old setting
	ld	h,8		; Force eight data bits
	ld	l,8
	call	userf		; And set it really
	dw	cdinit
	ret
;
; Get screen coordinates
; EXIT	Reg H holds row
;	Reg L holds column
;
sysxy:
	call	userf
	dw	teask		; Get viewpoint
	ret
;
; This routine blanks the screen and resets all attributes
;
ResScr:
	call	ClrTop		; Clear screen
	ld	de,$RESATT	; Print the string to reset attributes
	call	prtstr
	ret
$RESATT:
	_nunder			; No underline
	_normal			; Not inversed
	_curson			; Cursor on
	db	eot
;
; This routine blanks the screen.
;
ClrTop:
	ld	de,$CLRTOP	; Print the string to blank the screen.
	call	prtstr
	ret
$CLRTOP:
	_home			; Go home.
	_clscr			; And clear screen
	db	eot
;
; This routine disables the cursor.
;
Invis:
	ld	de,$INVIS	; Print the string to disable the cursor.
	call	prtstr
	ret
$INVIS:
	_nocurs
	db	eot		; Disable cursor
;
; This routine enables the cursor.
;
Visib:
	ld	de,$VISIB	; Print the string to enable the cursor.
	call	prtstr
	ret
$VISIB:
	_curson
	db	eot		; Enable cursor
;
; This routine deletes a line.
;
DelLin:
	ld	de,$DELLIN	; Print the string to delete a line.
	call	prtstr
	ret
$DELLIN:
	db	cr
	_clline
	db	eot		; Clear line.
;
; This routine clears a line.
;
ClrLin:
	ld	de,$CLRLIN	; Print the string to clear a line.
	call	prtstr
	ret
$CLRLIN:
	db	cr
	_clreol
	db	cr,eot		; Clear to end of line
;
; This routine clears to end of a line.
;
ClrEol:
	ld	de,$CLREOL	; Print the string to clear to end of line.
	call	prtstr
	ret
$CLREOL:
	_clreol
	db	eot
;
; This routine sets invers video.
;
Invers:
	ld	de,$INVERS	; Print the string to invers video.
	call	prtstr
	ret
$INVERS:
	_invers
	db	eot		; Invers video
;
; This routine sets normal video.
;
Normal:
	ld	de,$NORMAL	; Print the string to normalize video.
	call	prtstr
	ret
$NORMAL:
	_normal
	db	eot		; Normalize video
;
; This routine sets underline mode.
;
Under:
	ld	de,$UNDER	; Print the string to underline mode.
	call	prtstr
	ret
$UNDER:
	_under
	db	eot		; Underline mode
;
; This routine sets normal mode.
;
Nunder:
	ld	de,$NOUNDER	; Print the string to normalize mode.
	call	prtstr
	ret
$NOUNDER:
	_nunder
	db	eot		; Normalize mode

