Yellow marks are automatic key passages. They aren’t personal saved highlights. Select a timestamp to follow along with the video.
A fully optimized Mario 64 mod, rewritten with the goal of a steady 30 FPS on real Nintendo 64 hardware, covers reading and optimizing the entire source code, from memory layout and frame scheduling to compiler settings and rendering. The work rests on benchmarking each stage of the frame, then reducing RAM bus contention by separating CPU and RCP memory, batching rendering, removing duplicate variables, debug code, and safety checks, and moving shadow calculation to the RCP. These changes raised the frame rate from 29 to 41 FPS and make multiplayer split-screen practical, which the original game could not support. The project ends with a rewrite that is about halfway done, with new levels and a planned Return to Yoshi's Island release that includes built-in multiplayer.
A Fully Optimized Mario 64 Mod
By the author's account, the entire Mario 64 source code has been read, optimized, and bug-fixed. The game runs at a steady 30 FPS on real Nintendo 64 hardware. The new code renders the game up to six times faster than the vanilla Mario 64 code on a real N64. Every variable was examined and every function was understood before the code was optimized. The work covered everything listed in a 13,000-line map file, which adds up to over 100,000 lines of code read and potentially edited. The project took weeks and a few hundred hours of concentrated work.
Motivation and Earlier Work
The main goal was a higher frame rate and a less janky game. Over the past year, the author had already rewritten parts of the source to improve performance. One rewrite gained more than 100% performance in a graphics-heavy department. Another reworked the janky wall collision to behave more consistently and gained around 100% performance there as well. The author also read through all of Mario's triangle collision code. Mario's physics are too large a topic for this project, so they will be covered separately.
What Remained to Optimize
After these changes, the game's logic already runs in under 25 milliseconds, even though each frame has 33.3 milliseconds to process at 30 FPS. The question is how the game can be optimized further when the logic already seems as fast as it can be. The answer lies in the shared memory of the N64. A single RAM bus transports memory between different parts of the architecture. That memory is shared between the render unit, called the RCP, and the logic unit, the CPU. This architecture allowed the N64 to produce nearly every special effect of its era, including real-time shadows cast from actual geometry on objects like this teapot. These effects came at a large cost in performance.
How a Frame Is Built on the N64
Improving performance requires understanding how a frame is built on the N64. On the CPU, the physics run first, covering things like Mario's jumps, actors, and coins. Next, all sound effects and music are played. The CPU then builds a graph for the render unit. This graph describes where each object is positioned and where its animations are. Once that is done, the CPU waits for the render unit to be ready for the next frame, sends over the graph, and restarts the frame. On the render unit, the RCP waits for the CPU to send the graph. When it arrives, the RCP renders it. Because the CPU and the render unit can run in parallel, their work overlaps.
Parallel Work Across the First Frames
In the first frame, the CPU runs the physics, plays the sounds, and builds the graph. The RCP is ready, so it takes the graph and starts rendering the game. While the RCP renders, the CPU goes back to running physics, playing sounds, and building a new graph. Then the CPU waits for the RCP to finish with the previous graph. Because the two units almost always run in parallel, the CPU is usually done a bit sooner. This leaves the RCP with exclusive access to the RAM bus, which lets it run a bit faster.
Bus Contention and Why Faster CPU Work Helps
Parallel operation has a drawback. The renderer and the CPU share the same RAM bus, so they constantly compete for the same resources while running at the same time. If the CPU finishes before the RCP, the RCP can use the RAM bus entirely for itself and render new frames faster than it could while the CPU is still running. The sooner the CPU finishes, the less this infighting happens, and the faster a frame can be rendered.
Three Steps to Reduce Infighting
Three main changes reduce this infighting. First, RAM alignment is fixed so that the shared RAM is less of a bottleneck. Second, the compiler flags are changed. This goes beyond the common O2 optimization that most developers already know, and the changes go three steps further. Third, the source code as a whole was not optimized for these principles. Because of that, every function in the game needed to be rewritten to run well.
Benchmarking the Frame Budget
Before any optimizations, benchmarks were taken for each stage. All numbers are in microseconds, where 1,000 microseconds equals 1 millisecond. At 30 FPS, a frame has 33,333 microseconds to process. The CPU logic benchmarks cover the following. The sound benchmark measures the time needed to compute the sound for the frame. The render graph benchmark measures the time to build the graph. The level benchmark measures the time to process all of the game's physics. The render unit, or RCP, benchmarks cover the following. The TMM benchmark measures the time to load all currently drawn textures. The CMD benchmark measures the time to draw the frame. The CLK benchmark measures the time until the next frame starts rendering. As long as the total stays under 33,333 microseconds, the game can run a smooth 30 FPS. In the baseline, the CPU finishes processing in 28 milliseconds, leaving 5.3 milliseconds to spare. The RDP needs the full 33.3 milliseconds to finish, which gives exactly 30 FPS.
Switching Between RAM Segments
Advice from Wise Guy, who has read The Forbidden Texts, pointed to the importance of switching between RAM segments during runtime. Each of the eight RAM segments can be thought of as needing time to fill up with fuel for the RAM bus. If a read comes from a different segment than the previous one, that segment has time to fill up faster, which wastes less time. The author mapped out which RAM sections are accessed in parallel and used that knowledge to make sure the game cross-references them as it should. A detailed graph is available for anyone who wants to explore exactly why it was done this way. The real behavior of RAM is more complicated than this simplified explanation, but this approximation is good enough to understand what is happening.
Separate RAM Segments for CPU and RCP
The RCP and the CPU each have their own separate RAM segments, and this change produced a significant speedup. Sound improved considerably. Render time dropped substantially. The level script, for some unexplained reason, takes even longer. Texture memory and the CMD step became faster, which gained around the full milliseconds on the RSP. This approach would not have been possible with the 4 megabytes the original game uses. Using separate RAM segments like this requires the Expansion Pak, which is the reason the technique works at all. For that reason, Nintendo should not be blamed for not doing this. The hardware simply could not have supported it.
Code Review of the Mario 64 Source
The Mario 64 source code needs a code review, which means examining every line. Most of the mistakes in the original code are understandable. C was a fairly new language at the time, the developers did not know the hardware they were working with, and even programmers in 2022 make many of the same mistakes. Developers either learn these lessons or stumble into them, with no real middle ground.
Unrolling Loops and Illegal C Tricks
The first example of code that can be improved is loops that are not unrolled. Code is loaded from RAM, which holds up the RCP for a fair amount of time. Every instruction in a function takes eight extra cycles just to load. Unrolling loops reduces how often the slow N64 RAM must be read, so the code runs faster and frees up RAM bus capacity. Another trick uses so-called illegal C code to save one instruction. Normally this would get a programmer fired, but on the N64 every instruction counts.
Simplifying Functions and Removing Unused Variables
Some functions in Mario 64 can be made more elegant by removing unnecessary comparisons and loading less code. In one example, the finished code is only a fifth of the size of the original. The original game also uses many useless or unused variables and sets values that are never needed. Removing these, along with some variables unused in the mod, reduces RAM reads and writes, which gives the RAM bus more room. Nintendo had many programmers working on the game, and as a result, many variables were acquired twice because one programmer did not know what another had already written. The author estimates that about half of the variables being acquired had already been acquired earlier. Removing these duplicates reduces RAM reads considerably.
Removing Duplicate Functions and Patchwork Fixes
Many functions in the game have duplicate functionality. Removing these duplicates saves RAM and reduces instruction cache misses, which is very valuable for speed. The source also contains patchwork fixes for problems that were never addressed at their root. Fixing the underlying issues removes some jank and also saves performance. Luckily, there are not many such systems in Mario 64, but each one is quite silly when it appears.
Boolean Flags, Safety Checks, and Debug Code
Mario 64 typically uses distinct lists of different types. These can be turned into Boolean flags, which reduces the number of comparisons needed. As a side effect, this allows more complex collision types. For example, different sound effects and particle effects can now be applied to every surface. All of the original game's safety checks were also removed. Each safety check usually costs two to three useless instructions. Safety checks prevent crashes, but well-written code does not need them to avoid crashes in the first place. The original game also shipped with all of its debug code, which still runs checks whenever it is active. This adds a lot of unnecessary processing, and all of it can be removed.
Restructuring Data for the Cache
The types have been restructured to make the files more efficient. The CPU always loads 32 bytes at once into its cache. Files should therefore be structured so that variables accessed next to each other are also stored next to each other in RAM. This increases the likelihood that variables are already in the data cache, which means the RAM bus is used less.
Compiler Limits and Forcing Better Output
The 2022 version of GCC is not necessarily optimized for the Nintendo 64. It does not know all the instructions the N64 can use, and it sometimes compiles inefficient code. To work around this, the compiler is sometimes forced to produce the correct output by inlining assembly functions. This makes GCC generate the optimal result and yields a small overall speedup. When a function is called by only one other function, the two can be merged into one to save some processing time. When a similar code snippet appears in many functions, it is often better to outline it into a separate function that only runs that snippet.
Outlining Functions and Batched Rendering
Outlining a shared snippet into its own function means the same RAM address is used every time, so the code does not need to be reloaded each time. This makes it run about eight times faster. Many systems in the original source are inefficient and need to be rewritten to run properly on the N64. For example, the author implemented batch rendering, which lets the game render all instances of a type at once instead of reloading the same data over and over. This approach was first applied to coins alone.
Batching Repeated Rendering Work
Repeated work is also trimmed by batching it. When a function is called by only one other function, the two can be merged to save a little processing time. When a similar code snippet appears in many functions, it is better to move that snippet into its own function. The same RAM address is then used every time, so the code does not have to be reloaded on each call, and this makes it run about eight times faster. Many systems in the original source code are inefficient and need to be rewritten to run properly on the N64. Applying this batching to the coins alone saved almost 2 milliseconds of render time. The technique is common in modern rendering, but on the N64 it speeds up everything by a large margin.
Calculating Values as Few Times as Possible
Values should be calculated as few times as possible. The billboard matrix, for example, was being calculated for every single billboard instance. In Castle Grounds, that already meant 24 matrix calculations. This can be reduced to one, because the matrix only needs to be computed once per camera. The total improvement is substantial: the RDP is 2.8 milliseconds faster and the CPU is 7 milliseconds faster. The CPU needs only two-thirds of its previous processing time to perform the same calculations. This turned out to be worth more than expected, and the results were better than the developer had anticipated.
Removing the Debug Flag
Super Mario 64 is famously compiled with a debug flag that can significantly slow the game down. In stages such as Dire Dire Docks, removing that flag alone makes the code run about 30% faster. This change was made years ago, before any of the other optimizations described here. Compiler optimization involves far more than simply turning optimization on. The 2022 version of GCC is not well optimized for a console from 1996. This is not surprising, given how slow the N64's RAM is and the fact that it is shared with the renderer.
Favoring Short Functions and Per-File Compiler Settings
On the N64, it is almost always better to make functions as short as possible rather than as fast as possible. This is the opposite of the usual approach on modern computers. The developer switched between size and speed optimizations from file to file, depending on how each file's code is used. The developer admits having little experience with compilers, and without help from another AI model, figuring out how to communicate effectively with the compiler would not have been possible. Given how little effort it took to find good compiler flags, the improvement is striking. CPU speed stayed almost the same. The code now loads less, but the code that does load runs more slowly. Loading less code, however, greatly sped up the RCP, and the frame rate in the scene is now easily above 30 FPS.
Minimizing Data Access and Moving Shadows to the RCP
Code should also be written to need as little data as possible. Accessing data keeps the RAM bus busy, so reducing access leaves it free and makes it easier to load other data. One major offender is the shadow calculation. The original game goes through a large block of code that allocates new vertices and triangles to build shadows dynamically. Instead, a single matrix can be pushed and the entire calculation performed on the RCP. The RCP uses vector processing instructions, which are computationally much faster and need next to no data transfers. This actually speeds up the RCP, even though it does more total work, because the RAM bus is freed up.
Moving Write-Only Data to Uncached RAM
Data that the CPU only writes but never reads can also be moved to uncached RAM. The CPU has to write out a large amount of data for the RCP to render the game, which is the part of the process the developer calls the graph build. Moving this data keeps the cache free, while the other data still loads efficiently. Applying this change across the whole source code produces a large performance gain. Frame rate rose from 29 FPS to 41 FPS, a 41% increase. The rewrite also has other significant implications, which the following section covers.
Multiplayer Bottlenecks in the Original Game
The original game had bottlenecks in both the CPU and the renderer, which made multiplayer mods impossible. Before these changes, the CPU took about 30 milliseconds to calculate a frame. With two players, it would most likely reach the mid-40s, which would cap performance at about 20 FPS, even after the rendering speed had already more than doubled. Running all the code simply took too long to render multiplayer at 30 FPS. Even the developer's multiplayer mod, with compiler optimizations turned on, often dropped to 20 or 15 FPS. Nintendo reportedly cited this as the reason Luigi was cut from the original game.
The Bottleneck No Longer Exists
With these cleanups, the render is easily eight times faster than in vanilla Mario 64, and the bottleneck no longer exists. Even if the graph render were calculated twice, which would not be necessary, the total would be 22.5 milliseconds. That is still easily enough time to compute the whole frame.
Pixel Fill Rate and Split-Screen Layouts
In Return to Yoshi's Island, the RCP is typically the bottleneck, because drawing pixels is slow. The RDP needs 23.7 milliseconds to draw a 240x320 N64 screen. However, placing two 16:9 split-screen windows side by side fills fewer pixels than one large 4:3 window. The RDP therefore draws fewer pixels, which speeds up the RCP significantly. Fewer pixels also leave more RAM bus bandwidth available, so the code has more access to memory and runs faster. As a result, a multiplayer mode will likely run well. Polygon counts are a concern, but they are typically not a major issue.
Preparing Multiplayer Support
The developer has already made a multiplayer mod for the original game. The new project, however, will require a more complex solution because of the new code that has been added. Parts of the code have been prepared to allow a multiplayer mod later, though the work is not finished. Return to Yoshi's Island will likely release with built-in split-screen multiplayer. The project is also a heavy departure from the original Mario 64 source. Nearly everything has been replaced or edited, including Mario's 3D model, and the enemies have their own custom models.
Benchmark Comparisons with Vanilla Mario 64
These figures compare against fast compilation with all the previous optimizations applied. Compared with vanilla Mario 64, each of them more than doubles. Sound is 24.4% faster, rendering is 70 to 90% faster, level loading is 77% faster, texture memory is 87.7% faster, and the total frame render time is 22.5% lower. Together, these changes gain about 12 FPS, and that gain is expected to grow as more improvements are found.
Demo Frame Rates Across Levels
The demo uses the vanilla render engine compiled with O2 optimization to show how much the optimizations matter. Comparing frame rates in the demo with the current levels shows the difference. The demo levels are older versions released one year ago, while the new levels use higher polygon counts and higher texture resolutions. FPS roughly doubled in all three demo levels compared with vanilla Mario 64 without compiler optimizations. Compared with that baseline, FPS would probably be tripled or more, even with the visual upgrades. The current levels also look significantly better, with added detail.
Closing Remarks and Upcoming Work
The developer thanks Patreon supporters, noting that without them the project could not reasonably occupy all of the developer's time. This is a major mod that is about halfway done, and the goal is to finish it as soon as possible. Upcoming videos will cover other optimizations and the changes needed in the original code. Wow, here we go.
Turn your watchlist into a reading routine
Bring your YouTube videos together, read summaries with key passages marked, and explore the full transcript. Save your own highlights with Pro.
Start with 5 free batteries. Each covers up to 30 minutes of a new summary.
New summaries depend on video availability and demand; processing may take time or require retry.