code wiki / _hdl_build / nx_rv64_ctier_gate.nx

nx_rv64_ctier_gate.nx source

↩ module page · 59 lines · 5001 B

1// nx_rv64_ctier_gate.nx -- the TIERED engine runs REAL compressed rv64imac code. Loads knowledge/hw/realc_c.bin (gcc 2// -march=rv64imac, 59 compressed instrs; UART==QEMU 010937272a proven by the compressed real-C suite), runs it on 3// tier_run_c (halfword-indexed, JIT-hot-loop-capable) with UART capture, and confirms UART == 01 09 37 27 2a. Also 4// confirms fk_run_c (interpreter) agrees. Proves the PRODUCTION tiered path RUNS compressed binaries. expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_rv64_fast.nx" 7import "nx_rv64_jit.nx" 8import "nx_rv64_tier.nx" 9 10func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x==0{b[0]=48;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 } 12func g_hx(v: i64) -> i64 { let b: *u8=sys_mmap(4); let n0: i64=(v>>4)&15; let n1: i64=v&15; if n0<10 { b[0]=(48+n0) as u8 } else { b[0]=(87+n0) as u8 } if n1<10 { b[1]=(48+n1) as u8 } else { b[1]=(87+n1) as u8 } b[2]=32 as u8; sys_write(1,b,3); return 0 } 13func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c } 14const MEMSZ: i64 = 262144 15 16func run_capture(useTier: i64, code: *u8, nb: i64, txbuf: *u8, stats: *i64) -> i64 { 17 let nslot: i64=nb/2 + 16 18 let opk: *i64=sys_mmap(nslot*8) as *i64; let rd: *i64=sys_mmap(nslot*8) as *i64; let rs1: *i64=sys_mmap(nslot*8) as *i64; let rs2: *i64=sys_mmap(nslot*8) as *i64; let imm: *i64=sys_mmap(nslot*8) as *i64 19 let nc: i64=fk_predecode_c(code, nb, opk, rd, rs1, rs2, imm) 20 let reg: *i64=sys_mmap(32*8) as *i64; let mem: *u8=sys_mmap(MEMSZ); var z: i64=0; while z<32 { reg[z]=0; z=z+1 } var m: i64=0; while m<nb { mem[m]=code[m]; m=m+1 } 21 fk_tx_reset(txbuf) 22 if useTier==1 { tier_run_c(opk, rd, rs1, rs2, imm, nc, reg, mem, MEMSZ, 500000000, (0 as i64) as *NxVirtioMmio, 2, stats) } 23 else { fk_run_c(opk, rd, rs1, rs2, imm, nc, reg, mem, MEMSZ, 500000000, (0 as i64) as *NxVirtioMmio) } 24 return fk_tx_count() 25} 26func match5(tx: *u8, n: i64) -> i64 { if n!=5 { return 0 } if tx[0]!=(0x01 as u8) { return 0 } if tx[1]!=(0x09 as u8) { return 0 } if tx[2]!=(0x37 as u8) { return 0 } if tx[3]!=(0x27 as u8) { return 0 } if tx[4]!=(0x2a as u8) { return 0 } return 1 } 27 28func main() -> i64 { 29 g_puts("nx_rv64_ctier_gate (the TIERED engine runs real COMPRESSED rv64imac code -- vs QEMU 01 09 37 27 2a)\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 let lenbox: *i64=sys_mmap(16) as *i64 32 let code: *u8=sys_read_file("knowledge/hw/realc_c.bin" as *u8, lenbox) 33 if (code as i64)==0 { g_puts(" NO realc_c.bin -- compile with -march=rv64imac first\n" as *u8); sys_exit(1); return 1 } 34 let nb: i64=lenbox[0] 35 g_puts(" loaded realc_c.bin: "); g_pn(nb); g_puts(" bytes of gcc -march=rv64imac (compressed)\n" as *u8) 36 37 let txf: *u8=sys_mmap(512); let statsf: *i64=sys_mmap(64) as *i64 38 let nf: i64=run_capture(0, code, nb, txf, statsf) 39 g_puts(" fk_run_c (interp) UART: "); var i: i64=0; while i<nf { g_hx(txf[i] as i64); i=i+1 } g_puts("\n" as *u8) 40 var t1: i64=match5(txf, nf) 41 pass=pass+ck("T1: fk_run_c interpreter runs the compressed binary -> 01 09 37 27 2a (== QEMU)" as *u8, t1); total=total+1 42 43 let txt: *u8=sys_mmap(512); let statst: *i64=sys_mmap(64) as *i64 44 let nt: i64=run_capture(1, code, nb, txt, statst) 45 g_puts(" tier_run_c UART: "); i=0; while i<nt { g_hx(txt[i] as i64); i=i+1 } g_puts(" (interpreted-steps="); g_pn(statst[0]); g_puts(" loops-JIT'd="); g_pn(statst[1]); g_puts(")\n" as *u8) 46 var t2: i64=match5(txt, nt) 47 pass=pass+ck("T2: the TIERED engine (tier_run_c) runs the compressed binary -> 01 09 37 27 2a (== QEMU)" as *u8, t2); total=total+1 48 var t3: i64=0; if statst[0]>500 { t3=1 } // real execution (fib recursion), not an early halt 49 pass=pass+ck("T3: the tiered engine executed the full program (compressed loops interpret; JIT-of-compressed = documented follow-up)" as *u8, t3); total=total+1 50 51 var okall: i64=0; if pass==total { okall=1 } 52 g_puts("---- nx_rv64_ctier_gate: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 53 if okall==1 { 54 let logf: i64=sys_openat_append("knowledge/status/ctier.log" as *u8, 420) 55 if logf>=0 { let z: i64=sys_write(logf,"NXRV64CTIER GREEN: the tiered engine (tier_run_c, halfword-indexed) RUNS real compressed rv64imac binaries == QEMU (01 09 37 27 2a); compressed loops interpret (JIT-of-compressed = follow-up). every fast-family engine now runs rv64imac.\n" as *u8,240); sys_close(logf) } 56 g_puts("verdict=GREEN (the TIERED engine runs real compressed rv64imac code == QEMU; every fast-family engine now runs default-compiled binaries)\n" as *u8); sys_exit(0); return 0 57 } 58 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 59}