;PROGRAM:	SV
;MODULE:	DO
;Math and display routines for Show Value
;

			;Convert string to internal - function
	PUBLIC	DOASC	;	ascii
	PUBLIC	DOHEX	;	hex
	PUBLIC	DODEC	;	decimal
	PUBLIC	DOBIN	;	binary
	PUBLIC	DISPLAY	;Display function - convert back to characters
	PUBLIC	VALUE	;storage for converted value

	EXTRN	PSTR
	EXTRN	EVAL2,EVAL10,EVAL16	;SYSLIB
	EXTRN	PHL4HC,PHL5DC		;SYSLIB
	EXTRN	CCOUT,COUT		;SYSLIB

	MACLIB	TERMCAP

BUFFER	EQU	82h
;
;For each of these the input string is in the TBuffer at 80h
; so just point at it and call the appropriate SYSLIB routine,
; then store the value returned.

DOHEX:
	LXI	H,BUFFER
	CALL	EVAL16
EXIT	XCHG	
	SHLD	VALUE
	RET
DODEC:
	LXI	H,BUFFER
	CALL	EVAL10
	JMP	EXIT
DOBIN:
	LXI	H,BUFFER
	CALL	EVAL2
	JMP	EXIT
DOASC:			
	LDA	BUFFER	;All we have to do is store the character
	MVI	H,0
	MOV	L,A
	SHLD	VALUE
	RET

;-------------------------------------------
;Start in window 6 - ASCII window then increment for each succsessive
; base. For each base - set the window, load the VALUE and call the 
; appropriate routine to do the printing

DISPLAY:

	MVI	A,'6'	;Window 6 is ASCII
	CALL	ThisWindow
;ASCII
	MVI	A,LF	;A line feed will
	CALL	COUT	; clear the window
	MOV	A,L	; then
	CALL	CCOUT	; just print the low byte
;HEX
	CALL	NextWindow
	CALL	PHL4HC	;A SYSLIB routine to do the work
;DECIMAL
	CALL	NextWindow
	CALL	PHL5DC	;SYSLIB routine
;BINARY
	CALL	NextWindow
	CALL	PHLBIN	;Our own  see below
	RET

NextWindow:
	LDA	WNR
	INR	A	;Brings us to next window
ThisWindow:
	STA	WNR
	PRINT   SETW
	LHLD	VALUE	;This is the value to print
	RET

;-------------------------------------------------
;
DJNZ	MACRO	TO		;;Z80 macro
	DB	10H,TO-$-1	;;Decrement B jmp nonzero
	ENDM
;
PHLBIN:
	XRA	A	;Clear the flags
	MOV	A,H	;get the high byte
	CALL	PBYTE   ;Print one byte
	MVI	A,' '
	CALL	COUT	; a space
	XRA	A
	MOV	A,L	; and the low byte
	CALL	PBYTE	;is printed
	RET

PBYTE:
	MVI	B,8	;eight bits
PLOOP:
	RLC			;Left bit -> right bit
	PUSH	PSW		;Save right bits
	ANI	1		;keep only right bit
	ADI	'0'		;make it ASCII
	CALL	COUT		;print it
	POP	PSW
	DJNZ	PLOOP	;loop if less than 8 done
	RET
	
;=======================================================
	DSEG
;======================================================

SETW:			;To set the windows
	DB	ESC,SETWNDW
WNR	DB	'6'
	DB	0

VALUE	DW	0	;The value we convert

	END
