Nicholas Carlini Crams a Working Intel 4004 Calculator Into 4004 Bytes of Obfuscated C Code

Custom compression and clever coding crams Intel's iconic 4-bit microprocessor, and its calculator support chips, into a tiny block of code.

ghalfacree
4 months ago Retro Tech

Research scientist Nicholas Carlini has celebrated the birth of the microprocessor with a gate-level emulation of Intel's classic 4004 chip — distributed, for good measure, as 4004 bytes of obfuscated C code.

"The International Obfuscated C Code Contest results were announced this last weekend, and I was thrilled to see that my entry was among the winners," Carlini writes. "[It's] a feature-complete gate-level emulator for the Intel 4004 that's capable of emulating the original Busicom 141-PF calculator ROM — the application for which the 4004 was originally designed."

It might look like a corrupt source file, but this is in fact a gate-level simulation of Intel's 4004 microprocessor. (📷: Nicholas Carlini)

Intel's 4004 was launched back in 1971, and is generally recognized as the first commercially-successful microprocessor built using large-scale integration (LSI) — though its success came outside the project for which it was designed, a printing calculator from Busicom. While a four-bit design, it would lay the groundwork for what would become the x86 architecture and Intel's rise to one of the biggest names in computing.

Carlini's recreation works as a simulation at the level of individual gates — capable of running, he says, at a few hundreds of instructions per second on modern hardware or enough to complete a calculation in around three to 10 minutes, "which is actually fast enough to be usable, just as long as you're not in a hurry." How it's made, though, is the interesting part: the emulation comes courtesy of a design written in miniHDL, a Python-based domain-specific language (DSL), which is then compressed down into a 4004-byte C program of seeming gibberish deliberately designed to hide its intended purpose to serve as an entry to the IOCCC.

Squeezing the simulation into just 4004 bytes required some trickery, including the implementation of custom compression for the circuit itself. (📷: Nicholas Carlini)

"If one were to rank programs by human-hours-per-byte, this program would be certainly top the ranking for the all the code I've ever written," Carlini guesses. "I didn't keep good track of this, but I'd estimate I spent probably a few hundred hours over the last four years working on it. (Honestly I felt kind of empty after submitting the code back in June. I've been working on this since almost the start of the pandemic, and whenever I felt like I had to take a break from my large (mostly Python) actual work this was the perfect place to come and fiddle with the bytes of a program that fits comfortably on a single piece of paper.)"

The project is documented in detail on Carlini's website; the source code is available on GitHub under the reciprocal GNU General Public License 3.

Main article image courtesy of Thomas Nguyen, CC-BY-SA 4.0 International.

ghalfacree

Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.

Latest Articles