code wiki / _hdl_build / nx_rvc_kat.nx

nx_rvc_kat.nx source

↩ module page · 28 lines · 2611 B

1// nx_rvc_kat.nx -- KAT for the RVC expander: expand known compressed instructions, check the 32-bit equivalents against 2// hand-computed base encodings (before hooking into the sim). expect_exit: 0 3import "nx_syscalls.nx" 4import "nx_rvc_expand.nx" 5 6func 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 } 7func g_ph(v: i64) -> i64 { g_puts("0x" as *u8); let b: *u8=sys_mmap(12); var i: i64=7; while i>=0 { let nib: i64=(v>>(i*4))&15; if nib<10 { b[7-i]=(48+nib) as u8 } else { b[7-i]=(87+nib) as u8 } i=i-1 } sys_write(1,b,8); return 0 } 8func ck(name: *u8, got: i64, exp: i64) -> i64 { var c: i64=0; if got==exp { c=1 } if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts(" got="); g_ph(got); g_puts(" exp="); g_ph(exp); g_puts("\n" as *u8); return c } 9 10func main() -> i64 { 11 g_puts("nx_rvc_kat (RVC expander -- compressed -> 32-bit base, vs hand-computed encodings)\n" as *u8) 12 var pass: i64=0; var total: i64=0 13 pass=pass+ck("c.li x5,3 -> addi x5,x0,3" as *u8, nx_rvc_expand(0x428D), 0x00300293); total=total+1 14 pass=pass+ck("c.addi x5,x5,1 -> addi x5,x5,1" as *u8, nx_rvc_expand(0x0285), 0x00128293); total=total+1 15 pass=pass+ck("c.mv x6,x7 -> add x6,x0,x7" as *u8, nx_rvc_expand(0x831E), 0x00700333); total=total+1 16 pass=pass+ck("c.slli x5,x5,4 -> slli x5,x5,4" as *u8, nx_rvc_expand(0x0292), 0x00429293); total=total+1 17 pass=pass+ck("c.add x6,x6,x7 -> add x6,x6,x7" as *u8, nx_rvc_expand(0x931E), 0x00730333); total=total+1 18 // c.jr x1 (ret): op=10 f3=100 b12=0 rd=1 rs2=0 -> jalr x0,0(x1) = 0x00008067 19 pass=pass+ck("c.jr x1 (ret) -> jalr x0,0(x1)" as *u8, nx_rvc_expand(0x8082), 0x00008067); total=total+1 20 // c.compressed detection 21 var t7: i64=0; if nx_rvc_is_compressed(0x428D)==1 { if nx_rvc_is_compressed(0x00300293)==0 { t7=1 } } // 0x293 low2=11 -> not compressed 22 pass=pass+ck("is_compressed: 0x428D yes, 0x...93 no" as *u8, t7, 1); total=total+1 23 24 var okall: i64=0; if pass==total { okall=1 } 25 g_puts("---- nx_rvc_kat: passed "); let pb: *u8=sys_mmap(8); var pv: i64=pass; var pd: i64=0; if pv==0 {pb[0]=48;pd=1} else { var y: i64=pv; var n: i64=0; while y>0 {n=n+1;y=y/10} pd=n; var j: i64=n-1; y=pv; while j>=0 {pb[j]=(48+y%10) as u8;y=y/10;j=j-1} } sys_write(1,pb,pd); g_puts(" / " as *u8); let tb: *u8=sys_mmap(8); tb[0]=(48+total) as u8; sys_write(1,tb,1); g_puts(" ----\n" as *u8) 26 if okall==1 { g_puts("verdict=GREEN (RVC expander encodings correct)\n" as *u8); sys_exit(0); return 0 } 27 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 28}