	title	MALLARD BASIC Reference Program
	name	('BASREF')
	maclib	base80

; Program BASREF

; Copyright (C)	W.Cirsovius
; 		Hohe Weide 44
;		D-2000 Hamburg 20
;		Tel.:040/4223247

; Revised version 5.0, January 1989

; Working on CP/M 2.2 and later versions, requires Z80 CPU

; This program types a reference list of all variables and lines
; accessed by any BASIC program for the JOYCE PCW 8256

; The printing will be performed in upper case only, although
; variables names in BASIC may be in lower case.

; How to run this program:

; Type BASREF {'drive:file.ext'} to call the reference program

; Where	'drive' is the file resident drive
;	'file'  is the name of the file
;	'ext'   is the extension of the file

; You may omit	- 'drive' , default is the current drive
;		- 'ext'   , default is extension 'BAS'

; If no parameter is given program asks for filename.

; Program first checks the presence of the file on disk. If the
; file is not found, the following error message will be printed
; and BASREF restarts:

;		File not on disk

; Now the BASIC tokened program file.ext will be loaded. The
; following error messages may be printed and the program exits
; to CP/M:

;	File ASCII coded : The file is stored with option 'a'

;	File protected   : The file is stored with option 'p'

; Program first asks for those items to print
; These may be :V  Variables
;		A  Arrays
;		S  Strings
;		I  Indexed strings
;		L  Lines
;		N  Numeric variables V + A
;		T  Text  variables   S + I
;		C  Combined V + A + S + I + L
;		F  Input another file
;		E  To exit BASREF.COM

; If you typed V,A,S,I or L program asks if you want to print
; a single item. Type Y if so, otherwise N

; If you selects the single item output, type the item to be
; searched for closed by a RETURN.

; If you selects not the single item output, program continues
; as follows:

; The program then asks if you want to print. If so, type Y
; otherwise type N

; At last program asks for long or short output
;  L  'Long' prints all lines on which variables
;     will be accessed
;  S  'Short' prints only the variables within
;     the BASIC program

; Now program runs. But you may break it by pressing any key.

; If an error occurs while running, program prints a message
; and returns to CP/M

; ===== Constants =====

upmsb	equ	11011111b	; UPPER mask

conbit	equ	00000001b	; The console
prnbit	equ	00000010b	; The printer

noindx	equ	0
index	equ	1

item	equ	2

zero	equ	0
one	equ	1
two	equ	2

endlin	equ	0
variab	equ	1
strvar	equ	2
linec	equ	3

; ===== Tokens =====

eoltok	equ	000h		; End of line
deltok	equ	001h		; End of statement
vartk1	equ	002h		; Integer variable
varstk	equ	003h		; String variable
vartk2	equ	004h		; Single precision variable
vartk3	equ	005h		; Double precision variable
vartk4	equ	006h		; Simple variable
sintok	equ	018h		; Single byte constant
hextk1	equ	019h		; Two byte integer constant
hextk2	equ	01ah		; Two byte octal constant
hextk3	equ	01bh		; Two byte hex constant
lintok	equ	01dh		; Two byte line number
flptok	equ	01eh		; Single precision constant
dobtok	equ	01fh		; Double precision constant
blntok	equ	' '		; Blank token
strtok	equ	022h		; Quote for immediate string
indtok	equ	'('		; Index token
gostok	equ	09bh		; GOSUB token
gotok	equ	09ch		; GOTO token
ontok	equ	0adh		; ON token
remtku	equ	0b6h		; Special REM token
remtok	equ	0b9h		; REM token
restok	equ	0bdh		; RESTORE token
resutk	equ	0beh		; RESUME token
thntok	equ	0e2h		; THEN token
funtok	equ	0ffh		; Funktion prefix token

; From library BASELIB

	extrn	open,string,dskget,jmptab
	extrn	getlin,getver,vernum,parse

	extrn	rdbfp,dskget,tstdig,defio,jmptab,uppcon,@caljp
	extrn	break,string,uppin,comout,decout,subhd2,upincl

; ===== To the rest of the world =====

	entry	$memry

; ----------  Main part of the program ----------
; ----------- Start at absolute 0100h  -----------

	sub	a		; Verify Z80 CPU
	jp	po,Z80ok	; Got it
	ld	de,no8080
	ld	c,.string
	call	BDOS		; Tell invalid CPU
	jp	OS		; Give up
Z80ok:
	ld	sp,(TPAtop)	; Get top of memory
				; As new stack
	ld	a,30h
	ld	(vernum),a
	call	getver		; We expect at least CP/M 3.0
	jr	c,illos
gotfile:
	ld	a,(fcbnam)	; Test name of file
	cp	' '
	jr	nz,tstext	; Yeap
;
; Empty file, input new one and parse it
;
restart:
	ld	de,infilm
	call	string		; Ask for name of file
	ld	de,filnam
	call	getlin		; Read line from keyboard
	call	crlf		; Give new line
	ld	de,parspb
	call	parse		; Parse file
	jr	nc,gotfile	; Ok
	ld	de,illfil	; Invalid file name
newtry:
	call	string		; Tell error
	jr	restart		; Retry
tstext:
	ld	hl,fcbext
	ld	a,(hl)
	cp	' '		; Test type defined
	ld	bc,extdef
	call	z,setdef	; If not, set default
	ld	de,fcb
	call	open		; Open file
	jp	nc,entry
nofil:
	ld	de,opnmsg
	jr	newtry		; Tell error, restart
illos:
	ld	de,illosm
	jp	chrsst		; Tell invalid OS and exit
entry:
	ld	de,copmes
	call	string		; Tell copyright
	ld	bc,tabjoy
	ld	hl,protcd
	call	setdef
	call	gtleg		; Get legal file
	jr	c,restart	; Exit on error
	call	gtlng		; Fetch length of file
	ld	hl,($memry)
	ld	(basadr),hl	; Set BASIC start
;
; Load BASIC file into memory
;
gtfil:
	call	dskget		; Get byte
	jr	c,eofile
	ld	(hl),a
	inc	hl
	dec	bc
	ld	a,b
	or	c
	jr	nz,gtfil
eofile:
	ld	(treest),hl	; Set start of tree
	dec	hl
	ld	(toktop),hl	; Save token top
;
; Give messages and ask for options
;
start:
	xor	a
	ld	(runflg),a	; Reset run flag
	ld	(pcod),a	; Reset access flag
	ld	(mode),a	; Set default mode
	ld	a,conbit
	ld	(iocod),a
	ld	de,outms
	call	get		; Ask for item
	ld	(val),a		; Save character
	call	jmptab		; Test end mode F or E
	dw	tab0
	ld	a,(val)		; Fetch character
	call	fintab		; Get routine
	dw	tab1
	jr	nz,start	; Restart if not in table
	push	hl
	ld	a,(val)		; Fetch character
	call	jmptab		; Test single mode
	dw	tab1a
	jp	more
;
; Select parameters for single search
;
sinv:
	ld	hl,noindx*256+variab
	jr	single
sina:
	ld	hl,index*256+variab
	jr	single
sins:
	ld	hl,noindx*256+strvar
	jr	single
sini:
	ld	hl,index*256+strvar
	jr	single
sinl:
	ld	hl,linec*256+linec
;
; Test single mode req'd and perform if so
;
single:
	push	hl		; Save code
	ld	de,snglms
	call	get		; Ask for it
	pop	de
	cp	'Y'		; Test it
	jr	nz,more		; No, do all
	pop	hl		; Fix stack
	push	de		; Save code
	ld	de,itmes
	call	string		; Get item
	ld	de,valcnt	; Init pointer
	ld	a,16
	ld	(de),a		; Init max count
	call	getlin		; Get it
	ld	d,0
	ld	a,(valcnt+1)	; Get real count
	ld	e,a
	ld	hl,val-1
	add	hl,de		; Point to last character
	ld	a,(hl)
	or	msb		; Set MSB
	ld	(hl),a
	ld	de,val
	call	upson		; Convert to upper case
	pop	de		; Get code
	ld	a,e
	cp	linec		; Test line
	jr	z,fixnum
dosing:
	call	inifmt		; Init formatter
	call	crlf
	ld	hl,(basadr)	; Get start of BASIC
	call	elesng		; Print
	call	crlfc
	jp	start
;
; Fix line number to five digits
;
fixnum:
	ld	a,(valcnt+1)	; Get count
	sub	6		; Test max
	jr	nc,numovr
	neg			; Get count
	push	de
	ld	de,val
	call	nulset		; Insert zeroes
	ld	de,val
	ld	hl,linsav
	call	move		; Unpack
	pop	de
	jr	dosing
numovr:
	ld	de,ovfmes
	call	string		; Tell error
	jp	start
more:
	ld	de,inmes
	call	get		; Ask for print device
	cp	'Y'
	jr	nz,shln
	ld	a,prnbit
	ld	(iocod),a
shln:
	ld	de,slmes
	call	get		; Ask for long/short
	cp	'S'
	jr	nz,run
	ld	a,one
	ld	(mode),a	; Set short mode
run:
	call	inifmt		; Init formatter
	pop	hl		; Get address
	ld	de,start
	push	de		; Set return address
	jp	(hl)		; Jump
;
; Call print options
;
pass1d:
	call	pass1		; -> Variables
	ret
passn:
	call	pass1		; -> Numeric variables
pass2d:
	call	pass2		; -> Arrays
	ret
pass3d:
	call	pass3		; -> Strings
	ret
passs:
	call	pass3		; -> String variables
pass4d:
	call	pass4		; -> Indexed strings
	ret
passc:
	call	pass1		; -> Combination
	call	pass2
	call	pass3
	call	pass4
pass5d:
	call	pass5		; -> Lines
	ret
;
; Pass 1 : Print variables
;
pass1:
	ld	hl,varms
	ld	e,variab	; Set parameters
	ld	d,noindx
	jr	pascom		; Start
;
; Pass 2 : Arrays
;
pass2:
	ld	hl,arrms
	ld	e,variab	; Set parameters
	ld	d,index
	jr	pascom		; Start
;
; Pass 3 : Strings
;
pass3:
	ld	hl,strms
	ld	e,strvar	; Set parameters
	ld	d,noindx
	jr	pascom		; Start
;
; Pass 4 : Indexed strings
;
pass4:
	ld	hl,inxms
	ld	e,strvar	; Set parameters
	ld	d,index
	jr	pascom		; Start
;
; Pass 5 : Line access
;
pass5:
	ld	hl,linms
	ld	e,linec		; Set parameter
pascom:
	call	dovar		; Start
	ld	a,(mode)	; Test any item
	cp	item
	call	z,crlf		; If so, close line
	ret
;
; End requested
;
endprg:
	ld	de,endmsg
	jr	chrsst		; Tell end of program
crash:				; System crash on erroneous token found
	ld	de,crshms
chrsst:
	call	string		; Tell error
	jp	OS

;
; Subroutine package for binary tree manipulation
; DOVAR - INSERT - SEARCH - NAMPRN (PUTREE)
;
; Look for all variables A .. Z and lines
;
dovar:
	push	hl		; Push message
	push	de		; And code
	ld	de,srcmes
	call	string		; Tell searching
	ex	de,hl
	inc	de
	inc	de
	call	string		; And item
	pop	de
	ld	a,(iocod)
	ld	(defio),a	; Set I/O device
	ld	hl,zero
	ld	(root),hl	; Init the tree
	ld	hl,(treest)
	ld	(first),hl
	ld	hl,(basadr)	; Init start of tokens
endova:
	push	de
	push	hl
	call	finnxt		; Find lines
	pop	hl
	pop	de
	jr	c,dovar2	; That's all, now print it
	inc	hl
	inc	hl
	inc	hl
	inc	hl
dovlop:
	call	line		; Get token codes
	ld	a,c
	cp	endlin		; Test end of line
	jr	z,endova
	cp	e		; Requested ?
	jr	nz,dovlop
	cp	linec		; Test line
	jr	z,newitm	; Skip index
	call	typ		; Same mode ?
	jr	c,dovlop	; No, skip
newitm:
	push	hl
	push	de
	ld	bc,varsto
	ld	hl,(root)
	ld	de,root
	call	search		; Can we insert string
	call	c,insert	; Insert if yes
	pop	de
	pop	hl
	jr	dovlop
;
; We checked the program this time, let's print now
;
dovar2:
	ld	hl,(root)	; Init node
	ld	a,l
	or	h		; Test zero
	jp	nz,namprn	; No, print
	pop	de		; Fix stack
	ld	de,nonmes
	jp	string		; Tell no item found
;
; Insert a string into tree
;
insert:
	ld	hl,(first)
	ld	a,l
	ld	(de),a		; Save new pointer
	inc	de
	ld	a,h
	ld	(de),a
	xor	a
	ld	(hl),a
	inc	hl		; Clear new nodes
	ld	(hl),a
	inc	hl
	ld	(hl),a
	inc	hl
	ld	(hl),a
	inc	hl
	ld	de,varsto	; Point to string
	ex	de,hl		; Swap source and destination
	call	move		; Unpack
	ex	de,hl
	ld	(first),hl	; Update pointer
	ret
;
; Find room for new string
;
search:
	ld	a,h		; Test empty
	or	l
	scf			; Set empty
	ret	z
	push	bc
	push	hl
	push	de
	inc	hl
	inc	hl
	inc	hl
	inc	hl
nextc:
	ld	a,(bc)		; Get character
	and	nomsb
	ld	d,a
	ld	a,(hl)
	and	nomsb
	cp	d		; Compare
	jr	nz,tsted	; Aha, not equal
	ld	d,(hl)
	ld	a,(bc)
	inc	bc
	inc	hl
	rlca			; Last character ?
	jr	c,mbsam
	ld	a,d
	rlca			; Second one ended ?
	jr	nc,nextc
notequ:
	pop	de
	pop	hl
	pop	bc
	jr	nc,min
	jr	minhl		; Fix node pointer
tsted:
	pop	de
	pop	hl
	pop	bc
	jp	p,min
minhl:
	inc	hl
	inc	hl		; Fix node pointer
min:
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	dec	hl
	ex	de,hl		; Get next node pointer
	jr	search		; Search on
mbsam:
	ld	a,d		; Test end
	rlca
	jr	nc,notequ
	pop	de
	pop	hl
	pop	bc
	or	a		; Set found flag
	ret
;
; Get substring and find access
;
namprn:	
	ld	(par),de	; Save requested parameters
	pop	de		; Get item message
	call	strfor
	ld	de,(par)
putree:
	push	de
	push	hl
	ld	e,(hl)		; Get node
	inc	hl
	ld	d,(hl)
	inc	hl
	ex	de,hl
	ld	a,h
	or	l		; Test empty node
	call	nz,putree
	ex	de,hl
	inc	hl
	inc	hl
	push	hl
	push	de
	ld	de,val
	call	move		; Get string to be searched for
	ld	hl,(par)	; Get parameters requested
	ex	de,hl
	ld	hl,(basadr)	; Set start of BASIC
	call	elesng		; Print
	call	crlfc
	pop	de
	pop	hl
	dec	hl
	ld	d,(hl)
	dec	hl
	ld	e,(hl)
	ex	de,hl
	ld	a,h
	or	l		; Test empty node
	call	nz,putree
	pop	hl
	pop	de
	ret
;
; Subroutine : LINE
; Decodes a line from tokens
; ENTRY	: HL points to tokens
; EXIT	:  C holds code	: 0 end of line
;			  1 variable
;			  2 string variable
;			  3 line access
;
linenw:
	pop	hl
line:
	call	brkcon		; Test console break
	ld	a,(toktop)
	sub	l		; Test legal area
	ld	a,(toktop+1)
	sbc	a,h
	jp	m,crash		; *** BAD BAD ERROR
	ld	a,(hl)		; Get token
	inc	hl
	push	hl		; Save pointer
	call	jmptab
	dw	tab2
	jr	linenw		; Repeat if not found
;
; Bump pointer
;
skip1:
	ld	c,1		; Load bump value
	jr	skpcom
skip2:
	ld	c,2
	jr	skpcom
skip4:
	ld	c,4
	jr	skpcom
skip8:
	ld	c,8
skpcom:
	ld	a,b
	ld	b,0
	pop	hl		; Get pointer
	add	hl,bc		; Bump
	ld	b,a
	jr	line		; Re-enter de-token
;
; Skip immediate string
;
strskp:
	pop	hl
	ld	a,(strval)	; Get string end
	call	bmptok		; Fix pointer
	jr	line
;
; Set ON flag if lines required
;
onset:
	ld	a,e
	cp	linec		; Test lines
	jp	nz,linenw	; No, skip
	ld	a,one
	jr	onstor
;
; Clear ON flag
;
onrst:
	xor	a
onstor:
	ld	(onflg),a	; Store into flg
	jp	linenw
;
; Skip line if REM found
;
skprem:
	pop	hl
	ld	a,(eolval)	; Get end of line
	call	bmptok		; Fix pointer
	push	hl
;
; End of line found
;
eol:
	pop	hl
	xor	a
	ld	(onflg),a	; Reset ON flag
	ld	c,endlin	; Return '0'
	ret
;
; Number found, maybe ON argument
;
onacc:
	ld	a,(onflg)	; Test ON flag
	cp	two
	jp	m,skip2		; Not requested
	pop	hl
	jp	linret
;
; GOTO or GOSUB Found
;
goacc:
	ld	a,e
	cp	linec		; Lines requested ?
	jr	nz,linac	; No, skip
	ld	a,(onflg)	; Else fix access
	or	a		; Test ON requested
	jr	z,linac		; Don't bump
	inc	a
	ld	(onflg),a
;
; Line access found
;
linac:
	pop	hl
	call	skpblk		; Ignore blanks
	ld	a,(linval)
	cp	(hl)		; Expected token ?
	jp	nz,line		; Ignore if not
	inc	hl		; Skip it
linret:
	push	de
	ld	e,(hl)		; Get line number
	inc	hl
	ld	d,(hl)
	inc	hl
	push	hl
	ex	de,hl
	call	conv16		; Convert to BCD
	ld	de,varsto
	ld	hl,linsav
	call	move		; Unpack
	pop	hl
	pop	de
	ld	c,linec		; Return '3'
	ret
;
; Variable found
;
var:
	ld	c,variab	; Return '1'
	jr	varstr
;
; String found
;
str:
	ld	c,strvar	; Return '2'
varstr:
	pop	hl
	inc	hl
	inc	hl		; Skip two places
	push	de
	ld	de,varsto
	call	move		; Unpack variable name
	call	upset		; Allow only upper case
	pop	de
	ret
;
; Routine BMPTOK bumps pointer till token found
; ENTRY : Accu holds token
;
bmptok:
	ld	c,(hl)		; Get token
	inc	hl
	cp	c		; Compare
	jr	nz,bmptok
	ret
;
; Special line address token found		**** ??????
;
lintst:
	ld	a,e
	cp	linec		; Test line requested
	jp	nz,onacc	; Skip if not
	ld	de,errlms	; Else break
	jp	chrsst
;
; Subroutine : ELEMNT
; Looks for token type to be searched for
; ENTRY	: HL points to token
;	   E holds code : 1 variable
;			  2 string
;			  3 line
;	   D holds mode : 0 simple variable
;			  1 array or index
;	<VAL> holds item to be searched - A..Z
;					- line number
; INTERNAL : <PCOD> Access flag - 0 1st access
;				  1 nth access
;	     <CURLIN> Current line
;
; Decide if short mode
;
elesng:
	ld	a,(mode)	; Get mode
	or	a
	jr	z,elemnt	; It's long
	ld	a,two		; Set access flag
	ld	(mode),a
	jp	tstxxx		; Print item to be searched
elemnt:
	xor	a
	ld	(pcod),a	; Clear access flag
eleol:
	push	de
	push	hl
	call	finnxt		; Find next and current line
	jr	c,pophd		; Exit if no more
	ld	(curlin),hl	; Save current line
	ld	(nxtlin),de	; Save next line
	pop	hl
	pop	de
	xor	a
	ld	(onflg),a	; Reset ON flag
	inc	hl
	inc	hl
	inc	hl
	inc	hl
;
; Here on main loop
;
elego:
	call	line		; Get code of token
	ld	a,c
	cp	endlin		; Test end of line
	jr	z,eleol
	cp	variab		; Test variable
	jr	z,stvarf
	cp	strvar		; Test string
	jr	z,stvarf
	cp	linec		; Test line
	jr	nz,elego
;
; Line access, is it real good ?
;
	cp	e		; Requested ?
	jr	nz,elego	; No
	jr	finlin
;
; String or variable found
;
stvarf:
	cp	e		; Requested ?
	jr	nz,elego
	call	typ		; Test indexed
	jr	c,elego
finlin:
	call	comp		; Compare
	jr	c,elego
fxtprn:							;; ?????
	call	tstfst
	ld	hl,(curlin)	; Get current line
	call	val16		; Print it
	ld	hl,(nxtlin)	; Get new line
	jr	eleol
pophd:
	pop	hl
	pop	de
	ret
;
; Subroutine : FINNXT
; Looks for next and current line
; ENTRY	: HL points to start of current line
; EXIT	: DE holds address of next line
;	  HL holds current line number
; NOTE : If carry set, we found last line
;
finnxt:
	push	hl
	ld	a,(hl)		; Get length of line
	ld	e,a
	inc	hl
	ld	d,(hl)
	or	d		; Test zero
	jr	nz,nxtok
	pop	hl
	scf			; Set end of tokens
	ret
nxtok:
	dec	hl
	add	hl,de		; Get pointer to next line
	ex	de,hl
	pop	hl
	inc	hl
	inc	hl
	ld	a,(hl)		; Get line number
	inc	hl
	ld	h,(hl)
	ld	l,a
	or	a		; Set ok flag
	ret
;
; Test index requested
;
typ:
	call	skpblk		; No leading blanks
	ld	a,d		; Test submode
	or	a
	ld	a,(indxtk)	; Get token
	jr	z,varonl
	cp	(hl)
	jr	nz,typcy
typnc:
	or	a		; Set same type
	ret
varonl:
	cp	(hl)		; Index ?
	jr	nz,typnc
typcy:
	scf			; Set different type
	ret
;
; Skip blanks within tokens
;
skpblk:
	ld	a,(blktok)	; Get token
skpblx:
	cp	(hl)		; Test blank
	ret	nz		; Ignore if not
	inc	hl		; Else bump
	jr	skpblx
;
; Convert name to upper case
;
upset:
	ld	de,varsto	; Point to name
upson:
	push	bc
?up:
	ld	a,(de)		; Get character
	and	msb		; Save MSB
	ld	b,a
	ld	a,(de)
	and	nomsb		; Strip off MSB
	call	uppcon		; Convert to upper case
	or	b		; Don't forget MSB
	ld	(de),a
	inc	de
	rlca
	jr	nc,?up		; Until MSB set
	pop	bc
	ret
;
; Fill BCD with leading zeroes for total of five digits
;
filzer:
	ld	hl,tmpcnt
	ld	a,6
	sub	(hl)		; Get leading zero count
	ld	de,tmpsav
nulset:
	ld	hl,linsav	; Point to real buffer
nullit:
	dec	a
	jr	z,nulend	; Test filled
	ld	(hl),'0'	; Fill with zeroes
	inc	hl
	jr	nullit
nulend:
	ex	de,hl		; Prepare move
;
; Move name of variable from <HL> to <DE>
;
move:
	ld	a,(hl)		; Get character
	ld	(de),a		; Store it
	inc	de
	inc	hl
	rlca			; Test MSB
	jr	nc,move
	ret
;
; Move characters from <BC> to <HL>
; Number of characters on entry in <BC>
;
setdef:
	ld	a,(bc)		; Get character count
	ld	e,a
	inc	bc
setlop:
	ld	a,(bc)		; Now move it
	ld	(hl),a
	inc	bc
	inc	hl
	dec	e
	jr	nz,setlop
	ret
;
; Test first access of printing
;
tstfst:
	ld	a,(pcod)	; Get flag
	or	a
	jp	nz,blnk		; Print blank
	ld	a,one
	ld	(pcod),a
tstxxx:
	push	hl
	push	de
	ld	a,e		; Test line
	cp	linec
	jr	z,linpri
	ld	de,val
	call	prvar		; Print variable
	jr	comli		; Set delimiter
linpri:
	ld	hl,val
	ld	de,linsav
	call	move		; Unpack
	call	pr16		; Then print
comli:
	ld	de,delim
	call	strfor		; Set delimiter
	pop	de
	pop	hl
	ret
;
; Compare two variable names
;
comp:
	push	de
	push	hl
	ld	de,varsto	; Set pointers
	ld	hl,val
compon:
	ld	a,(de)
	cp	(hl)		; Compare
	jr	nz,nocmp
	inc	de
	inc	hl
	rlca			; Test end
	jr	nc,compon
	pop	hl
	pop	de
	or	a
	ret
nocmp:
	pop	hl
	pop	de
	scf
	ret
;
; Convert value of HL to decimal and store it
;
conv16:
	push	hl
	push	de
	ld	de,tmpsav
	call	decout		; Convert to BCD
	dec	hl
	ld	a,(hl)
	or	msb		; Set MSB of last digit
	ld	(hl),a
	ld	de,tmpsav-1
	call	subhd2		; Get digit count
	ld	a,l
	ld	(tmpcnt),a
	call	filzer		; Fill for five digits
	pop	de
	pop	hl
	ret
;
; Init formatter output
;
inifmt:
	ld	a,one		; Set run flag
	ld	(runflg),a
iniint:
	ld	hl,bufpnt
	ld	(curbuf),hl	; Set buffer start
	xor	a
	ld	(bufcnt),a	; Reset character count
	ret
;
; Print unformatted string
;
inidir:
	call	iniint		; Init pointer
strhl:
	ld	a,(hl)
	cp	eot		; Test end of string
	ret	z
	call	comout		; Output it
	inc	hl
	jr	strhl
;
; Print a blank
;
blnk:
	ld	a,' '
;
; MAIN ROUTINE : Format output stream
;
put:
	ld	(chrsav),a	; Save character
	ld	a,(runflg)	; Test mode
	or	a
	ld	a,(chrsav)
	jp	z,comout	; Print unformatted
	cp	' '		; Test control character
	jr	nc,putnor
	ld	a,(bufcnt)
	or	a		; Test count
	ld	a,(chrsav)
	jp	z,comout	; If zero, pass through
	push	hl
	ld	hl,(curbuf)
	ld	(hl),eot	; Close line
	call	inidir		; And print it
	ld	a,(chrsav)
	pop	hl
	jp	comout		; Output control
putnor:
	push	hl
	ld	hl,(curbuf)
	ld	a,(chrsav)
	ld	(hl),a		; Store character
	inc	hl
	ld	(curbuf),hl	; Update all
	ld	a,(bufcnt)
	inc	a
	ld	(bufcnt),a
	cp	80		; Test length
	pop	hl
	ret	c		; End if ok
	push	hl
	ld	hl,(curbuf)
	ld	(hl),msb	; Set marker
finblk:
	dec	hl
	ld	a,(hl)
	cp	' '		; Find last blank
	jr	nz,finblk
	ld	(hl),eot	; Close substring
	push	de
	inc	hl		; Fix pointer
	push	hl
	call	inidir		; And print it
	ld	hl,crlfms
	call	strhl		; Close line
	ld	de,bufpnt	; Init pointer
	ld	a,(mode)	; Test long/short
	or	a
	jr	nz,fixdwn
	ld	hl,bufpnt
	ld	a,(hl)
	cp	' '		; Test first line
	jr	nz,nxtc
bmpblk:
	ld	a,(hl)
	cp	' '		; Find first non blank
	jr	nz,okchar
	inc	hl
	jr	bmpblk
nxtc:
	ld	a,(hl)
	ld	(hl),' '	; Blank entry
	inc	hl
	cp	':'		; Find delimiter
	jr	nz,nxtc
okchar:
	ex	de,hl
	pop	hl
movdwn:
	call	move		; Get substring
	ex	de,hl
	dec	hl
	ld	(curbuf),hl	; Set pointer
	ld	de,bufpnt
	or	a
	sbc	hl,de		; Get count
	ld	a,l
	ld	(bufcnt),a	; Save it
	pop	de
	pop	hl
	ret
;
; Test end of new item
;
fixdwn:
	pop	hl
	ld	a,(hl)
	cp	':'		; Test delimiter
	jr	nz,movdwn
	inc	hl		; Skip
	jr	movdwn
;
; Check legal file parameters
; Part 1 : Test ASCII
;
gtleg:
	ld	a,reclng
	ld	(rdbfp),a	; Force read if new file selected
;
	call	dskget		; Get first character
	call	tstdig		; Test 0 .. 9 (Line number)
	ld	de,ascmes
	jr	nc,gtill
;
; Part 2 : Test protection
;
	ld	a,(protcd)
	call	fixbuf
	rra
	ret	nc
	ld	de,protms
gtill:
	call	string		; Tell error
	scf
	ret
;
; Get length of file
; EXIT : BC holds length
;
gtlng:
	ld	a,(leng)
	call	fixbuf
	ld	c,a
	inc	hl
	ld	b,(hl)
	ld	a,(pntfix)
	ld	(rdbfp),a	; Update pointer to start
	ret
;
; Fix relative pointer to absolute
; ENTRY : Accu holds relative pointer
; EXIT  : Accu holds byte of absolute pointer
;
fixbuf:
	add	a,dma		; Add base
	ld	h,0		; Clear high end
	ld	a,(hl)		; Get code
	ret
;
; Close line if any item printed
;
crlfc:
	ld	a,(pcod)	; Test print
	or	a
	ret	z		; Immediate return if no print
;
; Close line
;
crlf:
	push	de
	ld	de,crlfms
	call	strfor		; Close
	pop	de
	ret
;
; Routine prints string till '$' found
;
strfor:
	ld	a,(de)
	cp	eot
	ret	z
	call	put		; Print if not end
	inc	de
	jr	strfor
;
; Routine prints string and gets console input
;
get:
	call	string		; Print string
	call	upincl		; Get answer
	ret
;
; Test console break
;
brkcon:
	call	break		; Test console braek
	ret	nc
	ld	de,brkmes
	call	get		; Tell abort
	ld	sp,(TPAtop)	; Load work stack
	jp	start		; Restart
;
; Print current variable from <DE>
;
prvar:
	ld	a,(de)
	and	nomsb
	call	put		; Print
	ld	a,(de)		; Test end
	inc	de
	rlca
	jr	nc,prvar
	ret
;
; Print 16 bit value from <HL>
;
val16:
	call	conv16		; Convert to BCD
pr16:
	push	de
	ld	de,linsav
skpzer:
	ld	a,(de)
	inc	de
	cp	'0'		; Ignore leading zeroes
	jr	z,skpzer
	dec	de
	call	prvar		; Print
	pop	de
	ret
;
; Find item in table
; ENTRY	: PC points to table
; EXIT	: Zero set if item found
;
fintab:
	pop	hl
	inc	hl
	inc	hl		; Fix PC
	push	hl
	dec	hl
	dec	hl
	jp	@caljp

	dseg

extdef:
	db	exlen
?ex:
	db	'BAS'
exlen	equ	$-?ex
;
; Table of relative pointers
;
; Byte 1 : Protection pointer
; Byte 2 : Length pointer
; Byte 3 : Start of token pointer
; Byte 4 : Line number token
;
tabjoy:
	db	joylen
?joy:
	db	2,3,5,01dh
joylen	equ	$-?joy
;
; Command input table for restart
;
tab0:
	db	tab0l
?t0:
	db	'EF'
tab0l	equ	$-?t0
	dw	restart
	dw	endprg
;
; Command input table
;
tab1:
	db	tab1l
?t1:
	db	'VASILNTC'
tab1l	equ	$-?t1
	dw	passc,passs,passn,pass5d
	dw	pass4d,pass3d,pass2d,pass1d
;
; Command table for single mode
;
tab1a:
	db	tab1al
?t1a:
	db	'VASIL'
tab1al	equ	$-?t1a
	dw	sinl,sini,sins,sina,sinv
tab2:				; Table of tokens
	db	tab2l
?t2:
	db	eoltok,vartk1,vartk2,vartk3,vartk4,varstk
	db	gostok,gotok,restok,resutk,thntok,funtok
	db	strtok,remtku,remtok,ontok,deltok,sintok
	db	hextk1,hextk2,hextk3,lintok,flptok,dobtok
tab2l	equ	$-?t2
	dw	skip8,skip4,onacc,skip2,skip2,skip2
	dw	skip1,onrst,onset,skprem,skprem,strskp
	dw	skip1,linac,linac,linac,goacc,goacc
	dw	str,var,var,var,var,eol
;
; Special tokens
;
eolval:
	db	eoltok
strval:
	db	strtok
indxtk:
	db	indtok
blktok:
	db	blntok
;
; ===== Messages =====
;
no8080:
	db	cr,lf,'** Program requires Z80 CPU **',cr,lf,eot
srcmes:
	db	cr,lf,'Searching for : ',eot
nonmes:
	db	cr,lf,'** None **',cr,lf,eot
ovfmes:
	db	cr,lf,'** Line number overflow **',cr,lf,eot
snglms:
	db	cr,lf,'Wanna single value (y/n) - default no - ? ',eot
itmes:
	db	cr,lf,'Input item to be searched for : ',eot
errlms:
	db	cr,lf,'*** LINE ADDRESS NOT NUMBER FOUND ***'
	db	cr,lf,lf,'*** BREAK ***',cr,lf,lf,eot
brkmes:
	db	cr,lf,lf,'*** USER ABORTED ***',cr,lf,lf,eot
illosm:
	db	cr,lf,'Requires CP/M 3.x',cr,lf,eot
crshms:
	db	cr,lf,'*** Bad token error ***',cr,lf,lf
	db	'Contact your distributor !!',cr,lf,lf,eot
copmes:
	db	cr,lf,lf,'MALLARD BASIC (JOYCE) reference list program'
	db	cr,lf,'Copyright (C) Werner Cirsovius, Hamburg, Germany',cr,lf
	db	'Revision 5.0, January 1989',cr,lf,lf
	db	'File will be read ...',cr,lf,eot
ascmes:
	db	cr,lf,'File ASCII coded',cr,lf,eot
endmsg:
	db	cr,lf,'*** END OF PROGRAM ***',cr,lf,lf,eot
protms:
	db	cr,lf,'File protected',cr,lf,eot
opnmsg:
	db	cr,lf,'File not on disk',cr,lf,eot
infilm:
	db	cr,lf,'Input filename {default .BAS}:',eot
illfil:
	db	cr,lf,'Illegal filename',cr,lf,eot
inmes:
	db	cr,lf,'Wanna print (y/n) - default no - ? ',eot
slmes:
	db	cr,lf,'Short or long output',cr,lf
	db	'(s/l) - default long - ? ',eot
outms:
	db	cr,lf,'Variables,Arrays,Strings,Indexed strings,Lines,Numeric variables,'
	db	cr,lf,'Text Variables,Combined,File,Exit (V/A/S/I/L/N/T/C/F/E) - ? '
	db	eot
varms:
	db	cr,lf,'Variables',cr,lf,eot
arrms:
	db	cr,lf,'Arrays',cr,lf,eot
strms:
	db	cr,lf,'Strings',cr,lf,eot
inxms:
	db	cr,lf,'Indexed strings',cr,lf,eot
linms:
	db	cr,lf,'Lines'
crlfms:
	db	cr,lf,eot
delim:
	db	' :',eot
parspb:
	dw	filnam+2,fcb
filnam:
	db	14,0
	ds	14
;
; ===== Variable storage =====
;
basadr:
	ds	2		; BASIC start address
treest:
	ds	2		; Start of binary tree
toktop:
	ds	2		; Top of tokened BASIC program
iocod:
	ds	1		; I/O device code
mode:
	ds	1		; Mode word
onflg:
	ds	1		; ON .. GOxxx code
par:
	ds	2		; Holds parameter on tree search
pcod:
	ds	1		; Print flag
curlin:
	ds	2		; Current line #
valcnt:
	ds	2		; Prefix for VAL
val:
	ds	16		; Item to be searched for
nxtlin:
	ds	2		; Next line pointer
varsto:
	ds	16		; Variable name buffer
protcd:
	ds	1		; Protection offset
leng:
	ds	1		; Length offset
pntfix:
	ds	1		; FCB data offset
linval:
	ds	1		; Line number token
linsav:
	ds	5		; BCD line number
tmpsav:
	ds	5		; Temporary pointer
tmpcnt:
	ds	1		; Temporary counter
runflg:
	ds	1		; Run flag for formatter
bufpnt:
	ds	82		; Formatter buffer
bufcnt:
	ds	1		; Buffer counter
curbuf:
	ds	2		; Buffer address
chrsav:
	ds	1		; Character save area
;
; ===== Binary tree storage =====
;
root:
	ds	2		; First node pointer
first:
	ds	2		; Current node pointer
$memry:
	ds	2		; Heap start

	end

