## Building ```text cargo build --release scripts/ladder.sh # the gate (needs external/refcache and the HuCard library) target/release/pce-bench CARD.pce --frames 600 [--machine tg16] ``` The CPU is [huc6280-rust](../huc6280-rust) (crate `huc6280`), **by path (`../huc6280-rust`) while it is local only**, as in the base board: clone both side by side. This fork does not edit it, nor 6502-rust under it. `external/refcache` (the reference's traces of the 275 cases) is copied from pcengine-rust's; it is not committed. ROMs are bring-your-own, read in place and never committed (`.gitignore` refuses their extensions). Nothing GPL or LGPL was read or transcribed: the only sources are pcengine-rust and, through it, pcengine-python, both ours and MIT. No emulator's source was opened. ## Where the time goes Share of the samples by source file (`pce-profile`, library code charged to its caller), 600 frames at the fork point and 2,000 now: | | Blazing Lazers, fork point | now | Street Fighter II', fork point | now | | --- | ---: | ---: | ---: | ---: | | The CPU core (huc6280-rust, and the 65xx frame of 6502-rust under it) | 10.4% | @@BL_CORE@@ | 8.0% | @@SF_CORE@@ | | `board.rs`: the bus callbacks, `run_to`, the decode | 28.4% | @@BL_BOARD@@ | 29.2% | @@SF_BOARD@@ | | `vdc/chip.rs`: `next_event`, `catch_up`, the port | 35.7% | @@BL_CHIP@@ | 39.2% | @@SF_CHIP@@ | | `vdc/sequencer.rs`, `vdc/dma.rs` | 5.8% | @@BL_SEQ@@ | 5.4% | @@SF_SEQ@@ | | `vdc/background.rs`, `vdc/sprites.rs`, `video.rs`: the picture | 9.0% | @@BL_PIC@@ | 10.0% | @@SF_PIC@@ | | `cards.rs`, `pagetable.rs` | 3.3% | @@BL_CARD@@ | 2.4% | @@SF_CARD@@ | | `psg/chip.rs`, `mixer.rs` | 1.1% | @@BL_PSG@@ | 0.7% | @@SF_PSG@@ | At the fork point two thirds of a frame were the scheduler: after every CPU cycle `run_to` asked each VDC and the timer for their next event (`Vdc::next_event` alone was a third, most of it an iterator over an array of three options) and `deliver_lines` recomputed the three interrupt lines. The picture, the sound and the CPU together were a fifth. Now the profile is flat: no source line is above 4% (the hottest, at 3 to 3.5% each, are the bus callbacks' first lines in `board.rs` and, in the CPU core, the opcode table lookup of `execute_sequence` and the dispatch's `match`), and **the largest share is the CPU core**: @@BL_CORE@@ of Blazing Lazers and @@SF_CORE@@ of Street Fighter II', which this fork does not edit. What a `huc6280-rust-fast` would target, from the lines that show: the dispatch (`OPCODES[opcode]` gives an operation and an addressing mode, then two nested matches: one table of handlers would be one indirect jump an instruction); the frame's bookkeeping in every cycle (`pins.kind`, the cycle count, `Frame::access`, and `Part::step`'s polled options once an instruction); the MPR lookup in every cycle (`physical`); and a fetch that could read a program byte from a page the board lends, where today every byte is a `Bus::read`. The board's own share is mostly the bus callbacks: about thirty instructions a quiet cycle (the clock, the horizon test, the page's handler, the byte, the taps). The picture is a fifth: the sprite scan and drawing, the background, the capture's lookup, each a few per cent (two attempts at the sprites did not measure: docs/not-kept.md). ## The rule and the gate A change is kept only if the gate stays green and an A/B measurement shows a gain above the A/A noise; it is then one commit with its factor in the message. A rejected one is a line in [docs/not-kept.md](docs/not-kept.md). **The gate**, `scripts/ladder.sh` (about a minute; `--faults` adds the seeded faults): `cargo test`; every lockstep case against the reference traces (`python3 scripts/lockstep.py --reference-cache external/refcache`: 275 of 275; the cache is pcengine-rust's, whose traces are pcengine-python's on all of them, so no Python runs); the save-state round trip and the run-ahead pass on the 18 picture-gate titles (rung 6). Every commit of this fork's history passed it as a tree of its own. `pce-bench` adds a check the gate's lines do not have: its checksum covers the snapshot's bytes at the end of the run, and `scripts/ab.py` refuses to compare two builds whose checksums differ, so **the state is the base board's too**, not only the bus, the picture and the sound. **The state after every cycle is the base board's.** The shortcuts skip work only where it would have found nothing to do; `tests/fork.rs` checks it directly: after every instruction of a program that runs the timer, a VRAM-to-VRAM DMA through the display period and VRAM writes at high CPU speed, a copy of the VDC and of the timer caught up to the clock equals the original; and cycle by cycle with a port device that starts a DMA from its `catch_up`. **Measuring** on a shared machine: `pce-bench` times `run_frame` and `take_audio` by the thread's own CPU time (`/proc/thread-self/schedstat`), and `scripts/ab.py` alternates two builds title by title on one pinned core and compares each side's best of 12 runs (other work only ever slows a run down). The five titles are Blazing Lazers, Bonk's Adventure, R-Type, After Burner II and Street Fighter II', with the picture gate's input (RUN at frame 250, I at 400, right with II from 480); `--extra "--device lockstep-port"` measures the port-device path. An A/A run is within 1.5% overall and 6% a title. A build has its own luck too: without LTO, how the compiler splits the crate into codegen units moved one and the same change from x1.09 to x1.01 between two builds of it, so each change's factor in the table above is between two builds with fat LTO and one codegen unit, and a change below about 3% is not kept. **Profiling**: `perf` is not allowed here, so `pce-profile` (feature `profile`) samples the program counter on SIGPROF and `scripts/profile.py` resolves the samples with `addr2line`, inlined frames included, by crate and file, by function and by line. ## What a host must know Nothing, if it uses `System` (`board_mut()`, `step()`, `run_frame()`, `run_until()`) and `BoardOptions`, as the add-on crates and the pages do. The fork adds three public functions and changes none: `Board::touch`, `vdc::background::row_into`, `vdc::sprites::scan_into`. - **`Board::touch()`** tells the board it was reached from outside a bus access, so it forgets the event horizon and decides its shortcuts again. `System::board_mut`, `step`, `run_frame` and `run_until` call it, and so do `Board::reset`, `hold`, `advance` and `vdc`. The one case a host must call it itself: it holds a bare `Board` (or goes through `system.cpu.bus`), changes a public field between two bus callbacks, and then drives the bus (or `system.cpu.step()`) directly. - **A `RomCard`'s `data` is with the board while it runs** (moved, not copied), when there is no port device and the pixel path is the default: `touch()` puts it back, so a host that looks through `board_mut()` finds it, and changes to it are what the CPU reads next. The mapper cards and any card that is not this crate's own are not lent. - **The taps** (`bus_log`, `bus_detail`) are honoured on every path; the gate runs with them on. - **Save states** are the base board's, byte for byte, for the same run (the bench's checksum); the format and the refusals are unchanged. - **Build with `lto = "fat"` and `codegen-units = 1`** in the depending crate's release profile (this crate's own profile only reaches its own binaries): it is worth a tenth. ## Under wasm `wasm-bench/` is the board behind five plain exports (no wasm-bindgen), built for `wasm32-unknown-unknown` with the rustup 1.93.1 toolchain and run under Node by `scripts/wasm-bench.sh`, which also runs the same five runs natively and compares the checksums. `--base DIR` builds the same bench on another checkout (the fork point). **The PSG's level table is not the same under wasm.** `Psg::new` computes `10f64.powf(-1.5 * u / 20.0)` for 128 attenuations with the platform's `pow`; under `wasm32-unknown-unknown` (Rust's own libm) 9 of the 128 values differ from the native build's (glibc) by one unit in the last place: entries 6, 10, 18, 42, 50, 70, 74, 81 and 109. This is the base board's (it is so at the fork point too) and is not changed here, since it is not speed. What it means today: on the five benchmark runs every mixed sample and every picture is still identical under wasm and natively (the level differences vanish in the rounding to 16 bits), but nothing guarantees that for every game, and **a snapshot or a save state taken under wasm does not have the native build's bytes** (the table is in the PSG's state; a state loads either way, because a loaded state keeps the system's own table). The fix belongs in pcengine-rust (and the reference decides what the values are: Python's `**` is the platform's `pow` too): the 128 values as literals, or a `pow` that is the same everywhere.