The recompiled ROM

cpu/recomp.c takes the ROM's ARM and Thumb code through the host's native back end, block by block (src/common/cpu/arm/aot.h; the recompile says how many it made, and why it refused one). It links the objects with the helpers they call (armlib.c) into a native module with the recompiler's own linker (src/common/cpu/recompiler/object/link.c), as the Switch's 32-bit modules are: no C compiler is needed. Loading a ROM does this automatically and caches the module in the game's folder as <name>_code.so, as the NX system type caches <Game>_code.so; -r rebuilds it. Blocks the discovery step does not reach, a branch through a register or the instruction after one the board interpreted, are compiled when they are first dispatched. The miss starts a discovery walk of its own, and each block it finds is compiled in the process at its first dispatch (src/common/cpu/arm/exec.c), so a miss costs that block and not the whole ROM. When the run ends, the blocks it compiled are kept in the map as hints, at once, as the 3DS and Vita drivers keep theirs: the next run compiles them again in the process when it reaches them, and a recompile (-r, R in the launcher) builds every block the runs reached into the module. A block built from RAM runs while RAM holds the words it was built from, and is built again once the game writes over them; the map names the ROM's hash, and a map of another ROM is refused with both.

The CPU state is the recompiler's SnxArmCPU on a flat window (cpu/memmap.c) where the board's regions live at their canonical addresses with the console's mirrors mapped to the same pages, so generated code reaches everything with one pointer add. Each region is a host memory object (src/common/memory/host.c, as the 3DS's RAM) mapped at every mirror at the host's page size; a host whose pages are past 32 KiB keeps each mirror as RAM of its own, and the runner says so. The blocks are compiled with the backend's ARM7TDMI profile (src/common/cpu/arm/README.md), which follows the console's CPU where ARMv8-A differs (rotated misaligned loads, PC + 12 stores, POP {pc} without interworking) and sends the BIOS, I/O window, palette, VRAM, OAM and save memory accesses to the board's bus hook. The privileged instructions the recompiler refuses, the mode switching MSR/MRS forms, SWP and the exception return, are interpreted against the banked register state. Software interrupts go to the HLE BIOS and interrupts follow the entry and return the BIOS stub performs. The board starts with the registers as the BIOS leaves them (forced blank, the serial port's general purpose mode, the sound's bias at its middle, POSTFLG), which a game reads to tell a link boot from its own. A block's time is replayed from the trace it leaves (cpu/timing.c, NanoBoyAdvance's model of the bus): each fetch, load and store at its region's wait states from WAITCNT, the cartridge's prefetch unit and the internal cycles. An access to the I/O window first runs the board up to its own cycle, so a timer read, a DMA or an interrupt lands inside the block where the console has it; near an interrupt the block runs an instruction at a time. The save memory width rules are approximations.


Made from docs/platforms/agb/README.md at 69237645142c. GPL-3.0-or-later.