Repairing Embedded Firmware with AI Coding Models: Lessons from a Real-World Experiment
This article examines a practical experiment in which one AI coding model repaired an unbuildable Tetris game written for a bare-metal firmware environment by another model. It covers the debugging process—from global code review and wild-pointer diagnosis to protocol identifier correction—and discusses a two-stage workflow for moving from “runs” to “usable,” along with five lessons for effective human–AI software collaboration.
Repairing Embedded Firmware with AI Coding Models: Lessons from a Real-World Experiment
Introduction: An Unusual Comparison
As new generations of AI coding models continue to emerge, more developers are assigning real engineering tasks to them. Recently, I conducted an interesting comparison: I asked two flagship coding models from different vendors to implement graphical games in a bare-metal firmware environment.
The first stage was particularly dramatic. Working with an AI coding assistant, one model completed a fully functional Breakout game in two and a half hours. The project compiled successfully on the first attempt and ran correctly the first time it was launched. The other model spent nearly six hours trying to implement Tetris, failed to complete the build environment, and ultimately produced code that could not run.
After the article was published, some readers pointed out that the two models had been given different tasks, making the comparison unfair. That criticism was reasonable. This was not intended to be a rigorous benchmark, but rather an intuitive demonstration of the capabilities and limitations of AI coding tools in a real engineering scenario.
However, the first model had left behind code with a relatively sound structure: seven source files, modular design, and clear layering. Throwing it away seemed wasteful. That led to a more ambitious idea: ask the first model to take over the broken Tetris project and repair it until it ran successfully. In other words, use one AI tool to clean up the mess created by another.
Why Firmware Is an Excellent Test of AI Coding Ability
Before discussing the experiment, it is worth explaining why firmware development is such a demanding test environment. Compared with conventional application development, low-level firmware programming places much greater requirements on an AI coding model.
First, the environment is highly constrained. In a pre-boot environment before an operating system has loaded, there are no dynamic libraries, standard input or output, mature debugging tools, or familiar runtime services. Even printing a string requires manually sending data through a serial protocol. The model cannot rely on assumptions derived from common application frameworks.
Second, protocol interaction is complex. Firmware development requires direct interaction with graphics output protocols, block device protocols, input protocols, and other standardized interfaces. These interfaces have strict versioning conventions and globally unique identifier systems. A small mistake can cause protocol discovery to fail or result in an invalid pointer.
Third, debugging tools are primitive. There are no breakpoints or memory-checking tools. Problems must be isolated through serial logging and binary-search-style troubleshooting. This requires rigorous logical reasoning rather than repeated trial-and-error modifications.
Fourth, training data is relatively scarce. Compared with the enormous amount of publicly available web content and backend code, firmware code is much less common in public datasets. Performance in this type of task is therefore more likely to reflect genuine code comprehension and reasoning than simple pattern recall.
Experiment Design: A Two-Phase Repair Strategy
The model was given a Tetris project generated by another model that could neither compile nor run. The goal was to repair it until it became usable in a bare-metal firmware environment. The experiment was divided into two rounds:
- Round 1: Make the program run by reviewing the code, fixing compilation issues, and locating runtime failures.
- Round 2: Improve the working implementation, focusing on rendering performance, input responsiveness, and maintainability.
This staged task decomposition is itself an important method for working with AI coding tools. Giving the model a large, all-encompassing objective at once often divides its attention and prevents it from handling any individual aspect thoroughly.
Round 1: From Broken Code to a Running Program
Step 1: Global Code Review
After taking over the legacy code, the first instruction was deliberately simple: review the code and identify potential problems.
The model performed well. Instead of immediately modifying files, it first mapped the project structure. It examined the entry point, main loop, game logic, rendering module, and input handling, analyzing the responsibilities and dependencies of each component. Within a few minutes, it produced a structured list of issues covering potential memory problems, incorrect protocol call sequences, and missing resource cleanup. It also provided an assessment of what would be required to make the project runnable.
This stage demonstrated an important capability of current-generation coding models: building a global understanding before making local changes. Earlier code-completion tools often focused only on the immediate context. Leading models can now approach a project more like an experienced engineer, first constructing a mental model of the system as a whole.
Problem 1: A Crash Caused by a Wild Pointer
After the initial fixes, the project compiled successfully. When launched, it printed a message indicating that the main loop had started, then exited immediately—a typical runtime failure.
The model was asked to add serial debug output along the critical execution path to narrow down where the failure occurred. It quickly identified the problem as occurring after a graphics output protocol mode-setting call.
Further analysis showed that the original code had saved a pointer to the information structure returned while querying the display mode. After the display mode was changed, that memory was no longer valid. The code was therefore using a classic wild pointer.
The fix was standard: after completing the mode-setting operation, query the display information again and retrieve the framebuffer address instead of reusing the old pointer. After recompilation and another test run, the crash was resolved.
This case is particularly notable because it demonstrates the model’s ability to perform causal reasoning without an interactive debugger. By inserting logs at key points, analyzing the order of operations, and reasoning about memory lifetime, the model identified the root cause rather than making random changes.
Problem 2: An Incorrect Globally Unique Identifier
After the crash was fixed, the program no longer terminated unexpectedly, but the graphical interface still produced no output. Once this behavior was reported to the model, it determined that the graphics output protocol had been configured with the wrong identifier.
This is a classic firmware-development pitfall. Common firmware development frameworks may define multiple identifiers for the same general type of protocol, with each identifier corresponding to a different protocol version or device-path scenario. If the wrong identifier is used when locating a protocol handle, the system call may appear to succeed while returning an object that is not the one expected. All subsequent graphics operations then fail silently.
The model corrected the identifier reference. The graphical interface finally rendered correctly, and Tetris was running in the firmware environment.
Round 2: From Running to Usable
The first round produced a program that could run, but it was still far from providing a good user experience. The second round focused on three areas.
Rendering: The initial implementation redrew the entire screen and refreshed the full framebuffer on every frame, which caused visible flickering. The model changed the implementation to incremental dirty-region rendering, updating only the areas containing blocks that had changed. This significantly improved frame stability and rendering performance.
Input: Keyboard input in the firmware environment depends on event polling. The original implementation could lose key events and trigger repeated actions. By introducing an input-state cache and edge-detection logic, the model brought the controls much closer to a normal gameplay experience.
Code structure: The model separated responsibilities in the tightly coupled original code. The game state machine was decoupled from the rendering logic, substantially reducing the cost of adding future features such as scoring and level-based speed increases.
Five Lessons from the Experiment
This experiment—having one AI tool repair code produced by another—led to several broader conclusions.
1. Code review may distinguish models more effectively than code generation. Generating new code can often be handled through pattern matching. Understanding unfamiliar legacy code, identifying its defects, and locating the actual root cause require deeper reasoning. When evaluating coding models, developers should consider repair tasks in addition to generation tasks.
2. A structured problem statement is a prerequisite for high-quality repair. In this experiment, an instruction such as “review the code and provide a list of problems” produced better results than a vague request such as “fix it for me.” Asking the model to diagnose first and prescribe changes second can significantly reduce incorrect modifications.
3. Debugging information is the bridge between humans and models. In an environment without an interactive debugger, asking the model to add logging points and design ways to observe runtime behavior is often more effective than simply asking it to fix the program. The model cannot see the runtime environment directly; logs serve as its eyes.
4. Staged goals are better than trying to do everything at once. The two-round strategy—first make the program run, then make it usable—provided clear and testable acceptance criteria for each stage. It prevented the model from losing focus among multiple competing objectives.
5. Treat model output with engineering skepticism. Even a model that performs well may claim that a change is ready when it still contains defects. Its output must be verified through actual testing. The most sustainable approach is to treat the model as a highly capable colleague who can still make mistakes, then combine its work with code review and automated validation.
Conclusion
AI coding models are moving beyond code completion toward engineering delivery. The real dividing line is not how many lines of code a model can generate, but whether it can systematically diagnose and repair code in situations where no ready-made answer exists—for example, a firmware project broken by another developer or another model.
In this experiment, the model completed troubleshooting work in a few hours that might have taken a human one or two days. Such productivity gains are especially valuable in low-level development.
For developers, learning how to describe problems to a model, design verification methods, and divide work into stages is becoming more important than memorizing individual syntax details. These collaboration skills may be among the most valuable capabilities to develop in the era of AI-assisted programming.
