mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 17:06:04 +00:00
The IrLowerer's wide_hi map records "this u8 temp's high byte lives at this other temp" pairs whenever a 16-bit value is produced. Both lower_function and lower_handler reset next_temp to 0 at the start of each function, but neither cleared wide_hi — so stale (low_id -> high_id) entries from earlier functions leaked into subsequent ones. When a fresh function reused those temp IDs for unrelated u8 expressions, is_wide() returned spurious true and widen() handed back stale (lo, hi) pairs whose hi happened to coincide with the *next* temp ID fresh_temp() was about to allocate. The result was 16-bit IR ops (CmpEq16 in particular) where the destination temp aliased one of the source operand high bytes — for War this made `match phase` arms past P_WIN_B impossible to enter and the game would freeze with both face-up cards on the table forever. Fix: clear wide_hi alongside the next_temp reset in both lower_function and lower_handler. Adds a regression test (ir::tests::wide_hi_does_not_leak_between_functions) that constructs a function whose body has no u16 ops but follows a function that does, and asserts no CmpEq16 op aliases its dest with an operand high byte. Also: - Convert the war Playing state's phase machine from an if-chain to a `match`, which is what tripped this bug to the surface (it was lurking in earlier ROMs too but their layouts never produced the dest/source collision shape). - Refactor begin_draw_a/b to set fly_card / fly_face_up via globals before calling arm_fly, since arm_fly only takes 4 params (the v0.1 ABI limit, now diagnosed by E0506). - Hoist the P_RESOLVE comparison result to the global pf_result to dodge the param-clobbering issue documented in examples/war/COMPILER_BUGS.md §2. - Document the bug as item #6 in COMPILER_BUGS.md with a minimal repro and reproducer-test pointer. - Refresh the war golden + audio hash to match the new ROM. https://claude.ai/code/session_0143dTgh3UeRrtfHgQwzcv5z |
||
|---|---|---|
| .. | ||
| war | ||
| arrays_and_functions.ne | ||
| arrays_and_functions.nes | ||
| audio_demo.ne | ||
| audio_demo.nes | ||
| auto_chr_background.ne | ||
| auto_chr_background.nes | ||
| auto_chr_bg.png | ||
| bitwise_ops.ne | ||
| bitwise_ops.nes | ||
| bouncing_ball.ne | ||
| bouncing_ball.nes | ||
| coin_cavern.ne | ||
| coin_cavern.nes | ||
| comparisons.ne | ||
| comparisons.nes | ||
| friendly_assets.ne | ||
| friendly_assets.nes | ||
| function_chain.ne | ||
| function_chain.nes | ||
| hello_sprite.ne | ||
| hello_sprite.nes | ||
| inline_asm_demo.ne | ||
| inline_asm_demo.nes | ||
| logic_ops.ne | ||
| logic_ops.nes | ||
| loop_break_continue.ne | ||
| loop_break_continue.nes | ||
| match_demo.ne | ||
| match_demo.nes | ||
| metasprite_demo.ne | ||
| metasprite_demo.nes | ||
| mmc1_banked.ne | ||
| mmc1_banked.nes | ||
| mmc3_per_state_split.ne | ||
| mmc3_per_state_split.nes | ||
| nested_structs.ne | ||
| nested_structs.nes | ||
| noise_triangle_sfx.ne | ||
| noise_triangle_sfx.nes | ||
| palette_and_background.ne | ||
| palette_and_background.nes | ||
| platformer.ne | ||
| platformer.nes | ||
| README.md | ||
| scanline_split.ne | ||
| scanline_split.nes | ||
| sfx_pitch_envelope.ne | ||
| sfx_pitch_envelope.nes | ||
| sprites_and_palettes.ne | ||
| sprites_and_palettes.nes | ||
| state_machine.ne | ||
| state_machine.nes | ||
| structs_enums_for.ne | ||
| structs_enums_for.nes | ||
| two_player.ne | ||
| two_player.nes | ||
| uxrom_banked.ne | ||
| uxrom_banked.nes | ||
| uxrom_banked_to_banked.ne | ||
| uxrom_banked_to_banked.nes | ||
| uxrom_user_banked.ne | ||
| uxrom_user_banked.nes | ||
| war.ne | ||
| war.nes | ||
NEScript Examples
Quick Start
# Build the compiler
cargo build --release
# Compile all examples
for f in examples/*.ne; do cargo run -- build "$f"; done
# Or compile one
cargo run -- build examples/hello_sprite.ne
Open any .nes file in an NES emulator (Mesen, FCEUX, etc.)
Examples
| File | Features | Description |
|---|---|---|
hello_sprite.ne |
input, draw | Move a sprite with the d-pad |
bouncing_ball.ne |
if/else, variables | Auto-bouncing sprite with edge detection |
coin_cavern.ne |
states, functions, constants | 3-state game with gravity and coin collection |
arrays_and_functions.ne |
arrays, functions, while | Enemy array with collision detection |
state_machine.ne |
on enter/exit, transitions | Multi-state flow with timers |
sprites_and_palettes.ne |
sprites, scroll, cast | Inline CHR data, PPU scroll writes, type casting |
mmc1_banked.ne |
MMC1, banks, multiply | Banked mapper with software multiply |
uxrom_user_banked.ne |
UxROM, bank Foo { fun ... }, cross-bank trampoline |
First example to put real user code inside a switchable bank. The animation step lives in bank Extras and is invoked from the fixed-bank state handler via a generated __tramp_step_animation stub that selects bank 0, JSRs the body, then restores the fixed bank before returning. |
uxrom_banked_to_banked.ne |
UxROM, banked → banked cross-bank call | Two bank Foo { fun ... } blocks: step lives in bank Logic and calls clamp in bank Helpers. The trampoline uses ZP_BANK_CURRENT + PHA/PLA to save and restore the caller's bank, so the same per-callee stub works whether the caller is in the fixed bank or another switchable bank. |
palette_and_background.ne |
palette, background, set_palette, load_background | Reset-time initial load plus vblank-safe runtime swaps |
auto_chr_background.ne |
background @nametable(...) with auto-CHR |
First example to use the @nametable("file.png") shortcut without supplying any matching CHR data. The resolver dedupes the PNG's 8×8 cells, encodes them via the same brightness-bucketing the sprite CHR encoder uses, and slots them into CHR ROM at the next free tile slot. The committed auto_chr_bg.png is a 256×240 grayscale gradient that exercises ~50 unique tiles. |
friendly_assets.ne |
named colours, grouped palette, pixel art, tilemap+legend, palette_map, scalar sfx pitch, note-name music | Exercises every "friendlier" asset syntax at once — the palette uses bg0..sp3 + a shared universal:, the sprite is authored as ASCII pixel art, the background uses a legend { ... } + map: tilemap with a palette_map: for attributes, the sfx uses a scalar pitch: + envelope: alias, and the music uses note names (C4, E4 40, rest 10) with a tempo: default. |
noise_triangle_sfx.ne |
channel: noise, channel: triangle on sfx blocks |
Demonstrates the noise and triangle sfx channels. Declares one noise burst and one triangle bass note, plays each on a timer so the emulator harness captures both the pixel output and the APU state. |
sfx_pitch_envelope.ne |
varying-pitch pulse SFX | A 16-frame frequency sweep written as a per-frame pitch: array on a Pulse-1 sfx. The compiler emits a separate __sfx_pitch_<name> blob and gates the audio tick's pitch update path on the __sfx_pitch_used marker, so programs that stick to the scalar pitch: form still get byte-identical ROM output. |
metasprite_demo.ne |
declarative multi-tile sprites | A 16×16 hero sprite split into a metasprite Hero { sprite: Hero16, dx: [...], dy: [...], frame: [...] } declaration. draw Hero at: (px, py) then expands to one DrawSprite op per tile in the IR lowering, each with its dx/dy added to the user's anchor point and the frame offset by the underlying sprite's base tile. The codegen needs no metasprite-specific support — it sees N regular draws and the OAM cursor allocator handles the slots. |
nested_structs.ne |
nested struct fields, array struct fields, chained literals | Two Hero instances each carry a Vec2 position and a u8[4] inventory. Exercises hero.pos.x chained access, hero.inv[i] array-field access, and chained struct-literal initializers (Hero { pos: Vec2 { x: ..., y: ... }, inv: [...] }). |
platformer.ne |
every subsystem | End-to-end side-scrolling demo: custom CHR tileset, full 32×30 nametable with per-region attribute palettes, 2×2 metasprite hero with gravity/jump physics, wrap-around horizontal scrolling, stomp-or-die enemy collisions with a live stomp-count HUD, coin pickups, user-declared SFX + music, and a Title → Playing → GameOver state machine with a proximity-based autopilot so the headless harness cycles through stomp, stomp, die, and retry inside six seconds. Regenerate the tile art with cargo run --bin gen_platformer_tiles. |
Emulator Controls
| NES Button | Typical Key |
|---|---|
| D-pad | Arrow keys |
| A | Z |
| B | X |
| Start | Enter |
| Select | Right Shift |
About Sprites
Sprite names in draw Player at: (x, y) are parsed and recorded in the AST.
You can define sprites with inline CHR tile data:
sprite Player {
chr: [0x3C, 0x42, 0x81, 0x81, 0x81, 0x81, 0x42, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}
If no matching sprite declaration exists, the draw uses the built-in default
tile (a smiley face). See sprites_and_palettes.ne for a full example.
Compiler Commands
# Compile to ROM
cargo run -- build game.ne
# Custom output path
cargo run -- build game.ne --output my_game.nes
# Type-check only
cargo run -- check game.ne
# View generated 6502 assembly
cargo run -- build game.ne --asm-dump
# Debug mode
cargo run -- build game.ne --debug