nx_pcb_gerber_gate.nx source
↩ module page · 179 lines · 7453 B
1// nx_pcb_gerber_gate.nx -- R1 GATE of the Omniforge PCB/EDA leg (the "chips" near-term path).
2// Builds a small real board (pads + traces), emits a Gerber RS-274X copper layer via nx_pcb_gerber,
3// writes the real .gbr artifact, then VERIFIES by INDEPENDENTLY reconstructing the canonical ASCII
4// for every element from the integer model and asserting EXACT presence in the emitted bytes -- a
5// true integer round-trip proving um -> format-units -> ASCII is exact with zero float drift.
6// Counts D01/D02/D03 to confirm element totals, checks format/unit/end headers, and a NEGATIVE
7// CONTROL: a coordinate off by 1um must be ABSENT (so the round-trip check has teeth = liar-kill).
8// 100% sovereign, integer-only. Mirrors the nx_cad_test idiom (imports nx_syscalls + the organ).
9// license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_pcb_gerber.nx"
12
13func sw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func sn(v: i64) -> i64 {
15 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
16 var m: i64 = v
17 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
18 let d: *u8 = sys_mmap(24); var k: i64 = 0
19 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 let o: *u8 = sys_mmap(24); var i: i64 = 0
21 while i < k { o[i] = d[k - 1 - i]; i = i + 1 }
22 sys_write(1, o, k); return 0
23}
24
25func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
26
27// does hay[0..haylen) contain the NUL-terminated needle?
28func contains(hay: *u8, haylen: i64, needle: *u8) -> i64 {
29 let nl: i64 = slen(needle)
30 if nl == 0 { return 1 }
31 var i: i64 = 0
32 while i + nl <= haylen {
33 var j: i64 = 0
34 var ok: i64 = 1
35 while j < nl {
36 if hay[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 }
37 }
38 if ok == 1 { return 1 }
39 i = i + 1
40 }
41 return 0
42}
43
44// count non-overlapping occurrences of needle in hay[0..haylen)
45func count_occur(hay: *u8, haylen: i64, needle: *u8) -> i64 {
46 let nl: i64 = slen(needle)
47 if nl == 0 { return 0 }
48 var c: i64 = 0
49 var i: i64 = 0
50 while i + nl <= haylen {
51 var j: i64 = 0
52 var ok: i64 = 1
53 while j < nl {
54 if hay[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 }
55 }
56 if ok == 1 { c = c + 1; i = i + nl } else { i = i + 1 }
57 }
58 return c
59}
60
61// build expected "X..Y..D0d*" (no newline, NUL-terminated) into nb using the ORGAN's own formatter
62func expect_xyd(nb: *u8, x_um: i64, y_um: i64, d: i64) -> i64 {
63 let len: *i64 = sys_mmap(8) as *i64
64 len[0] = 0
65 g_xyd(nb, len, x_um, y_um, d) // appends "...*\n"
66 nb[len[0] - 1] = 0 as u8 // replace trailing \n with NUL -> "...*"
67 return 0
68}
69
70func check(name: *u8, cond: i64, tot: *i64) -> i64 {
71 if cond == 1 { sw(" ok " as *u8); tot[0] = tot[0] + 1 }
72 else { sw(" FAIL " as *u8); tot[1] = tot[1] + 1 }
73 sw(name); sw("\n" as *u8)
74 return 0
75}
76
77func main() -> i64 {
78 let tot: *i64 = sys_mmap(16) as *i64
79 tot[0] = 0; tot[1] = 0 // pass, fail
80
81 sw("=== nx_pcb_gerber_gate R1 -- PCB/EDA Gerber RS-274X fab seam (integer-only round-trip) ===\n" as *u8)
82
83 // ---- board model (struct-free parallel arrays); all coordinates in micrometers ----
84 // apertures: 0 = circle pad 600um, 1 = circle trace 250um
85 let ap_kind: *i64 = sys_mmap(64) as *i64
86 let ap_d1: *i64 = sys_mmap(64) as *i64
87 let ap_d2: *i64 = sys_mmap(64) as *i64
88 ap_kind[0] = 0; ap_d1[0] = 600; ap_d2[0] = 0
89 ap_kind[1] = 0; ap_d1[1] = 250; ap_d2[1] = 0
90 let n_ap: i64 = 2
91
92 // 3 pads (aperture 0) forming an L
93 let pad_ap: *i64 = sys_mmap(64) as *i64
94 let pad_x: *i64 = sys_mmap(64) as *i64
95 let pad_y: *i64 = sys_mmap(64) as *i64
96 pad_ap[0] = 0; pad_x[0] = 1000; pad_y[0] = 1000
97 pad_ap[1] = 0; pad_x[1] = 9000; pad_y[1] = 1000
98 pad_ap[2] = 0; pad_x[2] = 9000; pad_y[2] = 6000
99 let n_pad: i64 = 3
100
101 // 2 traces (aperture 1) connecting the pads
102 let tr_ap: *i64 = sys_mmap(64) as *i64
103 let tr_x1: *i64 = sys_mmap(64) as *i64
104 let tr_y1: *i64 = sys_mmap(64) as *i64
105 let tr_x2: *i64 = sys_mmap(64) as *i64
106 let tr_y2: *i64 = sys_mmap(64) as *i64
107 tr_ap[0] = 1; tr_x1[0] = 1000; tr_y1[0] = 1000; tr_x2[0] = 9000; tr_y2[0] = 1000
108 tr_ap[1] = 1; tr_x1[1] = 9000; tr_y1[1] = 1000; tr_x2[1] = 9000; tr_y2[1] = 6000
109 let n_tr: i64 = 2
110
111 // ---- emit ----
112 let cap: i64 = 65536
113 let out: *u8 = sys_mmap(cap)
114 let glen: i64 = gerber_emit(ap_kind, ap_d1, ap_d2, n_ap,
115 tr_ap, tr_x1, tr_y1, tr_x2, tr_y2, n_tr,
116 pad_ap, pad_x, pad_y, n_pad, out)
117 sw(" emitted Gerber bytes=" as *u8); sn(glen); sw("\n" as *u8)
118
119 // write the real artifact (best-effort; knowledge/ exists)
120 let fd: i64 = sys_openat_wr("knowledge/nx_omniforge_demo.gbr" as *u8, 0x1a4)
121 if fd >= 0 { sys_write(fd, out, glen); sys_close(fd); sw(" wrote knowledge/nx_omniforge_demo.gbr\n" as *u8) }
122 else { sw(" (artifact write skipped: open failed)\n" as *u8) }
123
124 // ---- headers ----
125 check("format spec %FSLAX46Y46*% present" as *u8, contains(out, glen, "%FSLAX46Y46*%" as *u8), tot)
126 check("units %MOMM*% present" as *u8, contains(out, glen, "%MOMM*%" as *u8), tot)
127 check("end-of-file M02* present" as *u8, contains(out, glen, "M02*" as *u8), tot)
128 check("aperture D10 circle 0.600000mm present" as *u8, contains(out, glen, "%ADD10C,0.600000*%" as *u8), tot)
129 check("aperture D11 circle 0.250000mm present" as *u8, contains(out, glen, "%ADD11C,0.250000*%" as *u8), tot)
130
131 // ---- integer round-trip: every pad/trace coordinate reconstructed EXACTLY ----
132 let nb: *u8 = sys_mmap(64)
133 var allpad: i64 = 1
134 var i: i64 = 0
135 while i < n_pad {
136 expect_xyd(nb, pad_x[i], pad_y[i], 3)
137 if contains(out, glen, nb) == 0 { allpad = 0 }
138 i = i + 1
139 }
140 check("all pad flashes (D03) round-trip exact" as *u8, allpad, tot)
141
142 var alltr: i64 = 1
143 i = 0
144 while i < n_tr {
145 expect_xyd(nb, tr_x1[i], tr_y1[i], 2)
146 if contains(out, glen, nb) == 0 { alltr = 0 }
147 expect_xyd(nb, tr_x2[i], tr_y2[i], 1)
148 if contains(out, glen, nb) == 0 { alltr = 0 }
149 i = i + 1
150 }
151 check("all trace move+draw (D02/D01) round-trip exact" as *u8, alltr, tot)
152
153 // ---- element counts ----
154 var c03: i64 = count_occur(out, glen, "D03*" as *u8)
155 var ok03: i64 = 0
156 if c03 == n_pad { ok03 = 1 }
157 check("D03 flash count == n_pad (3)" as *u8, ok03, tot)
158
159 var c01: i64 = count_occur(out, glen, "D01*" as *u8)
160 var ok01: i64 = 0
161 if c01 == n_tr { ok01 = 1 }
162 check("D01 draw count == n_tr (2)" as *u8, ok01, tot)
163
164 var c02: i64 = count_occur(out, glen, "D02*" as *u8)
165 var ok02: i64 = 0
166 if c02 == n_tr { ok02 = 1 }
167 check("D02 move count == n_tr (2)" as *u8, ok02, tot)
168
169 // ---- NEGATIVE CONTROL: a coordinate off by 1um must be ABSENT ----
170 expect_xyd(nb, pad_x[0] + 1, pad_y[0], 3) // 1001um instead of 1000um
171 var neg_absent: i64 = 0
172 if contains(out, glen, nb) == 0 { neg_absent = 1 }
173 check("NEG-CONTROL: off-by-1um pad is ABSENT (round-trip has teeth)" as *u8, neg_absent, tot)
174
175 sw("=== VERDICT pass=" as *u8); sn(tot[0]); sw(" fail=" as *u8); sn(tot[1]); sw(" ===\n" as *u8)
176 if tot[1] == 0 { sys_exit(0) }
177 sys_exit(1)
178 return 1
179}