;Show Value - converts ascii/hex/decimal/binary
;Top level routines

;
;	Copyright 1985	Gordon Wilk
;	Released for non-commercial use
;	

	PUBLIC	PSTR	;function
	EXTRN	DOASC,DOHEX,DODEC,DOBIN,DISPLAY

	MACLIB	TERMCAP

BDOS	EQU	05
BOOT	EQU	00
PrStr 	EQU	09		;BDOS functions
DirIO	EQU	06
GetStr  EQU	10
BUFFmx	EQU	80h		;CPM defulat buffer addresses
BUFFnc	EQU	81h
BUFFfc	EQU	82h
UpArrow	EQU	8Ah		;arrow keys
DnArrow	EQU	8Ch
DELIMIT	EQU	110

;---------------------------------------------------------

;SETUP
	LXI	SP,STAK		;Get a stack
	MVI	E,0		;Set delimiter to 0
	MVI	C,DELIMIT
	CALL	BDOS
	PRINT 	SCREEN		;Set up the screen

MainLoop
	CALL	SELECT		;Get input base

;An ASCII character is returned which is the window number selected
;6 = ASCII input	8 = hex input
;7 = decmial input	9 = binary input
;This is the Underline Window Number - UWNR

	STA	UWNR		;store window for input
	PRINT	UBAR		;Show where to write
	CALL	GETS		;Get input
	PRINT	NORMAL		;Get rid of underlines

;CASE -select conversion based on which window used for input
	LDA	UWNR
	CPI	'6'
	CZ	DOASC		;If input in ASCII
	LDA	UWNR
	CPI	'7'
	CZ	DOHEX		;If input in hex
	LDA	UWNR
	CPI	'8'
	CZ	DODEC		;If input in decimal
	LDA	UWNR
	CPI	'9'
	CZ	DOBIN		;If input in binary

;Now display the results in each base and loop back
	CALL	DISPLAY		;Show answers
	JMP	MainLoop

;------------------------------------------------------------
;LOOP
;Keep looping as long as arrows are pressed
;Return when CR pressed
;Return A = window from which to accept input 

SELECT:
	PRINT	RBAR		;Reverse Bar - hilite the base
	CALL	INKEY		;Get an arrow or CR
	PUSH	PSW		;Save it
	PRINT	NORMAL		;Set current window to normal
	POP	PSW		;Restore chosen window
	call	CHANGE		;interpret character entered

;Returns an ASCII char which is the current window selected
; If <5 we are still on the Reverse bars and have not picked a base
; If >5 we are in Underline windows and have picked a base

	CPI	'5'		
	RNC			;base picked
	STA	RWNR
	JMP	SELECT		;Again

;----------------------------------------------------------
;CASE
;Allowable entries are arrows to move bars
; or CR to select the input mode

;Enter with A = input key
;Return	with A = new window 

CHANGE:
	CPI	CR		;Mode picked?
	JNZ	noPICK
	LDA	RWNR		;The Underline window
	ADI	5		; = the Reverse window plus 5
	RET

noPICK	CPI	UpArrow		;move up?
	JNZ	notUP
UP	LDA	RWNR
	DCR	A		;Upper window nrs are lower
	CPI	'0'		;Too far up?
	JZ	noMOVE
	RET
notUP	CPI	DnArrow		;move down?
	JNZ	noMOVE
DOWN	LDA	RWNR
	INR	A
	CPI	'5'		;Too far down?
	RNZ
noMOVE:
	LDA	RWNR
	RET

;======================================================
;	utility routines
;=======================================================

INKEY				;Get a key w/o echo
	MVI	E,0FDh
	MVI	C,DirIO
	CALL	BDOS
	CPI	03		;Quit?
	RNZ
EXIT	PRINT	W0		;Always restore normal  
	JMP	BOOT		;screen on exit

PSTR:	MVI	C,PrStr		;Print string
	jmp	BDOS		; DE->string
				;return from BDOS

GETS:				;Get string
	LDA	UWNR		
	SUI	'6'
	LXI	D,0		;Get string length max
	MOV	E,A		; based on window nr
	LXI	H,LENGTH	;LENGTH contains the lengths for each window
	DAD	D
	MOV	A,M		;String goes
	LXI	D,BUFFmx		; to buffer @ 80h
	STAX	D
	MVI	C,GetStr
	CALL	BDOS
	LDA	BUFFnc		;nr of chars
	ADI	BUFFfc		;+ adr of 1st char
	MOV	L,A		; to HL
	XRA	A
	MOV	H,A		;= last + 1
	MOV	M,A		;set it to 0
	RET

;======================================================
	DSEG
;=====================================================

SCREEN:			;Just fills in the screen
	DB	CLS
	DB	ESC,FKEYOFF	;disable function key translate
	DB	'Use arrows to select entry form. '
	DB	' Press <CR> then enter value.'
	DB	'  (^C TO EXIT) '
	DB	ESC,CURS@,32+10,32+11,'   ASCII: '
	DB	ESC,CURS@,32+12,32+11,'     HEX: '
	DB	ESC,CURS@,32+14,32+11,' DECIMAL: '
	DB	ESC,CURS@,32+16,32+11,'  BINARY: '
DEFBARS:		;Defines the windows 
	DB	ESC,WINDOW,'1',32+10,32+10,32+10,32+21
	DB	ESC,WINDOW,'6',32+10,32+23,32+10,32+25
	DB	ESC,WINDOW,'2',32+12,32+10,32+12,32+21	
	DB	ESC,WINDOW,'7',32+12,32+23,32+12,32+27
	DB	ESC,WINDOW,'3',32+14,32+10,32+14,32+21
	DB	ESC,WINDOW,'8',32+14,32+23,32+14,32+28
	DB	ESC,WINDOW,'4',32+16,32+10,32+16,32+21
	DB	ESC,WINDOW,'9',32+16,32+23,32+16,32+40,0
			;Exit with window 9 set


RBAR:			;Set a window background to reverse
	DB	ESC,SETWNDW
RWNR	DB	'1'	;window number - set by program
	DB	ESC,DEFBG,'4'
	DB	0	;Exit with this window set

W0:			;Restore whole screen normal video
	db	ESC,FKEYON	
	DB	ESC,DEFBG,'0'
	DB	ESC,SETWNDW,'0'
NORMAL:			;Set current window to normal video
	DB	ESC,DEFBG,'0'
	DB	0	;Current window still set

UBAR:			;Set a window background to UNDERLINE
	DB	ESC,SETWNDW
UWNR	DB	'6'	;window number - set by program
	DB	ESC,DEFBG,'8'
	DB	CLS
	DB	0	;Exit with this window set

CRLF:
	DB	CR,LF,0

LENGTH: DB	1,4,5,16	;window accept lengths

	DS	64		;stack
STAK	DW	0

	END
