Using Large Model Coding Assistants to Fix Firmware Graphics Legacy Code

    This article presents a practical, two-round workflow for using large model coding assistants to repair legacy firmware graphics code. It explains a structured code review across the entry point, main loop, graphics output, input handling, game logic, and resource management; how to fix common failures such as a crash after entering the main loop and a running program with no display; and how to refine rendering, input, collision detection, exception safety, and observability. It also outlines an effective division of labor between developers and AI tools, together with the capability boundaries and risks of relying on such tools in low-level development.

    1. Background: Why Firmware Graphics Programs Are Hard to Maintain

    Firmware-level graphics programs run in environments without full operating system support. They usually cannot rely on mature graphics frameworks, file systems, window managers, or interactive debuggers. Developers often have to work directly with low-level details such as frame buffers, pixel formats, protocol call ordering, limited memory, hardware initialization state, and serial-port logs. As a result, even code that compiles cleanly can still fail at runtime with crashes, black screens, screen tearing, unresponsive input, or misaligned display.

    In the scenario discussed in this article, a brick-breaking game already has baseline code generated by an earlier tool. The code includes modules for the entry point, main loop, rendering, input, and game logic, but it cannot start reliably after being ported to a new firmware development environment. This is where a large model coding assistant can act as a kind of “senior code reviewer” and “debugging assistant”: not as a replacement for the engineer’s final acceptance, but as a way to help developers quickly narrow down the problem, identify high-risk code, and generate verifiable fixes.

    The key to this kind of task is not to have the AI tool “rewrite everything from scratch,” but to bring the legacy code into a verifiable engineering loop: first make the program start, then make graphics output stable, and finally refine the logic, rendering, input, and exception handling.

    2. Overall Approach: Two Rounds, Run First Then Optimize

    When facing legacy code that will not run, it is not advisable to start with a large-scale rewrite. Problems in low-level graphics programs are rarely a single syntax error. They usually come from a combination of factors such as initialization order, memory lifetime, how protocols are acquired, and display mode configuration. If you rewrite broadly from the start, you can easily mix the original problems with new ones and make them harder to isolate.

    A more reliable approach is to split the task into two phases.

    The goal of the first round is a “minimal runnable” version. The focus is to let the program enter the main loop, complete graphics output initialization, and emit enough information on the serial port or debug output. This phase does not pursue visual quality or performance optimization. It only requires that the program does not crash or hang and can enter a continuous rendering flow.

    The goal of the second round is “refinement and engineering.” Once the program runs, you progressively fix rendering anomalies, input logic, collision detection, frame-rate control, resource release, and error handling, so the code becomes more maintainable and testable.

    This phased method also reflects the core principle of human-AI collaboration: let the AI tool handle repetitive review, code location, and patch generation, while the developer remains responsible for confirming environment constraints, verifying real execution, and owning final quality.

    3. Round One: Code Review and Runnability Fixes

    3.1 Which Modules Should the Code Review Cover

    Before starting repairs, you can ask a large model coding assistant to perform a structured review of the legacy code. To get more valuable review results, the context should be as complete as possible, including the project directory structure, main modules, compile entry point, target runtime environment, serial logging method, and the graphics output initialization code.

    When reviewing, it is recommended to focus on the following modules first.

    First, the entry function. Confirm whether the program performs the necessary initialization at startup, and whether there are unchecked error codes, unreleased resources, or missing default states.

    Second, the main loop. Confirm whether the main loop has a stable exit mechanism, and whether there are null pointer accesses, out-of-bounds array access, infinite loops, or blocking waits.

    Third, the graphics output module. Confirm whether the graphics output protocol is correctly located, whether the mode is successfully set, and whether the frame buffer address is re-acquired after a mode switch.

    Fourth, the input handling module. Confirm whether input scanning corrupts memory, whether it blocks the main loop, and whether key states are reasonably debounced.

    Fifth, the game logic module. Confirm whether the states of objects such as the ball, bricks, and paddle are fully initialized, and whether collision detection has out-of-bounds access or abnormal coordinates.

    Sixth, resource management. Confirm whether protocol handles, buffers, memory pools, files, or configuration resources are released in the correct place, to avoid startup failures or abnormal behavior after repeated runs.

    The value of the AI tool at this stage is that it can quickly produce a risk list and help developers turn vague symptoms into verifiable debugging steps.

    3.2 Problem One: The Program Crashes After Entering the Main Loop

    After the first run, the serial output shows that the program has entered the startup flow, for example by printing “Starting Game Loop,” but the program then exits immediately. This usually indicates that the startup initialization phase has completed, but the first resource access in the main loop failed.

    After analysis, the problem was concentrated on the handling order of the graphics output mode. The original code called a mode information query once before setting the display mode and saved the returned information pointer. The code then called the set-mode interface to switch the display mode. Because the underlying graphics output resources may change after a mode switch, the previously saved information pointer may have become invalid. When the program continued to use that pointer during rendering, it accessed invalid memory, ultimately causing the crash.

    This kind of problem is very typical in low-level graphics development. The information returned by a mode query interface usually contains key parameters such as the frame buffer address, resolution, and pixel format, but these parameters are only valid under a specific call order and lifetime. Once the display mode changes, the old information structure or old pointer may no longer represent the current state.

    The fix is not simply adding a null pointer check, but adjusting the call order: first complete the mode set, then query the current mode information again, and finally obtain the frame buffer address from the latest mode information.

    The following pseudocode illustrates this:

    1. // Probe whether the mode exists
    2. ModeInfo = QueryMode(GraphicsOutput, Mode);
    3. // Actually switch the display mode
    4. Status = SetMode(GraphicsOutput, Mode);
    5. if (Status != Success) {
    6. LogError(Status);
    7. return Status;
    8. }
    9. // Re-query the current mode information after the mode switch
    10. ModeInfo = QueryMode(GraphicsOutput, Mode);
    11. if (ModeInfo == NULL) {
    12. LogError("Query mode failed after SetMode");
    13. return Error;
    14. }
    15. // Initialize the frame buffer using the latest mode information
    16. FrameBufferBase = ModeInfo->FrameBufferBase;
    17. FrameBufferSize = ModeInfo->FrameBufferLength;
    18. PixelFormat = ModeInfo->PixelFormat;

    After the fix, the program no longer crashes from accessing an invalid pointer. This case shows that the value of a large model coding assistant in low-level debugging is not only finding that “a certain line of code has a problem,” but more importantly, being able to explain the lifetime rules behind the problem and provide a verifiable path to a fix.

    3.3 Problem Two: The Program Runs but the Graphics Do Not Display

    After the crash problem is resolved, the program runs continuously, but the screen still shows no proper image. This kind of problem is usually more subtle than a crash, because the program does not exit abnormally, making it harder for the debugger to directly determine whether the problem occurs in protocol location, mode setting, frame buffer writing, or render coordinate calculation.

    In this case, the problem was located in the acquisition of the graphics output protocol. A firmware environment may contain multiple related protocols or protocol identifiers. If the program uses the wrong protocol identifier, or does not open the graphics output protocol from the correct device handle, it may obtain the wrong object, causing the subsequent mode query, frame buffer acquisition, and pixel writing to fail to produce the expected results.

    There are three directions to investigate when fixing this.

    First, check whether the protocol identifier is consistent. The protocol identifier used by the program must match the graphics output protocol in the target firmware environment. If the identifier is wrong, locating the protocol will fail or return an error object.

    Second, check whether the device handle is correct. In some environments, the graphics output protocol may be attached to a specific device handle. Searching on an arbitrary handle may not obtain the correct instance.

    Third, add error code logging. In low-level development, you cannot judge only by “whether there is an image.” You also need to see whether each step of the call succeeds. Protocol location failures, unsupported modes, abnormal frame buffer addresses, and pixel format mismatches should all be output through the serial log.

    The logic is illustrated below:

    1. Status = LocateProtocol(GraphicsOutputProtocolId, NULL, &GraphicsOutput);
    2. if (Status != Success) {
    3. LogError("Graphics output protocol not found, status=0x%x", Status);
    4. return Status;
    5. }
    6. Status = QuerySupportedMode(GraphicsOutput, DesiredMode);
    7. if (Status != Success) {
    8. LogError("Desired mode is not supported, status=0x%x", Status);
    9. return Status;
    10. }

    By unifying the protocol entry point, verifying error codes, and adding explicit log output, the program gradually moves from “runs but black screen” to “able to enter the graphics rendering flow.” The lesson from this stage is that in firmware graphics development, success must be explicitly defined. Merely “not crashing” is not enough. You must further confirm that the protocol is loaded, the mode is set, the frame buffer is valid, and pixel writing succeeds.

    4. Round Two: Refinement and Engineering

    Once the program can enter the main loop and complete basic graphics output, you can move to the second round of optimization. The focus at this stage is not to continue solving “completely won’t run” problems, but to improve stability, maintainability, and playability.

    4.1 Rendering Layer Optimization: Pixel Coordinates and Buffer Management

    Firmware graphics programs usually operate directly on the frame buffer. Developers need to know the current screen resolution, color depth, and pixel format, and calculate the memory position of each pixel accordingly. Common pseudocode looks like this:

    1. void DrawPixel(int x, int y, Color color) {
    2. if (x < 0 || x >= ScreenWidth || y < 0 || y >= ScreenHeight) {
    3. return;
    4. }
    5. int Offset = (y * ScreenWidth + x) * BytesPerPixel;
    6. FrameBuffer[Offset + 0] = color.Component0;
    7. FrameBuffer[Offset + 1] = color.Component1;
    8. FrameBuffer[Offset + 2] = color.Component2;
    9. }

    There are three key points here.

    First, coordinate clipping. If the incoming coordinates are out of bounds, you must return early, otherwise you may corrupt memory beyond the frame buffer.

    Second, pixel format. The order of color channels may differ between environments, so you cannot assume a fixed RGB order and must write according to the actual pixel format.

    Third, update range. If the entire screen is redrawn every frame, it adds unnecessary computation. You can adopt a partial refresh strategy that redraws only the small regions of bricks, the ball, the paddle, and other objects that have changed.

    If the environment allows, you can also introduce an off-screen buffer or double buffering, drawing to a back buffer first and then flushing it to the display buffer in one go, which reduces screen tearing and flicker.

    4.2 Input Logic: Avoid Blocking the Main Loop

    In a firmware environment, input handling often has to be done by polling. If input scanning is too frequent, or if the logic that waits for a key press is written inside the main loop, it can cause the frame rate to drop or even freeze the program.

    A more reasonable approach is to design input handling as a non-blocking state machine. Each main loop iteration reads only the current input state and updates the game objects based on that state, rather than blocking for a long time in some key-wait function.

    For example, you can maintain the following state:

    1. KeyState CurrentKey;
    2. KeyState LastKey;
    3. UpdateInput(&CurrentKey);
    4. if (CurrentKey == LeftPressed && LastKey != LeftPressed) {
    5. MoveLeftOnce();
    6. }
    7. if (CurrentKey == RightPressed && LastKey != RightPressed) {
    8. MoveRightOnce();
    9. }
    10. LastKey = CurrentKey;

    This edge-detection approach distinguishes the “moment a key is pressed” from “the key being held down,” avoiding problems such as a single press triggering multiple moves, or continued triggering after a key is released.

    4.3 Game Logic: Collision Detection and Time Step

    The core logic of a brick-breaking game includes the movement of the ball or blocks, paddle control, brick collision, boundary bouncing, and game state transitions. To keep behavior as consistent as possible across different machines, it is recommended not to bind movement speed directly to the frame count, but to use a fixed time step or a time-delta-based update.

    For example, you can define:

    1. Ball.x += Ball.VelocityX * DeltaTime;
    2. Ball.y += Ball.VelocityY * DeltaTime;

    When performing collision detection, pay attention to the following details.

    First, boundary handling. When an object reaches the screen edge, you should reverse its direction according to its velocity direction and correct its coordinates, so the object does not get stuck outside the boundary.

    Second, brick collision. If the speed is high, a single object may pass through a brick within one frame. In this case, you can use finer-grained movement simulation, or split the movement distance into multiple small steps to avoid tunneling.

    Third, paddle collision. The paddle is usually a horizontal rectangle. During detection, you need to determine whether the object lands above the paddle, and adjust the bounce angle or horizontal velocity based on the impact position, which improves playability.

    Fourth, game state. You should clearly distinguish states such as start, in progress, paused, and game over, to avoid interference between logic in different states.

    4.4 Exception Safety and Resource Management

    Low-level graphics programs have high requirements for exception safety. A single null pointer access, a single out-of-bounds write, or a single incorrect mode switch can directly cause the system to hang or reboot. Therefore, the refinement phase needs to add defensive checks.

    It is recommended to add the following protections on critical paths.

    First, all externally supplied coordinates, sizes, and indices must have their ranges validated.

    Second, all pointers must be confirmed non-null before use, especially protocol interfaces, mode information, and frame buffer pointers.

    Third, all return status codes must be checked. In low-level development, you cannot assume that “a call always succeeds.”

    Fourth, operations such as mode switching, resource release, and protocol closing must come in pairs, to avoid repeated initialization or resource leaks.

    Fifth, for critical errors such as startup failure, unsupported mode, or unavailable protocol, you should output an explicit error code rather than exiting silently.

    The code below illustrates this:

    1. Status = InitializeGraphics();
    2. if (Status != Success) {
    3. LogError("Graphics initialization failed, status=0x%x", Status);
    4. HaltWithError(Status);
    5. }

    This kind of defensive design significantly improves the diagnosability of the program in abnormal environments, and also makes it easier for AI tools to continue analyzing logs and narrowing down the problem later.

    4.5 Observability: Logging, Frame Counting, and Debug Output

    Firmware graphics programs lack the debugging tools of traditional desktop environments, so observability is especially important. Serial logs, debug ports, status LEDs, memory markers, and frame counters can all help developers determine which step the program is currently executing.

    It is recommended to divide logs into multiple levels.

    Startup-phase logs confirm whether protocol location, mode setting, and frame buffer initialization succeed.

    Runtime-phase logs confirm whether the main loop continues to execute, whether input is being read, and whether objects are being updated.

    Exception-phase logs output error codes, the names of failed functions, key parameters, and the current state.

    Performance-phase logs can be used to track frame count, average frame time, render time, or logic time. There is no need to preset specific performance targets here; instead, establish observable metrics based on the actual environment.

    For example:

    1. FrameCount++;
    2. if (FrameCount % 60 == 0) {
    3. LogInfo("frame=%d, ballX=%d, ballY=%d", FrameCount, BallX, BallY);
    4. }

    This kind of log is simple, but it provides a real basis for subsequent optimization.

    5. Engineering Lessons in Human-AI Collaboration

    In this kind of task, a large model coding assistant cannot complete all the work on its own. The firmware environment, hardware differences, protocol specifications, and final result verification still require the developer to confirm personally. A more reasonable division of labor between humans and AI is as follows.

    First, the developer defines the constraints. These include the target runtime environment, allowed protocol versions, screen resolution, input method, resource limits, logging method, and acceptance criteria.

    Second, the AI tool handles code review and solution generation. It can check the startup flow, locate suspicious pointers, point out mode-switching risks, generate fixes, and explain error codes.

    Third, the developer performs run verification. After every change, you should recompile, run, and observe the serial logs. You cannot skip verification just because the code “looks reasonable.”

    Fourth, make progress in small steps. Solve only one core problem at a time, for example first the crash, then the black screen, then rendering anomalies, and finally gameplay. This avoids the situation where multiple problems overlap and cannot be isolated.

    Fifth, keep version snapshots. Every time you complete a runnable milestone, save the current code state. If later changes introduce new problems, you can quickly roll back to a known-working version.

    Sixth, feedback should be specific. When describing a problem to the AI tool, provide the symptoms, logs, error codes, relevant files, and expected behavior, rather than just asking “why won’t it run.” Specific context significantly improves localization efficiency.

    6. Capability Boundaries and Risk Control

    Large model coding assistants have clear value in low-level development, but they also have capability boundaries.

    First, they cannot replace real hardware testing. Firmware programs are strongly tied to specific hardware environments, and the same code may behave differently on different devices. Final verification must be done in the target environment.

    Second, they cannot replace specification confirmation. When it comes to protocol calls, memory layout, secure boot, firmware interfaces, and similar content, the corresponding technical specification should be the authority, and you cannot rely solely on code-based inference.

    Third, they may generate code that looks reasonable but does not conform to low-level lifetime requirements. For example, problems such as saving a pointer before a mode switch, incomplete error handling, and unreasonable initialization order all need to be ruled out through compilation, execution, and log verification.

    Finally, the output of an AI tool can only serve as engineering assistance, not a quality guarantee. All critical changes should go through code review, regression testing, and abnormal-scenario verification.

    7. Conclusion

    Governing legacy code in firmware graphics programs is essentially an engineering process that goes from not runnable to runnable, and then to maintainable and extensible. In this process, a large model coding assistant can help developers quickly complete code review, locate the root cause of crashes, fix the protocol acquisition flow, optimize rendering logic, and add exception handling, thereby significantly reducing the cost of repeated investigation.

    But what truly determines the success of a project is still a clear engineering method: define the problem, preserve context, verify in phases, fix in small steps, continuously observe the logs, and confirm manually at key points. AI tools can amplify a developer’s ability, but they cannot replace a developer’s understanding of the underlying system, judgment of the runtime environment, or responsibility for final quality. Only by placing AI tools within a rigorous engineering process can they deliver stable and sustained value in complex low-level development.