The Basics Of Commodore 64 Hi-Res Graphics
David Martin
Creating an interesting high-resolution screen on the Commodore 64 can be a chore. These short programs will make it easier to design detailed screens for your games or business applications. Program 1 is in BASIC so it can be easily modified and understood. Program 2 demonstrates some of the potential of the VIC-II chip.
High-resolution screens use a technique called bitmapping. That's just a different way of setting up a display screen. In bitmap mode, the VIC-II chip displays an 8K section of memory on your screen instead of the normal 1K for a text screen. The reason for this is that in bitmap mode you need eight bytes for each character space on the screen. It's like having 1000 redefinable characters on the screen at one time.
A standard text screen is 25 by 40 characters wide. If you could fill that standard text screen with a thousand redefinable characters, you would have a screen that could be easily bitmapped. The bitmap mode enables you to turn on individual pixels on the screen and create intricate graphs and game backgrounds.
In bitmap mode the screen is divided into 320 horizontal pixels by 200 vertical pixels, each of which can be turned on and off individually. The formulas in line 10 of Program 1 do all the calculation that is necessary to turn on the pixel that you prefer. The reason that formulas are necessary is that the pixel locations are not continuous (right to left and top to bottom). Instead, they are located eight bits across and eight bytes down, then back up to the top byte of the next character space.
For example, say that you wanted to turn on a complete row of pixels to form a horizontal line. You would first have to turn on the first eight bits by POKEing a 255 into the first memory location of the high-resolution screen area, then skip the next seven bytes and POKE 255 into the eighth byte, and follow this pattern 40 times to create the line. In any case, the formulas in line 10 will figure out which pixel you want to turn on.
Erasing Program Lines
To use bitmapped graphics, you will have to know not only how to set pixels, but also how to set up an 8000-byte section of memory for the bitmap and a 1K section of memory for the background color screen. This involves working with the VIC-II chip. In Program 1 the text screen is used as the background color screen, and the section of memory starting at location 8192 for the bitmap. Lines 3 and 4 in Program 1 take care of this. The bitmap could have been moved to another section of memory, but that would have involved several extra steps, such as telling the VIC-II chip to look at the second 16K bank of memory. For short programs this is not necessary. Program 1 makes itself shorter using a technique called the "Electric Eraser," which appeared in the August 1982 issue of COMPUTE!. You will find the routine that does this in line 96 of the program. After the data for two short machine language routines has been placed into memory, the Electric Eraser erases everything after line 94 (so remember to save the program before running it).
The first of the machine language routines in Program 1 is used for erasing the 8K bitmapped screen. The second routine sets the background color of the hi-res screen to whatever color you specify by filling the background color screen with the value for the desired color. Both programs are very similar; they are just general transfer routines that could be used for other purposes. If these routines had not been included, you would have had to wait about 40 seconds while the entire hi-res screen cleared. In machine language, the clearing is almost instantaneous.
Program 1: Hi-Res Screen Sketching
0 POKE 56, 32 : POKE 52, 32 : CLR : REM PROTECT SCREEN FROM BASIC :rem 108 1 POKE 53280, 1 : PRINT "{CLR}{WHT}" : GOTO 100 :rem 102 2 GOSUB 26 : BASE = 2 * 4096 : REM START ADDRESS OF HIRES SCREEN :rem 93 3 POKE 53272, PEEK(53272) OR 8 : REM BIT MAP AT 8192 :rem 39 4 POKE 53265, PEEK(53265)OR 32 : REM BIT MAP ON :rem 141 5 SYS 49152 : REM CLR HIRES SCREEN :rem 115 6 SYS 49173 : REM SET SCREEN COLOR (BITS THAT ARE OFF) :rem 237 7 X = 160 : Y = 100 : REM X & Y START POSITIONS :rem 15 8 GO SUB 13 : REM READ JOYSTICK :rem 198 9 REM UPDATE SCREEN :rem 160 10 CH = INT(X/8) : RO = INT(Y/8) : LN = YAND7 : BY = BA SE + RO * 320 + 8 * CH + LN : BI = 7 - (X AND 7) :rem 90 11 POKE BY, PEEK(BY) OR (2↑BI) : GOTO 8 :rem 33 12 REM READ JOYSTICK :rem 211 13 JV = PEEK(56320) : FR = JV AND 16 :rem 160 15 X = X + ((JV AND 4) = 0) - ((JV AND 8) = 0) :rem 27 16 Y = Y + ((JV AND 1) = 0) - ((JV AND 2) = 0) :rem 21 19 IF FR = 0 THEN 5 :rem 98 20 IF X > 319 THEN X = 319 :rem 133 21 IF Y > 199 THEN Y = 199 :rem 148 22 IF X < 0 THEN X = 0 :rem 171 23 IF Y < 0 THEN Y = 0 :rem 174 24 GET A$ : IF A$ < > "Q" THEN RETURN :rem 247 25 POKE 56, 160 : POKE 52, 160 : POKE 53272, 21 : POKE 53265, 27 : PRINT "{CLR}" : END :rem 4 26 PRINT "{CLR}" TAB(18)"{DOWN}MENU{DOWN}{4 LEFT}[<4 Y>]" :rem 72 27 PRINT "{DOWN}" TAB(16) "Q{2 SPACES} - QUIT" :rem 223 28 PRINT "{DOWN}" TAB(9) "FIRE BUTTON - CLR SCREEN" :rem 193 29 PRINT "{DOWN}" TAB(10) "JOYSTICK MOVES LINE." :rem 106 30 PRINT "{3 DOWN}{7 RIGHT} ENTER BORDER COLOR (0 TO 15)." : PRINT SPC(18); :rem 71 31 INPUT BC : POKE 53280, BC AND 15 :rem 206 32 PRINT"{3 DOWN}{7 RIGHT} ENTER SCREEN COLOR (0 TO 15)." : PRINT SPC(18); :rem 75 33 INPUT SC : POKE 49174, SC AND 15 : RETURN :rem 19 94 END : REM ELECTRIC ERASER :rem 111 95 A = PEEK(61) + 256 * PEEK(62) + 3 : POKE 786, INT(A/256) : POKE 785,A - 256 * PEEK(786) :rem 3 96 POKE A - 2, 0 : POKE A - 1,0 : POKE 45, PEEK(785) : POKE 46, PEEK(786) : CLR : GOTO95 :rem 44 100 FOR I = 0 TO 42 : READ J : POKE 49152 + I, J : NEXT I : GOTO 2 :rem 150 101 DATA 169, 0, 162, 32, 160, 0, 132, 33, 134, 34, 145, 33, 200, 208, 251, 232, 224, 64, 208, 244 :rem 17 102 DATA 96, 169, 1, 162, 4, 160, 0, 132, 33, 134, 34, 145, 33, 200, 208, 251, 232, 224, 8, 208, 244 :rem 75 103 DATA 96, 0 :rem 121
Program 2: Multicolor Hi-Res Screen
1 PRINT "{CLR}" :rem 149 2 BASE = 10 * 4096 : REM START OF HIRES SCREEN :rem 100 3 POKE 53272, PEEK(53272) OR 10 : REM PUT BIT {SPACE} MAP AT 40960:rem 120 4 POKE 53265, PEEK(53265) OR 32 : REM ENTER BIT MAP MODE :rem 147 5 POKE 53270, PEEK(53270) OR 16 : REM MULTI-COLOR ON :rem 2 6 POKE 56576, 5 : REM SELECT VIDEO BANK :rem 68 7 FOR I = BASE TO BASE + 7999 : POKE I,0 : NEXT I : REM {SPACE}CLEAR GRAPHIC SCREEN :rem 157 8 END :rem 15