code wiki / _hdl_build / nx_clock_tickless_gate.nx

nx_clock_tickless_gate.nx source

↩ module page · 44 lines · 4095 B

1// nx_clock_tickless_gate.nx -- proves the TICKLESS driver (sched_tickless.raw lesson) entirely IN the nishi 2// ecosystem (a harness-run gate, no shell hand-driving). Two sparse jobs (intervals 3 and 5, no interval-1) are 3// registered via clk_register; the tickless loop wakes ONLY at the minimum next_due, skipping every idle tick. It 4// reaches tick 12 in just 6 wakes (skipping 6 idle ticks), and each wake fork+execs the real blessed marker organ 5// (_offc/nx_clock_marker.elf) which increments a counter -> proven REAL execution, not a constant tick. 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_clock_sched.nx" 8import "nx_syscalls.nx" 9 10const TLG_MAXBEATS: i64 = 6 11const TLG_TICK_MS: i64 = 20 12 13func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 15func dun(p: *u8) -> i64 { __syscall(87, p as i64, 0, 0, 0, 0, 0); return 0 } 16func readnum(p: *u8) -> i64 { let lp: *i64 = sys_mmap(8) as *i64; let d: *u8 = sys_read_file(p, lp); if (d as i64)==0 { return 0 } var c: i64=0; var i: i64=0; while i<lp[0] { if d[i]>=(48 as u8) { if d[i]<=(57 as u8) { c=c*10+(d[i]-(48 as u8)) as i64 } } i=i+1 } return c } 17 18func main(argc: i64, argv: *i64) -> i64 { 19 w("=== nx_clock_tickless_gate -- wake only when due, skip idle ticks (no shell) ===\n" as *u8) 20 let names: *u8 = sys_mmap(CLK_MAXJOBS*CLK_NAMEW); let orgs: *u8 = sys_mmap(CLK_MAXJOBS*CLK_NAMEW) 21 let iv: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64; let nd: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 22 let np: *i64 = sys_mmap(8) as *i64; np[0]=0 23 clk_register(names, orgs, iv, nd, np, "mj3" as *u8, "_offc/nx_clock_marker.elf" as *u8, 3) 24 clk_register(names, orgs, iv, nd, np, "mj5" as *u8, "_offc/nx_clock_marker.elf" as *u8, 5) 25 dun("/tmp/clk_mark.txt" as *u8) // reset the marker counter (sovereign unlink, not `rm`) 26 27 let out: *i64 = sys_mmap(64) as *i64 28 clk_run_tickless(orgs, iv, nd, np[0], 0, TLG_MAXBEATS, TLG_TICK_MS, out) // the PRODUCTION tickless loop, under test 29 let beats: i64 = out[0]; let dispatches: i64 = out[1]; let skipped: i64 = out[2]; let T: i64 = out[3] 30 let ran: i64 = readnum("/tmp/clk_mark.txt" as *u8) 31 w(" reached tick " as *u8); wn(T); w(" in " as *u8); wn(beats); w(" wakes; SKIPPED " as *u8); wn(skipped); w(" idle ticks; organ ran " as *u8); wn(ran); w(" times\n" as *u8) 32 33 var pass: i64=0; var tot: i64=0 34 // T1 TICKLESS: reached a far tick in fewer wakes than ticks elapsed (skipped idle ticks) 35 tot=tot+1; if skipped > 0 { if beats < T { pass=pass+1; w(" PASS tickless: tick " as *u8); wn(T); w(" in only " as *u8); wn(beats); w(" wakes (no constant tick)\n" as *u8) } else { w(" FAIL woke every tick\n" as *u8) } } else { w(" FAIL skipped 0 idle ticks\n" as *u8) } 36 // T2 REAL execution: the blessed marker organ ran once per dispatch 37 tot=tot+1; if ran == dispatches { if ran > 0 { pass=pass+1; w(" PASS organ ran " as *u8); wn(ran); w("x = every dispatch really executed\n" as *u8) } else { w(" FAIL organ never ran\n" as *u8) } } else { w(" FAIL ran(" as *u8); wn(ran); w(")!=dispatches(" as *u8); wn(dispatches); w(")\n" as *u8) } 38 // T3 EXACT divider schedule: ticks 3,5,6,9,10,12 -> 6 beats, 6 dispatch, 6 skipped, final 12 39 tot=tot+1; if beats==6 { if dispatches==6 { if skipped==6 { if T==12 { pass=pass+1; w(" PASS exact: 6 wakes / 6 dispatch / 6 skipped / tick 12\n" as *u8) } else { w(" FAIL tick!=12\n" as *u8) } } else { w(" FAIL skipped!=6\n" as *u8) } } else { w(" FAIL dispatch!=6\n" as *u8) } } else { w(" FAIL beats!=6\n" as *u8) } 40 41 w("nx_clock_tickless_gate pass=" as *u8); wn(pass); w("/" as *u8); wn(tot) 42 if pass==tot { w(" verdict=GREEN (tickless: one spark, sparse wakes, real organ execution)\n" as *u8); sys_exit(0); return 0 } 43 w(" verdict=RED\n" as *u8); sys_exit(1); return 1 44}