From 0cc9b0a8bdf774ee376937d5a6767ccdced51245 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 18 Jul 2026 10:10:04 +0200 Subject: kernel: a real timer source (64 Hz tick IRQ) + uptime(1) The kernel had no clock: TimerISR was a reti stub, IEF_TIMER masked, and IME was never enabled - the vectors were decorative. sys_sleep just polls DIV deltas per-process; nothing counted globally. Now: TAC runs the hardware timer at 16384 Hz with TMA=0, so TIMA overflows at exactly 64 Hz; TimerISR increments a monotonic 32-bit wTicks (wraps after ~2.1 years). Scheduling stays cooperative - the ISR is transparent. Enabling IME in a kernel written for zero interrupts needs care wherever SP points into memory whose bank is being switched (an IRQ pushes onto SP): - read_block/write_block map the disk bank over the $A000 window that holds the caller's stack -> di/ei around the transfer (~2ms, well under the 15.6ms tick period, so no tick is ever lost) - hSwitchTo switches SVBK + cart-RAM banks under the outgoing stack -> di on entry, ei once the incoming stack is mapped - fork already runs on KSTACK_TOP2 (fixed WRAM) - safe as-is - term_putc's SVBK switch only remaps $Dxxx, stacks live in $Axxx/$Cxxx SYS_UPTIME (36) copies the counter (4B LE, di/ei so the read can't tear) to a user buffer; libc gticks(); usr/uptime.c formats 'up [Nd] H:MM:SS'. uptime avoids SDCC long div/shift entirely: sm83.lib modules link into their own areas that land in the $A000 RAM window (latent build.sh trap, documented there) - bytewise >>6 plus bounded subtraction loops instead. --- usr/build.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'usr/build.sh') diff --git a/usr/build.sh b/usr/build.sh index 93815d2..5d494cf 100755 --- a/usr/build.sh +++ b/usr/build.sh @@ -10,7 +10,12 @@ sdasgb -o "$B/crt0.rel" usr/crt0.s sdasgb -o "$B/libc.rel" usr/libc.s sdcc -msm83 -c -Iusr usr/libc.c -o "$B/libc_c.rel" sdcc -msm83 -c -Iusr "$SRC" -o "$B/prog.rel" -# link: crt0 first (=> _start at 0x4000), then libc, program, and sm83 lib +# link: crt0 first (=> _start at 0x4000), then libc, program, and sm83 lib. +# TRAP: only _CODE/_DATA have pinned bases. sm83.lib modules (long div/mod/ +# shift helpers) link into their own areas, which sdldgb places after _DATA - +# i.e. code in the $A000 RAM window ("buffer too small" from makebin, or +# garbage at runtime). Don't use long division/shifts in programs; see +# usr/uptime.c for the inlined-arithmetic workaround. sdldgb -n -m -w -j -i "$B/prog.ihx" \ -b _CODE=0x4000 -b _DATA=0xa000 \ -k "$LIBDIR" -l sm83 \ -- cgit v1.3.1-sl0p