Debugging Legacy Code with AI: Running a Graphics Game in Firmware

    This article presents a reusable workflow for using AI to debug and maintain legacy code, using the task of running a small graphics game in a firmware environment as the technical backdrop. It explains why taking over existing code is harder than generating from scratch, and recommends splitting the work into two rounds: first static review and blocking fixes to get a reproducible run, then performance and robustness refinements. Typical failures are covered, including stale frame buffer information after a display mode switch and incorrect graphics protocol identifier configuration, along with their root causes and minimal fixes. Readers will learn how to turn vague "it does not run" symptoms into verifiable hypotheses and close the validation loop.

    1. Background: Why Taking Over Legacy Code Is Harder Than Generating from Scratch

    In real development, we often encounter code generated by different tools or different people. The structure looks clean and the modules are well separated, yet the program simply will not run. When generating a new program from scratch, a model can organize the implementation entirely around its own approach. When taking over existing code, the model must reason on top of someone else’s design (or another model’s design) and reconstruct implicit assumptions. This is much closer to real engineering.

    This article uses “running a small graphics game in a firmware environment” as the technical background to discuss a reusable collaboration workflow. Firmware-level programs share a common trait: there is no mature operating system to fall back on. Memory, display, and input must all be requested directly from firmware services, and any pointer lifetime or identifier configuration error shows up immediately as a crash or a black screen.

    2. Overall Approach: Two Rounds, Run First and Optimize Later

    It is best to split the task strictly into two rounds to avoid tangling problems by asking for too much at once.

    • Round 1 (runnable): static review → fix blocking issues → obtain a reproducible run.
    • Round 2 (refinement): performance, input feel, screen refresh, resource release, and exception path handling.

    Each round needs a clear acceptance criterion. For example, Round 1 only requires “entering the main loop and producing output on screen,” while frame rate and input latency are discussed only in Round 2.

    3. Round 1: Static Review First

    Do not rush to run the code. First have the model read through it and produce an issue list. Splitting the review by module is usually more reliable:

    1. Entry and dependencies: whether the protocol request order is correct and whether handle selection is reasonable.
    2. Main loop: whether there is blocking, missing timeouts, or incorrect exit conditions.
    3. Game logic: whether the state machine is complete and whether collision and boundary checks have gaps.
    4. Rendering module: mode setup, frame buffer acquisition, and pixel write method.
    5. Input handling: key scanning, debouncing, and event mapping.

    A sample review prompt can be written as:

    1. Review the following code module by module. List only the issues that
    2. "will prevent the program from running or cause a crash," sort them by
    3. severity, and explain the reasoning behind each issue along with the
    4. minimal fix.

    The value of this step is turning a vague “it does not run” into several verifiable hypotheses.

    4. Typical Failure 1: Frame Information Becomes Invalid After a Display Mode Switch

    The symptom is that the program prints the loop-start log after launch and then exits immediately. After adding serial debug output, the exception can be traced to the call after the graphics output protocol is invoked.

    The root cause is usually this: frame buffer information obtained before setting the display mode becomes invalid after the mode switch, and continuing to write to that address is a wild pointer access.

    The fix principle is “set the mode first, then query the information, and use only the most recently obtained description throughout”:

    1. // Pseudocode: re-acquire the frame buffer description after a mode switch
    2. set_mode(gop, target_mode);
    3. frame_info = query_mode_info(gop); // key: re-acquire after switching
    4. write_pixel(frame_info->base, frame_info->stride, x, y, color);

    Two additional engineering practices are needed: first, check the return status on every call; second, limit the frame information to the smallest possible scope to avoid stale cached reuse.

    5. Typical Failure 2: Incorrect Graphics Protocol Identifier Configuration

    The program runs but nothing appears on screen, which is most often an identifier configuration problem. In the firmware specification, there is more than one graphics-related identifier, and picking the wrong one makes it impossible to bind to the correct output device.

    The recommended troubleshooting order is: confirm the protocol was located successfully → print the device handle and protocol attributes → verify the resolution and pixel format → only then examine the write logic. Example:

    1. status = locate_protocol(&gop_guid, &gop);
    2. if (error(status)) { debug_print("gop locate failed\n"); return; }
    3. debug_print("mode=%d stride=%d fmt=%d\n",
    4. gop->mode->info->mode, gop->mode->info->stride,
    5. gop->mode->info->pixel_format);

    6. Round 2: Refinement and Robustness Checklist

    Once output appears on screen, address experience and robustness:

    • Double buffering or dirty rectangle refresh to reduce flicker and bandwidth usage;
    • Input debouncing and fixed-step updates to avoid speed depending on frame rate;
    • Frame rate statistics and timing instrumentation to locate the source of stutter;
    • Resource release and exception exit paths to prevent handle leaks;
    • Assertions on critical paths and log level separation for easier on-site diagnosis.

    7. Collaboration Takeaways and Conclusion

    Treating AI as “an assistant that is good at reading” rather than “an author that gets it right in one shot” works best: constrain the output format during review, fix only one class of problem at a time, and require verifiable reasoning at every step. If stronger engineering support is needed, a cloud container platform can be combined for build reproduction, and log services and monitoring alerts can keep a trace of the process, making every change traceable. The final conclusion is that the core competency in taking over legacy code lies not in generation speed, but in assumption management and a closed validation loop.