Running Unix v0 - from 1969 - on a PDP-7 emulator


I was blown away by Christopher Mills’ winning entry for the IOCCC last year. In 3636 bytes, he offers a PDP-7 emulator which can run Unix v0. You can compile a B program, and look around in ken’s and dmr’s home directories.

B can be thought of as C without types; more accurately, it is BCPL squeezed into 8K bytes of memory and filtered through Thompson’s brain.

For extra interest, also included is a PDP-11 emulator which runs on the PDP-7 emulator, with lots of swapping, and which can run Research UNIX Version 6 (from 1975 or so) or BSD 2.9 (1983). In these environments you can compile a C program - even an obfuscated one.

… we are simulating a system that can have up to 124 Kwords of memory on a system with only 8 Kwords total. That meant I needed to virtualize the PDP-11/40’s physical memory

Despite the complexity of the PDP-11 instruction set and its emulated I/O devices and the corresponding primitiveness of the PDP-7’s instruction set, the emulation itself is a suprisingly small amount of code, not really taxing the 8 Kword memory, with about 2.25 Kwords of actual code and a similar amount of space for the memory virtualization cache and disk block buffer, leaving nearly half (3.5 Kwords) of the memory unused. The simulator executes somewhere around 250 PDP-7 instructions per PDP-11 instruction.

A large amount of time was spent tuning this code to be efficient. The memory virtualization uses part of the 8 Kword memory as an LRU cache, and uses a number of first-level microcaches to avoid having to do the LRU updates. This also serves to offload the complexity of the PDP-11’s segmentation logic from a large number of memory accesses (the PDP-11 does as many as eight accesses per instruction). … the PDP-7 code in the emulator is slow, mostly because it has to do a bunch of divides to convert the linear disk offset into a BCD-coded track and sector for the emulated RB09.

Lots to learn about the 7 and the 11 in the writeup:
Best of show

And as a final bonus, the shape of the code is also a gift: