Compare commits

..

No commits in common. "e5ab8b51a94344130da0b92f28c5da300f2d704d" and "9e2e8c3d1adc20568b218692312b48897feb52e6" have entirely different histories.

2 changed files with 94 additions and 120 deletions

View File

@ -58,7 +58,7 @@ pub fn read32(self: *const Self, addr: u32) u32 {
0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000),
else => blk: { else => blk: {
log.warn("Tried to read from 0x{X:0>8}", .{addr}); log.warn("32-bit read from 0x{X:0>8}", .{addr});
break :blk 0x0000_0000; break :blk 0x0000_0000;
}, },
}; };
@ -78,7 +78,7 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word),
else => log.warn("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), else => log.warn("32-bit write of 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
} }
} }
@ -100,7 +100,7 @@ pub fn read16(self: *const Self, addr: u32) u16 {
0x0A00_0000...0x0BFF_FFFF => self.pak.get16(addr - 0x0A00_0000), 0x0A00_0000...0x0BFF_FFFF => self.pak.get16(addr - 0x0A00_0000),
0x0C00_0000...0x0DFF_FFFF => self.pak.get16(addr - 0x0C00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get16(addr - 0x0C00_0000),
else => std.debug.panic("Tried to read from 0x{X:0>8}", .{addr}), else => std.debug.panic("16-bit read from 0x{X:0>8}", .{addr}),
}; };
} }
@ -117,7 +117,7 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword),
else => std.debug.panic("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }), else => std.debug.panic("16-bit write of 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
} }
} }
@ -138,9 +138,9 @@ pub fn read8(self: *const Self, addr: u32) u8 {
0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), 0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000),
0x0A00_0000...0x0BFF_FFFF => self.pak.get8(addr - 0x0A00_0000), 0x0A00_0000...0x0BFF_FFFF => self.pak.get8(addr - 0x0A00_0000),
0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000),
0x0E00_0000...0x0E00_FFFF => std.debug.panic("Read from 0x{X:0>2} in Game Pak SRAM", .{addr}), 0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] read from 0x{X:} in Game Pak SRAM", .{addr}),
else => std.debug.panic("Tried to read from 0x{X:0>2}", .{addr}), else => std.debug.panic("8-bit read from 0x{X:0>8}", .{addr}),
}; };
} }
@ -150,10 +150,9 @@ pub fn write8(self: *Self, addr: u32, byte: u8) void {
0x0200_0000...0x02FF_FFFF => self.ewram.set8(addr & 0x3FFFF, byte), 0x0200_0000...0x02FF_FFFF => self.ewram.set8(addr & 0x3FFFF, byte),
0x0300_0000...0x03FF_FFFF => self.iwram.set8(addr & 0x7FFF, byte), 0x0300_0000...0x03FF_FFFF => self.iwram.set8(addr & 0x7FFF, byte),
0x0400_0000...0x0400_03FE => io.write8(self, addr, byte), 0x0400_0000...0x0400_03FE => io.write8(self, addr, byte),
0x0400_0410 => log.info("Wrote 0x{X:0>2} to 0x{X:0>8}. Ignored", .{ byte, addr }),
// External Memory (Game Pak) // External Memory (Game Pak)
0x0E00_0000...0x0E00_FFFF => log.err("Wrote 0x{X:0>2} to 0x{X:0>8} 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 }),
else => std.debug.panic("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), else => std.debug.panic("8-bit write of 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
} }
} }

View File

@ -4,8 +4,6 @@ const Bit = @import("bitfield").Bit;
const Bitfield = @import("bitfield").Bitfield; const Bitfield = @import("bitfield").Bitfield;
const Bus = @import("../Bus.zig"); const Bus = @import("../Bus.zig");
const log = std.log.scoped(.@"I/O");
pub const Io = struct { pub const Io = struct {
const Self = @This(); const Self = @This();
@ -13,7 +11,6 @@ pub const Io = struct {
ime: bool, ime: bool,
ie: InterruptEnable, ie: InterruptEnable,
irq: InterruptRequest, irq: InterruptRequest,
postflg: PostFlag,
is_halted: bool, is_halted: bool,
keyinput: KeyInput, keyinput: KeyInput,
@ -23,120 +20,12 @@ pub const Io = struct {
.ime = false, .ime = false,
.ie = .{ .raw = 0x0000 }, .ie = .{ .raw = 0x0000 },
.irq = .{ .raw = 0x0000 }, .irq = .{ .raw = 0x0000 },
.postflg = .{ .raw = 0x00 },
.keyinput = .{ .raw = 0x03FF }, .keyinput = .{ .raw = 0x03FF },
.is_halted = false, .is_halted = false,
}; };
} }
}; };
pub fn read32(bus: *const Bus, addr: u32) u32 {
return switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw,
0x0400_0004 => bus.ppu.dispstat.raw,
0x0400_0006 => bus.ppu.vcount.raw,
0x0400_0200 => bus.io.ie.raw,
0x0400_0208 => @boolToInt(bus.io.ime),
else => std.debug.panic("Tried to read from {X:0>8}", .{addr}),
};
}
pub fn write32(bus: *Bus, addr: u32, word: u32) void {
switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw = @truncate(u16, word),
0x0400_0004 => {
bus.ppu.dispstat.raw = @truncate(u16, word);
bus.ppu.vcount.raw = @truncate(u16, word >> 16);
},
0x0400_0008 => {
bus.ppu.bg[0].cnt.raw = @truncate(u16, word);
bus.ppu.bg[1].cnt.raw = @truncate(u16, word >> 16);
},
0x0400_000C => {
bus.ppu.bg[2].cnt.raw = @truncate(u16, word);
bus.ppu.bg[3].cnt.raw = @truncate(u16, word >> 16);
},
0x0400_0010 => {
bus.ppu.bg[0].hofs.raw = @truncate(u16, word);
bus.ppu.bg[0].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0014 => {
bus.ppu.bg[1].hofs.raw = @truncate(u16, word);
bus.ppu.bg[1].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0018 => {
bus.ppu.bg[2].hofs.raw = @truncate(u16, word);
bus.ppu.bg[2].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_001C => {
bus.ppu.bg[3].hofs.raw = @truncate(u16, word);
bus.ppu.bg[3].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0200 => bus.io.ie.raw = @truncate(u16, word),
0x0400_0204 => log.warn("Wrote 0x{X:0>8} to WAITCNT", .{word}),
0x0400_0208 => bus.io.ime = word & 1 == 1,
else => std.debug.panic("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
}
}
pub fn read16(bus: *const Bus, addr: u32) u16 {
return switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw,
0x0400_0004 => bus.ppu.dispstat.raw,
0x0400_0006 => bus.ppu.vcount.raw,
0x0400_0130 => bus.io.keyinput.raw,
0x0400_0200 => bus.io.ie.raw,
0x0400_0208 => @boolToInt(bus.io.ime),
else => std.debug.panic("Tried to read from {X:0>4}", .{addr}),
};
}
pub fn write16(bus: *Bus, addr: u32, halfword: u16) void {
switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw = halfword,
0x0400_0004 => bus.ppu.dispstat.raw = halfword,
0x0400_0008...0x0400_000F => bus.ppu.bg[addr & 0x7].cnt.raw = halfword,
0x0400_0010 => bus.ppu.bg[0].hofs.raw = halfword, // TODO: Don't write out every HOFS / VOFS?
0x0400_0012 => bus.ppu.bg[0].vofs.raw = halfword,
0x0400_0014 => bus.ppu.bg[1].hofs.raw = halfword,
0x0400_0016 => bus.ppu.bg[1].vofs.raw = halfword,
0x0400_0018 => bus.ppu.bg[2].hofs.raw = halfword,
0x0400_001A => bus.ppu.bg[2].vofs.raw = halfword,
0x0400_001C => bus.ppu.bg[3].hofs.raw = halfword,
0x0400_001E => bus.ppu.bg[3].vofs.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("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
}
}
pub fn read8(bus: *const Bus, addr: u32) u8 {
return switch (addr) {
0x0400_0000 => @truncate(u8, bus.ppu.dispcnt.raw),
0x0400_0004 => @truncate(u8, bus.ppu.dispstat.raw),
0x0400_0200 => @truncate(u8, bus.io.ie.raw),
0x0400_0300 => bus.io.postflg.raw,
0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw),
else => std.debug.panic("Tried to read from 0x{X:0>8}", .{addr}),
};
}
pub fn write8(self: *Bus, addr: u32, byte: u8) void {
switch (addr) {
0x0400_0208 => self.io.ime = byte & 1 == 1,
0x0400_0301 => self.io.is_halted = byte >> 7 & 1 == 0, // TODO: Implement Stop?
else => std.debug.panic("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
}
}
/// Read / Write
pub const PostFlag = extern union {
/// 0 if First Boot, 1 if a Reset has been done
not_first_boot: Bit(u8, 0),
raw: u8,
};
/// Read / Write /// Read / Write
pub const DisplayControl = extern union { pub const DisplayControl = extern union {
bg_mode: Bitfield(u16, 0, 3), bg_mode: Bitfield(u16, 0, 3),
@ -240,3 +129,89 @@ 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.ppu.dispcnt.raw,
0x0400_0004 => bus.ppu.dispstat.raw,
0x0400_0006 => bus.ppu.vcount.raw,
0x0400_0200 => bus.io.ie.raw,
0x0400_0208 => @boolToInt(bus.io.ime),
else => std.debug.panic("[Io:32] tried to read from {X:}", .{addr}),
};
}
pub fn write32(bus: *Bus, addr: u32, word: u32) void {
switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw = @truncate(u16, word),
0x0400_0010 => {
bus.ppu.bg[0].hofs.raw = @truncate(u16, word);
bus.ppu.bg[0].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0014 => {
bus.ppu.bg[1].hofs.raw = @truncate(u16, word);
bus.ppu.bg[1].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0018 => {
bus.ppu.bg[2].hofs.raw = @truncate(u16, word);
bus.ppu.bg[2].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_001C => {
bus.ppu.bg[3].hofs.raw = @truncate(u16, word);
bus.ppu.bg[3].vofs.raw = @truncate(u16, word >> 16);
},
0x0400_0200 => bus.io.ie.raw = @truncate(u16, word),
0x0400_0208 => bus.io.ime = word & 1 == 1,
else => std.debug.panic("[Io: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.ppu.dispcnt.raw,
0x0400_0004 => bus.ppu.dispstat.raw,
0x0400_0006 => bus.ppu.vcount.raw,
0x0400_0130 => bus.io.keyinput.raw,
0x0400_0200 => bus.io.ie.raw,
0x0400_0208 => @boolToInt(bus.io.ime),
else => std.debug.panic("[Io:16] tried to read from {X:}", .{addr}),
};
}
pub fn write16(bus: *Bus, addr: u32, halfword: u16) void {
switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw = halfword,
0x0400_0004 => bus.ppu.dispstat.raw = halfword,
0x0400_0008...0x0400_000F => bus.ppu.bg[addr & 0x7].cnt.raw = halfword,
0x0400_0010 => bus.ppu.bg[0].hofs.raw = halfword, // TODO: Don't write out every HOFS / VOFS?
0x0400_0012 => bus.ppu.bg[0].vofs.raw = halfword,
0x0400_0014 => bus.ppu.bg[1].hofs.raw = halfword,
0x0400_0016 => bus.ppu.bg[1].vofs.raw = halfword,
0x0400_0018 => bus.ppu.bg[2].hofs.raw = halfword,
0x0400_001A => bus.ppu.bg[2].vofs.raw = halfword,
0x0400_001C => bus.ppu.bg[3].hofs.raw = halfword,
0x0400_001E => bus.ppu.bg[3].vofs.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("[Io: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.ppu.dispcnt.raw),
0x0400_0004 => @truncate(u8, bus.ppu.dispstat.raw),
0x0400_0200 => @truncate(u8, bus.io.ie.raw),
0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw),
else => std.debug.panic("[Io: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,
0x0400_0301 => self.io.is_halted = byte >> 7 & 1 == 0, // TODO: Implement Stop?
else => std.debug.panic("[Io:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr }),
}
}