# Markus Maiwald (Architect) | Voxis Forge (AI)
# Nexus Rumpk: ION Control Plane

import ion/memory
export memory

type
  CmdType* = enum
    CMD_SYS_NOOP = 0
    CMD_SYS_EXIT = 1       # Dignified Exit (Subject Termination)
    CMD_ION_STOP = 2
    CMD_ION_START = 3
    CMD_GPU_MATRIX = 0x100
    CMD_GPU_CLEAR = 0x101
    CMD_GET_GPU_STATUS = 0x102
    CMD_FS_OPEN = 0x200
    CMD_FS_READ = 0x201
    CMD_FS_READDIR = 0x202 # Returns raw listing
    CMD_FS_WRITE = 0x203   # Write File (arg1=ptr to FileArgs)
    CMD_ION_FREE = 0x300   # Return slab to pool
    CMD_SYS_EXEC = 0x400   # Swap Consciousness (ELF Loading)
    CMD_NET_TX = 0x500     # Send Network Packet (arg1=ptr, arg2=len)
    CMD_NET_RX = 0x501     # Poll Network Packet (arg1=ptr, arg2=maxlen)
    CMD_BLK_READ = 0x600   # Read Sector (arg1=ptr to BlkArgs)
    CMD_BLK_WRITE = 0x601  # Write Sector (arg1=ptr to BlkArgs)

  CmdPacket* = object
    kind*: uint32
    reserved*: uint32    # Explicit Padding
    arg*: uint64         # Upgraded to u64 for Pointers
    id*: array[16, byte] # u128 for SipHash Provenance

  FsReadArgs* = object
    fd*: uint64
    buffer*: uint64

  FileArgs* = object
    name*: uint64
    data*: uint64
    len*: uint64

  NetArgs* = object
    buf*: uint64
    len*: uint64

  BlkArgs* = object
    sector*: uint64
    buf*: uint64
    len*: uint64

  # Binary compatible with hal/channel.zig
  HAL_Ring*[T] = object
    head*: uint32
    tail*: uint32
    mask*: uint32
    data*: array[256, T]

  SovereignChannel*[T] = object
    ring*: ptr HAL_Ring[T]

  SysTable* = object
    magic*: uint32                    # 0x4E585553
    reserved*: uint32                 # Explicit Padding for alignment
    s_rx*: ptr HAL_Ring[IonPacket]    # Kernel -> App
    s_tx*: ptr HAL_Ring[IonPacket]    # App -> Kernel
    s_event*: ptr HAL_Ring[IonPacket] # Telemetry
    s_cmd*: ptr HAL_Ring[CmdPacket]   # Command Ring (Control Plane)
    s_input*: ptr HAL_Ring[IonPacket] # Input to Subject
                                      # Function Pointers (Hypercalls)
    fn_vfs_open*: pointer
    fn_vfs_read*: pointer
    fn_vfs_list*: pointer
    fn_log*: pointer

include invariant

static: doAssert(sizeof(IonPacket) == 24, "IonPacket size mismatch!")
static: doAssert(sizeof(CmdPacket) == 32, "CmdPacket size mismatch!")
static: doAssert(sizeof(SysTable) == 80, "SysTable size mismatch!")

const SYSTABLE_BASE* = 0x83000000'u64

# HAL Imports (Hardened ABI - Handle Based)
proc hal_channel_push*(handle: uint64,
    pkt: IonPacket): bool {.importc: "hal_channel_push", cdecl.}
proc hal_channel_pop*(handle: uint64,
    out_pkt: ptr IonPacket): bool {.importc: "hal_channel_pop", cdecl.}

proc hal_cmd_push*(handle: uint64,
    pkt: CmdPacket): bool {.importc: "hal_cmd_push", cdecl.}
proc hal_cmd_pop*(handle: uint64,
    out_pkt: ptr CmdPacket): bool {.importc: "hal_cmd_pop", cdecl.}

proc send*(chan: var SovereignChannel[IonPacket], pkt: IonPacket) =
  secure_push_packet(chan.ring, pkt)

proc recv*(chan: var SovereignChannel[IonPacket],
    out_pkt: var IonPacket): bool =
  if (cast[uint](chan.ring) and 0b11) != 0:
    return false # Or panic
  return hal_channel_pop(cast[uint64](chan.ring), addr out_pkt)

proc send*(chan: var SovereignChannel[CmdPacket], pkt: CmdPacket) =
  secure_send(chan.ring, pkt)

proc recv*(chan: var SovereignChannel[CmdPacket],
    out_pkt: var CmdPacket): bool =
  return secure_recv_cmd(chan.ring, out_pkt)

# --- 6.1 THE INPUT SURGERY ---
var input_ring_memory: HAL_Ring[IonPacket]
var chan_input*: SovereignChannel[IonPacket] # The Kernel-side Channel

proc ion_init_input*() =
  # Manually Init the Ring (BSS is Alloc)
  input_ring_memory.head = 0
  input_ring_memory.tail = 0
  input_ring_memory.mask = 255 # 256 slots
  
  # Point Channel to Body
  chan_input.ring = addr input_ring_memory
