[Oscar] is no stranger to writing hard-to-read C code. While most of us
do that by accident, there are those who strive to write the most
unreadable code and enter it in the IOCCC — the International
Obfuscated C Code Contest. One of his winning entries was[1] a single C
function that emulates an 8080. With a few support files, the plucky
little emulator will run CP/M.
The emulator won best in show, but that was in 2006. Things have
changed a bit and [Oscar] has updated the code so that you can continue
to try it if you want to give yourself a headache reading code. The
portability isn’t a CPU issue — modern CPUs will happily run code from
2006. The problem is the compiler and operating system. Compilers are
much stricter these days, and Linux needs a little extra coaxing to
give access to the input stream the way the faux computer needs it.
If you want to puzzle out the code, don’t read any further. Spoilers
ahead! The code has an array of 64K representing all the memory an 8080
can use without some bank-switching scheme. After that, a loop simply
has to read the array, determine the opcode, and perform the operation.
The registers are in the o array. Why o? Well, it is supposed to be
obfuscated, remember?
The emulator needs two binary files: one containing a stripped-down
BIOS system and the other Tiny Basic. Alternatively, you can download a
file that will run CP/M, including Wordstar.
If you prefer running on real hardware, [2]slip one of these in your
pocket. If obfuscation isn’t your thing, run [3]CP/M on nearly anything
with more readable code.
References
1.
https://nanochess.org/emulator.html
2.
https://hackaday.com/2022/11/16/is-this-the-smallest-cp-m-machine-ever/
3.
https://hackaday.com/2024/02/19/linux-fu-forward-to-the-past/