*** MEMORY TEST PROGRAM
*
* This is the Vector Graphic memory test from the 2.0 monitor ROM
*       modified by Mike Douglas, 09/2018 for the Sol-20.
*
* Instructions for use:
*
*   Load and run at CAC0h. This is a hole in SOLOS in RAM on the
*   SOL-20 mainboard. 
*
*   At the "*" prompt, key in four hex characters for the starting
*   address followed by four hex characters for the ending address.
*   The test begins running immediately after the 4th character of
*   the ending address is typed. After each cycle through the
*   specified range, a "." pacifier is displayed. Nothing else is
*   displayed unless errors are detected.
*
*   If a typing error is made while entering the start/end address,
*   press ESC to return to the "*" prompt at which you can re-enter
*   the address parameters.
*
*   The test runs continuously until the computer is reset. The 
*   program generates a pseudo-random number sequence, writes the
*   sequence into RAM, and then regenerates the sequences from the
*   same point to compare with what is read from memory. If the
*   sequence passes, then a new sequence is written to subsequent
*   memory locations. Errors are displayed with the address, what
*   was written to RAM, followed by what was read.

SOUT	equ	0C019h		;SOLOS standard output
SINP	equ	0C01Fh		;SOLOS standard input
ESC	EQU	1BH		;ESC CHARACTER

	ORG	0CAC0h		;RUN IN HOLE IN SOLOS MAINBOARD RAM

START	CALL	CRLF
	MVI	A,'*'
	CALL	PTCN
	JMP	TMEM
*
*** CONVERT UP TO 4 HEX DIGITS TO BIN
*
AHEX	LXI	H,0	;GET 16 BIT ZERO
	MVI	C,4	;COUNT OF 4 DIGITS
AHE1	CALL	RDCN	;READ A BYTE
	DAD	H	;SHIFT 4 LEFT
	DAD	H
	DAD	H
	DAD	H
	SUI	48	;ASCII BIAS
	CPI	10	;DIGIT 0-10
	JC	ALF
	ANI	0DFH	;FORCE UPPER CASE
	SUI	7	;ALPHA BIAS
ALF	ADD	L
	MOV	L,A
	DCR	C	;4 DIGITS?
	JNZ	AHE1	;KEEP READING
	XCHG
SPCE	MVI	A,20H	;PRINT SPACE
PTCN	PUSH	PSW	;SAVE REG A
	PUSH	B	;AND B
	MOV	B,A	;CHARACTER TO DISPLAY IN B
	CALL	SOUT	;DISPLAY IT
	POP	B	
	POP	PSW	;THEN RECOVER A
	RET		;RETURN FROM PTCN
CRLF	MVI	A,0DH	;PRINT CR
	CALL	PTCN
	MVI	A,0AH
	JMP	PTCN
*
*** READ FROM CONSOLE TO REG A. IF ESC ENTERED, RESTART
*      AT THE PROGRAM ENTRY POINT
*	
*
RDCN	CALL	SINP	;CHECK FOR CHARACTER
	JZ	RDCN	;NOTHING TYPED
	ANI	7FH	;STRIP OFF MSB
	CPI	ESC	;ESCAPE PRESSED?
	JZ	START	;YES, START OVER
	JMP	PTCN	;ECHO ONTO PRINTER
*
*** MEMORY TEST ROUTINE
*
TMEM	CALL	AHEX	;READ BLOCK LEN
	CALL	AHEX	;READ START ADDRESS
	LXI	B,5A5AH	;INIT B,C
CYCL	CALL	RNDM
	PUSH	B	;KEEP ALL REGS
	PUSH	H
	PUSH	D
TLOP	CALL	RNDM
	MOV	M,B	;WRITE IN MEM
	CALL	BMP
	JNZ	TLOP	;REPEAT LOOP
	POP	D
	POP	H	;RESTORE ORIG
	POP	B	;VALUES OF
	PUSH	H
	PUSH	D
RLOP	CALL	RNDM	;GEN NEW SEQ
	MOV	A,M	;READ MEM
	CMP	B	;COMP MEM
	CNZ	ERR	;CALL ERROR ROUT
	CALL	BMP
	JNZ	RLOP
	POP	D
	POP	H
	MVI	A,'.'
	CALL	PTCN
	JMP	CYCL
*** THIS ROUTINE GENERATES RANDOM NOS ***
RNDM	MOV	A,B	;LOOK AT B
	ANI	0B4H	;MASK BITS
	ANA	A	;CLEAR CY
	JPE	PEVE	;JUMP IF EVEN
	STC
PEVE	MOV	A,C	;LOOK AT C
	RAL		;ROTATE CY IN
	MOV	C,A	;RESTORE C
	MOV	A,B	;LOOK AT B
	RAL		;ROTATE CY IN
	MOV	B,A	;RESTORE B
	RET		;RETURN W NEW B,C
*
*** ERROR PRINT OUT ROUTINE
*
PTAD	CALL	CRLF	;PRINT CR,LF
	MOV	A,H	;PRINT
	CALL	PT2	;ASCII
	MOV	A,L	;CODES
	CALL	PT2	;FOR
	CALL	SPCE	;ADDRESS
	RET
ERR	PUSH	PSW	;SAVE ACC
	CALL	PTAD	;PRINT ADD.
	MOV	A,B	;DATA
	CALL	PT2	;WRITTEN
	CALL	SPCE
	POP	PSW	;DATA READ
PT2	PUSH	PSW
	CALL	BINH
	POP	PSW
	JMP	BINL
BINH	RAR
	RAR
	RAR
	RAR
BINL	ANI	0FH	;LOW 4 BITS
	ADI	48	;ASCII BIAS
	CPI	58	;DIGIT 0-9
	JC	PTCN
	ADI	7	;DIGIT A-F
	JMP	PTCN
BMP	MOV	A,E
	SUB	L
	JNZ	GOON
	MOV	A,D
	SBB	H
GOON	INX	H
	RET
	END











