The compiled module

The game's code is in RAM on this console, and a game replaces parts of it as it plays: its overlays, which the SDK loads from the card over one another at the same addresses. So an address alone names no code. The loader reads from the image every piece of code whose address is known (src/nds/cartridge/rom.c): the ARM9 program, decompressed as its start code would decompress it (the field the start code decompresses by is cleared, so the game does not do it again), the blocks the start code copies elsewhere (the ITCM's code among them), and each overlay, decompressed. These are the image's regions.

The offline compiler (src/nds/aot/) discovers the code of each region from the entry point, the overlays' static initializers and the hints, along every direct branch and call. A function reached only through its address is found from the words that hold the address, where a function can start: at code that reads as code, right after the end of code already found or at a region's start. The blocks are compiled in chunks on the threads --aot-threads gives, by the host's native back end (src/common/cpu/arm/aot.h, which the recompile says), and linked by the backend's own linker, with a table of blocks for each region and instruction set and each block's length.

At run time (src/nds/cpu/recomp.c) a block runs only while memory holds the bytes it was compiled from, which every dispatch compares. The first time an address is dispatched, the module's regions that hold it are looked through for a block whose bytes are in memory. When none is, the block is compiled in the process (src/common/cpu/arm/exec.c) from memory as it is. Blocks of other code at the same address stay, and are found again when their code comes back. A block compiled at run time whose bytes are a region's is a hint: the run adds it to <name>.hints, and the next -r starts its discovery there too.

The blocks are compiled with the backend's ARM946E-S profile (src/common/cpu/arm/README.md): ARMv5TE on the flat window, with the I/O window's accesses sent to the board's bus hook. The instructions the profile leaves to the system run in the runtime against the banked registers (src/nds/cpu/cpu.c): MRS and MSR, SWP, the exception returns, the LDM and STM of the user bank, and CP15, whose registers the SDK's start code sets (the caches, the protection unit's regions, the two TCMs) and whose wait for an interrupt halts the machine.


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