mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 17:06:04 +00:00
Closes seven of the cc65/nesdoug parity gaps catalogued in docs/future-work.md in a single pass. All of the new features are gated on marker labels so programs that don't use them produce byte-identical ROM output (every pre-existing committed .nes file round-trips unchanged). Language / runtime additions: - `rand8()` / `rand16()` / `seed_rand(u16)` intrinsics backed by a 16-bit Galois LFSR (~30 bytes of runtime, ~40 cycles per draw). Reset path seeds state to 0xACE1 so the first draw is useful even without explicit seeding. - `p1.button.a.pressed` / `.released` edge-triggered input via a new ReadInputEdge IR op plus an NMI-side prev-frame snapshot into $07E6/$07E7, gated on the `__edge_input_used` marker. - `set_palette_brightness(level)` builtin mapping levels 0..8 to PPU mask emphasis bytes (`$2001`) for neslib-style screen fades. - `mapper: AxROM` (iNES 7) with automatic 32 KB PRG padding so emulators that enforce mapper-7's 32 KB page size boot cleanly. - `mapper: CNROM` (iNES 3) with a reset-time CHR bank 0 select. - `--fceux-labels <prefix>` CLI flag emitting per-bank `.nl` label files and a `.ram.nl` file for FCEUX's debugger. Tests + examples: - Five new example programs with committed .nes ROMs and pixel+audio goldens: prng_demo, edge_input_demo, palette_brightness_demo, axrom_simple, cnrom_simple. - Seven integration tests covering JSR emission, the omitted-when-unused invariant, the NMI prev-input snapshot, the correct mapper numbers for AxROM/CNROM, and negative tests for unknown button names and bad rand8 arity. - `is_intrinsic()` now runs in expression-position Call paths too, so `var x = rand8(1, 2)` errors at compile time instead of silently dropping the extra arguments.
42 lines
1.2 KiB
Text
42 lines
1.2 KiB
Text
// Edge-triggered input demo — the A / B buttons toggle a pair
|
|
// of sprites using `.pressed` / `.released` rather than the
|
|
// level-state `p1.button.a`. Held buttons only fire once per
|
|
// press, which is the canonical menu / one-shot action pattern.
|
|
|
|
game "Edge Input Demo" {
|
|
mapper: NROM
|
|
}
|
|
|
|
var ax: u8 = 64
|
|
var bx: u8 = 120
|
|
var toggle_a: u8 = 0
|
|
var toggle_b: u8 = 0
|
|
|
|
on frame {
|
|
// Each press moves the A-sprite 8 pixels right. Because
|
|
// `.pressed` fires once per press transition, holding the
|
|
// button down does not accelerate movement.
|
|
if p1.button.a.pressed {
|
|
ax += 8
|
|
toggle_a = toggle_a ^ 1
|
|
}
|
|
|
|
// Release moves the B-sprite 4 pixels right on letting go.
|
|
if p1.button.b.released {
|
|
bx += 4
|
|
toggle_b = toggle_b ^ 1
|
|
}
|
|
|
|
// Drive the frame colours off the toggles so the output is
|
|
// observable in the emulator golden.
|
|
draw Ball at: (ax, 80)
|
|
draw Ball at: (bx, 120)
|
|
|
|
// A tiny visual beacon for "toggle_a" / "toggle_b" so the
|
|
// golden captures the state even before the user presses
|
|
// anything.
|
|
if toggle_a == 1 { draw Ball at: (ax, 90) }
|
|
if toggle_b == 1 { draw Ball at: (bx, 130) }
|
|
}
|
|
|
|
start Main
|