Writing Graphics Games in Firmware with LLMs: A Cross-Model Code Takeover in Practice
This article walks through a practical case of building a graphical mini-game in a firmware environment with the help of large language models. It explains why generated code that looks complete often fails to run, then breaks the work into two rounds: first making the project build and boot, then polishing performance, input, timing, and error handling. You will find the specific failure modes encountered (a stale pointer after a display mode switch and a wrong protocol identifier), the fixes, the smoke-test approach based on serial logs, and a set of reusable prompting and collaboration rules for handing an existing codebase from one model to another.}
1. Background: Why “It Generates” Is Not “It Runs”
Developing a graphical mini-game in a firmware environment is nothing like writing an ordinary application on an operating system. There is no ready-made window system and no mature debugging experience. Rendering depends on a frame buffer exposed by a graphics output protocol, input depends on polling a keyboard device protocol, and failures usually show up as an immediate reboot or a black screen, which means observability is extremely low.
In a recent project, one AI coding tool first generated a modular block-style game project. The structure looked well organized, but the environment setup failed and no runnable artifact came out of it. A second AI coding tool then took over that half-finished code, with the goal of getting it running first and refining it afterward.
The experience points to a general rule: the value of LLM-generated code lies not in getting it right in one shot, but in whether it can be placed inside a controllable iteration loop. The process is abstracted below into a reusable method.
2. Method Overview: Two Rounds Plus One Takeover
- Round 1 (make it runnable): code review → environment alignment → defect fixes → smoke verification.
- Round 2 (refinement): performance, input experience, rendering timing, and fallback handling for exceptions.
- Takeover strategy: do not overturn the existing structure. Keep the modular assets and let one model clean up after another, which reduces the communication cost of starting from scratch.
3. Round 1: From “Spec-Looking Code Nobody Can Read” to “It Runs”
1. Do a Static Review First, Instead of Running It
The first instruction when taking over a project should be very restrained: review this code and list the issues that could prevent it from running. Have the model go through it module by module — entry point, main loop, game logic, rendering, input. The output is a list of issues sorted by severity, not a pile of edits. The point of this step is to establish a “change budget”: know how many places need changes and what the risk surface of each one is before touching anything.
2. Environment Alignment Is the First Killer
Most firmware build failures are not algorithm errors but mismatches between the toolchain and the configuration: compiler options, target architecture, dependency header versions, and image packaging. It is worth freezing the environment into a script or a container so that “the same source builds identically on any machine,” and keeping the full build log so the model can locate problems from real errors instead of guessing and patching code.
3. Typical Defect One: A Wild Pointer After a Display Mode Switch
The symptom was that the program started, printed the log showing the main loop had begun, and then exited immediately. After adding serial debug output, the exception could be confirmed to happen after the graphics output protocol call. The root cause was that the pointer obtained from querying mode information pointed to memory that had become invalid after the display mode was switched; continuing to use it triggered the exception.
The fix is to adjust the call order: set the mode first, then re-fetch the frame buffer information, and only ever use the currently valid descriptor.
// Wrong order: query first, then set the mode, so info becomes invalidinfo = gop->QueryMode(gop, mode, &size, &info);gop->SetMode(gop, mode);frame = (UINT8*)info->FrameBufferBase; // wild pointer// Correct order: re-query after setting the modegop->SetMode(gop, mode);info = gop->QueryMode(gop, mode, &size, &info);frame = (UINT8*)info->FrameBufferBase;
4. Typical Defect Two: A Wrong Protocol Identifier Keeps the GUI From Appearing
The program no longer crashed, but nothing appeared on screen. The usual cause is choosing the wrong protocol identifier constant: the firmware specification defines several identifiers related to graphics output, and mixing them up means the correct protocol instance is never obtained. The way to handle it is to reference the constants in the specification header consistently, forbid custom magic numbers, and add a capability probe at the entry point that prints an explicit error code on failure instead of silently continuing.
5. Smoke Verification Must Be Observable
The key to firmware debugging is adding logs: place checkpoints at four points — initialization, mode setting, per-frame drawing, and input scanning — and watch timestamps and return statuses over the serial port. Having the model iterate based on logs works far better than telling it to “think again.”
4. Round 2: Refinement and Experience Optimization
- Rendering performance: avoid full-screen redraws and use dirty-rectangle updates; cap the frame rate below half the refresh rate to reduce tearing and stutter.
- Input experience: change keyboard polling to edge triggering so one press produces exactly one action, and add throttling for long presses.
- Timing stability: decouple game logic from drawing, advance logic with a fixed step, and let rendering follow the display refresh.
- Exception fallback: check the return status of every external protocol call, and on failure degrade to text mode with a message instead of rebooting directly.
- Resource constraints: measure stack and heap usage explicitly, and avoid putting large temporary buffers on the stack.
5. Reusable Prompts and Collaboration Rules
- Solve only one class of problem at a time, and require output in four parts: issue — root cause — change points — verification method.
- Force the model to restate the constraints first (target architecture, available protocols, logging means) before giving a solution.
- Have the model produce a minimal verifiable patch, and forbid large-scale directory restructuring.
- Run the same smoke test suite after every change and compare results using the logs.
- Turn the lessons learned into a constraint list inside the project, to serve as context for the next round.
6. Conclusion
Taking over a “spec-compliant but non-runnable” project across models is essentially about locking uncertainty inside an iteration loop: static review sets the budget, environment alignment removes interference, logs drive localization, minimal patches move things forward, and refinement at the end buys the user experience. For teams that want to use LLMs to improve firmware or low-level development efficiency, build and logging capabilities can be managed centrally on a container platform and a logging service, with monitoring and alerting forming a sustainable development pipeline; the general capabilities that Baidu AI Cloud provides for this kind of engineering foundation can also help teams turn “model generation” reliably into “deliverable artifacts.”
