SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

Card Attack

Doing the Impossible on CARDIAC.

GitHub Join us on Discord

Card Attack wasn't really something I planned. I was just playing around, idly sketching some ideas, and then suddenly it took off.

I was sitting in a hotel room in Seattle in early October after a conference. I had been playing around with CARDIAC a little for a few weeks, building a couple of visualizers, but they all seemed so boring. I had built a CARDIAC emulator, a compiler for a small language and a debugger about ten years ago, but I felt like that work had been left kind of unfinished.

As I was playing around that evening, my mind drifted to the 90s-era demoscene. Groups like Future Crew and Complex were huge influences on young me, and they managed to produce art from similarly underpowered computers. Floating point math, trigonometry and tessellation were all far too expensive to do in real time, but the demoscene coders coaxed those systems into doing more than anybody expected with a variety of clever tricks and optimizations. Floating point math is too slow? Use fixed point. Sin(x) too expensive? Use a table lookup. Put the video card in a special mode to give you more room.

The one thing CARDIAC can do is integer math, which means it can do fixed point math, which means it can probably do a lot of the things the demoscene coders could do. The one hard thing to work around: it can only handle numbers in the range [-999, 999]. That turned out to be the biggest limitation of all. There were plenty of clock cycles to do the computation, but dealing with that small range of numbers was the hardest thing. But I'm getting ahead of myself.

Back to that evening in Seattle. I asked Claude to help me do a proof of concept. Could we build a simple ray tracer?

And the answer was yes, we totally could. I think it was less than an hour to get the first ray-traced image on the screen.

As soon as I saw this image on the screen, I knew. If we could do a little bit of ray tracing, we could probably do most of what the demoscene could do. It was just a matter of finding out how far we could take it. Even that ray-traced image was something nobody had ever done before on CARDIAC. Not because it couldn't be done, but because nobody had tried. It didn't take long before I had a variety of demoscene staples running.

The first ray tracing on CARDIAC: a mirror ball above a red and white checkerboard. An early test: a cyan wireframe drawn by CARDIAC programs. An early test: a flat-shaded spinning cube. An early test: a wireframe cube over a red pattern. An early test: a checkerboard tunnel.

This was fun, but really tedious, and I started wishing I had a more modern language to work in. Could we port one to target CARDIAC? On its face the idea is ridiculous: CARDIAC has 100 memory cells, one register, and no stack. But I couldn't shake the idea. If we could develop segments in a higher-level language, they would be a lot easier to develop, debug and understand.

We considered a few languages, C being the most obvious, but the one that showed the most promise was Rust. In a single evening I was able to compile some super basic Rust to CARDIAC. That compiler was terrible: it relied on regular expressions run against a text dump of Rust's MIR. But it did mean I could write code in Rust, test it in VS Code compiled for Apple silicon, then throw a switch and generate CARDIAC instructions.

And so I started building. There was a wonderful cycle of writing CARDIAC or Rust code, compiling it into a demo, then improving the compiler to produce better code by profiling the demo itself. I started thinking I might be able to make a video worth sharing at a demoscene party.

And that's about when I found out that Demosplash would be running at Carnegie Mellon in late November. This seemed like too good a coincidence to pass up. It gave me a deadline of about a month to produce and polish something for the competition, and I thought the CARDIAC story made for a unique hook. I spent the next several weeks building various little sequences, most of which never made it into the final video (ask me about the Money for Nothing / Take on Me gag), but all of which helped improve the tooling around it. That tooling is now all released on Strange Query. There's the CARDIAC machine itself, heartburn, which we optimized to be insanely fast. There's a visual IDE, Cardigan. There's the rust-cardiac compiler (available on crates.io), with VS Code support, so you can do your development in VS Code. And there's still work happening under the hood, continually improving rust-cardiac to get closer and closer to hand-assembled CARDIAC code.

Card Attack owes a great debt to the demoscene coders of the 90s. Those pioneers paved the way with algorithms we were able to adapt or reuse, and I've tried to credit them where I could. The CARDIAC platform itself turned out to be a very fun one to work with. Its non-binary nature introduced us to lots of novel optimization tricks that wouldn't make sense on a binary computer. All of the source code for Card Attack, and most of the tools used to build it, is available on GitHub.

I hope you enjoy watching it as much as I enjoyed making it.

The work behind it

Card Attack stands on everything else in the lab.