code wiki / _hdl_build / nx_assembly_gate.nx
nx_assembly_gate.nx source
↩ module page · 95 lines · 6239 B
1// nx_assembly_gate.nx -- GATE: a whole-PRODUCT digital twin -- a REFRIGERATOR, modeled as an assembly tree from the
2// real supply chain, with the nx_bom PCB control board composed in as a subsystem.
3// T1 EFF COUNTS -- the assembly multiplies correctly (2 doors -> effective count 2).
4// T2 ROLLED COST -- exact total bill of materials = 14005 cents ($140.05), summed over the whole tree.
5// T3 ROLLED MASS -- exact total mass = 46950 g (46.95 kg).
6// T4 PART COUNT -- 9 real parts.
7// T5 SOURCEABLE -- all parts in stock -> 0 shortfalls; dropping door stock to 1 (need 2) -> a real shortfall (liar-kill).
8// T6 COMPOSE PCB -- the control board cost = the nx_bom PCB twin (the PCB is a SUBSYSTEM of the fridge).
9// GREEN iff all. Sovereign nx_cc->nxasm. The same engine scales to a car (a bigger tree). expect_exit: 0 license_tier: ORIGINAL
10import "nx_assembly.nx"
11import "nx_bom.nx"
12import "nx_syscalls.nx"
13
14func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func 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 }
16
17func main() -> i64 {
18 w("=== nx_assembly_gate: whole-product DIGITAL TWIN -- a REFRIGERATOR from the real supply chain ===\n" as *u8)
19 var pass: i64 = 0; var total: i64 = 0
20
21 // control board cost = the nx_bom PCB twin (MCU + 2 resistors + 1 cap)
22 let cb_price: *i64=sys_mmap(8*4) as *i64; cb_price[0]=200; cb_price[1]=1; cb_price[2]=3
23 let cb_comp: *i64=sys_mmap(8*8) as *i64; cb_comp[0]=0; cb_comp[1]=1; cb_comp[2]=1; cb_comp[3]=2
24 let cb_cost: i64 = bom_total_cents(cb_comp, 4, cb_price, 3) // 200 + 1 + 1 + 3 = 205
25
26 let n: i64 = 11
27 let nm: *i64=sys_mmap(8*16) as *i64
28 let parent: *i64=sys_mmap(8*16) as *i64
29 let qty: *i64=sys_mmap(8*16) as *i64
30 let leaf: *i64=sys_mmap(8*16) as *i64
31 let cost: *i64=sys_mmap(8*16) as *i64
32 let mass: *i64=sys_mmap(8*16) as *i64
33 let stock: *i64=sys_mmap(8*16) as *i64
34
35 nm[0]="Refrigerator" as *u8 as i64; parent[0]=0-1; qty[0]=1; leaf[0]=0; cost[0]=0; mass[0]=0; stock[0]=999999
36 nm[1]="RefrigerationSystem" as *u8 as i64; parent[1]=0; qty[1]=1; leaf[1]=0; cost[1]=0; mass[1]=0; stock[1]=999999
37 nm[2]="Compressor" as *u8 as i64; parent[2]=1; qty[2]=1; leaf[2]=1; cost[2]=4500; mass[2]=8000; stock[2]=100
38 nm[3]="CondenserCoil" as *u8 as i64; parent[3]=1; qty[3]=1; leaf[3]=1; cost[3]=1200; mass[3]=1500; stock[3]=100
39 nm[4]="Evaporator" as *u8 as i64; parent[4]=1; qty[4]=1; leaf[4]=1; cost[4]=1000; mass[4]=1200; stock[4]=100
40 nm[5]="Refrigerant-R600a" as *u8 as i64; parent[5]=1; qty[5]=1; leaf[5]=1; cost[5]=300; mass[5]=50; stock[5]=100
41 nm[6]="Cabinet" as *u8 as i64; parent[6]=0; qty[6]=1; leaf[6]=0; cost[6]=0; mass[6]=0; stock[6]=999999
42 nm[7]="SteelShell" as *u8 as i64; parent[7]=6; qty[7]=1; leaf[7]=1; cost[7]=3000; mass[7]=25000; stock[7]=100
43 nm[8]="InsulationFoam" as *u8 as i64; parent[8]=6; qty[8]=1; leaf[8]=1; cost[8]=800; mass[8]=3000; stock[8]=100
44 nm[9]="Door" as *u8 as i64; parent[9]=6; qty[9]=2; leaf[9]=1; cost[9]=1500; mass[9]=4000; stock[9]=100
45 nm[10]="ControlBoard(PCB)" as *u8 as i64; parent[10]=0; qty[10]=1; leaf[10]=1; cost[10]=cb_cost; mass[10]=200; stock[10]=100
46
47 let eff: *i64=sys_mmap(8*16) as *i64
48 asm_eff_count(parent, qty, n, eff)
49
50 // emit the BOM tree
51 w(" --- PRODUCT BOM (effective qty x unit cost) ---\n" as *u8)
52 var i: i64 = 0
53 while i < n {
54 if leaf[i] == 1 {
55 w(" " as *u8); w(nm[i] as *u8); w(" x" as *u8); wn(eff[i]); w(" @" as *u8); wn(cost[i]); w("c = " as *u8); wn(eff[i]*cost[i]); w("c\n" as *u8)
56 }
57 i = i + 1
58 }
59 let tcost: i64 = asm_total(eff, cost, leaf, n)
60 let tmass: i64 = asm_total(eff, mass, leaf, n)
61 let tparts: i64 = asm_part_count(eff, leaf, n)
62 w(" TOTAL cost=" as *u8); wn(tcost); w("c mass=" as *u8); wn(tmass); w("g parts=" as *u8); wn(tparts); w("\n" as *u8)
63
64 // T1 eff counts
65 total=total+1; if eff[9]==2 { if eff[2]==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } } else { w(" [FAIL] " as *u8) }
66 w("T1 eff counts: door=" as *u8); wn(eff[9]); w(" compressor=" as *u8); wn(eff[2]); w("\n" as *u8)
67
68 // T2 rolled cost
69 total=total+1; if tcost==14005 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
70 w("T2 rolled cost: " as *u8); wn(tcost); w(" cents ($140.05, exact tree sum)\n" as *u8)
71
72 // T3 rolled mass
73 total=total+1; if tmass==46950 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
74 w("T3 rolled mass: " as *u8); wn(tmass); w(" g (46.95 kg)\n" as *u8)
75
76 // T4 part count
77 total=total+1; if tparts==9 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
78 w("T4 part count: " as *u8); wn(tparts); w(" real parts\n" as *u8)
79
80 // T5 sourceable + liar-kill
81 let sf0: i64 = asm_shortfalls(eff, stock, leaf, n)
82 stock[9] = 1
83 let sf1: i64 = asm_shortfalls(eff, stock, leaf, n)
84 stock[9] = 100
85 total=total+1; if sf0==0 { if sf1>0 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } } else { w(" [FAIL] " as *u8) }
86 w("T5 sourceable: shortfalls=" as *u8); wn(sf0); w(" ; with door stock=1 (need 2) -> " as *u8); wn(sf1); w(" (real supply risk)\n" as *u8)
87
88 // T6 compose the PCB twin
89 total=total+1; if cb_cost==205 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
90 w("T6 compose PCB: control board cost=" as *u8); wn(cb_cost); w("c from the nx_bom PCB twin (a subsystem of the fridge)\n" as *u8)
91
92 w("\n=== nx_assembly_gate " as *u8); wn(pass); w("/" as *u8); wn(total)
93 if pass == total { w(" GREEN (whole-product digital twin: hierarchical real supply-chain rollup, exact cost/mass/parts, composes the PCB twin; scales to a car)\n" as *u8); sys_exit(0); return 0 }
94 w(" RED\n" as *u8); sys_exit(1); return 1
95}