chore: give io read/write functions access to the entire Bus

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-02-13 04:13:06 -04:00
parent ec25a9aae4
commit 31fa06ac4a
2 changed files with 71 additions and 69 deletions

View File

@ -8,6 +8,7 @@ const Iwram = @import("bus/Iwram.zig");
const Ppu = @import("ppu.zig").Ppu; const Ppu = @import("ppu.zig").Ppu;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const io = @import("bus/io.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Bus); const log = std.log.scoped(.Bus);
const Self = @This(); const Self = @This();
@ -44,7 +45,7 @@ pub fn read32(self: *const Self, addr: u32) u32 {
0x0000_0000...0x0000_3FFF => self.bios.get32(addr), 0x0000_0000...0x0000_3FFF => self.bios.get32(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000), 0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_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 // Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get32(addr - 0x0500_0000), 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 // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word), 0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word),
0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_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 // Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.set32(addr - 0x0500_0000, word), 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), 0x0000_0000...0x0000_3FFF => self.bios.get16(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000), 0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_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 // Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get16(addr - 0x0500_0000), 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 // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword), 0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword),
0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_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 // Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.set16(addr - 0x0500_0000, halfword), 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), 0x0000_0000...0x0000_3FFF => self.bios.get8(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000), 0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_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 // Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get8(addr - 0x0500_0000), 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 // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte), 0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte),
0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_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) // 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 }), 0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in Game Pak SRAM", .{ byte, addr }),

View File

@ -2,6 +2,7 @@ const std = @import("std");
const Bit = @import("bitfield").Bit; const Bit = @import("bitfield").Bit;
const Bitfield = @import("bitfield").Bitfield; const Bitfield = @import("bitfield").Bitfield;
const Bus = @import("../Bus.zig");
pub const Io = struct { pub const Io = struct {
const Self = @This(); const Self = @This();
@ -66,69 +67,6 @@ pub const Io = struct {
.keyinput = .{ .raw = 0x03FF }, .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 /// Read / Write
@ -231,3 +169,66 @@ const InterruptRequest = extern union {
game_pak: Bit(u16, 13), game_pak: Bit(u16, 13),
raw: u16, 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 }),
}
}