code wiki / _hdl_build / nx_riscv_decode.nx

nx_riscv_decode.nx source

↩ module page · 71 lines · 4837 B

1// nx_riscv_decode.nx -- SOVEREIGN RISC-V backend RUNG 1b: our own rv64im DECODER / round-trip oracle. Now on the 2// GENERAL nx_riscv_lib (shared encoders, decoders, ident, UI) -- only name_of + the tests are local. 3// T1 round-trip decode(encode(x))==x. T2 read back the bytes our encoder emitted == the no-float kernel. 4// T3 (teeth) a corrupted word decodes to a different instruction. (no objdump/gcc/sh) 5// expect_exit: 0 Sovereign: nx_riscv_lib (+ nx_syscalls). 6import "nx_riscv_lib.nx" 7import "nx_syscalls.nx" 8 9func name_of(id: i64) -> i64 { 10 if id==1 { g_puts("add " as *u8) } else { if id==2 { g_puts("sub " as *u8) } else { if id==3 { g_puts("mul " as *u8) } else { 11 if id==4 { g_puts("addi" as *u8) } else { if id==5 { g_puts("ld " as *u8) } else { if id==6 { g_puts("sd " as *u8) } else { 12 if id==7 { g_puts("srai" as *u8) } else { if id==8 { g_puts("jalr" as *u8) } else { if id==9 { g_puts("ecall" as *u8) } else { g_puts("????" as *u8) } } } } } } } } } 13 return 0 14} 15 16func main() -> i64 { 17 g_puts("nx_riscv_decode (RUNG 1b decoder/round-trip oracle; now on the GENERAL nx_riscv_lib; no objdump)\n" as *u8) 18 var pass: i64=0; var total: i64=0 19 20 g_puts(" -- T1: round-trip decode(encode(x))==x --\n" as *u8) 21 var rt: i64=0; var rtn: i64=0 22 let w1: i64=enc_r(0x33,0,0,5,6,7); rtn=rtn+1; if d_op(w1)==0x33 { if d_f3(w1)==0 { if d_f7(w1)==0 { if d_rd(w1)==5 { if d_rs1(w1)==6 { if d_rs2(w1)==7 { rt=rt+1 } } } } } } 23 let w2: i64=enc_r(0x33,0,1,9,9,10); rtn=rtn+1; if d_op(w2)==0x33 { if d_f7(w2)==1 { if d_rd(w2)==9 { if d_rs1(w2)==9 { if d_rs2(w2)==10 { rt=rt+1 } } } } } 24 let w3: i64=enc_r(0x33,0,0x20,1,2,3); rtn=rtn+1; if d_f7(w3)==0x20 { if ident(w3)==2 { rt=rt+1 } } 25 let w4: i64=enc_i(0x13,0,8,8,8); rtn=rtn+1; if d_op(w4)==0x13 { if d_rd(w4)==8 { if d_rs1(w4)==8 { if d_immi(w4)==8 { rt=rt+1 } } } } 26 let w5: i64=enc_i(0x03,3,11,18,0); rtn=rtn+1; if ident(w5)==5 { if d_rd(w5)==11 { if d_rs1(w5)==18 { rt=rt+1 } } } 27 let w6: i64=enc_i(0x13,5,10,10,0x410);rtn=rtn+1; if ident(w6)==7 { if d_immi(w6)==0x410 { rt=rt+1 } } 28 g_puts(" round-trips OK: "); g_pn(rt); g_puts("/"); g_pn(rtn); g_puts("\n" as *u8) 29 var t1: i64=0; if rt==rtn { t1=1 } 30 pass=pass+ck("T1: every encoded instruction decodes back to identical fields (encoder/decoder are inverses)" as *u8, t1); total=total+1 31 32 g_puts(" -- T2: decode the bytes our encoder wrote to riscv_matvec.bin --\n" as *u8) 33 let fd: i64=sys_openat_rd("knowledge/research/riscv_matvec.bin" as *u8) 34 var t2: i64=0 35 if fd>=0 { 36 let buf: *u8=sys_mmap(64); let got: i64=sys_read(fd, buf, 64); sys_close(fd) 37 let nw: i64=got/4 38 let exp: *i64=sys_mmap(8*8) as *i64; exp[0]=5; exp[1]=5; exp[2]=3; exp[3]=7; exp[4]=1; exp[5]=4; exp[6]=4 39 var okseq: i64=1; var rok: i64=1; var i: i64=0 40 while i<nw { 41 let w: i64=(buf[i*4] as i64)|((buf[i*4+1] as i64)<<8)|((buf[i*4+2] as i64)<<16)|((buf[i*4+3] as i64)<<24) 42 let id: i64=ident(w) 43 g_puts(" word "); g_pn(i); g_puts(": "); name_of(id) 44 if i<7 { if id != exp[i] { okseq=0; g_puts(" (UNEXPECTED)" as *u8) } } 45 var re: i64=w 46 if id==3 { re=enc_r(0x33,0,1,d_rd(w),d_rs1(w),d_rs2(w)) } 47 if id==1 { re=enc_r(0x33,0,0,d_rd(w),d_rs1(w),d_rs2(w)) } 48 if id==5 { re=enc_i(0x03,3,d_rd(w),d_rs1(w),d_immi(w)) } 49 if id==4 { re=enc_i(0x13,0,d_rd(w),d_rs1(w),d_immi(w)) } 50 if id==7 { re=enc_i(0x13,5,d_rd(w),d_rs1(w),d_immi(w)) } 51 if re != w { rok=0; g_puts(" (RE-ENCODE MISMATCH)" as *u8) } 52 g_puts("\n" as *u8) 53 i=i+1 54 } 55 if nw==7 { if okseq==1 { if rok==1 { t2=1 } } } 56 } 57 pass=pass+ck("T2: our decoder reads our emitted bytes as the no-float kernel [ld,ld,mul,srai,add,addi,addi]" as *u8, t2); total=total+1 58 59 let good: i64=enc_r(0x33,0,0,5,6,7); let corrupt: i64=good ^ 0x02000000 60 var t3: i64=0; if ident(good)==1 { if ident(corrupt)==3 { t3=1 } } 61 pass=pass+ck("T3 (teeth): a corrupted word decodes to a DIFFERENT instruction (add->mul)" as *u8, t3); total=total+1 62 63 var okall: i64=0; if pass==total { okall=1 } 64 g_puts("---- riscv_decode: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 65 if okall==1 { 66 let logf: i64=sys_openat_append("knowledge/status/riscv_decode.log" as *u8, 420) 67 if logf>=0 { let z: i64=sys_write(logf,"RISCVDECODE on nx_riscv_lib GREEN: round-trip + read-back of emitted no-float kernel (no objdump)\n" as *u8,94); sys_close(logf) } 68 g_puts("verdict=GREEN (RUNG 1b on the GENERAL nx_riscv_lib: encoder/decoder inverses; reads our emitted kernel; ecosystem self-checks)\n" as *u8); sys_exit(0); return 0 69 } 70 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 71}