Running Graphics Games in Firmware: A Practical AI Relay Debugging Walkthrough
This article walks through a real-world relay debugging session on a firmware-layer graphics program. It explains why firmware graphics are prone to failure, then documents two iteration rounds: first getting legacy modular code to run by fixing a wild pointer and a mismatched protocol identifier, then improving maintainability through state consolidation, input robustness, logic-rendering decoupling, and observability. It also provides a reusable four-part prompt structure and a verification checklist for AI-assisted handoffs.
1. Background: Why “AI Relay” Development Is Becoming the Norm
In real development work, AI-generated code rarely comes out complete on the first try. A more common scenario is this: a coding tool produces a piece of code that looks structurally complete but cannot run, leaving behind a half-finished artifact. Someone then hands it off to a different model or a new session to continue the work. This relay-style development places very different demands on a model than generating from scratch. It tests code comprehension, fault localization, and the ability to self-correct under unfamiliar engineering constraints.
This article uses a firmware-layer graphics program as a case study and walks through a complete relay repair. The starting point is legacy code that is modular but will not boot. Through two rounds of iteration (get it running first, then refine), it becomes a program that both runs and is maintainable. No specific product comparisons are involved; the focus is on method.
2. Runtime Constraints: Why Firmware Development Fails So Easily
Firmware-layer graphics programs differ sharply from ordinary desktop programs. The main constraints fall into four categories:
- No mature runtime: there is no operating system, no standard library, and no process or memory protection. Everything must be managed manually.
- A long boot chain: from firmware loading, protocol location, and mode setup to drawing output, a failure at any stage shows up as a black screen or an immediate fallback.
- Scarce debugging tools: in practice you rely on serial logs and single-step tracing, with no graphical debugger.
- Sensitivity to resources and state: the lifetimes of frame buffers, mode information, and protocol handles must line up precisely.
This explains why “clean code structure” does not equal “it runs.” The legacy code is split across seven files with clear module boundaries, which only means readability is acceptable. It says nothing about whether the runtime assumptions hold.
3. Round One: From Not Running to Running
3.1 Start With a Static Review, Not Direct Edits
The first step is not to run the code but to have the model review it module by module: entry point, main loop, game logic, rendering, and input handling. The review focuses on three classes of problems:
- Timing issues: does the sequence of protocol calls satisfy “locate first, then query, then set, and finally get the frame buffer”?
- Lifetime issues: after a pointer is obtained, is it invalidated by a later call?
- Configuration issues: do constants such as identifiers and GUIDs match the platform definitions?
The output should be a checkable list of issues, not a vague statement that “the code has bugs.”
3.2 Fault One: Exits Immediately on Startup (Wild Pointer)
The symptom is an immediate fallback right after the loop-start log is printed. The root cause is that the original mode information pointer becomes invalid after the mode is set. The information block returned by the mode query is overwritten or freed after SetMode, and continuing to dereference it triggers a fault.
The repair principle: place the “get” after the “set” rather than reusing the old pointer.
// Bad: info is already invalid after SetModeQueryMode(gop, 0, &size, &info);SetMode(gop, 1);draw(info->FrameBufferBase, info->HorizontalResolution); // wild pointer// Good: re-fetch after setting the modeSetMode(gop, 1);UINTN size = 0;VOID *info = NULL;QueryMode(gop, 1, &size, &info); // obtain valid information againdraw(info->FrameBufferBase, info->HorizontalResolution);
Key takeaway: the firmware layer has no memory protection, so a wild pointer does not throw an exception. It silently corrupts data or causes an immediate fallback, and you must localize it by bisecting with logs.
3.3 Fault Two: It Runs but No Graphics Appear (Identifier Mismatch)
The program no longer exits, but nothing appears on screen. Localization revealed that the wrong identifier constant was used when locating the graphics output protocol. A platform typically defines several similar identifiers with different semantics, and mixing them up causes protocol location to fail or to return the wrong instance.
There are three repair strategies:
- Use an identifier that is semantically clear and corresponds directly to graphics output;
- Handle location failure explicitly and print the status code instead of continuing execution;
- Wrap protocol location in a single function to avoid repeating the constant in multiple places.
EFI_STATUS locate_gop(EFI_HANDLE image, EFI_GRAPHICS_OUTPUT_PROTOCOL **gop) {*gop = NULL;EFI_STATUS st = gBS->LocateProtocol(&gGraphicsOutputProtocolGuid, NULL, (VOID **)gop);if (EFI_ERROR(st) || *gop == NULL) {log(L"locate gop failed: %r\n", st); // failures must be visiblereturn st;}return EFI_SUCCESS;}
4. Round Two: Refinement and Engineering Quality
Getting it running is only the starting point. The second round focuses on maintainability and can proceed in this order:
- Consolidate resources and state: manage all protocol handles centrally and forbid scattering them across globals.
- Harden input handling: keyboard and key scanning must handle debouncing and repeated triggers to avoid “one press moves several cells.”
- Decouple rendering from logic: the logic layer maintains only the grid state, while the rendering layer only maps that state to the frame buffer, making it easier to swap resolutions later.
- Frame rate and tearing: use double buffering or dirty-rectangle refresh to reduce flicker.
- Observability: grade serial logs (error/warning/status) and add checkpoints on critical paths.
A schematic main loop structure:
while (running) {input_poll(&ev); // 1. sample inputlogic_update(&ev); // 2. update state (pure logic, no drawing)render_present(); // 3. submit a framegBS->Stall(16667); // 4. throttle to roughly 60Hz}
5. Reusable Prompts and a Verification Checklist
For relay repair, the prompt is best kept in a fixed four-part structure:
- Role and constraints: state the runtime environment, available interfaces, and prohibited actions;
- Input materials: provide a complete file list and key snippets;
- Output requirements: give the issue list first (with localization evidence), then a minimal-change patch;
- Verification method: require a list of log points to observe and the expected output.
Verification checklist (check off each item):
- Is protocol location failure visible?
- Is frame buffer information re-fetched after the mode is set?
- Are there any pointers reused across calls?
- Do identifier constants have a single source?
- Do exception paths have logs and status codes?
- Are logic and rendering decoupled?
6. Conclusion
For AI relay repair of firmware-layer graphics programs, the difficulty lies not in writing code but in building a habit of validating “runtime assumptions”: timing, lifetimes, and configuration consistency. Moving the review earlier, making failures explicit, and consolidating state, combined with two rounds of iteration and checklist-based verification, can significantly improve the odds of getting it running on the first pass. This method also applies to strongly constrained scenarios such as drivers, kernel modules, and embedded graphical interfaces, and it can be combined with cloud-based log services, monitoring and alerting, and containerized build environments to form a closed loop from development to verification.
