Large Language Models for Firmware Development: From Legacy Code to a Runnable System

    This article presents a practical, two-stage workflow for using large language models to repair legacy firmware projects. Using a small game with graphics, input handling, and a main loop as the example, it explains how to move from failed initialization and unstable execution to a runnable, testable, and maintainable system through evidence-based debugging, minimal patches, improved observability, and regression testing. The discussion focuses on reusable engineering practices rather than evaluating any specific model or platform.

    In low-level systems development, large language models are often most impressive when asked to generate a runnable demo. Given a clear specification, a model can produce several files, compile the project, and deliver something that appears to work.

    The greater engineering value, however, often appears in a different situation: taking over an incomplete legacy project that does not run reliably, looks well structured on the surface, but contains hidden defects. With limited information and limited observability, the real challenge is to identify problems step by step and return the project to a runnable, testable, and maintainable state.

    This article does not evaluate a particular model, platform, or product. Instead, it discusses a reusable technical practice: how to involve a large language model in repairing legacy projects in modern firmware environments.

    The example is a small application with a graphical interface, input handling, and a main loop. The initial code was divided into modules for the entry point, game logic, rendering, input, and platform adaptation. Its structure appeared clean, but the program could not complete initialization or enter stable execution in the target environment.

    The repair process was divided into two rounds:

    • Round one: make the program start, enter the main loop, and display a frame.
    • Round two: improve logical correctness, rendering stability, input behavior, memory safety, and maintainability.

    Why Firmware Is a Serious Test of AI Engineering Capability

    Compared with ordinary application development, firmware projects have several distinct characteristics.

    First, the runtime environment is much thinner. There may be no complete operating system services available. Many capabilities depend on services, protocols, and memory layouts provided by the boot environment. Developers cannot assume the availability of a file system, dynamic linking, thread scheduling, or full standard library support. As a result, an incorrect initialization order, an invalid handle, or unsafe buffer access can cause the program to exit immediately or leave the screen blank.

    Second, observability is limited. When a graphical interface fails, the result may be nothing more than a black screen or a frozen frame. When input stops working, it can be difficult to determine whether the event queue was never polled, the key mapping is incorrect, or the main loop is not running at all. Unlike a web application with a browser console, firmware debugging often depends on serial logs, debug output, status codes, and stage markers to reconstruct the execution path.

    Third, interface semantics depend heavily on context. A query interface may return a pointer that is only a borrowed view and becomes invalid after the next configuration change. A service-location function may require an exact unique identifier; a small mismatch can result in a missing service or an incorrect object. Graphics buffers may also involve stride, pixel format, and cache-flushing details.

    Code that compiles is not necessarily semantically correct. Code that reaches the main loop is not necessarily safe in terms of resource lifetime.

    Fourth, build and runtime feedback is slow. A firmware project may require building, packaging, booting, and observing behavior on hardware or in a virtual environment. Each change costs considerably more to validate than a change to an ordinary script. If a model only knows how to rewrite large sections of code, but cannot make small, evidence-based fixes, it will have difficulty delivering reliable value.

    For this reason, a more realistic test is not asking a model to create a sample from scratch. It is asking the model to repair a project left by someone else: a project that expresses a design intent but currently fails to run. This task simultaneously tests code comprehension, fault localization, log analysis, patch discipline, regression awareness, and engineering rigor.

    The Experiment: Two Repair Rounds Instead of One-Click Generation

    To keep the process controlled, the task was divided into two rounds with clear constraints.

    Round One: From Unrunnable to Basically Functional

    The program had to:

    • Complete initialization;
    • Obtain the required display service;
    • Enter the main loop;
    • Render the first frame;
    • Respond to basic input.

    The preferred approach was minimal change rather than a complete redesign.

    Round Two: From Running to Usable and Maintainable

    Without breaking the existing module boundaries, the second round focused on:

    • Main-loop and state management;
    • Input debouncing;
    • Collision logic;
    • Scoring and restart behavior;
    • Rendering and refresh strategy;
    • Error handling;
    • Log levels;
    • Maintainability.

    The information provided to the model was not simply “please fix this.” It included:

    • The module tree and entry path, including platform initialization, game state, rendering, input, and resource definitions;
    • Reproduction steps, including how to build and launch the program, what should appear, and what actually appeared;
    • Runtime evidence, including startup logs, error codes, exit locations, and screen behavior;
    • Constraints, such as preferring minimal changes, preserving module interfaces, avoiding unavailable dependencies, and making every patch explainable;
    • An output format requiring symptoms and hypotheses first, followed by minimal validation steps, patches, and regression checks.

    A reusable collaboration prompt can look like this:

    1. You are a low-level systems troubleshooting assistant. The following material
    2. describes the project modules, relevant source code, and the most recent runtime
    3. log.
    4. Goal: make the program complete initialization and enter the main loop reliably.
    5. Constraints: prefer minimal changes; do not rewrite the entire module; explain the
    6. symptom, root-cause hypothesis, validation method, and regression risk for every
    7. change.
    8. Use the following output format:
    9. 1. Summary of observed behavior;
    10. 2. Suspicious areas ranked by probability;
    11. 3. Most likely root cause and supporting evidence;
    12. 4. Minimal validation steps;
    13. 5. Patch snippets;
    14. 6. Logs and regression items to observe after the change.

    This format forces the model to turn guesses into testable hypotheses instead of producing a large amount of code that merely looks plausible.

    Round One: From Legacy Code to a Runnable Program

    1. Review the Fault Surface Before Editing Code

    The first step was not to modify the source immediately, but to perform a structured review. The model examined the entry function, platform initialization, display-service acquisition, configuration queries, main loop, input polling, rendering output, and resource-release paths.

    Instead of asking the model only to “find bugs,” it was more effective to request a fault-surface inventory. Each item had to identify the related symptom, the evidence required, and the estimated validation cost.

    The resulting issues fell into several categories:

    1. The program entered the main-loop area but exited shortly afterward, suggesting a problem with the loop condition, blocking behavior, or an error-return path.
    2. There was no clear log showing whether the display service had been acquired successfully. Graphics failures had no fallback or diagnostic path.
    3. Information returned by a configuration query continued to be used after a mode change, creating a lifetime risk.
    4. The unique identifier used for service location might not match the required interface, preventing the correct graphics object from being obtained.
    5. The frame buffer, stride, and pixel format were not validated before the first frame was rendered, making a black screen difficult to classify.
    6. Error codes were ignored, delaying the discovery of failures until later stages.

    The purpose of this step was to create a problem map. If the model is allowed to change initialization order, rendering logic, and input handling at the same time, the program might eventually run, but it becomes difficult to determine which change actually fixed the problem.

    2. Symptom: The Program Exits Immediately After Entering the Main Loop

    During the first run, the program produced some output rather than failing silently. The log indicated that execution reached the area near the main loop and then terminated. This was easier to diagnose than a completely unresponsive program because it showed that building, loading, and entry-point execution were basically working.

    Three categories of problems should be checked first:

    • Whether the main-loop condition becomes false after the first iteration;
    • Whether an initialization function returns an error that the caller interprets as a normal exit;
    • Whether a control-flow path prints a startup marker and then returns directly, such as an error branch that does not continue execution.

    A more reliable approach is to add staged logging:

    • Initialization start;
    • Display-service acquisition;
    • Configuration query;
    • Mode selection;
    • Frame-buffer acquisition;
    • First-frame rendering;
    • Input polling;
    • Each major main-loop state transition.

    Logs should include error codes and important object states, but they should not flood the output.

    If the logs show that the exit occurs after a graphics-service call, the investigation can be narrowed to graphics initialization and mode selection.

    3. Key Issue: A Pointer Becomes Invalid After a Mode Change

    This is a common pattern in low-level development. The code first obtains a configuration object from a query interface and reads a mode identifier or resolution from it. It then calls a mode-selection function. After the mode is successfully changed, the code continues to use the old configuration object to access the frame buffer or stride.

    At first glance, this appears valid because the pointer still exists. In some implementations, however, the pointer refers to an internal temporary structure or a snapshot of the previous state. Once the mode changes, the old contents may no longer be valid.

    The unsafe pattern can be abstracted as follows:

    1. mode_info_t *info = query_mode_info(display);
    2. select_mode(display, info->mode_id);
    3. /* Risk: info may be invalid after select_mode */
    4. draw_frame(info->frame_buffer, info->stride, info->width, info->height);

    A safer approach is to extract only the required parameters during the query phase, select the mode, and then retrieve the current mode information again. Every return value and critical field should be checked.

    1. mode_info_t initial;
    2. status_t st = query_mode_info(display, &initial);
    3. if (st != STATUS_OK) {
    4. log_error("query mode failed: %d", st);
    5. return st;
    6. }
    7. mode_id_t target = initial.mode_id;
    8. st = select_mode(display, target);
    9. if (st != STATUS_OK) {
    10. log_error("select mode failed: %d", st);
    11. return st;
    12. }
    13. /* Important: reacquire the current mode information */
    14. mode_info_t current;
    15. st = query_current_mode_info(display, &current);
    16. if (st != STATUS_OK) {
    17. log_error("reacquire mode failed: %d", st);
    18. return st;
    19. }
    20. if (current.frame_buffer == NULL || current.stride == 0) {
    21. log_error("invalid frame buffer");
    22. return STATUS_INVALID_STATE;
    23. }
    24. render_init(&current);

    The repair principle can be summarized in three points:

    1. Treat borrowed pointers as short-lived objects.
    2. After any operation that changes device state, reacquire the related state.
    3. Before accessing a frame buffer, validate the pointer, stride, width, height, and pixel format.

    The important lesson is not a single line of code, but awareness of resource lifetime.

    4. Symptom: The Wrong Graphics Service Is Acquired

    After the crash was fixed, the next problem was that the graphical interface still did not appear. Logs showed that some service calls returned errors or produced an incomplete object.

    A common cause is a mismatched unique identifier. A firmware environment may contain several interfaces with similar names or related capabilities, such as query, management, and configuration interfaces. If the code uses a similar but incorrect identifier, it may compile successfully but fail at runtime to obtain the expected object.

    The investigation should not blindly replace identifiers. Instead:

    1. Log the interface identifier and return value to determine whether the failure occurs during service location or later use;
    2. Compare the identifier with existing declarations and module dependencies to confirm its intended capability;
    3. Add an explicit failure path and diagnostic message when the service is unavailable instead of continuing with a null or incomplete object.

    For example:

    1. status_t get_display_service(display_service_t **out) {
    2. status_t st = locate_service(DISPLAY_INTERFACE_ID, NULL, (void **)out);
    3. if (st != STATUS_OK || *out == NULL) {
    4. log_error("locate display service failed: id=%x status=%d",
    5. DISPLAY_INTERFACE_ID, st);
    6. return STATUS_NOT_FOUND;
    7. }
    8. return STATUS_OK;
    9. }

    After the fix, validation should cover more than whether an image appears. The program should also produce a useful diagnostic when the interface is missing. In low-level engineering, the observability of failure paths is as important as the success path.

    5. Validate the First Frame and the Rendering Pipeline

    Even with the correct display service, the first frame may remain blank because of:

    • A null frame buffer;
    • An incorrect stride unit;
    • An incompatible pixel format;
    • An out-of-bounds drawing region;
    • An unsupported refresh or presentation method.

    Rendering initialization should be divided into checkable stages:

    • Retrieve the current mode information;
    • Validate the frame-buffer address and size;
    • Record width, height, stride, and pixel format;
    • Use a solid-color fill or simple rectangle as a first-frame self-test;
    • Only then draw game elements.

    The first-frame self-test is particularly useful. It distinguishes a broken display path from a game-logic problem. If a solid fill is visible, the display path is basically functional. If the screen remains blank, the frame buffer, stride, or refresh behavior should be investigated first.

    6. Completion Criteria for Round One

    Round one should not be considered complete merely because the application appears to run. The following conditions should be met:

    • The build process is repeatable, and important warnings have been explained or eliminated;
    • Startup logs clearly identify each major stage;
    • Service-location failures, mode-query failures, and frame-buffer errors produce explicit return values and messages;
    • The program enters the main loop reliably and does not exit because of a single invalid input or null object;
    • The first frame is rendered successfully, and input polling produces observable feedback;
    • Each patch can be explained and corresponds to a specific symptom or risk.

    Round Two: From Running to Maintainable

    The first round addresses whether the program works at all. The second addresses whether it is stable, usable, and maintainable. This is where a large language model can help with refactoring suggestions, boundary-condition enumeration, and test-plan generation, while human review remains essential.

    1. Main Loop and State Machine

    Even a small game should have clear states, such as initialization, running, paused, finished, and restart. The main loop should use a fixed time step or at least a stable delta time so that behavior does not vary significantly across machines.

    Input handling, logic updates, and rendering should occur in distinct stages. Rendering code should not modify objects while it is traversing them.

    1. while (app.running) {
    2. input_poll(&app.input);
    3. if (input_is_pressed(&app.input, INPUT_PAUSE)) {
    4. app.state = APP_PAUSED;
    5. }
    6. if (app.state == APP_RUNNING) {
    7. game_update(&app.game, &app.input, app.delta_time);
    8. }
    9. render_frame(&app.renderer, &app.game);
    10. present_frame(&app.display);
    11. app_tick(&app);
    12. }

    A model can help inspect loop-exit conditions, pause and resume behavior, restart handling, and timer drift. These checks still need to be implemented and tested in the actual project.

    2. Input Debouncing and Boundary Conditions

    Input failures are common in firmware environments. Typical causes include an event queue that is not drained in time, repeated triggering from a held key, confusion between edge-triggered and level-triggered input, and conflicting directional inputs.

    The model can be asked to enumerate boundary conditions such as:

    • Rapid repeated presses;
    • Simultaneous keys;
    • Input while paused;
    • Input after the game has ended;
    • Repeated restart commands.

    Each case should then be validated individually.

    3. Rendering and Memory Safety

    In a resource-constrained environment, rendering optimization should not begin with complex effects. The first priority should be correctness and predictable memory use:

    • Avoid dynamic allocation every frame;
    • Use static or preallocated memory for fixed-size objects;
    • Clip drawing regions before writing to the buffer;
    • Reduce unnecessary full-screen fills;
    • Confirm that the display path actually supports the intended double-buffering or dirty-rectangle strategy;
    • Release or reset resources correctly during exit and restart.

    A large language model can quickly suggest refactoring ideas, but issues involving memory layout, alignment, caching, and platform differences must be verified against the project constraints.

    4. Error Handling and Log Levels

    Logs should generally be divided into four levels:

    • Error;
    • Warning;
    • Information;
    • Debug.

    High-frequency debug logs should be disabled in release builds. Error messages should include the stage, error code, relevant parameters, and an actionable next step.

    1. [ERROR][display.init] locate service failed, id=0x...., status=0x....
    2. [WARN ][display.mode] reacquire mode info returned empty buffer
    3. [INFO ][app.loop] enter main loop, target_fps=30

    The purpose of logging is not to produce more output. It is to reduce the time required to narrow down the next failure.

    5. Regression Test Matrix

    A simple matrix can be used after round two:

    Check Validation method Pass criteria
    Cold start Launch after clearing the environment The first frame appears and stage logs are produced
    Display failure Simulate a missing service The program reports a clear error instead of crashing
    Input Test single keys, repeated keys, and combinations State changes match expectations
    Pause and resume Pause and resume during execution Logic and timing do not jump unexpectedly
    Restart Restart from the finished state Score, position, and resources reset correctly
    Long-running execution Run continuously for an extended period No obvious memory growth or visual drift
    Reproducible build Build from a clean directory Artifacts and documented steps are consistent

    Engineering Practices for Working with Large Language Models

    This practice shows that the primary advantage of a large language model in low-level engineering is not one-click code generation. It is fast, low-cost assistance with code comprehension and hypothesis generation.

    A model can:

    • Read across multiple files;
    • Summarize call chains;
    • Identify abnormal patterns in logs;
    • Propose several possible root causes;
    • Generate small validation patches;
    • Produce regression checklists.

    Its limitations are equally important. It may be overly confident about interface semantics, overlook object lifetime and error codes, or perform a broad rewrite simply because a complete-looking result appears more convincing.

    More reliable collaboration practices include the following.

    1. Address One Verifiable Problem at a Time

    Do not ask the model to repair initialization, rendering, and input handling simultaneously. Each patch should be tied to one symptom and one validation method.

    2. Require an Evidence Chain

    Ask the model which log entry, call order, or return value supports its conclusion. A conclusion without evidence should be treated only as a hypothesis.

    3. Preserve a Known-Good Version

    Save a rollback point before each repair round. Firmware debugging can be expensive, and the ability to revert is more valuable than continuously stacking patches.

    4. Add Observability Before Changing Logic

    Many failures are not caused by incorrect logic, but by swallowed errors. Adding logs and explicit error returns before changing the root logic often produces a more reliable process.

    5. Ask for Regression Items, Not Only Code

    After a change, ask the model which execution paths may be affected. Convert those paths into concrete checks.

    6. Keep Boundary and Compliance Decisions Human-Owned

    Hardware behavior, protocol semantics, licensing, data confidentiality, and release procedures require human review. Generated code should not enter a production firmware project without being built, executed, and checked for safety.

    Conclusion

    If the goal is to generate a demonstration program from scratch, a large language model can appear extremely impressive. That performance can nevertheless hide much of the complexity of real engineering.

    A more valuable scenario is placing the model inside a repair workflow involving legacy defects, limited observability, explicit constraints, and regression testing. In that workflow, the model acts more like a tireless code reviewer and hypothesis generator. It can quickly identify suspicious pointer lifetimes, ignored error codes, confusing initialization order, and missing boundary conditions. Whether the result is deliverable still depends on builds, logs, tests, and human judgment.

    The two-round repair process leads to a simple conclusion:

    The key to AI-assisted low-level development is not making the model get everything right in one attempt. It is making the model participate in a verifiable engineering loop.

    Establish observability before narrowing the problem. Apply minimal repairs before structural optimization. Make failures diagnosable before pursuing complete functionality. For firmware, drivers, boot environments, and other low-level projects, this method is more meaningful than simply comparing code-generation speed.

    When a team needs collaboration and auditability, it can also combine general-purpose container builds, logging, and object storage capabilities from services such as Baidu AI Cloud to archive build artifacts, runtime logs, and repair records in one place. The important point is the general engineering workflow: every AI-assisted change should have a source, evidence, regression coverage, and a rollback path.

    Only when model capabilities are placed inside this kind of closed loop does “large language models writing code” become more than a demonstration. It becomes a reusable engineering capability.