;;; -*- Mode:LISP; Package:LAMBDA; Base:8; Fonts:(CPTFONTB) -*-;;;
;;; Copyright LISP Machine, Inc. 1984                          ;;;
;;;   See filename "Copyright" for                             ;;;
;;; licensing and release information.                         ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; in this file we test a new DEF-UTEST option, :UCODE
;;; which is like the :CODE option except it will use the
;;; regular microcode assembler and interface with the
;;; debugger symbol table facilities. how to do this will be
;;; engineered from looking at LAM-RUN-MTEST.
;;; 8/29/84 11:39:56 -George Carrette
;;; At this time the representations used are perverse. the assembler
;;; has been hacked to optionally output a special representation
;;; called LAM-ULOAD looking like:
;;; I 0 1600000000000000141170 
;;; I 1 500000000000000101000 
;;; D 7740 1600000 
;;; The analogous function to our previous UTEST-LOAD-AND-GO
;;; is peformed by the functions LAM-UCODE-LOADER, LAM-RECORD-SYMBOL-TABLE,
;;; LAM-SELECT-SYMBOL-TABLE, FLD-STRAIGHT-MAP, LAM-RESET-CACHE,
;;; ENABLE-CACHE, ENABLE-PARITY etc. (some optional).
;;; The usual register deposites for input values, this time done
;;; symbolically. The process-sleep, and then checking of output values.
;;; The perversity refered to is that LAM-UCODE-LOADER takes as argument
;;; either name of a file to read characters from (synax as above) or
;;; a closure (actually a symbol) to funcall with :TYI to get characters
;;; from. The "loaded" representation of the ucode and symbol table
;;; is an array of characters.

;;; The intent is to preserve the symbol nature of things, but make it
;;; easier to manage, with a single source file and command (QC-FILE!)
;;; for defining tests, while at the same time allowing the automatic
;;; translation into C to take place with symbols possibly thrown out.
;;; (Of course, since there exists SDU C code to read the LAM-ULOAD
;;; format we could keep it. The disadvantage is that using variously
;;; generated output files (instead of the single-input-file single-output-file
;;; model) increases the complexity of our file management, naming and moving
;;; from one file system to another. This is the kind of complexity which
;;; has kept people from using the regular assembler tools in the first
;;; place, therefore we will avoid it, risking running out of executable
;;; space on the SDU instead.)




;;; Memory diagnostic system, includes microcode and debugging
;; function for each test, we step through all memory locations
;; doing a write followed by an immediate read.  We then make a
;; second pass of reads over the memory, verifying that the pattern
;; has been written as expected (the second phase catches refresh
;; problems in memories and also address problems.  Address
;; problems are noticed when data is overwritten by a later part of
;; the test, when the two writes should have been to different
;; addresses

(DEF-UTEST EXPERIMENTAL-MEMORY-DIAGNOSTIC "Should be like LAM-RUN-MTEST"
  ;; we will need some extensions to input values and output values
  ;; with multiple starts and stops, maybe. but for now, have one argument
  ;; TEST which
  :arguments (test)
  :input-values ((M-TEST 'TEST)
		 (A-MEMORY-START 0)
		 (A-MAIN-MEMORY-SIZE 'LAM-MAIN-MEMORY-SIZE))
  :good-stop (memory-test-ok 1)
  :error-stops (((error-wrong-data 1)
		 "incorrect data in the main memory location"))
  :start memory-data-test
  :time-out 60.
  :initializers ()
  :postializers ()
  :switches ()
  :ucode (

 (DEF-DATA-FIELD OAL-MROT 5 0)				;MROT field of microinstruction

 (LOCALITY M-MEM)

M-GARBAGE	(0)
M-HUNOZ		(0)
M-ZERO		(0)
M-MINUS-ONE 
M-ONES		(0)

M-A		(0)
M-B		(0)
M-C		(0)
M-D		(0)
M-S		(0)

M-1	(0)
M-2	(0)

M-LOOP-COUNT	(0)
M-CURRENT-MEMORY-DATA	(0)
M-TEST	                (0)     ;Dispatch offset of current test
M-STARTING-POINT	(0)
M-CURRENT-MEMORY-LOC    (0)	;The memory location being hacked currently


 (LOCALITY A-MEM)

A-GARBAGE	(0)
A-HUNOZ		(0)
A-ZERO		(0)
A-ONES		(0)

A-A		(0)
A-B		(0)
A-C		(0)
A-D		(0)
A-S		(0)

A-1	(0)
A-2	(0)

A-LOOP-COUNT	(0)
A-CURRENT-MEMORY-DATA   (0)     ;The data that should be at VMA
A-TEST	                (0)     ;Dispatch offset of current test
A-STARTING-POINT	(0)	;Starting point of next pass over memory for certain tests
A-CURRENT-MEMORY-LOC    (0)	;The memory location being hacked currently

 (LOC 40)

A-TEM1			(0)
A-TEM2			(0)
A-TEM3			(0)

A-MAIN-MEMORY-START	(0)     ;First memory loc of interest, mapped to vir adr 0
A-MAIN-MEMORY-SIZE      (0)     ;Size of memory under test
A-REPEAT-COUNT		(0)	;Number of times to do the main data loop (decremented til 0)

;Below registers used by test loop TL
A-WA1 (0)	;address for first cycle (write)
A-RA1 (0)	;address for 2nd cycle (read)
A-WA2 (0)	;  " 3rd cycle (write)
A-RA2 (0)       ;  " 4th cycle (read)
A-WD1 (0)       ;data for first write
A-WD2 (37777777777)  ;data for second write 

 (LOCALITY D-MEM)


 (START-DISPATCH 3 (PLUS INHIBIT-XCT-NEXT-BIT P-BIT))

MEMORY-DATA-TESTS-INIT-DATA
	(DATA-ZEROES-INIT)                              ;ZEROES
	(DATA-ONES-INIT)                                ;ONES
	(DATA-FLOATING-ZEROES-INIT)                     ;FLOATING ZEROES
	(DATA-FLOATING-ONES-INIT)                       ;FLOATING ONES
	(DATA-OFFSET-FLOATING-ZEROES-INIT)      	;FLOATING ZEROES, OFFSET PER ADR
	(DATA-OFFSET-FLOATING-ONES-INIT)		;FLOATING ONES, OFFSET PER ADR
	(DATA-FLOATING-ADDRESS-INIT)                    ;DATA IS FLOATING ADDRESS
	(DATA-FLOATING-ADDRESS-COM-INIT)		;DATA IS FLOATING COM OF ADDRESS

 (END-DISPATCH)


 (START-DISPATCH 3 (PLUS INHIBIT-XCT-NEXT-BIT P-BIT))

MEMORY-DATA-TESTS-INIT-ADDRESS
	(R-BIT)                                         ;ZEROES, DROP THROUGH
	(R-BIT)                                         ;ONES
	(R-BIT)                                         ;FLOATING ZEROES
	(R-BIT)                                         ;FLOATING ONES
	(DATA-OFFSET-FLOATING-ZEROES-ADR-INIT)          ;FLOATING ZEROES OFFSET
	(DATA-OFFSET-FLOATING-ONES-ADR-INIT)            ;FLOATING ONES OFFSET
	(R-BIT)                                         ;DATA IS ADDRESS
	(R-BIT)                                         ;DATA IS COMPLEMENT OF ADDRESS


 (END-DISPATCH)


 (START-DISPATCH 3 (PLUS INHIBIT-XCT-NEXT-BIT P-BIT))

MEMORY-DATA-TESTS-STEP-DATA
	(MEMORY-TEST-OK)                                ;ZEROES
	(MEMORY-TEST-OK)                                ;ONES
	(DATA-FLOATING-ZEROES-STEP)                     ;FLOATING ZEROES
	(DATA-FLOATING-ONES-STEP)                       ;FLOATING ONES
	(DATA-OFFSET-FLOATING-ZEROES-STEP)              ;FLOATING ZEROES OFFSET
	(DATA-OFFSET-FLOATING-ONES-STEP)		;FLOATING ONES OFFSET
	(DATA-FLOATING-ADDRESS-STEP)			;DATA IS ADDRESS
	(DATA-FLOATING-ADDRESS-COM-STEP)		;DATA IS COMPLEMENT OF ADDRESS

 (END-DISPATCH)


 (START-DISPATCH 3 (PLUS INHIBIT-XCT-NEXT-BIT P-BIT))

MEMORY-DATA-TESTS-STEP-ADDRESS
	(R-BIT)                                         ;ZEROES, DROP THROUGH
	(R-BIT)                                         ;ONES
	(R-BIT)                                         ;FLOATING ZEROES
	(R-BIT)                                         ;FLOATING ONES
	(DATA-OFFSET-FLOATING-ZEROES-ADR-STEP)          ;FLOATING ZEROES OFFSET
	(DATA-OFFSET-FLOATING-ONES-ADR-STEP)            ;FLOATING ONES OFFSET
	(DATA-FLOATING-ADDRESS-ADR-STEP)		;DATA IS ADDRESS
	(DATA-FLOATING-ADDRESS-COM-ADR-STEP)		;DATA IS COMPLEMENT OF ADDRESS

 (END-DISPATCH)


 (LOCALITY I-MEM)

 (LOC 0)

;;; Main diagnostic loop, data tests

MEMORY-DATA-TEST				;perform init for the test
	((M-ONES) SETO)				; - executed only once
	((M-ZERO) SETZ)
	(DISPATCH MEMORY-DATA-TESTS-INIT-DATA (BYTE-FIELD 3 0) M-TEST)
						;set the value of a-repeat-count
	(NO-OP)


;; this is the the begining of the main loop, which is done until a-repeat-count
;; has been decremented to zero. 
MEMORY-DATA-TEST-1
	((M-CURRENT-MEMORY-LOC) A-MAIN-MEMORY-START)
						;start from the beginning memory loc
						;init the address for tests 4 and 5
						;drop through the dispatch for all others
	(DISPATCH MEMORY-DATA-TESTS-INIT-ADDRESS (BYTE-FIELD 3 0) M-TEST)
	(NO-OP)
	((M-LOOP-COUNT) SETZ)

MEMORY-DATA-LOOP-1
        (DISPATCH MEMORY-DATA-TESTS-STEP-ADDRESS (BYTE-FIELD 3 0) M-TEST)
						;step the address for tests 4-7
						;drop through for tests 0-3
	(NO-OP)
	((VMA) M-CURRENT-MEMORY-LOC)
	((MD-START-WRITE) M-CURRENT-MEMORY-DATA)        ;WRITE APPROPRIATE DATA
	(JUMP-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)
	((VMA-START-READ) M-CURRENT-MEMORY-LOC) 	;NOW READ BACK THE DATA IMMEDIATLY
	(JUMP-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)

MEMORY-DATA-IMMEDIATE-READBACK
	(CALL-NOT-EQUAL-XCT-NEXT MD A-CURRENT-MEMORY-DATA ERROR-WRONG-DATA)
						;test for failure on immediate readback
       ((M-LOOP-COUNT) M+A+1 M-LOOP-COUNT A-ZERO)
						;increment loop counter
	(JUMP-LESS-THAN-XCT-NEXT M-LOOP-COUNT A-MAIN-MEMORY-SIZE MEMORY-DATA-LOOP-1)
						;jump to top of loop unless we've run
						;out of memory
       ((M-CURRENT-MEMORY-LOC) M+A+1 M-CURRENT-MEMORY-LOC A-ZERO)
						;increment memory location counter

	;; NOW WE HAVE WRITTEN A DATA PATTERN, READ IT BACK AND INSURE IT IS CORRECT
	;; - identical to memory-data-loop except that we only read

MEMORY-CHECK-LOOP
	((M-CURRENT-MEMORY-LOC) A-MAIN-MEMORY-START)
	(DISPATCH MEMORY-DATA-TESTS-INIT-ADDRESS (BYTE-FIELD 3 0) M-TEST)
	(NO-OP)
	((M-LOOP-COUNT) SETZ)

MEMORY-CHECK-LOOP-1
	(DISPATCH MEMORY-DATA-TESTS-STEP-ADDRESS (BYTE-FIELD 3 0) M-TEST)
	(NO-OP)
	((VMA-START-READ) M-CURRENT-MEMORY-LOC)
	(JUMP-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)

MEMORY-CHECK
	(CALL-NOT-EQUAL-XCT-NEXT MD A-CURRENT-MEMORY-DATA ERROR-WRONG-DATA)
       ((M-LOOP-COUNT) M+A+1 M-LOOP-COUNT A-ZERO)
	(JUMP-LESS-THAN-XCT-NEXT M-LOOP-COUNT A-MAIN-MEMORY-SIZE MEMORY-CHECK-LOOP-1)
       ((M-CURRENT-MEMORY-LOC) M+A+1 M-CURRENT-MEMORY-LOC A-ZERO)
	(DISPATCH MEMORY-DATA-TESTS-STEP-DATA (BYTE-FIELD 3 0) M-TEST)
	(NO-OP)
	(JUMP-LESS-THAN-XCT-NEXT M-ZERO A-REPEAT-COUNT MEMORY-DATA-TEST-1)
       ((A-REPEAT-COUNT) ADD M-MINUS-ONE A-REPEAT-COUNT)

MEMORY-TEST-OK					;you get here if you fall through the
						;memory check loop, or by an explicit
						;dispatch in the case of tests 0,1
	(JUMP HALT-CONS MEMORY-TEST-OK)
	(NO-OP)

ERROR-BAD-PAGE-FAULT
	(JUMP HALT-CONS ERROR-BAD-PAGE-FAULT)
	(NO-OP)

ERROR-WRONG-DATA
	(NO-OP HALT-CONS)                               ;DATA ERROR, CONTINUABLE
	(POPJ)
	(NO-OP)		;SO PC SAYS ERROR-WRONG-DATA INSTEAD OF DATA-ZEROS WHEN IT STOPS

;;; Data tests

;;	the zeroes test writes 0 in all memory locations and then checks
DATA-ZEROES 

DATA-ZEROES-INIT
	(POPJ-AFTER-NEXT (A-REPEAT-COUNT) M+A+1 M-ZERO A-ZERO)
	((M-CURRENT-MEMORY-DATA) SETZ)

DATA-ONES
;;	the ones test writes -1 in all locations and then checks

DATA-ONES-INIT
	(POPJ-AFTER-NEXT (A-REPEAT-COUNT) M+A+1 M-ZERO A-ZERO)
	((M-CURRENT-MEMORY-DATA) SETO)


DATA-FLOATING-ZEROES
;;	the floating zeroes test inits current-memory-data to 17777777777
;;	and then rotates current-memory-data on each step.  we get a pattern in which
;;	the bits walk as the addresses increment

DATA-FLOATING-ZEROES-INIT
	(POPJ-AFTER-NEXT (A-REPEAT-COUNT) (A-CONSTANT 32.))
	((M-CURRENT-MEMORY-DATA) (BYTE-FIELD 37 0) M-ONES A-ZERO)

DATA-FLOATING-ZEROES-STEP
	((M-CURRENT-MEMORY-DATA) (BYTE-FIELD 40 1) M-CURRENT-MEMORY-DATA)
	(POPJ)


DATA-FLOATING-ONES
;;	the floating ones test is like the floating zeroes except that we init
;;	current-memory-data to 20000000000 

DATA-FLOATING-ONES-INIT
	(POPJ-AFTER-NEXT (A-REPEAT-COUNT) (A-CONSTANT 32.))
	((M-CURRENT-MEMORY-DATA) DPB (BYTE-FIELD 1 37) M-ONES A-ZERO)

DATA-FLOATING-ONES-STEP
	((M-CURRENT-MEMORY-DATA) (BYTE-FIELD 40 1) M-CURRENT-MEMORY-DATA)
	(POPJ)


DATA-OFFSET-FLOATING-ZEROES
;;	the diference between the floating tests and the offset floating tests
;;	is that m-starting-point is initialized by the same al

DATA-OFFSET-FLOATING-ZEROES-INIT
	(POPJ-AFTER-NEXT (M-STARTING-POINT) (BYTE-FIELD 37 0) M-ONES A-ZERO)
	((A-REPEAT-COUNT) (A-CONSTANT 32.))

DATA-OFFSET-FLOATING-ZEROES-ADR-INIT
	((M-CURRENT-MEMORY-DATA) M-STARTING-POINT)
	(POPJ)

DATA-OFFSET-FLOATING-ZEROES-ADR-STEP
	((M-CURRENT-MEMORY-DATA) (BYTE-FIELD 40 1) M-CURRENT-MEMORY-DATA)
	(POPJ)

DATA-OFFSET-FLOATING-ZEROES-STEP
	((M-STARTING-POINT) (BYTE-FIELD 40 1) M-STARTING-POINT)
	(POPJ)


DATA-OFFSET-FLOATING-ONES 

DATA-OFFSET-FLOATING-ONES-INIT
	(POPJ-AFTER-NEXT (M-STARTING-POINT) DPB (BYTE-FIELD 1 37) M-ONES A-ZERO)
	((A-REPEAT-COUNT) (A-CONSTANT 32.))

DATA-OFFSET-FLOATING-ONES-ADR-INIT
	((M-CURRENT-MEMORY-DATA) M-STARTING-POINT)
	(POPJ)

DATA-OFFSET-FLOATING-ONES-ADR-STEP
	((M-CURRENT-MEMORY-DATA) (BYTE-FIELD 40 1) M-CURRENT-MEMORY-DATA)
	(POPJ)

DATA-OFFSET-FLOATING-ONES-STEP
	((M-STARTING-POINT) (BYTE-FIELD 40 1) M-STARTING-POINT)
	(POPJ)


DATA-FLOATING-ADDRESS 

DATA-FLOATING-ADDRESS-INIT
	(POPJ-AFTER-NEXT (M-STARTING-POINT) SETZ)
	((A-REPEAT-COUNT) (A-CONSTANT 32.))

DATA-FLOATING-ADDRESS-ADR-STEP
	(POPJ-AFTER-NEXT (OA-REG-LOW) DPB M-STARTING-POINT OAL-MROT A-ZERO)
	((M-CURRENT-MEMORY-DATA) LDB (BYTE-FIELD 40 0)
				 M-CURRENT-MEMORY-LOC A-ZERO)

DATA-FLOATING-ADDRESS-STEP
	((M-STARTING-POINT) M+A+1 M-STARTING-POINT A-ZERO)
	(POPJ)

DATA-FLOATING-ADDRESS-COM 

DATA-FLOATING-ADDRESS-COM-INIT
	(POPJ-AFTER-NEXT (M-STARTING-POINT) SETZ)
	((A-REPEAT-COUNT) (A-CONSTANT 32.))
						;make starting point = 0 and
						;a-repeat-count = 32.

DATA-FLOATING-ADDRESS-COM-ADR-STEP
	((OA-REG-LOW) DPB M-STARTING-POINT OAL-MROT A-ZERO)
						;substitute m-starting-point as the
						;mrot value for the subsequent instruction
	(POPJ-AFTER-NEXT (M-CURRENT-MEMORY-DATA) LDB (BYTE-FIELD 40 0)
						 M-CURRENT-MEMORY-LOC A-ZERO)
						;make the current-memory-data
						;the rotated current-memory-loc
	((M-CURRENT-MEMORY-DATA) SETCM M-CURRENT-MEMORY-DATA)
						;then complement the data

DATA-FLOATING-ADDRESS-COM-STEP
	((M-STARTING-POINT) M+A+1 M-STARTING-POINT A-ZERO)	;increment starting-point
	(POPJ)

;separate little test loop

TL	((MD) A-WD1)
	((VMA-START-WRITE) A-WA1)
        (CALL-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)
	((VMA-START-READ) A-RA1)
        (CALL-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)
	((MD) A-WD2)
	((VMA-START-WRITE) A-WA2)
        (CALL-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)
	((VMA-START-READ) A-RA2)
        (CALL-IF-PAGE-FAULT ERROR-BAD-PAGE-FAULT)
	(JUMP TL)
	(NO-OP)

))	;END OF MICROCODE

