chore: don't panic on unknown bus and io writes/reads

This will lead to emulation bugs due to devices I've yet to implement but by
doing this a lot of games become playable "by default" such as Doom or
Kirby: Nightmare in Dream Land.

When implementing feature and/or debuggin make sure to set:
panic_on_und_bus and panic_on_und_io to true so that the emu crashes
on unknown reads/writes
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-03-13 05:39:09 -03:00
parent 4a76611fca
commit 017ec407f5
2 changed files with 30 additions and 18 deletions

View File

@ -13,6 +13,8 @@ const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Bus); const log = std.log.scoped(.Bus);
const Self = @This(); const Self = @This();
const panic_on_und_bus: bool = false;
pak: GamePak, pak: GamePak,
bios: Bios, bios: Bios,
ppu: Ppu, ppu: Ppu,
@ -57,7 +59,7 @@ pub fn read32(self: *const Self, addr: u32) u32 {
0x0A00_0000...0x0BFF_FFFF => self.pak.get32(addr - 0x0A00_0000), 0x0A00_0000...0x0BFF_FFFF => self.pak.get32(addr - 0x0A00_0000),
0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000),
else => failedRead("Tried to read from 0x{X:0>8}", .{addr}), else => undRead("Tried to read from 0x{X:0>8}", .{addr}),
}; };
} }
@ -75,7 +77,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 => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
} }
} }
@ -97,7 +99,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 => undRead("Tried to read from 0x{X:0>8}", .{addr}),
}; };
} }
@ -114,7 +116,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 => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
} }
} }
@ -137,7 +139,7 @@ pub fn read8(self: *const Self, addr: u32) u8 {
0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000),
0x0E00_0000...0x0E00_FFFF => self.pak.sram.get8(addr - 0x0E00_0000), 0x0E00_0000...0x0E00_FFFF => self.pak.sram.get8(addr - 0x0E00_0000),
else => std.debug.panic("Tried to read from 0x{X:0>2}", .{addr}), else => undRead("Tried to read from 0x{X:0>2}", .{addr}),
}; };
} }
@ -151,11 +153,15 @@ pub fn write8(self: *Self, addr: u32, byte: u8) void {
// External Memory (Game Pak) // External Memory (Game Pak)
0x0E00_0000...0x0E00_FFFF => self.pak.sram.set8(addr - 0x0E00_0000, byte), 0x0E00_0000...0x0E00_FFFF => self.pak.sram.set8(addr - 0x0E00_0000, byte),
else => std.debug.panic("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
} }
} }
fn failedRead(comptime format: []const u8, args: anytype) u8 { fn undRead(comptime format: []const u8, args: anytype) u8 {
log.warn(format, args); if (panic_on_und_bus) std.debug.panic(format, args) else log.warn(format, args);
return 0; return 0;
} }
fn undWrite(comptime format: []const u8, args: anytype) void {
if (panic_on_und_bus) std.debug.panic(format, args) else log.warn(format, args);
}

View File

@ -7,6 +7,8 @@ const DmaController = @import("dma.zig").DmaController;
const Timer = @import("timer.zig").Timer; const Timer = @import("timer.zig").Timer;
const Scheduler = @import("../scheduler.zig").Scheduler; const Scheduler = @import("../scheduler.zig").Scheduler;
const panic_on_und_io: bool = false;
const log = std.log.scoped(.@"I/O"); const log = std.log.scoped(.@"I/O");
pub const Io = struct { pub const Io = struct {
@ -79,7 +81,7 @@ pub fn read32(bus: *const Bus, addr: u32) u32 {
0x0400_0104 => @as(u32, bus.io.tim1.cnt.raw) << 16 | bus.io.tim1.counter(), 0x0400_0104 => @as(u32, bus.io.tim1.cnt.raw) << 16 | bus.io.tim1.counter(),
0x0400_0108 => @as(u32, bus.io.tim2.cnt.raw) << 16 | bus.io.tim2.counter(), 0x0400_0108 => @as(u32, bus.io.tim2.cnt.raw) << 16 | bus.io.tim2.counter(),
0x0400_010C => @as(u32, bus.io.tim3.cnt.raw) << 16 | bus.io.tim3.counter(), 0x0400_010C => @as(u32, bus.io.tim3.cnt.raw) << 16 | bus.io.tim3.counter(),
else => std.debug.panic("Tried to read word from 0x{X:0>8}", .{addr}), else => undRead("Tried to read word from 0x{X:0>8}", .{addr}),
}; };
} }
@ -117,7 +119,7 @@ pub fn write32(bus: *Bus, addr: u32, word: u32) void {
0x0400_0200 => bus.io.setIrqs(word), 0x0400_0200 => bus.io.setIrqs(word),
0x0400_0204 => log.warn("Wrote 0x{X:0>8} to WAITCNT", .{word}), 0x0400_0204 => log.warn("Wrote 0x{X:0>8} to WAITCNT", .{word}),
0x0400_0208 => bus.io.ime = word & 1 == 1, 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 }), else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
} }
} }
@ -138,8 +140,8 @@ pub fn read16(bus: *const Bus, addr: u32) u16 {
0x0400_010A => bus.io.tim2.cnt.raw, 0x0400_010A => bus.io.tim2.cnt.raw,
0x0400_010C => bus.io.tim3.counter(), 0x0400_010C => bus.io.tim3.counter(),
0x0400_010E => bus.io.tim3.cnt.raw, 0x0400_010E => bus.io.tim3.cnt.raw,
0x0400_0204 => failedRead("Tried to read halfword from WAITCNT", .{}), 0x0400_0204 => undRead("Tried to read halfword from WAITCNT", .{}),
else => std.debug.panic("Tried to read halfword from 0x{X:0>8}", .{addr}), else => undRead("Tried to read halfword from 0x{X:0>8}", .{addr}),
}; };
} }
@ -201,7 +203,7 @@ pub fn write16(bus: *Bus, addr: u32, halfword: u16) void {
0x0400_0202 => bus.io.irq.raw &= ~halfword, 0x0400_0202 => bus.io.irq.raw &= ~halfword,
0x0400_0204 => log.warn("Wrote 0x{X:0>4} to WAITCNT", .{halfword}), 0x0400_0204 => log.warn("Wrote 0x{X:0>4} to WAITCNT", .{halfword}),
0x0400_0208 => bus.io.ime = halfword & 1 == 1, 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 }), else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
} }
} }
@ -212,8 +214,8 @@ pub fn read8(bus: *const Bus, addr: u32) u8 {
0x0400_0200 => @truncate(u8, bus.io.ie.raw), 0x0400_0200 => @truncate(u8, bus.io.ie.raw),
0x0400_0300 => @enumToInt(bus.io.postflg), 0x0400_0300 => @enumToInt(bus.io.postflg),
0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw), 0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw),
0x0400_0089 => failedRead("Tried to read (high) byte from SOUNDBIAS", .{}), 0x0400_0089 => undRead("Tried to read (high) byte from SOUNDBIAS", .{}),
else => std.debug.panic("Tried to read byte from 0x{X:0>8}", .{addr}), else => undRead("Tried to read byte from 0x{X:0>8}", .{addr}),
}; };
} }
@ -232,15 +234,19 @@ pub fn write8(self: *Bus, addr: u32, byte: u8) void {
0x0400_007D => log.warn("Tried to write 0x{X:0>2} to SOUND4CNT_H (high)", .{byte}), 0x0400_007D => log.warn("Tried to write 0x{X:0>2} to SOUND4CNT_H (high)", .{byte}),
0x0400_0080 => log.warn("Tried to write 0x{X:0>2} to SOUNDCNT_L (low)", .{byte}), 0x0400_0080 => log.warn("Tried to write 0x{X:0>2} to SOUNDCNT_L (low)", .{byte}),
0x0400_0089 => log.warn("Tried to write 0x{X:0>2} to SOUNDBIAS (high)", .{byte}), 0x0400_0089 => log.warn("Tried to write 0x{X:0>2} to SOUNDBIAS (high)", .{byte}),
else => std.debug.panic("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
} }
} }
fn failedRead(comptime format: []const u8, args: anytype) u8 { fn undRead(comptime format: []const u8, args: anytype) u8 {
log.warn(format, args); if (panic_on_und_io) std.debug.panic(format, args) else log.warn(format, args);
return 0; return 0;
} }
fn undWrite(comptime format: []const u8, args: anytype) void {
if (panic_on_und_io) std.debug.panic(format, args) else log.warn(format, args);
}
/// Read / Write /// Read / Write
pub const PostFlag = enum(u1) { pub const PostFlag = enum(u1) {
FirstBoot = 0, FirstBoot = 0,