From 90d5c19e0159776a2f45e7e56aad982885de7408 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 29 Jun 2023 23:12:08 -0500 Subject: [PATCH] feat: implement debug i/o in Bus Interface --- src/lib.zig | 109 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index cd73a37..fae4590 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -45,6 +45,14 @@ pub const Bus = struct { write16: *const fn (ptr: *anyopaque, address: u32, value: u16) void, write32: *const fn (ptr: *anyopaque, address: u32, value: u32) void, + dbg_read8: *const fn (ptr: *anyopaque, address: u32) u8, + dbg_read16: *const fn (ptr: *anyopaque, address: u32) u16, + dbg_read32: *const fn (ptr: *anyopaque, address: u32) u32, + + dbg_write8: *const fn (ptr: *anyopaque, address: u32, value: u8) void, + dbg_write16: *const fn (ptr: *anyopaque, address: u32, value: u16) void, + dbg_write32: *const fn (ptr: *anyopaque, address: u32, value: u32) void, + reset: *const fn (ptr: *anyopaque) void, }; @@ -89,6 +97,36 @@ pub const Bus = struct { self.write(u32, address, value); } + fn dbgRead8(ptr: *anyopaque, address: u32) u8 { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + return self.dbgRead(u8, address); + } + + fn dbgRead16(ptr: *anyopaque, address: u32) u16 { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + return self.dbgRead(u16, address); + } + + fn dbgRead32(ptr: *anyopaque, address: u32) u32 { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + return self.dbgRead(u32, address); + } + + fn dbgWrite8(ptr: *anyopaque, address: u32, value: u8) void { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + self.dbgWrite(u8, address, value); + } + + fn dbgWrite16(ptr: *anyopaque, address: u32, value: u16) void { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + self.dbgWrite(u16, address, value); + } + + fn dbgWrite32(ptr: *anyopaque, address: u32, value: u32) void { + const self = @ptrCast(P, @alignCast(alignment, ptr)); + self.dbgWrite(u32, address, value); + } + fn reset(ptr: *anyopaque) void { const self = @ptrCast(P, @alignCast(alignment, ptr)); self.reset(); @@ -106,6 +144,14 @@ pub const Bus = struct { .write16 = impl.write16, .write32 = impl.write32, + .dbg_read8 = impl.dbgRead8, + .dbg_read16 = impl.dbgRead16, + .dbg_read32 = impl.dbgRead32, + + .dbg_write8 = impl.dbgWrite8, + .dbg_write16 = impl.dbgWrite16, + .dbg_write32 = impl.dbgWrite32, + .reset = impl.reset, }, }; @@ -130,16 +176,21 @@ pub const Bus = struct { } pub fn dbgRead(self: @This(), comptime T: type, address: u32) T { - _ = address; - _ = self; - @panic("TODO: Implement Debug Reads via Bus Interface"); + return switch (T) { + u32 => self.vtable.dbg_read32(self.ptr, address), + u16 => self.vtable.dbg_read16(self.ptr, address), + u8 => self.vtable.dbg_read8(self.ptr, address), + else => @compileError("TODO: Fill this in"), + }; } pub fn dbgWrite(self: @This(), comptime T: type, address: u32, value: T) void { - _ = self; - _ = value; - _ = address; - @panic("TODO: Implement Debug Writes via Bus Interface"); + switch (T) { + u32 => self.vtable.dbg_write32(self.ptr, address, value), + u16 => self.vtable.dbg_write16(self.ptr, address, value), + u8 => self.vtable.dbg_write8(self.ptr, address, value), + else => @compileError("TODO: Fill this in"), + } } pub fn reset(self: @This()) void { @@ -206,6 +257,17 @@ const ExampleBus = struct { _ = value; _ = address; } + pub fn dbgRead(self: *@This(), comptime T: type, address: u32) T { + _ = self; + _ = address; + return 0; + } + + pub fn dbgWrite(self: *@This(), comptime T: type, address: u32, value: T) void { + _ = self; + _ = value; + _ = address; + } pub fn reset(self: *@This()) void { self._ = 0; @@ -224,6 +286,38 @@ const ExampleScheduler = struct { } }; +test "create IBus" { + var bus_impl = ExampleBus{}; + const iface = Bus.init(&bus_impl); + _ = iface; +} + +test "call IBus reads" { + var bus_impl = ExampleBus{}; + const iface = Bus.init(&bus_impl); + + _ = iface.read(u32, 0x0000_0000); + _ = iface.read(u16, 0x0000_0000); + _ = iface.read(u8, 0x0000_0000); + + _ = iface.dbgRead(u32, 0x0000_0000); + _ = iface.dbgRead(u16, 0x0000_0000); + _ = iface.dbgRead(u8, 0x0000_0000); +} + +test "call IBus writes" { + var bus_impl = ExampleBus{}; + const iface = Bus.init(&bus_impl); + + _ = iface.write(u32, 0x0000_0000, 0x0000_0000); + _ = iface.write(u16, 0x0000_0000, 0x0000); + _ = iface.write(u8, 0x0000_0000, 0x00); + + _ = iface.dbgWrite(u32, 0x0000_0000, 0x0000_0000); + _ = iface.dbgWrite(u16, 0x0000_0000, 0x0000); + _ = iface.dbgWrite(u8, 0x0000_0000, 0x00); +} + test "create ARMv4T interface" { var bus_impl = ExampleBus{}; var scheduler_impl = ExampleScheduler{}; @@ -242,6 +336,7 @@ test "create ARMv4T interface" { icpu.reset(); icpu.step(); + // TODO: call icpu.panic() } test "create ARMv5TE interface" {