	title	RSX reporting all console output characters
	name	('VIDDUMP')

; This RSX simply looks for control characters.
; It acts as follows:
;
; 00H-1AH	Dump as hex byte <xx>
; 1BH		Get next character and dump both
; 1CH-1FH	Dump as hex byte <xx>
; 20H-7EH	Normal output
; 7FH		Dump as hex byte <xx>
; 80H-FFH	Dump as hex byte <xx> with video attribute on

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		(+49)40/4223247

;		Version 1.0, October 1992

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

esc	equ	1bh
eot	equ	'$'

; ===== RSX starts here =====

; ===== RSX header starts =====

	db	0,0,0,0,0,0	; Six byte serial number
	jp	start		; Jump to RSX program
BDOS:
	jp	$-$		; Jump to next RSX
	dw	0		; Address of previous RSX
	db	0ffh		; Remove flag (0 keep permanent)
	db	0		; Non bank only flag
	db	'VIDDUMP '	; Any eight character name
	db	0		; Loader flag
	dw	0		; Spare (reserved)
;
COexe:
	dw	cout1

; ===== RSX header ends =====

start:
	ld	a,c		; Get BDOS function
	cp	.conout		; Test console output
	jr	z,cout
	cp	.string		; Test string output
	jr	z,strout
	cp	.condir		; Test direct console output
	jr	nz,BDOS
	ld	a,e		; Get character or code
	cp	0fdh		; Verify output
	jr	nc,BDOS
;
; Entry of console output
;
cout:
	ld	hl,(COexe)	; Fetch routine
	jp	(hl)		; Execute it
;
; Entry of string output
;
strout:
	ld	a,(de)		; Get string character
	cp	eot		; Test end of string
	ret	z		; Yeap
	push	de
	ld	e,a		; Get character
	call	cout		; Output it
	pop	de
	inc	de
	jr	strout		; Try next
;
; Console output - level 1
;
cout1:
	ld	a,e		; Get character
	cp	esc		; Test prefix
	jr	z,doESC
	cp	' '		; Test control
	jr	c,ctout		; Yeap
	cp	'~'+1
	jr	z,ctout		; Yeap
	jr	c,CO		; Normal output
	push	af
	call	vidon		; Indicate hi bit set
	pop	af
	res	7,a		; Clear hi bit
	ld	e,a
	call	cout1		; Recursive call
	call	vidoff
	ret
;
; Dump character as hex
;
ctout:
	ld	a,'<'
	call	CO		; Indicate hex follows
	ld	a,e
	call	hxout		; Dump it
	ld	a,'>'
CO:
	push	de
	ld	c,.conout
	ld	e,a
	call	BDOS		; Output to console
	pop	de
	ret
;
; Found ESCape character - get next character and dump it
;	
doESC:
	ld	de,$ESC
	ld	c,.string
	call	BDOS		; Tell ESCape follows
	call	swlevel		; Switch level
	ld	a,e
	call	hxout		; Dump ESC control
	ld	hl,cout1
	ld	(COexe),hl	; Reset routine
	ret
;
; Print byte in Accu as hex ASCII
;
hxout:
	push	af		; Save character
	rrca			; Get hi bits
	rrca
	rrca
	rrca
	call	nibout		; Print it
	pop	af
nibout:
	and	00001111b	; Mask bits
	add	a,090h		; Convert it
	daa
	adc	a,040h
	daa
	ld	e,a
	call	CO		; Print character
	ret
;
; Change execution address and exit
;
swlevel:
	pop	hl
	ld	(COexe),hl	; Change routine
	ret
;
; Turn on inverse video
;
vidon:
	ld	e,'p'		; Set code
	jr	..vid
;
; Turn on inverse video
;
vidoff:
	ld	e,'q'		; Set code
..vid:
	push	de
	ld	e,esc
	call	CO		; Print escape
	pop	de
	call	CO
	ret
;
$ESC:
	db	'<ESC::>',eot

	end

