From 76789aa8bc29ca38deeafc7fd4950194d6b0de12 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 8 Apr 2022 17:07:36 -0300 Subject: [PATCH] chore: rewrite I/O read/writes --- src/Bus.zig | 14 +- src/bus/io.zig | 470 ++++++++++++++++++++++++------------------------- 2 files changed, 234 insertions(+), 250 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index 949e36e..181d6e3 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -63,12 +63,7 @@ pub fn read(self: *const Self, comptime T: type, address: u32) T { 0x00 => self.bios.read(T, align_addr), 0x02 => self.ewram.read(T, align_addr), 0x03 => self.iwram.read(T, align_addr), - 0x04 => switch (T) { - u32 => io.read32(self, align_addr), - u16 => io.read16(self, align_addr), - u8 => io.read8(self, align_addr), - else => @compileError("I/O: Unsupported read width"), - }, + 0x04 => io.read(self, T, align_addr), // Internal Display Memory 0x05 => self.ppu.palette.read(T, align_addr), @@ -102,12 +97,7 @@ pub fn write(self: *Self, comptime T: type, address: u32, value: T) void { 0x00 => self.bios.write(T, align_addr, value), 0x02 => self.ewram.write(T, align_addr, value), 0x03 => self.iwram.write(T, align_addr, value), - 0x04 => switch (T) { - u32 => io.write32(self, align_addr, value), - u16 => io.write16(self, align_addr, value), - u8 => io.write8(self, align_addr, value), - else => @compileError("I/O: Unsupported write width"), - }, + 0x04 => io.write(self, T, align_addr, value), // Internal Display Memory 0x05 => self.ppu.palette.write(T, align_addr, value), diff --git a/src/bus/io.zig b/src/bus/io.zig index 56aba7c..16c0eb6 100644 --- a/src/bus/io.zig +++ b/src/bus/io.zig @@ -38,274 +38,268 @@ pub const Io = struct { } }; -pub fn read32(bus: *const Bus, addr: u32) u32 { - return switch (addr) { - // Display - 0x0400_0000 => bus.ppu.dispcnt.raw, - 0x0400_0004 => @as(u32, bus.ppu.vcount.raw) << 16 | bus.ppu.dispstat.raw, - 0x0400_0006 => @as(u32, bus.ppu.bg[0].cnt.raw) << 16 | bus.ppu.vcount.raw, +pub fn read(bus: *const Bus, comptime T: type, address: u32) T { + return switch (T) { + u32 => switch (address) { + // Display + 0x0400_0000 => bus.ppu.dispcnt.raw, + 0x0400_0004 => @as(T, bus.ppu.vcount.raw) << 16 | bus.ppu.dispstat.raw, + 0x0400_0006 => @as(T, bus.ppu.bg[0].cnt.raw) << 16 | bus.ppu.vcount.raw, - // DMA Transfers - 0x0400_00B8 => @as(u32, bus.dma._0.cnt.raw) << 16, - 0x0400_00C4 => @as(u32, bus.dma._1.cnt.raw) << 16, - 0x0400_00D0 => @as(u32, bus.dma._1.cnt.raw) << 16, - 0x0400_00DC => @as(u32, bus.dma._3.cnt.raw) << 16, + // DMA Transfers + 0x0400_00B8 => @as(T, bus.dma._0.cnt.raw) << 16, + 0x0400_00C4 => @as(T, bus.dma._1.cnt.raw) << 16, + 0x0400_00D0 => @as(T, bus.dma._1.cnt.raw) << 16, + 0x0400_00DC => @as(T, bus.dma._3.cnt.raw) << 16, - // Timers - 0x0400_0100 => @as(u32, bus.tim._0.cnt.raw) << 16 | bus.tim._0.counter(), - 0x0400_0104 => @as(u32, bus.tim._1.cnt.raw) << 16 | bus.tim._1.counter(), - 0x0400_0108 => @as(u32, bus.tim._2.cnt.raw) << 16 | bus.tim._2.counter(), - 0x0400_010C => @as(u32, bus.tim._3.cnt.raw) << 16 | bus.tim._3.counter(), + // Timers + 0x0400_0100 => @as(T, bus.tim._0.cnt.raw) << 16 | bus.tim._0.counter(), + 0x0400_0104 => @as(T, bus.tim._1.cnt.raw) << 16 | bus.tim._1.counter(), + 0x0400_0108 => @as(T, bus.tim._2.cnt.raw) << 16 | bus.tim._2.counter(), + 0x0400_010C => @as(T, bus.tim._3.cnt.raw) << 16 | bus.tim._3.counter(), - // Interrupts - 0x0400_0200 => @as(u32, bus.io.irq.raw) << 16 | bus.io.ie.raw, - 0x0400_0208 => @boolToInt(bus.io.ime), - else => undRead("Tried to read word from 0x{X:0>8}", .{addr}), - }; -} - -pub fn write32(bus: *Bus, addr: u32, word: u32) void { - switch (addr) { - // Display - 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); + // Interrupts + 0x0400_0200 => @as(T, bus.io.irq.raw) << 16 | bus.io.ie.raw, + 0x0400_0208 => @boolToInt(bus.io.ime), + else => undRead("Tried to read {} from 0x{X:0>8}", .{ T, address }), }, - 0x0400_0008 => bus.ppu.setAdjCnts(0, word), - 0x0400_000C => bus.ppu.setAdjCnts(2, word), - 0x0400_0010 => bus.ppu.setBgOffsets(0, word), - 0x0400_0014 => bus.ppu.setBgOffsets(1, word), - 0x0400_0018 => bus.ppu.setBgOffsets(2, word), - 0x0400_001C => bus.ppu.setBgOffsets(3, word), + u16 => switch (address) { + // Display + 0x0400_0000 => bus.ppu.dispcnt.raw, + 0x0400_0004 => bus.ppu.dispstat.raw, + 0x0400_0006 => bus.ppu.vcount.raw, - // Sound - 0x0400_00A0 => log.warn("Wrote 0x{X:0>8} to FIFO_A", .{word}), - 0x0400_00A4 => log.warn("Wrote 0x{X:0>8} to FIFO_B", .{word}), + // Sound + 0x0400_0088 => bus.apu.bias.raw, - // DMA Transfers - 0x0400_00B0 => bus.dma._0.writeSad(word), - 0x0400_00B4 => bus.dma._0.writeDad(word), - 0x0400_00B8 => bus.dma._0.writeCnt(word), - 0x0400_00BC => bus.dma._1.writeSad(word), - 0x0400_00C0 => bus.dma._1.writeDad(word), - 0x0400_00C4 => bus.dma._1.writeCnt(word), - 0x0400_00C8 => bus.dma._2.writeSad(word), - 0x0400_00CC => bus.dma._2.writeDad(word), - 0x0400_00D0 => bus.dma._2.writeCnt(word), - 0x0400_00D4 => bus.dma._3.writeSad(word), - 0x0400_00D8 => bus.dma._3.writeDad(word), - 0x0400_00DC => bus.dma._3.writeCnt(word), + // DMA Transfers + 0x0400_00BA => bus.dma._0.cnt.raw, - // Timers - 0x0400_0100 => bus.tim._0.writeCnt(word), - 0x0400_0104 => bus.tim._1.writeCnt(word), - 0x0400_0108 => bus.tim._2.writeCnt(word), - 0x0400_010C => bus.tim._3.writeCnt(word), + // Timers + 0x0400_0100 => bus.tim._0.counter(), + 0x0400_0102 => bus.tim._0.cnt.raw, + 0x0400_0104 => bus.tim._1.counter(), + 0x0400_0106 => bus.tim._1.cnt.raw, + 0x0400_0108 => bus.tim._2.counter(), + 0x0400_010A => bus.tim._2.cnt.raw, + 0x0400_010C => bus.tim._3.counter(), + 0x0400_010E => bus.tim._3.cnt.raw, - // Serial Communication 1 - 0x0400_0120 => log.warn("Wrote 0x{X:0>8} to SIODATA32", .{word}), + // Serial Communication 1 + 0x0400_0128 => unimplementedRead("Read halfword from SIOCNT", .{}), - // Interrupts - 0x0400_0200 => bus.io.setIrqs(word), - 0x0400_0204 => log.warn("Wrote 0x{X:0>8} to WAITCNT", .{word}), - 0x0400_0208 => bus.io.ime = word & 1 == 1, - else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), - } -} + // Keypad Input + 0x0400_0130 => bus.io.keyinput.raw, -pub fn read16(bus: *const Bus, addr: u32) u16 { - return switch (addr) { - // Display - 0x0400_0000 => bus.ppu.dispcnt.raw, - 0x0400_0004 => bus.ppu.dispstat.raw, - 0x0400_0006 => bus.ppu.vcount.raw, + // Interrupts + 0x0400_0200 => bus.io.ie.raw, + 0x0400_0202 => bus.io.irq.raw, + 0x0400_0204 => unimplementedRead("Read halfword from WAITCNT", .{}), + 0x0400_0208 => @boolToInt(bus.io.ime), + else => undRead("Tried to read halfword from 0x{X:0>8}", .{address}), + }, + u8 => return switch (address) { + // Display + 0x0400_0000 => @truncate(T, bus.ppu.dispcnt.raw), + 0x0400_0004 => @truncate(T, bus.ppu.dispstat.raw), + 0x0400_0006 => @truncate(T, bus.ppu.vcount.raw), - // Sound - 0x0400_0088 => bus.apu.bias.raw, + // Sound + 0x0400_0060 => bus.apu.ch1.sweep.raw, + 0x0400_0063 => bus.apu.ch1.envelope.raw, + 0x0400_0069 => bus.apu.ch2.envelope.raw, + 0x0400_0073 => bus.apu.ch3.vol.raw, + 0x0400_0079 => bus.apu.ch4.envelope.raw, + 0x0400_007C => bus.apu.ch4.poly.raw, + 0x0400_0081 => @truncate(T, bus.apu.ch_vol_cnt.raw >> 8), + 0x0400_0089 => @truncate(T, bus.apu.bias.raw >> 8), - // DMA Transfers - 0x0400_00BA => bus.dma._0.cnt.raw, + // Serial Communication 1 + 0x0400_0128 => unimplementedRead("Read (low) byte from SIOCNT", .{}), - // Timers - 0x0400_0100 => bus.tim._0.counter(), - 0x0400_0102 => bus.tim._0.cnt.raw, - 0x0400_0104 => bus.tim._1.counter(), - 0x0400_0106 => bus.tim._1.cnt.raw, - 0x0400_0108 => bus.tim._2.counter(), - 0x0400_010A => bus.tim._2.cnt.raw, - 0x0400_010C => bus.tim._3.counter(), - 0x0400_010E => bus.tim._3.cnt.raw, - - // Serial Communication 1 - 0x0400_0128 => unimplementedRead("Read halfword from SIOCNT", .{}), - - // Keypad Input - 0x0400_0130 => bus.io.keyinput.raw, - - // Interrupts - 0x0400_0200 => bus.io.ie.raw, - 0x0400_0202 => bus.io.irq.raw, - 0x0400_0204 => unimplementedRead("Read halfword from WAITCNT", .{}), - 0x0400_0208 => @boolToInt(bus.io.ime), - else => undRead("Tried to read halfword from 0x{X:0>8}", .{addr}), + // Interrupts + 0x0400_0200 => @truncate(T, bus.io.ie.raw), + 0x0400_0300 => @enumToInt(bus.io.postflg), + else => undRead("Tried to read byte from 0x{X:0>8}", .{address}), + }, + else => @compileError("I/O: Unsupported read width"), }; } -pub fn write16(bus: *Bus, addr: u32, halfword: u16) void { - switch (addr) { - // Display - 0x0400_0000 => bus.ppu.dispcnt.raw = halfword, - 0x0400_0004 => bus.ppu.dispstat.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_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_0020 => log.warn("Wrote 0x{X:0>4} to BG2PA", .{halfword}), - 0x0400_0026 => log.warn("Wrote 0x{X:0>4} to BG2PD", .{halfword}), - 0x0400_0030 => log.warn("Wrote 0x{X:0>4} to BG3PA", .{halfword}), - 0x0400_0036 => log.warn("Wrote 0x{X:0>4} to BG3PD", .{halfword}), - 0x0400_0040 => log.warn("Wrote 0x{X:0>4} to WIN0H", .{halfword}), - 0x0400_0042 => log.warn("Wrote 0x{X:0>4} to WIN1H", .{halfword}), - 0x0400_0044 => log.warn("Wrote 0x{X:0>4} to WIN0V", .{halfword}), - 0x0400_0046 => log.warn("Wrote 0x{X:0>4} to WIN1V", .{halfword}), - 0x0400_0048 => log.warn("Wrote 0x{X:0>4} to WININ", .{halfword}), - 0x0400_004A => log.warn("Wrote 0x{X:0>4} to WINOUT", .{halfword}), - 0x0400_004C => log.warn("Wrote 0x{X:0>4} to MOSAIC", .{halfword}), - 0x0400_0050 => log.warn("Wrote 0x{X:0>4} to BLDCNT", .{halfword}), - 0x0400_0052 => log.warn("Wrote 0x{X:0>4} to BLDALPHA", .{halfword}), - 0x0400_0054 => log.warn("Wrote 0x{X:0>4} to BLDY", .{halfword}), +pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void { + return switch (T) { + u32 => switch (address) { + // Display + 0x0400_0000 => bus.ppu.dispcnt.raw = @truncate(u16, value), + 0x0400_0004 => { + bus.ppu.dispstat.raw = @truncate(u16, value); + bus.ppu.vcount.raw = @truncate(u16, value >> 16); + }, + 0x0400_0008 => bus.ppu.setAdjCnts(0, value), + 0x0400_000C => bus.ppu.setAdjCnts(2, value), + 0x0400_0010 => bus.ppu.setBgOffsets(0, value), + 0x0400_0014 => bus.ppu.setBgOffsets(1, value), + 0x0400_0018 => bus.ppu.setBgOffsets(2, value), + 0x0400_001C => bus.ppu.setBgOffsets(3, value), - // Sound - 0x0400_0080 => bus.apu.ch_vol_cnt.raw = halfword, - 0x0400_0082 => bus.apu.dma_cnt.raw = halfword, - 0x0400_0084 => bus.apu.setSoundCntX(halfword >> 7 & 1 == 1), - 0x0400_0088 => bus.apu.bias.raw = halfword, + // Sound + 0x0400_00A0 => log.warn("Wrote 0x{X:0>8} to FIFO_A", .{value}), + 0x0400_00A4 => log.warn("Wrote 0x{X:0>8} to FIFO_B", .{value}), - // Dma Transfers - 0x0400_00B8 => bus.dma._0.writeWordCount(halfword), - 0x0400_00BA => bus.dma._0.writeCntHigh(halfword), - 0x0400_00C4 => bus.dma._1.writeWordCount(halfword), - 0x0400_00C6 => bus.dma._1.writeCntHigh(halfword), - 0x0400_00D0 => bus.dma._2.writeWordCount(halfword), - 0x0400_00D2 => bus.dma._2.writeCntHigh(halfword), - 0x0400_00DC => bus.dma._3.writeWordCount(halfword), - 0x0400_00DE => bus.dma._3.writeCntHigh(halfword), + // DMA Transfers + 0x0400_00B0 => bus.dma._0.writeSad(value), + 0x0400_00B4 => bus.dma._0.writeDad(value), + 0x0400_00B8 => bus.dma._0.writeCnt(value), + 0x0400_00BC => bus.dma._1.writeSad(value), + 0x0400_00C0 => bus.dma._1.writeDad(value), + 0x0400_00C4 => bus.dma._1.writeCnt(value), + 0x0400_00C8 => bus.dma._2.writeSad(value), + 0x0400_00CC => bus.dma._2.writeDad(value), + 0x0400_00D0 => bus.dma._2.writeCnt(value), + 0x0400_00D4 => bus.dma._3.writeSad(value), + 0x0400_00D8 => bus.dma._3.writeDad(value), + 0x0400_00DC => bus.dma._3.writeCnt(value), - // Timers - 0x0400_0100 => bus.tim._0.writeCntLow(halfword), - 0x0400_0102 => bus.tim._0.writeCntHigh(halfword), - 0x0400_0104 => bus.tim._1.writeCntLow(halfword), - 0x0400_0106 => bus.tim._1.writeCntHigh(halfword), - 0x0400_0108 => bus.tim._2.writeCntLow(halfword), - 0x0400_010A => bus.tim._2.writeCntHigh(halfword), - 0x0400_010C => bus.tim._3.writeCntLow(halfword), - 0x0400_010E => bus.tim._3.writeCntHigh(halfword), + // Timers + 0x0400_0100 => bus.tim._0.writeCnt(value), + 0x0400_0104 => bus.tim._1.writeCnt(value), + 0x0400_0108 => bus.tim._2.writeCnt(value), + 0x0400_010C => bus.tim._3.writeCnt(value), - // Serial Communication 1 - 0x0400_0120 => log.warn("Wrote 0x{X:0>4} to SIOMULTI0", .{halfword}), - 0x0400_0122 => log.warn("Wrote 0x{X:0>4} to SIOMULTI1", .{halfword}), - 0x0400_0124 => log.warn("Wrote 0x{X:0>4} to SIOMULTI2", .{halfword}), - 0x0400_0126 => log.warn("Wrote 0x{X:0>4} to SIOMULTI3", .{halfword}), - 0x0400_0128 => log.warn("Wrote 0x{X:0>4} to SIOCNT", .{halfword}), - 0x0400_012A => log.warn("Wrote 0x{X:0>4} to SIOMLT_SEND", .{halfword}), + // Serial Communication 1 + 0x0400_0120 => log.warn("Wrote 0x{X:0>8} to SIODATA32", .{value}), - // Keypad Input - 0x0400_0130 => log.warn("Wrote 0x{X:0>4} to KEYINPUT. Ignored", .{halfword}), - 0x0400_0132 => log.warn("Wrote 0x{X:0>4} to KEYCNT", .{halfword}), + // Interrupts + 0x0400_0200 => bus.io.setIrqs(value), + 0x0400_0204 => log.warn("Wrote 0x{X:0>8} to WAITCNT", .{value}), + 0x0400_0208 => bus.io.ime = value & 1 == 1, + else => undWrite("Tried to write {} 0x{X:0>8} to 0x{X:0>8}", .{ T, value, address }), + }, + u16 => switch (address) { + // Display + 0x0400_0000 => bus.ppu.dispcnt.raw = value, + 0x0400_0004 => bus.ppu.dispstat.raw = value, + 0x0400_0008 => bus.ppu.bg[0].cnt.raw = value, + 0x0400_000A => bus.ppu.bg[1].cnt.raw = value, + 0x0400_000C => bus.ppu.bg[2].cnt.raw = value, + 0x0400_000E => bus.ppu.bg[3].cnt.raw = value, + 0x0400_0010 => bus.ppu.bg[0].hofs.raw = value, // TODO: Don't write out every HOFS / VOFS? + 0x0400_0012 => bus.ppu.bg[0].vofs.raw = value, + 0x0400_0014 => bus.ppu.bg[1].hofs.raw = value, + 0x0400_0016 => bus.ppu.bg[1].vofs.raw = value, + 0x0400_0018 => bus.ppu.bg[2].hofs.raw = value, + 0x0400_001A => bus.ppu.bg[2].vofs.raw = value, + 0x0400_001C => bus.ppu.bg[3].hofs.raw = value, + 0x0400_001E => bus.ppu.bg[3].vofs.raw = value, + 0x0400_0020 => log.warn("Wrote 0x{X:0>4} to BG2PA", .{value}), + 0x0400_0026 => log.warn("Wrote 0x{X:0>4} to BG2PD", .{value}), + 0x0400_0030 => log.warn("Wrote 0x{X:0>4} to BG3PA", .{value}), + 0x0400_0036 => log.warn("Wrote 0x{X:0>4} to BG3PD", .{value}), + 0x0400_0040 => log.warn("Wrote 0x{X:0>4} to WIN0H", .{value}), + 0x0400_0042 => log.warn("Wrote 0x{X:0>4} to WIN1H", .{value}), + 0x0400_0044 => log.warn("Wrote 0x{X:0>4} to WIN0V", .{value}), + 0x0400_0046 => log.warn("Wrote 0x{X:0>4} to WIN1V", .{value}), + 0x0400_0048 => log.warn("Wrote 0x{X:0>4} to WININ", .{value}), + 0x0400_004A => log.warn("Wrote 0x{X:0>4} to WINOUT", .{value}), + 0x0400_004C => log.warn("Wrote 0x{X:0>4} to MOSAIC", .{value}), + 0x0400_0050 => log.warn("Wrote 0x{X:0>4} to BLDCNT", .{value}), + 0x0400_0052 => log.warn("Wrote 0x{X:0>4} to BLDALPHA", .{value}), + 0x0400_0054 => log.warn("Wrote 0x{X:0>4} to BLDY", .{value}), - // Serial Communication 2 - 0x0400_0134 => log.warn("Wrote 0x{X:0>4} to RCNT", .{halfword}), - 0x0400_0140 => log.warn("Wrote 0x{X:0>4} to JOYCNT", .{halfword}), - 0x0400_0158 => log.warn("Wrote 0x{X:0>4} to JOYSTAT", .{halfword}), + // Sound + 0x0400_0080 => bus.apu.ch_vol_cnt.raw = value, + 0x0400_0082 => bus.apu.dma_cnt.raw = value, + 0x0400_0084 => bus.apu.setSoundCntX(value >> 7 & 1 == 1), + 0x0400_0088 => bus.apu.bias.raw = value, - // Interrupts - 0x0400_0200 => bus.io.ie.raw = halfword, - 0x0400_0202 => bus.io.irq.raw &= ~halfword, - 0x0400_0204 => log.warn("Wrote 0x{X:0>4} to WAITCNT", .{halfword}), - 0x0400_0208 => bus.io.ime = halfword & 1 == 1, - else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }), - } -} + // Dma Transfers + 0x0400_00B8 => bus.dma._0.writeWordCount(value), + 0x0400_00BA => bus.dma._0.writeCntHigh(value), + 0x0400_00C4 => bus.dma._1.writeWordCount(value), + 0x0400_00C6 => bus.dma._1.writeCntHigh(value), + 0x0400_00D0 => bus.dma._2.writeWordCount(value), + 0x0400_00D2 => bus.dma._2.writeCntHigh(value), + 0x0400_00DC => bus.dma._3.writeWordCount(value), + 0x0400_00DE => bus.dma._3.writeCntHigh(value), -pub fn read8(bus: *const Bus, addr: u32) u8 { - return switch (addr) { - // Display - 0x0400_0000 => @truncate(u8, bus.ppu.dispcnt.raw), - 0x0400_0004 => @truncate(u8, bus.ppu.dispstat.raw), - 0x0400_0006 => @truncate(u8, bus.ppu.vcount.raw), + // Timers + 0x0400_0100 => bus.tim._0.writeCntLow(value), + 0x0400_0102 => bus.tim._0.writeCntHigh(value), + 0x0400_0104 => bus.tim._1.writeCntLow(value), + 0x0400_0106 => bus.tim._1.writeCntHigh(value), + 0x0400_0108 => bus.tim._2.writeCntLow(value), + 0x0400_010A => bus.tim._2.writeCntHigh(value), + 0x0400_010C => bus.tim._3.writeCntLow(value), + 0x0400_010E => bus.tim._3.writeCntHigh(value), - // Sound - 0x0400_0060 => bus.apu.ch1.sweep.raw, - 0x0400_0063 => bus.apu.ch1.envelope.raw, - 0x0400_0069 => bus.apu.ch2.envelope.raw, - 0x0400_0073 => bus.apu.ch3.vol.raw, - 0x0400_0079 => bus.apu.ch4.envelope.raw, - 0x0400_007C => bus.apu.ch4.poly.raw, - 0x0400_0081 => @truncate(u8, bus.apu.ch_vol_cnt.raw >> 8), - 0x0400_0089 => @truncate(u8, bus.apu.bias.raw >> 8), + // Serial Communication 1 + 0x0400_0120 => log.warn("Wrote 0x{X:0>4} to SIOMULTI0", .{value}), + 0x0400_0122 => log.warn("Wrote 0x{X:0>4} to SIOMULTI1", .{value}), + 0x0400_0124 => log.warn("Wrote 0x{X:0>4} to SIOMULTI2", .{value}), + 0x0400_0126 => log.warn("Wrote 0x{X:0>4} to SIOMULTI3", .{value}), + 0x0400_0128 => log.warn("Wrote 0x{X:0>4} to SIOCNT", .{value}), + 0x0400_012A => log.warn("Wrote 0x{X:0>4} to SIOMLT_SEND", .{value}), - // Serial Communication 1 - 0x0400_0128 => unimplementedRead("Read (low) byte from SIOCNT", .{}), + // Keypad Input + 0x0400_0130 => log.warn("Wrote 0x{X:0>4} to KEYINPUT. Ignored", .{value}), + 0x0400_0132 => log.warn("Wrote 0x{X:0>4} to KEYCNT", .{value}), - // Interrupts - 0x0400_0200 => @truncate(u8, bus.io.ie.raw), - 0x0400_0300 => @enumToInt(bus.io.postflg), - else => undRead("Tried to read byte from 0x{X:0>8}", .{addr}), + // Serial Communication 2 + 0x0400_0134 => log.warn("Wrote 0x{X:0>4} to RCNT", .{value}), + 0x0400_0140 => log.warn("Wrote 0x{X:0>4} to JOYCNT", .{value}), + 0x0400_0158 => log.warn("Wrote 0x{X:0>4} to JOYSTAT", .{value}), + + // Interrupts + 0x0400_0200 => bus.io.ie.raw = value, + 0x0400_0202 => bus.io.irq.raw &= ~value, + 0x0400_0204 => log.warn("Wrote 0x{X:0>4} to WAITCNT", .{value}), + 0x0400_0208 => bus.io.ime = value & 1 == 1, + else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ value, address }), + }, + u8 => switch (address) { + // Display + 0x0400_0004 => bus.ppu.dispstat.raw = (bus.ppu.dispstat.raw & 0xFF00) | value, + 0x0400_0005 => bus.ppu.dispstat.raw = (@as(u16, value) << 8) | (bus.ppu.dispstat.raw & 0xFF), + + // Sound + 0x0400_0060 => bus.apu.ch1.sweep.raw = value, + 0x0400_0062 => bus.apu.ch1.duty.raw = value, + 0x0400_0063 => bus.apu.ch1.envelope.raw = value, + 0x0400_0064 => bus.apu.ch1.setFreqLow(value), + 0x0400_0065 => bus.apu.ch1.setFreqHigh(value), + 0x0400_0068 => bus.apu.ch2.duty.raw = value, + 0x0400_0069 => bus.apu.ch2.envelope.raw = value, + 0x0400_006C => bus.apu.ch2.setFreqLow(value), + 0x0400_006D => bus.apu.ch2.setFreqHigh(value), + 0x0400_0070 => bus.apu.ch3.select.raw = value, + 0x0400_0072 => bus.apu.ch3.length = value, + 0x0400_0073 => bus.apu.ch3.vol.raw = value, + 0x0400_0074 => bus.apu.ch3.setFreqLow(value), + 0x0400_0075 => bus.apu.ch3.setFreqHigh(value), + 0x0400_0078 => bus.apu.ch4.len = @truncate(u6, value), + 0x0400_0079 => bus.apu.ch4.envelope.raw = value, + 0x0400_007C => bus.apu.ch4.poly.raw = value, + 0x0400_007D => bus.apu.ch4.cnt.raw = value, + 0x0400_0080 => bus.apu.setSoundCntLLow(value), + 0x0400_0081 => bus.apu.setSoundCntLHigh(value), + 0x0400_0084 => bus.apu.setSoundCntX(value >> 7 & 1 == 1), + 0x0400_0089 => bus.apu.setBiasHigh(value), + + // Serial Communication 1 + 0x0400_0128 => log.warn("Wrote 0x{X:0>2} to SIOCNT (low)", .{value}), + + // Serial Communication 2 + 0x0400_0140 => log.warn("Wrote 0x{X:0>2} to JOYCNT (low)", .{value}), + + // Interrupts + 0x0400_0208 => bus.io.ime = value & 1 == 1, + 0x0400_0301 => bus.io.haltcnt = if (value >> 7 & 1 == 0) .Halt else std.debug.panic("TODO: Implement STOP", .{}), + else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ value, address }), + }, + else => @compileError("I/O: Unsupported write width"), }; } -pub fn write8(bus: *Bus, addr: u32, byte: u8) void { - switch (addr) { - // Display - 0x0400_0004 => bus.ppu.dispstat.raw = (bus.ppu.dispstat.raw & 0xFF00) | byte, - 0x0400_0005 => bus.ppu.dispstat.raw = (@as(u16, byte) << 8) | (bus.ppu.dispstat.raw & 0xFF), - - // Sound - 0x0400_0060 => bus.apu.ch1.sweep.raw = byte, - 0x0400_0062 => bus.apu.ch1.duty.raw = byte, - 0x0400_0063 => bus.apu.ch1.envelope.raw = byte, - 0x0400_0064 => bus.apu.ch1.setFreqLow(byte), - 0x0400_0065 => bus.apu.ch1.setFreqHigh(byte), - 0x0400_0068 => bus.apu.ch2.duty.raw = byte, - 0x0400_0069 => bus.apu.ch2.envelope.raw = byte, - 0x0400_006C => bus.apu.ch2.setFreqLow(byte), - 0x0400_006D => bus.apu.ch2.setFreqHigh(byte), - 0x0400_0070 => bus.apu.ch3.select.raw = byte, - 0x0400_0072 => bus.apu.ch3.length = byte, - 0x0400_0073 => bus.apu.ch3.vol.raw = byte, - 0x0400_0074 => bus.apu.ch3.setFreqLow(byte), - 0x0400_0075 => bus.apu.ch3.setFreqHigh(byte), - 0x0400_0078 => bus.apu.ch4.len = @truncate(u6, byte), - 0x0400_0079 => bus.apu.ch4.envelope.raw = byte, - 0x0400_007C => bus.apu.ch4.poly.raw = byte, - 0x0400_007D => bus.apu.ch4.cnt.raw = byte, - 0x0400_0080 => bus.apu.setSoundCntLLow(byte), - 0x0400_0081 => bus.apu.setSoundCntLHigh(byte), - 0x0400_0084 => bus.apu.setSoundCntX(byte >> 7 & 1 == 1), - 0x0400_0089 => bus.apu.setBiasHigh(byte), - - // Serial Communication 1 - 0x0400_0128 => log.warn("Wrote 0x{X:0>2} to SIOCNT (low)", .{byte}), - - // Serial Communication 2 - 0x0400_0140 => log.warn("Wrote 0x{X:0>2} to JOYCNT (low)", .{byte}), - - // Interrupts - 0x0400_0208 => bus.io.ime = byte & 1 == 1, - 0x0400_0301 => bus.io.haltcnt = if (byte >> 7 & 1 == 0) .Halt else std.debug.panic("TODO: Implement STOP", .{}), - else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), - } -} - fn undRead(comptime format: []const u8, args: anytype) u8 { if (panic_on_und_io) std.debug.panic(format, args) else log.warn(format, args); return 0;