nx_deadair_gate.nx source
↩ module page · 153 lines · 5765 B
1// nx_deadair_gate.nx -- REFEREE for MEDIA-STUDIO R0 (nx_deadair).
2//
3// Frames are 64x48 grayscale, base luma 128. noise_floor=12, active=20 permille.
4//
5// POSITIVE (dead-air): two identical static frames -> activity 0 -> DEAD-AIR.
6// POSITIVE (active): a 16x16 subject block appears -> high activity -> ACTIVE.
7// NEG-CONTROL #1 (noise): per-pixel noise in [-11,+11] (all below the floor).
8// With the floor it MUST read DEAD-AIR. With floor=0 (naive diff) the SAME
9// frame reads ACTIVE -- printed side-by-side to PROVE the floor does real
10// work and the metric is not just raw difference. (metric != raw-diff)
11// NEG-CONTROL #2 (debounce): a single changed pixel MUST read DEAD-AIR, not
12// trip "active" on one hot pixel.
13//
14// Every measured value is PRINTED (no fabricated greens). Evidence ->
15// stdout + knowledge/status/deadair_gate.log. Exit 0 GREEN / 1 RED.
16// Sovereign: syscalls (-> nx_syscalls) + nx_image + nx_deadair only.
17// license_tier: ORIGINAL
18import "syscalls.nx"
19import "nx_image.nx"
20import "nx_deadair.nx"
21
22const W: i64 = 64
23const H: i64 = 48
24const FLOOR: i64 = 12
25const ACTV: i64 = 20
26
27func gp(logfd: i64, s: *u8) -> i64 {
28 var n: i64 = 0
29 while s[n] != (0 as u8) { n = n + 1 }
30 sys_write(1, s, n)
31 if logfd > 0 { sys_write(logfd, s, n) }
32 return 0
33}
34func gn(logfd: i64, v: i64) -> i64 {
35 let bb: *u8 = sys_mmap(28)
36 var m: i64 = v
37 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
38 let t: *u8 = sys_mmap(28)
39 var k: i64 = 0
40 if m == 0 { t[0] = 48 as u8; k = 1 }
41 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
42 var i: i64 = 0
43 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
44 sys_write(1, bb, k)
45 if logfd > 0 { sys_write(logfd, bb, k) }
46 return 0
47}
48
49func fill(img: *Image, v: i64) -> i64 {
50 var y: i64 = 0
51 while y < H {
52 var x: i64 = 0
53 while x < W { nx_image_set(img, x, y, 0, v); x = x + 1 }
54 y = y + 1
55 }
56 return 0
57}
58
59// base 128 + per-pixel signed noise in [-11, +11] (|delta| <= 11 < FLOOR).
60func fill_noise(img: *Image, seed: i64) -> i64 {
61 var s: i64 = seed
62 var y: i64 = 0
63 while y < H {
64 var x: i64 = 0
65 while x < W {
66 s = (s * 1103515245 + 12345) & 0x7fffffff
67 let n: i64 = (s % 23) - 11
68 nx_image_set(img, x, y, 0, 128 + n)
69 x = x + 1
70 }
71 y = y + 1
72 }
73 return 0
74}
75
76// report one case: prints "<label> act=.. class=ACTIVE|DEAD"
77func report(logfd: i64, label: *u8, act: i64) -> i64 {
78 let cls: i64 = nx_deadair_classify(act, ACTV)
79 gp(logfd, label)
80 gp(logfd, " act=\x00" as *u8); gn(logfd, act)
81 gp(logfd, "permille class=\x00" as *u8)
82 if cls == NX_DEADAIR_ACTIVE { gp(logfd, "ACTIVE\x00" as *u8) }
83 if cls == NX_DEADAIR_DEAD { gp(logfd, "DEAD-AIR\x00" as *u8) }
84 gp(logfd, "\n\x00" as *u8)
85 return 0
86}
87
88func main() -> i64 {
89 let logfd: i64 = sys_openat_append("knowledge/status/deadair_gate.log\x00" as *u8, 0x1a4)
90 gp(logfd, "DEADAIR-GATE MEDIA-STUDIO R0 frame=\x00" as *u8)
91 gn(logfd, W); gp(logfd, "x\x00" as *u8); gn(logfd, H)
92 gp(logfd, " floor=\x00" as *u8); gn(logfd, FLOOR)
93 gp(logfd, " active>=\x00" as *u8); gn(logfd, ACTV); gp(logfd, "permille\n\x00" as *u8)
94
95 let base: *Image = nx_image_alloc(W, H, 1)
96 let same: *Image = nx_image_alloc(W, H, 1)
97 let subj: *Image = nx_image_alloc(W, H, 1)
98 let nois: *Image = nx_image_alloc(W, H, 1)
99 let tiny: *Image = nx_image_alloc(W, H, 1)
100 fill(base, 128)
101 fill(same, 128)
102
103 // subject: base 128 + a 16x16 block at (8,8) set to 255
104 fill(subj, 128)
105 var by: i64 = 8
106 while by < 24 {
107 var bx: i64 = 8
108 while bx < 24 { nx_image_set(subj, bx, by, 0, 255); bx = bx + 1 }
109 by = by + 1
110 }
111
112 // noise frame + tiny single-pixel change
113 fill_noise(nois, 305419896)
114 fill(tiny, 128)
115 nx_image_set(tiny, 30, 20, 0, 255)
116
117 // ---- measurements (all printed) ----
118 let a_static: i64 = nx_deadair_activity_permille(base, same, FLOOR)
119 report(logfd, " static \x00" as *u8, a_static)
120
121 let a_subj: i64 = nx_deadair_activity_permille(base, subj, FLOOR)
122 report(logfd, " subject \x00" as *u8, a_subj)
123
124 let a_noise_f: i64 = nx_deadair_activity_permille(base, nois, FLOOR)
125 report(logfd, " noise(floor) \x00" as *u8, a_noise_f)
126
127 let a_noise_0: i64 = nx_deadair_activity_permille(base, nois, 0)
128 report(logfd, " noise(naive) \x00" as *u8, a_noise_0)
129
130 let a_tiny: i64 = nx_deadair_activity_permille(base, tiny, FLOOR)
131 report(logfd, " tiny(1px) \x00" as *u8, a_tiny)
132
133 // ---- verdict ----
134 var ok: i64 = 1
135 if nx_deadair_classify(a_static, ACTV) != NX_DEADAIR_DEAD { ok = 0 } // static = dead
136 if nx_deadair_classify(a_subj, ACTV) != NX_DEADAIR_ACTIVE { ok = 0 } // subject = active
137 if a_subj <= 50 { ok = 0 } // clearly active
138 if nx_deadair_classify(a_noise_f, ACTV) != NX_DEADAIR_DEAD { ok = 0 } // noise floored = dead
139 if nx_deadair_classify(a_noise_0, ACTV) != NX_DEADAIR_ACTIVE { ok = 0 } // naive WOULD call it active (floor matters)
140 if a_subj <= a_noise_f { ok = 0 } // real motion separates from noise
141 if nx_deadair_classify(a_tiny, ACTV) != NX_DEADAIR_DEAD { ok = 0 } // 1 hot pixel debounced
142
143 if ok == 1 {
144 gp(logfd, "DEADAIR-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
145 if logfd > 0 { sys_close(logfd) }
146 sys_exit(0)
147 return 0
148 }
149 gp(logfd, "DEADAIR-GATE result=FAIL verdict=RED\n\x00" as *u8)
150 if logfd > 0 { sys_close(logfd) }
151 sys_exit(1)
152 return 1
153}