Skip to content

Fix bit manipulation overflow, VCD ID overflow, and corner tile detection#14

Open
hobostay wants to merge 1 commit intoMidstallSoftware:masterfrom
hobostay:fix/simulation-and-packing-bugs
Open

Fix bit manipulation overflow, VCD ID overflow, and corner tile detection#14
hobostay wants to merge 1 commit intoMidstallSoftware:masterfrom
hobostay:fix/simulation-and-packing-bugs

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented Apr 9, 2026

Summary

  • Fix integer overflow in bitstream read/write (crates/aegis-ip/src/tile_bits.rs) — 1 << i defaults to i32 in Rust, which panics in debug mode when i >= 32. Changed to 1u64 << i and 1u8 << for correct types.

  • Fix VCD writer signal ID overflow (crates/aegis-sim/src/lib.rs) — next_id wraps past '~' into invalid characters. Added guard to panic with clear message.

  • Fix incorrect corner tile detection in nextpnr (nextpnr-aegis/aegis.cc) — x == y only works for square grids. Fixed to explicitly check all four corners.

Test plan

  • Verify existing tile_bits_tests.rs still pass
  • Test with tracks=4 for 102+ bit configs
  • Run blinky sim on non-square fabric (e.g. 4x6)

🤖 Generated with Claude Code

…tion

1. Fix integer overflow in bitstream read/write (tile_bits.rs)
   - `1 << i` defaults to i32, panics in debug when i >= 32
   - `write_bits`: changed to `1u64 << i` to match the u64 value type
   - `read_bits`: changed to `1u64 << i` to match the u64 accumulator
   - `clear_bits`: changed to `1u8 <<` for the byte mask type
   - Without this fix, configs wider than 32 bits would panic or corrupt

2. Fix VCD writer signal ID overflow (aegis-sim/src/lib.rs)
   - `next_id` would wrap past '~' (126) into non-printable/control
     characters, producing invalid VCD files
   - Added guard that panics with a clear message when the limit is reached

3. Fix incorrect corner tile detection in nextpnr (aegis.cc)
   - Used `x == y` / `x != y` to skip corner tiles, which only works
     for square grids (W == H)
   - For non-square grids (e.g. 3x5 fabric), this would skip wrong tiles
     (e.g. tile at (5,0) is a corner but x != y) or fail to skip actual
     corners (e.g. tile at (0,6) when W=5)
   - Fixed in all three locations: init_wires, init_bels, init_pips
   - Now correctly checks all four corners regardless of grid aspect ratio

Co-Authored-By: Claude Opus 4.6 <[email protected]>
if id < '~' {
self.next_id = (self.next_id as u8 + 1) as char;
} else {
panic!("VcdWriter: too many signals for single-character VCD IDs");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return an error, please use thiserror to define a new error type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants