	title	BASIC DATA to COM converter
	name	('DA2COM')
	maclib	base80.lib	

; Program converts BASIC DATA statements (ASCII hex or decimal constants)
; to hex and writes it to a COM file.
; ASCII file option is '-H' if hex data follow, '-C' if hex data with
; checksum follow or '-D' in case of decimal numbers.
;
; Special case '-M' allows mapping of eight bytes to character setting.
; 

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

; All wrongs reserved
;
;		Version 1.0 Oktober 1994

; Call it:
;		DATA2COM -opt[[&]num] ASCII_file{.ext}
;
; 'opt' is
;	H Hex code less checksum
;	C Hex code including checksum
;	D Decimal code
;	M Map character from '[&]num' to end
;
; 'ext' defaults to 'TXT'
; A new file 'ASCII_file.COM' will be created

; Differ the following line types:
;
; (1) 540 DATA FD,21,A0,C0,0A,B7,3E,86,28,02,3E,C6,32,68,C0,4E,23,46,EB,C5,5E,23,56,23,ED,53,B9
;     600 DATA 85,D3,F1,3E,86,D3,F2,FB,C9,F3,3E,82,D3,F2,5E,23,56,3E,86,D3,F2,FB,C9 of above

; (2) 54160 DATA CD, DF, C4, CD, 2C, C4, CD, 04, 4FE
; (2) 50120 DATA 0A,32,7C,C0,4E,23,46,21,250
; (3) 350 DATA 229,213,197,221,229,42,27,192,237,91,25,192,205,29,192
; (4) 70 DATA 0,0,30,102,198,205,118,0,48,48,96,108,198,199,60,0,0,0,60,102,192,193,126,0

; (1) Hex, less checksum	H
; (2) Hex, with checksum	C
; (3) Decimal			D
; (4) Map			M

	entry	$memry

	ext	conout,string
	ext	open,close,creatd,dskred,dskwrt

_SUCC	equ	0
_CRECER	equ	1*2
_CLSERR	equ	2*2
_WRTERR	equ	3*2
_HEXERR	equ	4*2
_HELP	equ	5*2
_ASCERR	equ	6*2
_OPNERR	equ	7*2
_REDERR	equ	8*2
_ERRLIN	equ	9*2
_DECERR	equ	10*2
_INVDAT	equ	11*2
_ILLCRC	equ	12*2

DIGlen	equ	5

Bit1	equ	'X'		; ASCII for bit set
Bit0	equ	'_'		; ASCII for bit reset

eol	equ	'*'		; End of ASCII line

;
; Copy second FCB to main FCB
;
cpyFCB:
	ld	hl,FCB2
	ld	de,FCB
	ld	bc,dirlen
	ldir			; Copy it
	ret
;
; Process option [&]num
;
optMap:
	ld	a,(de)
	cp	'&'		; Test hex
	jr	z,optMHex
	call	decbyt		; Get key number
	jr	stKey
optMHex:
	inc	de
	call	hexbyt
stKey:
	ld	(StartKey),a	; Save it
	ret
;
; Process option
; ENTRY	Reg DE points to option line
; EXIT	Reg HL holds execution address
;
procOpt:
	inc	de		; Skip minus sign
	ld	a,(de)		; Get type character
	ld	bc,TTlen
	ld	hl,TypeTable+TTlen-1
	cpdr			; Find character in table
	inc	de
	jr	nz,IllOpt	; Error if not found
	ld	hl,TypeExec
	add	hl,bc		; Position in table
	add	hl,bc
	ld	c,(hl)		; Fetch address
	inc	hl
	ld	h,(hl)
	ld	l,c
	cp	'M'		; Test character mapping
	jr	z,optMap
	ld	a,(de)
	cp	' '		; Verify end
	ret	z
	dec	de
IllOpt:
	ld	a,_ASCERR
	jr	LastMsg		; Error if not found
;
; ###########################
; ### Program starts here ###
; ###########################
;
DA2COM:
	ld	sp,(TPATOP)	; Get top of memory for stack
	ld	a,(FCBnm2)
	cp	' '		; Verify file name given
	ld	a,_HELP
	jr	z,LastMsg	; Nope, give help
	ld	de,FCBnam
	ld	a,(de)
	cp	'-'		; Verify option there
	ld	a,_HELP
	jr	nz,LastMsg	; Nope, give help
	call	procOpt		; Process option
	push	hl		; Save execution pointer
	call	cpyFCB		; Copy FCB

; +++++++++++++++++++++++++++++++++++++++++++++++++
; +++++ Load DATA file into memory, determine +++++
; +++++ data type and do the conversion       +++++
; +++++++++++++++++++++++++++++++++++++++++++++++++
;
	call	LdFile		; Load file into memory
	ld	de,($HEXDAT)	; Init pointer to ASCII
	pop	hl
	call	jhl		; Do the conversion task
	jp	nc,OS		; Skip writing on option M
;
; ++++++++++++++++++++++++++++++++++++++++++
; +++++ Write binary data to .COM file +++++
; ++++++++++++++++++++++++++++++++++++++++++
;
	call	Rewrite		; Rewrite new file
	ld	hl,(HexDat)	; Init data pointer
WrLoop:
	call	WrData		; Write to disk
	dec	bc
	ld	a,b
	or	c		; Test end
	jr	nz,WrLoop	; Nope
	ld	de,FCB
	call	close		; Close file
Success:
	ld	a,_SUCC
	jr	nc,LastMsg	; Success, tell it
	ld	a,_CLSERR	; Error
;
; ++++++++++++++++++++++++++++++++++++++++++++++
; +++++ Process I/O error or final message +++++
; ++++++++++++++++++++++++++++++++++++++++++++++
;
LastMsg:
	call	ProcMsg		; Process error or normal end
	jp	OS		; Exit to OS
;
; Missing DATA error
;
invDATA:
	ld	a,_INVDAT
	jr	LastMsg		; Do it
;
; Decimal error
;
DecErr:
	ld	a,_DECERR
	jr	LastMsg		; Do it
;
; Hex error
;
HexErr:
	ld	a,_HEXERR
	jr	LastMsg		; Do it
;
; +++++++++++++++++++++++++++++
; +++++ File I/O routines +++++
; +++++++++++++++++++++++++++++
;
; Load file into memory
;
LdFile:
	ld	a,(FCBext)
	cp	' '		; Test extension given
	call	z,defext	; Nope, set default
	call	Reset		; Open file
	call	RdFile		; Read into memory
	ld	(HexDat),de	; Set start of binary
	ld	hl,($HEXDAT)	; Get start of data
	ex	de,hl
	or	a
	sbc	hl,de		; Calculate length
	ld	c,l
	ld	b,h
	ex	de,hl
	ld	a,eof
	cpir			; Find end of file
	dec	hl
	ld	(hl),eol	; Mark end of text
	ret
;
; Read file into memory
;
RdFile:
	ld	de,($memry)	; Init disk buffer
	ld	($HEXDAT),de	; Set for ASCII base address
	ld	hl,0		; Clear record counter
RdNext:
	call	RdData		; Read record
	jr	nc,RdNext
	ld	a,l		; Verify at least one record read
	or	h
	ret	nz
	ld	a,_REDERR
	jr	LastMsg		; Do error on empty file
;
; Read data from disk
; ENTRY	Reg DE points to current data
;	Reg HL holds records read
; EXIT	Reg DE points to next data
;	Reg HL updated
;	Carry flag set on end of file
;
RdData:
	push	de
	ld	de,FCB
	call	dskred		; Read to disk
	pop	de
	ret	c		; End of file
	inc	hl		; Update record count
	push	hl
	ld	hl,DMA
	ld	bc,reclng
	ldir			; Unpack data
	pop	hl
	or	a		; Indicate not end of file
	ret
;
; Write data to disk
; ENTRY	Reg HL points to current data
; EXIT	Reg HL points to next data
;
WrData:
	push	bc
	ld	de,DMA
	ld	bc,reclng
	ldir			; Unpack data
	push	hl
	ld	de,FCB
	call	dskwrt		; Write to disk
	pop	hl
	pop	bc
	ret	nc
	ld	a,_WRTERR
	jp	LastMsg		; Do write error
;
; Open existing file
;
Reset:
	ld	de,FCB
	call	open		; Open file
	ret	nc		; Ok
	ld	a,_OPNERR
	jp	LastMsg		; Error
;
; Rewrite new file
; EXIT	Reg BC holds record count
;
Rewrite:
	call	prepWrt		; Prepare write to file
	push	hl		; Save it
	call	setCOM		; Change extension
	ld	de,FCB
	call	creatd		; Create new one
	pop	bc		; Get back size
	ret	nc		; Success
	ld	a,_CRECER
	jp	LastMsg		; Error
;
; +++++++++++++++++++++++++++++++
; ++++ Set default extensions +++
; +++++++++++++++++++++++++++++++
;
; Set default extension .COM
;
setCOM:
	ld	hl,$COM
	jr	setext		; Set new extension
;
; Set default extension .TXT
;
defext:
	ld	hl,$TXT
setext:
	ld	de,FCBext
	ld	bc,.fext
	ldir			; Unpack extension
	ret
;
; +++++++++++++++++++++++++++++++
; ++++ Prepare disk functions +++
; +++++++++++++++++++++++++++++++
;
; Clear entire disk buffer
;
ClrData:
	push	hl
	ld	d,h		; Copy pointer
	ld	e,l
	inc	de
	ld	bc,reclng-1
	ld	(hl),eof	; Fill remainder of buffer with EOF
	ldir
	pop	hl
	ret
;
; Prepare write to file
; EXIT	Reg HL holds record coount
;
prepWrt:
	call	ClrData		; Clear remainder of disk buffer
	ld	de,reclng-1
	add	hl,de
	ld	de,(HexDat)	; Get base pointer
	or	a
	sbc	hl,de		; Calculate length of data
	ld	(DatLen),hl	; Save length of data
	xor	a
	add	hl,hl		; Divide by record size
	adc	a,a
	ld	l,h
	ld	h,a
	ret
;
; ++++++++++++++++++++++++++++++++++++
; +++++ Base conversion routines +++++
; +++++ Decimal conversion +++++++++++
; ++++++++++++++++++++++++++++++++++++
;
; Increment an ASCII coded number
; ENTRY	Reg pair HL points to LSDigit of numeric string
;	Reg B holds length of number
; EXIT	ASCII number incremented
;
more.asc:
	ld	a,(hl)		; Get character
	cp	'0'		; Test digit
	jr	c,asc.1
	cp	'9'+1
	jr	c,incasc
asc.1:
	ld	(hl),'0'	; Init digit
incasc:
	inc	(hl)		; Bump digit
	ld	a,(hl)
	cp	'9'+1		; Test overflow
	ret	nz		; No
	ld	(hl),'0'	; Init digit
	dec	hl
	djnz	more.asc
	ret
;
; Increment an ASCII coded line number
;
incLine:
	push	bc
	push	hl
	ld	hl,$LINENR	; Point to number
	ld	b,DIGlen	; Set length
	call	incasc		; Increment it
	pop	hl
	pop	bc
	ret
;
; Get decimal byte
; ENTRY	Reg DE points to decimal ASCII buffer
; EXIT	Accu holds byte
;
decbyt:
	push	hl
	ld	hl,DecErr	; Set error routine
	ld	c,0		; Init result
nxtDec:
	call	isDel		; Test end
	jr	c,endDec
	call	decnyb		; Get digit
	ld	b,a
	ld	a,c		; Get old
	add	a,a		; Calculate *10
	add	a,a
	add	a,c
	add	a,a
	add	a,b
	ld	c,a
	jr	nxtDec		; Next digit
endDec:
	ld	a,c		; Get result
	pop	hl
	ret
;
; Get decimal digit
; ENTRY	Reg DE points to decimal ASCII buffer
;	Reg HL holds address of error routine
; EXIT	Accu holds digit
;
decnyb:
	push	hl		; Change return address in case of error
	ld	a,(de)		; Get character
	inc	de
	sub	'0'		; Strip off offset
	ret	c		; Error
	cp	9+1		; Verify decimal
	ret	nc
	pop	hl
	ret
;
; ++++++++++++++++++++++++++++++++
; +++++ Hex conversion +++++++++++
; ++++++++++++++++++++++++++++++++
;
; Get hex byte
; ENTRY	Reg DE points to hex ASCII buffer
;	Reg HL holds address of error routine
; EXIT	Accu holds byte
;
hexbyt:
	push	hl
	ld	hl,HexErr	; Set error routine
	call	hexnyb		; Get hi nybble
	add	a,a		; Shift into right place
	add	a,a
	add	a,a
	add	a,a
	ld	c,a
	call	hexnyb		; Get low nybble
	or	c		; Combine byte
	pop	hl
	ret
;
; Get hex nybble
; ENTRY	Reg DE points to hex ASCII buffer
; EXIT	Accu holds nybble
;
hexnyb:
	push	hl		; Change return address in case of error
	ld	a,(de)		; Get character
	inc	de
	call	isHexdig	; Test valid hex character
	ret	c		; Nope, error
	pop	hl
	ret
;
; +++++++++++++++++++++++++
; +++++ Test routines +++++
; +++++++++++++++++++++++++
;
; Test valid hex character
; ENTRY	Reg DE points to hex ASCII buffer
; EXIT	Carry set if not
;	Accu holds nybble
;
isHexdig:
	sub	'0'		; Strip off offset
	ret	c		; Error
	cp	10		; Test decimal
	ccf
	ret	nc
	sub	'A'-'9'-1	; Fix for hex
	cp	10		; Verify hex
	ret	c
	cp	15+1
	ccf
	ret
;
; Skip line number and DATA
; ENTRY	Reg DE points to line
; EXIT	Reg DE positioned to first hex-decimal byte
;
skpHead:
	call	skpline		; Skip line number
	call	skpspc		; Skip space
	call	skpDATA		; Skip DATA
	call	skpspc		; Skip space
	ret
;
; Test character delimiter or end of line
; ENTRY	Reg DE points to decimal ASCII buffer
; EXIT	Carry set on delimiter or end
;
isDel:
	ld	a,(de)		; Get character
	cp	','		; Test delimiter
	scf			; Yeap
	ret	z
	cp	' '		; Blank is delimiter, too
	scf
	ret	z
	cp	' '		; Verify printable character
	ret
;
; Skip blank in line
; ENTRY	Reg DE points to line
; EXIT	Pointer updated
;
skpspc:
	ld	a,(de)		; Get character
	cp	' '		; Test blank
	ret	nz		; Nope
	inc	de
	jr	skpspc		; Skip it
;
; Skip delimiter
; ENTRY	Reg DE points to line
; EXIT	Pointer updated
;
skpon:
	inc	de
skpdel:
	ld	a,(de)		; Get character
	cp	' '		; Test blank
	jr	z,skpon		; Skip it
	cp	','		; Test comma
	jr	z,skpon		; Skip it, too
	ret
;
; Skip decimal line number in line
; ENTRY	Reg DE points to line
; EXIT	Pointer updated
;
skpline:
	ld	a,(de)		; Get character
	inc	de
	cp	' '		; Test end of line number
	ret	z		; Yeap
	call	isdecdig	; Verify decimal digit
	jr	nc,skpline	; Try next
	ld	a,_ERRLIN
	jp	LastMsg		; Error
;
; Skip end of line
; ENTRY	Reg DE points to line
; EXIT	Pointer updated
;
nxtLine:
	call	incLine		; Increment line count
noCRLF:
	inc	de
skpCRLF:
	ld	a,(de)		; Get character
	cp	cr
	jr	z,noCRLF	; Skip new line
	cp	lf
	jr	z,nxtLine	; Increment line count ans skip it
	ret
;
; Verify decimal digit
; ENTRY	Accu holds character
; EXIT	Carry set if invalid
;
isdecdig:
	cp	'0'		; Check it
	ret	c
	cp	'9'+1
	ccf
	ret
;
; Skip keyword "DATA" in line
; ENTRY	Reg DE points to line
; EXIT	Pointer updated
;
skpDATA:
	push	hl
	push	de
	pop	ix		; Copy pointer
	ld	hl,invDATA
	push	hl		; Set error
	ld	a,'D'
	cp	(ix+0)		; Verify the sequence
	ret	nz
	ld	a,'T'
	cp	(ix+2)
	ret	nz
	ld	a,'A'
	cp	(ix+1)
	ret	nz
	cp	(ix+3)
	ret	nz
	pop	hl
	inc	de		; Skip DATA
	inc	de
	inc	de
	inc	de
	pop	hl
	ret
;
; ++++++++++++++++++++++++++++
; +++++ Message routines +++++
; ++++++++++++++++++++++++++++
;
; Error routine
; ENTRY	Accu holds index*2 to error procedure
;
ProcMsg:
	ld	(ErrPos),de	; Save current pointer
	ld	c,a		; Expand index
	ld	b,0
	ld	hl,ErrTab
	add	hl,bc		; Position table
	ld	e,(hl)		; Get address
	inc	hl
	ld	d,(hl)
	ex	de,hl
ErrLoop:
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	ld	a,e
	or	a		; Test end
	jr	z,crlf		; Yeap, close line
	push	hl
	ex	de,hl
	call	jhl
	pop	hl
	jr	ErrLoop
crlf:
	ld	de,$CRLF	; Close line
e_string:
	call	string		; Put to console
	ret
;
; Jump thru HL
;
jhl:
	jp	(hl)
;
; Print invalid line
;
PrLine:
	ld	de,$LINE
	call	e_string
	ld	de,(CurLine)	; Get current line
PrLLoop:
	ld	a,(de)		; Get character
	cp	' '		; Test end of line
	ret	c		; Yeap
	call	conout		; Print it
	inc	de
	jr	PrLLoop
;
Help:
	ld	de,$HELP
	jr	e_string
DataLen:
	ld	de,$DATLEN
	jr	e_string
Result:
	ld	de,$RESULT
	jr	e_string
File:
	ld	de,$FILE
	jr	e_string
Cannot:
	ld	de,$CANNOT
	jr	e_string
E_Open:
	ld	de,$OPEN
	jr	e_string
Create:
	ld	de,$CREATE
	jr	e_string
E_Close:
	ld	de,$CLOSE
	jr	e_string
ReadErr:
	ld	de,$READERR
	jr	e_string
ErrMess:
	ld	de,$ERROR
	jr	e_string
WriteErr:
	ld	de,$WRITERR
	jr	e_string
Invalid:
	ld	de,$INVALID
	jr	e_string
Character:
	ld	de,$CHAR
	jr	e_string
Illhex:
	ld	de,$ILLHEX
	jr	e_string
Illdec:
	ld	de,$ILLDEC
	jr	e_string
MissDATA:
	ld	de,$MISSDATA
	jr	e_string
InvLine:
	ld	de,$INVLINE
	jr	e_string
TypeErr:
	ld	de,$TYPEERR
	jr	e_string
Checksum:
	ld	de,$CHECKSUM
	jr	e_string
;
; Indcate position of line error
;
IndicErr:
	call	crlf		; Close line
	ld	hl,(ErrPos)	; Get last position
	ld	de,(CurLine)	; Get current line
	or	a
	sbc	hl,de		; Get position
	jr	z,PosFnd
	dec	hl
PosLine:
	ld	a,'-'
	call	conout		; Blank till error position
	dec	hl
	ld	a,l
	or	h
	jr	nz,PosLine
PosFnd:
	ld	a,'^'
	call	conout		; Indicate it
	jp	crlf		; Close line
;
; Print name of file
;
Fname:
	ld	hl,FCBnam	; Point to name part
	ld	b,.fname
	call	prFN		; Print name
	ld	a,'.'		; Indicate delimiter
	call	conout
	ld	b,.fext		; Print extension
prFN:
	ld	a,(hl)		; Get character
	and	NOMSB		; Less attribute
	cp	' '
	call	nz,conout	; Print if non blank
	inc	hl
	djnz	prFN
	ret
;
; Tell length of data read
;
GetLen:
	ld	hl,(DatLen)	; Get length
	ld	a,h		; Get hi
	call	ascbyt
	ld	a,l		; Get low
ascbyt:
	rrca
	rrca
	rrca
	rrca
	call	ascnyb		; Output it
ascnyb:
	and	LOMASK
	add	a,90h		; Do the trick
	daa
	adc	a,'0'+16
	daa
	call	conout
	ret
;
; Indicate invalid hex character
;
PutChar:
	ld	de,(ErrPos)	; Save current pointer
	dec	de		; Fix pointer
	ld	a,(de)		; Get character
	call	conout		; Put it
	ret
;
	dseg
TypeTable:
	db	'H','D','C','M'
TTlen	equ	$-TypeTable
TypeExec:
	dw	hexASCII,decASCII,crcASCII,mapASCII
ErrTab::
	dw	Error0,Error2,Error4,Error6,Error8
	dw	Error10,Error12,Error14,Error16,Error18
	dw	Error20,Error22,Err24
Error0:
	dw	Result,File,Fname,DataLen,GetLen,NULL
Error2:
	dw	Cannot,Create,File,Fname,NULL
Error4:
	dw	Cannot,E_Close,File,Fname,NULL
Error6:
	dw	ErrMess,WriteErr,File,Fname,NULL
Error8:
	dw	Invalid,Illhex,Character,PutChar,PrLine,IndicErr,NULL
Error10:
	dw	Help,NULL
Error12:
	dw	Invalid,TypeErr,Character,PutChar,NULL
Error14:
	dw	Cannot,E_Open,File,Fname,NULL
Error16:
	dw	ErrMess,ReadErr,File,Fname,NULL
Error18:
	dw	Invalid,InvLine,PrLine,IndicErr,NULL
Error20:
	dw	Invalid,Illdec,Character,PutChar,PrLine,IndicErr,NULL
Error22:
	dw	MissDATA,PrLine,NULL
Err24:
	dw	Invalid,Checksum,PrLine,NULL
;
$RESULT:
	db	'Result in',eot
$DATLEN:
	db	cr,lf
	db	'Bytes read: 0x',eot
$FILE:
	db	' file ',eot
$CANNOT:
	db	'Cannot ',eot
$CREATE:
	db	'create',eot
$OPEN:
	db	'open',eot
$CLOSE:
	db	'close',eot
$ERROR:
	db	'Error ',eot
$WRITERR:
	db	'writing to',eot
$READERR:
	db	'reading from',eot
$INVALID:
	db	'Invalid ',eot
$CHAR:
	db	' character ',eot
$ILLHEX:
	db	'hex',eot
$ILLDEC:
	db	'dec',eot
$MISSDATA:
	db	'Missing DATA statement',eot
$INVLINE:
	db	'line number',eot
$TYPEERR:
	db	'type',eot
$CHECKSUM:
	db	'checksum',eot
$LINE:
	db	cr,lf
	db	'Faulty line '
	ds	DIGlen-1,' '
$LINENR:			; \
	db	'1 : '		; |
$CRLF:				; |
	db	cr,lf,eot	; /
$TXT:
	db	'TXT'
$COM:
	db	'COM'
$HELP:
	db	'Program converts BASIC DATA statements (ASCII hex or decimal constants)'
	db	cr,lf
	db	'to hex and writes it to a COM file.'
	db	cr,lf
	db	'It also maps groups of eight character defining bytes.'
	db	cr,lf,lf
	db	'Call it:'
	db	cr,lf
	db	tab,tab,'DATA2COM -opt ASCII_file{.ext}'
	db	cr,lf
	db	'''opt'' is'
	db	cr,lf
	db	tab,tab,'H       Hex code less checksum'
	db	cr,lf
	db	tab,tab,'C       Hex code including checksum'
	db	cr,lf
	db	tab,tab,'D       Decimal code'
	db	cr,lf
	db	tab,tab,'M[&]num Map character matrix starting with key ''num'''
	db	cr,lf,lf
	db	'''ext'' defaults to ''TXT'''
	db	cr,lf
	db	'A new file ''ASCII_file.COM'' will be created'
	db	cr,lf,lf
	db	'++++++++++++++++++++++++++++++++++++++++++++++'
	db	cr,lf,lf
	db	'Sample lines:'
	db	cr,lf,lf
	db	'Type ''H'''
	db	cr,lf
	db	'540 DATA FD,21,A0,C0'
	db	cr,lf,lf
	db	'Type ''C'''
	db	cr,lf
	db	'54160 DATA CD, DF, C4, CD, 2C, C4, CD, 04, 4FE'
	db	cr,lf
	db	'50120 DATA 0A,32,7C,C0,4E,23,46,21,250'
	db	cr,lf,lf
	db	'Type ''D'''
	db	cr,lf
	db	'350 DATA 229,213,197,221,229,42,27,192,237,91'
	db	cr,lf,lf
	db	'Type ''M'''
	db	cr,lf
	db	'70 DATA 0,0,30,102,198,205,118,0,48,48,96,108,198,199,60,0'
	db	cr,lf,eot
$KEYFND:
	db	'Keys found: ',eot
$KEYNUM:
	db	'Current key number: ',eot
;
$memry:
	ds	2
HexDat:
	ds	2
$HEXDAT:
	ds	2
DatLen:
	ds	2
CurLine:
	ds	2
ErrPos:
	ds	2
StartKey:
	ds	1
	cseg
;
; ###############################
; ### The conversion routines ###
; ###############################
;
; Do the hex conversion
;
; Expected line type:
; 540 DATA FD,21,A0,C0,0A,B7,3E,86,28,02,3E,C6,32,68,C0,4E,23,46,EB,C5,5E,23,56,23,ED,53,B9
;
hexASCII:
	call	_hexASCII
	scf
	ret
_hexASCII:
	ld	hl,(HexDat)	; Init pointer to hex
hexLoop:
	call	isEndDATA	; Test end of ASCII
	ret	z		; Yeap
LoopHex:
	call	hexbyt		; Get hex byte
	ld	(hl),a		; Store
	inc	hl		; Update pointer
	call	skpdel		; Skip comma
	ld	a,(de)
	cp	' '		; Test end of line
	jr	nc,LoopHex	; Nope
	call	skpCRLF		; Skip end of line
	jr	hexLoop		; Try next line
;
; Do the hex conversion with checksum
;
; Expected line types:
; 50120 DATA 0A,32,7C,C0,4E,23,46,21,250             or
; 54160 DATA CD, DF, C4, CD, 2C, C4, CD, 04, 4FE
;
crcASCII:
	call	_crcASCII
	scf
	ret
_crcASCII:
	ld	hl,(HexDat)	; Init pointer to hex
crcLoop:
	call	isEndDATA	; Test end of ASCII
	ret	z		; Yeap
	exx
	ld	hl,0		; Clear checksum
	exx
LoopCRC:
	call	hexbyt		; Get hex byte
	ld	c,a		; Save it
	ld	a,(de)
	call	isHexdig	; Test next hex, too
	jr	c,stByte	; Nope, store this one
	inc	de		; Skip digit
	ld	b,0		; Clear high part
	push	hl
	push	bc
	pop	hl
	add	hl,hl		; Shift byte into right place
	add	hl,hl
	add	hl,hl
	add	hl,hl
	or	l		; Insert last digit
	ld	l,a
	push	hl
	exx	
	pop	bc
	or	a
	sbc	hl,bc		; Verify correct sum
	exx
	pop	hl
	ld	a,_ILLCRC
	jp	nz,LastMsg	; Invalid sum
skpCRC:
	inc	de
	ld	a,(de)
	cp	' '		; Test end of line
	jr	nc,skpCRC	; Nope
	call	skpCRLF		; Skip end of line
	jr	crcLoop		; Try next line
stByte:
	ld	a,c		; Get back byte
	ld	(hl),a		; Store it
	inc	hl		; Update pointer
	exx
	ld	c,a
	ld	b,0
	add	hl,bc		; Update checksum
	exx
	call	skpdel		; Skip comma
	ld	a,(de)
	cp	' '		; Test end of line
	jr	nc,LoopCRC	; Nope
	call	skpCRLF		; Skip end of line
	jr	crcLoop		; Try next line
;
; Do the decimal conversion
;
; Expected line type:
; 350 DATA 229,213,197,221,229,42,27,192,237,91,25,192,205,29,192
;
decASCII:
	call	_decASCII
	scf
	ret
_decASCII:
	ld	hl,(HexDat)	; Init pointer to hex
decLoop:
	call	isEndDATA	; Test end of DATA lines
	ret	z		; Yeap
LoopDec:
	call	decbyt		; Get hex byte
	ld	(hl),a		; Store
	inc	hl		; Update pointer
	call	skpdel		; Skip comma
	ld	a,(de)
	cp	' '		; Test end of line
	jr	nc,LoopDec	; Nope
	call	skpCRLF		; Skip end of line
	jr	decLoop		; Try next line
;
; Test end of DATA lines
; EXIT	Zero set if end detected
;
isEndDATA:
	ld	(CurLine),de	; Save current line
	ld	a,(de)		; Get character
	cp	eol		; Test end of ASCII
	push	af
	call	nz,skpHead	; Skip line number and DATA if not
	pop	af
	ret
;
; Do the decimal character display
;
; Expected line type:
; 350 DATA 229,213,197,221,229,42,27,192,237,91,25,192,205,29,192
;
mapASCII:
	call	decASCII	; Do the conversion task
	call	DispChar	; Display characters
	xor	a
	ret
;
; Display the character setting
;
DispChar:
	ld	de,(HexDat)	; Init pointer to hex
	or	a
	sbc	hl,de		; Get length of data
	ld	a,l
	and	00000111b	; Verify byte boundary
;;	NZ is error
	srl	h		; Divide by 8
	rr	l
	srl	h
	rr	l
	srl	h
	rr	l
	ld	a,h		; Verify max
	or	a
;;	NZ is error
	ld	(DatLen),hl	; Save key count
	ld	c,l
	ld	de,$KEYFND
	call	NumOut		; Tell it
	ld	a,(StartKey)	; Load key number
	ld	c,a
	ld	hl,(HexDat)	; Init pointer to hex
NxtKey:
	ld	de,$KEYNUM
	call	NumOut		; Tell key number
	ld	b,8		; Init byte count
DCloop:
	ld	a,(hl)		; Get pixel byte
	inc	hl
	call	DispBits
	djnz	DCloop
	inc	c		; Next key number
	call	LastKey		; Test last number
	jr	nz,NxtKey
	ret
; Display 8 bis in a line
; ENTRY	Accu holds byte
;
DispBits:
	push	bc
	ld	b,8		; Set length
DBloop:
	add	a,a		; Get hi bit
	push	af
	call	Disp01		; Display it
	pop	af
	djnz	DBloop
	call	crlf		; Close line
	pop	bc
	ret
;
; Display a bit
; ENTRY	Carry set marks a 1
;
Disp01:
	ld	a,Bit1		; Set default one
	jr	c,bitout	; Got it
	ld	a,Bit0		; Change to zero
bitout:
	call	conout		; Put bit
	ret
;
; Print message followed by decimal number
; ENTRY	Reg DE holds message
;	Reg C  holds number
;
NumOut:
	push	bc
	push	hl
	call	string		; Give message
	ld	l,c		; Get number
	ld	h,0
	call	prdec		; Print it
	call	crlf
	pop	hl
	pop	bc
	ret
;
; Print decimal number
; ENTRY	Reg HL holds number
;
prdec:
	ld	bc,-10		; Load constant
Numb:
	ld	de,-1		; Init quotient
.Numb:
	add	hl,bc		; Subtract 10s
	inc	de		; Update quotient
	jr	c,.Numb
	push	hl		; Save remainder
	ex	de,hl
	ld	a,h
	or	l		; Test if remainder zero
	call	nz,Numb		; New division if not
	pop	hl		; Get back remainder
	ld	a,l
	add	a,'0'+10	; Make printable
	call	conout		; Print digit
	ret
;
; Test more keys
; EXIT	Zero flag set if so
;
LastKey:
	exx
	ld	hl,(DatLen)	; Get length
	dec	hl		; Count down
	ld	(DatLen),hl	; Save length
	ld	a,l
	or	h		; Fix for end
	exx
	ret

	end	DA2COM

