Compare commits

..

4 Commits

4 changed files with 169 additions and 83 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,88 +2,30 @@ 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();
dispcnt: DisplayControl,
dispstat: DisplayStatus,
vcount: VCount,
/// Read / Write /// Read / Write
ime: bool, ime: bool,
ie: InterruptEnable, ie: InterruptEnable,
irq: InterruptRequest,
keyinput: KeyInput, keyinput: KeyInput,
pub fn init() Self { pub fn init() Self {
return .{ return .{
.dispcnt = .{ .raw = 0x0000 },
.dispstat = .{ .raw = 0x0000 },
.vcount = .{ .raw = 0x0000 },
.ime = false, .ime = false,
.ie = .{ .raw = 0x0000 }, .ie = .{ .raw = 0x0000 },
.irq = .{ .raw = 0x0000 },
.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_0200 => self.ie.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, addr: u32, byte: u8) void {
std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr });
}
}; };
/// Read / Write /// Read / Write
const DisplayControl = extern union { pub const DisplayControl = extern union {
bg_mode: Bitfield(u16, 0, 3), bg_mode: Bitfield(u16, 0, 3),
frame_select: Bit(u16, 4), frame_select: Bit(u16, 4),
hblank_interval_free: Bit(u16, 5), hblank_interval_free: Bit(u16, 5),
@ -97,7 +39,7 @@ const DisplayControl = extern union {
}; };
/// Read / Write /// Read / Write
const DisplayStatus = extern union { pub const DisplayStatus = extern union {
vblank: Bit(u16, 0), vblank: Bit(u16, 0),
hblank: Bit(u16, 1), hblank: Bit(u16, 1),
coincidence: Bit(u16, 2), coincidence: Bit(u16, 2),
@ -109,7 +51,7 @@ const DisplayStatus = extern union {
}; };
/// Read Only /// Read Only
const VCount = extern union { pub const VCount = extern union {
scanline: Bitfield(u16, 0, 8), scanline: Bitfield(u16, 0, 8),
raw: u16, raw: u16,
}; };
@ -148,3 +90,103 @@ const KeyInput = extern union {
shoulder_l: Bit(u16, 9), shoulder_l: Bit(u16, 9),
raw: u16, raw: u16,
}; };
// Read / Write
pub const BackgroundControl = extern union {
bg_priority: Bitfield(u16, 0, 2),
char_base: Bitfield(u16, 2, 2),
mosaic_enable: Bit(u16, 6),
palette_type: Bit(u16, 7),
screen_base: Bitfield(u16, 8, 5),
display_overflow: Bit(u16, 13),
screen_size: Bitfield(u16, 14, 2),
raw: u16,
};
/// Write Only
pub const BackgroundOffset = extern union {
offset: Bitfield(u16, 0, 9),
raw: u16,
};
/// Read / Write
const InterruptRequest = extern union {
vblank: Bit(u16, 0),
hblank: Bit(u16, 1),
coincidence: Bit(u16, 2),
tim0_overflow: Bit(u16, 3),
tim1_overflow: Bit(u16, 4),
tim2_overflow: Bit(u16, 5),
tim3_overflow: Bit(u16, 6),
serial: Bit(u16, 7),
dma0: Bit(u16, 8),
dma1: Bit(u16, 9),
dma2: Bit(u16, 10),
dma3: Bit(u16, 11),
keypad: Bit(u16, 12),
game_pak: Bit(u16, 13),
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("[I/O: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_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.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("[I/O: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 => bus.ppu.bg0.cnt.raw = halfword,
0x0400_0010 => bus.ppu.bg0.hofs.raw = halfword,
0x0400_0012 => bus.ppu.bg0.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("[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.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("[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 }),
}
}

View File

@ -1,10 +1,11 @@
const std = @import("std"); const std = @import("std");
const io = @import("bus/io.zig");
const EventKind = @import("scheduler.zig").EventKind; const EventKind = @import("scheduler.zig").EventKind;
const Io = @import("bus/io.zig").Io;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
pub const width = 240; pub const width = 240;
pub const height = 160; pub const height = 160;
pub const framebuf_pitch = width * @sizeOf(u16); pub const framebuf_pitch = width * @sizeOf(u16);
@ -12,6 +13,16 @@ pub const framebuf_pitch = width * @sizeOf(u16);
pub const Ppu = struct { pub const Ppu = struct {
const Self = @This(); const Self = @This();
// Registers
bg0: Background,
bg1: Background,
bg2: Background,
bg3: Background,
dispcnt: io.DisplayControl,
dispstat: io.DisplayStatus,
vcount: io.VCount,
vram: Vram, vram: Vram,
palette: Palette, palette: Palette,
oam: Oam, oam: Oam,
@ -33,6 +44,15 @@ pub const Ppu = struct {
.sched = sched, .sched = sched,
.framebuf = framebuf, .framebuf = framebuf,
.alloc = alloc, .alloc = alloc,
// Registers
.bg0 = Background.init(),
.bg1 = Background.init(),
.bg2 = Background.init(),
.bg3 = Background.init(),
.dispcnt = .{ .raw = 0x0000 },
.dispstat = .{ .raw = 0x0000 },
.vcount = .{ .raw = 0x0000 },
}; };
} }
@ -42,11 +62,15 @@ pub const Ppu = struct {
self.palette.deinit(); self.palette.deinit();
} }
pub fn drawScanline(self: *Self, io: *const Io) void { pub fn drawScanline(self: *Self) void {
const bg_mode = io.dispcnt.bg_mode.read(); const bg_mode = self.dispcnt.bg_mode.read();
const scanline = io.vcount.scanline.read(); const scanline = self.vcount.scanline.read();
switch (bg_mode) { switch (bg_mode) {
0x0 => {
// Mode 0
},
0x3 => { 0x3 => {
const start = framebuf_pitch * @as(usize, scanline); const start = framebuf_pitch * @as(usize, scanline);
const end = start + framebuf_pitch; const end = start + framebuf_pitch;
@ -54,7 +78,7 @@ pub const Ppu = struct {
std.mem.copy(u8, self.framebuf[start..end], self.vram.buf[start..end]); std.mem.copy(u8, self.framebuf[start..end], self.vram.buf[start..end]);
}, },
0x4 => { 0x4 => {
const select = io.dispcnt.frame_select.read(); const select = self.dispcnt.frame_select.read();
const vram_start = width * @as(usize, scanline); const vram_start = width * @as(usize, scanline);
const buf_start = vram_start * @sizeOf(u16); const buf_start = vram_start * @sizeOf(u16);
@ -70,7 +94,7 @@ pub const Ppu = struct {
self.framebuf[buf_start + j] = self.palette.buf[id]; self.framebuf[buf_start + j] = self.palette.buf[id];
} }
}, },
else => {}, // std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}), else => std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),
} }
} }
}; };
@ -201,3 +225,22 @@ const Oam = struct {
return self.buf[idx]; return self.buf[idx];
} }
}; };
const Background = struct {
const Self = @This();
/// Read / Write
cnt: io.BackgroundControl,
/// Write Only
hofs: io.BackgroundOffset,
/// Write Only
vofs: io.BackgroundOffset,
fn init() Self {
return .{
.cnt = .{ .raw = 0x0000 },
.hofs = .{ .raw = 0x0000 },
.vofs = .{ .raw = 0x0000 },
};
}
};

View File

@ -38,28 +38,28 @@ pub const Scheduler = struct {
}, },
.HBlank => { .HBlank => {
// The End of a Hblank (During Draw or Vblank) // The End of a Hblank (During Draw or Vblank)
const old_scanline = bus.io.vcount.scanline.read(); const old_scanline = bus.ppu.vcount.scanline.read();
const scanline = (old_scanline + 1) % 228; const scanline = (old_scanline + 1) % 228;
bus.io.vcount.scanline.write(scanline); bus.ppu.vcount.scanline.write(scanline);
bus.io.dispstat.hblank.unset(); bus.ppu.dispstat.hblank.unset();
if (scanline < 160) { if (scanline < 160) {
// Transitioning to another Draw // Transitioning to another Draw
self.push(.Draw, self.tick + (240 * 4)); self.push(.Draw, self.tick + (240 * 4));
} else { } else {
// Transitioning to a Vblank // Transitioning to a Vblank
if (scanline < 227) bus.io.dispstat.vblank.set() else bus.io.dispstat.vblank.unset(); if (scanline < 227) bus.ppu.dispstat.vblank.set() else bus.ppu.dispstat.vblank.unset();
self.push(.VBlank, self.tick + (240 * 4)); self.push(.VBlank, self.tick + (240 * 4));
} }
}, },
.Draw => { .Draw => {
// The end of a Draw // The end of a Draw
bus.ppu.drawScanline(&bus.io); bus.ppu.drawScanline();
// Transitioning to a Hblank // Transitioning to a Hblank
bus.io.dispstat.hblank.set(); bus.ppu.dispstat.hblank.set();
self.push(.HBlank, self.tick + (68 * 4)); self.push(.HBlank, self.tick + (68 * 4));
}, },
.VBlank => { .VBlank => {