Running Graphics Games in Firmware: A Practical Guide to LLM-Assisted Debugging

    This article walks through a hands-on experiment in building a graphical game directly in a low-level firmware environment with help from large language models. It shows how to turn a project that looks complete but fails to run into a working, optimized program in two rounds: first a layered static review, then minimal fixes for a dangling pointer after mode switching and a wrong protocol GUID causing a black screen, followed by rendering, input, resource, and maintainability improvements. Readers will also get a reusable four-step collaboration loop and practical quality gates for constrained environments.

    ## 1. Background and Problem Definition

    Building a small game with a graphical interface in a low-level firmware environment is on a completely different scale from ordinary application development. Instead of a mature graphics framework, you work with a minimal runtime: no OS scheduler, no windowing system, display output obtained through the graphics output protocol provided by firmware, and input handled by key scanning. A single out-of-bounds memory access can trigger an immediate reboot.

    This kind of task exposes the real boundaries of LLM-assisted development. A common experiment design is to hand the model a project that “looks well structured but does not run,” and require it to go from non-runnable to runnable, and then to optimized, within two rounds. To keep the comparison fair, two models are given problems of similar difficulty, and the process is broken into observable stages.

    Note that this article does not evaluate any specific product. Instead, it abstracts the process into a transferable engineering method: how to involve models in legacy code remediation, and how to build a feedback loop in a strongly constrained environment.

    2. Round One: From “Looks Right” to “Actually Runs”

    1. Do a static review first, not direct code changes

    The first instruction when taking over an unfamiliar project should be review, not modification:

    1. Read through the project structure, organized into layers:
    2. startup chain / main loop / logic layer / rendering layer / input layer.
    3. List the issues that could prevent it from running, ordered by priority,
    4. and explain the reasoning behind each.

    After checking layer by layer, the model typically produces a list like this: the entry point is missing required initialization; mode setup and frame buffer acquisition happen in the wrong order; the protocol identifier is used incorrectly; the main loop has no exit condition; input state has no debouncing. The value of this list is not that every item is correct, but that it narrows the investigation from “the whole project” to “a few suspicious spots.”

    2. Issue one: a dangling pointer that exits immediately

    After running, the screen prints a loop-start message, and then the process ends directly. After asking the model to add serial debugging output, the fault is localized to an exception occurring after the graphics output protocol call.

    The root cause is a classic lifetime error: the information pointer obtained when querying the mode points to memory that becomes invalid after the mode is set, so continuing to use it is a dangling pointer access.

    1. // Bad: info becomes invalid after SetMode
    2. info = GraphicsOutput->QueryMode(GraphicsOutput, mode, &size);
    3. GraphicsOutput->SetMode(GraphicsOutput, mode);
    4. frame = info->FrameBufferBase; // dangerous
    5. // Good: re-query after the mode switch
    6. GraphicsOutput->SetMode(GraphicsOutput, mode);
    7. info = GraphicsOutput->QueryMode(GraphicsOutput, mode, &size);
    8. frame = info->FrameBufferBase;

    After the fix, the crash no longer occurs. The methodological point here: add observability first, then change logic; change only one variable at a time.

    3. Issue two: wrong protocol identifier causes no display

    The program now runs continuously, but the screen is completely black. The model determines that the graphics output protocol identifier is misconfigured—multiple related identifiers exist in the firmware environment, and using the wrong one prevents the graphics output service from being located correctly.

    The fix is to unify the source of the identifier and avoid hardcoding it in multiple places:

    1. // Keep a single definition; other modules reference the same constant
    2. static const EFI_GUID gEfiGraphicsOutputProtocolGuid = { /* ... */ };
    3. status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid, NULL, (void**)&gfx);

    Also add return value checks, turning “silent failure” into “diagnosable failure”:

    1. if (EFI_ERROR(status)) {
    2. DEBUG((DEBUG_ERROR, "locate gfx failed: %r\n", status));
    3. return status;
    4. }

    3. Round Two: Refinement and Optimization

    Once it runs, the focus shifts to stability and experience, advanced along four main lines:

    • Rendering performance: reduce full-screen refreshes and switch to dirty rectangle updates; precompute constant color blocks to avoid repeated computation inside loops.
    • Input handling: add debouncing and repeat delay to avoid continuous triggering from long presses.
    • Resources and state: explicitly pair initialization and deinitialization to avoid leaks on exit paths.
    • Maintainability: decouple logic, rendering, and input, and use a fixed-width frame buffer abstraction to hide resolution differences.

    A sketch of the main loop skeleton:

    1. while (!should_exit) {
    2. scan_input(&input);
    3. update(&game, &input, dt);
    4. render_dirty(&game);
    5. gBS->Stall(1000);
    6. }

    4. A Reusable Collaboration Pattern and Quality Gates

    Abstracting the experiment into a process yields a four-step loop: use static review to narrow the problem space, apply minimal fixes to restore runnability, use feedback to locate deeper defects, and use metrics to drive refinement. Recommended quality gates include: zero compiler warnings, full return value coverage on the startup path, explicit comments on the lifetime of critical pointers, tiered serial logs, and committing only one fix at a time.

    If this process is placed in a cloud engineering system, a container platform can provide a unified build environment, object storage can hold build artifacts, and a logging service with monitoring and alerting can take over serial and runtime logs—making “reproducibility” a default capability.

    5. Conclusion

    In a strongly constrained firmware environment, the value of large models is not generating perfect code in one shot, but compressing review, localization, fixing, and verification into short iteration cycles. What really determines success is whether the developer has established a clear description of constraints, an observable feedback channel, and strict minimal-change discipline. Baidu AI Cloud likewise emphasizes this “reproducible, observable, rollback-capable” collaboration approach in general engineering practice, which is also a key prerequisite for low-level development to scale.