2022-10-21 08:11:50 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const Bios = @import("bus/Bios.zig");
|
2022-10-21 08:11:53 +00:00
|
|
|
const Ewram = @import("bus/Ewram.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const GamePak = @import("bus/GamePak.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Io = @import("bus/io.zig").Io;
|
2022-10-21 08:11:53 +00:00
|
|
|
const Iwram = @import("bus/Iwram.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Ppu = @import("ppu.zig").Ppu;
|
2022-10-21 08:12:27 +00:00
|
|
|
const Apu = @import("apu.zig").Apu;
|
2022-10-21 08:12:28 +00:00
|
|
|
const DmaControllers = @import("bus/dma.zig").DmaControllers;
|
|
|
|
const Timers = @import("bus/timer.zig").Timers;
|
2022-10-21 08:11:50 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:20 +00:00
|
|
|
const io = @import("bus/io.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Allocator = std.mem.Allocator;
|
2022-10-21 08:12:18 +00:00
|
|
|
const log = std.log.scoped(.Bus);
|
2022-10-21 08:12:32 +00:00
|
|
|
|
|
|
|
const rotr = @import("util.zig").rotr;
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
const Self = @This();
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
const panic_on_und_bus: bool = false;
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pak: GamePak,
|
|
|
|
bios: Bios,
|
|
|
|
ppu: Ppu,
|
2022-10-21 08:12:27 +00:00
|
|
|
apu: Apu,
|
2022-10-21 08:12:28 +00:00
|
|
|
dma: DmaControllers,
|
|
|
|
tim: Timers,
|
2022-10-21 08:11:53 +00:00
|
|
|
iwram: Iwram,
|
|
|
|
ewram: Ewram,
|
2022-10-21 08:12:28 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
io: Io,
|
|
|
|
|
2022-10-21 08:12:29 +00:00
|
|
|
pub fn init(alloc: Allocator, sched: *Scheduler, rom_path: []const u8, bios_path: ?[]const u8, save_path: ?[]const u8) !Self {
|
2022-10-21 08:11:51 +00:00
|
|
|
return Self{
|
2022-10-21 08:12:29 +00:00
|
|
|
.pak = try GamePak.init(alloc, rom_path, save_path),
|
|
|
|
.bios = try Bios.init(alloc, bios_path),
|
2022-10-21 08:11:50 +00:00
|
|
|
.ppu = try Ppu.init(alloc, sched),
|
2022-10-21 08:12:27 +00:00
|
|
|
.apu = Apu.init(),
|
2022-10-21 08:11:53 +00:00
|
|
|
.iwram = try Iwram.init(alloc),
|
|
|
|
.ewram = try Ewram.init(alloc),
|
2022-10-21 08:12:28 +00:00
|
|
|
.dma = DmaControllers.init(),
|
|
|
|
.tim = Timers.init(sched),
|
|
|
|
.io = Io.init(),
|
2022-10-21 08:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
pub fn deinit(self: Self) void {
|
2022-10-21 08:11:53 +00:00
|
|
|
self.iwram.deinit();
|
|
|
|
self.ewram.deinit();
|
2022-10-21 08:11:50 +00:00
|
|
|
self.pak.deinit();
|
|
|
|
self.bios.deinit();
|
|
|
|
self.ppu.deinit();
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn read32(self: *const Self, address: u32) u32 {
|
|
|
|
const align_addr = address & 0xFFFF_FFFC; // Force Aligned
|
|
|
|
|
|
|
|
return switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0000_0000...0x0000_3FFF => self.bios.read(u32, align_addr),
|
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.read(u32, align_addr),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.read(u32, align_addr),
|
|
|
|
0x0400_0000...0x0400_03FE => io.read32(self, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u32, align_addr),
|
|
|
|
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u32, align_addr),
|
|
|
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u32, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0800_0000...0x09FF_FFFF => self.pak.read(u32, align_addr),
|
|
|
|
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u32, align_addr),
|
|
|
|
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u32, align_addr),
|
|
|
|
0x0E00_0000...0x0FFF_FFFF => @as(u32, self.pak.backup.read(address)) * 0x01010101,
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
else => undRead("Tried to read from 0x{X:0>8}", .{address}),
|
2022-10-21 08:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn write32(self: *Self, address: u32, word: u32) void {
|
|
|
|
const align_addr = address & 0xFFFF_FFFC; // Force Aligned
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.write(u32, align_addr, word),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.write(u32, align_addr, word),
|
|
|
|
0x0400_0000...0x0400_03FE => io.write32(self, align_addr, word),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.write(u32, align_addr, word),
|
|
|
|
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u32, align_addr, word),
|
|
|
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u32, align_addr, word),
|
|
|
|
0x0E00_0000...0x0FFF_FFFF => self.pak.backup.write(address, @truncate(u8, rotr(u32, word, 8 * (address & 3)))),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn read16(self: *const Self, address: u32) u16 {
|
|
|
|
const align_addr = address & 0xFFFF_FFFE; // Force Aligned
|
|
|
|
|
|
|
|
return switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0000_0000...0x0000_3FFF => self.bios.read(u16, align_addr),
|
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.read(u16, align_addr),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.read(u16, align_addr),
|
|
|
|
0x0400_0000...0x0400_03FE => io.read16(self, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u16, align_addr),
|
|
|
|
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u16, align_addr),
|
|
|
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u16, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0800_0000...0x09FF_FFFF => self.pak.read(u16, align_addr),
|
|
|
|
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u16, align_addr),
|
|
|
|
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u16, align_addr),
|
|
|
|
0x0E00_0000...0x0FFF_FFFF => @as(u16, self.pak.backup.read(address)) * 0x0101,
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
else => undRead("Tried to read from 0x{X:0>8}", .{address}),
|
2022-10-21 08:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn write16(self: *Self, address: u32, halfword: u16) void {
|
|
|
|
const align_addr = address & 0xFFFF_FFFE;
|
|
|
|
|
|
|
|
switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.write(u16, align_addr, halfword),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.write(u16, align_addr, halfword),
|
|
|
|
0x0400_0000...0x0400_03FE => io.write16(self, align_addr, halfword),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.write(u16, align_addr, halfword),
|
|
|
|
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u16, align_addr, halfword),
|
|
|
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u16, align_addr, halfword),
|
2022-10-21 08:12:30 +00:00
|
|
|
0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0E00_0000...0x0FFF_FFFF => {
|
|
|
|
self.pak.backup.write(address, @truncate(u8, rotr(u16, halfword, 8 * (address & 1))));
|
|
|
|
},
|
2022-10-21 08:12:32 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn read8(self: *const Self, address: u32) u8 {
|
|
|
|
return switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0000_0000...0x0000_3FFF => self.bios.read(u8, address),
|
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.read(u8, address),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.read(u8, address),
|
|
|
|
0x0400_0000...0x0400_03FE => io.read8(self, address),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u8, address),
|
|
|
|
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u8, address),
|
|
|
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u8, address),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0800_0000...0x09FF_FFFF => self.pak.read(u8, address),
|
|
|
|
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u8, address),
|
|
|
|
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u8, address),
|
|
|
|
0x0E00_0000...0x0FFF_FFFF => self.pak.backup.read(address),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
else => undRead("Tried to read from 0x{X:0>2}", .{address}),
|
2022-10-21 08:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:32 +00:00
|
|
|
pub fn write8(self: *Self, address: u32, byte: u8) void {
|
|
|
|
switch (address) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0200_0000...0x02FF_FFFF => self.ewram.write(u8, address, byte),
|
|
|
|
0x0300_0000...0x03FF_FFFF => self.iwram.write(u8, address, byte),
|
|
|
|
0x0400_0000...0x0400_03FE => io.write8(self, address, byte),
|
|
|
|
0x0400_0410 => log.info("Ignored write of 0x{X:0>2} to 0x{X:0>8}", .{ byte, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:32 +00:00
|
|
|
0x0E00_0000...0x0FFF_FFFF => self.pak.backup.write(address, byte),
|
|
|
|
else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:12:25 +00:00
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
fn undRead(comptime format: []const u8, args: anytype) u8 {
|
|
|
|
if (panic_on_und_bus) std.debug.panic(format, args) else log.warn(format, args);
|
2022-10-21 08:12:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
|
|
|
fn undWrite(comptime format: []const u8, args: anytype) void {
|
|
|
|
if (panic_on_und_bus) std.debug.panic(format, args) else log.warn(format, args);
|
|
|
|
}
|