/* CubeCactusOS Kernel - Header File
 * Core kernel definitions and structures
 */

#ifndef KERNEL_H
#define KERNEL_H

#include <stdint.h>
#include <stddef.h>

/* Kernel configuration */
#define MAX_PROCESSES 256
#define MAX_MESSAGES 1024
#define KERNEL_STACK_SIZE 16384

/* System call numbers (MINIX3-inspired) */
#define SYS_EXIT        1
#define SYS_FORK        2
#define SYS_READ        3
#define SYS_WRITE       4
#define SYS_OPEN        5
#define SYS_CLOSE       6
#define SYS_EXEC        11
#define SYS_KILL        37
#define SYS_SENDREC     100  /* MINIX IPC */

/* Process states */
typedef enum {
    PROC_UNUSED,
    PROC_RUNNING,
    PROC_READY,
    PROC_BLOCKED,
    PROC_ZOMBIE
} proc_state_t;

/* Process structure */
typedef struct process {
    int pid;                    /* Process ID */
    proc_state_t state;         /* Process state */
    int parent_pid;             /* Parent process ID */
    uint32_t priority;          /* Process priority */
    void* stack_ptr;            /* Stack pointer */
    void* page_table;           /* Page table pointer */
    uint32_t quantum;           /* Time quantum */
} process_t;

/* Message structure (MINIX3 IPC) */
typedef struct message {
    int source;                 /* Sender process ID */
    int type;                   /* Message type */
    union {
        struct {
            int i1, i2, i3;
            void *p1, *p2;
        } m1;
        uint8_t data[56];       /* Raw message data */
    } m;
} message_t;

/* Kernel functions */
void kernel_panic(const char* message);
int sys_call(int call_num, ...);

/* Process management */
int create_process(void (*entry)(), int priority);
void schedule();
void switch_process(process_t* next);

/* IPC functions */
int send(int dest, message_t* msg);
int receive(int source, message_t* msg);
int sendrec(int dest, message_t* msg);

/* Memory management */
void* kalloc(size_t size);
void kfree(void* ptr);

/* I/O functions */
void outb(uint16_t port, uint8_t value);
uint8_t inb(uint16_t port);

#endif /* KERNEL_H */
