As it happens with older computers, with the C64 the programmer had complete control over the machine and could write directly to any part of memory, perform I/O and so on. Video memory was mapped to the CPU address space and so could be read and written directly as well.
For the C64, the relevant memory addresses are 0x0400 for characters and 0xD800 for colors. The byte at 0x0400 determines the character that appears at the top left corner at the screen, while the byte at 0xD800 determines the color of this character. The following bytes store the next characters in order going from left to right, and top to bottom. So, to find the address of screen position (x, y) for column x and row y, you have to calculate 40*y+x. Multiplication can be slow in these old CPUs, but we can fill the memory sequentially and, en passant, see the complete character set of the computer (it does not follow ASCII).
So here's a small BASIC program to manipulate video memory using POKEs. We can also set the background color using the memory-mapped location at 0xD021 as in the last post, but we can do this directly using a POKE instead of jumping to a machine-language subroutine.
10 SYS 58692:REM CLEAR SCREEN 20 POKE 53281,0:REM BLACK BKG 30 FOR I=0 TO 255 40 POKE I+1024,I 50 POKE I+55295,(I-INT(I/15)*15)+1 60 NEXT I 70 FOR J=1 TO 1600:NEXT J 80 POKE 53281,6:REM RESTORE BKG 90 PRINT"↓↓↓↓↓"
The colors range from 0 to 15 and, because the background is black, we want to avoid color 0 (black) as characters with this color would be invisible. That's the reason for the complicated expression at 50. It's just (color mod 15) + 1 to get colors from 1 to 15, but this BASIC does not have a modulus/remainder operator. At the end, line 90, the down arrows represent the cursor down character that print on the C64 screen as reversed Qs. After displaying all the characters, the program runs an empty loop to make a delay (line 70), and then restore the default blue color to the background.
This is the program running:
Comments
Post a Comment