.org $1300
   jmp main

;**** variables

p      = $fb  ;(2)  ;screen location pointer
temp   = $fd  ;(1)  ;temp computation storage
l      .buf 1
c      .buf 1
climit .buf 1
ch     .buf 1
n      .buf 1
y      .buf 1
ylimit .buf 1
x      .buf 1
m      .buf 256
mlen   .buf 1

;**** line 100 ***************************************************************

main = *
   lda #0        ;l=0
   sta l

   lda #<message ;read m$
   ldy #>message
   jsr LoadM

   ldx mlen      ;m$=m$+" "
   lda #" "
   sta m,x
   inc mlen

;**** line 110 ***************************************************************

   lda #1        ;for c=1 to 9+len(m$)
   sta c
   clc
   lda mlen
   adc #9
   sta climit

-  lda c         ;ch=c
   sta ch

   sta n         ;n=c

   cmp #9        ;if n>9 then n=9
   bcc +
   beq +
   lda #9
   sta n

;**** line 120 ***************************************************************

+  lda #10       ;for y=10 to 11-n step -1
   sta y
   sec
   lda #11
   sbc n
   sta ylimit

-  ldx #6        ;x=6

   ldy ch        ;a=asc(mid$(m$,ch,1))
   cpy mlen      ;  check for going beyond the end of the string
   bcc +
   ldy mlen      ;  adjust if so
+  lda message-1,y

   ldy y
   jsr PlotChar  ;gosub 150

   dec ch        ;ch=ch-1

   dec y         ;next y
   lda y
   cmp ylimit
   bcs -

;**** line 130 ***************************************************************

   lda c         ;if c>9 then
   cmp #9
   bcc +
   beq +

   inc l         ;l=l+1

   ldy #1        ;y=1

   ldx l         ;x=l

   lda message-1,x ;a=asc(mid$(m$,l,1))

   jsr PlotChar  ;gosub 150

;**** line 140 ***************************************************************

+  inc c        ;next c
   lda c
   cmp climit
   bcc --
   beq --

   ldx #1       ;for x=1 to 39

-  ldy #1       ;y=1

   lda #32      ;a=32

   jsr PlotChar ;gosub 150

   inx          ;next x
   cpx #39+1
   bcc -

   rts          ;end

;**** line 150 ***************************************************************

PlotChar = *  ;PlotChar( .A=char, .X=col, .Y=row ) ;.X unchanged
   pha          ;save .A
   lda #$00     ;clear scrloc accumulator
   sta p+0      ;  multiplying an 8/16-bit value by 40 is kind of a pain,
   sta p+1      ;  so it will have to be done in pieces
   tya
   sta temp
   asl          ;get .Y * 2  (range 0--48)
   asl          ;get .Y * 4  (range 0--96, still fits in 8 bits)
   clc          ;get .Y * 5  (range 0--120)
   adc temp
   asl          ;get .Y * 10 (range 0--240)
   asl          ;get .Y * 20 (range 0--480, need to save high byte)
   rol p+1
   asl          ;get .Y * 40 (range 0--960)
   rol p+1
   sta p+0
   clc          ;add 1024 to scrloc (only have to add 4 to high byte)
   lda p+1
   adc #>1024
   sta p+1      ;at this point, we have P=1024+(Y*40); we will add X later

   pla          ;if a>=64 then
   cmp #64
   bcc +

   sec          ;a=a-64
   sbc #64

;**** line 160 ***************************************************************

+  cmp #0       ;if a=0 then
   bne +

   lda #32      ;a=32

;**** line 170 ***************************************************************

+  stx temp     ;poke p,x
   ldy temp
   sta (p),y

   lda #1       ;slow down so you can actually see the effect
   jsr Delay

   rts

;**** line 180 ***************************************************************

message = *
   .byte "this is a test message",0

;**** EXTRA ******************************************************************

LoadM = *  ;( .AY=zStringPtr )  ;read m$
   sta p+0
   sty p+1
   ldy #0
-  lda (p),y
   sta m,y
   beq +
   iny
   bne -
+  sty mlen
   rts

Delay = *  ;( .A=count )  ;.X unchanged
   ldy #0
-  iny
   bne -
   sec
   sbc #1
   bne -
   rts
