Zolatron 64 6502 homebrew – even better LCD printing

Many thanks to reader Michael McLaren for giving me a heads-up about a better way to move the cursor on the LCD display.

In an earlier post, I described how I was using the ‘shift cursor right’ command multiple times to put the cursor in the desired position – mainly so that I could use the second line of the display.

Michael pointed out that it’s possible to talk directly to the display’s memory – which is known as DDRAM (display data random access memory). It has two sets of 40 bytes with addresses $00–$27 and $40–$67. Setting the address of the byte you want to talk to next is a simple matter of sending a byte to the command (aka instruction) register. You set the top bit – bit 7 – of this byte to 1 and the other seven bits contain the address of the byte.

I created another constant in the code:

LCD_SET_DDRAM = %10000000

And now, the code to set the cursor position is:

.lcd_set_cursor ; assumes X & Y co-ords have been put in X and Y
  ; X should contain the X param in range 0-15.
  ; Y should be 0 or 1.
  txa                 ; A now contains X position
  cpy #1
  bcc lcd_move_curs   ; Y is less than 1
  adc #$3F             ; if we want second line, add $40
  ora #LCD_SET_DDRAM  ; OR with LCD_SET_DDRAM command byte
  jsr lcd_cmd

This is so much more compact and efficient than my original code. It’s seven lines of actual code (ie, not including labels and comments) compared to 14. And it doesn’t need a time-wasting loop. Basically, if the Y register is set to 1, the code just adds $3F to the value from the X register (which has been shifted into A for the purpose). This gives us the DDRAM address we need, so that gets ORed with LCD_SET_DDRAM and the command is sent to set the address.

BTW, the datasheet says you can use the memory addresses of bytes that aren’t going to be displayed just as memory. We’re using 16 bytes out of 40 for each row, leaving a total of 48 bytes going spare. I’m sure I could make use of that somehow. You know, if I were clever…

But, hmm. When we want to start at the beginning of the second line, we’re adding $3F to a value that is 0. But the first address of the second line is $40. There’s still that discrepancy. I’m not sure what I make of that just yet. But as it’s working, I’m not going to fix it.

Never miss a post

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Support this blog:
BTC: bc1qd9sh4l7rzmsnqxu3yqgnq3wekwmaqy9cf5j6ef

2 thoughts on “Zolatron 64 6502 homebrew – even better LCD printing

  1. Michael McLaren

    Hi Steve. The reason you’re adding #$3F instead of #$40 is because you’re adding the Carry status bit which was set in the previous instruction. You could replace the ADC #$3F instruction with an ORA #$40 instruction if that seems more intuitive to you

    Have fun. Stay safe. Cheerful regards…


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.