Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
91ff9c6
Docs: Add the scripts spec
Nov 27, 2026
3d6ba03
Docs: Answer the scripts phase 2 review
Nov 28, 2026
372bb4b
Docs: Answer the scripts phase 1 review
Nov 28, 2026
53e1c8c
Docs: Answer the scripts phase 2 second review
Nov 28, 2026
7060e9d
Docs: Fix the scripts vptr and SetCob wording
Nov 29, 2026
2224de5
Add: scripts module
Nov 29, 2026
9449059
Docs: Add the scripts spec questions
Nov 29, 2026
c3ce676
Add: a replayer of the scripts recordings
Nov 29, 2026
26011f9
Docs: Record the gate refusal in the spec questions
Nov 29, 2026
154bc73
Docs: Write the scripts implementation notes
Nov 29, 2026
9279e11
Docs: Answer the scripts implementer questions
Nov 29, 2026
a12614d
Update: Pin the scripts spec to interfaces-v89
ozgb Nov 29, 2026
aefb925
Fix: List RandomInt in the scripts imports
ozgb Nov 29, 2026
4dce544
Merge branch 'main' into impl/scripts
ozgb Nov 29, 2026
e3a0b97
Update: Merge main, move scripts notes into scripts/
ozgb Nov 29, 2026
1a1bc9e
Merge branch 'spec-scripts' into impl/scripts
ozgb Nov 29, 2026
e3d3451
Update: Follow the v89 scripts spec
ozgb Nov 29, 2026
270c45d
Update: Write the CobScript class word, record gate answers
ozgb Nov 29, 2026
5ba7776
Update: Pin the scripts spec to interfaces-v101
ozgb Nov 30, 2026
e54b8f2
Update: Tighten the scripts SetCob wording
ozgb Nov 30, 2026
83ab746
Update: Answer the scripts phase 1 review
ozgb Nov 30, 2026
9b3d073
Merge branch 'main' into impl/scripts
ozgb Nov 30, 2026
36c5f99
Merge branch 'spec-scripts' into impl/scripts
ozgb Nov 30, 2026
2aa14ce
Update: Pin the scripts spec to interfaces-v102
ozgb Nov 30, 2026
b543584
Update: Read script data by address, ask the verdict
ozgb Nov 30, 2026
b39cfb4
Update: Merge spec-scripts
ozgb Nov 30, 2026
a690493
Docs: Record the equal verdict
ozgb Nov 30, 2026
9cdacff
Update: Answer scripts review round 1
ozgb Nov 30, 2026
99f6958
Merge branch 'spec-scripts' into impl/scripts
ozgb Nov 30, 2026
38638e7
Fix: Answer review, keep RandomInt by pointer
ozgb Nov 30, 2026
f762d66
Update: Answer scripts review round 2
ozgb Nov 30, 2026
f92dba1
Merge branch 'spec-scripts' into impl/scripts
ozgb Nov 30, 2026
47e802a
Fix: pose only the root subtree, guard null root
ozgb Nov 30, 2026
5b1ca63
Update: Say how scripts find the unit of an id
ozgb Nov 30, 2026
3838f38
Update: Merge spec-scripts
ozgb Nov 30, 2026
19ff592
Fix: Treat unit index 0 as no unit
ozgb Nov 30, 2026
a2c2e1d
Update: Pin the scripts spec to interfaces-v103
ozgb Nov 30, 2026
e7aa03c
Update: Merge spec-scripts
ozgb Nov 30, 2026
091af46
Docs: Bring scripts notes up to date
ozgb Nov 30, 2026
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
7 changes: 7 additions & 0 deletions scripts/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 scripts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "scripts"
version = "0.1.0"
edition = "2021"

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

[profile.release]
panic = "abort"
opt-level = "z"
lto = true
codegen-units = 1
debug = false
84 changes: 84 additions & 0 deletions scripts/IMPLEMENTATION-NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# scripts implementation notes

## What was built
`scripts/` is a `no_std` Rust cdylib (`src/lib.rs`, with the bit-exact maths in `src/math.rs`), built by
`scripts/build.sh`:
- `ld-wrap.sh` is the linker wrapper of the wind_meteors module (drops `--stack-first`, `--allow-undefined`
and the `__heap_base`/`__data_end` exports; adds `--no-stack-first` when the linker lists it).
- Link options: data base 589824, table base 432, stack 4096, `--import-memory --import-table`.
- The fuel pass is the kit's `tools/wasm_fuel.py` (rule v1); `--check` accepts the module (29255 bytes).
- `target-cpu=mvp` plus sign-ext and bulk-memory. No saturating float conversion is left in the module
(`trunc64` in `math.rs` truncates a double by its bits).
- All 21 slots of the entry are exported. `UpdateObjectState` implements the update rule on the unit record. The three engine slots are not exported.
- Imports: the eleven of the entry (some unused). `Emit` publishes SET_YARD, AIM_DONE, EXPLODE_PIECE, ATTACH_TO_PIECE and
SCRIPT_VALUE with the sizes of the entry's `publishes` (12, 16, 16, 20, 16).
- No statics but the four data words and constants (the strings, the table anchor, the maths tables).
- Rotation of a point, the angle of a vector, hypot and `bx_ftol` are the kit's runtime ported to Rust
(`runtime/*.c`; the double-double arctangent tables are in `math.rs`). The sine and cosine of a 16-bit
angle at 64 bits are computed (a 120-bit fixed-point Taylor series, rounded to nearest even) rather than
read from the 65536-entry table, which would not fit the 32 KB window. `mathtest/` (a std crate that
includes `math.rs`) checks them against `runtime/vectors` (`rotate_point`, `angle_of_vector`, `hypot`,
`ftol`: no difference) and against the kit's generator for all 65536 angles (no difference).
- A direct-mapped cache of 16 `Trig` entries lives on the stack of one slot call (no static).
- `tools/replay.js` replays the four recordings of `tests/replay/scripts/` against the module with node
(`node tools/replay.js module.wasm RECORDING [--cob=ptr|off] [--smoke]`). It names objects as the format
does, sets the `r` lines, calls the slot, compares imports, `w` and `o` lines. `--smoke` also poses every
unit's model and checks that the posed `offset` of each piece equals the piece-offset rule, and prints
the stack depth reached (about 1.1 KB of the 4 KB).

## Update to spec v101/v102 (this session)
- `SetCob` stores `services.GetCobChecksum(data)`; the checksum code and the relocation guesses are gone
(the table words are addresses). The entry's imports are all kept (`RandomInt`, `CanPlaceUnitFootprint`
stay imported; only `RandomInt` is unused, `CanPlaceUnitFootprint` is called by DROP_UNIT).
- Pose rule: a record is restored when it or any ancestor has `modified` 0 (walk up the parent links).
- The query, wait, unit value 4, SET_YARD and heading rules of the new spec were already as the code had them.
- `tools/replay.js` now puts each script and model in the `load_data` region of a 5136-page memory, answers
`GetCobChecksum` from the recording and checks that a call leaves the load data, the table header and the
statics/pieces alone unless a `w` line says so. Locally 74 of 78 calls are equal; the other 4 are RunScripts calls flagged by my own extra check (a hash of a fixed 64 bytes of statics / 1344 bytes of pieces, not the real extent), which the gate judged `equal`.

## Verdicts, in order
1. req1 (kit interfaces-v102 in the verdict): `differs` on all four recordings, call 0, `RunScripts`,
field `CobScript#1.statics`, value `0 elements`.
2. req2 (the same module with `RunScripts` returning at once): the same verdict on all four, so the cause
is not in what the slot does. Raised as question A in `SPEC-QUESTIONS.md`.

3. Session 2, after merging main and spec-scripts (kit interfaces-v102, reference-traces-v64): req1 `equal` on all four
recordings (module 30143 bytes). The old `CobScript#N.statics` verdicts came from the gate's length bug, now fixed;
question A is resolved.

## Unclear points in the spec or kit
No question is open (`SPEC-QUESTIONS.md`: question A was the gate's length bug, resolved by the session-2 verdict).
Points the spec left unclear and later settled, or that I resolved by reading it:
- `UnitDraw View` has no layout in the kit; `UpdateObjectState` implements the spec's update rule on the unit record.
- TURN reads the rotation as an unsigned 16-bit value (`r16`), as the spec says.
- Unit values 12 and 14 are masked to 16 bits unsigned.
- The kit has no `map` import for the drop of a carried unit, so DROP_UNIT calls `map.CanPlaceUnitFootprint` with the
address of the unit's `def`; `RecordSimEvent` is not used.
- Old-kit claims (checksum code, relocation guesses, `CobScript.table` as an offset) are gone: the kit is interfaces-v103,
tables are linear addresses and `services.GetCobChecksum` gives the checksum.

## Review round 1 (this session)
- `RandomInt` is kept by a never-called function pointer in a static (read volatile), not by a call in `draw`.
- DROP: `CanPlaceUnitFootprint` gets the address of `def` (block base + offset), as spec v102 says.
- EXPLODE_PIECE/effect piece range: spec now says it is not judged; left as is. Pose rule: the spec now says
"equivalently, any ancestor with modified 0 at the start of the pass"; kept. `strend` removed.
- INIT_STATE with a null feature model: feature index outside the contract; left.
- The wind_meteors notes that were mixed into this file are removed.

4. Session 3 (merged main and spec-scripts, review fixes): req1 `equal` on all four recordings (module 30162 bytes).

5. Session 4 (merged main and spec-scripts; review round 2): req1 `equal` on all four recordings (module 30311 bytes).
- Pose rule: only the root's subtree is posed and `pos` is added only for the root itself (new spec); a top-level
sibling is restored but not posed. Piece offset adds the unit's angles for any parentless record.
- Null `root` is guarded in `pose_unit_state` and `update_state`. INIT_STATE with a null feature model is now
outside the contract (spec §4), so it is left. EXPLODE_PIECE out-of-range is not judged (left as dropped).
- Stale notes (UpdateObjectState, imports, TURN signedness) corrected: TURN reads the rotation unsigned (`r16`).

6. Session 5 (merged main and spec-scripts; review round 3): req1 `equal` on all four recordings (module 30273 bytes).
- Spec change: unit index 0 is no unit and `Game.poolCount` is not tested (unit values 9-11, attach, drop). `find_unit`
returns 0 for index 0 and callers treat 0 as absent. This answers review item 5 (poolCount is deliberately not tested).
- Review nits 1-3 (null-root early return in `update_state`, redundant ancestor walks, table anchor) left as they are: a built
state always has a root, the walks only cost fuel, and the anchor keeps the table import for ABI 9.1. Item 4 (stale note) fixed.

7. Session 6 (merged main and spec-scripts; spec pinned to interfaces-v103 / reference-traces-v65, a view region above the load
data that this module does not touch). Notes brought up to date; no source change. Gate: req1 `equal` on all four recordings (module 30273 bytes).
3 changes: 3 additions & 0 deletions scripts/SPEC-QUESTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spec questions for `scripts`

None open. (Question A, every recording differing on `CobScript#1.statics`, was the gate's length bug and is resolved.)
13 changes: 13 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# Builds module.wasm: the cdylib linked at the manifest's data base and table base, then metered
# by the kit's fuel pass (ABI 11, rule v1). Usage: build.sh [FUEL_PASS.py]
set -e
cd "$(dirname "$0")"
PASS=${1:-${WASM_FUEL:-/data/tools/wasm_fuel.py}}
cargo rustc --release --target wasm32-unknown-unknown -- -C linker=$PWD/ld-wrap.sh -C target-cpu=mvp -C target-feature=+sign-ext,+bulk-memory \
-C link-arg=--import-memory -C link-arg=--import-table -C link-arg=--global-base=589824 -C link-arg=--table-base=432 \
-C link-arg=--no-entry -C link-arg=-zstack-size=4096 -C link-arg=--gc-sections
T=$(mktemp -d)
cp target/wasm32-unknown-unknown/release/scripts.wasm "$T/raw.wasm"
python3 "$PASS" "$T/raw.wasm" -o module.wasm
rm -rf "$T"
9 changes: 9 additions & 0 deletions scripts/ld-wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# rustc passes --stack-first and --allow-undefined; the ABI forbids both.
# ABI 3.1 wants --no-stack-first; a linker whose help doesn't list it already puts the stack after the data.
for a in "$@"; do
shift
case "$a" in --stack-first|--allow-undefined|--export=__heap_base|--export=__data_end) ;; *) set -- "$@" "$a";; esac
done
if wasm-ld --help | grep -q -e '--no-stack-first'; then set -- "$@" --no-stack-first; fi
exec wasm-ld "$@"
7 changes: 7 additions & 0 deletions scripts/mathtest/Cargo.lock

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

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

[workspace]

[profile.release]
opt-level = 2
47 changes: 47 additions & 0 deletions scripts/mathtest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[path = "../../src/math.rs"]
#[allow(dead_code)]
mod math;
use std::fs;

fn recs(name: &str, insz: usize, outsz: usize) -> Vec<Vec<u8>> {
let b = fs::read(format!("/data/runtime/vectors/{}.bin", name)).unwrap();
b.chunks(insz + outsz).map(|c| c.to_vec()).collect()
}
fn i(b: &[u8], o: usize) -> i32 { i32::from_le_bytes(b[o..o + 4].try_into().unwrap()) }
fn d(b: &[u8], o: usize) -> f64 { f64::from_bits(u64::from_le_bytes(b[o..o + 8].try_into().unwrap())) }

fn main() {
let mut bad = 0;
for r in recs("rotate_point", 12, 8) {
let mut c = math::TrigCache::new();
let (rx, rz) = math::rotate_point(&mut c, i(&r, 0) as u32 as u16, i(&r, 4), i(&r, 8));
if (rx, rz) != (i(&r, 12), i(&r, 16)) { bad += 1; }
}
println!("rotate_point bad {}", bad);
bad = 0;
for r in recs("angle_of_vector", 8, 4) {
if math::angle_of_vector(i(&r, 0), i(&r, 4)) != i(&r, 8) { bad += 1; }
}
println!("angle bad {}", bad);
bad = 0;
for r in recs("hypot", 16, 8) {
if math::hypot(d(&r, 0), d(&r, 8)).to_bits() != d(&r, 16).to_bits() { bad += 1; }
}
println!("hypot bad {}", bad);
bad = 0;
for r in recs("ftol", 8, 4) {
if math::ftol(d(&r, 0)) != i(&r, 8) { bad += 1; }
}
println!("ftol bad {}", bad);
// sincos table from the kit's generator
if let Ok(t) = fs::read_to_string("/tmp/w/sincos.txt") {
bad = 0;
for (n, l) in t.lines().enumerate() {
let v: Vec<u64> = l.split_whitespace().map(|s| u64::from_str_radix(s, 16).unwrap()).collect();
let tr = math::trig(n as u16);
if n == 0 { continue; } if n==24807 { println!("24807 ok? {:x?} {:x} {:x}", v, tr.cm, tr.ce);}
if (tr.sm, tr.se as u64, tr.cm, tr.ce as u64) != (v[0], v[1], v[2], v[3]) { bad += 1; if bad < 5 { println!("angle {} got {:x} {:x} {:x} {:x} want {:x?}", n, tr.sm, tr.se, tr.cm, tr.ce, v); } }
}
println!("sincos bad {}", bad);
}
}
Loading
Loading