“Static recompilation” is “the difference between weeks of work and years of work.”

N64

In recent years, we’ve reported on multiple efforts to reverse-engineer Nintendo 64 games into fully decompiled, human-readable C code that can then become the basis for full-fledged PC ports. While the results can be impressive, the decompilation process can take years of painstaking manual effort, meaning only the most popular N64 games are likely to get the requisite attention from reverse engineers.

Now, a newly released tool promises to vastly reduce the amount of human effort needed to get basic PC ports of most (if not all) N64 games. The N64 Recompiled project uses a process known as static recompilation to automate huge swaths of the labor-intensive process of drawing C code out of N64 binaries.

While human coding work is still needed to smooth out the edges, project lead Mr-Wiseguy told Ars that his recompilation tool is “the difference between weeks of work and years of work” when it comes to making a PC version of a classic N64 title. And parallel work on a powerful N64 graphic renderer means PC-enabled upgrades like smoother frame rates, resolution upscaling, and widescreen aspect ratios can be added with little effort.

Inspiration hits

Mr-Wiseguy told Ars he got his start in the N64 coding space working on various mod projects around 2020. In 2022, he started contributing to the then-new RT64 renderer project, which grew out of work on a ray-traced Super Mario 64 port into a more generalized effort to clean up the notoriously tricky process of recreating N64 graphics accurately. While working on that project, Mr-Wiseguy said he stumbled across an existing project that automates the disassembly of NES games and another that emulates an old SGI compiler to aid in the decompilation of N64 titles.

YouTuber Nerrel lays out some of the benefits of Mr-Wiseguy’s N64 recompilation tool.

“I realized it would be really easy to hook up the RT64 renderer to a game if it could be run through a similar static recompilation process,” Mr-Wiseguy told Ars. “So I put together a proof of concept to run a really simple game and then the project grew from there until it could run some of the more complex games.”

A basic proof of concept for Mr-Wiseguy’s idea took only “a couple of weeks at most” to get up and running, he said, and was ready as far back as November of 2022. Since then, months of off-and-on work have gone into rounding out the conversion code and getting a recompiled version of The Legend of Zelda: Majora’s Mask ready for public consumption.

FURTHER READING

Perfect Dark finally gets the full-featured PC port it deserves

At its most basic level, the N64 recompilation tool takes a raw game binary (provided by the user) and reprocesses every single instruction directly and literally into corresponding C code. The N64’s MIPS instruction set has been pretty well-documented over years of emulation work, so figuring out how to translate each individual opcode to its C equivalent isn’t too much of a hassle.

An early beta of the RT64 renderer shows how ray-tracing shadows and reflections might look in a port of <em>Wave Race 64</em>.
Enlarge / An early beta of the RT64 renderer shows how ray-tracing shadows and reflections might look in a port of Wave Race 64.Nerrel

The main difficulty, Mr-Wiseguy said, can be figuring out where to point the tool. “The contents of the [N64] ROM can be laid out however the developer chose to do so, which means you have to find where code is in the ROM before you can even start the static recompilation process,” he explained. And while N64 emulators automatically handle games that load and unload code throughout memory at runtime, handling those cases in a pre-compiled binary can add extra layers of complexity.

ARS VIDEO

How Lighting Design In The Callisto Protocol Elevates The Horror

EDGE CASES

In addition to the standard N64 graphics microcodes, a small subset of games that use custom microcodes require extra work to support in the renderer. In particular, Factor 5’s games (such as Star Wars: Rogue Squadron and Indiana Jones and the Infernal Machine) are known for having particularly difficult custom microcodes that need to be deciphered, Mr-Wiseguy said.

With the game logic converted to C, the graphical processing is handed off to the RT64 renderer, which converts the N64’s graphical microcodes into low-level API calls in either DirectX12 or Vulkan. RT64 can perform many graphical enhancements automatically, but a human coder may still need to manually tweak some things to get a PC port to perform as expected. Adding widescreen support, for instance, can mean diving into the code and telling the game to render content that would usually fall outside of the usual 4:3 CRT box.

A visualization of how the RT64 renderer tracks in-game objects to create new frames for smoother frame rates.
Enlarge / A visualization of how the RT64 renderer tracks in-game objects to create new frames for smoother frame rates.Nerrel

Smoothing out the N64’s usual 20 to 30 fps frame rates also involves more than just modifying a “framerate” variable in the C code. RT64 pulls off this smoothing by tracking each in-game object’s position a few frames ahead, then performing calculations to determine where that object should be for the “in-between” frames needed for smoother play. “RT64 is capable of doing some of that detection automatically, but the auto-tracking isn’t perfect, so extra tracking data is added to the game’s logic to fix any errors that come up in the automatic tracking,” Mr-Wiseguy said.

Despite the name, RT64 doesn’t currently support the ray-tracing capabilities that have been shown off in some impressive N64 conversions thus far. Other features like DLSS frame generation, support for high-resolution texture packs and custom models, and free-floating cameras are planned for future builds of the renderer.

The mod squad

Unlike manual decompilation projects, which are designed to be easily read (and potentially edited) by humans, the code generated by the N64 recompilation tool’s build process is “not very readable unless you know the assembly language used on the N64’s CPU,” Mr-Wiseguy said. Thankfully, he added, “there’s basically no reason to read the recompiler output code besides debugging if something isn’t working.”

Did someone say widescreen? Recompilation makes it possible in games like <em>Majora's Mask.</em>
Enlarge / Did someone say widescreen? Recompilation makes it possible in games like Majora’s Mask.Nerrel

Coders who might want to add some mods to their recompiled PC ports can still do so, Mr-Wiseguy said, by writing new C code that fully replaces a portion of that messy output during the build process. The only difficulty there is in “reverse-engineer[ing] the game in order to find what code to replace,” he said, which can be a difficult process if a human-readable decompilation doesn’t already exist.Advertisement

LEGAL WRANGLING

Nintendo has a long history of taking action against various projects that remix its intellectual property. But Mr-Wiseguy said he hopes the layers of abstraction between his tool and any copyrighted code will legally protect it.

“The recompiler itself has absolutely nothing in it from any games and is mainly just information about the MIPS instruction set,” he pointed out. “There’s less of the original game in the compiled binary than there is in even a decompilation repo.”

Even for modders, though, Mr-Wiseguy thinks recompilation can save a lot of manual work. In a manual decompilation, he explained, every single portion of the game logic has to be reverse-engineered before you can even build the port in the first place. With recompilation, Mr-Wiseguy said, “you only need to reverse-engineer just enough of the game to figure out how to make the mods you want,” and you can let the automated system handle the rest. That means “fewer opportunities to accidentally introduce bugs in the porting process,” he added.

While the recompilation tool does a lot of the nitty-gritty work for you, Mr-Wiseguy warned that this still isn’t a completely turnkey system for converting an N64 game to a PC executable. Going from “knowing absolutely nothing to having a port running” for a game like Superman 64 still took about 10 to 15 hours of coding to massage the recompiled output into a PC-playable form, for instance. “It’s a very technical process on top of the reverse engineering work needed for a given game,” he said.

The PC port of <em>Superman 64</em> we've all been dreaming of is finally feasible.
Enlarge / The PC port of Superman 64 we’ve all been dreaming of is finally feasible.Nerrel

For games with already well-understood structures, though, the PC porting process may take even less time. “I’ve already helped people get projects started for some other games or even existing mods… there was some feedback about usability and some edges cases did come up, but a few of them have already gotten their target running in just the few days since this came out,” Mr-Wiseguy said.

Mr-Wiseguy said there are plans to add Lua output to the tool to enable easier runtime modifications and renderer support for the Apple Metal API to help enable Mac ports. For now, though, he has been gratified by the reception for what is already available. “I’ve put a lot of work into the project, and I know it’s something a lot of people are enjoying,” he said.

LEAVE A REPLY

Please enter your comment!
Please enter your name here