Porting a 6502 Assembly Game to UEFI with a Large Language Model
This article describes a practice project that reconstructs one stage of a classic side-scrolling shooter from 6502 disassembly and runs it in the UEFI Shell. It covers the constraints of a near-firmware environment, how a long-context model was used to recover structure and behavior, an agent-style milestone workflow, and the cost and engineering tradeoffs of assembly-level porting.
1. Background: Why Port at the Assembly Level
In embedded and low-level systems work, bringing a classic platform game back to life on bare metal or near-bare-metal hardware is a hard problem. These projects often have no reusable high-level source. The only reliable reference is community reverse-engineered 6502 disassembly. Compared with porting from a high-level reference implementation, an assembly-level port is an order of magnitude harder:
- Logic is implicit in the instruction stream. Function boundaries, data structures, and state machines must be inferred from labels, jump tables, and calling conventions.
- Asset formats are idiosyncratic. Graphics are organized as Super-Tile composite tiles and run-length encoded (RLE) data that must be decompressed and assembled at runtime.
- Behavior is tightly coupled to hardware. Programmable bank switching, vertical-sync timing, and sprite versus background priority are written directly into the instructions.
The starting point for this project was exactly that kind of disassembly: seven banks, more than eight thousand lines of .asm, and dozens of labels and jump tables. The goal was to fully reconstruct one stage of a classic side-scrolling shooter and run it smoothly in the UEFI Shell.
2. Technical Constraints of the Target Environment
The UEFI Shell sits close to the firmware. It has no operating-system graphics library or audio stack. The useful capabilities are mainly:
- Graphics Output Protocol: a linear framebuffer base address, pixel format, and resolution. Pixel writes must be done by the program.
- Boot Services: basic services such as memory allocation, events, and timers.
- File services: assets can be read from disk or embedded in the executable image.
The core of the port is therefore to reimplement the game’s render, input, and logic loops in C, and to handle scaling and double buffering directly:
// Pseudocode: integer scaling plus double-buffered renderingfor (int y = 0; y < 240; y++) {for (int x = 0; x < 256; x++) {uint32_t px = backbuffer[y * 256 + x]; // native 256x240 framefor (int sy = 0; sy < scale; sy++)for (int sx = 0; sx < scale; sx++)fb[(y * scale + sy) * pitch + x * scale + sx] = px;}}
A native resolution of 256×240 with 2× or 3× integer scaling avoids the pixel distortion of non-integer scaling and removes tearing. In the emulator used for this project, scrolling stayed stable and smooth.
3. From Assembly to C: Getting the Model to Understand the Game
This was the most important part of the project, and the place where a long context window mattered most. Success did not come from translating the listing piece by piece. The model first needed a global view, then a layered implementation:
Ingest the full context. The complete disassembly, including every bank and jump table, went into context instead of fragmented snippets. A window on the order of a million tokens let the model see data tables, interrupt handlers, and the main loop together, which reduced mistranslations caused by a local-only view.
Recover static structure. The model was first asked for a high-level map: the PRG-ROM UNROM mapper layout (128 KB split into eight 16 KB banks, with a fixed bank mapped at the high end of the address space and the others switchable), reset and interrupt entry points, the main-loop skeleton, enemy AI update functions, and the call chains for collision detection and weapons.
Separate data from logic. Graphics are stored as RLE-compressed Super-Tile composite tiles. They were extracted into independent asset files, decompressed at runtime, and assembled from the tile map, rather than hardcoded into the logic. That separation made later debugging much cheaper.
Reproduce behavior, not instructions. Enemy AI (fixed-point fire, tracking aim, fast movement, and obstacle crossing), weapon switching (six types: rifle, machine gun, fireball, spread shot, laser, and shield), and boss-fight logic were rebuilt as state machines plus parameter tables. The result stayed close to the original feel and was easier to debug in C.
4. Agent Workflow: Task Breakdown and Iterative Verification
Asking a model to “generate the whole game” in one pass is not realistic. In practice the agent has to be directed like a senior collaborator:
- Break the work into milestones: render pipeline, level-data loading, player control, enemy AI, weapons and collision, then boss and stage flow. Each milestone must be independently verifiable.
- Close the feedback loop: run in a UEFI emulation environment and send compile errors, runtime logs, and descriptions of screenshots back to the model to localize faults.
- Manage context: a longer context is not automatically better. Unrelated modules dilute attention. Organize context by module and prefer the source and failure information that belong to the current task.
- Keep commitments conservative: for uncertain hardware behavior, such as side effects of bank switching, require the model to state its assumptions explicitly, then correct them with runtime checks.
With that process, one complete stage was finished in a day: 13 screens of horizontal scrolling, smooth scroll, the full enemy AI set, six weapons, and a complete state machine from title, through player select, auto-demo, flash transition, and the core loop, to the end-of-stage result screen.
5. Cost Versus Benefit
Assembly-level understanding consumes a large number of tokens. Reinjecting thousands of lines of assembly and iterating on bugs added up, in this project, to on the order of tens of RMB, about RMB 45. Practical ways to control that cost include:
- Replace confirmed assembly sections with an understanding summary, and use the summary instead of the original text in later iterations.
- Prefer locating questions (“which code references this jump table?”) over open-ended prompts, to cut unproductive generation.
- Freeze code that has already been verified so later rounds do not rewrite stable modules.
6. Engineering Takeaways
- Long context changes the reverse-engineering workflow: from annotating the listing by hand, section by section, toward global model understanding plus human checks of the critical assumptions.
- Agent workflows fit work that has a clear verification signal. Compile and run results are the most reliable judge. The value of the workflow is turning that signal into corrections efficiently.
- The same method can extend to assembly-level reconstruction of other 8-bit and 16-bit platform games, and to behavioral understanding in firmware security research.
- Keep the result auditable. Have the model emit a structure map and an assumption list so a human can review the port and avoid hidden bias from a black-box translation.
7. Conclusion
This project shows that a large language model with a very long context and agent-style iteration can reconstruct complete game logic from 6502 assembly into C and deliver a full stage that runs under the UEFI Shell. It does not show a model replacing low-level developers. It shows a different collaboration: the developer owns the goal, verification, and key decisions; the model handles large-scale pattern recognition and code generation. For engineers in reverse engineering, firmware research, and systems porting, that is a toolchain upgrade worth learning deliberately.
