; > MemTest writes memory block and tests it
;
        ORG     O1000
START:  MOV     #O1000,R6               ; set up stack
        MOV     #&FFFF,R0               ; test data in R0
LOOP:   MOV     #O20000,R1              ; start address in R1
        MOV     #O40000,R2              ; upper boundary in R2
        JSR     R7,@#WRT                ; write data block
        JSR     R7,@#TST                ; test data block
        COM     R0                      ; toggle all bits
        BR      LOOP                    ; repeat until error
;
WRT:    MOV     R1,R3                   ; save start address for re-use
WRT1:   MOV     R0,(R1)+                ; write a word, and point to next
        CMP     R1,R2                   ; see if finished block
        BNE     WRT1                    ; if not, repeat
        MOV     R3,R1                   ; restore address
        RTS     R7
;
TST:    MOV     R1,R3                   ; save start
TST1:   CMP     R1,R2                   ; see if finished
        BEQ     DONE                    ; if so, go home
        CMP     R0,(R1)+                ; else test and point to next
        BEQ     TST1                    ; if OK, repeat
        DEC     R1                      ; point back...
        DEC     R1                      ; ...to faulty word
        MOV     (R1),R0                 ; copy bad data
        HALT                            ; and return to ODT
        BR      START                   ; if restarting, do it properly
DONE:   MOV     R3,R1                   ; restore pointer
        RTS     R7                      ; and return to sender
