1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 00:45:38 +00:00
nescript/examples/mmc3_per_state_split.ne
Claude 7899714af1
examples: 4 new programs covering MMC3 + other e2e gaps
Four new examples bring total coverage to 18/18 ROMs through
the jsnes smoke test:

- mmc3_per_state_split.ne — two states, each with their own
  `on scanline(N)` handler at a different line (80 vs 160).
  Pressing START transitions between them. Verifies the
  per-state MMC3 IRQ dispatch: the `__ir_mmc3_reload` helper
  CMPs `current_state` on every NMI and writes the right
  latch value to `$C000`/`$C001`, and `__irq_user` runs the
  current state's handler when the counter fires. This is
  the first example that exercises the per-state reload logic
  at runtime, not just at compile-time.

- two_player.ne — exercises `p2.button.*` reads alongside
  the default (P1) `button.*`. Two independently-moveable
  sprites sharing a single frame handler and the runtime OAM
  cursor. The runtime's NMI controller poll already reads
  both `$4016` and `$4017`, but until this example no
  runtime test actually looked at `$08` (the P2 input byte).

- function_chain.ne — five-deep user-function call chain
  (`frame -> compute -> scale -> clamp -> fold -> taper`)
  with parameter passing through ZP `$04-$07` and return
  values through A. Early returns inside nested `if`s,
  handler-local result var, mixed shift + additive transforms.
  Catches any regression in: JSR stack discipline, param slot
  layout, RTS stack unwinding, return-value flow, or the
  analyzer's call-graph / max-depth computation.

- comparisons.ne — one `if` per comparison operator
  (`==`, `!=`, `<`, `<=`, `>`, `>=`) gated on a u8 ramping
  through 0..255. Each `if` drives a pip sprite at a fixed
  column. Exercises every `CmpKind::*` case in the IR
  codegen's `gen_cmp`, catching regressions in branch-opcode
  selection (BEQ/BNE/BCC/BCS) and inverted-branch peephole
  folding.

Smoke test deltas (all 18/18 pass, with per-example floors):

    comparisons           208  (floor 150)
    function_chain        104  (floor 100)
    mmc3_per_state_split  104  (floor 80)
    two_player            104  (floor 100)

`tests/emulator/run_examples.mjs` gets new `EXAMPLE_FLOORS`
entries for each, with notes describing the expected content
so a regression prints a helpful reason.

cargo test (313 unit + 37 integration), cargo fmt --check,
cargo clippy --release -- -D warnings all clean.

https://claude.ai/code/session_014Z5y3Q9krLcAxYpZQJhZ5V
2026-04-12 20:48:31 +00:00

112 lines
3.1 KiB
Text

// MMC3 Per-State Scanline Split — proves the compiler's per-state
// IRQ dispatch and reload logic. Two states each own their own
// `on scanline(N)` handler at a different scanline; pressing START
// transitions between them. Because the MMC3 IRQ latch is
// reloaded from the *current* state's scanline each frame (via
// the `__ir_mmc3_reload` helper), the visible split line moves
// when the state changes.
//
// What this exercises end-to-end:
// - MMC3 scanline IRQ firing at the latched line
// - `__ir_mmc3_reload` walking the dispatch table to pick the
// latch value for the new state after a transition
// - `__irq_user` dispatching to the right per-state handler
// - scroll writes from inside an IRQ handler landing before the
// PPU renders the next visible scanline
//
// Controls:
// START — toggle between Upper-split and Lower-split states
//
// Build: cargo run -- build examples/mmc3_per_state_split.ne
game "MMC3 Split" {
mapper: MMC3
mirroring: horizontal
}
// Scroll values for the two halves of the screen. `frame_scroll`
// drifts every frame so the split is easy to see in motion — the
// top half scrolls right, the bottom scrolls left.
var top_scroll: u8 = 0
var bottom_scroll: u8 = 0
// Tiny debouncer so one press of START doesn't cycle through the
// states multiple times per frame.
var debounce: u8 = 0
// Shared drift counter — primarily for observability (the split
// animation works from `top_scroll` / `bottom_scroll` directly).
var _frame_counter: u8 = 0
state Upper {
on enter {
// When we arrive, reset the scroll so the split is easy
// to see from the first frame onward.
top_scroll = 0
bottom_scroll = 0
}
on frame {
_frame_counter += 1
top_scroll += 1
bottom_scroll -= 1
// Initial scroll for the TOP half. The scanline handler
// below will rewrite scroll midway through the frame.
scroll(top_scroll, 0)
// Draw a marker at the top half and a player sprite in
// the bottom half so the split position is visually
// obvious.
draw Marker at: (40, 40)
draw Player at: (120, 140)
if debounce > 0 {
debounce -= 1
}
if button.start and debounce == 0 {
transition Lower
debounce = 30
}
}
// Split at line 80 — the top 80 rows use `top_scroll`, the
// rest use `bottom_scroll`.
on scanline(80) {
scroll(bottom_scroll, 0)
}
}
state Lower {
on enter {
top_scroll = 0
bottom_scroll = 0
}
on frame {
_frame_counter += 1
top_scroll += 1
bottom_scroll -= 1
scroll(top_scroll, 0)
draw Marker at: (40, 40)
draw Player at: (120, 140)
if debounce > 0 {
debounce -= 1
}
if button.start and debounce == 0 {
transition Upper
debounce = 30
}
}
// Split at line 160 — lower split, so the top 160 rows use
// `top_scroll` and only the last ~80 rows use `bottom_scroll`.
on scanline(160) {
scroll(bottom_scroll, 0)
}
}
start Upper