code wiki / _hdl_build / nx_gamesave_core.nx
nx_gamesave_core.nx source
↩ module page · 41 lines · 1906 B
1// nx_gamesave_core.nx -- the PURE serialize/validate core of the save law (seq1151 design-flaw fix).
2// nx_gamesave.nx (file surface, native) and nx_wasm_craft.nx (save-image surface, wasm) each carried
3// their OWN copy of the same mechanism -- little-endian i64<->bytes and the rolling xor-multiply-mask
4// checksum fold -- because gs_save is file-syscall-coupled and wasm code cannot call it. That parallel
5// re-derivation was the bug. This core is the ONE definition; each format's constants (seed, multiplier,
6// mask, granularity) are PARAMETERS, so both existing on-disk formats stay byte-identical while the
7// mechanism has a single home.
8// PURE BY CONSTRUCTION: imports NOTHING, allocates NOTHING, touches no syscall -> wasm-dialect-clean.
9// license_tier: ORIGINAL expect_exit: 0
10
11// little-endian i64 -> 8 bytes at b[off..off+7]
12func gsc_put64(b: *u8, off: i64, v: i64) -> i64 {
13 var i: i64 = 0
14 while i < 8 { b[off+i] = ((v >> (i*8)) & 255) as u8; i = i + 1 }
15 return 0
16}
17// 8 little-endian bytes at b[off..off+7] -> i64
18func gsc_get64(b: *u8, off: i64) -> i64 {
19 var v: i64 = 0
20 var i: i64 = 7
21 while i >= 0 { v = (v*256) + (b[off+i] as i64); i = i - 1 }
22 return v
23}
24// ONE fold step: h' = ((h ^ v) * mult) & mask -- the single definition of the rolling-checksum shape.
25func gsc_fold(h: i64, v: i64, mult: i64, mask: i64) -> i64 {
26 return ((h ^ v) * mult) & mask
27}
28// fold n bytes starting at b (h0 = seed or running value)
29func gsc_fold_bytes(h0: i64, b: *u8, n: i64, mult: i64, mask: i64) -> i64 {
30 var h: i64 = h0
31 var i: i64 = 0
32 while i < n { h = ((h ^ (b[i] as i64)) * mult) & mask; i = i + 1 }
33 return h
34}
35// fold i64 words w[from..to)
36func gsc_fold_words(h0: i64, w: *i64, from: i64, to: i64, mult: i64, mask: i64) -> i64 {
37 var h: i64 = h0
38 var i: i64 = from
39 while i < to { h = ((h ^ w[i]) * mult) & mask; i = i + 1 }
40 return h
41}