132 lines
4.6 KiB
C
132 lines
4.6 KiB
C
#pragma once
|
||
// hal_api.h - HAL interface table exported as HalGetApi()
|
||
#include <stdint.h>
|
||
#include <stdbool.h>
|
||
#include <stddef.h>
|
||
#include <ke_status.h>
|
||
|
||
typedef struct BOOT_INFO BOOT_INFO;
|
||
|
||
typedef struct HAL_SCREEN {
|
||
void (*Clear)(uint32_t color);
|
||
void (*PutPixel)(int x, int y, uint32_t color);
|
||
void (*Blit)(int x, int y, int w, int h, const uint32_t* pixels);
|
||
void (*GetSize)(int* width, int* height);
|
||
void (*Scroll)(int lines);
|
||
void (*PutChar)(char c, int x, int y, uint32_t fg, uint32_t bg);
|
||
void (*PrintChar)(char c, uint32_t fg, uint32_t bg);
|
||
// Hardware mouse cursor: save/restore background and blit sprite
|
||
void (*DrawCursor)(int x, int y);
|
||
void (*EraseCursor)(void);
|
||
} HAL_SCREEN;
|
||
|
||
typedef struct HAL_KEYBOARD {
|
||
bool (*Poll)(void);
|
||
uint8_t (*ReadScancode)(void);
|
||
char (*ReadChar)(void);
|
||
uint32_t (*GetModifiers)(void);
|
||
void (*SetCallback)(void (*cb)(uint8_t scancode));
|
||
void (*Isr)(void);
|
||
uint8_t IrqNumber;
|
||
} HAL_KEYBOARD;
|
||
|
||
// HAL_MOUSE – PS/2 auxiliary mouse on IRQ 12.
|
||
// Packet format: standard 3-byte PS/2 (buttons, deltaX, deltaY).
|
||
// Y is already screen-oriented (positive = down) after ISR inversion.
|
||
typedef struct HAL_MOUSE {
|
||
// Non-blocking: returns true if a packet is waiting in the ring buffer.
|
||
bool (*Poll)(void);
|
||
// Non-blocking dequeue. Returns false when buffer is empty.
|
||
// buttons: bit0=left, bit1=right, bit2=middle
|
||
bool (*Read)(int* dx, int* dy, uint8_t* buttons);
|
||
// Callback invoked from ISR context after every completed packet.
|
||
void (*SetCallback)(void (*cb)(int dx, int dy, uint8_t buttons));
|
||
// Absolute cursor position, clamped to screen bounds.
|
||
void (*GetPosition)(int* x, int* y);
|
||
void (*SetPosition)(int x, int y);
|
||
// Update the clamping rectangle (call after mode change / resize).
|
||
void (*SetBounds)(int w, int h);
|
||
// Raw ISR – called by KeIrqDispatch for IRQ 12.
|
||
void (*Isr)(void);
|
||
uint8_t IrqNumber; // always 12
|
||
} HAL_MOUSE;
|
||
|
||
typedef struct HAL_DISK {
|
||
KSTATUS (*ReadSectors)(uint8_t drive, uint64_t lba,
|
||
uint32_t count, void* buf);
|
||
KSTATUS (*WriteSectors)(uint8_t drive, uint64_t lba,
|
||
uint32_t count, const void* buf);
|
||
bool (*DrivePresent)(uint8_t drive);
|
||
uint64_t (*DriveSectorCount)(uint8_t drive);
|
||
} HAL_DISK;
|
||
|
||
typedef struct HAL_TIMER {
|
||
uint64_t (*QueryTicks)(void);
|
||
void (*Stall)(uint32_t us);
|
||
void (*Isr)(void);
|
||
uint8_t IrqNumber;
|
||
} HAL_TIMER;
|
||
|
||
typedef struct HAL_PCI {
|
||
uint32_t (*Read32)(uint8_t bus, uint8_t dev,
|
||
uint8_t fn, uint8_t reg);
|
||
void (*Write32)(uint8_t bus, uint8_t dev,
|
||
uint8_t fn, uint8_t reg, uint32_t val);
|
||
bool (*FindDevice)(uint16_t vendor, uint16_t device,
|
||
uint8_t* bus, uint8_t* dev, uint8_t* fn);
|
||
bool (*FindClass)(uint8_t classCode, uint8_t subclass,
|
||
uint8_t* bus, uint8_t* dev, uint8_t* fn);
|
||
// Returns the IRQ line from PCI config space (register 0x3C, byte 0).
|
||
// Returns 0xFF if no interrupt is wired.
|
||
uint8_t (*GetIrqLine)(uint8_t bus, uint8_t dev, uint8_t fn);
|
||
} HAL_PCI;
|
||
|
||
typedef struct HAL_INTC {
|
||
void (*RouteIrq)(uint8_t irq);
|
||
void (*MaskIrq)(uint8_t irq);
|
||
void (*UnmaskIrq)(uint8_t irq);
|
||
void (*SendEoi)(void);
|
||
} HAL_INTC;
|
||
|
||
typedef struct HAL_POWER {
|
||
// ACPI S5 soft-off. Disables interrupts, writes SLP_EN to PM1a/b_CNT,
|
||
// and halts if firmware doesn't respond. Does not return.
|
||
void (*Shutdown)(void);
|
||
} HAL_POWER;
|
||
|
||
typedef struct HAL_API {
|
||
uint32_t Version;
|
||
void *(*PhysToVirt)(uint64_t physAddr);
|
||
HAL_SCREEN Screen;
|
||
HAL_KEYBOARD Keyboard;
|
||
HAL_MOUSE Mouse;
|
||
HAL_DISK Disk;
|
||
HAL_TIMER Timer;
|
||
HAL_PCI Pci;
|
||
HAL_INTC Intc;
|
||
HAL_POWER Power;
|
||
} HAL_API;
|
||
|
||
typedef struct HAL_INIT_PARAMS {
|
||
// Framebuffer
|
||
uintptr_t FramebufferBase;
|
||
uint32_t FramebufferWidth;
|
||
uint32_t FramebufferHeight;
|
||
uint32_t FramebufferStride;
|
||
|
||
// ACPI
|
||
uintptr_t RsdpPhysical;
|
||
|
||
// Memory
|
||
uint64_t PhysicalWindowBase;
|
||
|
||
// Debug
|
||
int DebugEnabled;
|
||
} HAL_INIT_PARAMS;
|
||
|
||
#define HAL_API_VERSION 1
|
||
|
||
__declspec(dllexport) const HAL_API* HalGetApi(void);
|
||
__declspec(dllexport) void HalRegisterScheduler(void (*pKeSchedulerTick)(void));
|
||
__declspec(dllexport) KSTATUS HalInitialize(const HAL_INIT_PARAMS* params);
|