Clearing The Screen

Clearing The Screen

There are a few ways to clear the screen, but in this tutorial, we will have a look at a way, that most programmers will do it.

         ldx #0           ; Load X with 0
         lda #32          ; Load A with keycode for character 'space'           

 loop    
         sta $0400,x      ; Put 'space' at mem location 0400,x
         sta $0500,x      ; Put 'space' at mem location 0500,x
         sta $0600,x      ; Put 'space' at mem location 0600,x
         sta $0700,x      ; Put 'space' at mem location 0700,x

         inx              ; Increase X from 0 to 255
         bne loop         ; Branch if not equal to 0

         rts

The c64 screen = 40 characters wide, by 25 characters down, so we need to clear a 1000 locations on screen. (40 x 25 = 1000)

The starting address, for the top left corner of the C64 screen = $0400, and as X is starting at 0, on the first pass, the character ‘space’ will be placed at $0400. Then we increase X by 1, so that on the second pass, X will be 1, and therefore the character ‘space’ will be placed at $0401.

As the 6502 can only count from 0 to 255, (256) because its an 8 bit processor, it would only roughly fill in a quarter of the screen with the character ‘space’.

So, if you add 256 to $0400, you get $0500, and the same for $0600 and $0700.

The problem with this method, is that it will override an extra 24 bytes at the end of the screen (256 x 4 = 1024)

And, in them extra 24 bytes, are pointers for sprite addressing, so if you are using sprites, then you don’t really want to go over them.

To avoid this, all you need to do is, set $0700 back 24 bytes, so $06e8.

         ldx #0           ; Load X with 0
         lda #32          ; Load A with keycode for character 'space'           

 loop    
         sta $0400,x      ; Put 'space' at mem location 0400,x
         sta $0500,x      ; Put 'space' at mem location 0500,x
         sta $0600,x      ; Put 'space' at mem location 0600,x
         sta $06e8,x      ; Put 'space' at mem location 0700,x

         inx              ; Increase X from 0 to 255
         bne loop         ; Branch if not equal to 0

         rts