	title	SET24X80 utility
	name	('SET24X80')

; The AMSTRAD SET24X80 utility for PCW machines

; Disassembled by W.Cirsovius

; Call: SET24X80 ON or OFF

OS	equ	0000h
BDOS	equ	0005h
CCPcmd	equ	0080h

_Conout	equ	 2
_Condir	equ	 6
_String	equ	 9
_vers	equ	12

CPM31	equ	31h
CPM22	equ	22h

CD.VERS	equ	00e3h

_Userf	equ	30

lf	equ	0ah
cr	equ	0dh
eot	equ	'$'
esc	equ	1bh
eof	equ	'Z'-'@'
eos	equ	-1		; End of table marker

_IG	equ	0
_EO	equ	5

MSB	equ	7

	jr	SET24X80

	db	'SET24X80 #115',cr,lf
	db	'Developed by Locomotive Software Ltd.',cr,lf
	db	'Copyright (C) 1985 '
	db	'Amstrad Consumer Electronics PLC',cr,lf
;
; %%%%%%%%%%%%%%%%%%%%%%
; %%% START SET24X80 %%%
; %%%%%%%%%%%%%%%%%%%%%%
;
SET24X80:
	ld	(UsrStk),sp	; Save callers stack
	ld	sp,LocStk	; Get local stack
	call	GetVersion	; Get machine version
	call	PrepCCPLine	; Prepare command line
	call	c,ProcSET	; Process language set up
	ld	sp,(UsrStk)	; Get back stack
	ret
;
; Process screen set up
; ENTRY	Reg C holds macine type
;
ProcSET:
	ld	a,c		; Test correct machine
	cp	3
	jp	z,InvalEnv	; .. nope
	ld	hl,$ON.OFF
	call	FindStr		; Find ON or OFF
	ld	a,l		; Test result
	or	a
	jr	nz,SetScr	; .. well done
	call	FixNL		; Test end of line
	jr	z,SetERR	; .. nope, should be
	ld	l,'x'		; Set default ON
SetScr:
	ld	a,esc
	call	Condir		; Give ESCape
	ld	a,l
	jr	Condir		; .. and character
;
$ON.OFF:
	db	'ON',eos,'x',0
	db	'OFF',eos,'y',0
	db	eos,0,0
;
SetERR:
	ld	de,$SET.ERR
	jp	ProcErr
$SET.ERR:
	db	'Must be ON or OFF',cr,lf,eot
;
; Print character on console via BDOS
; ENTRY	Accu holds character
;
Condir:
	push	hl
	push	de
	push	bc
	push	af
	ld	e,a
	ld	c,_Condir
	call	BDOS		; .. print
	pop	af
	pop	bc
	pop	de
	pop	hl
	ret
;
; Process error
; ENTRY	Reg DE points to error message
;
ProcErr:
	push	bc
	push	af
	call	String		; Print error
	call	GetAttr		; Test EOL or EOF
	jr	nz,PE.ex	; .. yeap
	ld	de,$IGNORE
	call	String		; Tell ignoring
	jr	PE.go
PE.loop:
	ld	a,c
	call	Conout		; Print character
PE.go:
	call	GetChr		; Get character and attribute
	call	GetAttr		; Test EOL or EOF
	call	Get		; Get character
	jr	z,PE.loop	; .. there is more to print
	ld	de,$CRLF
	call	String		; .. close line
PE.ex:
	pop	af
	pop	bc
	ret
$IGNORE:
	db	'Ignoring rest of line: ',eot
$CRLF:
	db	cr,lf,eot
;
; Tell invalid environment
;
InvalEnv:
	push	de
	ld	de,$INVENV
	call	String		; Print error
	pop	de
	ret
$INVENV:
	db	'This program will not run in this environment'
	db	cr,lf,eot
;
; Print string on console
; ENTRY	Reg DE points to string closed by '$'
;
String:
	push	hl
	push	de
	push	bc
	push	af
	ld	c,_String
	call	BDOS		; .. print
	pop	af
	pop	bc
	pop	de
	pop	hl
	ret
;
; Print character on console
; ENTRY	Accu holds character
;
Conout:
	push	hl
	push	de
	push	bc
	push	af
	ld	e,a
	ld	c,_Conout
	call	BDOS		; .. print
	pop	af
	pop	bc
	pop	de
	pop	hl
	ret
;
; Fix for end of line
;
FixNL:
	call	GetAttr		; Get attributes
	call	nz,Get		; .. read next if EOL or EOF
	ret
;
; Find string from table
; ENTRY	Reg HL points to table
; EXIT	Reg L holds 1st operand
;	Reg H holds 2nd operand or -1 if not found
;
FindStr:
	push	de
	push	bc
	push	af
	call	ChrGet		; Get valid character
	ld	d,b		; Save attribute
	dec	hl		; .. fix list
	jr	FS.go
FS..unget:
	inc	e
FS.unget:
	dec	e
	call	nz,UnGet	; Unget character
	jr	nz,FS.unget
FS.skip:
	ld	a,(hl)		; Get character
	inc	hl
	inc	a
	jr	nz,FS.skip	; .. skip item
	inc	hl
FS.go:
	ld	e,0		; Init counter
	jr	FS.cmp
FS.loop:
	call	CmpChr		; Compare character
	jr	nz,FS..unget	; .. not found
	call	Get		; Get character from file
	inc	e
FS.cmp:
	call	GetChr		; Get character and attribute
	inc	hl
	ld	a,(hl)
	cp	eos		; Test end of string
	jr	nz,FS.loop	; .. nope
	inc	e
	dec	e		; Test total end
	jr	z,FS.end	; .. yeap
	ld	a,b
	xor	d		; Test same attribute
	and	00101011b	; Test result
	jr	z,FS..unget	; .. still searching
FS.end:
	inc	hl
	ld	e,(hl)		; Get operands
	inc	hl
	ld	d,(hl)
	ex	de,hl		; .. into right reg
	pop	af
	pop	bc
	pop	de
	ret
;
; Compare characters
; ENTRY	Accu and reg C hold characters
; EXIT	Zero set if same
;
CmpChr:
	sub	c		; Compare
	ret	z		; .. found
	cp	-' '		; Test blank ????
	ret	nz		; .. nope
	ld	a,b
	or	11111101b	; Fix attribute
	inc	a		; .. fix result
	ret
;
; Get valid definition character from file
;
ChrGet:
	jr	.ChrGet		; .. skip reading from file
CG.loop:
	call	Get		; Get character from file
.ChrGet:
	call	GetChr		; Get character and attribute
	bit	_EO,b		; Test end of file or line
	ret	nz		; .. exit
	bit	_IG,b		; Test valid character
	jr	nz,CG.loop	; .. nope
	ret
;
; Prepare CCP command line
; EXIT	Carry set if line OK
;
PrepCCPLine:
	push	hl
	push	de
	push	bc
	ld	hl,CCPcmd
	ld	a,(hl)		; Fetch length of input
	inc	a
	ld	(RecPtr),a	; .. save it
	inc	hl
	ld	(BufPtr),hl	; Init buffer
	dec	a
	add	a,l		; Point to end of line
	ld	l,a
	adc	a,h
	sub	l
	ld	h,a
	ld	(hl),eof	; Close line
	scf
	pop	bc
	pop	de
	pop	hl
	ret	nc
;
; Get character from command line
; Store character and attribute
;
Get:
	push	hl
	push	de
	push	bc
	push	af
	ld	hl,(BufPtr)	; Get current pointer
	ld	c,(hl)		; .. get character
	ex	de,hl
	call	PutChr		; .. save it
	ld	a,c
	cp	eof		; Test end of line
	jr	z,Get.eof
	ld	a,(RecPtr)
	dec	a		; Fix pointers
	ld	(RecPtr),a
	ex	de,hl
	inc	hl
	ld	(BufPtr),hl
Get.eof:
	pop	af
	pop	bc
	pop	de
	pop	hl
	ret
;
; Unget current character
;
UnGet:
	push	hl
	push	de
	push	bc
	push	af
	ld	a,(Char)	; Get character
	cp	eof		; Test end of line
	ld	hl,(BufPtr)
	jr	z,UG.dummy	; .. yeap, skip
	ld	hl,RecPtr
	inc	(hl)		; Fix pointers if not
	ld	hl,(BufPtr)
	dec	hl
	ld	(BufPtr),hl
UG.dummy:
	dec	hl
	ld	c,(hl)
	call	PutChr		; Save character and attribute
	pop	af
	pop	bc
	pop	de
	pop	hl
	ret
;
; Put character and attribute into memory
; ENTRY	Reg C holds character
;
PutChr:
	bit	MSB,c		; Test 80..FF
	ld	b,01h
	jr	nz,.PutChr	; .. yeap, set standard
	ld	b,0
	ld	hl,AttrTable
	add	hl,bc
	ld	b,(hl)		; Fetch attribute
.PutChr:
	ld	(Char),bc	; Save character and attribute
	ret
;
; Get current character and attribute
; EXIT	Reg C holds character
;	Reg B holds attribute
;
GetChr:
	ld	bc,(Char)	; Get character and attribute
	ret
;
; Get EOL or EOF state of current character
; EXIT	Zero set if not EOL or EOF
;
GetAttr:
	ld	a,(Attr)	; Get attribute
	and	1 SHL _EO	; .. test bit
	ret
;
; Get version of machine
; EXIT	Reg C holds type of machine
;	0 : CPC6128
;	1 : PCW8256
;	3 : CP/M 2.2 version
;	4 : Other machine
;
GetVersion:
	ld	c,_vers
	call	BDOS		; Get OS version
	cp	CPM31		; Test CP/M+
	jr	z,Vers31
	cp	CPM22		; Test CP/M 2.2
	ld	c,4
	ret	nz
	ld	c,3
	ret
Vers31:
	ld	hl,(OS+1)
	ld	de,3*(_Userf-1)
	add	hl,de
	ld	(Userf+1),hl	; Set USERF vector
	call	GetType		; Get machine type
	ld	c,a		; .. set result
	ret
;
; Execute BIOS function 30 : USERF
;
Userf:
	jp	$-$
;
; Get type of machine
; EXIT	Accu holds type of machine
;
GetType:
	call	Userf		; .. get type
	dw	CD.VERS
	ret
;
; Attribute table of all ASCII characters
; Bit definitions:
;
;    7    6    5    4    3    2    1    0
; +----+----+----+----+----+----+----+----+
; | xx | xx | EO | EX | NM | HX | AN | IG |
; +----+----+----+----+----+----+----+----+
;
; xx	N.C.
; EO	EOL or EOF
; EX	Extension string
; NM	Numeric 0..9
; HX	Hex range A..F
; AN	Alphanumeric prefix
; IG	Ignore for definition
;
AttrTable:
;
; Control characters
;
	db	01h,01h,01h,01h,01h,01h,01h	; 00..06
	db	01h,01h,01h,01h,01h,01h,21h	; 07..0D
	db	01h,01h,01h,01h,01h,01h,01h	; 0E..14
	db	01h,01h,01h,01h,01h,21h,01h	; 15..1B
	db	01h,01h,01h,01h			; 1C..1F
;
; ASCII characters
;
	db	01h,01h,00h,10h,01h,01h,10h	;  !"#$%&
	db	01h,01h,01h,01h,01h,01h,01h	; '()*+,\
	db	00h,01h,0ch,0ch,0ch,0ch,0ch	; ./01234
	db	0ch,0ch,0ch,0ch,0ch,01h,01h	; 56789:;
	db	01h,01h,01h,01h,01h,06h,06h	; <=>?@AB
	db	06h,06h,06h,06h,02h,02h,02h	; CDEFGHI
	db	02h,02h,02h,02h,02h,02h,02h	; JKLMNOP
	db	02h,02h,02h,02h,02h,02h,02h	; QRSTUVW
	db	02h,02h,02h,01h,01h,01h,01h	; XYZ[\]^
	db	01h,01h,06h,06h,06h,06h,06h	; _`abcde
	db	06h,02h,02h,02h,02h,02h,02h	; fghijkl
	db	02h,02h,02h,02h,02h,02h,02h	; mnopqrs
	db	02h,02h,02h,02h,02h,02h,02h	; tuvwxyz
	db	01h,01h,01h,01h,01h		; {|}~DEL

	db	eof

	ds	259,0

DATA	equ	1000h

UsrStk	equ	DATA+256
LocStk	equ	UsrStk+258
BufPtr	equ	UsrStk+258
RecPtr	equ	BufPtr+2
Char	equ	RecPtr+1
Attr	equ	Char+1

	end

