,

DOOM Is a Houseguest: Porting DOOM to Tom Liston’s Forth OS

My friend Tom Liston released f64OS, a bootable 64-bit operating system written almost entirely in Forth, on a Tuesday. By Wednesday afternoon it was running DOOM, with sound, music, a mouse, and savegames, and Tom merged it upstream. This is the story of how that went, what broke along the way, and the one rule…

The test of a good guest isn’t how loud the party gets. It’s whether the host can find the forks the next morning. -Nor

On a Tuesday morning in November, my friend Tom Liston pushed the first commit of a project called f64OS to GitHub. If the name rings a bell, it should: this is the same Tom who gave the world the LaBrea tarpit back when worms were eating the internet. f64OS is a 64-bit, bootable, preemptively multitasking kernel whose whole reason for existing is to run Tom’s own 64-bit Forth on a VGA text console. A handful of bootstrapping words are written in x86-64 assembly. Everything else, including the language itself, a little filesystem, and a full-screen text editor you can use to edit the editor, is written in Forth.

It is a lovely, slightly unhinged piece of work. It boots in QEMU, puts a status bar at the bottom of the screen running as its own task, and greets you with an ok prompt and absolutely nothing else.

There is a law of the internet older than most of the people enforcing it: if a thing has a CPU and a screen, someone will ask whether it runs DOOM. Calculators, pregnancy tests, a John Deere tractor, a Lego brick. It is the hacker’s version of a handshake.

So I asked. Tom’s code was public on Tuesday. By Wednesday afternoon, DOOM was running on f64OS as a native 64-bit binary, loaded off the f64OS filesystem by Forth, with Sound Blaster sound effects, AdLib FM music, a PS/2 mouse, and savegames written back to the f64OS disk. Tom merged it upstream. This is the story of how that went, where it got interesting, and the one design rule that made the whole thing work.

The rule: DOOM is a guest in Tom’s house. It can borrow anything it needs. It has to put everything back.

What f64OS gave me to work with

Before you can be a polite guest, you have to know what’s in the house. Here’s what f64OS had on day one:

  • A long-mode kernel with an IDT, a PIC, a PIT timer, and a preemptive task switcher.
  • A VGA text-mode console and a keyboard driver that turns scancodes into characters for Forth.
  • A tiny filesystem with directories, LS, CD, MKDIR, INCLUDE, and a word to read 512-byte sectors.
  • Forth. All the way down.

Here’s what it did not have: a C compiler, a C library, a graphics mode, a sound driver, a mouse driver, any way to run code that wasn’t Forth, or any way to write a file that wasn’t built into the disk image at build time.

That second list is the port.

The first thing I did was get it building on my Windows workstation without WSL, which turned into its own small adventure. Git for Windows helpfully converted every Forth source file to CRLF on checkout, which broke Tom’s comment stripper and shoved carriage returns into the disk image. Python on Windows then helpfully wrote CRLF to stdout. Two helpful tools, one broken boot. I ended up with a small run.ps1 that replaces make plus run.sh and writes everything with Unix line endings, and I turned off core.autocrlf globally, which I should have done a decade ago.

The approach: don’t teach Forth to speak C

The tempting, wrong idea is to make Forth run C. Write a loader for ELF objects in Forth, a relocator, a dynamic linker. That’s a month of work and a monument to scope creep.

The right idea is much dumber. Compile DOOM on the host with gcc into one flat binary that expects to live at one fixed address. Copy that binary into the f64OS filesystem like any other file. Let Forth read it into memory and jump to it. When DOOM quits, it returns, and Forth picks up where it left off.

For the DOOM side I used doomgeneric, which is exactly what it sounds like: the Chocolate Doom code base trimmed down so that a new platform only has to supply a framebuffer, a millisecond clock, a sleep, and a way to read keys. Everything else is the original game.

For the Forth side, Tom’s kernel needed exactly one new idea, a word that calls native code on a fresh stack and comes back:

defcode "CALL-NATIVE", CALL_NATIVE
pop rax ; stack-top
pop rbx ; entry
pop rcx ; arg
mov [native_saved_rsp], rsp
mov [native_saved_rsi], rsi
mov [native_saved_rbp], rbp
mov [native_saved_r15], r15
and rax, -16 ; 16-byte align the new stack
mov rsp, rax
mov rdi, rcx
sub rsp, 32 ; shadow space for the Microsoft x64 ABI
call rbx
cld
mov rsp, [native_saved_rsp]
mov rsi, [native_saved_rsi]
mov rbp, [native_saved_rbp]
mov r15, [native_saved_r15]
push rax
NEXT

That’s the whole bridge. Save the registers the Forth interpreter cares about, switch stacks, put the argument where both calling conventions will find it, call, restore, push the result. From the Forth prompt, playing DOOM looks like this:

CD doom
INCLUDE doom.f
DOOM

doom.f is about a hundred lines of Forth. It finds an IWAD in the current directory, prints a dot per megabyte while it pulls doom.bin and the WAD into RAM, fills in an eleven-cell boot block (where the WAD lives, where the heap starts and ends, the address of a kernel service table, the current directory), and calls CALL-NATIVE. I like that the loader is Forth. It means the thing that decides how DOOM starts is editable in Tom’s own editor, on the running system.

Here’s where everything lives while DOOM runs:

AddressContents
0x1000000 to 0x1BFFFFFdoom.bin code, data, and BSS
0x1C00000 to 0x1FFFFFFDOOM’s 4 MiB stack
0x2000000The IWAD
After the IWAD, to 0x6FFFFFFHeap for malloc
0xF00000 to 0xF03FFFSound Blaster DMA buffer

QEMU now needs -m 128M. That’s the one thing about f64OS the port made heavier.

What I had to bring with me

A guest who shows up with nothing ends up rummaging through your drawers. So DOOM brings its own supplies:

PartFileLinesWhat it does
Loaderdoom.f~100Reads doom.bin and the WAD, fills the boot block, calls CALL-NATIVE
Entryf64_entry.c74Clears BSS, starts DOOM, catches exit() so it returns to Forth
Platformdoomgeneric_f64.c592VGA mode 13h, PIT at 1000 Hz, scancodes, mouse, ENDOOM
C librarylibc.c1,333First-fit malloc, strings, printf, sscanf, files in RAM
Sound effectssound_f64.c506Sound Blaster 16 driver, 16-channel mixer, IRQ 5 handler
Musicmusic_f64.c806MIDI player for the OPL2 using the WAD’s GENMIDI instruments
Diskdisk_f64.c342ATA PIO and the f64OS directory format, for saves and config

About 3,700 lines of new C. The resulting doom.bin is 448 KB. The changes to Tom’s kernel were deliberately small: turn on SSE in CR0 and CR4 so compiled C can run, keep a ring buffer of raw scancodes alongside the existing keyboard driver, add a kernel service table with the Forth word KSERVICES to find it, add an IRQ 12 handler for the mouse, and print the faulting RIP and CR2 to the screen and COM1 when the CPU throws an exception. That last one isn’t for DOOM. It is for whoever is staring at a black screen wondering what just happened.

Putting everything back

This is the part I’m proudest of, and it’s the part nobody will ever notice, which is the point.

When DOOM starts, it switches the VGA card into mode 13h, 320 by 200 with 256 colors. Mode 13h stomps on everything the text console depends on, including the font. In VGA text mode, the character shapes live in plane 2 of video memory, and switching to graphics overwrites them. So before DOOM draws a single pixel, the platform layer saves every VGA register, all 768 bytes of the color palette, and the font out of plane 2. When you quit, it puts all of it back, shows you the ENDOOM screen like it’s 1993, waits for a key, and returns to Forth. Tom’s status bar is still ticking. The ok prompt is still there. You can type DOOM again.

The same rule ran through every other piece:

  • The PIT runs at 1000 Hz while DOOM plays, so a kernel tick is a millisecond. It goes back to Tom’s rate on exit.
  • The Sound Blaster driver installs its own IRQ 5 handler. On exit it restores the old vector and the old PIC mask.
  • The OPL2 reset releases every envelope before it clears the registers. If you just zero the registers, the chip can freeze mid-waveform and leave a DC offset on the line, which on real speakers is a quiet, annoying thump that never goes away.
  • The kernel never turns on the PS/2 mouse. DOOM enables it through the 8042 controller when it starts and disables it and restores the controller’s configuration when it stops. The Forth prompt behaves exactly as it did before DOOM ever existed.

None of this makes DOOM better. All of it makes f64OS still Tom’s.

A port that leaves the host in a weird state isn’t a port. It’s a break-in with good frame rates.

Where it got surprising

The Windows toolchain is a PE toolchain wearing a Unix costume. MSYS2 gcc happily compiles freestanding C, but it emits Microsoft-ABI PE objects, not ELF. That meant -mno-stack-arg-probe so gcc wouldn’t call a __chkstk that doesn’t exist, -fcommon for DOOM’s old-school globals, and a linker script that puts .text at 0x1000000. My first build set the PE image base to the load address too, which put the code at relative address zero and produced a 17 MB flat binary that was almost entirely padding. It is also why CALL-NATIVE puts the argument in both rcx and rdi and reserves 32 bytes of shadow space: the same Forth word works whether the binary came out of a Microsoft-ABI or a System V compiler. I’ll be honest that the Linux build path exists in the Makefile and has never been tested. It builds in theory. Theory is where untested code lives.

Turning on SSE is a one-way door I only half walked through. gcc assumes x86-64 means SSE, so DOOM is full of XMM instructions, and the kernel had to enable them. But Tom’s task switcher doesn’t save XMM registers, and I didn’t change it. That’s fine today because only the Forth task runs native code, and the status bar task doesn’t touch floating point. It is also exactly the kind of assumption that bites somebody in two years. It’s written down in the README so that somebody at least gets a warning.

I found two bugs in the host’s house, and one of them was mine to find only because of DOOM. Adding the mouse exposed that the keyboard interrupt handler read port 0x60 without checking whether the byte came from the keyboard or the mouse. The 8042 can deliver mouse bytes on IRQ 1, and in QEMU’s SDL window they showed up as phantom keypresses. That bug was in the original kernel, waiting for a mouse. The other was pure accident: Forth uppercases input, so typing exit at the prompt ran EXIT, which popped a return address that didn’t exist, jumped into data, and threw a General Protection Fault. Also in the original. Both got their own small, separate commits, because if you notice the host’s sink is leaking, you mention it and you fix it, and you don’t bury it inside a commit titled “Add DOOM sound.”

QEMU on Windows lies about the screen. Once the Sound Blaster DMA was running, the default GTK display window went solid black in graphics mode. QEMU was rendering the frames correctly. The window just stopped showing them. The fix was -display sdl,, and the cost was a stretch of time spent convinced the VGA code was broken. I verified the audio the same way I wish I’d verified the video from the start: record QEMU’s output to a WAV file and measure it, instead of trusting my ears or my eyes.

The build tried to eat my savegames. In f64OS, the disk image is rebuilt from scratch out of a disk_files/ directory every time you run the build. That’s fine when every file comes from the host. It’s a problem once DOOM starts writing savegames to the f64OS disk over ATA PIO, because the next rebuild throws them away. So before each build, a small Python script reaches into the old os.bin, reads Tom’s directory format, and copies any .dsg and .cfg files back out into disk_files/doom/. The disk image is the master copy. The build just has to respect it. While I was in there I found that DOOM’s config loader quietly zeroed any key binding with a code above 127, which is where doomgeneric puts Fire, Use, and Strafe. Your settings saved fine. They just came back without the ability to shoot.

What I’d tell another builder

Find the smallest bridge. The entire interface between Tom’s Forth and 450 KB of C is one assembly word and an eleven-cell array. Every time I was tempted to add a second mechanism, the boot block or the service table already had room for it.

Be a good guest. Save the state you change, restore it when you leave, and test the leaving as hard as you test the arriving. “Quit, then run it again” was in my test plan from the first build, and it caught more bugs than playing the game did.

Measure the thing, not your impression of it. The black screen was QEMU, not my VGA code. A recorded WAV, a serial log on COM1, and a headless screendump settle arguments faster than staring does.

Report the host’s bugs separately. If your port finds a bug in somebody else’s code, that is a gift to them. Wrap it like one: small commit, clear message, nothing else in the box.

Admit what you didn’t test. The Linux build, the XMM state, real hardware. Saying so in the README costs nothing. Discovering it in someone else’s bug report costs your credibility.

On the AI in the room

This port was built with Claude doing most of the typing, and every commit says so. I set the design rules, made the calls about what to change in Tom’s kernel and what to leave alone, drove the testing, and argued with it when it wanted to be clever. It wrote the drivers, the C library, and the linker gymnastics, and it was very good at the parts that are mostly reading old hardware documentation carefully: 8042 command bytes, SB16 DSP commands, OPL2 register maps.

It was less good at noticing when a fix in one place quietly broke an assumption somewhere else, which is the job that’s still mine. The XMM caveat, the “test quitting, not just playing” rule, and the decision to split host bugs into their own commits all came from a human who has cleaned up after enough clever guests. A day of directed work produced something that would have taken me a few weekends alone. Not fifty times faster. Fast enough to make a joke into a merged pull request before the joke got old.

What it intentionally does not do

  • It does not make f64OS a general-purpose C platform. There is no loader for arbitrary executables, just a convention for one flat binary at one address.
  • It does not save XMM state across task switches. Only the Forth task can run native code.
  • It has never run on real hardware. ATA PIO, the 8042, and an SB16 at the default ports should all be there on an old enough PC, but “should” is doing a lot of work in that sentence.
  • It does not ship any WAD files. Use the shareware doom1.wad if you own it, or grab Freedoom, which is BSD-licensed and what I tested with.

What’s next

  1. Save and restore XMM state in Tom’s task switcher, so native code can run in more than one task.
  2. Test the Linux build path, so the Makefile’s claim is a fact instead of a hope (though Tom has reported that it works fine on Linux)

Closing

There’s something satisfying about a system where the operating system, the language, the editor, and the loader for a 1993 shooter are all made of the same few hundred Forth words, and you can open any of them in an editor that’s also made of those words. Tom built a house where you can see every beam. I just wanted to come over, make some noise, and leave it exactly as I found it, minus a couple of leaks.

Thanks, Tom. The forks are where you left them.


Repo: github.com/tliston/f64OS. The DOOM port lives in doom/ and disk_files/doom/. The doomgeneric code is GPL-2.0. Freedoom is BSD.

Response to “DOOM Is a Houseguest: Porting DOOM to Tom Liston’s Forth OS”

  1. Marcus Ranum

    I suppose “can it run DOOM?” Is our generation’s “can you play Freebird?”

    Like

Leave a Reply