1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 08:55:38 +00:00
nescript/examples/two_player.ne

75 lines
2.1 KiB
Text
Raw Normal View History

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
// Two Player — proves the compiler can read both NES controllers.
//
// Player 1 input goes through `button.*` (the implicit default,
// equivalent to `p1.button.*`); player 2 input goes through
// `p2.button.*`. The language guide mentions both; until this
// example, no runtime test actually exercised player 2.
//
// What this exercises end-to-end:
// - `p2.button.up/down/left/right/a` reads from the player-2
// ZP slot ($08) rather than player 1 ($01)
// - The runtime's NMI controller poll reads both $4016 and
// $4017 per frame and shifts the bits into $01 and $08
// - Two independently-controlled sprites sharing a frame
// handler's OAM cursor
//
// Controls:
// D-pad, A/B — player 1 moves red sprite
// p2.D-pad, — player 2 moves blue sprite
// p2.A/B — p2 shoots "bullets" (visual marker)
//
// Build: cargo run -- build examples/two_player.ne
game "Two Player" {
mapper: NROM
}
var p1x: u8 = 64
var p1y: u8 = 112
var p2x: u8 = 192
var p2y: u8 = 112
// A simple "shot" indicator for each player — when any button is
// pressed this frame, we light up a pixel near the player.
var p1_shot: u8 = 0
var p2_shot: u8 = 0
on frame {
// Player 1 — uses the implicit prefix.
if button.left { p1x -= 1 }
if button.right { p1x += 1 }
if button.up { p1y -= 1 }
if button.down { p1y += 1 }
if button.a or button.b {
p1_shot = 1
} else {
p1_shot = 0
}
// Player 2 — explicit `p2.` prefix.
if p2.button.left { p2x -= 1 }
if p2.button.right { p2x += 1 }
if p2.button.up { p2y -= 1 }
if p2.button.down { p2y += 1 }
if p2.button.a or p2.button.b {
p2_shot = 1
} else {
p2_shot = 0
}
// Draw each player, and a shot indicator above their head
// whenever they're holding a face button. Using separate
// `draw` statements so each gets its own OAM slot via the
// runtime cursor.
draw Player1 at: (p1x, p1y)
draw Player2 at: (p2x, p2y)
if p1_shot == 1 {
draw Shot at: (p1x, p1y - 8)
}
if p2_shot == 1 {
draw Shot at: (p2x, p2y - 8)
}
}
start Main