; DMTRam.asm
; Copyright Xerox Corporation 1979
; Last modified April 1, 1980  12:48 AM by Boggs

; Outgoing
.ENT	INITRAMTEST, RAMTEST, PRINTRAMTEST
.BEXTZ	RAMFLAG

; Incoming
.BEXTZ	PDEC, PMSG1, ALTO2, SOM, ERROR

	.SREL

INITRAMTEST:	.INITRAMTEST
RAMTEST:	.RAMTEST
PRINTRAMTEST:	.PRINTRAMTEST

RANDOM:		.RANDOM
COUNTBADCHIPS:	.COUNTBADCHIPS

	.ZREL

RAMFLAG:	0		; 0 = NONE, 1 = 1K, 3 = 3K

pSBCTAB:	.SBCTAB		; START OF BAD CHIP TABLE
pEBCTAB:	.EBCTAB		; END OF BAD CHIP TABLE

pCHIPTAB:	0		; -> TO ONE OF THE FOLLOWING
pCHIPTAB1:	.CHIPTAB1	; ALTO I BIT-TO-CHIP CONVERSION TABLE
pCHIPTAB2:	.CHIPTAB2	; 2K ROM BIT-TO-CHIP CONVERSION TABLE
pCHIPTAB3:	.CHIPTAB3	; 3K RAM BIT-TO-CHIP CONVERSION TABLE

	.NREL

;----------------------------------------------------------------------------
.INITRAMTEST:
;----------------------------------------------------------------------------
	STA 3 IRRET

	MKZERO 0 0		; ZERO THE BAD CHIP TABLE
	LDA 1 pEBCTAB
	LDA 3 M32D
	BLKS

	MKONE 0 0
	STA 0 RAMFLAG
	JSRII ppRAMTEST		; DO A CURSORY TEST
	MKZERO 0 0
	STA 0 RAMFLAG

	JSRII ppCOUNTBADCHIPS
	LDA 1 M32D
	ADD 0 1 SNR
	 JMP @IRRET		; NO RAM
	MKONE 0 0
	STA 0 RAMFLAG		; AT LEAST 1K - MAYBE MORE

; DETERMINING THE SIZE OF THE RAM IS A BIT TRICKY WHEN IT MAY NOT BE
; WORKING CORRECTLY.  BY THIS POINT WE KNOW WE HAVE AT LEAST 1K OF RAM.
; WRITE A TEST VALUE INTO LOCATION 0 IN BANK 0 AND THE COMPLEMENT OF THE
; TEST VALUE INTO LOCAITON 0 OF BANK 1.  READ THEM BACK AND COMPARE
; THEM - IF THEY ARE THE SAME, THEN THIS IS A 1K RAM, BUT IF THEY ARE
; DIFFERENT, THIS IS A 3K RAM.  NOTE THAT WE DON'T TEST THAT THE VALUES
; WE READ BACK ARE EQUAL TO THE TESTVAL, SINCE SOME BITS MAY BE FAILING.
; THIS METHOD ASSUMES THAT A BROKEN LOCATION READS BACK THE SAME VALUE
; WHEN READ TWICE WITHOUT WRITING IN BETWEEN.
	LDA 0 TESTVAL
	MKZERO 1 1
	LDA 3 TESTVAL
	61012			; WRITE TESTVAL INTO BANK 0
	LDA 2 C10000
	ADD 2 1			; BANK FIELD ← 1
	COM 0 0			; INVERT TESTVAL
	COM 3 3			; INVERT TESTVAL
	61012			; WRITE NOT TESTVAL INTO BANK 1
	61011			; READ FROM BANK 1
	MOV 0 3
	COM 2 2
	AND 2 1
	61011			; READ FROM BANK 0
	SNE 0 3
	 JMP ONEK		; 1K - NOW MUST DECIDE ALTO TYPE
	LDA 0 pCHIPTAB3		; 3K RAM
	STA 0 pCHIPTAB
	ISZ RAMFLAG
	ISZ RAMFLAG		; RAMFLAG ← 3
	JMP @IRRET

; 1K RAM.  STILL MUST DECIDE WHICH BIT-TO-CHIP TABLE TO USE
ONEK:	LDA 0 ALTO2
	LDA 1 pCHIPTAB2
	SNZ 0 0
	 LDA 1 pCHIPTAB1	; ALTO 1
	STA 1 pCHIPTAB
	JMP @IRRET

TESTVAL:		125252
C10000:			10000
M32D:			-32.
IRRET:			0
ppRAMTEST:		RAMTEST
ppCOUNTBADCHIPS:	COUNTBADCHIPS

;----------------------------------------------------------------------------
.RAMTEST:
;----------------------------------------------------------------------------
	LDA 0 RAMFLAG
	SNZ 0 0
	 JMP 0,3
	STA 3 RTRET

	MKZERO 0 0
	STA 0 BANK
	JSR .TESTRAMBANK
	MKONE 1 1
	LDA 0 RAMFLAG
	SNE 0 1			; DONE IF RAMFLAG IS ONE
	 JMP @RTRET
	ISZ BANK
	JSR .TESTRAMBANK
	ISZ BANK
	JSR .TESTRAMBANK
	JMP @RTRET

RTRET:		0
BANK:		0

;----------------------------------------------------------------------------
.COUNTBADCHIPS:
;----------------------------------------------------------------------------
	STA 3 CBCRET
	MKZERO 0 0
	LDA 1 D32		; LOOP COUNTER
	NEG 1 1
	LDA 2 pSBCTAB		; ERRTAB!I NONZERO MEANS ERROR

CBC:	LDA 3 0,2		; CHIP ERROR COUNTER
	SZ 3 3			; ANY ERRORS?
	 INC 0 0		; YES
	INC 2 2			; TABLE INDEX
	INC 1 1 SZR		; DONE?
	 JMP CBC		; NO

	JMP @CBCRET

CBCRET:		0

;----------------------------------------------------------------------------
.TESTRAMBANK:
;----------------------------------------------------------------------------
	STA 3 TRBRET		; RETURN

; INITIALIZE THE RANDOM NUMBER TABLE

	LDA 2 SOM		; USE SOME OF THE TEST AREA
	LDA 0 D2048
	STA 0 RCOUNT

TR1:	JSRII ppRANDOM
	STA 0 0,2
	INC 2 2
	DSZ RCOUNT
	 JMP TR1

; WRITE RANDOM NUMBERS INTO THE RAM

	LDA 2 SOM
	LDA 0 BANK
	CYCLE 12.
	MOV 0 1
	LDA 0 D1024
	STA 0 RCOUNT

TR2:	LDA 3 0,2		; LOW HALF
	INC 2 2
	LDA 0 0,2		; HIGH HALF
	INC 2 2
	61012			; WRTRAM
	INC 1 1			; RAM ADDRESS
	DSZ RCOUNT
	 JMP TR2

; READ IT BACK AND COMPARE

	LDA 0 D2048
	STA 0 RCOUNT
	MKZERO 3 3		; TABLE INDEX 0..2047

TR3:	LDA 2 C2000		; FORM RAM ADDRESS FROM TABLE INDEX
	MOVZR 3 1 SZC
	 ADD 2 1
	LDA 0 BANK
	CYCLE 12.
	ADD 0 1
	LDA 2 SOM		; TABLE BASE
	ADD 3 2
	LDA 2 0,2		; RANDOM NUMBER FROM TABLE
	61011			; RDRAM
	SNE 0 2
	 JMP TR4
	JSR RAMERROR
	LDA 3 D2048
	LDA 2 RCOUNT
	SUB 2 3
TR4:	INC 3 3
	DSZ RCOUNT
	 JMP TR3

;CLEAR IT OUT

	MKZERO 0 0		; VALUE TO BE BLOCK STORED
	LDA 3 D2048
	LDA 1 SOM
	ADD 3 1			; LAST WORD OF DEST AREA
	NEG 3 3			; - WORD COUNT
	BLKS
	JMP @TRBRET

ppRANDOM:	RANDOM
TRBRET:		0
RCOUNT:		0
D1024:
C2000:		2000
D2048:
C4000:		4000

;----------------------------------------------------------------------------
RAMERROR:		; DIFFERING WORDS IN AC0 AND AC2, ADDRESS IN AC1
;----------------------------------------------------------------------------
; ALTOIICODE2 HAS A BUG WHICH WILL OCCASIONALLY MAKE RDRAM FAIL.
; IT HAS BEEN FIXED IN VERSION 3.  IF RETRYING THE RDRAM WORKS,
; IT IS COUNTED AS A 'FLAKY RDRAM' AND NOT ANALYZED.

	61011			; TRY THE READ AGAIN
	SE 0 2			; DID IT WORK THIS TIME?
	 JMP RE2		; NO.  SEEMS TO BE A SOLID FAILURE
	ISZ FLAKYREADS		; COUNT THEM
	 JMP .+2
	DSZ FLAKYREADS		; CAN'T SKIP
	JMP 0 3

; COME HERE IF WE REALLY SEEM TO HAVE A BAD CHIP

RE2:	STA 3 RERET
	MOV 0 3			; AC0 ← AC0 XOR AC2.  AC3 IS TEMP
	ANDZL 2 3
	ADD 2 0
	SUB 3 0

	LDA 2 C2000
	AND# 2 1 SZR		; HIGH HALF?
	 MKZERO 2 2 SKP		; YES
	LDA 2 D16		; NO
	LDA 1 pSBCTAB		; TABLE BASE
	ADD 1 2
RE1:	MOVZL 0 0 SZC
	 ISZ 0 2		; THIS CHIP IS BAD
	  JMP .+2
	 DSZ 0,2		; CAN'T SKIP
	INC 2 2
	SZ 0 0			; FOUND ALL OF THE DATA BITS?
	 JMP RE1		; NO

	JSR @ERROR		; NOTIFY REPORT GENERATOR
	JMP @RERET		; ALL DONE

RERET:		0
D16:		16.
D32:		32.
FLAKYREADS:	0

;----------------------------------------------------------------------------
.PRINTRAMTEST:
;----------------------------------------------------------------------------
	LDA 0 RAMFLAG
	SNZ 0 0
	 JMP 0,3

	STA 3 PRRET
	JSR .COUNTBADCHIPS
	STA 0 ERRCNT
	MOV 0 1
	JSR @PDEC
	JSR @PMSG1
	 .TXT " Bad control memory chips*N"

	LDA 0 ERRCNT
	SNZ 0 0
	 JMP FRR
	JSR @PMSG1
	 .TXT "*N Chip Errors*N"

	MKZERO 0 0
	STA 0 BITNO
PRLOOP:	LDA 2 pSBCTAB
	LDA 1 BITNO		; FOR THIS BIT (CHIP)
	ADD 1 2
	LDA 0 0,2
	SNZ 0 0			; IF ZERO, DON'T PRINT ANYTHING
	 JMP PRNEXT
	STA 0 ERRCNT
	LDA 2 pCHIPTAB
	LDA 1 BITNO
	ADD 1 2
	LDA 1 0,2
	JSR @PDEC		; CHIP NO
	JSR @PMSG1
	 .TXT "  "
	LDA 1 ERRCNT		; ERRORS
	JSR @PDEC
	JSR @PMSG1
	 .TXT "*N"

PRNEXT:	ISZ BITNO
	LDA 0 BITNO
	LDA 1 D31
	SGTU 0 1
	 JMP PRLOOP

FRR:	LDA 1 FLAKYREADS
	SNZ 1 1			; ANY FLAKY RDRAMS?
	 JMP @PRRET		; NO
	JSR @PMSG1
	 .TXT "*N"
	LDA 1 FLAKYREADS
	JSR @PDEC
	JSR @PMSG1
	 .TXT " flakey RDRAMs*N"
	JMP @PRRET

D31:		31.
D15:		15.
PRRET:		0
BITNO:		0
ERRCNT:		0

;----------------------------------------------------------------------------
; Random()
; Returns a 16-bit random number using the generator
;	x[n] = (x[n-33] + x[n-13]) mod 2↑16
;----------------------------------------------------------------------------

.RANDOM:	sta 3 RanRet
	jsr ran1

	160315
	 34255
	 43770
	104071
	 21360
	135442
	 45545
	106565
	  6714
	133667
	176741
	 12402
	114375
	 36624
	 34427
	105045
	146515
	 63276
	 36434
	 67771
	127054
	 77772
	 26244
	171113
	164223
	114267
	132355
	146005
	 44600
	 41705
	144466
	131235
	 31377

ran1:	lda 0 index		; Get index (counts toward 0)
	add 0 3			; Point to table entry +1
	lda 1 d20		; Is x[n-13] ahead or behind us in table?
	sgt 0 1
	 jmp ran2
	lda 1 -21.,3		; x[n-13] from ahead of us (lower address)
	jmp ran3

ran2:	lda 1 12.,3		; x[n-13] from behind us (higher address)
ran3:	lda 0 -1,3		; Add x[n-33]
	add 1 0
	sta 0 -1,3		; Result is x[n]
	lda 3 RanRet
	dsz index		; Advance index
	 jmp 0,3
	lda 1 d33		; Wrap around
	sta 1 index
	jmp 0,3

index:	33.			; Current table index [1..33]
d20:	20.
d33:	33.
RanRet:	0

;----------------------------------------------------------------------------
; CHIP AND ERROR TABLES
;----------------------------------------------------------------------------

.SBCTAB:	.BLK 32.	; 1ST WORD IS MSB (BIT 0 OF HIGH HALF)
.EBCTAB = .-1

.CHIPTAB1:			; ALTO I BIT-TO-CHIP TABLE
	73.			; RSEL.0 = BIT 0 = CHIP 73
	63.
	53.
	43.			; RSEL.4
	75.			; ALUF.0
	65.
	55.
	45.			; ALUF.3
	77.			; BS.0
	67.
	57.			; BS.2
	47.			; F1.0
	79.
	69.
	59.
	49.			; F1.3
	74.			; F2.0 = BIT 17 = CHIP 74
	64.
	54.
	44.			; F2.3
	76.			; LOADT
	66.			; LOADL
	56.			; NEXT.00
	46.
	78.
	68.
	58.
	48.
	80.
	70.
	60.
	50.			; NEXT.09 = BIT 31 = CHIP 50

.CHIPTAB2:			; 2K ROM BIT-TO-CHIP TABLE
	74.			; RSEL.0 = BIT 0 = CHIP 74
	86.
	62.
	50.
	75.			; RSEL.4
	87.			; ALUF.0
	63.
	51.
	76.			; ALUF.3
	88.			; BS.0
	64.
	52.			; BS.2
	73.			; F1.0
	85.
	49.
	61.			; F1.3
	84.			; F2.0 = BIT 17 = CHIP 84
	72.
	48.
	60.			; F2.3
	69.			; LOADT
	81.			; LOADL
	57.			; NEXT.00
	45.
	70.
	82.
	58.
	46.
	83.
	59.
	47.
	71.			; NEXT.09 = BIT 31 = CHIP 71

.CHIPTAB3:			; 3K RAM BIT-TO-CHIP TABLE
	93.			; RSEL.0 = BIT 0 = CHIP 74
	80.
	67.
	54.
	94.			; RSEL.4
	81.			; ALUF.0
	68.
	55.
	95.			; ALUF.3
	82.			; BS.0
	69.
	56.			; BS.2
	92.			; F1.0
	79.
	66.
	53.			; F1.3
	90.			; F2.0 = BIT 17 = CHIP 84
	77.
	64.
	51.			; F2.3
	89.			; LOADT
	76.			; LOADL
	63.			; NEXT.00
	50.
	88.
	75.
	62.
	49.
	91.
	78.
	65.
	52.			; NEXT.09 = BIT 31 = CHIP 71


	.END