aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-01-28 13:41:32 +0530
committerBobby <[email protected]>2026-01-28 13:41:32 +0530
commit78e8df49c4b8b4331e90bc1575e994517459efe8 (patch)
tree11e19364562dbec0b226d908bb6c5828b351b9e6
parentf95c1af73c986162a452d375b45ce085784e62c1 (diff)
downloadakiba-78e8df49c4b8b4331e90bc1575e994517459efe8.tar.xz
akiba-78e8df49c4b8b4331e90bc1575e994517459efe8.zip
refactor: Replace inline assembly with I/O functions in ATA, keyboard, and PCI drivers
-rw-r--r--mirai/drivers/ata.zig41
-rw-r--r--mirai/drivers/keyboard.zig24
-rw-r--r--mirai/drivers/pci.zig39
3 files changed, 20 insertions, 84 deletions
diff --git a/mirai/drivers/ata.zig b/mirai/drivers/ata.zig
index 453d2a2..1e4d4ec 100644
--- a/mirai/drivers/ata.zig
+++ b/mirai/drivers/ata.zig
@@ -1,5 +1,6 @@
//! ATA PIO block device driver
+const io = @import("../asm/io.zig");
const serial = @import("serial.zig");
pub const SECTOR_SIZE: usize = 512;
@@ -21,36 +22,14 @@ const STATUS_DRDY: u8 = 0x40;
const STATUS_DRQ: u8 = 0x08;
const STATUS_ERR: u8 = 0x01;
-fn outb(port: u16, value: u8) void {
- asm volatile ("outb %[value], %[port]"
- :
- : [value] "{al}" (value),
- [port] "{dx}" (port),
- );
-}
-
-fn inb(port: u16) u8 {
- return asm volatile ("inb %[port], %[result]"
- : [result] "={al}" (-> u8),
- : [port] "{dx}" (port),
- );
-}
-
-fn inw(port: u16) u16 {
- return asm volatile ("inw %[port], %[result]"
- : [result] "={ax}" (-> u16),
- : [port] "{dx}" (port),
- );
-}
-
fn wait_ready() void {
- while ((inb(ATA_STATUS) & STATUS_BSY) != 0) {}
+ while ((io.read_port_byte(ATA_STATUS) & STATUS_BSY) != 0) {}
}
fn wait_data_ready() bool {
var timeout: u32 = 0;
while (timeout < 10000) : (timeout += 1) {
- const status = inb(ATA_STATUS);
+ const status = io.read_port_byte(ATA_STATUS);
if ((status & STATUS_ERR) != 0) return false;
if ((status & STATUS_DRQ) != 0) return true;
}
@@ -74,12 +53,12 @@ pub const BlockDevice = struct {
wait_ready();
const drive_bits: u8 = 0xE0 | (self.disk_id << 4) | @as(u8, @truncate((lba >> 24) & 0x0F));
- outb(ATA_DRIVE_SELECT, drive_bits);
- outb(ATA_SECTOR_COUNT, 1);
- outb(ATA_LBA_LOW, @as(u8, @truncate(lba)));
- outb(ATA_LBA_MID, @as(u8, @truncate(lba >> 8)));
- outb(ATA_LBA_HIGH, @as(u8, @truncate(lba >> 16)));
- outb(ATA_COMMAND, CMD_READ_SECTORS);
+ io.write_port_byte(ATA_DRIVE_SELECT, drive_bits);
+ io.write_port_byte(ATA_SECTOR_COUNT, 1);
+ io.write_port_byte(ATA_LBA_LOW, @as(u8, @truncate(lba)));
+ io.write_port_byte(ATA_LBA_MID, @as(u8, @truncate(lba >> 8)));
+ io.write_port_byte(ATA_LBA_HIGH, @as(u8, @truncate(lba >> 16)));
+ io.write_port_byte(ATA_COMMAND, CMD_READ_SECTORS);
if (!wait_data_ready()) {
serial.print("ERROR: ATA read timeout\r\n");
@@ -88,7 +67,7 @@ pub const BlockDevice = struct {
var i: usize = 0;
while (i < SECTOR_SIZE) : (i += 2) {
- const word = inw(ATA_DATA);
+ const word = io.read_port_word(ATA_DATA);
buffer[i] = @as(u8, @truncate(word));
buffer[i + 1] = @as(u8, @truncate(word >> 8));
}
diff --git a/mirai/drivers/keyboard.zig b/mirai/drivers/keyboard.zig
index bee442a..7f7d22b 100644
--- a/mirai/drivers/keyboard.zig
+++ b/mirai/drivers/keyboard.zig
@@ -1,5 +1,6 @@
//! PS/2 Keyboard Driver
+const io = @import("../asm/io.zig");
const serial = @import("serial.zig");
const sensei = @import("../kata/sensei.zig");
@@ -63,38 +64,23 @@ const SCANCODE_LCTRL: u8 = 0x1D;
const SCANCODE_LALT: u8 = 0x38;
const SCANCODE_CAPS: u8 = 0x3A;
-fn inb(port: u16) u8 {
- return asm volatile ("inb %[port], %[result]"
- : [result] "={al}" (-> u8),
- : [port] "{dx}" (port),
- );
-}
-
-fn outb(port: u16, value: u8) void {
- asm volatile ("outb %[value], %[port]"
- :
- : [value] "{al}" (value),
- [port] "{dx}" (port),
- );
-}
-
pub fn init() void {
serial.print("Initializing PS/2 keyboard...\n");
// Clear keyboard buffer
- while ((inb(KEYBOARD_STATUS_PORT) & 0x01) != 0) {
- _ = inb(KEYBOARD_DATA_PORT);
+ while ((io.read_port_byte(KEYBOARD_STATUS_PORT) & 0x01) != 0) {
+ _ = io.read_port_byte(KEYBOARD_DATA_PORT);
}
serial.print("Keyboard ready\n");
}
export fn keyboard_handler() void {
- const scancode = inb(KEYBOARD_DATA_PORT);
+ const scancode = io.read_port_byte(KEYBOARD_DATA_PORT);
handle_scancode(scancode);
// Send EOI to PIC
- outb(0x20, 0x20);
+ io.write_port_byte(0x20, 0x20);
}
fn handle_scancode(scancode: u8) void {
diff --git a/mirai/drivers/pci.zig b/mirai/drivers/pci.zig
index 0be7659..c8b0b8d 100644
--- a/mirai/drivers/pci.zig
+++ b/mirai/drivers/pci.zig
@@ -1,5 +1,6 @@
//! PCI (Peripheral Component Interconnect) bus enumeration
+const io = @import("../asm/io.zig");
const serial = @import("serial.zig");
pub const PCIDevice = struct {
@@ -29,36 +30,6 @@ const MAX_DEVICES = 64;
var devices: [MAX_DEVICES]PCIDevice = undefined;
var device_count: usize = 0;
-fn outl(port: u16, value: u32) void {
- asm volatile ("outl %[value], %[port]"
- :
- : [value] "{eax}" (value),
- [port] "{dx}" (port),
- );
-}
-
-fn inl(port: u16) u32 {
- return asm volatile ("inl %[port], %[result]"
- : [result] "={eax}" (-> u32),
- : [port] "{dx}" (port),
- );
-}
-
-fn outw(port: u16, value: u16) void {
- asm volatile ("outw %[value], %[port]"
- :
- : [value] "{ax}" (value),
- [port] "{dx}" (port),
- );
-}
-
-fn inw(port: u16) u16 {
- return asm volatile ("inw %[port], %[result]"
- : [result] "={ax}" (-> u16),
- : [port] "{dx}" (port),
- );
-}
-
fn pci_config_read_u32(bus: u8, device: u8, function: u8, offset: u8) u32 {
const address: u32 = (@as(u32, 1) << 31) |
(@as(u32, bus) << 16) |
@@ -66,8 +37,8 @@ fn pci_config_read_u32(bus: u8, device: u8, function: u8, offset: u8) u32 {
(@as(u32, function) << 8) |
(@as(u32, offset) & 0xFC);
- outl(PCI_CONFIG_ADDRESS, address);
- return inl(PCI_CONFIG_DATA);
+ io.write_port_long(PCI_CONFIG_ADDRESS, address);
+ return io.read_port_long(PCI_CONFIG_DATA);
}
fn pci_config_read_u16(bus: u8, device: u8, function: u8, offset: u8) u16 {
@@ -89,8 +60,8 @@ fn pci_config_write_u32(bus: u8, device: u8, function: u8, offset: u8, value: u3
(@as(u32, function) << 8) |
(@as(u32, offset) & 0xFC);
- outl(PCI_CONFIG_ADDRESS, address);
- outl(PCI_CONFIG_DATA, value);
+ io.write_port_long(PCI_CONFIG_ADDRESS, address);
+ io.write_port_long(PCI_CONFIG_DATA, value);
}
fn pci_config_write_u16(bus: u8, device: u8, function: u8, offset: u8, value: u16) void {