Appearance
navx SDK — C programs for the MK4
@emdzej/navx-sdk builds OS-9000/MIPS program modules from C with a stock LLVM clang (no Microware tools), and @emdzej/navx-emu runs them.
sh
brew install llvm # Apple's clang has no MIPS backend
navx-cc hello.c -o hello # → OS-9000 program module "hello"
navx run hello one two # run it in the emulator
navx run --navboot navboot date # stock programs need csl from the poolclang is found via $NAVX_CLANG, then Homebrew's llvm/llvm@N, then PATH.
Code model
The kernel loads a module anywhere and gives each process its own data area, so code must be position-independent and data reached through gp (gp = data area + 0x7ff0, set by the kernel). clang runs with:
| Flag | Why |
|---|---|
-target mips-unknown-none-elf -march=mips1 | MIPS I, big-endian: a safe subset of the unit's R3900-class core (which adds branch-likely and emulates the FPU) |
-mno-abicalls -fno-pic | abicalls PIC derives gp from the code address; here code and data are unrelated |
-G 65535 | every object, constants and string literals too, goes to .sdata/.sbss → gp-relative |
-fno-jump-tables | switch tables would hold absolute code addresses |
-msoft-float | the unit has no FPU (the kernel would trap and emulate each instruction), and clang's MIPS I backend can't load a double (ldc1 is MIPS II): float/double are calls into compiler-rt's soft-float (below) |
-mno-check-zero-division | LLVM's trap for it is teq, a MIPS II instruction |
-fomit-frame-pointer, no unwind tables | smaller code, no .eh_frame |
The linker (in TypeScript, no lld needed) then:
- lays out code after the module name, initialised data from data offset 0, then zeroed data;
- rewrites
jal/j(absolute within a 256 MB segment) intobal/b(PC-relative, ±128 KB); - resolves gp-relative references against
gp - 0x7ff0= data offset 0; - turns every pointer in initialised data into an idref entry (code refs / data refs) — the kernel relocates them at fork; the stock C start-up does not (checked by running
datewith an empty data area: it exits 1); - rejects absolute addressing in code (
R_MIPS_HI16/LO16) with an explanation: data over 64 KB, or a function address taken in code (put it in an initialised table instead —static int (*ops[])(…) = {f, g}works); - checks the code for MIPS I: no MIPS II+ instructions, no use of a register in its load-delay slot, no hi/lo write within two instructions of
mfhi/mflo. The emulator doesn't model load delays, so this is what catches those bugs before a car does.
Large data (navx-cc --large-data)
Globals over 64 KB (Doom's tables, frame buffers) don't fit in gp reach. With --large-data (build(…, { largeData: true })) the inputs compile as abicalls PIC (-mabicalls -fpic -G 0, LARGE_DATA_CFLAGS): code reaches each global through a GOT entry (lw rX, %got(sym)(gp)) and calls go through the GOT too (lw t9, %call16(f)(gp); jalr t9). The linker builds the GOT at data offset 0, in gp reach, with one entry per global symbol and one per local symbol and high half of the addend: a local GOT16's entry holds the symbol's address plus its immediate's high half, and the LO16s that use it keep the addend's low half. No GOT16/LO16 pairing is needed; clang's relocations aren't always in pairing order. Every entry is an idref, so the kernel relocates it at fork like any pointer in initialised data; the data itself can be any size.
abicalls code sets gp = _gp_disp + t9 in each function's prologue, which assumes the data sits at a fixed distance from the code. Here it doesn't (each process gets its own data area). clang also uses $28 as a scratch register between calls, and leaves out setting $gp before a call because the callee rebuilds it from t9 anyway. So each function must get gp from t9 alone. With --large-data each function has its own section (-ffunction-sections), and the linker:
- puts a word in front of it;
- turns the
lui/addiu _gp_disppair intolw rX, -4(t9); nop; - lists the words in
__navx_gp_slots(count, then the addresses as idrefs).
crt0's start_c stays small-model on the kernel's gp, writes gp - function into each word, and calls main (and os9_exit) through pointers, so t9 is set. Everything else the program calls (the runtime, libc, soft-float) is compiled the same way. The words are in the module, so one module serves one process at a time.
The small model's .sdata/.sbss (crt0's, the signal handler's word) and the word list go right after the GOT (the zeroed part included, as initialised zeros) so they stay in reach. The cost is a GOT load per global access and an indirect call per call. Verified in the emulator: sdk.test.ts (280 KB of globals, a page past 64 KB, a function address in code, calls into the runtime), and doomgeneric (#24: 540 KB module, 418 KB of data), which boots and plays its demo. Not yet on a unit.
Runtime
runtime/ is compiled into every module:
crt0.c—_start(struct os9_startup *): argv, thenexit(main(argc, argv)). The shell and sysgo pass an argv table: u160xfc01, +4 argv offset, +8 argc, +0x0c envp offset, with array entries and strings as offsets from +0x10. The layout is from csl's decoder at +0x11ae8 and was checked against the shell's forks in the emulator. A plain string is split at spaces after the module name.os9.c— the system-call stub (same instructions as BMW's),os9_exit,os9_write,os9_writln,os9_time,os9_srqmem,os9_srqcmem(memory colour, e.g. 128 for display pages),os9_fork,os9_wait,os9_send,os9_sleep,os9_open,os9_create,os9_close,os9_read,os9_ready(SS_Ready),os9_getstat/os9_setstat(raw parameter pointer),os9_id,os9_gprdsc(PD_*offsets),os9_datmodandos9_link(F_LINK{name, out header, out exec, u16 type_lang, u16 attr_rev}, as the emulator's handler). Block layouts come from the stock wrappers (tools/re/layouts.pyover csl and procs). Alsoos9_puts(\n→ CR) andos9_putu.string.c—mem*,strlen,strcmp.
Header: #include <navx/os9.h>.
The C library
runtime/libc and runtime/include (stdio.h, stdlib.h, string.h, strings.h, ctype.h, errno.h, assert.h, inttypes.h, sys/stat.h, …) are enough for ported code like doomgeneric. navx-cc links them like an archive: a member only when the program uses one of its symbols, so a program that calls none of it is unchanged (carbus builds byte-identical). Also linked that way, runtime/softfp: compiler-rt's soft-float and 64-bit helpers (__adddf3, __divdi3, …; Apache-2.0 with LLVM exceptions, unchanged, see its README).
- stdio:
FILEs over OS-9 paths, unbuffered:fopen(r/w/a,+;wdeletes and creates),fread/fwrite,fseek(SEEK_ENDfrom getstat SS_Size) andftell,fgets/fgetc/ungetc,remove(I_DELETE).renamefails withENOSYS. stdout and stderr send\nas CR; files are binary. - printf family:
%d %i %u %x %X %o %c %s %p %f %%, flags, width, precision,*.%ftakes at most 9 decimals and values up to 2·10⁹. - sscanf:
%d %i %u %x %o %c %s %[…] %n, widths and*. - malloc: first fit over F_SRQMEM chunks of at least 256 KB, merging freed neighbours. Memory goes back to the system when the process exits.
- Others:
strto*,atoi/atof,qsort(insertion sort),mkdir(I_MAKDIR),errno(the OS-9 error code),atexit(up to 8, run byexit, not byos9_exit).getenvreturns NULL andsystemreturns -1.
The block layouts of I_SEEK, I_DELETE and I_MAKDIR are the emulator's (path/name at +0x0c); they aren't traced from stock code yet.
The hi/lo hazard (--pad-hilo)
MIPS I needs two instructions between mfhi/mflo and the next mult/div. clang's MIPS I support doesn't keep that gap, and the link rejects code that breaks it. The library members go through assembly with two nops after every mfhi/mflo (padHiLo in cc.ts). --pad-hilo (padHiLo: true) does the same for a program's own sources: use it for ported code that multiplies and divides. One mfhi/mflo in a branch delay slot would keep the hazard on the taken path; the link's check still catches that case.
Gotchas
- A choice between two string literals crashes clang 22's MIPS backend with
-G 65535("Cannot select … MipsISD::GPRel"). It happens withc ? "a" : "b", and with if/else calling the same function, which the optimiser merges into the same select. Index a table instead:static const char *msg[] = {"a", "b"}; puts(msg[c]);. The table's pointers become idref entries.
Not yet
- Calling into
csl(BMW's C library) — needsfp = module + 0x7ff0kept in our code and the trampoline table. - Events, chain and alarms have no wrappers yet.
- A plain parameter string is split at spaces; no quoting.