Commodore 64 Programming #6: Raster lines


Update: All needed files for this tutorial can be found in the GitHub repo linked in the bottom of this page.

A raster line is the line that is being redrawn on the screen. You can create a lot of cool effects and implement smart logic that happens when the video signal reaches a specific raster line of your choice.

The screen is redrawn 50 times per second, from top to bottom, from the left to the right. This can be used to synchronize your logic with the screen refresh (and also with the clock!).

The current raster line is stored in the memory location $d012.

Let’s go back to our example in tutorial #1 where we changed the background color on the main area, using the following code:

    processor   6502
org    $1000

loop:    inc $d021
jmp loop

This resulted in something similar to the screen below:


Now, let’s implement the same example, but synced with the screen refresh. The code is really short, so I’ll just give you the complete listing.

        processor   6502
org    $1000

loop:     lda $d012
cmp #$ff
bne    loop
inc $d021
jmp loop

What this code does is to first load the value of d012 into the accumulator. And we check if it equals the value of ff (could be anything you want really).

Note: The screen size goes from 0 – 319, and the byte in d012 only got the range of 0 – 255. To get the rest of the screen, you can use the high bit in d011. If this is set, the value in d012 will contain the raster lines after 255. Smilefjes In other words, d011 is the 8th bit of d012.

Now, if you run the code above, you will get a screen that flashes different colors 50 times per second, without the color distortion that is created due to interrupts and timing.


Raster interrupts covered in tutorial 10 is a really important topic. It can be used to create a lot of cool effects like rendering more than 8 hardware sprites, split your screen into sections (one displaying hiQ graphics, other part displaying a font) and so on.

Have fun! Smilefjes

Download the source from GitHub:

This entry was posted in Commodore 64. Bookmark the permalink.

13 Responses to Commodore 64 Programming #6: Raster lines

  1. Pingback: Windows Client Developer Roundup 067 for 5/1/2011 - Pete Brown's

  2. Hewitson says:

    That’s not a raster interrupt, that’s making the CPU wait until a certain line is reached. A good example of raster interrupts is available here:

    Cheers 🙂

  3. digitalerr0r says:

    yeah, you are correct. I covered some of the IRQs in tutorial 9 and 10. I changed the text to avoid confusion. Thanks for the feedback! 😀

  4. Anonymous says:

    When you say “In other words, d011 is the 8th bit of d012.”, I guess you mean “the 9th bit”, as there are already 8 bits in the byte at d012 ?

    Great set of tutorials anyway 🙂

    • says:

      Bits count from 0 – 7, bit 0 being the first bit (8 bits in total). The next address holds the 8th bit because you only get 8 bits in a byte.

      Good to see people are still interested in coding the C64! Just fired mine up. Can’t remember half the stuff I used to know.

      Cheers 🙂

      • You are mistaking cardinal and ordinal numbers. Bit 0 is the first bit. Bit 2 is the third bit. Bit 8 is the 9th bit. The commenter who corrected you is right.

      • John says:

        @ David Hoelzer (Below, no Reply).
        Cardinal & Ordinal are fine and technically correct, for Mr prefect. But; it adds to the complexity of conversation when engineering your expression. The 8th bit is 8-bit of a sequence of bit starting from zero and terminating at (N-1) –> KISS: Keep It Simple Stupid.

  5. tomse says:

    What happend to Tutorial #11+ ? 🙂
    I’d like to know more about sinus tables, vector calculations, bits and bobs, how to prevent screen from flickering when sprites are on top of eachother… now, get your C64 out now. 2 years being packed away is a long time.

    After that, feel free to head over to and keep yourself updated with news + more

  6. MANU says:

    “Note: The screen size goes from 0 – 319, and the byte in d012 only got the range of 0 – 255. To get the rest of the screen, you can use the high bit in d011. If this is set, the value in d012 will contain the raster lines after 255. Smilefjes In other words, d011 is the 8th bit of d012.”

    For the first time, I couldn’t understand it. Ok it just a note, something I might understand later but I think it’s easier than the sprite pointer, so please, explain this better.
    high bit? is it d0 or 11 well I don’t understand even with that. it’s 100% clearly a note for people who knows allready how it works.

  7. MANU says:

    Oh, it could be explanated in next chapter with the bitmap. I’ll know tommorow 😉
    still, I liked it was explanate better here.
    Anyway did I said enough thanks for your awesome tutos?

  8. Andrea says:

    Individuals keep stating to use WordPress, do you have any type of help for a novice user that isn’t really that fantastic along with technological stuff?
    I merely love to compose!

  9. Vance says:

    It is perfect time to make some plans for the future and it is time to be happy.

    I have read this post and if I could I want to suggest you few interesting things or suggestions.
    Perhaps you could write next articles referring to this
    article. I want to read more things about it!

  10. Teodor says:

    Hi there, I tried to increment the border color at two different lines of the screen, but it seems that my code doesn’t work correctly. Here it is:
    loop: lda $d012
    cmp #$20
    bne loop:
    inc $d020
    jmp loop2:
    loop2: lda $d012
    cmp #$ff
    bne loop2:
    inc $d020
    jmp loop:
    Please explain me how to do that.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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