	.IF	LIST9
	.LIST
	.PAGE
	.ELSE
	.NOLIST
	.ENDC
;****************************************************************************
;
;	Hardware Tests & Non RAM Terminal I/O
;
;	File:	SAGE.PROM9.TEXT
;	Date:	23-Mar-83
;	Issue:	3
;
;	COPYRIGHT (c) 1982, 1983 SAGE Computer Technology
;	All Rights Reserved.
;
;****************************************************************************
;
;	Release History:
;
;	1     13-Jun-82 Initial release.
;	2     21-Sep-82 Modified NRCHAR to blink LED instead of hang on
;			terminal failure.
;	3     23-Mar-83 Initial SAGE IV release.
;
;****************************************************************************



.PAGE
;****************************************************************************
;
;	Check memory locations.
;
;	Calling Sequence:
;	  A0 contains base of memory to be checked.
;	  A1 contains top of memory to be checked (inclusive).
;	  A2 contains return address from MEMCHK routine.
;
;	The MEMCHK routine only returns if the memory is good.
;	  D0, D1 and A3 are not preserved.
;
;	The MEMCHK routine stores and checks four byte (long word) values
;	of all zero bits, all one bits, and then stores the memory address.
;	Once all the addresses are written, the stored addresses are
;	verified to make sure that the wrong addresses were not disturbed.
;
;	If an error occurs a message is sent to the console.
;
;	   Bad Memory @ XXXXXXXX is YYYYYYYY instead of ZZZZZZZZ
;
;****************************************************************************

MEMCHK	MOVEA.L A0,A3		;A3 is working pointer
	CLR.L	D0
	CLR.L	D1
	NOT.L	D1		;D1 := 0FFFFFFFFH  Test Pattern

$10	MOVE.L	D0,(A3)		;Store zero
	TST.L	(A3)
	BNE.S	$30		;Failed
	MOVE.L	D1,(A3)		;Store all ones
	CMP.L	(A3),D1
	BNE.S	$50		;Failed
	MOVE.L	A3,(A3)+	;Store address for memory address test
	CMPA.L	A1,A3
	BLS.S	$10		;Try next location

;	Now check addresses
	MOVEA.L A0,A3		;Base address
$20	CMPA.L	(A3),A3
	BNE.S	$40		;Failed address test
	ADDQ.L	#4,A3
	CMPA.L	A1,A3
	BLS.S	$20		;Try previous location
	CLR.L	D0		;Clear error
	JMP	(A2)		;Successful memory test completed, return
	
;	Failed memory test (should have been 0)
$30	MOVE.L	D0,D1		;Save expected value
	BRA.S	$50

;	Failed address test
$40	MOVE.L	A3,D1		;Save expected value
	
;	Failed memory test (should have been 1)
$50	MOVE.L	(A3),D0		;Get value from RAM
	
	LEA	MSG13,A4	;Printout BAD MEMORY @
	LEA	$60,A5		;Return address
	BRA	NRTEXT		;Printout string

$60	MOVE.L	A3,D5		;Printout address
	LEA	$70,A5		;Return address
	BRA	NRHEX		;Printout value
	
$70	LEA	MSG15,A4	;Printout ' is '
	LEA	$80,A5		;Return address
	BRA	NRTEXT		;Printout string
	
$80	MOVE.L	D0,D5		;Printout actual memory value
	LEA	$90,A5		;Return address
	BRA	NRHEX		;Printout value
	
$90	LEA	MSG16,A4	;Printout ' instead of '
	LEA	$100,A5		;Return address
	BRA	NRTEXT		;Printout string
	
$100	MOVE.L	D1,D5		;Printout expected memory value
	LEA	$105,A5		;Return address
	BRA	NRHEX		;Printout value

$105	LEA	MSG22,A4	;Printout ' (Test Aborted)'
	LEA	$110,A5		;Return address
	BRA	NRTEXT
	
$110	LEA	MSG17,A4	;Printout <CR> <LF>
	LEA	$120,A5		;Return address
	BRA	NRTEXT		;Printout string

$120	MOVEQ	#1,D0		;Set error flag
	JMP	(A2)		;Terminate operations


.PAGE
;****************************************************************************
;
;	Terminal Output Routines Without RAM
;
;	NRCHAR	- Character output routine.
;		  Outputs character in D6.
;		  Return address in A6.
;		  Also uses D7.
;
;	NRTEXT	- Text string output routine.
;		  Outputs string pointed to by A4.
;		  (string ends with zero byte)
;		  Return address in A5.
;		  Also uses D6, D7, A6.
;
;	NRHEX	- Long word hex value output routine.
;		  Outputs long word from D5.
;		  Return address in A5.
;		  Also uses D4, D6, D7, A6.
;
;****************************************************************************

;	Character output (without using RAM)
NRCHAR
	MOVE.W	#0FFFFH,D7	;Timeout count
$10	BTST	#2,TERM_S	;Check if transmitter empty
	DBNE	D7,$10
	BEQ.S	$20		;Had character timeout, just STOP
	MOVE.B	D6,TERM_O	;Output character
	MOVEQ	#14.,D7
$15	DBF	D7,$15		;Short delay > 14 microseconds
	BTST	#2,TERM_S	;Check if transmitter not empty
	BNE.S	$20		;Had lockup
	JMP	(A6)		;Return
	
;	Terminal port hung
$20	BCHG	#3,C8255+4	;Toggle the LED
$30	DBF	D7,$30
	BRA	$20

	

;	String output (without using RAM)
NRTEXT	MOVE.B	(A4)+,D6	;Get next character
	BEQ.S	$10		;Done with string
	LEA	NRTEXT,A6	;Return address
	BRA.S	NRCHAR		;Output the character

$10	JMP	(A5)		;Return from string routine


;	Hex long word output (without using RAM)
NRHEX
	MOVEQ	#8-1,D4		;Digit count
$10	ROL.L	#4,D5
	MOVEQ	#0FH,D6
	AND.B	D5,D6		;Isolate digit
	CMPI.B	#9,D6
	BLS.S	$20		;Was in decimal range
	ADDI.B	#"A"-"0"-10.,D6
$20	ADDI.B	#"0",D6
	LEA	$30,A6		;Return address
	BRA.S	NRCHAR		;Printout a digit

$30	DBF	D4,$10
	JMP	(A5)		;Return from NRHEX

                                                                             