Commodore 64 Programming #5: Clearing the screen

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

In the previous tutorials we created stuff that rendered a sprite and changed the color on things. In this tutorial, we will clear the screen to a black color.

To clear the screen, we should set the color of the border and the main screen to a given color, like black, and then we must clear the characters on the screen. This can be done by setting them to the character SPACE.

To do this, first we put the index for black into the accumulator, and store this into the border and the main screen.

processor   6502
org    \$1000

lda #\$00
sta \$d020
sta \$d021

Next we copy the value in the accumulator into the x-register.
tax

Then we put the value of #\$20 in to the accumulator. #\$20 is the value of the SPACE character.
lda #\$20

Now we are ready to put the value inserted in the accumulator into every character on the screen memory. This will be done by creating a loop that goes through the entire screen memory. The screen memory is located at \$0400 and ends at \$07FF.

Linus Åkerlund of Fairlight shared a smart way of looping through the screen memory. Before the loop starts, x contains the value 0. If we decrement it, it will become FF (255), and then go all the way down to 0 again, setting all the characters to space in the range 0400-04FF, 0500-05FF, 0600-06FF and 0700 to 07FF.

loop:   sta \$0400,x
sta \$0500,x
sta \$0600,x
sta \$0700,x
dex
bne loop

The last instruction is new; bne will jump (branch) to loop if X is not zero. That means that when we start, X will be 0, and right before the bne operation, we will decrease it so it contains FF. This means that the loop will go all the way from FF to 00 before exiting the loop.

Listing 5.1 – Clear loop

processor   6502
org    \$1000

lda #\$00
sta \$d020
sta \$d021
tax
lda #\$20
loop:   sta \$0400,x
sta \$0500,x
sta \$0600,x
sta \$0700,x
dex
bne loop

That’s it for clearing the screen. When the clear loop is done, you can continue doing what else you want the program to do.

https://github.com/petriw/Commodore64Programming/tree/master/5-Clear%20Loop

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

8 Responses to Commodore 64 Programming #5: Clearing the screen

1. Tod says:

The only thing to remember, when clearing the screen this way, you will overwrite your sprite pointers, so clear BEFORE you set your sprites up.

Also, good tutorial.

2. Amir Khan says:

There is a typo in your tutorials, “boarder” should be “border”

3. Ben says:

well some things, first quoting you “When the clear loop is done, you can continue doing what else you want the program to do.” if nothing else we want the program to do then it need a rts at the end or a brk while occurs and your screen while not remain black. second, thats a hell of bytes for clearing the screen. when it could be done with loading clearscreen character in acumulator then calling \$ffd2 or even with calling directly \$e544.
so
lda #\$93
jsr \$ffd2
or
jsr \$e544
also as tod said it will overwrite your sprite pointers, but however the way of clearing screen that way as it interrest in the fact it doesn’t call external routine.

4. MANU says:

Yes it’s more bytes, and I bet it’s faster(i think) or slower, it’s not what I want to know anyway. Here the point is the tutorial for newbie.

We learn since 2 days or 3, it’s no sense to use jsr and the new addresses you put.
We know few instructions from previous pages.
Optimizing is something we’ll all do later, without the need of help then.

So… very good tutos mate. the previous was SO harder with that crazy pixel pointers, so many parameters to remember 🙂 But I could understand it from your great explanations.
Thanx forever! (stay awhile…)

5. Loving this tutorial! Recently got me a c64 (breadbox) for some sid synth action and some programming. I really do miss this way of doing things.

6. Marko says:

// Kick Assembler – Faster way of clearing the screen 25 x 40 characters

BasicUpstart2(main)
.encoding “screencode_upper”

main:
FillSCreen(‘X’) // Fill screen with ‘X’
jmp *

.macro FillSCreen(byteValue) {
lda #byteValue
jsr fillSCreenFunction
}

fillSCreenFunction:
ldx #250
!:
sta 1023,x
sta 1273,x
sta 1523,x
sta 1773,x
dex
bne !-
rts

7. Jed says:

Your loop only needs to repeat 250 times, not 255. The screen ram is 1000 bytes.

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