code wiki / _hdl_build / nx_assembly.nx
nx_assembly.nx source
↩ module page · 44 lines · 2111 B
1// nx_assembly.nx -- LIB: GENERAL hierarchical PRODUCT digital twin -- the LibrePCB/BOM capability scaled all the way
2// up from a PCB to a whole product (a fridge, a car). A product is an ASSEMBLY TREE: nodes are sub-assemblies or
3// real parts (leaves); each leaf carries real supply-chain data (unit cost cents, mass grams, stock). Rollup computes
4// EXACT integer totals (cost, mass, part count) and supply-chain shortfalls over the whole tree -- the real "what it
5// costs / weighs / can we source it" of the product, hardware-rung-up. Sub-assemblies compose lower twins (e.g. the
6// control board = the nx_bom PCB twin). Data-driven: a new product = a new node table. never-brick #26: pure
7// arithmetic, bounded, deterministic. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9
10// effective count of each node = qty[i] * eff(parent[i]); root (parent < 0) -> eff = qty[root].
11// Nodes MUST be ordered parent-before-child (a real BOM is naturally top-down).
12func asm_eff_count(parent: *i64, qty: *i64, n: i64, eff_out: *i64) -> i64 {
13 var i: i64 = 0
14 while i < n {
15 let p: i64 = parent[i]
16 if p < 0 { eff_out[i] = qty[i] } else { eff_out[i] = qty[i] * eff_out[p] }
17 i = i + 1
18 }
19 return 0
20}
21
22// total over LEAVES of eff * unit (cost cents, or mass grams). is_leaf[i]=1 for parts, 0 for assemblies.
23func asm_total(eff: *i64, unit: *i64, is_leaf: *i64, n: i64) -> i64 {
24 var t: i64 = 0
25 var i: i64 = 0
26 while i < n { if is_leaf[i] == 1 { t = t + eff[i] * unit[i] } i = i + 1 }
27 return t
28}
29
30// total leaf part count = sum of eff over leaves.
31func asm_part_count(eff: *i64, is_leaf: *i64, n: i64) -> i64 {
32 var c: i64 = 0
33 var i: i64 = 0
34 while i < n { if is_leaf[i] == 1 { c = c + eff[i] } i = i + 1 }
35 return c
36}
37
38// supply-chain shortfalls: number of leaf parts whose required count exceeds available stock (0 = fully sourceable).
39func asm_shortfalls(eff: *i64, stock: *i64, is_leaf: *i64, n: i64) -> i64 {
40 var sf: i64 = 0
41 var i: i64 = 0
42 while i < n { if is_leaf[i] == 1 { if eff[i] > stock[i] { sf = sf + 1 } } i = i + 1 }
43 return sf
44}