code wiki / _hdl_build / _clk_probe.nx

_clk_probe.nx source

↩ module page · 24 lines · 1102 B

1// _clk_probe.nx -- empirical check that nx_clock_monotonic_ns() works on the 2// sovereign x86_64 target (rv64 syscall number 113 -> x86 228 via the compiler's 3// translation table). Prints two monotonic reads + their delta in ns/us. 4import "nx_clock.nx" 5 6func cp_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 7func cp_n(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 8 9func main() -> i64 { 10 let t0: i64 = nx_clock_monotonic_ns() 11 var acc: i64 = 0 12 var i: i64 = 0 13 while i < 5000000 { 14 acc = acc + i 15 i = i + 1 16 } 17 let t1: i64 = nx_clock_monotonic_ns() 18 cp_p("t0=" as *u8); cp_n(t0); cp_p(" t1=" as *u8); cp_n(t1) 19 cp_p(" delta_ns=" as *u8); cp_n(t1 - t0) 20 cp_p(" delta_us=" as *u8); cp_n((t1 - t0) / 1000) 21 cp_p(" acc=" as *u8); cp_n(acc) 22 cp_p("\n" as *u8) 23 return 0 24}