AI-Assisted Development of UEFI Graphics Applications: Practices and Key Points
This article explains how to use AI programming assistants to build a UEFI graphics application, using a vertical shooter as an example. It covers the firmware graphics environment and GOP, emulator-based testing, task decomposition into environment setup, core features, and enhancement iterations, and implementation points for double buffering, keyboard input, collision detection, the game loop, and offline glyph data.
1. Problem Background
UEFI applications run before the operating system loads. They do not have a complete process model, driver stack, or window system. The program directly works with the graphics frame buffer, input devices, and timers. Such programs are often used to validate a development environment, understand firmware-layer interfaces, and serve as a complete small engineering sample.
When using AI programming assistants in this type of project, the main factor affecting the result is not code generation speed, but whether the model can accurately understand the constraints: what the target platform is, which protocols are available, which libraries cannot be depended on, and how the build chain is organized. If these constraints are not stated clearly, the model tends to apply patterns from general-purpose applications, and the result cannot be compiled or run in the firmware environment.
This article uses a vertical shooter as an example to show how to break the task into steps that can be verified one by one, along with the key checkpoints for each step.
2. Technical Approach
2.1 Runtime Environment and Graphics Output
UEFI applications are compiled in PE/COFF format. The entry function receives an image handle and a system table pointer. Graphics output relies on the standard EFI_GRAPHICS_OUTPUT_PROTOCOL (GOP). The program must first locate this protocol through Boot Services, then read the resolution information of the current display mode.
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop = NULL;EFI_STATUS Status;Status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid,NULL,(VOID **)&Gop);if (EFI_ERROR(Status)) {return Status;}UINT32 Width = Gop->Mode->Info->HorizontalResolution;UINT32 Height = Gop->Mode->Info->VerticalResolution;
The resolution must be read at runtime and must not be hard-coded. Display modes may differ between emulators and real machines. The game coordinate system should be calculated based on the actual width and height, for example, placing the initial fighter at the bottom center of the screen.
2.2 Role of the Emulated Runtime Environment
When real hardware debugging conditions are not available, an emulator can be used to run the build output. The emulator provides protocol interfaces close to those of real firmware and is suitable for rapid iteration in the early development stage. Its limitations should also be understood: display refresh, timer accuracy, and input latency differ from real hardware. Frame rates measured on an emulator only indicate whether the logic is working normally and cannot be used directly as a real-hardware performance conclusion.
3. Task Decomposition for AI-Assisted Development
Most rework comes from task granularity being too large. It is recommended to split instructions into the following three stages.
3.1 Environment Preparation Stage
The goal of this stage is to obtain a minimal project that can compile and boot. The instructions should include:
- Target architecture and toolchain version requirements;
- Configuration path for the emulated runtime environment and location of output artifacts;
- External dependencies that are not allowed;
- Acceptance criteria, such as “compiles without errors, and the emulator can enter the application and display text.”
Do not mix environment setup with business logic in the same round of instructions. First confirm that the build and boot process are normal, then move to the next stage. This limits the scope of subsequent regression issues to the business code.
3.2 Core Feature Stage
Core features include graphics drawing, input handling, collision detection, and the game loop. It is recommended to list them as a checklist, with explicit inputs and expected outputs for each item. For example:
- Player fighter: moves left and right, fires bullets when a key is pressed, and does not move beyond the screen boundaries;
- Enemy aircraft: spawns at random positions above the screen, moves downward at a constant speed, and is destroyed after leaving the screen;
- Collision: when a bullet collides with an enemy aircraft, both are destroyed; when the player collides with an enemy aircraft, the player’s life value is reduced.
Each feature should have a phenomenon that can be checked directly in the emulator. Unclear requirements, such as “increase gameplay,” will cause the model to decide the details on its own, making the result difficult to accept.
3.3 Enhancement Iteration Stage
The enhancement stage includes power-ups, weapon upgrades, wingmen, and boss fights. Add only one category of functionality per round, and keep the previous round’s runnable version. In this way, when a regression occurs, it is possible to quickly identify which category of change introduced the problem.
4. Key Implementation Points of Core Modules
4.1 Double-Buffered Drawing
Drawing primitives directly on the screen one by one causes flicker. A common approach is to first complete the drawing of one frame in an in-memory back buffer, then output the entire frame to the display with a single Blt operation.
Gop->Blt(Gop,BackBuffer, // EFI_GRAPHICS_OUTPUT_BLT_PIXEL arrayEfiBltBufferToVideo,0, 0, // Source origin0, 0, // Destination originWidth, Height,Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
The size of the back buffer should match the current resolution. It must be reallocated when the resolution changes; otherwise, out-of-bounds writes may occur.
4.2 Keyboard Input Handling
UEFI’s Simple Text Input protocol reports only key press events, not key release events, and it cannot reliably detect multiple keys pressed at the same time. Therefore, movement control should be designed as “a key press triggers one displacement” or “after a key press, the movement state is maintained for a period of time,” rather than relying on a continuously held state.
The following shows a non-blocking key read:
EFI_INPUT_KEY Key;EFI_STATUS Status;Status = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);if (Status == EFI_NOT_READY) {// No input this frame; continue updating game state} else if (!EFI_ERROR(Status)) {// Process input based on Key.ScanCode and Key.UnicodeChar}
4.3 Collision Detection
Most shoot-‘em-up games use axis-aligned bounding box (AABB) detection. A collision is determined when two rectangles overlap on both coordinate axes.
BOOLEANCheckCollision(INT32 AX, INT32 AY, INT32 AW, INT32 AH,INT32 BX, INT32 BY, INT32 BW, INT32 BH){return (AX < BX + BW) && (AX + AW > BX) &&(AY < BY + BH) && (AY + AH > BY);}
This method requires little computation and has straightforward logic, making it suitable for use in the firmware environment. If the number of enemy aircraft increases, objects can first be grouped by row or column to reduce the number of pairwise comparisons.
4.4 Game Loop and Frame Rate Control
The game loop usually includes four steps: read input, update state, render the frame, and wait for the next frame. A fixed timestep approach sets a target duration for each frame, for example about 16 milliseconds, and uses a timed wait to fill the remaining time.
while (!GameOver) {ProcessInput();UpdateEntities();RenderFrame();gBS->Stall(16000); // Unit: microseconds}
The precision of Stall is affected by the firmware implementation, and the actual interval may vary in different environments. If game speed depends on frame rate, the update function should record the timestamp of the previous frame and adjust displacement according to the actual elapsed time.
4.5 Glyph Data
Firmware programs usually cannot depend on a runtime file system. Therefore, glyph data should be generated offline before the build and compiled into the program as constant arrays. The process is roughly as follows:
- Extract the required characters from a font file, such as frequently used Chinese characters and UI text;
- Rasterize each character into a fixed-size bitmap;
- Generate a header
