*
.   logical address memory test
.
memtest:   proc

data       equ     8                   length of local data

startad    equ     0-data              memory test start address
endaddr    equ     2-data              memory test ending address
shiftct    equ     4-data              data value shift counter
passctr    equ     5-data              test pass counter
oldbyte    equ     6-data              value of byte in memory
limit      equ     7-data              pass counter limit
*
.   set up the stack and initialize counters
.
           mark                        save pointers to stack and stuff
           leas    -data,s             allocate local data area
           clr     passctr,u           initialize the pass counter
           clr     shiftct,u           then clear the shift counter
           clr     limit,u
*
.   get range of addresses and validate them
.
           bsr     getword             get starting address
           bcs     abort               if not hex, abort the test
           sty     startad,u           else save starting address
           leax    hyphen,pc           point to separator
           bsr     pdata               to denote the range of addresses
           bsr     getword             get memory ending address
           bcs     abort               if not hex, abort
           sty     endaddr,u           save the ending address
           bsr     outsp               dump out a trailing space
           cmpy    startad,u           compare against start address
           bhs     nextpass            if range is correct, do the test
abort      exit
*
.   internal Q-test
.
qtest:
           mark
           leas    -data,s
           stx     startad,u           save starting address
           cmpy    startad,u           check for reasonable boundaries
           blo     abort               if not, abort
           sty     endaddr,u           save ending address
           stb     limit,u             save pass count
*
.   perform a pass on memory
.
nextpass   lda     #"+"                display a "+" for each pass
           bsr     outch               to tell dude test is alive
           lda     limit,u             get pass count limit
           cmpa    #0                  if limit is zero, ignore
           beq     skip1               the pass checking
           cmpa    passctr,u           check for done enough passes
           beq     abort
skip1      inc     passctr,u           increment the pass counter
           ldy     startad,u           get the starting address
           bsr     incheck             check for interrupted test
           beq     nextbyte            if not, just continue
           exit
*
.   process the next location
.
nextbyte   pshs    y                   stuff address on stack
           lda     0,s+                get msp of address value
           adda    0,s+                add lsp off the stack
           adda    passctr,u           add in the pass counter, also
           sta     0,y                 store the byte into memory
           cmpy    endaddr,u           compare to end address
           beq     checkit             if equal, go check results
           leay    1,y                 continue to the next address
           bra     nextbyte            go on to the next byte
*
.   verify the memory results
.
checkit    ldy     startad,u           get the starting address

chekbyte   lda     0,y                 pick up the current data
           sta     oldbyte,u           store in a temporary
           pshs    y                   stuff the address on the stack
           lda     0,s+                get msp of address value
           adda    0,s+                add lsp off the stack
           adda    passctr,u           add in the pass counter, also
           eora    oldbyte,u           generate exclusive or of value
           beq     nextchk             if all bits match, great!
*
.   we have a memory error, report discrepancy
.
           pshs    a                   save the errant bits
           leax    nextadr,pc          point to the message
           bsr     pdata               tell dude of problem
           tfr     y,d                 get address of error
           bsr     hexword             convert address into hex
           bsr     outsp               separate by a space
           lda     oldbyte,u           pick up the old byte
           bsr     hexbyte             convert to hex
           bsr     outsp
*
.   indicate what bit problems we are seeing
.
           ldb     #8                  set up number of bits
nextbit    lda     #"-"                set a hyphen for identical bits
           lsl     0,s                 shift the error indicator left
           bcc     showbit             if no carry, bit is correct
           lda     #"0"                indicate a dropped bit
           tst     oldbyte,u           check sign bit of old byte
           bmi     showbit             if one, bit was dropped
           lda     #"1"                else a false one bit
*
.   display the status of this bit
.
showbit    lsl     oldbyte,u           shift the byte left, also
           bsr     outsp               dump out a separator
           bsr     outch               then tell status of bit
           decb                        decrement bit count
           bne     nextbit             if non-zero, continue
           leas    1,s                 throw away the error byte
           bsr     incheck             check for possible abort
           bne     abort
*
.   continue on to next verification pass
.
nextchk    cmpy    endaddr,u           check for end address
           beq     nextpass            if so, start the next pass
           leay    1,y                 bump on to next byte
           bra     chekbyte
*
.  get a 16 bit address from keyboard into d
.
getword    bsr     getbyte
           bcs     getwexit            exit if invalid
           tfr     a,b                 keep value in B
           bsr     getbyte
           exg     b,a
           tfr     d,y                 memtest expects it in Y
getwexit   rts

           end

