From 223a3403c027b74edd552c5d3141a5c7bab1ee25 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:20 -0300 Subject: [PATCH] chore: give io read/write functions access to the entire Bus --- src/Bus.zig | 13 ++--- src/bus/io.zig | 127 +++++++++++++++++++++++++------------------------ 2 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index ec3ec77..a1190e7 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -8,6 +8,7 @@ const Iwram = @import("bus/Iwram.zig"); const Ppu = @import("ppu.zig").Ppu; const Scheduler = @import("scheduler.zig").Scheduler; +const io = @import("bus/io.zig"); const Allocator = std.mem.Allocator; const log = std.log.scoped(.Bus); const Self = @This(); @@ -44,7 +45,7 @@ pub fn read32(self: *const Self, addr: u32) u32 { 0x0000_0000...0x0000_3FFF => self.bios.get32(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000), - 0x0400_0000...0x0400_03FE => self.io.read32(addr), + 0x0400_0000...0x0400_03FE => io.read32(self, addr), // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get32(addr - 0x0500_0000), @@ -70,7 +71,7 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { // General Internal Memory 0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word), 0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_0000, word), - 0x0400_0000...0x0400_03FE => self.io.write32(addr, word), + 0x0400_0000...0x0400_03FE => io.write32(self, addr, word), // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.set32(addr - 0x0500_0000, word), @@ -87,7 +88,7 @@ pub fn read16(self: *const Self, addr: u32) u16 { 0x0000_0000...0x0000_3FFF => self.bios.get16(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000), - 0x0400_0000...0x0400_03FE => self.io.read16(addr), + 0x0400_0000...0x0400_03FE => io.read16(self, addr), // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get16(addr - 0x0500_0000), @@ -112,7 +113,7 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { // General Internal Memory 0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword), 0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_0000, halfword), - 0x0400_0000...0x0400_03FE => self.io.write16(addr, halfword), + 0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword), // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.set16(addr - 0x0500_0000, halfword), @@ -129,7 +130,7 @@ pub fn read8(self: *const Self, addr: u32) u8 { 0x0000_0000...0x0000_3FFF => self.bios.get8(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000), - 0x0400_0000...0x0400_03FE => self.io.read8(addr), + 0x0400_0000...0x0400_03FE => io.read8(self, addr), // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get8(addr - 0x0500_0000), @@ -154,7 +155,7 @@ pub fn write8(self: *Self, addr: u32, byte: u8) void { // General Internal Memory 0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte), 0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_0000, byte), - 0x0400_0000...0x0400_03FE => self.io.write8(addr, byte), + 0x0400_0000...0x0400_03FE => io.write8(self, addr, byte), // External Memory (Game Pak) 0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in Game Pak SRAM", .{ byte, addr }), diff --git a/src/bus/io.zig b/src/bus/io.zig index 134fedb..277c7b2 100644 --- a/src/bus/io.zig +++ b/src/bus/io.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Bit = @import("bitfield").Bit; const Bitfield = @import("bitfield").Bitfield; +const Bus = @import("../Bus.zig"); pub const Io = struct { const Self = @This(); @@ -66,69 +67,6 @@ pub const Io = struct { .keyinput = .{ .raw = 0x03FF }, }; } - - pub fn read32(self: *const Self, addr: u32) u32 { - return switch (addr) { - 0x0400_0000 => self.dispcnt.raw, - 0x0400_0004 => self.dispstat.raw, - 0x0400_0006 => self.vcount.raw, - 0x0400_0200 => self.ie.raw, - 0x0400_0208 => @boolToInt(self.ime), - else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}), - }; - } - - pub fn write32(self: *Self, addr: u32, word: u32) void { - switch (addr) { - 0x0400_0000 => self.dispcnt.raw = @truncate(u16, word), - 0x0400_0200 => self.ie.raw = @truncate(u16, word), - 0x0400_0208 => self.ime = word & 1 == 1, - else => std.debug.panic("[I/O:32] tried to write 0x{X:} to 0x{X:}", .{ word, addr }), - } - } - - pub fn read16(self: *const Self, addr: u32) u16 { - return switch (addr) { - 0x0400_0000 => self.dispcnt.raw, - 0x0400_0004 => self.dispstat.raw, - 0x0400_0006 => self.vcount.raw, - 0x0400_0130 => self.keyinput.raw, - 0x0400_0200 => self.ie.raw, - 0x0400_0208 => @boolToInt(self.ime), - else => std.debug.panic("[I/O:16] tried to read from {X:}", .{addr}), - }; - } - - pub fn write16(self: *Self, addr: u32, halfword: u16) void { - switch (addr) { - 0x0400_0000 => self.dispcnt.raw = halfword, - 0x0400_0004 => self.dispstat.raw = halfword, - 0x0400_0008 => self.bg0cnt.raw = halfword, - 0x0400_0010 => self.bg0hofs.raw = halfword, - 0x0400_0012 => self.bg0vofs.raw = halfword, - 0x0400_0200 => self.ie.raw = halfword, - 0x0400_0202 => self.irq.raw = halfword, - 0x0400_0208 => self.ime = halfword & 1 == 1, - else => std.debug.panic("[I/O:16] tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }), - } - } - - pub fn read8(self: *const Self, addr: u32) u8 { - return switch (addr) { - 0x0400_0000 => @truncate(u8, self.dispcnt.raw), - 0x0400_0004 => @truncate(u8, self.dispstat.raw), - 0x0400_0200 => @truncate(u8, self.ie.raw), - 0x0400_0006 => @truncate(u8, self.vcount.raw), - else => std.debug.panic("[I/O:8] tried to read from {X:}", .{addr}), - }; - } - - pub fn write8(self: *Self, addr: u32, byte: u8) void { - switch (addr) { - 0x0400_0208 => self.ime = byte & 1 == 1, - else => std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr }), - } - } }; /// Read / Write @@ -231,3 +169,66 @@ const InterruptRequest = extern union { game_pak: Bit(u16, 13), raw: u16, }; + +pub fn read32(bus: *const Bus, addr: u32) u32 { + return switch (addr) { + 0x0400_0000 => bus.io.dispcnt.raw, + 0x0400_0004 => bus.io.dispstat.raw, + 0x0400_0006 => bus.io.vcount.raw, + 0x0400_0200 => bus.io.ie.raw, + 0x0400_0208 => @boolToInt(bus.io.ime), + else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}), + }; +} + +pub fn write32(bus: *Bus, addr: u32, word: u32) void { + switch (addr) { + 0x0400_0000 => bus.io.dispcnt.raw = @truncate(u16, word), + 0x0400_0200 => bus.io.ie.raw = @truncate(u16, word), + 0x0400_0208 => bus.io.ime = word & 1 == 1, + else => std.debug.panic("[I/O:32] tried to write 0x{X:} to 0x{X:}", .{ word, addr }), + } +} + +pub fn read16(bus: *const Bus, addr: u32) u16 { + return switch (addr) { + 0x0400_0000 => bus.io.dispcnt.raw, + 0x0400_0004 => bus.io.dispstat.raw, + 0x0400_0006 => bus.io.vcount.raw, + 0x0400_0130 => bus.io.keyinput.raw, + 0x0400_0200 => bus.io.ie.raw, + 0x0400_0208 => @boolToInt(bus.io.ime), + else => std.debug.panic("[I/O:16] tried to read from {X:}", .{addr}), + }; +} + +pub fn write16(bus: *Bus, addr: u32, halfword: u16) void { + switch (addr) { + 0x0400_0000 => bus.io.dispcnt.raw = halfword, + 0x0400_0004 => bus.io.dispstat.raw = halfword, + 0x0400_0008 => bus.io.bg0cnt.raw = halfword, + 0x0400_0010 => bus.io.bg0hofs.raw = halfword, + 0x0400_0012 => bus.io.bg0vofs.raw = halfword, + 0x0400_0200 => bus.io.ie.raw = halfword, + 0x0400_0202 => bus.io.irq.raw = halfword, + 0x0400_0208 => bus.io.ime = halfword & 1 == 1, + else => std.debug.panic("[I/O:16] tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }), + } +} + +pub fn read8(bus: *const Bus, addr: u32) u8 { + return switch (addr) { + 0x0400_0000 => @truncate(u8, bus.io.dispcnt.raw), + 0x0400_0004 => @truncate(u8, bus.io.dispstat.raw), + 0x0400_0200 => @truncate(u8, bus.io.ie.raw), + 0x0400_0006 => @truncate(u8, bus.io.vcount.raw), + else => std.debug.panic("[I/O:8] tried to read from {X:}", .{addr}), + }; +} + +pub fn write8(self: *Bus, addr: u32, byte: u8) void { + switch (addr) { + 0x0400_0208 => self.io.ime = byte & 1 == 1, + else => std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr }), + } +}