Compare commits

...

2 Commits

2 changed files with 32 additions and 17 deletions

View File

@ -14,7 +14,7 @@ pub const Io = struct {
ie: InterruptEnable, ie: InterruptEnable,
irq: InterruptRequest, irq: InterruptRequest,
postflg: PostFlag, postflg: PostFlag,
is_halted: bool, haltcnt: HaltControl,
keyinput: KeyInput, keyinput: KeyInput,
@ -23,9 +23,9 @@ 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, .postflg = .FirstBoot,
.haltcnt = .Execute,
}; };
} }
}; };
@ -33,9 +33,9 @@ pub const Io = struct {
pub fn read32(bus: *const Bus, addr: u32) u32 { pub fn read32(bus: *const Bus, addr: u32) u32 {
return switch (addr) { return switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw, 0x0400_0000 => bus.ppu.dispcnt.raw,
0x0400_0004 => bus.ppu.dispstat.raw, 0x0400_0004 => @as(u32, bus.ppu.vcount.raw) << 16 | bus.ppu.dispstat.raw,
0x0400_0006 => bus.ppu.vcount.raw, 0x0400_0006 => @as(u32, bus.ppu.bg[0].cnt.raw) << 16 | bus.ppu.vcount.raw,
0x0400_0200 => bus.io.ie.raw, 0x0400_0200 => @as(u32, bus.io.irq.raw) << 16 | bus.io.ie.raw,
0x0400_0208 => @boolToInt(bus.io.ime), 0x0400_0208 => @boolToInt(bus.io.ime),
0x0400_00C4 => failed_read("Tried to read word from DMA1CNT", .{}), 0x0400_00C4 => failed_read("Tried to read word from DMA1CNT", .{}),
0x0400_00D0 => failed_read("Tried to read word from DMA2CNT", .{}), 0x0400_00D0 => failed_read("Tried to read word from DMA2CNT", .{}),
@ -78,7 +78,10 @@ pub fn write32(bus: *Bus, addr: u32, word: u32) void {
0x0400_00C0 => log.warn("Wrote 0x{X:0>8} to DMA1DAD", .{word}), 0x0400_00C0 => log.warn("Wrote 0x{X:0>8} to DMA1DAD", .{word}),
0x0400_00C8 => log.warn("Wrote 0x{X:0>8} to DMA2SAD", .{word}), 0x0400_00C8 => log.warn("Wrote 0x{X:0>8} to DMA2SAD", .{word}),
0x0400_00CC => log.warn("Wrote 0x{X:0>8} to DMA2DAD", .{word}), 0x0400_00CC => log.warn("Wrote 0x{X:0>8} to DMA2DAD", .{word}),
0x0400_0200 => bus.io.ie.raw = @truncate(u16, word), 0x0400_0200 => {
bus.io.ie.raw = @truncate(u16, word);
bus.io.irq.raw &= ~@truncate(u16, word >> 16);
},
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 => std.debug.panic("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
@ -92,6 +95,7 @@ pub fn read16(bus: *const Bus, addr: u32) u16 {
0x0400_0006 => bus.ppu.vcount.raw, 0x0400_0006 => bus.ppu.vcount.raw,
0x0400_0130 => bus.io.keyinput.raw, 0x0400_0130 => bus.io.keyinput.raw,
0x0400_0200 => bus.io.ie.raw, 0x0400_0200 => bus.io.ie.raw,
0x0400_0202 => bus.io.irq.raw,
0x0400_0208 => @boolToInt(bus.io.ime), 0x0400_0208 => @boolToInt(bus.io.ime),
0x0400_0102 => failed_read("Tried to read halfword from TM0CNT_H", .{}), 0x0400_0102 => failed_read("Tried to read halfword from TM0CNT_H", .{}),
0x0400_0106 => failed_read("Tried to read halfword from TM1CNT_H", .{}), 0x0400_0106 => failed_read("Tried to read halfword from TM1CNT_H", .{}),
@ -106,7 +110,10 @@ pub fn write16(bus: *Bus, addr: u32, halfword: u16) void {
switch (addr) { switch (addr) {
0x0400_0000 => bus.ppu.dispcnt.raw = halfword, 0x0400_0000 => bus.ppu.dispcnt.raw = halfword,
0x0400_0004 => bus.ppu.dispstat.raw = halfword, 0x0400_0004 => bus.ppu.dispstat.raw = halfword,
0x0400_0008...0x0400_000F => bus.ppu.bg[addr & 0x3].cnt.raw = halfword, 0x0400_0008 => bus.ppu.bg[0].cnt.raw = halfword,
0x0400_000A => bus.ppu.bg[1].cnt.raw = halfword,
0x0400_000C => bus.ppu.bg[2].cnt.raw = halfword,
0x0400_000E => bus.ppu.bg[3].cnt.raw = halfword,
0x0400_0010 => bus.ppu.bg[0].hofs.raw = halfword, // TODO: Don't write out every HOFS / VOFS? 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_0012 => bus.ppu.bg[0].vofs.raw = halfword,
0x0400_0014 => bus.ppu.bg[1].hofs.raw = halfword, 0x0400_0014 => bus.ppu.bg[1].hofs.raw = halfword,
@ -162,7 +169,7 @@ pub fn read8(bus: *const Bus, addr: u32) u8 {
0x0400_0000 => @truncate(u8, bus.ppu.dispcnt.raw), 0x0400_0000 => @truncate(u8, bus.ppu.dispcnt.raw),
0x0400_0004 => @truncate(u8, bus.ppu.dispstat.raw), 0x0400_0004 => @truncate(u8, bus.ppu.dispstat.raw),
0x0400_0200 => @truncate(u8, bus.io.ie.raw), 0x0400_0200 => @truncate(u8, bus.io.ie.raw),
0x0400_0300 => bus.io.postflg.raw, 0x0400_0300 => @enumToInt(bus.io.postflg),
0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw), 0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw),
0x0400_0089 => failed_read("Tried to read (high) byte from SOUNDBIAS", .{}), 0x0400_0089 => failed_read("Tried to read (high) byte from SOUNDBIAS", .{}),
else => std.debug.panic("Tried to read byte from 0x{X:0>8}", .{addr}), else => std.debug.panic("Tried to read byte from 0x{X:0>8}", .{addr}),
@ -171,8 +178,10 @@ pub fn read8(bus: *const Bus, addr: u32) u8 {
pub fn write8(self: *Bus, addr: u32, byte: u8) void { pub fn write8(self: *Bus, addr: u32, byte: u8) void {
switch (addr) { switch (addr) {
0x0400_0004 => self.ppu.dispstat.raw = (self.ppu.dispstat.raw & 0xFF00) | byte,
0x0400_0005 => self.ppu.dispstat.raw = (@as(u16, byte) << 8) | (self.ppu.dispstat.raw & 0xFF),
0x0400_0208 => self.io.ime = byte & 1 == 1, 0x0400_0208 => self.io.ime = byte & 1 == 1,
0x0400_0301 => self.io.is_halted = byte >> 7 & 1 == 0, // TODO: Implement Stop? 0x0400_0301 => self.io.haltcnt = if (byte >> 7 & 1 == 0) .Halt else std.debug.panic("TODO: Implement STOP", .{}),
0x0400_0063 => log.warn("Tried to write 0x{X:0>2} to SOUND1CNT_H (high)", .{byte}), 0x0400_0063 => log.warn("Tried to write 0x{X:0>2} to SOUND1CNT_H (high)", .{byte}),
0x0400_0065 => log.warn("Tried to write 0x{X:0>2} to SOUND1CNT_X (high)", .{byte}), 0x0400_0065 => log.warn("Tried to write 0x{X:0>2} to SOUND1CNT_X (high)", .{byte}),
0x0400_0069 => log.warn("Tried to write 0x{X:0>2} to SOUND2CNT_L (high)", .{byte}), 0x0400_0069 => log.warn("Tried to write 0x{X:0>2} to SOUND2CNT_L (high)", .{byte}),
@ -192,10 +201,16 @@ fn failed_read(comptime format: []const u8, args: anytype) u8 {
} }
/// Read / Write /// Read / Write
pub const PostFlag = extern union { pub const PostFlag = enum(u1) {
/// 0 if First Boot, 1 if a Reset has been done FirstBoot = 0,
not_first_boot: Bit(u8, 0), FurtherBoots = 1,
raw: u8, };
/// Write Only
pub const HaltControl = enum {
Halt,
Stop,
Execute,
}; };
/// Read / Write /// Read / Write

View File

@ -247,7 +247,7 @@ pub const Arm7tdmi = struct {
pub fn step(self: *Self) u64 { pub fn step(self: *Self) u64 {
// If we're halted, the cpu is disabled // If we're halted, the cpu is disabled
if (self.bus.io.is_halted) return 1; if (self.bus.io.haltcnt == .Halt) return 1;
if (self.cpsr.t.read()) { if (self.cpsr.t.read()) {
const opcode = self.thumbFetch(); const opcode = self.thumbFetch();
@ -270,7 +270,7 @@ pub const Arm7tdmi = struct {
const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw; const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw;
if (should_handle != 0) { if (should_handle != 0) {
self.bus.io.is_halted = false; self.bus.io.haltcnt = .Execute;
// log.info("An Interrupt was Fired!", .{}); // log.info("An Interrupt was Fired!", .{});
// Either IME is not true or I in CPSR is true // Either IME is not true or I in CPSR is true