scarletnx-3ds runs application games — a .cia, a cartridge image
(.cci, .3ds), a bare NCCH (.cxi, an .app, homebrew's .hbb), a bundle of
one (.bcci, .bcxi, .bcia), or a z3ds one (.zcci, .zcxi, .zcia) — through
offline static AOT -> native module -> checked ARM/Thumb execution,
with the compiler built into the driver. A launch reuses a finished extraction
and module and runs until the game exits or its window closes (or for --seconds N or
--frames N, after which it reports where each thread is).
A game's folder is a folder of the data folder's recompiled/, named after its title
ID as a Switch game's is named (recompiled/0004000000123400/). It holds the compiled
module (<title ID>_code.so, _code.dll on Windows), the blocks runs compiled at
run time (<title ID>.hints), the game's state (sdmc/, savedata/, frames,
audio), the 48x48 icon (icon.rgba, when the game has an SMDH), <title ID>.ini,
a copy of the runtime named <title ID> and, on Windows, the DLLs beside that
runtime, which the copy loads. The .ini
names the game (source=), the console (model=) and the system (system=3ds),
which the launcher and a build's deploy pick the driver by. Running the copy plays the
game, as a Switch game's copy does: recompiled/<title ID>/<title ID> --seconds=60
--script=15000:1, or scarletnx-3ds --game=recompiled/<title ID>. Where scarletnx is
also the 3DS driver (SNX_MULTICALL), the copy is scarletnx, which runs a 3DS game
under a 3DS title ID's name. A build copies its runtime beside every game again
(cmake/deploy.cmake): a 3DS title ID's folder gets the 3DS driver. --outdir DIR
names another folder of games, and --output DIR the game's folder itself. A game
an older version kept in work/3ds/<the game's name> moves into its new folder the
first time it is played, its module and hints renamed.
scarletnx-3ds takes the common options, each value as --option VALUE or
--option=VALUE. Its --script is the setting ctr_script, -f the setting fullscreen, --frames N ends the run
after N decoded frames and --shot PATH writes the last frame of both screens as one PPM to exactly PATH. -H
(--headless) starts no SDL video, audio or gamepad subsystem and opens no window, whatever display the host has. Of the recompile's, --chunk N puts
N instructions in a chunk (512 blocks without it), --jit-cache names another game folder's <title ID>.hints,
--jit-only makes a module of no blocks, and -O and --keep-ir, LLVM's, are said and do nothing. A recompile says a line for each chunk, as the Switch's
says one for each code unit.
What a file holds is read from its own header, not its name: a CIA's magic at 0, an
NCSD's at 0x100, a bare NCCH's at 0x100 (src/3ds/loader/loader.c). A bundle is a
tar whose game is its first entry named as a game's, read where it lies in the
archive (src/3ds/loader/tar.c), each header held to its checksum and no entry's
data read past the archive's end. A z3ds game is Azahar's launcher's compressed one:
its frames are decompressed once into the plain container beside the title's folder
(<folder>.unpacked.cci, named by the input's extension without its z) and read from
then on as that container is (src/3ds/loader/z3ds.c), the stream held to the size
its header declares. A cartridge
image's game is its NCSD partition 0, and the whole image is read where it lies (the
loader takes a card-sized image, up to 8 GiB, since nothing of it is written). A
cartridge whose partitions carry the game's update title (the base title ID with the
update high bits 0x0004000e00000000, in partition 7 or a New 3DS's 6) is played as
that update: the update NCCH's ExHeader, code, ExeFS sections and RomFS are the
game's, as Azahar's overlay_ncch is, so the process the game runs in is the update
title's, as the console runs it once an update is applied, while the game's folder
stays named by the base NCCH's title ID, as the card is one game.
A game's standalone update (a CIA of title type 0x0004000e, or a bare update NCCH) and
its DLC (a CIA of title type 0x0004008c) are kept as a Switch game's are: each is
recorded beside the game in its folder, <title ID>_<its entry>.ini naming its file
(source=, read where it lies) and its data folder <title ID>_<its entry>_data, and
checked in the game's addons= (one update at most). scarletnx-3ds UPDATE.cia or
scarletnx-3ds DLC.cia, alone once the game is made, or scarletnx-3ds GAME.cia
--update UPDATE.cia --update DLC.cia, records each and plays the game with them; the
launcher's box of the game lists them and picks them as a Switch game's. An update's
title ID is the game's low half under 0x0004000e, as Azahar's am makes it, and a
DLC's the low half's 28 bits under 0x0004008c, as Azahar's game list finds it; another
game's is refused with both IDs. The game plays with what its addons= checks: the
update's ExHeader, code and RomFS are the game's, its ExeFS files answering by name
before the base's, and a DLC's contents are read where they lie, served by the NCCH
archive (0x2345678A) by their place in the TMD, and answered by am:app as Azahar
answers for an installed DLC at 6e42de8: GetDLCContentInfoCount gives the TMD's
contents, ListDLCContentInfos lists them from the given start (owned, and downloaded
when the CIA holds the content), GetDLCTitleInfos gives the main content's size, the
title version and type. A checked update or DLC whose file is not there is said, with
the path, and the game plays without it; a game played from its extraction plays with
none, and says which it leaves out. Each is decrypted, when it needs it, as a game is.
A CIA's content table, ticket and TMD are parsed as before; a cartridge and a bare NCCH have none, and their NCCH is checked as a CIA's boot content is.
An encrypted CIA, as a console dump is, is decrypted once with the user's own keys
into a copy beside the title's folder (<folder>.decrypted.cia, published only
complete), which is read from then on (src/3ds/loader/decrypt.c). The keys are an aes_keys.txt (Azahar's format,
or the plain key dumps that call the constant generator= rather than generatorConstant=; GodMode9 itself keeps
its keys in a binary aeskeydb.bin and writes no aes_keys.txt), looked for in
~/.3ds/aes_keys.txt (%USERPROFILE%\.3ds on Windows) as
the Switch driver looks in ~/.switch, or named with --keys FILE; the launcher's
Install Keys installs it there too (docs/user/gui.md). Only encrypted 3DS files need it: a decrypted or homebrew 3DS
game runs without an aes_keys.txt. The keys are
never part of ScarletNX. Both layers come off: each content's AES-CBC under the
ticket's title key (itself under the common key), and the NCCH's AES-CTR under
the primary key (slot 0x2C) and the secondary one its crypto method names (0x2C,
0x25, 0x18, 0x1B). The decrypted CIA says NoCrypto and carries the decrypted
contents' hashes, and it is checked as any game is before it is published, so keys
that are not the console's never leave a game behind. Seed crypto (seeddb) is not
supported yet. An encrypted cartridge image or bare NCCH has no CIA layer: its
NCCHs' own layer is taken off where each NCCH lies (the game partition and an update
partition both), and the whole image copied through into the copy beside the title's
folder (<folder>.decrypted.cci), named by the input's extension, then checked as
any image is (a bundle's copy is the bare game, read from then on by its content). A copy of a card-sized image takes its size in disk, and an
already-decrypted image is read as it is.
Code the static discovery misses (an indirect call's target, a return into it)
compiles when the dispatch first reaches it (src/3ds/aot/jit.c, through the ARM
backend's executor), and goes in the game's <title ID>.hints, which the next
recompile (-r) seeds discovery with, so it is static from then on. A
commercial title's 7 MiB of text compiles to about 340000 blocks in about six
and a half minutes on six cores (the chunks compile in parallel).
A commercial title, decrypted this way, runs its startup (cfg, FS with its save data, APT, and the system services the runtime answers generically), creates its save data and stops at its first prompt for the A button, drawn through the PICA renderer. Input, audio and the kernel are implemented.
Made from docs/platforms/3ds/README.md at 69237645142c. GPL-3.0-or-later.