	title	Character Representation for the JOYCE
	name	('JOYCHAR')

; This small utility displays all available characters of the
; Joyce using BDOS an BIOS calls.

; Select calls by parameters:

; JOYCHAR 6	Direct console output
;		It is not possible to display the characters
;		0xFDh, 0xFEh and 0xFFh
; JOYCHAR 2	Console output
;		The tabulator will be handled
; JOYCHAR 2T	Console output with suppressed tabulator
;		The tabulator will not be handled
; JOYCHAR B	BIOS console output
;
; Only calls 'JOYCHAR 2T' and 'JOYCHAR B' are displaying the complete
; character set

OS	equ	0000h
BDOS	equ	0005h
TPAtop	equ	BDOS+1
FCBnam	equ	005dh

.conout	equ	  2
.condir	equ	  6
.string	equ	  9
.consta	equ	109

_COT	equ	4		; BIOS function

null	equ	00h
tab	equ	09h
lf	equ	0ah
cr	equ	0dh
esc	equ	1bh
eot	equ	'$'
DEL	equ	7fh
SPC	equ	80h

	ld	sp,(TPAtop)	; Load stack
	ld	a,(FCBnam)	; Fetch parameter
	cp	' '		; End if no parameter available
	jr	z,Help
	sub	'0'		; Make binary from ASCII
	cp	.conout
	jr	z,setcon
	cp	.condir
	jr	z,setcon
	cp	'B'-'0'		; Test BIOS
	jr	nz,Help		; Invalid
	call	BIOSini
	jr	gochar
Help:
	ld	de,$HELP
	call	String		; Print small help text
	jp	OS
setcon:
	ld	(concall),a	; Save function
	cp	.conout
	call	z,chgtab	; Turn off tab handling on request
gochar:
	ld	de,$HEAD
	call	String		; Print headline
	ld	c,null		; Set start character
	ld	b,2		; Number of lines
	call	put		; Print in range 0x00h-0x1Fh
	ld	de,$DEL
	call	String
	ld	a,DEL
	call	conout		; Print special character
	ld	de,$EXTEND
	call	String
	ld	c,SPC		; Set start character
	ld	b,8		; Number of lines
	call	put		; Print in range 0x80h-0xFFh
	jp	OS		; End
;
; Print line with extended characters
; Start character in reg C, number of lines in reg B
;
put:
	ld	a,c		; Get start character
	call	hexout		; Print as hex
	ld	de,$DELIM
	call	String		; Print delimiter
	push	bc
	ld	b,16		; Number of characters within one line
put2:
	ld	a,c		; Get character
	cp	' '		; Test control
	call	c,ESCout	; Print ESCape if so
	ld	a,c
	call	conout		; Print character
	ld	de,$BLANK
	call	String		; Print space
	inc	c		; Next character
	djnz	put2
	call	crlf		; Close line
	ld	a,c
	pop	bc
	ld	c,a
	djnz	put
crlf:
	ld	de,$CRLF
	call	String
	ret
;
; Print byte in Accu as characters
;
hexout:
	push	af
	rra			; Get upper bits
	rra
	rra
	rra
	call	prhex		; Print
	pop	af		; Get lower bits
prhex:
	and	00001111b	; Mask bits
	add	a,90h
	daa			; Convert to ASCII
	adc	a,40h
	daa
	jr	conout		; Print
;
; Print ESCAPE character
;
ESCout:
	ld	a,esc
;
; Print ESCAPE character in Accu
;
conout:
	push	bc
	push	de
	push	hl
	ld	hl,(CONvec)	; Get output vector
	call	jp.r		; Print it
	pop	hl
	pop	de
	pop	bc
	ret
concall:
	ds	1
;
jp.r:
	jp	(hl)
;
; Character output through BDOS
;
BDOS.COT:
	ld	e,a		; Unpack character
	ld	a,(concall)	; Get BDOS function
	ld	c,a
	call	BDOS		; Output
	ret
;
; Character output through BIOS
;
BIOS.COT:
	ld	c,a		; Unpack character
	ld	hl,(@COT)	; Get BIOS address
	call	jp.r		; Output
	ret
;
; Put string ^DE to console
;
String:
	push	bc
	push	de
	push	hl
	ld	c,.string
	call	BDOS		; Put it
	pop	hl
	pop	de
	pop	bc
	ret
;
; Turn off tab handling on request
;
chgtab:
	ld	a,(FCBnam+1)	; Get character following '2'
	cp	'T'		; Test suppressing of tab handling
	ret	nz		; No	
	ld	de,-1
	call	conset		; Get state of console
	ex	de,hl
	set	2,e		; Modify tab bit
conset:
	ld	c,.consta
	call	BDOS		; Attache state of console
	ret
;
; Calculate BIOS vector referring to console output
;
BIOSini:
	ld	hl,(OS+1)	; Get BIOS base address
	ld	de,3*(_COT-1)
	add	hl,de
	ld	(@COT),hl	; Save address for console output
	ld	hl,BIOS.COT
	ld	(CONvec),hl
	ret
@COT:
	ds	2
CONvec:
	dw	BDOS.COT
;
$HELP:
	db	cr,lf,lf
	db	'his small utility displays all available characters of the'
	db	cr,lf
	db	'Joyce using BDOS an BIOS calls.'
	db	cr,lf,lf
	db	'Select calls by parameters:'
	db	cr,lf,lf
	db	'JOYCHAR 6',tab,'Direct console output'
	db	cr,lf
	db	tab,tab,'It is not possible to display the characters'
	db	cr,lf
	db	tab,tab,'0xFDh, 0xFEh and 0xFFh'
	db	cr,lf
	db	'JOYCHAR 2',tab,'Console output'
	db	cr,lf
	db	tab,tab,'The tabulator will be handled'
	db	cr,lf
	db	'JOYCHAR 2T',tab,'Console output with suppressed tabulator'
	db	cr,lf
	db	tab,tab,'The tabulator will not be handled'
	db	cr,lf
	db	'JOYCHAR B',tab,'BIOS console output'
	db	cr,lf,lf
	db	'Only calls ''JOYCHAR 2T'' and ''JOYCHAR B'' are displaying the complete'
	db	cr,lf
	db	'character set'
	db	cr,lf,lf,eot
$HEAD:
	db	cr,lf,lf
	db	'    00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F'
	db	cr,lf
	db	'    -----------------------------------------------'
	db	cr,lf
	db	'    (ESCape praefix)'
	db	cr,lf,eot
$DEL:
	db	'   (ASCII special)'
	db	cr,lf
	db	'7F:                                              '
	db	eot
$EXTEND:
	db	cr,lf
	db	'    (Extended characters)'
$CRLF:
	db	cr,lf,eot
$DELIM:
	db	': '
	db	eot
$BLANK:
	db	'  '
	db	eot

	end

