Skip to main content

Module speedrun

Module speedrun 

Source
Expand description

v3.5.0 (T-SPEEDRUN-MEMIF) — speedrun auto-timing on RAM predicates, counted in master-clock cycles.

A speedrun timer has to answer two questions every frame: has the run started (or reached its next split, or been reset), and how long has it taken. RustyNES answers the first with RAM predicates written in the RetroAchievements condition grammar (the 0xH0770=1_d0xH0770=0 strings achievement sets are written in, so a condition copied from an achievement set or a splitter’s notes works unchanged), and the second with the console’s own master clock: the time of a run is the number of master- clock cycles between the frame its start predicate fired and the frame its last split fired.

§Why the master clock, not frames or the wall clock

  • Wall-clock time depends on the host (a stutter, fast-forward, a paused window). A run timed from a movie must give the same number on every machine and every replay, and only emulated time can.
  • Frames are not all the same length. An NTSC frame is 89,342 or 89,341 PPU dots depending on the odd-frame skip, the extra-scanline option lengthens them, and a lag frame is still a frame. The master clock counts what the console actually ran: crate::Nes’s CPU master clock, which advances by the region’s divider (12 NTSC, 16 PAL, 15 Dendy) on every CPU cycle. A difference of two readings is exact, and seconds follow from the region’s master-clock frequency (master_clock_hz).

The predicates are evaluated once per frame, after the frame (the same cadence RetroAchievements evaluates achievements at), so a split’s time is resolved to the end of the frame its condition first held in. That is the resolution every RAM-driven splitter has; what this adds is that the frame boundary itself is exact.

§The condition grammar (the subset implemented)

Written from the RetroAchievements developer documentation of the condition syntax (public prose; no implementation was consulted). A condition set is one or more groups separated by S; the set is true when the first (core) group is true and, if there are more groups, at least one of them is. A group is conditions separated by _, all of which must hold. A condition is

operand  comparison  operand  [ "." hits "." ]
PieceAccepted
memory operand0x + size + hex address. Sizes: M..T bit 0..7, L low nibble, U high nibble, H 8-bit, none (or a space) 16-bit, W 24-bit, X 32-bit (little-endian), I / J / G 16 / 24 / 32-bit big-endian, K bit count of a byte
operand prefixd delta (the value at the previous evaluation), p prior (the value before the last change), b BCD, ~ bitwise invert (within the operand’s size)
constantdecimal (12) or h + hex (h1F)
comparison= != < <= > >=
hit target.N.: the condition must have been true on N evaluations (counted while its trigger is armed)

Addresses are RetroAchievements NES addresses, the same ones rustynes-cheevos maps (ra_addr_to_nes): the CPU bus itself, per the rcheevos region table (system RAM at 0x0000, cartridge RAM at 0x6000, ROM at 0x8000), read side-effect-free; anything above 0xFFFF reads 0. ra_flat_to_cpu is the mapping; a frontend test (in a retroachievements build) pins it to the cheevos crate’s. (Until v3.5.0’s review both mapped 0x0800..=0x27FF onto cartridge RAM, which is not RA’s layout; CodeRabbit on #624.)

Not implemented, and refused with a message rather than misread: condition flags (R: reset-if, P: pause-if, A: add-source, B:, C:, N:, M:, Q:, I:, T:, Z:, O:, D:, K:), float and negative constants, arithmetic operators (*, /, &, ^, %, +, -), and the f float sizes. A splits file has its own reset predicate, which covers the common use of R:.

§The splits definition

A small line-based text format (SplitsDefinition::parse):

# comments and blank lines are ignored
game = Super Mario Bros.
category = Any%
start = 0xH0770=1_d0xH0770=0
split = World 1 | 0xH075f=1_d0xH075f=0
split = Bowser | 0xH0770=2
reset = 0xH0770=0

start and at least one split are required; the last split ends the run. reset is optional. Keys are split from values at the FIRST =, so a condition keeps its own = signs; a split’s name and condition are split at the first |.

§Determinism

The timer only reads: a FnMut(u32) -> u8 peek closure (side-effect-free on the NES: crate::Nes::peek) and two counters. It never writes the console, so attaching it cannot change a frame, a sample or a bus cycle, and the same movie replayed twice produces the same split times (crates/rustynes-test-harness/tests/speedrun_timer.rs checks both).

Structs§

ParseError
Why a condition or a splits file was refused.
SpeedrunTimer
The timer. Feed it Self::observe once per frame, after the frame.
Split
A named split: the predicate that ends its segment.
SplitTime
One split’s time.
SplitsDefinition
A parsed splits file (see the module docs for the format).
Trigger
A parsed condition set: the core group and any alternative groups.

Enums§

TimerEvent
What one observation changed.
TimerState
Where a timer is.

Constants§

MAX_OBSERVATION_GAP
The longest gap between two observations that still counts as one continuous run.
MAX_SPLITS
The most splits a definition may have, which is also the size of the memory-export interface’s split table.

Functions§

cycles_to_seconds
cycles master-clock cycles of region, in seconds.
master_clock_hz
The master-clock frequency of region in hertz.
master_clock_hz_ratio
The master-clock frequency of region as an exact fraction (numerator, denominator) in hertz.
ra_flat_to_cpu
A RetroAchievements NES address as a CPU-bus address: the identity over 0x0000..=0xFFFF, None above it. See the module docs.