  processor 6502 ; This source code is written for the DASM cross-assembler.

  org 5000

SCREENCOLOR  = 8
SCREGISTER   = 36879

  jmp init3k$
  jmp initblk1$
  jmp initblk2$
  jmp initblk3$
  jmp initblk5$

init3k$
  lda #$04
  sta test$+4
  sta test$+7
  lda #$10
  sta endaddress$+1
  jmp main$

initblk1$
  lda #$20
  sta test$+4
  sta test$+7
  lda #$40
  sta endaddress$+1
  jmp main$

initblk2$
  lda #$40
  sta test$+4
  sta test$+7
  lda #$60
  sta endaddress$+1
  jmp main$

initblk3$
  lda #$60
  sta test$+4
  sta test$+7
  lda #$80
  sta endaddress$+1
  jmp main$

initblk5$
  lda #$a0
  sta test$+4
  sta test$+7
  lda #$c0
  sta endaddress$+1
  jmp main$

main$
  lda #SCREENCOLOR
  sta SCREGISTER
  lda 648
  sta noletters$+2
  ldx #0      ; X contains the low byte of the current memory location.
  stx test$+1 ; Initialize the self-modifying code.
test$
  lda #0             ; Self-modifying code.  This is the test value to be
                     ; stored into the current memory location.
  sta $0400,x        ; Self-modifying code.  Only the hi-byte will change,
  cmp $0400,x        ; since the index X contains the lo-byte.
  beq ok$            ; If the value in the current memory location differs
  jsr error$         ; from the value of A, there is a memory error.
ok$
  inc test$+1 ; Increase the test value.
  bne test$   ; If the value is not 0 again, continue testing the same memory
              ; location.
  inx         ; Increase the lo-byte of the memory location.
  bne test$   ; If the X is not 0 again, the page boundary was not crossed.
  inc test$+4 ; Increase the hi-byte of the memory location.
  inc test$+7
endaddress$
  lda #$10         ; LDA the hi-byte of the end address,
  cmp test$+4      ; and compare it to the current memory location.
  bne test$        ; If not equivalent, continue testing.
  rts              ; That was all, folks.  No news is good news.

error$
  lda test$+4 ; Calculate the first character of the hi-byte
  lsr
  lsr
  lsr
  lsr
  jsr print$
  lda #15 ; Calculate the second character of the hi-byte
  and test$+4
  jsr print$
  txa ; Calculate the first character of the lo-byte (X)
  lsr
  lsr
  lsr
  lsr
  jsr print$
  txa ; Calculate the second character of the lo-byte (X)
  and #15
  jsr print$
  lda test$+1 ; Calculate the first character of the test value
  lsr
  lsr
  lsr
  lsr
  jsr print$
  lda #15 ; Calculate the second character of the test value
  and test$+1
  jsr print$
  lda #0
  sta noletters$+1
  rts

print$
  clc
  adc #48
  cmp #58
  bmi noletters$
  clc
  adc #199
noletters$
  sta $1e00
  inc noletters$+1
  rts
