Mandelbrot

C64 adventures | download | source

I started with installing the ACME assembler and the VICE emulator for the Commodore 64. Spent some time getting familiar with the assembly language. It wasn't totally new, but not because of my childhood, I only used BASIC then. I learned a lot about the 65xx processors when I wrote a NES emulator a few years ago. That family was quite common in the 1980s, many Commodore machines and also the Apple ][ used that.

screenshot

The biggest obstacle on the C64 is the lack of multiplication. It can add and subtract numbers, but not multiply (or divide). One has two options: either using the floating point routines that come with the C64 BASIC interpreter or implement them by hand. There are various sources with assembly snippets around, and after I realised that the floating point arithmetic is unbearably slow for my purposes I decided to switch to 16 bit fixed point arithmetic (with 8 bit for the fraction part) and pulled in some routines from https://codebase64.org/.

It didn't work of course... The output resembled the Mandelbrot set but was kind of well...

mandelbrot-fail

Now how do I debug this thing? I wrote a few assembly routines and tried the arithmetic on a few examples, but it looked all good. There are so many places to screw it up....

I quickly created a reference implementation in Javascript with Html canvas so that I know what to expect, but it was just fine. 16 bit is definitely enough. Finally, after some pondering I figured that I could redirect the standard output of the C64 to the printer, and redirect the printer from the emulator to a file. The magic commands are:

OPEN4,4
CMD4

I could just call the regular PRINT routines to trace my variables and compare it to the reference implementation.

I found a few places where the multiplication returned bad results. Went through the code a couple of times, but it took me an hour until I found that my Mandelbrot drawer is using the same memory locations for its local variables as the multiplication routine for the helper tables...

I essentially overwrote things like 100 * 100 = 10000 with some garbage... (The reality is a bit more tricky as it's not directly storing 100* 1 00, but you get the idea.)

Anyway, once it was fixed, it really started to look like a Mandelbrot set.

Real hardware

Using my proven tactic, I went to reddit and asked around for help. A few hours later fellow reddit user u/pskipw created a few pictures for me. Here is one:

pskipw-c64.jpg Source: https://imgur.com/a/ogF4kFR

I have never heard about it, but the C64 had a portable version as well, called the SX-64. u/eventhorizonrocket shot this. It takes about 5 minutes to render:

eventhorizonrocket-sx64.jpg Source: https://imgur.com/a/MxnhAKi

A close up from u/conventionalmemories:

conventionalmemories-sx64.jpg Source: https://imgur.com/gallery/SWjcJBm

The colors are different because the SX-64 has different color settings after startup than the C64 and I didn't count with that.