“Hi-res” bitmap graphics with Commodore 64 BASIC 2.0, fast screen clearing routine

Bitmap graphics with Commodore 64 BASIC 2.0 is not easy, but it is possible. The problem is the lack of commands specifically designed to handle the bitmapped screen. Although Simons’ BASIC and other extensions are available, we may still try to manage “hi-res” graphics with the C64 native BASIC in a not too slow way.

If you have tried the programs from the Commodore 64 Programmer’s Reference Guide, you will have realized that clearing the bitmap screen is the most time-consuming operation. The simple FOR… NEXT cycle used there to clear the screen seems to be very inefficient.

To speed things up, we could change the content of location 648 decimal so that it is possible to print characters on the same memory area the bitmap screen is placed to. This way, slow POKEing is replaced by fast PRINTing. On this article, you can see how a horizontal scrolling of a chessboard can be performed by using this technique. Values are just printed in memory.

Since I have been lately making experiments dealing with garbage collection and creating strings in memory, I have come up with another solution.

As we know from this article, when a string is assigned to a variable, on most cases a new string is created in the string storage area. This can happen even when an already existing variable is re-assigned. As we have seen from the garbage collection article, string manipulation inside loops is very likely to create a lot of garbage, eventually filling the String Storage Area. This is usually a problem that must be handled with careful programming, but it can turn out to be a useful feature.

Clearing the bitmap screen means, of course, setting to zero all the bytes making up that screen. Since Commodore 64 graphics resolution is 320×200 hi-res dots, there are 64000 dots on the bitmap screen. That is, 8000 bytes. We now must find a way to clear those bytes in a fast way from BASIC.

An assignment such as:

A$ = CHR$(65)

will create an “A” string somewhere in the String Storage Area. Strings are stored in memory with the ASCII codes of each character. So, the above statement will store the value ’65’ somewhere in memory.

Since the String Storage Area Pointer is on locations 51 and 52 decimal, if you issue the above statement in direct mode, it is easy to see where the value 65 will be stored in memory. Please take a look at the following picture:

 

If we instead use the statement:

A$ = CHR$(0)

Then, the value 0 will be stored in memory.

So, by creating long strings made up of the CHR$(0) character, it is possible to clear the memory starting from the String Storage Area address referenced by the String Storage Area Pointer. If we change that pointer and we start creating strings, we can virtually put any value on many locations in a fast way.

If the bitmap screen starts from 8192 decimal, the following code will clear the screen:

10 a$="":s1=peek(51):s2=peek(52):poke 51,64:poke 52,63
20 fort=1to125:a$=a$+chr$(0):next
30 poke51,s1:poke52,s2

 

First of all, the program stores current values of the String Pointer. Than, it changes it to point to location 16192 decimal. This way, the BASIC interpreter thinks this is the latest location used to create strings. Then, when strings are created from now on, those will be stored in memory starting from location 16191 decimal, backwards. This address contains the last byte of the bitmap screen.

In the line 20, the FOR… NEXT loop creates strings made up of the CHR$(0) character. Those strings are bigger and bigger, and make it possible to clear off the whole bitmap screen. Strings are created from 16191 backwards, till location 8192 is reached. This way, all 8000 bytes of the bitmap screen are set to zero very fast.

Line 30 restores the initial String Pointer values.

The following BASIC program shows off this approach, by plotting some functions and clearing the screen at times.

DOWNLOAD – Cosine functions plot.

 

The cosine functions plotted. Please note that the origin is on the top, left corner of the screen. So, the Y axis points downwards.

 

RUN/STOP is disabled by the program. You can exit the program by pressing RUN/STOP and RESTORE. You can also wait the program to end, then press a key.

The program has three subroutines. The one starting from line 23 clears the bitmap screen, and it is the same as the code above explained.

Another routine, starting from line 35, is used to set the colors of the bitmap screen. The video matrix is used to define bitmap colors (in hi-res mode).

The routine starting from line 42 plots the points on the bitmap screen. The formulas used here come straight from the book “Mapping the Commodore 64 & 64 C”, page 133, with little modifications. The table created in line 4 is used to gain speed.

Not many points are plotted, but I made this choice to set plotting speed to an acceptable level.

This program shows, ironically, that garbage strings can be actually very useful.

 

One Reply to ““Hi-res” bitmap graphics with Commodore 64 BASIC 2.0, fast screen clearing routine”

  1. Thanks so much for this routine!!! It’s damn near as fast as a machine language Hi-res clear, close enough for my purposes.

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert