;
;	===========================================
;	
;	ascii to baudot to ascii program
;	with buffer to disk and disk to buffer
;	routines taken from 'link.asm' by l.e.hughs
;	
;	randy suess 12/20/78
;	============================================
;	
;	08/12/79 modifications by jim mills wb9kfp to
;	allow operation using pmmi mm-103 direct-connect
;	modem
;	
;	19-09-83 modificaties door jaap beekman pe1bfs	
;	om het programma beter aan cpm aan te passen.
;	De in en uitvoer is aangepast aan de bdos-calls
;	aan de reader is een 8251-uart gekoppeld waarvan
;	de status door een non-bdos call onderzocht
;	wordt. De baudrate is programmatisch aan te passen
;	dmv een programmeerbare deler (74ls161) die door een
;	code naar de poortnummer BRDIV ingesteld wordt.
;	Niet aan CPM aangepaste programmaregels worden in
;	het commentaarveld aangegeven met ';*'
;	
;	laatste wijziging 27-02-84
;
;	Besturingscodes
;
;	ctrl-c  keer terug naar cpm
;	ctrl-d  breekt een uitzendende cpm-file af
;	ctrl-g	geeft een helpmessage op het scherm
;	ctrl-l	stuurt de in het geheugen opgeslagen tekens
;		naar cpm-file
;	ctrl-p	schakelt printer aan/uit bij zenden en
;		ontvangen
;	ctrl-r  software baudrate schakelaar 46-50-55-60-66-
;		-75-46-50-etc baud 
;	ctrl-u  wis filename buffer en begin opnieuw
;	ctrl-x  zendt cpm-file naar uart
;	ctrl-y	sla ontvangen teken in geheugen buffer op
;
;*****************************************************
;
$title('rtty receive/transmit program tbv CPM-competition')
;
;	bdos entry point and function codes
;
bdos	equ	005h
sycpm	equ	000h	; system reset
cicpm	equ	001h	; console input
cocpm	equ	002h	; console output
ricpm	equ	003h	; reader input
pocpm	equ	004h	; punch output
locpm	equ	005h	; list output
ioset	equ	008h	; set iobyte
cscpm	equ	00bh	; console status
offc	equ	00fh	; open file
cffc	equ	010h	; close file
dffc	equ	013h	; delete file
rrfc	equ	014h	; read record
wrfc	equ	015h	; write record
mffc	equ	016h	; make file
;
;	default fcb and field definitions
;
fcb	equ	05ch	; file controle block
fn	equ	001h	; file name field (rel)
ft	equ	009h	; file type field (rel)
nr	equ	020h	; next record field (rel)
dbuf	equ	080h	; default disk buffer address
;
;	ascii control characters
;
ctr@	equ	000h	;
ctrB	equ	002h	;
ctrC	equ	003h	;
ctrD	equ	004h	;
ctrG	equ	007h	;
bel	equ	007h	; bell
lf	equ	00ah	; line feed
ctrL	equ	00ch	;	
cr	equ	00dh	; carriage return
ctrP	equ	010h	;
ctrR	equ	012h	;
ctrU	equ	015h	;
ctrW	equ	017h	;
ctrX	equ	018h	;
ctrY	equ	019h	;
ctrZ	equ	01ah	;
spt	equ	020h	; spatie
del	equ	07fh	; delete
;
;	uart constanten
;
tbrdy	equ	001h	;* transmitter buffer maskering
rbrdy	equ	002h	;* receiver buffer maskering
tts	equ	071h	;* uart status poort
ttc	equ	071h	;* uart commando poort
brdiv	equ	090h	;* baudrate devider
uasrt	equ	040h	;* software reset 8251
rttymd	equ	043h	;* 1 stopbit 5 databits
			;* no parity en devide by 64
termmd	equ	0ceh	;* 1 stopbit 8 databits
			;* no parity and divide by 16
cmd	equ	027h	;* same as in 8 bit mode
cst50	equ	050h	;* cst voor 50 baud
;
;	iobyte constanten
;
iortty	equ	094h	; co=co	ri=uart po=uart lo=fri
;
;	main program
;
	org	0100h
;
link:	lxi	sp,stack+64	; create local stack
	xra	a	; clear char buffers
	sta	inch
	sta	outch
	sta	memfl	; clear text save flag
	sta	trfl	; transmitting flag
	sta	prflag	;
	sta	brtgl	;
	mvi	e,iortty;
	call	setio	; set iobyte
	xra	a	;*
	out	ttc	;*
	out	ttc	;*
	out	ttc	;*
	mvi	a,uasrt	;*
	out	ttc	;* reset uart
	mvi	a,rttymd;*
	out	ttc	;* command uart for baudot code
	mvi	a,cmd	;*
	out	ttc	;* put uart command
	call	brset	; set up for 50 baud
	lxi	h,tbuf	; set ptr to tbuf
	shld	ptr	;
	lxi	h,0	; size = 0
	shld	size
	call	wccr	; write cr/lf to console
	mvi	a,bel	; ring bell on console, to let
	call	co	; clone know we're ready
;
;	main loop
;
;	control or keyboardinput routine
;
link1:	call	cssts	; look for console input
	cpi	000h	;
	jz	link2
	call	ci	; get key
	ani	07fh	; call pcc if control char
	cpi	spt	; 
	cc	pcc
	jc	link2	; jump if pcc handled char
	ori	080h	; else set valid data bit
	sta	inch	;  store in input char buffer
;
;	rtty input routine
;
link2:	call	uarst	;* get uart receiver status
	jz	link3
	call	rmc
	call	save	; save char in text buffer if
			; flag on
	ori	080h	; set data valid bit
	sta	outch	; store in output char buffer
;
;	rtty output routine
;
link3:	lda	inch	; jump if no data for rtty
	ora	a
	jp	link5
	call	uatst	;* get uart transmitter status
	jz	link5
	ani	07fh	; else discard valid data bit
	cpi	cr
	jnz	link4
	mvi	a,cr
	call	wmc
	mvi	a,lf
	call	wmc
link4:	call	wmc
	xra	a	; then clear input char buffer
	sta	inch
;
;	display	character from rtty
;
link5:	mvi	d,001h	;* delay to ease my display controller
	call	delay	;*
	lda	outch	; 
	ora	a	;
	jz	link1	;
	cpi	0ffh	;
	jz	link1	; avoid non printables
	ani	07fh	; strip flagbit
	cpi	lf	;
	jnz	link6	;
	call	wccr	;
	jmp	link7	;
link6:	call	co	; output if character found
link7:	xra	a	;
	sta	outch	;
	jmp	link1	; end of main loop
;
;	pcc - process control character
;
pcc:	cpi	ctrC	; jump if not control-c
	jnz	pcc1
	xra	a	;*
	out	ttc	;*
	out	ttc	;*
	out	ttc	;*
	mvi	a,uasrt	;*
	out	ttc	;* reset uart
	mvi	a,termmd;*
	out	ttc	;* command uart for terminal
	mvi	a,cmd	;*
	out	ttc	;* put uart command
	jmp	syres	; start cpm again
pcc1:	cpi	ctrX	; jump if not control-x
	jnz	pcc2
	call	stf	; send text file to rtty
	stc		; tell link to ignore this character
	ret
pcc2:	cpi	ctrY	; jump if not control-y
	jnz	pcc3
	mvi	a,1	; turn on text save flag
	sta	memfl
	stc
	ret
pcc3:	cpi	ctrL	; jump if not control-l
	jnz	pcc4
	xra	a	; turn off text save flag
	sta	memfl
	call	wtb	; write text buffer to disk
	lxi	h,tbuf	; clear text buffer
	shld	ptr
	lxi	h,0
	shld	size
	stc
	ret
pcc4:	cpi	ctrR	; jump if not control-r
	jnz	pcc5	;
	call	brset	; baudrate toggle switch
	stc		;
	ret		;
pcc5:	cpi	ctrP	; jump if not control-P
	jnz	pcc6	;
	lda	prflag	;
	xri	080h	; invert bit 7
	sta	prflag
	stc
	ret	 
pcc6:	cpi	ctrG	;
	jnz	pcc7	; jump if not control-g
	lxi	h,msghlp;
	call	wcs	; put help message on screen
	stc		;
	ret		;
pcc7:	stc		; let link handle all other control codes
	cmc
	ret
;
; help message
;
msghlp:	db	cr,lf,'control-C returns to CPM',cr,lf
	db	'control-D aborts a transmitting file',cr,lf
	db	'control-G gives a help mesage',cr,lf
	db	'control-L loads memory buffer into CPM-file',cr,lf
	db	'control-P toggles printer switch',cr,lf
	db	'control-R toggles baudrate switch from 46 to 75 bd',cr,lf
	db	'control-S stops output (cpm function)',cr,lf
	db	'control-U deletes progressing file name',cr,lf
	db	'control-X transmits cpm file',cr,lf
	db	'control-Y enables next characters into memory buffer',cr,lf
	db	'normal keys will be transmitted via uart',cr,lf,0
;
;	stf - send text file (to rtty)
;
stf:	call	gfn	; get name of disk file to send
	jc	stf6	; jump if file name error
	call	open	; try to open specified file
	cpi	0ffh	; jump if file not found
	jz	stf7
	xra	a	; else rewind file and get on with it
	sta	fcb+nr
	cma		; ff in a
	sta	trfl	; set transmitting flag
stf1:	call	read	; read next record into dbuf
	cpi	001h	; jump if end-of-file
	jz	stf5
	lxi	h,dbuf	; point to disk buffer
	mvi	c,128
stf2:	mov	a,m	; fetch next char from dbuf
	inx	h
	cpi	ctrZ	; jump if end-of-file character
	jz	stf5
	call	wmc	; write character to rtty
	call	co	; write character to console
	call	cssts	; onderzoek keyboard
	cpi	000h	;
	jz	stf4	; spring bij geen teken
	call	ci	; haal teken
	ani	07fh
	cpi	ctrD	; test op control-d
	jz	stf5
stf4:	dcr	c	; loop thru rest of dbuf
	jnz	stf2
	jmp	stf1	; go get next record from disk
stf5:	call	close	; clean up act and go home
	mvi	a,bel	; ring bell to let clone
	call	co	; know we're through
	xra	a	;
	sta	trfl	; reset transmitting flag
	ret
stf6:	lxi	h,stfs1	; print 'file name error'
	call	wcs
	ret
stf7:	lxi	h,stfs2	; print 'file not found'
	call	wcs
	ret
;
stfs1:	db	'file name error',cr,lf,0
stfs2:	db	'file not found',cr,lf,0
;
;	save - save char in text buffer if flag on
;	
;	entry conditions
;	a - character to save
;
save:	push	psw
	lda	memfl
	ora	a
	jnz	save1
	pop	psw
	ret
save1:	pop	psw
	push	h
	lhld	ptr
	ani	07fh	; discard parity bit
	jz	save3	; jump if char is a null
	cpi	07fh	;
	jz	save3	;
	mov	m,a
	inx	h
save2:	shld	ptr
	lhld	size	; size = size + 1
	inx	h
	shld	size
save3:	pop	h
	ret
;
;	wtb - write text buffer to disk
;
wtb:	lhld	size	; jump if text buffer empty
	mov	a,l
	ora	h
	jz	wtb5
	call	gfn	; get file name
	jc	wtb6	; jump if file name error
	call	delt	; delete old file, if any
	call	make	; make new file
	xra	a	; rewind file
	sta	fcb+nr
	lhld	size	; de = tbuf size
	xchg
	lxi	h,dbuf	; top of stack points to dbuf
	push	h
	lxi	h,tbuf	; hl points to tbuf
wtb1:	mvi	c,128	; disk buffer size
wtb2:	mov	a,m	; fetch next byte of tbuf
	inx	h
	xthl
	mov	m,a	; store in dbuf
	inx	h
	xthl
	dcx	d	; size = size - 1
	mov	a,d	; exit loop if size = 0
	ora	e
	jz	wtb3
	dcr	c	; loop until dbuf full
	jnz	wtb2
	call	write	; write full dbuf to disk
	xthl		; top of stack points to dbuf
	lxi	h,dbuf
	xthl
	jmp	wtb1	; loop until end of tbuf
wtb3:	pop	h	; hl points to current place in dbuf
wtb4:	mvi	m,ctrZ	; store eof code
	inx	h
	dcr	c	; loop thru rest of dbuf
	jnz	wtb4
	call	write	; write last sector to disk
	call	close	; clean up act and go home
	mvi	a,bel	; ring bell to let clone
	call	co	; know we're through
	ret
wtb5:	lxi	h,wtbs1	; print 'text buffer empty'
	call	wcs
	ret
wtb6:	lxi	h,wtbs2	; print 'file name error'
	call	wcs
	ret
;
wtbs1:	db	'text buffer empty',cr,lf,0
wtbs2:	db	'file name error',cr,lf,0
;
;	wccr - write console carriage return
;	 (and line feed)
;
wccr:	mvi	a,cr
	call	co	;
	mvi	a,lf
	call	co	;
	ret		;
;
;	wcs - write console string
;	
;	entry conditions:
;	hl - points to string (terminate by zero byte)
;
wcs:	mov	a,m	;
	inx	h	;
	ora	a	;
	rz		; terminate
	call	co	;
	jmp	wcs	;
;
;	rcs - read console string (with echo)
;	
;	exit conditions
;	b - number of characters read (<255)
;	hl - points to last char stored (cr)
;
rcs:	lxi	h,ibuf
	mvi	b,000h
rcs1:	call	ci	; read next char from console
	cpi	del	; jump if not del
	jnz	rcs2
	inr	b	; ignore del if ibuf already empty
	dcr	b
	jz	rcs1
	dcx	h	; else discard last char
	mov	a,m	; echo discarded char to console
	call	co	;
	dcr	b	; decrement count
	jmp	rcs1	; and loop
rcs2:	cpi	ctrU	; jump if not control-u
	jnz	rcs3
	call	wccr	; else abort current line
	jmp	rcs	; and start over
rcs3:	mov	m,a	; store char in ibuf
	inr	b	; increment count
	cpi	cr	; jump if carriage return
	jz	rcs4
	inx	h	; else advance pointer
	jmp	rcs1	; and loop
rcs4:	mvi	a,lf	; issue line feed and return
	call	co	;
	ret
;
;	wmc - write rtty character
;	
;	entry conditions
;	a - character to write
;
wmc:	call	rttyxmit
	push	psw	; gonna print it?
	mov	e,a	; save character
	lda	prflag	;
	ani	080h
	mov	a,e	; character in a
	cnz	lo	; print it
	pop	psw
	ret
;	
;
;	rmc - read rtty character
;	
;	exit conditions:
;	a - character read
;
rmc:	call	rttyread
	push	psw	;
	mov	e,a	; save character
	lda	prflag	;
	ani	080h
	mov	a,e	; character in a
	cnz	lo	; print it
	pop	psw
	ret
;
$eject
;	
;	ascrcv(4), ver. 2
;	by keith petersen, w8sdz.	10/30/78.
;	
;	this is a program to change
;	ascii-coded-baudot to ascii.
;	the routine returns with a 0ffh
;	in the 'a' register if character
;	is to be ignored.	otherwise it
;	returns with the ascii character
;	in the 'a' register.	it is assumed
;	that a status test has been done before
;	calling this routine to determine that
;	a baudot character is available.
;	
;	equates
;	
bell	equ	007h	; ascii bell
ltrs	equ	01fh	; baudot 'ltrs'
space	equ	004h	; baudot 'space'
figs	equ	01bh	; baudot 'figs'
apos	equ	027h	; apostrophe
cntc	equ	003h	; control c
larw	equ	05fh	; left arrow
aspc	equ	020h	; ascii space
atsn	equ	040h	; "at" sign
cntv	equ	016h	; control v
cnto	equ	00fh	; control o
rubt	equ	07fh	; rubout
cnte	equ	005h	; control e
pct	equ	025h	; percent sign
eql	equ	03dh	; equals sign
grth	equ	03eh	; greater-than sign
cnth	equ	008h	; control h
leth	equ	03ch	; less-than sign
cntb	equ	002h	; control b
star	equ	02ah	; asterisk
cnti	equ	009h	; control i
cntz	equ	01ah	; control z
cnta	equ	001h	; control a
cntx	equ	018h	; control x
uarw	equ	05eh	; up-arrow
plus	equ	02bh	; plus sign
resl	equ	05ch	; reverse slant
lbkt	equ	05bh	; left bracket
rbkt	equ	05dh	; right bracket
escp	equ	01bh	; escape key
almd	equ	07dh	; alternate mode key
;	
;	program starts here
;	
rttyread:
;	
ascrcv:	push	h	; save hl registers
	lhld	casave	; get present case
;	
start:	call	ri	; get baudot character
	ani	01fh	; baudot characters only
	cpi	figs
	jz	ucase
	cpi	ltrs
	jz	lcase
	ora	a	; baudot 'blank' ?
	jz	tcase
	cpi	space
	jnz	start1
	lxi	h,ltable;space gives lower case
;	
start1:	shld	casave	; save present case
	add	l	; add char. to table adrs.
	mov	l,a	; l points to ascii char.
	mov	a,h	; get h for correction
	aci	000h	; see if correction needed
	mov	h,a	; h is correct now
	xra	a	; put zero in a reg.
	sta	flagr	; clear ltrs-figs last flag
	mov	a,m	; get ascii equivalent
	pop	h	; restore hl registers
	ora	a	; is it printable?
	rnz		; yes, valid char. return
	cma		; make 0ffh 'ignore flag'
	ret		; return to calling program
;	
lcase:	lxi	h,ltable ;lower case table address
	jmp	setflg	; set ltrs last flag, exit
;	
ucase:	lxi	h,utable ;upper case table address
	jmp	setflg	; set figs last flag, exit
;	
tcase:	lda	flagr	; get shift reminder flag
	cpi	figs
	jnz	fcase
	lxi	h,ttable ;third case table address
	jmp	clrflg
;	
fcase:	cpi	ltrs	; check for ltrs last
	jnz	clrflg
	lxi	h,ftable;fourth case table address
;	
clrflg:	xra	a	; ready to clear flag
;	
setflg:	sta	flagr	; ltrs-figs last flag
	shld	casave	; save present case
	mvi	a,0ffh	; 'ignore flag'
	pop	h	; restore hl regs.
	ret		; return to calling program
;	
;	this is the lower-case table.
;	
ltable:	db	0,'e',lf,'a siu',cr,'drjnfck'
	db	'tzlwhypqobg',figs,'mxv'
;	
;	this is the upper-case table.
;	
utable:	db	0,'3',lf,'- ',bell,'87',cr,'$4'
	db	apos,',!:(5")2#6019?&',figs,'./;'
;	
;	this is the third-case table.
;	it is used for special characters.
;	the first column in the table
;	the ascii character equivalent.
;	the second column is the special
;	case baudot character from which
;	the conversion is made.
;	
ttable:	db	0,cntc,lf	; bl	3	lf
	db	larw,aspc,atsn	; -	sp	bell
	db	cntv,cnto,cr	; 8	7	cr
	db	almd,cnte,pct	; $	4	'
	db	rubt,eql,escp	; ,	!	:
	db	leth,cnth,lbkt	; (	5	"
	db	grth,cntb,star	; )	2	#
	db	cnti,cntz,cnta	; 6	0	1
	db	cntx,uarw,plus	; 9	?	&
	db	0,rbkt,resl	; fig   .	/
	db	0		; ;
;	
;	this is the fourth case table
;	for lower-case alpha characters.
;	
ftable:	db	000h,065h,lf,061h,aspc,073h
	db	069h,075h,cr,064h,072h,06ah
	db	06eh,066h,063h,06bh,074h,07ah
	db	06ch,077h,068h,079h,070h,071h
	db	06fh,062h,067h,figs,06dh,078h
	db	076h
;	
;	temporary storage area
;	
casave:	dw	ltable	; present case reminder
flagr:	db	0	; figs/ltrs last flag
;
$eject
;	
;	ascxmt(4) rev. 2
;	by keith petersen, w8sdz.	10/29/78.
;	
;	this program is for transmitting ascii
;	through a baudot medium use a special
;	format of figures blank (x) - where (x)
;	is a special baudot character used to
;	convey the ascii equivalent character to the
;	receiving end where it is then converted back
;	to ascii again.	lower case alpha characters
;	are transmitted via a 4th case, ltrs blank (x).
;	
;	equates
;	
ltrsx	equ	01fh
figsx	equ	01bh
crx	equ	048h
lfx	equ	042h
altmod	equ	07dh	; alternate mode key
;	
;	program starts here.
;	call with ascii character in accumulator.
;	
;	
rttyxmit:
startx:	push	psw	; save registers used
	push	b
	push	h
;	
baudot:	mvi	c,0	; set up counter
	ani	07fh	; strip parity bit
	cpi	altmod	; alternate mode key?
	jnz	baud2	; no, ignore next routine
	mvi	a,09h	; baudot '$'
	jmp	baud3
;	
baud2:	cpi	07bh	; above l.c. 'z' ?
	jnc	tty0	; if so, ignore it
	lxi	h,btable;point to table start
	add	l	; add character value
	mov	l,a	; l points to baudot char.
	mov	a,h	; get h for correction
	aci	000h	; correct if necessary
	mov	h,a	; h is now correct
	mov	a,m	; get baudot character
	ora	a	; is it a non-printing char?
	jz	tty0	; if so ignore it
	cpi	crx	; is it a carriage return?
	jz	ttyout-1;yes, handle without case test
	cpi	lfx	; is it a line feed?
	jnz	baud3	; no, ignore next routine
	mvi	a,ltrsx	
	push	psw	; save it on stack
	mvi	a,lfx
	lxi	h,scase	; point to present case reg.
	mvi	m,040h	; set it to lower case
	jmp	ttyout-2 ;send lf and ltrs
;	
baud3:	push	psw	; save character
	ani	0c0h	; strip all but case bits
	lxi	h,scase	; point to present case reg.
	cmp	m	; same case as previous character?
	jz	ttyout	; yes no change necessary
	mov	m,a	; update case register
	ora	a
	jz	tcasex
	cpi	080h
	jz	ucasex
	cpi	0c0h
	jnz	lcassex
;	
fcasex:	xra	a	; get a baudot 'blank'
	push	psw	; save it on the stack
	inr	c	; increment character count
;	
lcassex:mvi	a,ltrsx
	jmp	ttyout-2
;	
tcasex:	xra	a	; get a baudot 'blank'
	push	psw	; save it on the stack
	inr	c	; increment character count
;	
ucasex:	mvi	a,figsx
	inr	c	; increment character count
	push	psw	; save it on the stack
;	
ttyout:	pop	psw	; get character
	ori	0e0h	; make upper 3 bits marking
	call	po	; send to rtty output
	dcr	c	; more to send?
	jp	ttyout	; yes, continue sending
;	
tty0:	pop	h	; restore registers
	pop	b
	pop	psw
	ret		; return to calling program
;	
btable:	db	05fh,017h,013h	; nul    soh    stx
	db	001h,000h,00ah	; etx    eot    enq
	db	000h,085h,010h	; ack    bel    bs
	db	015h,042h,0c2h	; ht     lf	vt
	db	002h,048h,000h	; ff     cr	so
	db	007h,000h,000h	; si     dle    dc1
	db	000h,000h,000h	; dc2    dc3    dc4
	db	000h,006h,000h	; nak    syn    etb
	db	018h,000h,016h	; can    em	sub
	db	00eh,000h,000h	; esc    fs	gs
	db	000h,000h,044h	; rs     us	space
	db	08dh,091h,094h	; !      "	#
	db	089h,00bh,09ah	; $      pct    &
	db	08bh,08fh,092h	; '	(	)
	db	014h,01ah,08ch	; as	pl	,
	db	083h,09ch,09dh	; -	.	/
	db	096h,097h,093h	; 0	1	2
	db	081h,08ah,090h	; 3	4	5
	db	095h,087h,086h	; 6	7	8
	db	098h,08eh,09eh	; 9	:	;
	db	00fh,00dh,012h	; lt	eq	gt
	db	099h,005h,043h	; ?	at	a
	db	059h,04eh,049h	; b	c	d
	db	041h,04dh,05ah	; e	f	g
	db	054h,046h,04bh	; h	i	j
	db	04fh,052h,05ch	; k	l	m
	db	04ch,058h,056h	; n	o	p
	db	057h,04ah,045h	; q	r	s
	db	050h,047h,05eh	; t	u	v
	db	053h,05dh,055h	; w	x	y
	db	051h,011h,01dh	; z	lb	ls
	db	01ch,019h,003h	; rb	ua	la
;	
;	these are the lower case alpha characters
;	
	db	000h,0c3h,0d9h	; '	a	b
	db	0ceh,0c9h,0c1h	; c	d	e
	db	0cdh,0dah,0d4h	; f	g	h
	db	0c6h,0cbh,0cfh	; i	j	k
	db	0d2h,0dch,0cch	; l	m	n
	db	0d8h,0d6h,0d7h	; o	p	q
	db	0cah,0c5h,0d0h	; r	s	t
	db	0c7h,0deh,0d3h	; u	v	w
	db	0ddh,0d5h,0d1h	; x	y	z
;	
;	temporary storage area
;	
scase:	db	40h	; present case register
;	
;
gfn:	call	wccr
	lxi	h,gfns1	; print 'filename? '
	call	wcs
	call	rcs	; read response into ibuf
	lxi	h,fcb+fn; blank fill fn and ft fields
	mvi	c,00bh
gfn1:	mvi	m,' '
	inx	h
	dcr	c
	jnz	gfn1
	lxi	h,ibuf	; point to input buffer
	lxi	d,fcb+fn; scan off fn field
	mvi	c,009h
gfn2:	mov	a,m	; fetch nsext char from ibuf
	inx	h
	cpi	061h	; if lc, convert to uc
	jc	$+5
	sui	spt
	cpi	cr	; jump if end of line
	jz	gfn5
	cpi	'.'	; jump if end of name
	jz	gfn3
	stax	d	; else store char in fn field
	inx	d
	dcr	c	; loop if 8 or less chars so far
	jnz	gfn2
	jmp	gfn6	; else take error sexit
gfn3:	lxi	d,fcb+ft; scan off ft field
	mvi	c,004h
gfn4:	mov	a,m	; fetch nsext char from ibuf
	inx	h
	cpi	061h	; if lc, convert to uc
	jc	$+5
	sui	spt
	cpi	cr	; jump if end of line
	jz	gfn5
	stax	d	; else store char in ft field
	inx	d
	dcr	c	; loop if 3 or less chars so far
	jnz	gfn4
	jmp	gfn6	; else take error sexit
gfn5:	stc		; clear error flag and return
	cmc
	ret
gfn6:	stc		; set error flag and return
	ret
;
gfns1:	db	'filename? ',0
;
;	syres - system reset
;
syres:	mvi	c,sycpm
	jmp	bdos
;
;	ci - console input
;
ci:	push	h	;
	push	b	;
	mvi	c,cicpm
	call	bdos
	pop	b	;
	pop	h	;
	ret
;
;	co - console output
;
co:	push	b
	push	psw	;
	push	h	;
	mov	e,a	; mov because of bdos call
	cpi	lf	; comparisen for status line
	jnz	co1	;
	lda	lfcnt	;
	inr	a	;
	sta	lfcnt	; increase lf counter
co1:	mvi	c,cocpm
	call	bdos
	lda	lfcnt	; get lf counter
	cpi	23	;
	cz	putstln	; put status line
	pop	h	;
	pop	psw	;
	pop	b	;
	ret
;
;	ri - reader input
;
ri:	push	h	;
	mvi	c,ricpm
	call	bdos
	pop	h	;
	ret
;
;	po - punch output
;
po:	push	h	;
	push	b	;
	mov	e,a	; mov because of bdos call
	mvi	c,pocpm
	call	bdos
	pop	b	;
	pop	h	;
	ret
;
;	lo - list output
;
lo:	push	h	;
	push	b	;
	mov	e,a	; mov because of bdos call
	mvi	c,locpm
	call	bdos
	pop	b	;
	pop	h	;
	ret
;
;	setio - set io byte
;
setio:	mvi	c,ioset
	call	bdos
	ret
;
;	cssts - console status
;
cssts:	push	h	;
	push	b	;
	mvi	c,cscpm
	call	bdos
	pop	b	;
	pop	h	;
	ret
;
;	open - open disk file
;
open:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,offc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;
;	read - read record from disk file
;
read:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,rrfc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;
;	close - close disk file
;
close:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,cffc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;
;	delt - delete disk file
;
delt:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,dffc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;
;	write - write record to disk
;
write:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,wrfc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;
;	make - make new disk file
;
make:	push	h
	push	d
	push	b
	lxi	d,fcb
	mvi	c,mffc
	call	bdos
	pop	b
	pop	d
	pop	h
	ret
;*
;*	uarst - uart receiver status
;*
uarst:	in	ttc	;* get status
	ani	rbrdy	;* demaskeer reader buffer
	ret		;*
;*
;*	uatst - uart transmitter status
;*
uatst:	in	ttc	;* get status
	ani	tbrdy	;* demaskeer transmitter buffer
	ret		;*
;*
;*	delay: delay routine base 10mS,  multiplier in d
;*
delay:	push	b	;*
loop1:	mov	a,d	;*
	cpi	000h	;*
	jz	uit	;*
	dcr	d	;*
	lxi	b,0100h	;* ca 10mS
loop2:	dcx	b	;*
	mov	a,b	;*
	cpi	0ffh	;*
	jnz	loop2	;*
	jmp	loop1	;*
uit:	pop	b	;*
	ret		;*
;
; putstln: put status line
;
putstln:xra	a	;
	sta	lfcnt	; reset lfcount
	call	wccr	; start with cr
	lxi	h,msgst	; "-x status:"
	call	wcs	; put on screen
	lda	trfl	; get transmitting flag
	ora	a	;
	jnz	put2	; jmp for transm.
	lxi	h,msgrc	; "receiving"
	call	wcs	; put on screen
	lda	memfl	;
	ora	a	;
	jnz	put1	;
	lxi	h,msgcg	;
	call	wcs	; put 'type control-G for help'
	jmp	put3	; jmp for baudrate
put1:	lxi	h,msgtxt; "text into memory.act.size"
	call	wcs	; put on screen
	lhld	size	; get memory size
	mov	a,h	;
	call	cbyte	;
	mov	a,l	;
	call	cbyte	;
	mvi	a,048h	;
	call	co	; add 'H'
	mvi	a,spt	;
	call	co	; and ' '
	jmp	put3	;
put2:	lxi	h,msgtr	; "transmitting file"
	call	wcs	; put on screen
	call	ptfnm	;
	mvi	a,spt	; add ' '
	call	co	;
put3:	call	msgbr	; "baudrate set to xx bd"
	ret		; end putstln
;
; cbyte display byte in hex
;
cbyte:	push	psw	;
	rrc		;
	rrc		;
	rrc		;
	rrc		;
	call	cbyte1	;
	pop	psw	;
cbyte1:	ani	00fh	; strip 4 bits
	adi	090h	; correction for A...F
	daa		;
	aci	040h	;
	daa		;
	jmp	co	;
;
; ptfmn: put file name
;
ptfnm:	lxi	h,fcb+fn;
	mvi	b,00ch	; set counter
pt1:	mov	a,m	; get byte
	call	co	; put it on screen
	inx	h	; increment pointer
	dcr	b	; decrement counter
	jnz	pt1	;
	ret		; file name complete
;
msgst:	db	'-x status: ',0
msgrc:	db	'receiving, ',0
msgcg:	db	'type control-G for help, ',0
msgtxt:	db	'text into memory, act.size ',0
msgtr:	db	'transmitting file: ',0
;*
;*	brset - set baudrate for 46, 50 or 75 baud
;*
brset:	lda	brtgl	;* load baud rate togle flag
	inr	a	;*
	cpi	006h	;*
	jnz	brset0	;*
	xra	a	;*
brset0:	sta	brtgl	;*
	call	putstln	;* update status line	
	ret		;* end brset
;*
;* msgbr: message baudrate
;*
msgbr:	lda	brtgl	;*
	cpi	000h	;*
	jz	brset1	;*
	cpi	001h	;*
	jz	brset2	;*
	cpi	002h	;*
	jz	brset3	;*
	cpi	003h	;*
	jz	brset4	;*
	cpi	004h	;*
	jz	brset5	;*
	cpi	005h	;*
	jz	brset6	;*
brset1:	mvi	a,030h	;* deel door 13 voor 46 baud
	out	brdiv	;*
	lxi	h,msg46	;*
	jmp	brset7	;*
brset2:	mvi	a,040h	;* deel door 12 voor 50 baud
	out	brdiv	;*
	lxi	h,msg50	;*
	jmp	brset7	;*
brset3:	mvi	a,050h	;* deel door 11 voor 55 baud
	out	brdiv	;*
	lxi	h,msg55	;*
	jmp	brset7	;*
brset4:	mvi	a,060h	;* deel door 10 voor 60 baud
	out	brdiv	;*
	lxi	h,msg60	;*
	jmp	brset7	;*
brset5:	mvi	a,070h	;* deel door 9 voor 66 baud
	out	brdiv	;*
	lxi	h,msg66	;*
	jmp	brset7	;*
brset6:	mvi	a,080h	;* deel door 8 voor 75 baud
	out	brdiv	;*
	lxi	h,msg75	;*
brset7:	call	wcs	;* print boodschap
	ret		;*
;*
msg46:	db	'baudrate set to 46 bd x-',cr,lf,lf,0 ;*
msg50:	db	'baudrate set to 50 bd x-',cr,lf,lf,0 ;*
msg55:	db	'baudrate set to 55 bd x-',cr,lf,lf,0 ;*
msg60:	db	'baudrate set to 60 bd x-',cr,lf,lf,0 ;*
msg66:	db	'baudrate set to 66 bd x-',cr,lf,lf,0 ;*
msg75:	db	'baudrate set to 75 bd x-',cr,lf,lf,0 ;*
;
;	data area
;
trfl:	ds	1	; transmit flag
lfcnt:	ds	1	; line feed counter
brtgl:	ds	1	; baud rate control byte
inch:	ds	1	; input char buffer
outch:	ds	1	; output char buffer
stack:	ds	128	; local stack
ibuf:	ds	256	;
;
;	tsext buffer
;
prflag:	ds	1	; printflag
memfl:	ds	1	; tsext save flag
ptr:	ds	2	; tsext buffer pointer
size:	ds	2	; tsext buffer size
tbuf	equ	$	; start of tsext buffer
;
	end
