How a team of AI agents decompiled Pool of Radiance

From 8 proven routines to all 804 in three days, then one command that printed the word identical: how the system worked, how it fell apart and what it really cost.

19 min read decompilationAI agentsTurbo Pascal

The terminal after make rebuild: 51 of 51 segments, 561 of 561 relocations, the overlay file, and one word, IDENTICAL

Seven days after I began this project, I ran a single command which displayed a single word: identical. This meant that we had fully recreated the 1988 game Pool of Radiance from source code. We had managed to take that source code and compile it into the exact same 51 segments, 561 relocations and overlay file, byte for byte, using the same compiler as the original developers.1 Three days before, only 8 routines of the game had been proven. Now it was all 804.

This article is about how we did it: how me and a team of AI agents, most of them cheap, achieved this. But also how things fell apart, what it actually cost, and what I would do again.

There's a video which covers the same ground, but this written version includes the code, the tables and the links.

Every episode in the playlist

What a matching decompilation is

My boxed copy of Pool of Radiance

I have a boxed copy of Pool of Radiance, from SSI, one of the Gold Box games, the Advanced Dungeons and Dragons role playing games of the late eighties. On the PC it's a 65 kilobyte START.EXE plus a 206 kilobyte overlay file. Between them, they hold about 236 kilobytes of game specific machine code. There's no source code, so we made some.

There are a few different ways you can decompile, but the strictest is a matching decompilation. This means you write the source code in the original language and compile it using the original compiler. If the machine code comes out the same, it's done. If it differs by a single byte, it's not done. This is a key tenet of the project, expressed in the spec: the compiler, not a test, says the source is the program.2

Here's an example, a routine that says how much weight a character can carry:

Pascal
{ ovl21:0000. The most weight Who may carry: 1500 plus the strength allowance
  StrengthWeightAllowance (negative for a weak character). }
function CarryLimit(Who: PCharacter): Word;
begin
  CarryLimit := StrengthWeightAllowance(Who) + BaseLoad;
end;

The listing below is the original's machine code for that address. It's what Turbo Pascal 6.0 has to reproduce from these few lines of Pascal code:

x86 assembly
0000  55               push   bp
0001  89e5             mov    bp, sp
0003  83ec02           sub    sp, 2
0006  ff7608           push   word ptr [bp + 8]
0009  ff7606           push   word ptr [bp + 6]
000C  9aab13ba00       lcall  0xba, 0x13ab   ; StrengthWeightAllowance
0011  05dc05           add    ax, 0x5dc      ; 1500
0014  8946fe           mov    word ptr [bp - 2], ax
0017  8b46fe           mov    ax, word ptr [bp - 2]
001A  89ec             mov    sp, bp
001C  5d               pop    bp
001D  ca0400           retf   4

Here's the verdict from a tool:3

Output
MATCH   CarryLimit       ovl21:0000     32 bytes code,   0 pool, 1 address fields
Our compiled bytes against the original's: the address field is compared by what it points to, the constant 1500 must be identical

This one line looks simple, but there's an interesting subtlety here. The single address field it mentions is a result of the way we match. We match a routine while the rest of the program is still empty stubs, which means the addresses will not be where the original's were. The far call instruction in the listing above is an example of this, and we can't compare that byte by byte. To deal with this, the matcher compiles everything twice, the second time with padding pushed into every unit. A number that moves between the two compiles is an address, and we compare it based on what it points to. A number that doesn't move is a constant and has to be identical, for instance 1500, which is 05DC in hexadecimal.

The matcher's two compiles: the address moves between them, the constant stays

This is why the problem is so interesting for AI agents. An agent can be confidently wrong. A matcher can't. If the only way to succeed is a yes from the compiler, a confidently wrong answer is not good enough. You can let a cheap model try, and try again, and you never have to trust it.

Finding the exact compiler

But this only works if you have not just the right compiler but the exact one. There is a hint right in the program. The unpacked executable has Borland's runtime error messages and Portions Copyright (c) 1983,91 Borland. So it's Turbo Pascal.

Turbo Pascal was my first real programming language after BASIC, and I still have my first Turbo Pascal book. So discovering this at the bottom of Pool of Radiance made this project extra fun for me.

But which Turbo Pascal? As the following table shows, we compiled a test program with each candidate and compared their runtime libraries byte by byte against the one in the game:4

Candidate Bytes that couldn't be explained
Turbo Pascal 7.0 about 900, in the System unit alone
Turbo Pascal 6.0, October 1990 6
Turbo Pascal 6.0, June 1991 0

The funny thing is that the June 1991 compiler prints the exact same banner as the October 1990 one: same version number, same copyright years. You cannot ask it. Only the bytes tell.

The first plan, and the pivot

My first plan was C. My first architecture decision record, ADR-001, said I would recreate each routine in C, run it in the emulator in parallel to the original, and compare the two: proof by testing.

It kind of worked, but it was a lot of work to create the custom marshalling code, fuzz harness and comparison mechanism for each routine, and it was only ever as good as the tests.

Four days in I pivoted to ADR-002: Pascal first, matched against the original compiler.2 As of that commit, 8 of 1,289 game routines matched and 2 units were done.

The tool stack

All the tools are exposed to the agents through a single interface, make, so the tool stack is worth explaining. The table below has it from the bottom up, starting at the last row.

Layer What it is
One door make, 87 targets, for the agents and for me
Run The oracle: a simulated PC in Python on Unicorn, in virtual time, with title screens that match DOSBox-X's pixel for pixel
Compare The matcher: identical, or the first difference
Compile Turbo Pascal 6.0, June 1991, under emu2, a tiny DOS emulator with no window
Unpack and map Our EXEPACK unpacker, the overlay manifest (35 units), Ghidra, Capstone
Originals START.EXE, GAME.OVR and the data, pinned by their hashes

Ghidra was a helpful first pass to get the lay of the land, but the real ground truth is plain text files in the repo, like re/symbols.toml, which hold things like names, types and status. The output of the Ghidra decompiler is used as a hint, and the agents mostly read a simpler disassembly generated with Capstone.

The oracle records every read of the outside world (key presses, timer ticks), and these recordings are referred to as the goldens. The goldens are the ultimate arbiter of whether the reconstructed game runs the same as the original.

When an agent asks for a routine, it gets a work packet: a one pager with the annotated disassembly, stack frame, callers, current source, and the lessons left by earlier agents about this compiler. The table below shows the progress ladder. Notice how there's no level one.

Level Means
L0 Drafted Pascal that compiles
L2 Matched by make match
L3 Rebuilt: part of a whole program that is byte identical and plays the goldens

Only tools can write a level. Not the agents, and not even me.5 The rules for all of this are documented in one file, AGENTS.md. I am in Claude Code and the agents are in OpenCode, on models from several providers, but changing the model or the harness does not affect the tools themselves.

The loop for one routine: packet, Pascal, make match, L2

The real meat of the system is a per-routine loop: read the packet, write the Pascal, run make match, and retry if there's a difference. If the agent gets stuck on an idiom, it can run make tpc to compile a small probe and see what the compiler does with it. Once the bytes match, make match WRITE=1 records level two in the registry. The rest of this article is about how we keep that loop fed and honest.

Night one

It took a little over an hour and a half from the pivot for the multi-agent tooling to be in place. Basically an agent would claim a unit, which would take a lock and give it a git worktree, its own copy of the repo on its own branch. It would then decompile routines, prove them and hand off the branch. Then the orchestrator, the agent coordinating the others, would merge the hand-offs into main.

That first night I ran everything manually, and the orchestrator was itself a model session.

The first routine matched by an agent landed about three hours after the pivot. It was DrawAreaView, which paints the area map to the 3D window of the game. In that day alone we had 432 commits.

But the night went as first nights go. There were two places things went wrong.

Merges were where work died. Two agents would both add a name to the shared symbol registry and the merge would lose one. We had six merges reverted on main that first day.

Locks were held by mortals. Model sessions hold locks while they are working, but models can only hold so much text at once, and when the context window fills up the session ends. Some model sessions gave their locks back cleanly and some just stopped. This is why heartbeats exist. Every agent must post what it is doing at least every 20 minutes, because a silent agent looks just like a hung one.

Still, about 15 hours after the pivot, 316 routines matched, with a system held together by hand.

Every block was a tool gap

The next day was slow: about 5,200 bytes in fourteen and a half hours. Things were crawling. So that evening I decided to stop and get a review of the system as a whole, with a source for every claim. The verdict was: "The core is right and fast. The coordination layer around it is what costs you."6

Why 51 routines were blocked: nested routine limit 29, the sound driver 21, cross-unit address 4

It was a finding I keep coming back to. Looking at the 51 routines that were marked blocked by agents giving up, their notes named a limit in the matcher's nested routine comparison (29), or the sound driver (21), or an address crossing between units (4). That adds up to more than 51, so a few notes must have named two. None said they couldn't find the Pascal. The agents were not the problem. The tools were.

To make it worse, I had a rule that only a human could change the matcher. You want to protect the judge, but it parked up to 50 routines behind it for most of a day, waiting for me.

The sound driver is a great example. 21 routines in one unit could never match, because they were never Pascal: 5,721 bytes of assembly, linked in as an object file. The matcher had to learn that code can come from outside and the unit can still be complete.

It took about 25 minutes of commits that evening, but the coordination layer was rebuilt:

  • A deterministic integrator: a script, not a model, that merges each hand-off in a temporary worktree, runs every check, and only then moves main forward.7
  • A match gate: a matcher change is accepted without me if every matched routine still matches and every known bad example is still rejected.
  • A merge driver that merges the shared registries record by record, so two agents only conflict if they touch the same record.8
  • A worker supervisor, which turned out to be the biggest change of all.

Agents that stop, and an expert nobody needed

Two days after the pivot, I started my first proper run. A single command created a terminal tab for each entry in the roster, and I instructed each model to continue in a loop until I told them to stop.

Well, I never told them to stop, they did it themselves. Within 90 minutes, all the interactive decompilers and the reviewer had ended their turns with work still on the queue. This applied to all five models performing decompiling and reviewing. No matter what the prompt says, eventually the model will want to end its turn on its own.

I needed to move the looping out of the model and put it in a worker supervisor, a Python script.9 This would claim the unit, hold the lock, and launch a new model session for each batch of routines. Each session could take six routines and last up to 90 minutes. If and when the session ended, for any reason, the supervisor would run the matcher itself. Any work that did not match would be set aside as a patch, and only what did match would be recorded and committed.

The supervisor ring holds the lock around the loop, and a script replaces the model orchestrator

Then there was the expert. The concept was good in theory: a stuck cheap worker asking an expensive model for advice. In practice it didn't work. The first run used all five allowed cases in twenty minutes, four of them on routines already matched. There's a deeper issue too. The expert's advice is not a proof. The cheap worker still has to figure out how to prove the expert's advice, and one routine ended up requiring six round trips.

So I retired the expert and replaced it with an escalation tier. This is a stronger model that doesn't advise, but actually decompiles. If a routine failed two sessions, it would move to the frontier and not be offered to cheap workers again. It would go to the escalation tier, which takes one routine per session, gets the history of every failed attempt, and has a 150 minute and 8 dollar limit. If that also fails, it goes in my inbox.

What it cost

When I first went to write this part, the project's own meter said 28 dollars 54, but that's only what the agent tools could account for. The real numbers from the OpenCode session database are 473 dollars and 51 cents, 249 sessions, a little over four days.10 See the table below, broken down by model.

Model Sessions USD
Muse Spark 1.3 15 175.04
Grok 4.7 18 97.03
Claude Haiku 5.5 53 52.97
Claude Sonnet 5.5 62 39.69
Claude Opus 5.5 2 35.28
DeepSeek V4.1 flash 88 31.66
GPT-6.1 2 26.49
GLM 5.3 3 9.96
Others 6 5.39

The big number is the first night: Muse Spark, the model behind the busiest hand-started decompiler, at 175 dollars across just 15 sessions, with one decompiler session alone at 38 dollars.

In contrast, DeepSeek was about 34 cents per kilobyte matched on the day we measured it, versus 1.06 to 1.39 dollars for Haiku at the same speed, so tier one, the cheap tier doing the bulk of the work, settled on three DeepSeek workers.11

For the hard tail I ran a small A/B test. Two routines had each failed two Grok sessions, at about 17 dollars a session. I gave each to a single Claude Sonnet session and both matched, for 4.16 and 2.91 dollars. Over that last day Grok was about 30 dollars per matched routine and Sonnet about 3, and I retired Grok that afternoon.

The lesson is not that one model is good and another bad. You only know when you give them the same problem, and the price per token does not tell you the price per result.

The most infuriating failure was the kind that looks like work. The night after that big run started, cheap workers kept claiming units where all remaining routines were stuck, giving them back and claiming them again. 225 claims in four hours, and over eight hours of night the matched byte count went up by five bytes. The fix was one line: tier one stopped claiming a unit with nothing left it was allowed to do.

My own part was in Claude Code on my subscription. Priced at today's API rates, that would be at least 478 dollars through the rebuild, and at least 1,175 for the whole project so far including the CoCo 3 port.

So for the decompilation: 474 dollars actually spent on agents plus a subscription, about 2 dollars per kilobyte of proven code, around 60 cents a routine. Not a bargain, because most of that money was waste and the waste was mine. See it phase by phase in the table below.

Dollars per kilobyte, phase by phase
Phase Dollars per KB
Night one, hand-run, mostly Muse Spark 2.09
The redesign day 7.55
The supervised runs, cheap workers under a script 1.23
The dead night 28.64 dollars for 5 bytes
The hard tail, mostly Grok 2.49
The next project, start to finish 0.52

The best evidence is the last row of that table. I have since pointed the same system at another game from the same era, written in C. It matched about 220 kilobytes in about a day and a half, a quarter of what Pool of Radiance cost per kilobyte.

The finish line

Matched routines climbing to 804 while the total falls from 1,289
Hours after the pivot Routines matched Bytes Units complete
0 8 of 1,289 534 2
15.2 316 of 1,190 87,639 29
29.6 326 of 1,170 92,869 31
51.9 403 of 1,113 123,391 36
58.5 627 of 1,008 178,951 40
59.2 665 of 962 195,046 40
67.9 666 of 962 195,051 41
72.6 804 of 804 235,599 46

Take a look at the denominator here. The number of routines went from 1,289 down to 804. This is because the original list was derived from a static map of the binary, which was flawed. It would split routines in the middle of their data, and it once read a far call as an address and invented a routine there. The fix was to stop guessing and let the compiler say where its routines really start and end, and record that in the registry. More than a third of the map's original entries turned out not to be routines at all.

One lane remained. When a unit is complete and merged, a different agent reviews it.12 This is really to catch things that the tools can't. Are the names and comments accurate? Could someone port the unit from the source alone? If the reviewer finds something that must be fixed, the unit is sent back to a decompiler. All 46 units were signed off within the hour of the last match, and the reviews closed 107 must-fix findings across the project.

Less than three hours after the last unit landed, the rebuild came back identical, where two days earlier the same command had said 4 of 51.

What I would tell you to copy

  1. Find your judge. This worked because we had a yes or no answer that no agent could talk its way around. If your problem doesn't have one, build one first, before you build the agents.
  2. When agents block, suspect the tools first. We had 51 blocked routines and none of them were blocked on the actual decompiling.
  3. Put the loop outside the model. Sessions end, so let them. The state, the lock and the plan belong in a script and in the repository, and every session should be disposable.
  4. Merge mechanically. Shared files should merge record by record, every merge should be checked in a scratch copy, and main should stay green.
  5. Measure models on the same task and price them per result.
  6. Write down what you learn where the next agent will read it. There are 280 lessons in the lessons files now and 155 small probe programs that settle how the compiler handles an idiom.
The whole harness, with the six lessons pinned where they live

One more, for the game itself: faithful means keeping the original's oddities. 293 places in the Pascal are marked as quirks, things the original code really does, like assigning a variable to itself:13

Pascal
{ QUIRK 042B:032D: the original copies GameDir onto itself (System_StrLCopy with the same
  source and destination, limit $FF). Kept: the source's own assignment emits the bytes at 042B:032D. }
GameDir := GameDir;

The compiler will not let you clean that up, and that is the point.

Making the original faster

Now, this is the fun part. We have source we know rebuilds the original, so we can go and modify it, carefully. Let's make it faster.

Pool of Radiance is a slow game on my Tandy 1000 EX, with its 8088 running at a puny 7.16 megahertz. We profiled the original game using 12 recorded sessions against a model of the 8088, which estimates the cost of every instruction on the real chip. This showed us that, leaving out the waiting, 28% of the work was in Turbo Pascal's Move routine, which copies one byte at a time.

Where the work goes on the 8088 model, with waits left out: Move 28.0 percent, masked sprites 22.4, text 15.8, screen flush 10.3

We rewrote the slow parts of the game with one rule: don't modify the decompiled source, but instead create patches applied to a copy of it, so you can always toggle back to the faithful build. This gave us 8 patches which each rewrite one or a few routines in Turbo Pascal's built-in assembler. They cover the data unpacker, the text, the sprite drawing and mirroring, the window clears and screen flush, the picture display, the line of sight in combat, and the target sort.

The results are in the table below. Note that these come from the model, not a stopwatch against the real hardware.

Before and after on the 8088 model
On the 8088 model, Tandy video Before After
A step in the 3D view 1.2 s 0.55 s
Start-up 10.0 s 3.6 s
Character sheet 1.6 s 0.4 s
City Hall guards battle 95.9 s 34.5 s

We can't just replay a session with the faster game to know nothing else changed, since the game now polls the keyboard a different number of times and the replay drifts. Instead the checker uses a clock that belongs to the program, which advances only during waits. For each of 12 recorded sessions, at each key, it compares game memory, the screen, all random rolls, all files written and all sounds. This even helped us catch a real bug on our side (we were copying a sprite backwards over its own header!).

Also, playing on the real EX let us find a bug in SSI's original code: put a character carrying darts on quick combat, in just the wrong spot, and the game will hang forever! We replicated this in the emulator and fixed the bug in a common folder so it was fixed on all ports.

Asking the game questions, and drawing its maps

Not only that, but now that it's decompiled, we can ask questions and get answers about the game itself, directly from its code. As I was playing through, I kept asking Claude to clarify things about the rules, and it would answer by reading the source, all the way down to the line. One particular time, I appraised some jewelry that was worth over 7,000 gold but I didn't want to sell yet. The next time I looked, it was gone. Turns out this is the original working exactly as written. Why? Well, that's something I'll write up in a future piece about all the crazy little quirks in the game.

The game has this event system, where the events are scripted in a language called ECL, and the interpreter for those scripts is now readable source, so we were able to write a disassembler for those scripts based on that. This allowed us to create 29 one-page player maps directly from the files of the game. We decoded the walls and doors from the map data, and things like encounters, passwords, traps and treasure from the scripts.

We learned things like, if you attack the fortune teller in the slums, the gods take note and every encounter after that gets more likely. In the library, the reward needs the spectre dead, but the spectre only shows up if you take a book from the stacks. The walls and doors are generated, but the notes on each map were written by agents from the script listings, so they're as good as our reading of them.

A game you can work on

Proven source turned out to be a launch pad, and from there things went fast. Two days after the rebuild I had a C translation, generated from the Pascal, that was able to perfectly replay the recorded sessions. Eleven days after the rebuild I was running the game on a real Tandy Color Computer 3.

Another thing that's changed for me is my relationship with the game. Before, Pool of Radiance was like a sealed box: you played it, guessed at its rules and suffered its bugs. Now it's more like an open source project I can work on. In fact it's made me appreciate the original even more, in that every quirk I uncover is a decision made by someone at SSI decades ago, with a lot less than we have today.

If you want to dig in yourself, it's all in the GitHub repository at github.com/episcopus/poolrad-decomp, and the table below explains what's where.

The companion repository
Where What
AGENTS.md The rules every agent read
docs/spec/SPEC.md The spec and the architecture decisions
docs/process/playbooks/ The 12 numbered playbooks the agents followed
docs/design/WHITEPAPER.md How the game works
work/reviews/ A review of every unit
src/ The decompiled Pascal
c/ The C translation
ports/coco3/, ports/dos/ The CoCo 3 and DOS ports, with the speed patches
fixes/, tools/maps/ The bug fixes and the map tools

That's 171 markdown files and the full history of 1,702 commits. What you won't find is the game. You need your own copy, and your own Turbo Pascal 6, to build any of this.

Next up I'll dive deep with a series on how this classic game actually works. We'll start with an overview of the whole engine and then explore each subsystem in detail.

Notes and sources

  1. make rebuild; playbook 50-rebuild.md and the whitepaper. ↩︎

  2. docs/spec/SPEC.md, ADR-001 and ADR-002. ↩︎ ↩︎

  3. make match UNIT=ovl21 FN=CarryLimit. Source in src/OVL21.PAS, matcher in tools/match/match.py. ↩︎

  4. The version proof is in the whitepaper. ↩︎

  5. AGENTS.md, ground rule 2. ↩︎

  6. The system review, including the count of blocked notes. ↩︎

  7. Playbook 90-orchestrate.md. ↩︎

  8. tools/agent/merge_shared.py. ↩︎

  9. docs/process/WORKER.md and tools/agent/worker.py. ↩︎

  10. Summed from OpenCode's local session database, which is not part of the repository. The project's own meter records only what the tools could see. ↩︎

  11. The comments in work/roster.toml record the per-kilobyte and per-routine comparisons. ↩︎

  12. Playbook 70-review.md and work/reviews/. ↩︎

  13. src/RES08.PAS, line 95. grep -c QUIRK src/*.PAS counts the rest. ↩︎