Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions IMPLEMENTATION-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,44 @@
- `fuel.py` is my own pass (the kit now has `tools/wasm_fuel.py`; its `--check` accepts the module).
- `SaveMeteors`, `LoadMeteors`, `FatalError` and the Q3 outcomes follow the spec text only; no recording has them.
- The header's DISABLE comment ("drops no meteor") reads differently from spec Q4 (a due shower starts and is then made inactive); the module follows the spec and was equal.

# movement implementation notes

## What was built
`movement/` is a `no_std` Rust cdylib (`src/lib.rs`: the slots, `src/math.rs`: the kit's runtime arithmetic, `src/tables.rs`: generated by `tools/gen_tables.py` from the kit's `bitexact_tables.py`). `build.sh` links at data base 622592 and table base 464 (stack 4096) through `ld-wrap.sh` (which forwards to wind_meteors's one wrapper) and runs the kit's `tools/wasm_fuel.py`. The entry is `blocked`, so size and fuel are `not_judged`. Current module: see the last verdict below.
- Records are read and written in place at the `layout.json` offsets (unaligned loads and stores). A `Ref` field holds a block offset; the code turns it into an address with `g_blockBase` and back.
- `math.rs` ports the kit's integer and double-double code (`sqrt`, `ftol`, `hypot`, `atan2`) and builds the sine and cosine of the rotate-a-point rule with a double-double Taylor series (it stops once the terms are below 1e-45; the 65536-entry table does not fit the window). `mathtest/` replays the kit's `runtime/vectors` against it: every record equal.
- Class words are read from the table at 0x10100000; a target is valid when its class word is one of the five kinds and the record ends within `block_max`.
- `UnitRefSetUnit` and the table import are kept by a `#[used]` function-pointer table (`KEEP`, holding `UnitTarget` and a never-called wrapper of `UnitRefSetUnit`); the link uses `--no-gc-sections`, so no slot runs extra instructions for it.
- The float steps of the flying correction follow spec section 5 literally (f32 roundings, Q8).
- `size` of `Apply` and `ApplyPathEvent` is compared unsigned (spec section 2, merged from spec-movement 03e6ada).
- DESTROY_TARGET zeroes the record before `SimFree` (spec section 7, "A freed target's bytes").

## Verdicts, in order
1. req1: `differs`; 10 recordings `equal`, `orders-air` (tick 4095, call 35, `PathOrder#5.approachRadius`) and `orders-air-repair` (tick 670, call 10, `PathOrder#3.approachRadius`) differ.
2. req2 (every allocated record zeroed by the module): the same two differences.
3. req3 (the freed target's record zeroed before `SimFree`): `equal`, all 12 recordings.
4. req4 (after the first review fixes, 29123 bytes): `equal`, all 12 recordings.
5. req5 (second review, 29375 bytes): `equal`, all 12 recordings.
6. req6 (third review, 29489 bytes): `equal`, all 12 recordings.
Scenarios, digest, view and budgets are `not_judged` throughout (blocked entry).

## Answers to the reviews
First and second review: blocked-cell clamp uses `max().min()`; no extra zeroing of allocated records (`SimAlloc` zeroes); `UnitTarget` guards a null goal; `TargetType` gives 0 outside kinds 2 to 6; non-trapping float-to-int is on and the two remaining casts are range-guarded; the sine table is 512 entries; `build.sh` cleans its temp directory.

Third review:
- 1 (`size` signedness): the spec now says unsigned (merged 03e6ada); the module already compared unsigned (-1 is 4294967295, not below any size).
- 2 (stale target offset after reuse): kept. The spec says a goal keeps the offset it was given until given another and that a target is valid by its class word; the module cannot tell a reused block from the old one, and no recording reuses one. Destroying a target twice is guarded by the zeroed class word.
- 3 (`rotate_point` cost): the series stops once both terms are below 1e-45, far under double-double precision; the kit's rotate vectors are still all equal.
- 4 (keep-alive in `TargetType`): removed; see the `KEEP` table above.
- 5 (`alloc_zeroed`, `s_scaled`, `c_scaled`): removed, the callers use `sim_alloc` and the math functions directly.
- 6 (`build.sh` temp directory): `trap ... EXIT`.
- 7 (duplicate `ld-wrap.sh`): movement's forwards to wind_meteors's.
- 8 (notes): reorganised, one section each.

## Unclear points in the spec or kit
- DESTROY_TARGET: the replayer expects a freed target's bytes to read 0; the spec now says so, pending ozgb/byte-tactics-moddable#1380.
- The kit has no local checker (`wasm_modules.py` is not in it): exports, imports, window and element segment were checked by hand with `wasm-tools`.
- The toolchain (rustc 1.87, LLD 20) has no `--no-stack-first`; the stack falls after the data (inside the window).
- The block's real end is not visible to a module; the bound is `block_max` plus the record size, as the spec says.
- No recording reaches the patrol and packed goals, `ApplyPathEvent`, `HasNetState`, SET_SPEED, SET_HEADING, air maneuver targets, follow offsets or refused events: those paths follow the spec text only.
16 changes: 16 additions & 0 deletions SPEC-QUESTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Spec questions: movement

None of these blocks the module. Question 1 is now answered by the spec (section 7, "A freed target's bytes"), pending ozgb/byte-tactics-moddable#1380.

1. **A freed target's bytes (spec 4, DESTROY_TARGET; 3, "Between ticks").** The spec says only
`UnitRefUnlink`, then `SimFree`. With exactly that (req1, req2) the gate said `differs` in
`orders-air` (tick 4095, call 35, `PathOrder#5.approachRadius`) and `orders-air-repair` (tick 670, call 10,
`PathOrder#3.approachRadius`): the freed block still held the old record, and the replayer expects the bytes
of the freed address to read 0 at the end of the call. Zeroing the target's record before `SimFree` made
all 12 recordings `equal` (req3). Should the spec say that DESTROY_TARGET leaves the freed record zeroed, or is
this an artefact of the replayer that the spec should name?
2. **`SimAlloc` zeroing (spec 3, "Owned").** The spec says `SimAlloc` returns a zeroed record. The module relies on that and does not zero records itself. No verdict separates
the two, since req1 (no zeroing) and req2 differed only in the failure of question 1.
3. **Recordings that never reach a path** (patrol and packed goals, `ApplyPathEvent`, `HasNetState`, SET_SPEED,
SET_HEADING, an air maneuver target, the follow offset, a refused `Apply`): the module follows the spec text
and nothing judges it. Scenarios and the view are `not_judged` for this blocked entry.
2 changes: 2 additions & 0 deletions movement/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.wasm
target/
7 changes: 7 additions & 0 deletions movement/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions movement/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "movement"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[profile.release]
panic = "abort"
opt-level = "s"
lto = true
codegen-units = 1
debug = false
11 changes: 11 additions & 0 deletions movement/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
# Builds module.wasm: link at the manifest's data base 622592 and table base 464, then the kit's fuel pass.
set -e
cd "$(dirname "$0")"
cargo rustc --release --target wasm32-unknown-unknown -- -C linker=$PWD/ld-wrap.sh -C target-cpu=mvp -C target-feature=+sign-ext,+bulk-memory,-nontrapping-fptoint \
-C link-arg=--import-memory -C link-arg=--import-table -C link-arg=--global-base=622592 -C link-arg=--table-base=464 \
-C link-arg=--no-entry -C link-arg=-zstack-size=4096 -C link-arg=--no-gc-sections
T=$(mktemp -d)
trap 'rm -rf "$T"' EXIT
cp target/wasm32-unknown-unknown/release/movement.wasm "$T/raw.wasm"
python3 -I /data/tools/wasm_fuel.py "$T/raw.wasm" -o module.wasm
3 changes: 3 additions & 0 deletions movement/ld-wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# Same linker wrapper as wind_meteors's (one copy, so fixes do not drift).
exec "$(dirname "$0")/../wind_meteors/ld-wrap.sh" "$@"
7 changes: 7 additions & 0 deletions movement/mathtest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions movement/mathtest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "mathtest"
version = "0.1.0"
edition = "2021"
41 changes: 41 additions & 0 deletions movement/mathtest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[path = "../../src/tables.rs"]
mod tables;
#[path = "../../src/math.rs"]
mod math;
use std::fs;

fn r32(b: &[u8], o: &mut usize) -> i32 { let v = i32::from_le_bytes(b[*o..*o+4].try_into().unwrap()); *o += 4; v }
fn rf64(b: &[u8], o: &mut usize) -> f64 { let v = f64::from_le_bytes(b[*o..*o+8].try_into().unwrap()); *o += 8; v }

fn main() {
let d = "/data/runtime/vectors/";
// fixed-size int records
let mut fails = 0; let mut n = 0;
let b = fs::read(d.to_owned() + "scaled_sine.bin").unwrap();
let mut o = 0; while o < b.len() { let a = r32(&b,&mut o); let s = r32(&b,&mut o); let r = r32(&b,&mut o); n+=1; if math::scaled_sine(a,s)!=r {fails+=1;} }
println!("scaled_sine {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "scaled_cosine.bin").unwrap();
let mut o = 0; while o < b.len() { let a = r32(&b,&mut o); let s = r32(&b,&mut o); let r = r32(&b,&mut o); n+=1; if math::scaled_cosine(a,s)!=r {fails+=1;} }
println!("scaled_cosine {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "angle_of_vector.bin").unwrap();
let mut o = 0; while o < b.len() { let x = r32(&b,&mut o); let z = r32(&b,&mut o); let r = r32(&b,&mut o); n+=1; if math::angle_of_vector(x,z)!=r {fails+=1; if fails<5 {println!("A {} {} -> {} want {}",x,z,math::angle_of_vector(x,z),r);} } }
println!("angle {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "rotate_point.bin").unwrap();
let mut o = 0; while o < b.len() { let a = r32(&b,&mut o); let x = r32(&b,&mut o); let z = r32(&b,&mut o); let rx = r32(&b,&mut o); let rz = r32(&b,&mut o); n+=1; let (px,pz)=math::rotate_point(a,x,z); if px!=rx||pz!=rz {fails+=1; if fails<5 {println!("R {} {} {} -> {} {} want {} {}",a,x,z,px,pz,rx,rz);} } }
println!("rotate {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "hypot.bin").unwrap();
let mut o = 0; while o < b.len() { let x = rf64(&b,&mut o); let y = rf64(&b,&mut o); let r = rf64(&b,&mut o); n+=1; let h = math::hypot(x,y); if h.to_bits()!=r.to_bits() {fails+=1; if fails<5 {println!("H {} {} -> {} want {}",x,y,h,r);} } }
println!("hypot {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "sqrt.bin").unwrap();
let mut o = 0; while o < b.len() { let x = rf64(&b,&mut o); let r = rf64(&b,&mut o); n+=1; if math::sqrt(x).to_bits()!=r.to_bits() {fails+=1;} }
println!("sqrt {} fails {}", n, fails);
let (mut n, mut fails) = (0,0);
let b = fs::read(d.to_owned() + "ftol.bin").unwrap();
let mut o = 0; while o < b.len() { let x = rf64(&b,&mut o); let r = r32(&b,&mut o); n+=1; if math::ftol(x)!=r {fails+=1;} }
println!("ftol {} fails {}", n, fails);
}
Loading
Loading