code wiki / _hdl_build / nx_dxbc_gate.nx
nx_dxbc_gate.nx source
↩ module page · 96 lines · 6332 B
1// nx_dxbc_gate.nx -- D1 proof: parse a byte-exact, SPEC-FAITHFUL DXBC container + decode its SM5 shader tokens.
2// The fixture is hand-built to the documented DXBC layout (magic/checksum/offset-table/SHEX-chunk) with a real token
3// stream (dcl_temps, mov, mad, dp3, sample, ret at correct opcode+length encodings). Proves the container walk +
4// chunk-find + instruction classification. Neg-control: non-DXBC bytes are rejected. HONEST: fixture is spec-built,
5// not a shipped game's shader (D1b = feed a real .dxbc). license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7import "nx_dxbc.nx"
8
9func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
11func w32(buf: *u8, off: i64, v: i64) -> i64 {
12 buf[off] = (v & 255) as u8
13 buf[off+1] = ((v >> 8) & 255) as u8
14 buf[off+2] = ((v >> 16) & 255) as u8
15 buf[off+3] = ((v >> 24) & 255) as u8
16 return 0
17}
18func tok(op: i64, len: i64) -> i64 { return op | (len << 24) }
19
20func main() -> i64 {
21 hw("=== nx_dxbc_gate -- D1: decode a real DXBC shader-bytecode container ===\n" as *u8)
22 var fails: i64 = 0
23
24 // ---- build a spec-faithful DXBC blob (mmap zero-fills; operand dwords stay 0) ----
25 let buf: *u8 = sys_mmap(512)
26 w32(buf, 0, dx_fourcc(68, 88, 66, 67)) // "DXBC" magic
27 // bytes 4..19 = 16B checksum (left 0). byte 20 = u32 1.
28 w32(buf, 20, 1)
29 w32(buf, 24, 184) // total_size
30 w32(buf, 28, 1) // chunk_count = 1
31 w32(buf, 32, 36) // offset[0] = 36 (after the 1-entry offset table)
32 // ---- SHEX chunk at 36 ----
33 w32(buf, 36, dx_fourcc(83, 72, 69, 88)) // "SHEX"
34 w32(buf, 40, 140) // chunk payload size
35 w32(buf, 44, 65616) // version dword = vs_5_0 : (1<<16)|(5<<4)|0 -> program-type 1
36 w32(buf, 48, 35) // token-stream length in dwords (incl version+length)
37 w32(buf, 52, tok(104, 2)); w32(buf, 56, 1) // dcl_temps (opcode 104, 2 dwords) + temp-count operand
38 w32(buf, 60, tok(54, 5)) // mov (54, 5 dwords: +4 operand dwords @64..76, all 0)
39 w32(buf, 80, tok(50, 9)) // mad (50, 9 dwords)
40 w32(buf, 116, tok(15, 7)) // dp3 (15, 7 dwords)
41 w32(buf, 144, tok(69, 9)) // sample (69, 9 dwords)
42 w32(buf, 180, tok(62, 1)) // ret (62, 1 dword)
43 let n: i64 = 184
44
45 // T1 magic
46 var t1: i64 = 0
47 if dxbc_is(buf, n) == 1 { t1 = 1 }
48 if t1 == 1 { hw("T1 PASS DXBC magic recognized\n" as *u8) } else { fails=fails+1; hw("T1 FAIL magic\n" as *u8) }
49
50 // T2 container header
51 var t2: i64 = 0
52 if dxbc_chunk_count(buf) == 1 { if dxbc_total_size(buf) == 184 { t2 = 1 } }
53 if t2 == 1 { hw("T2 PASS container header: chunk_count=1, total_size=184\n" as *u8) } else { fails=fails+1; hw("T2 FAIL cc="); pn(dxbc_chunk_count(buf)); hw(" ts="); pn(dxbc_total_size(buf)); hw("\n" as *u8) }
54
55 // T3 find SHEX + program type
56 let payoff: i64 = dxbc_find(buf, n, dx_fourcc(83, 72, 69, 88))
57 let ptype: i64 = dxbc_program_type(dx_u32(buf, payoff))
58 hw(" SHEX payload @"); pn(payoff); hw(" program_type="); pn(ptype); hw(" (1=vertex)\n" as *u8)
59 var t3: i64 = 0
60 if payoff == 44 { if ptype == 1 { t3 = 1 } }
61 if t3 == 1 { hw("T3 PASS found SHEX chunk; program type = VERTEX shader (vs_5_0)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL payoff="); pn(payoff); hw(" ptype="); pn(ptype); hw("\n" as *u8) }
62
63 // T4 decode the token stream + classify instructions
64 let cnt: *i64 = sys_mmap(64) as *i64
65 dxbc_decode_shader(buf, payoff, n, cnt)
66 hw(" decoded: total="); pn(cnt[0]); hw(" dcl="); pn(cnt[1]); hw(" mov="); pn(cnt[2]); hw(" arith="); pn(cnt[3]); hw(" dot="); pn(cnt[4]); hw(" sample="); pn(cnt[5]); hw(" ret="); pn(cnt[6]); hw("\n" as *u8)
67 var t4: i64 = 0
68 if cnt[0] == 6 { if cnt[1] == 1 { if cnt[2] == 1 { if cnt[3] == 1 { if cnt[4] == 1 { if cnt[5] == 1 { if cnt[6] == 1 { t4 = 1 } } } } } } }
69 if t4 == 1 { hw("T4 PASS decoded 6 instructions, classified EXACT (dcl/mov/mad/dp3/sample/ret)\n" as *u8) } else { fails=fails+1; hw("T4 FAIL classification\n" as *u8) }
70
71 // T5 NEG-CONTROL: non-DXBC bytes must be rejected (defensive at the untrusted boundary)
72 let bad: *u8 = sys_mmap(64)
73 bad[0] = 70 as u8; bad[1] = 79 as u8; bad[2] = 79 as u8; bad[3] = 66 as u8 // "FOOB"
74 var t5: i64 = 0
75 if dxbc_is(bad, 64) == 0 { t5 = 1 }
76 if t5 == 1 { hw("T5 PASS neg-control: a non-DXBC blob is REJECTED (untrusted-input safe)\n" as *u8) } else { fails=fails+1; hw("T5 FAIL accepted garbage\n" as *u8) }
77
78 // T6 artifact
79 let rep: *u8 = sys_mmap(2048)
80 var q: i64 = 0
81 var s: *u8 = "NISHI DXBC DECODER -- D1 of the D3D-game ladder. Parses the DirectX ByteCode container (magic/checksum/offset-table/chunks) + decodes the SM4/5 shader token stream (opcode bits0-10, length bits24-30), classifying dcl/mov/arith/dot/sample/ret. Proven on a spec-faithful vs_5_0 fixture: 6 instructions, exact classification; non-DXBC rejected.\nThis is the gateway to D2 (translate DXBC -> our ISA / SPIR-V and run on nx_swgpu). NEXT D1b: feed a REAL shipped game shader blob.\n" as *u8
82 var k: i64 = 0
83 while s[k] != (0 as u8) { rep[q] = s[k]; q = q + 1; k = k + 1 }
84 rep[q] = 0 as u8
85 let fd: i64 = sys_openat_wr("knowledge/nx_dxbc.txt\x00" as *u8, 0x1a4)
86 sys_write(fd, rep, q); sys_close(fd)
87 // also persist the fixture blob (a real .dxbc byte stream) for D2 to consume
88 let fd2: i64 = sys_openat_wr("knowledge/nx_fixture.dxbc\x00" as *u8, 0x1a4)
89 sys_write(fd2, buf, n); sys_close(fd2)
90 hw("T6 artifact -> knowledge/nx_dxbc.txt + nx_fixture.dxbc ("); pn(n); hw(" byte blob)\n" as *u8)
91
92 if fails == 0 { hw("NX-DXBC GREEN -- D1: DXBC container + SM5 token decode proven; gateway to D2 translation\n" as *u8); sys_exit(0); return 0 }
93 hw("NX-DXBC RED fails="); pn(fails); hw("\n" as *u8)
94 sys_exit(1)
95 return 1
96}