2022-10-21 08:11:50 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
const AudioDeviceId = @import("sdl2").SDL_AudioDeviceID;
|
2022-10-21 08:11:50 +00:00
|
|
|
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:12:33 +00:00
|
|
|
const FilePaths = @import("util.zig").FilePaths;
|
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:11:50 +00:00
|
|
|
io: Io,
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
sched: *Scheduler,
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
pub fn init(alloc: Allocator, sched: *Scheduler, dev: AudioDeviceId, paths: FilePaths) !Self {
|
2022-10-21 08:11:51 +00:00
|
|
|
return Self{
|
2022-10-21 08:12:33 +00:00
|
|
|
.pak = try GamePak.init(alloc, paths.rom, paths.save),
|
|
|
|
.bios = try Bios.init(alloc, paths.bios),
|
2022-10-21 08:11:50 +00:00
|
|
|
.ppu = try Ppu.init(alloc, sched),
|
2022-10-21 08:12:33 +00:00
|
|
|
.apu = Apu.init(dev),
|
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:12:33 +00:00
|
|
|
.sched = sched,
|
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:33 +00:00
|
|
|
pub fn handleDMATransfers(self: *Self) void {
|
|
|
|
while (self.isDmaRunning()) {
|
|
|
|
if (self.dma._1.step(self)) continue;
|
|
|
|
if (self.dma._0.step(self)) continue;
|
|
|
|
if (self.dma._2.step(self)) continue;
|
|
|
|
if (self.dma._3.step(self)) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn isDmaRunning(self: *const Self) bool {
|
|
|
|
return self.dma._0.active or
|
|
|
|
self.dma._1.active or
|
|
|
|
self.dma._2.active or
|
|
|
|
self.dma._3.active;
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
pub fn read(self: *const Self, comptime T: type, address: u32) T {
|
|
|
|
const page = @truncate(u8, address >> 24);
|
|
|
|
const align_addr = alignAddress(T, address);
|
2022-10-21 08:12:33 +00:00
|
|
|
self.sched.tick += 1;
|
2022-10-21 08:12:32 +00:00
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
return switch (page) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:33 +00:00
|
|
|
0x00 => self.bios.read(T, align_addr),
|
|
|
|
0x02 => self.ewram.read(T, align_addr),
|
|
|
|
0x03 => self.iwram.read(T, align_addr),
|
2022-10-21 08:12:33 +00:00
|
|
|
0x04 => io.read(self, T, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:33 +00:00
|
|
|
0x05 => self.ppu.palette.read(T, align_addr),
|
|
|
|
0x06 => self.ppu.vram.read(T, align_addr),
|
|
|
|
0x07 => self.ppu.oam.read(T, align_addr),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// External Memory (Game Pak)
|
2022-10-21 08:12:33 +00:00
|
|
|
0x08...0x0D => self.pak.read(T, align_addr),
|
|
|
|
0x0E...0x0F => blk: {
|
|
|
|
const value = self.pak.backup.read(address);
|
|
|
|
|
|
|
|
const multiplier = switch (T) {
|
|
|
|
u32 => 0x01010101,
|
|
|
|
u16 => 0x0101,
|
|
|
|
u8 => 1,
|
|
|
|
else => @compileError("Backup: Unsupported read width"),
|
|
|
|
};
|
|
|
|
|
|
|
|
break :blk @as(T, value) * multiplier;
|
|
|
|
},
|
|
|
|
else => undRead("Tried to read {} from 0x{X:0>8}", .{ T, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
pub fn write(self: *Self, comptime T: type, address: u32, value: T) void {
|
|
|
|
const page = @truncate(u8, address >> 24);
|
|
|
|
const align_addr = alignAddress(T, address);
|
2022-10-21 08:12:33 +00:00
|
|
|
self.sched.tick += 1;
|
2022-10-21 08:12:32 +00:00
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
switch (page) {
|
2022-10-21 08:11:50 +00:00
|
|
|
// General Internal Memory
|
2022-10-21 08:12:33 +00:00
|
|
|
0x00 => self.bios.write(T, align_addr, value),
|
|
|
|
0x02 => self.ewram.write(T, align_addr, value),
|
|
|
|
0x03 => self.iwram.write(T, align_addr, value),
|
2022-10-21 08:12:33 +00:00
|
|
|
0x04 => io.write(self, T, align_addr, value),
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
// Internal Display Memory
|
2022-10-21 08:12:33 +00:00
|
|
|
0x05 => self.ppu.palette.write(T, align_addr, value),
|
|
|
|
0x06 => self.ppu.vram.write(T, align_addr, value),
|
|
|
|
0x07 => self.ppu.oam.write(T, align_addr, value),
|
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:33 +00:00
|
|
|
0x08...0x0D => {},
|
|
|
|
0x0E...0x0F => {
|
|
|
|
const rotate_by = switch (T) {
|
|
|
|
u32 => address & 3,
|
|
|
|
u16 => address & 1,
|
|
|
|
u8 => 0,
|
|
|
|
else => @compileError("Backup: Unsupported write width"),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.pak.backup.write(address, @truncate(u8, rotr(T, value, 8 * rotate_by)));
|
2022-10-21 08:12:32 +00:00
|
|
|
},
|
2022-10-21 08:12:33 +00:00
|
|
|
else => undWrite("Tried to write {} 0x{X:} to 0x{X:0>8}", .{ T, value, address }),
|
2022-10-21 08:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:33 +00:00
|
|
|
fn alignAddress(comptime T: type, address: u32) u32 {
|
|
|
|
return switch (T) {
|
|
|
|
u32 => address & 0xFFFF_FFFC,
|
|
|
|
u16 => address & 0xFFFF_FFFE,
|
|
|
|
u8 => address,
|
|
|
|
else => @compileError("Bus: Invalid read/write type"),
|
2022-10-21 08:11:50 +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);
|
|
|
|
}
|