Dirty tricks 6502 programmers use Janne Hellsten on August 18, 2019 This post recaps some of the C64 coding tricks used in my little Commodore 64 coding competition. The competition rules were simple: make a C64 executable (PRG) that draws two lines to form the below image. The objective was to do this in as few bytes as possible. Entries were posted as Twitter replies and DMs, containing only the PRG byte-length and an MD5 hash of the PRG file. Here’s a list of participants with source code links to their submissions: (If I missed someone, please let me know and I’ll update the post.) The rest of this post focuses on some of the assembly coding tricks used in the compo submissions. Basics The C64 default graphics mode is the 40x25 charset mode. The framebuffer is split into two arrays in RAM: $0400 (Screen RAM, 40x25 bytes) $d800 (Color RAM, 40x25 bytes) To set a character, you store a byte into screen RAM at $0400 (e.g., $0400+y*40+x). Color RAM is by default initialized to light blue (color 14) which happens to be the same color we use for the lines – meaning we can leave color RAM untouched. You can control the border and background colors with memory mapped I/O registers at $d020 (border) and $d021 (background). Drawing the two lines is pretty easy as we can hardcode for the fixed line slope. Here’s a C implementation that draws the lines and dumps screen contents on stdout (register writes stubbed out and screen RAM is malloc()’d to make it run on PC): #include <stdint.h> #include <stdio.h> #include <stdlib.h> void dump(const uint8_t* screen) { const uint8_t* s = screen; for (int y = 0; y < 25; y++) { for (int x = 0; x < 40; x++, s++) { printf("%c", *s == 0xa0 ? '#' : '.'); } printf("\n"); } } void setreg(uintptr_t dst, uint8_t v) { // *((uint8_t *)dst) = v; } int main() { // uint8_t* screenRAM = (uint_8*)0x0400; uint8_t* screenRAM = (uint8_t *)calloc(40*25, 0x20); setreg(0xd020, 0); // Set border color setreg(0xd021, 0); // Set background color int yslope = (...
First seen: 2025-04-16 15:18
Last seen: 2025-04-17 05:23