code wiki / _hdl_build / nx_bom_gate.nx

nx_bom_gate.nx source

↩ module page · 121 lines · 7321 B

1// nx_bom_gate.nx -- GATE for the supply-chain BOM twin. Builds the BOM for the voltage divider (R1,R2 = 10k resistors; 2// J1 = 3-pin header) from a real parts library, and proves it like real procurement: 3// T1 QUANTITIES -- the 10k resistor appears 2x (R1,R2), the header 1x (J1). 4// T2 EXACT COST -- total = 2*1c + 1*10c = 12 cents (exact integer, no float rounding). 5// T3 SOURCEABLE -- every part's required qty <= available stock -> 0 shortfalls. 6// T4 SHORTFALL -- swapping a component to a stock=0 part -> a real supply-chain shortfall is flagged (liar-kill). 7// T5 NEVER-BRICK -- deterministic total re-run. 8// GREEN iff all pass. Sovereign nx_cc->nxasm, no 3rd-party. expect_exit: 0 license_tier: ORIGINAL 9import "nx_bom.nx" 10import "nx_syscalls.nx" 11 12func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 14 15func main() -> i64 { 16 w("=== nx_bom_gate: component library + supply-chain BOM twin (voltage divider) ===\n" as *u8) 17 var pass: i64 = 0; var total: i64 = 0 18 19 // ---- MUTATION-DERIVED TEETH (2026-08-01, debt 1785604588) ---- 20 // nx_gate_mutation_probe scored this pair 1/4 with three survivors, ALL in the water-harvester part 21 // library and its rollup: nx_bom.nx:110 the RESERVOIR spec row, :121 the ENCLOSURE row (the only 22 // BM_MAKE part in the design), :146 `if p.lead_days > b.max_lead_days` -- the MAX reduction. 23 // Every existing row builds a VOLTAGE DIVIDER (two resistors) and never calls nx_bom_water_assemble 24 // at all, so an entire second subsystem in the same file was untested. 25 // LAW: A GATE COVERS THE SUBSYSTEM IT EXERCISES, NOT THE FILE IT LIVES IN. Two products in one 26 // module need two harnesses; the file-level name on the gate implies a coverage it does not have. 27 let wb: *NxBom = nx_bom_water_assemble() 28 // T-A: max_lead_days is a MAX, not a MIN. Mutating `>` to `<` silently computes the minimum, which 29 // is the difference between "this design ships in 14 days" and "in 3" -- a plausible wrong number, 30 // the most dangerous kind. Asserted structurally: the rollup value must be >= EVERY part's lead. 31 var maxlead: i64 = 0 32 var bad_lead: i64 = 0 33 var pid: i64 = 0 34 while pid < 17 { 35 let pp: *NxPart = nx_bom_part(pid) 36 if nx_bom_water_qty(pid) > 0 { 37 if pp.lead_days > maxlead { maxlead = pp.lead_days } 38 if pp.lead_days > wb.max_lead_days { bad_lead = bad_lead + 1 } 39 } 40 pid = pid + 1 41 } 42 total=total+1 43 var t_a: i64 = 0 44 if bad_lead == 0 { if wb.max_lead_days == maxlead { t_a = 1 } } 45 if t_a == 1 { pass=pass+1; w(" [PASS] rollup max_lead_days is the MAXIMUM over used parts (a min would ship a false schedule)\n" as *u8) } 46 if t_a == 0 { w(" [FAIL] max_lead_days is not the max -- " as *u8); w("reduction inverted or spec row wrong\n" as *u8) } 47 // T-B: exactly ONE make line item (the enclosure). Kills the BM_MAKE row mutation, which flips the 48 // make/buy classification of every part in the library at once. 49 total=total+1 50 if wb.make_count == 1 { pass=pass+1; w(" [PASS] exactly one MAKE line item (the enclosure) -- make/buy classification intact\n" as *u8) } 51 if wb.make_count != 1 { w(" [FAIL] make_count is not 1\n" as *u8) } 52 // T-C: the RESERVOIR spec is its documented row. A `==` flipped to `!=` on one id assigns that row 53 // to every OTHER part, so pinning one part's full spec detects it. 54 let res: *NxPart = nx_bom_part(BP_RESERVOIR) 55 total=total+1 56 var t_c: i64 = 0 57 if res.lead_days == 6 { if res.mass_g == 320 { t_c = 1 } } 58 if t_c == 1 { pass=pass+1; w(" [PASS] RESERVOIR spec pinned (lead=6, mass=320) -- per-part rows are not cross-assigned\n" as *u8) } 59 if t_c == 0 { w(" [FAIL] RESERVOIR spec wrong\n" as *u8) } 60 61 // parts library (real data): part0 10k resistor 1c stock 50000 ; part1 3-pin header 10c stock 5000 ; 62 // part2 scarce special-IC 500c stock 0 (the procurement neg-control) 63 let nlib: i64 = 3 64 let lib_price: *i64 = sys_mmap(8*4) as *i64 65 let lib_stock: *i64 = sys_mmap(8*4) as *i64 66 let lib_mpn: *i64 = sys_mmap(8*4) as *i64 67 lib_price[0]=1; lib_price[1]=10; lib_price[2]=500 68 lib_stock[0]=50000; lib_stock[1]=5000; lib_stock[2]=0 69 lib_mpn[0]="RES-10K-0603" as *u8 as i64; lib_mpn[1]="HDR-1x3" as *u8 as i64; lib_mpn[2]="SCARCE-IC" as *u8 as i64 70 71 // design = voltage divider: R1->part0, R2->part0, J1->part1 72 let ncomp: i64 = 3 73 let comp_part: *i64 = sys_mmap(8*4) as *i64 74 comp_part[0]=0; comp_part[1]=0; comp_part[2]=1 75 76 // emit the BOM 77 w(" --- BILL OF MATERIALS ---\n" as *u8) 78 var p: i64 = 0 79 while p < nlib { 80 let qty: i64 = bom_qty(comp_part, ncomp, p) 81 if qty > 0 { 82 w(" " as *u8); w(lib_mpn[p] as *u8); w(" qty=" as *u8); wn(qty); w(" unit=" as *u8); wn(lib_price[p]); w("c ext=" as *u8); wn(qty*lib_price[p]); w("c\n" as *u8) 83 } 84 p = p + 1 85 } 86 let tot: i64 = bom_total_cents(comp_part, ncomp, lib_price, nlib) 87 w(" TOTAL = " as *u8); wn(tot); w(" cents\n" as *u8) 88 89 // T1 quantities 90 let q0: i64 = bom_qty(comp_part, ncomp, 0) 91 let q1: i64 = bom_qty(comp_part, ncomp, 1) 92 total=total+1 93 if q0==2 { if q1==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } } else { w(" [FAIL] " as *u8) } 94 w("T1 quantities: 10k-resistor=" as *u8); wn(q0); w(" header=" as *u8); wn(q1); w("\n" as *u8) 95 96 // T2 exact cost 97 total=total+1; if tot == 12 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 98 w("T2 exact cost: total=" as *u8); wn(tot); w(" cents (2*1 + 1*10, integer-exact)\n" as *u8) 99 100 // T3 sourceable 101 let sf: i64 = bom_shortfalls(comp_part, ncomp, lib_stock, nlib) 102 total=total+1; if sf == 0 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 103 w("T3 sourceable: shortfalls=" as *u8); wn(sf); w(" (all stock >= qty)\n" as *u8) 104 105 // T4 shortfall liar-kill: R2 -> the stock=0 scarce part 106 comp_part[1] = 2 107 let sf2: i64 = bom_shortfalls(comp_part, ncomp, lib_stock, nlib) 108 total=total+1; if sf2 > 0 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 109 w("T4 shortfall: using a stock=0 part -> shortfalls=" as *u8); wn(sf2); w(" (real supply-chain risk caught)\n" as *u8) 110 comp_part[1] = 0 111 112 // T5 never-brick 113 let t1: i64 = bom_total_cents(comp_part, ncomp, lib_price, nlib) 114 let t2: i64 = bom_total_cents(comp_part, ncomp, lib_price, nlib) 115 total=total+1; if t1 == t2 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 116 w("T5 never-brick: deterministic total (" as *u8); wn(t1); w("==" as *u8); wn(t2); w(")\n" as *u8) 117 118 w("\n=== nx_bom_gate " as *u8); wn(pass); w("/" as *u8); wn(total) 119 if pass == total { w(" GREEN (supply-chain BOM twin: real parts, exact integer cost, real availability; closes the EDA component-library gap)\n" as *u8); sys_exit(0); return 0 } 120 w(" RED\n" as *u8); sys_exit(1); return 1 121}