code wiki / _hdl_build / nx_driver_emit.nx

nx_driver_emit.nx source

↩ module page · 74 lines · 4689 B

1// nx_driver_emit.nx -- DRIVER-FROM-SPEC (X-DRV-W1), built on the emitter-of-emitters keystone. 2// A DEVICE SPEC (here: the vendor id to bind) -> this organ EMITS a driver (NishiLang source) that 3// scans the REAL hardware map (knowledge/registry/hwmap.tsv, authored by HWMAP / X-DRV-W0) and 4// counts the devices whose vendor matches the spec = BINDS its devices. The match-loop control flow 5// is synthesized from the spec (the vendor pattern is baked in), and nx_cc compiles it -- so a NEW 6// device class = a NEW spec -> a NEW driver, NO new code (the driver-as-spec-composition thesis). 7// Composes X-DRV-W0 (HWMAP) + X-AUT-006c/e (emitter-of-emitters / compiler-dual). 8// nx_driver_emit <basename> <vendor> -> runtime/_hdl_build/<basename>.nx 9// Honest scope: this is the device PROBE/BIND layer (which devices this driver claims); the bare- 10// metal register protocol (handshake/ring/DMA) is the K-R2 virtio lane. Sovereign. license_tier: ORIGINAL 11import "nx_syscalls.nx" 12const K_MAGIC_65536: i64 = 65536 13 14func dv_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 15func dv_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i } 16func dv_catn(dst: *u8, off: i64, v: i64) -> i64 { 17 var o: i64 = off 18 if v == 0 { dst[o] = 48 as u8; return o + 1 } 19 var m: i64 = v 20 let t: *u8 = sys_mmap(28) 21 var k: i64 = 0 22 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 23 var i: i64 = 0 24 while i < k { dst[o+i] = t[k-1-i]; i = i + 1 } 25 return o + k 26} 27func dv_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 28 29func main(argc: i64, argv: *i64) -> i64 { 30 if argc < 3 { dv_p("usage: nx_driver_emit <basename> <vendor>\n" as *u8); return 2 } 31 let base: *u8 = argv[1] as *u8 32 let vendor: *u8 = argv[2] as *u8 33 let plen: i64 = dv_slen(vendor) + 2 // "\t" + vendor + "\t" 34 35 let path: *u8 = sys_mmap(512) 36 var po: i64 = 0 37 po = dv_cat(path, po, "runtime/_hdl_build/" as *u8); po = dv_cat(path, po, base); po = dv_cat(path, po, ".nx" as *u8); path[po] = 0 as u8 38 39 let buf: *u8 = sys_mmap(K_MAGIC_65536) 40 var o: i64 = 0 41 o = dv_cat(buf, o, "// GENERATED BY nx_driver_emit (driver-from-spec) -- binds devices by vendor from HWMAP; control flow + pattern synthesized from the spec, compiled by nx_cc. license_tier: ORIGINAL\n" as *u8) 42 o = dv_cat(buf, o, "import \"nx_syscalls.nx\"\n" as *u8) 43 o = dv_cat(buf, o, "func main() -> i64 {\n" as *u8) 44 o = dv_cat(buf, o, " let buf: *u8 = sys_mmap(262144)\n" as *u8) 45 o = dv_cat(buf, o, " let fd: i64 = sys_openat_rd(\"knowledge/registry/hwmap.tsv\" as *u8)\n" as *u8) 46 o = dv_cat(buf, o, " var n: i64 = 0\n" as *u8) 47 o = dv_cat(buf, o, " if fd >= 0 {\n" as *u8) 48 o = dv_cat(buf, o, " var r: i64 = 1\n" as *u8) 49 o = dv_cat(buf, o, " while r > 0 { let d: *u8 = ((buf as i64) + n) as *u8; r = sys_read(fd, d, 262144 - n); if r > 0 { n = n + r } }\n" as *u8) 50 o = dv_cat(buf, o, " sys_close(fd)\n" as *u8) 51 o = dv_cat(buf, o, " }\n" as *u8) 52 // baked match pattern "\t<vendor>\t" -- the spec drives WHICH devices bind 53 o = dv_cat(buf, o, " let pat: *u8 = \"\\t" as *u8); o = dv_cat(buf, o, vendor); o = dv_cat(buf, o, "\\t\" as *u8\n" as *u8) 54 o = dv_cat(buf, o, " var cnt: i64 = 0\n" as *u8) 55 o = dv_cat(buf, o, " var i: i64 = 0\n" as *u8) 56 o = dv_cat(buf, o, " while i + " as *u8); o = dv_catn(buf, o, plen); o = dv_cat(buf, o, " <= n {\n" as *u8) 57 o = dv_cat(buf, o, " var k: i64 = 0\n" as *u8) 58 o = dv_cat(buf, o, " var hit: i64 = 1\n" as *u8) 59 o = dv_cat(buf, o, " while k < " as *u8); o = dv_catn(buf, o, plen); o = dv_cat(buf, o, " { if buf[i+k] != pat[k] { hit = 0; k = " as *u8); o = dv_catn(buf, o, plen); o = dv_cat(buf, o, " } else { k = k + 1 } }\n" as *u8) 60 o = dv_cat(buf, o, " if hit == 1 { cnt = cnt + 1 }\n" as *u8) 61 o = dv_cat(buf, o, " i = i + 1\n" as *u8) 62 o = dv_cat(buf, o, " }\n" as *u8) 63 o = dv_cat(buf, o, " let ob: *u8 = sys_mmap(8)\n" as *u8) 64 o = dv_cat(buf, o, " ob[0] = (cnt & 0xff) as u8\n" as *u8) 65 o = dv_cat(buf, o, " sys_write(1, ob, 1)\n" as *u8) 66 o = dv_cat(buf, o, " return 0\n}\n" as *u8) 67 68 let wfd: i64 = sys_openat_wr(path, 420) 69 if wfd < 0 { dv_p("DRIVEREMIT verdict=RED reason=out-unwritable\n" as *u8); return 1 } 70 sys_write(wfd, buf, o) 71 sys_close(wfd) 72 dv_p("DRIVEREMIT name=" as *u8); dv_p(path); dv_p(" vendor=" as *u8); dv_p(vendor); dv_p(" verdict=EMITTED\n" as *u8) 73 return 0 74}