Skip to main content

Module nsf_rip

Module nsf_rip 

Source
Expand description

v3.6.0 (T-AUDIO-STEMS) — trace-assisted NSF ripping: find a game’s music driver from what the CPU does, propose its init and play addresses, and write an NSF that drives it.

An NSF file (nesdev “NSF”) is a music engine lifted out of a game plus a header that says how to drive it: load the program image at load, call init once with the song number in A, then call play once per frame. A ripper’s job is to find those two routines. This one finds them by watching the game run.

§How the routines are found

Tracer steps the console one instruction at a time and keeps a shadow call stack: a JSR pushes its target; an interrupt (a step that pushed three bytes and landed on the NMI or IRQ/BRK vector’s target) pushes the vector; and an entry is popped when the stack pointer climbs back to where it was before the call, which is what RTS and RTI do (and also what a routine that discards its own return address does). The CPU bus access log (Nes::set_access_logging) names every write an instruction made; each write to an APU register ($4000-$4013, $4015, $4017: not $4014, which is OAM DMA, nor $4016, the controller strobe) is recorded with the shadow stack it happened under.

propose then reads the evidence:

  • play is the routine that encloses every APU write of a frame. For each frame, the common prefix of its writes’ call chains is taken; a routine that is in that prefix in the most frames is the driver, and of several such (the NMI handler’s body calling the driver, say) the innermost is chosen, because an outer one also does the game’s other per-frame work. The evidence reported is how many frames it enclosed every write in, how many times it was called per frame, and how many of the run’s APU writes it enclosed.
  • init is the routine that encloses every APU write made outside a play call, if there is one: that is the code that silences the channels and sets the engine up before the first frame plays. When the game makes no such writes from a subroutine (its reset code writes the APU inline, or not at all), no init is proposed and the NSF gets a no-op one, said so in the report: the NSF player zeroes RAM before init, which is often all the engine needed.

These are heuristics over one run, and the report says which: the proposal is what the evidence supports, with the counts to judge it by. The gate is that the NSF plays the tune — rustynes-core’s tests and docs/audio-stems.md describe it: the NSF’s play calls make the same APU writes, call for call, as the game’s.

§What can be ripped

The program image is the cartridge’s PRG-ROM, loaded at $8000. That is only the code the console ran when the PRG is not bank-switched: mapper 0 (NROM) and mapper 3 (CNROM, whose banking is CHR only), up to 32 KiB, a 16 KiB image mirrored to fill $8000-$FFFF as the board does. A bank-switched board would need the NSF’s own 4 KiB bank registers set to the layout the driver runs under, which this does not infer; build_nsf refuses it with that reason rather than write a file that does not play. It refuses a driver that runs from RAM for the same reason: the code is not in the image (the game copied it there), and the NSF player clears RAM before init.

Modules§

example
The routines of example_music_rom’s program, for the tests and docs that drive it.

Structs§

ApuWrite
One APU register write and the calls it happened under.
Call
One entry of the shadow call stack, kept for the whole run.
NoOpInit
Where build_nsf pointed a no-op init.
NsfInfo
The metadata strings an NSF carries (32 bytes each, NUL-padded; longer text is cut at a character boundary).
PlayEvidence
What the trace says about the play routine.
Proposal
The proposal propose makes.
Tracer
A running trace over one Nes.

Enums§

CallKind
How a routine on the shadow call stack was entered.
InitEvidence
What the trace says about the init routine.
RipError
Why no proposal could be made.

Functions§

build_nsf
Write an NSF (version 1, one song) that drives proposal over the cartridge rom’s PRG.
example_music_rom
A homebrew NROM with a tiny music driver, written for the ripper’s tests and docs.
is_apu_register
The CPU registers a write in this range is an APU register write for the purposes of ripping: $4000-$4013, $4015, $4017.
propose
Propose init and play from a finished trace.