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

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

Drawing bitmap graphics using Commodore 64 BASIC 2.0 is not easy, but it is possible. The problem is the lack of commands specifically designed to handle bitmap screens. Although Simons’ BASIC and other extensions are available, we can still try to use the C64 native BASIC to manage “high-resolution” graphics in a not too slow way.

If you’ve tried the procedures in the Commodore 64 Programmer’s Reference Guide, you’ll realize that clearing the bitmap screen is the most time-consuming operation. A simple FOR…NEXT loop for clearing the screen seems very inefficient.

To speed things up, we can change the contents of decimal position 648 so that the characters can be printed in the same area of ​​memory as the bitmap screen. In this way, the slow POKE operation is replaced by the fast PRINT operation. exist This articleyou can see how this technique can be used to perform horizontal scrolling of the board. The value is simply printed in memory.

Since I’ve been experimenting recently with handling garbage collection and creating strings in memory, I came up with another solution.

As far as we know articlewhen a string is assigned to a variable, in most cases a new string will be created in the string storage area. This happens even if an already existing variable is reassigned. As we saw from the garbage collection article, string operations within a loop are likely to generate a lot of garbage, eventually filling up the string storage area. This is usually a problem that must be handled with careful programming, but it can become a useful feature.

Of course, clearing a bitmap screen means setting all the bytes that make up the screen to zero. Since the Commodore 64 graphics resolution is 320×200 high-resolution points, there are 64,000 points on the bitmap screen. That is 8000 bytes. We now have to find a way to quickly clear these bytes from BASIC.

Assignment example:

A$ = CHR$(65)

An “A” string will be created somewhere in the string storage area. The string is stored in memory along with the ASCII code for each character. Therefore, the above statement will store the value “65” somewhere in memory.

Since the string storage area pointers are at decimal positions 51 and 52, it is easy to see where the value 65 will be stored in memory if the above statement is issued in direct mode. Please see the picture below:

If we instead use the following statement:

A$ = CHR$(0)

The value 0 will then be stored in memory.

Therefore, by creating a long string consisting of CHR$(0) characters, you can clear the memory starting from the string storage area address referenced by the string storage area pointer. If we change this indicator and start building strings, we can actually put any value in many positions very quickly.

If the bitmap screen starts at decimal position 8192, 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, the program stores the current value of the string pointer. It then changes it to point to decimal location 16192. This way, the BASIC interpreter thinks this is the latest position used to build the string. Then, when strings are created from now on, they will be stored in memory starting at decimal position 16191 and going backwards. This address contains the last byte of the bitmap screen.

In line 20, the FOR…NEXT loop creates a string of CHR$(0) characters. These strings grow larger and larger, and can clear the entire bitmap screen. The string is created backwards from 16191 until it reaches position 8192. Thus, all 8000 bytes of the bitmap screen are quickly set to zero.

Line 30 restores the initial string pointer value.

The following BASIC program demonstrates this approach by drawing some functions and sometimes clearing the screen.

Download – Cosine Function Plot.

Graph the cosine function. Note that the origin is in the upper left corner of the screen. Therefore, the Y-axis points downward.

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

This program has three subroutines. Clearing the bitmap screen starts at line 23, same as the code explained above.

Another routine begins on line 35 and is used to set the color of the bitmap screen. The video matrix is ​​used to define bitmap colors (in high-resolution mode).

The routine starting on line 42 draws points on a bitmap surface. The formula used here comes directly from the book Mapping the Commodore 64 & 64 C, page 133, with few modifications. The table created in line 4 is used to increase speed.

Not many points are drawn, but I made this choice to set the drawing speed to an acceptable level.

Ironically, this program shows that garbage strings are actually very useful.

2024-12-07 14:52:15

Leave a Reply

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