code wiki / _hdl_build / nx_autofix_ens_lib.nx

nx_autofix_ens_lib.nx source

↩ module page · 96 lines · 3985 B

1// nx_autofix_ens_lib.nx -- pure logic for the live ensemble orchestrator (nx_autofix_ensemble). 2// Miss-extraction = the one novel step (the fork-loop + cascade-union are already-gated elfs). Kept 3// as a no-main lib so the organ AND its gate import the SAME code (DRY, rule-15). Dep = nx_syscalls 4// only. license_tier: ORIGINAL No hw writes (Rule 26). 5import "nx_syscalls.nx" 6 7const AEL_NL: i64 = 10 8const AEL_PIPE: i64 = 124 9 10func ael_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 11func ael_find(hay: *u8, hn: i64, needle: *u8, from: i64) -> i64 { 12 let m: i64 = ael_slen(needle) 13 if m == 0 { return 0 - 1 } 14 var i: i64 = from 15 while i + m <= hn { 16 var j: i64 = 0 17 var ok: i64 = 1 18 while j < m { if hay[i+j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } 19 if ok == 1 { return i } 20 i = i + 1 21 } 22 return 0 - 1 23} 24// is `cand=<name> ` present in led on a line that also holds maker=GREEN? 1 green / 0 miss / -1 absent 25func ael_green(led: *u8, ln: i64, name: *u8) -> i64 { 26 let needle: *u8 = sys_mmap(160) 27 var no: i64 = 0 28 let pfx: *u8 = "cand=" as *u8 29 var pi: i64 = 0 30 while pfx[pi] != (0 as u8) { needle[no] = pfx[pi]; no = no + 1; pi = pi + 1 } 31 var ni: i64 = 0 32 while name[ni] != (0 as u8) { needle[no] = name[ni]; no = no + 1; ni = ni + 1 } 33 needle[no] = 32 as u8; no = no + 1 34 needle[no] = 0 as u8 35 let at: i64 = ael_find(led, ln, needle, 0) 36 if at < 0 { return 0 - 1 } 37 var le: i64 = at 38 var go: i64 = 1 39 while go == 1 { if le >= ln { go = 0 } else { if led[le] == (AEL_NL as u8) { go = 0 } else { le = le + 1 } } } 40 let g: i64 = ael_find(led, le, "maker=GREEN" as *u8, at) 41 if g >= at { if g < le { return 1 } } 42 return 0 43} 44// per-stage scratch path "<pre><k><suf>" (F960 N-maker staging). Lives here, not in the organ, so the 45// gate can prove it: a stage-path collision would make maker#3 overwrite maker#2's ledger and the union 46// would silently lose a maker's greens. k is 1..9 (ONE digit) -- the organ's AE_MAXMK cap is what makes 47// that hold; widening the cap needs a real number formatter here, not a wider buffer. 48func ael_stage_path(dst: *u8, pre: *u8, k: i64, suf: *u8) -> i64 { 49 var o: i64 = 0 50 var i: i64 = 0 51 while pre[i] != (0 as u8) { dst[o] = pre[i]; o = o + 1; i = i + 1 } 52 dst[o] = (48 + k) as u8 53 o = o + 1 54 i = 0 55 while suf[i] != (0 as u8) { dst[o] = suf[i]; o = o + 1; i = i + 1 } 56 dst[o] = 0 as u8 57 return o 58} 59// misses-manifest: manifest rows whose cand is NOT green in l1. writes `out`, returns count; byte 60// length via nbox[0]. name = row text up to '|'. 61func ael_extract_misses(l1: *u8, l1n: i64, man: *u8, mann: i64, out: *u8, nbox: *i64) -> i64 { 62 var cnt: i64 = 0 63 var o: i64 = 0 64 let nmbuf: *u8 = sys_mmap(160) 65 var p: i64 = 0 66 while p < mann { 67 var e: i64 = p 68 var f: i64 = 0 69 while f == 0 { if e >= mann { f = 1 } else { if man[e] == (AEL_NL as u8) { f = 1 } else { e = e + 1 } } } 70 var isrow: i64 = 0 71 if e > p { if man[p] != (35 as u8) { isrow = 1 } } 72 if isrow == 1 { 73 var pipe: i64 = 0 - 1 74 var b: i64 = p 75 var bd: i64 = 0 76 while bd == 0 { if b >= e { bd = 1 } else { if man[b] == (AEL_PIPE as u8) { pipe = b; bd = 1 } else { b = b + 1 } } } 77 var nend: i64 = e 78 if pipe > p { nend = pipe } 79 var nl: i64 = 0 80 var x: i64 = p 81 while x < nend { if nl < 150 { nmbuf[nl] = man[x]; nl = nl + 1 } x = x + 1 } 82 nmbuf[nl] = 0 as u8 83 if nl > 0 { 84 if ael_green(l1, l1n, nmbuf) != 1 { 85 var y: i64 = p 86 while y < e { out[o] = man[y]; o = o + 1; y = y + 1 } 87 out[o] = AEL_NL as u8; o = o + 1 88 cnt = cnt + 1 89 } 90 } 91 } 92 p = e + 1 93 } 94 nbox[0] = o 95 return cnt 96}