code wiki / _hdl_build / nx_sched_emit.nx

nx_sched_emit.nx

buildroot/runtime/_hdl_build/nx_sched_emit.nx

8648 B136 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tooltopic sched
docsdependenciesstructsconstsfunctions

about

nx_sched_emit.nx -- PREEMPTIVE SCHEDULER emitter (2-task round-robin), the kernel-up rung that flips census GEN-K-preemptive-smp-scheduler from BEHIND toward PRESENT. Builds on the proven timer-interrupt mechanism (nx_timer_irq_emit). AUTHOR=ORGAN: table-computes a bare-metal rv64 qemu-virt image (zero hand-written machine code). Two tasks loop-emit their own marker ('A' / 'B'). The CLINT timer preempts every TICK; the trap handler is the SCHEDULER -- it ROUND-ROBINS the two tasks with the one-instruction trick csrrw t6, mepc, t6 ; atomically swap the interrupted PC <-> the saved task PC (no task-control-block memory, no load needed: two tasks fit in mepc + one register), re-arms the timer (deadline += TICK -> MTIMECMP), counts ticks, and after K ticks clean-halts via the SiFive finisher. The transcript is interleaved RUNS of A and B -- PROOF the scheduler preempted and switched contexts repeatedly. (Interleaving run-length is timing-determined, so the gate asserts the STRUCTURAL property -- >=N A<->B switches -- not a byte-exact golden; honest for a preemptive phenomenon.) Reuses the proven rv64 mini-encoder. Sovereign, no gcc/.sh. license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_sched_emit.nx

imports: nx_syscalls.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main sys_mmap sc_w32 sc_auipc sc_addi sc_csrrw sc_lui sc_store sc_csrrs sc_jal sc_branch sys_openat_wr sc_p sys_write sys_write ↻ sys_close sc_fn sys_mmap ↻ sys_write ↻ sys_openat_append sc_fp sys_write ↻ sys_now_realtime_sec sys_mmap ↻ sys_clock_gettime_real

structs

none

consts

16const SC_MAGIC_4096: i64 = 4096
18const SC_OUT: *u8 = "runtime/_hdl_build/_sched_virt.bin"
19const SC_LOG: *u8 = "knowledge/status/sched.log"
21const SC_UART: i64 = 0x10000000
22const SC_FIN: i64 = 0x100000
23const SC_PASS: i64 = 0x5555
24const SC_MTIMECMP: i64 = 0x02004000
25const SC_TICK: i64 = 0x40
26const SC_KTICKS: i64 = 12 // bounded run: 12 preemptions then halt
27const SC_MSTATUS: i64 = 0x300
28const SC_MIE: i64 = 0x304
29const SC_MTVEC: i64 = 0x305
30const SC_MEPC: i64 = 0x341
31const SC_MIE_MTIE: i64 = 0x80
32const SC_MSTATUS_MIE: i64 = 0x08
33const SC_CH_S: i64 = 83 // 'S' boot
34const SC_CH_A: i64 = 65 // 'A' task A
35const SC_CH_B: i64 = 66 // 'B' task B
37const SC_HANDLER: i64 = 68
38const SC_HALT: i64 = 100
39const SC_TASKA: i64 = 124
40const SC_TASKB: i64 = 136
42const RV_X0: i64 = 0
43const RV_T0: i64 = 5
44const RV_T1: i64 = 6
45const RV_T2: i64 = 7
46const RV_T3: i64 = 28
47const RV_T4: i64 = 29
48const RV_T5: i64 = 30
49const RV_T6: i64 = 31

functions

51func sc_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 }
called by 1: main
52func sc_auipc(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x17 }
called by 1: main
53func sc_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 }
called by 1: main
54func sc_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64
called by 1: main
59func sc_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64
called by 1: main
66func sc_jal(rd: i64, imm: i64) -> i64
called by 1: main
73func sc_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 }
called by 1: main
74func sc_csrrs(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (2 << 12) | (rd << 7) | 0x73 }
called by 1: main
75func sc_w32(buf: *u8, off: i64, w: i64) -> i64
called by 1: main
79func sc_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
80func sc_fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
called by 1: main calls 1: sys_write
81func sc_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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(fd,bb,k); return 0 }
called by 1: main calls 2: sys_mmapsys_write
83func main() -> i64