code wiki / _hdl_build / nx_sched_emit.nx
nx_sched_emit.nx
buildroot/runtime/_hdl_build/nx_sched_emit.nx
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
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
structs
| none |
consts
| 16 | const SC_MAGIC_4096: i64 = 4096 |
| 18 | const SC_OUT: *u8 = "runtime/_hdl_build/_sched_virt.bin" |
| 19 | const SC_LOG: *u8 = "knowledge/status/sched.log" |
| 21 | const SC_UART: i64 = 0x10000000 |
| 22 | const SC_FIN: i64 = 0x100000 |
| 23 | const SC_PASS: i64 = 0x5555 |
| 24 | const SC_MTIMECMP: i64 = 0x02004000 |
| 25 | const SC_TICK: i64 = 0x40 |
| 26 | const SC_KTICKS: i64 = 12 // bounded run: 12 preemptions then halt |
| 27 | const SC_MSTATUS: i64 = 0x300 |
| 28 | const SC_MIE: i64 = 0x304 |
| 29 | const SC_MTVEC: i64 = 0x305 |
| 30 | const SC_MEPC: i64 = 0x341 |
| 31 | const SC_MIE_MTIE: i64 = 0x80 |
| 32 | const SC_MSTATUS_MIE: i64 = 0x08 |
| 33 | const SC_CH_S: i64 = 83 // 'S' boot |
| 34 | const SC_CH_A: i64 = 65 // 'A' task A |
| 35 | const SC_CH_B: i64 = 66 // 'B' task B |
| 37 | const SC_HANDLER: i64 = 68 |
| 38 | const SC_HALT: i64 = 100 |
| 39 | const SC_TASKA: i64 = 124 |
| 40 | const SC_TASKB: i64 = 136 |
| 42 | const RV_X0: i64 = 0 |
| 43 | const RV_T0: i64 = 5 |
| 44 | const RV_T1: i64 = 6 |
| 45 | const RV_T2: i64 = 7 |
| 46 | const RV_T3: i64 = 28 |
| 47 | const RV_T4: i64 = 29 |
| 48 | const RV_T5: i64 = 30 |
| 49 | const RV_T6: i64 = 31 |
functions
| 51 | func sc_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } called by 1: main |
| 52 | func sc_auipc(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x17 } called by 1: main |
| 53 | func sc_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } called by 1: main |
| 54 | func sc_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 called by 1: main |
| 59 | func sc_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: main |
| 66 | func sc_jal(rd: i64, imm: i64) -> i64 called by 1: main |
| 73 | func sc_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } called by 1: main |
| 74 | func sc_csrrs(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (2 << 12) | (rd << 7) | 0x73 } called by 1: main |
| 75 | func sc_w32(buf: *u8, off: i64, w: i64) -> i64 called by 1: main |
| 79 | func 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 } |
| 80 | func 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 } |
| 81 | func 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 } |
| 83 | func main() -> i64 |