rumpk/core/kernel.nim

99 lines
3.2 KiB
Nim

# Rumpk Layer 1: The Logic Core
# Markus Maiwald (Architect) | Voxis Forge (AI)
#
# This is the Nim kernel entry point.
# Compiled with --os:standalone --mm:arc
{.push stackTrace: off, lineTrace: off.}
# =========================================================
# HAL Imports from Zig (Layer 0)
# =========================================================
proc console_write(p: pointer, len: csize_t) {.importc, cdecl.}
proc rumpk_halt() {.importc, cdecl, noreturn.}
# =========================================================
# Kernel I/O
# =========================================================
proc kprint(s: string) =
if s.len > 0:
console_write(unsafeAddr s[0], csize_t(s.len))
proc kprintln(s: string) =
kprint(s)
kprint("\n")
# =========================================================
# Panic Handler (Required for --os:standalone)
# =========================================================
proc nimPanic(msg: cstring) {.exportc: "panic", cdecl, noreturn.} =
kprint("\n[PANIC] ")
if msg != nil:
var i = 0
while msg[i] != '\0':
var buf: array[1, char]
buf[0] = msg[i]
console_write(addr buf[0], 1)
inc i
kprint("\n")
rumpk_halt()
# =========================================================
# Memory Allocator Stubs (Required for ARC on freestanding)
# =========================================================
# Static heap for bare metal (64KB)
var heapBase {.exportc.}: array[64 * 1024, byte]
var heapOffset {.exportc.}: csize_t = 0
proc allocImpl(size: csize_t): pointer {.exportc: "malloc", cdecl.} =
if heapOffset + size > csize_t(heapBase.len):
return nil
result = addr heapBase[heapOffset]
heapOffset += size
proc deallocImpl(p: pointer) {.exportc: "free", cdecl.} =
# Bump allocator - no dealloc
discard
proc reallocImpl(p: pointer, size: csize_t): pointer {.exportc: "realloc", cdecl.} =
# Simple realloc - just allocate new (wasteful but works)
result = allocImpl(size)
# Nim's internal allocation hooks
proc rawAlloc(size: Natural): pointer {.exportc: "rawAlloc", cdecl.} =
result = allocImpl(csize_t(size))
proc rawDealloc(p: pointer) {.exportc: "rawDealloc", cdecl.} =
deallocImpl(p)
proc rawRealloc(p: pointer, size: Natural): pointer {.exportc: "rawRealloc", cdecl.} =
result = reallocImpl(p, csize_t(size))
# =========================================================
# Kernel Main Entry
# =========================================================
proc kmain() {.exportc, cdecl.} =
kprintln("╔═══════════════════════════════════════╗")
kprintln("║ Layer 1: Nim Kernel Alive! ║")
kprintln("╚═══════════════════════════════════════╝")
kprintln("")
kprintln("[Rumpk L1] Memory: ARC (Deterministic)")
kprintln("[Rumpk L1] POSIX: None (Hostile)")
kprintln("[Rumpk L1] Status: OPERATIONAL")
kprintln("")
kprintln("[Rumpk L1] The Rubicon is crossed.")
kprintln("[Rumpk L1] Zig + Nim = Sovereign Metal.")
kprintln("")
kprintln("[Rumpk L1] Entering idle loop...")
# Idle loop - in real kernel, this would be the scheduler
while true:
{.emit: "asm volatile(\"wfi\");".}
{.pop.}