Compare commits
No commits in common. "6df55c2d86b6a2feaf7443c884fc563652159ae1" and "f8018854be96a0bad19224e44235cc1160b21e65" have entirely different histories.
6df55c2d86
...
f8018854be
82
src/Bus.zig
82
src/Bus.zig
|
@ -54,20 +54,20 @@ pub fn deinit(self: Self) void {
|
||||||
pub fn read32(self: *const Self, addr: u32) u32 {
|
pub fn read32(self: *const Self, addr: u32) u32 {
|
||||||
return switch (addr) {
|
return switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0000_0000...0x0000_3FFF => self.bios.read(u32, addr),
|
0x0000_0000...0x0000_3FFF => self.bios.get32(addr),
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.read(u32, addr),
|
0x0200_0000...0x02FF_FFFF => self.ewram.get32(addr & 0x3FFFF),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.read(u32, addr),
|
0x0300_0000...0x03FF_FFFF => self.iwram.get32(addr & 0x7FFF),
|
||||||
0x0400_0000...0x0400_03FE => io.read32(self, addr),
|
0x0400_0000...0x0400_03FE => io.read32(self, addr),
|
||||||
|
|
||||||
// Internal Display Memory
|
// Internal Display Memory
|
||||||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u32, addr),
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get32(addr & 0x3FF),
|
||||||
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u32, addr),
|
0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(addr - 0x0600_0000),
|
||||||
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u32, addr),
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get32(addr & 0x3FF),
|
||||||
|
|
||||||
// External Memory (Game Pak)
|
// External Memory (Game Pak)
|
||||||
0x0800_0000...0x09FF_FFFF => self.pak.read(u32, addr),
|
0x0800_0000...0x09FF_FFFF => self.pak.get32(addr - 0x0800_0000),
|
||||||
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u32, addr),
|
0x0A00_0000...0x0BFF_FFFF => self.pak.get32(addr - 0x0A00_0000),
|
||||||
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u32, addr),
|
0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000),
|
||||||
|
|
||||||
else => undRead("Tried to read from 0x{X:0>8}", .{addr}),
|
else => undRead("Tried to read from 0x{X:0>8}", .{addr}),
|
||||||
};
|
};
|
||||||
|
@ -78,14 +78,14 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
|
||||||
|
|
||||||
switch (addr) {
|
switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.write(u32, addr, word),
|
0x0200_0000...0x02FF_FFFF => self.ewram.set32(addr & 0x3FFFF, word),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.write(u32, addr, word),
|
0x0300_0000...0x03FF_FFFF => self.iwram.set32(addr & 0x7FFF, word),
|
||||||
0x0400_0000...0x0400_03FE => io.write32(self, addr, word),
|
0x0400_0000...0x0400_03FE => io.write32(self, addr, word),
|
||||||
|
|
||||||
// Internal Display Memory
|
// Internal Display Memory
|
||||||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.write(u32, addr, word),
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set32(addr & 0x3FF, word),
|
||||||
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u32, addr, word),
|
0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word),
|
||||||
0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u32, addr, word),
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word),
|
||||||
|
|
||||||
else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
|
else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
|
||||||
}
|
}
|
||||||
|
@ -94,20 +94,20 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
|
||||||
pub fn read16(self: *const Self, addr: u32) u16 {
|
pub fn read16(self: *const Self, addr: u32) u16 {
|
||||||
return switch (addr) {
|
return switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0000_0000...0x0000_3FFF => self.bios.read(u16, addr),
|
0x0000_0000...0x0000_3FFF => self.bios.get16(addr),
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.read(u16, addr),
|
0x0200_0000...0x02FF_FFFF => self.ewram.get16(addr & 0x3FFFF),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.read(u16, addr),
|
0x0300_0000...0x03FF_FFFF => self.iwram.get16(addr & 0x7FFF),
|
||||||
0x0400_0000...0x0400_03FE => io.read16(self, addr),
|
0x0400_0000...0x0400_03FE => io.read16(self, addr),
|
||||||
|
|
||||||
// Internal Display Memory
|
// Internal Display Memory
|
||||||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u16, addr),
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get16(addr & 0x3FF),
|
||||||
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u16, addr),
|
0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(addr - 0x0600_0000),
|
||||||
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u16, addr),
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get16(addr & 0x3FF),
|
||||||
|
|
||||||
// External Memory (Game Pak)
|
// External Memory (Game Pak)
|
||||||
0x0800_0000...0x09FF_FFFF => self.pak.read(u16, addr),
|
0x0800_0000...0x09FF_FFFF => self.pak.get16(addr - 0x0800_0000),
|
||||||
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u16, addr),
|
0x0A00_0000...0x0BFF_FFFF => self.pak.get16(addr - 0x0A00_0000),
|
||||||
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u16, addr),
|
0x0C00_0000...0x0DFF_FFFF => self.pak.get16(addr - 0x0C00_0000),
|
||||||
|
|
||||||
else => undRead("Tried to read from 0x{X:0>8}", .{addr}),
|
else => undRead("Tried to read from 0x{X:0>8}", .{addr}),
|
||||||
};
|
};
|
||||||
|
@ -117,14 +117,14 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
|
||||||
// TODO: write16 can write to GamePak Flash
|
// TODO: write16 can write to GamePak Flash
|
||||||
switch (addr) {
|
switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.write(u16, addr, halfword),
|
0x0200_0000...0x02FF_FFFF => self.ewram.set16(addr & 0x3FFFF, halfword),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.write(u16, addr, halfword),
|
0x0300_0000...0x03FF_FFFF => self.iwram.set16(addr & 0x7FFF, halfword),
|
||||||
0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword),
|
0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword),
|
||||||
|
|
||||||
// Internal Display Memory
|
// Internal Display Memory
|
||||||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.write(u16, addr, halfword),
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword),
|
||||||
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u16, addr, halfword),
|
0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword),
|
||||||
0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u16, addr, halfword),
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword),
|
||||||
0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}),
|
0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}),
|
||||||
|
|
||||||
else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
|
else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
|
||||||
|
@ -134,21 +134,21 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
|
||||||
pub fn read8(self: *const Self, addr: u32) u8 {
|
pub fn read8(self: *const Self, addr: u32) u8 {
|
||||||
return switch (addr) {
|
return switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0000_0000...0x0000_3FFF => self.bios.read(u8, addr),
|
0x0000_0000...0x0000_3FFF => self.bios.get8(addr),
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.read(u8, addr),
|
0x0200_0000...0x02FF_FFFF => self.ewram.get8(addr & 0x3FFFF),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.read(u8, addr),
|
0x0300_0000...0x03FF_FFFF => self.iwram.get8(addr & 0x7FFF),
|
||||||
0x0400_0000...0x0400_03FE => io.read8(self, addr),
|
0x0400_0000...0x0400_03FE => io.read8(self, addr),
|
||||||
|
|
||||||
// Internal Display Memory
|
// Internal Display Memory
|
||||||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.read(u8, addr),
|
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get8(addr & 0x3FF),
|
||||||
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u8, addr),
|
0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(addr - 0x0600_0000),
|
||||||
0x0700_0000...0x07FF_FFFF => self.ppu.oam.read(u8, addr),
|
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get8(addr & 0x3FF),
|
||||||
|
|
||||||
// External Memory (Game Pak)
|
// External Memory (Game Pak)
|
||||||
0x0800_0000...0x09FF_FFFF => self.pak.read(u8, addr),
|
0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000),
|
||||||
0x0A00_0000...0x0BFF_FFFF => self.pak.read(u8, addr),
|
0x0A00_0000...0x0BFF_FFFF => self.pak.get8(addr - 0x0A00_0000),
|
||||||
0x0C00_0000...0x0DFF_FFFF => self.pak.read(u8, addr),
|
0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000),
|
||||||
0x0E00_0000...0x0E00_FFFF => self.pak.backup.read(addr),
|
0x0E00_0000...0x0E00_FFFF => self.pak.backup.get8(addr & 0xFFFF),
|
||||||
|
|
||||||
else => undRead("Tried to read from 0x{X:0>2}", .{addr}),
|
else => undRead("Tried to read from 0x{X:0>2}", .{addr}),
|
||||||
};
|
};
|
||||||
|
@ -157,13 +157,13 @@ pub fn read8(self: *const Self, addr: u32) u8 {
|
||||||
pub fn write8(self: *Self, addr: u32, byte: u8) void {
|
pub fn write8(self: *Self, addr: u32, byte: u8) void {
|
||||||
switch (addr) {
|
switch (addr) {
|
||||||
// General Internal Memory
|
// General Internal Memory
|
||||||
0x0200_0000...0x02FF_FFFF => self.ewram.write(u8, addr, byte),
|
0x0200_0000...0x02FF_FFFF => self.ewram.set8(addr & 0x3FFFF, byte),
|
||||||
0x0300_0000...0x03FF_FFFF => self.iwram.write(u8, addr, byte),
|
0x0300_0000...0x03FF_FFFF => self.iwram.set8(addr & 0x7FFF, byte),
|
||||||
0x0400_0000...0x0400_03FE => io.write8(self, addr, byte),
|
0x0400_0000...0x0400_03FE => io.write8(self, addr, byte),
|
||||||
0x0400_0410 => log.info("Ignored write of 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
|
0x0400_0410 => log.info("Ignored write of 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
|
||||||
|
|
||||||
// External Memory (Game Pak)
|
// External Memory (Game Pak)
|
||||||
0x0E00_0000...0x0E00_FFFF => self.pak.backup.write(addr, byte),
|
0x0E00_0000...0x0E00_FFFF => self.pak.backup.set8(addr & 0xFFFF, byte),
|
||||||
else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
|
else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,15 +26,23 @@ pub fn deinit(self: Self) void {
|
||||||
if (self.buf) |buf| self.alloc.free(buf);
|
if (self.buf) |buf| self.alloc.free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, addr: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
if (self.buf) |buf| {
|
if (self.buf) |buf|
|
||||||
return switch (T) {
|
return (@as(u32, buf[idx + 3]) << 24) | (@as(u32, buf[idx + 2]) << 16) | (@as(u32, buf[idx + 1]) << 8) | (@as(u32, buf[idx]));
|
||||||
u32 => (@as(u32, buf[addr + 3]) << 24) | (@as(u32, buf[addr + 2]) << 16) | (@as(u32, buf[addr + 1]) << 8) | (@as(u32, buf[addr])),
|
|
||||||
u16 => (@as(u16, buf[addr + 1]) << 8) | @as(u16, buf[addr]),
|
std.debug.panic("[CPU/BIOS:32] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
|
||||||
u8 => buf[addr],
|
|
||||||
else => @compileError("BIOS: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.panic("[BIOS] ZBA tried to read {} from 0x{X:0>8} but not BIOS was present", .{ T, addr });
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
|
if (self.buf) |buf|
|
||||||
|
return (@as(u16, buf[idx + 1]) << 8) | @as(u16, buf[idx]);
|
||||||
|
|
||||||
|
std.debug.panic("[CPU/BIOS:16] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
if (self.buf) |buf|
|
||||||
|
return buf[idx];
|
||||||
|
|
||||||
|
std.debug.panic("[CPU/BIOS:8] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,32 +20,28 @@ pub fn deinit(self: Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = address & 0x3FFFF;
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(u32, self.buf[addr + 3]) << 24) | (@as(u32, self.buf[addr + 2]) << 16) | (@as(u32, self.buf[addr + 1]) << 8) | (@as(u32, self.buf[addr])),
|
|
||||||
u16 => (@as(u16, self.buf[addr + 1]) << 8) | @as(u16, self.buf[addr]),
|
|
||||||
u8 => self.buf[addr],
|
|
||||||
else => @compileError("EWRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *const Self, comptime T: type, address: usize, value: T) void {
|
pub fn set32(self: *Self, idx: usize, word: u32) void {
|
||||||
const addr = address & 0x3FFFF;
|
self.set16(idx + 2, @truncate(u16, word >> 16));
|
||||||
|
self.set16(idx, @truncate(u16, word));
|
||||||
return switch (T) {
|
}
|
||||||
u32 => {
|
|
||||||
self.buf[addr + 3] = @truncate(u8, value >> 24);
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
self.buf[addr + 2] = @truncate(u8, value >> 16);
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
}
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
||||||
u16 => {
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
self.buf[idx] = @truncate(u8, halfword);
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
}
|
||||||
},
|
|
||||||
u8 => self.buf[addr] = value,
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
else => @compileError("EWRAM: Unsupported write width"),
|
return self.buf[idx];
|
||||||
};
|
}
|
||||||
|
|
||||||
|
pub fn set8(self: *Self, idx: usize, byte: u8) void {
|
||||||
|
self.buf[idx] = byte;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,6 @@ const std = @import("std");
|
||||||
const Backup = @import("backup.zig").Backup;
|
const Backup = @import("backup.zig").Backup;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const log = std.log.scoped(.GamePak);
|
const log = std.log.scoped(.GamePak);
|
||||||
|
|
||||||
const intToBytes = @import("../util.zig").intToBytes;
|
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
title: [12]u8,
|
title: [12]u8,
|
||||||
|
@ -17,21 +14,11 @@ pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Sel
|
||||||
const file = try std.fs.cwd().openFile(rom_path, .{});
|
const file = try std.fs.cwd().openFile(rom_path, .{});
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
const file_buf = try file.readToEndAlloc(alloc, try file.getEndPos());
|
const len = try file.getEndPos();
|
||||||
defer alloc.free(file_buf);
|
const buf = try file.readToEndAlloc(alloc, len);
|
||||||
|
const title = parseTitle(buf);
|
||||||
|
|
||||||
const title = parseTitle(file_buf);
|
const kind = Backup.guessKind(buf) orelse .Sram;
|
||||||
const kind = Backup.guessKind(file_buf) orelse .Sram;
|
|
||||||
|
|
||||||
const buf = try alloc.alloc(u8, 0x200_0000); // 32MiB
|
|
||||||
|
|
||||||
// GamePak addressable space has known values if there's no cartridge inserted
|
|
||||||
var i: usize = 0;
|
|
||||||
while (i < buf.len) : (i += @sizeOf(u16)) {
|
|
||||||
std.mem.copy(u8, buf[i..][0..2], &intToBytes(u16, @truncate(u16, i / 2)));
|
|
||||||
}
|
|
||||||
|
|
||||||
std.mem.copy(u8, buf[0..file_buf.len], file_buf[0..file_buf.len]); // Copy over ROM
|
|
||||||
|
|
||||||
const pak = Self{
|
const pak = Self{
|
||||||
.buf = buf,
|
.buf = buf,
|
||||||
|
@ -73,13 +60,14 @@ pub fn deinit(self: Self) void {
|
||||||
self.backup.deinit();
|
self.backup.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: u32) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = address & 0x1FF_FFFF;
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
||||||
|
}
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
u16 => (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
u8 => self.buf[addr],
|
}
|
||||||
else => @compileError("GamePak: Unsupported read width"),
|
|
||||||
};
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
return self.buf[idx];
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,32 +20,30 @@ pub fn deinit(self: Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = address & 0x7FFF;
|
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(u32, self.buf[addr + 3]) << 24) | (@as(u32, self.buf[addr + 2]) << 16) | (@as(u32, self.buf[addr + 1]) << 8) | (@as(u32, self.buf[addr])),
|
|
||||||
u16 => (@as(u16, self.buf[addr + 1]) << 8) | @as(u16, self.buf[addr]),
|
|
||||||
u8 => self.buf[addr],
|
|
||||||
else => @compileError("IWRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *const Self, comptime T: type, address: usize, value: T) void {
|
pub fn set32(self: *Self, idx: usize, word: u32) void {
|
||||||
const addr = address & 0x7FFF;
|
self.buf[idx + 3] = @truncate(u8, word >> 24);
|
||||||
|
self.buf[idx + 2] = @truncate(u8, word >> 16);
|
||||||
return switch (T) {
|
self.buf[idx + 1] = @truncate(u8, word >> 8);
|
||||||
u32 => {
|
self.buf[idx] = @truncate(u8, word);
|
||||||
self.buf[addr + 3] = @truncate(u8, value >> 24);
|
}
|
||||||
self.buf[addr + 2] = @truncate(u8, value >> 16);
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
},
|
}
|
||||||
u16 => {
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
||||||
},
|
self.buf[idx] = @truncate(u8, halfword);
|
||||||
u8 => self.buf[addr] = value,
|
}
|
||||||
else => @compileError("IWRAM: Unsupported write width"),
|
|
||||||
};
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
return self.buf[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set8(self: *Self, idx: usize, byte: u8) void {
|
||||||
|
self.buf[idx] = byte;
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,42 +115,38 @@ pub const Backup = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, address: usize) u8 {
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
const addr = address & 0xFFFF;
|
|
||||||
|
|
||||||
switch (self.kind) {
|
switch (self.kind) {
|
||||||
.Flash => {
|
.Flash => {
|
||||||
switch (addr) {
|
switch (idx) {
|
||||||
0x0000 => if (self.flash.id_mode) return 0x32, // Panasonic manufacturer ID
|
0x0000 => if (self.flash.id_mode) return 0x32, // Panasonic manufacturer ID
|
||||||
0x0001 => if (self.flash.id_mode) return 0x1B, // Panasonic device ID
|
0x0001 => if (self.flash.id_mode) return 0x1B, // Panasonic device ID
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.flash.read(self.buf, addr);
|
return self.flash.read(self.buf, idx);
|
||||||
},
|
},
|
||||||
.Flash1M => {
|
.Flash1M => {
|
||||||
switch (addr) {
|
switch (idx) {
|
||||||
0x0000 => if (self.flash.id_mode) return 0x62, // Sanyo manufacturer ID
|
0x0000 => if (self.flash.id_mode) return 0x62, // Sanyo manufacturer ID
|
||||||
0x0001 => if (self.flash.id_mode) return 0x13, // Sanyo device ID
|
0x0001 => if (self.flash.id_mode) return 0x13, // Sanyo device ID
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.flash.read(self.buf, addr);
|
return self.flash.read(self.buf, idx);
|
||||||
},
|
},
|
||||||
.Eeprom => return self.buf[addr],
|
.Eeprom => return self.buf[idx],
|
||||||
.Sram => return self.buf[addr & 0x7FFF], // 32K SRAM chip is mirrored
|
.Sram => return self.buf[idx & 0x7FFF], // 32K SRAM chip is mirrored
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *Self, address: usize, byte: u8) void {
|
pub fn set8(self: *Self, idx: usize, byte: u8) void {
|
||||||
const addr = address & 0xFFFF;
|
|
||||||
|
|
||||||
switch (self.kind) {
|
switch (self.kind) {
|
||||||
.Flash, .Flash1M => {
|
.Flash, .Flash1M => {
|
||||||
if (self.flash.prep_write) return self.flash.write(self.buf, addr, byte);
|
if (self.flash.prep_write) return self.flash.write(self.buf, idx, byte);
|
||||||
if (self.flash.shouldEraseSector(addr, byte)) return self.flash.eraseSector(self.buf, addr);
|
if (self.flash.shouldEraseSector(idx, byte)) return self.flash.eraseSector(self.buf, idx);
|
||||||
|
|
||||||
switch (addr) {
|
switch (idx) {
|
||||||
0x0000 => if (self.kind == .Flash1M and self.flash.set_bank) {
|
0x0000 => if (self.kind == .Flash1M and self.flash.set_bank) {
|
||||||
self.flash.bank = @truncate(u1, byte);
|
self.flash.bank = @truncate(u1, byte);
|
||||||
},
|
},
|
||||||
|
@ -169,8 +165,8 @@ pub const Backup = struct {
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Eeprom => self.buf[addr] = byte,
|
.Eeprom => self.buf[idx] = byte,
|
||||||
.Sram => self.buf[addr & 0x7FFF] = byte,
|
.Sram => self.buf[idx & 0x7FFF] = byte,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
146
src/ppu.zig
146
src/ppu.zig
|
@ -92,11 +92,11 @@ pub const Ppu = struct {
|
||||||
search: while (i < self.oam.buf.len) : (i += 8) {
|
search: while (i < self.oam.buf.len) : (i += 8) {
|
||||||
// Attributes in OAM are 6 bytes long, with 2 bytes of padding
|
// Attributes in OAM are 6 bytes long, with 2 bytes of padding
|
||||||
// Grab Attributes from OAM
|
// Grab Attributes from OAM
|
||||||
const attr0 = @bitCast(Attr0, self.oam.read(u16, i));
|
const attr0 = @bitCast(Attr0, self.oam.get16(i));
|
||||||
|
|
||||||
// Only consider enabled Sprites
|
// Only consider enabled Sprites
|
||||||
if (!attr0.disabled.read()) {
|
if (!attr0.disabled.read()) {
|
||||||
const attr1 = @bitCast(Attr1, self.oam.read(u16, i + 2));
|
const attr1 = @bitCast(Attr1, self.oam.get16(i + 2));
|
||||||
|
|
||||||
// When fetching sprites we only care about ones that could be rendered
|
// When fetching sprites we only care about ones that could be rendered
|
||||||
// on this scanline
|
// on this scanline
|
||||||
|
@ -113,7 +113,7 @@ pub const Ppu = struct {
|
||||||
if ((start <= y and y < end) or (istart <= iy and iy < iend)) {
|
if ((start <= y and y < end) or (istart <= iy and iy < iend)) {
|
||||||
for (self.scanline_sprites) |*maybe_sprite| {
|
for (self.scanline_sprites) |*maybe_sprite| {
|
||||||
if (maybe_sprite.* == null) {
|
if (maybe_sprite.* == null) {
|
||||||
maybe_sprite.* = Sprite.init(attr0, attr1, @bitCast(Attr2, self.oam.read(u16, i + 4)));
|
maybe_sprite.* = Sprite.init(attr0, attr1, @bitCast(Attr2, self.oam.get16(i + 4)));
|
||||||
continue :search;
|
continue :search;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,7 @@ pub const Ppu = struct {
|
||||||
} else tile;
|
} else tile;
|
||||||
|
|
||||||
// Sprite Palette starts at 0x0500_0200
|
// Sprite Palette starts at 0x0500_0200
|
||||||
if (pal_id != 0) self.scanline_buf[@bitCast(u9, x)] = self.palette.read(u16, 0x200 + pal_id * 2);
|
if (pal_id != 0) self.scanline_buf[@bitCast(u9, x)] = self.palette.get16(0x200 + pal_id * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drawBackround(self: *Self, comptime n: u3) void {
|
fn drawBackround(self: *Self, comptime n: u3) void {
|
||||||
|
@ -241,7 +241,7 @@ pub const Ppu = struct {
|
||||||
|
|
||||||
// Grab the Screen Entry from VRAM
|
// Grab the Screen Entry from VRAM
|
||||||
const entry_addr = screen_base + tilemapOffset(size, x, y);
|
const entry_addr = screen_base + tilemapOffset(size, x, y);
|
||||||
const entry = @bitCast(ScreenEntry, self.vram.read(u16, entry_addr));
|
const entry = @bitCast(ScreenEntry, self.vram.get16(entry_addr));
|
||||||
|
|
||||||
// Calculate the Address of the Tile in the designated Charblock
|
// Calculate the Address of the Tile in the designated Charblock
|
||||||
// We also take this opportunity to flip tiles if necessary
|
// We also take this opportunity to flip tiles if necessary
|
||||||
|
@ -266,7 +266,7 @@ pub const Ppu = struct {
|
||||||
break :blk pal_bank | nybble_tile;
|
break :blk pal_bank | nybble_tile;
|
||||||
} else tile;
|
} else tile;
|
||||||
|
|
||||||
if (pal_id != 0) self.scanline_buf[i] = self.palette.read(u16, pal_id * 2);
|
if (pal_id != 0) self.scanline_buf[i] = self.palette.get16(pal_id * 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ pub const Ppu = struct {
|
||||||
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < width) : (i += 1) {
|
while (i < width) : (i += 1) {
|
||||||
const bgr555 = self.vram.read(u16, vram_base + i * @sizeOf(u16));
|
const bgr555 = self.vram.get16(vram_base + i * @sizeOf(u16));
|
||||||
std.mem.copy(u8, self.framebuf[fb_base + i * @sizeOf(u32) ..][0..4], &intToBytes(u32, toRgba8888(bgr555)));
|
std.mem.copy(u8, self.framebuf[fb_base + i * @sizeOf(u32) ..][0..4], &intToBytes(u32, toRgba8888(bgr555)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -321,7 +321,7 @@ pub const Ppu = struct {
|
||||||
// Render Current Scanline
|
// Render Current Scanline
|
||||||
for (self.vram.buf[vram_base .. vram_base + width]) |byte, i| {
|
for (self.vram.buf[vram_base .. vram_base + width]) |byte, i| {
|
||||||
const pal_id = @as(u16, byte) * @sizeOf(u16);
|
const pal_id = @as(u16, byte) * @sizeOf(u16);
|
||||||
const bgr555 = self.palette.read(u16, pal_id);
|
const bgr555 = self.palette.get16(pal_id);
|
||||||
|
|
||||||
std.mem.copy(u8, self.framebuf[fb_base + i * @sizeOf(u32) ..][0..4], &intToBytes(u32, toRgba8888(bgr555)));
|
std.mem.copy(u8, self.framebuf[fb_base + i * @sizeOf(u32) ..][0..4], &intToBytes(u32, toRgba8888(bgr555)));
|
||||||
}
|
}
|
||||||
|
@ -432,37 +432,30 @@ const Palette = struct {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = address & 0x3FF;
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
|
|
||||||
u16 => (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
|
|
||||||
u8 => self.buf[addr],
|
|
||||||
else => @compileError("PALRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
|
pub fn set32(self: *Self, idx: usize, word: u32) void {
|
||||||
const addr = address & 0x3FF;
|
self.set16(idx + 2, @truncate(u16, word >> 16));
|
||||||
|
self.set16(idx, @truncate(u16, word));
|
||||||
switch (T) {
|
|
||||||
u32 => {
|
|
||||||
self.buf[addr + 3] = @truncate(u8, value >> 24);
|
|
||||||
self.buf[addr + 2] = @truncate(u8, value >> 16);
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
u16 => {
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
else => @compileError("PALRAM: Unsupported write width"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
||||||
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
||||||
|
self.buf[idx] = @truncate(u8, halfword);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
return self.buf[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getBackdrop(self: *const Self) u16 {
|
fn getBackdrop(self: *const Self) u16 {
|
||||||
return self.read(u16, 0);
|
return self.get16(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -486,38 +479,26 @@ const Vram = struct {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = Self.mirror(address);
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
|
|
||||||
u16 => (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
|
|
||||||
u8 => self.buf[addr],
|
|
||||||
else => @compileError("VRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
|
pub fn set32(self: *Self, idx: usize, word: u32) void {
|
||||||
const addr = Self.mirror(address);
|
self.set16(idx + 2, @truncate(u16, word >> 16));
|
||||||
|
self.set16(idx, @truncate(u16, word));
|
||||||
switch (T) {
|
|
||||||
u32 => {
|
|
||||||
self.buf[addr + 3] = @truncate(u8, value >> 24);
|
|
||||||
self.buf[addr + 2] = @truncate(u8, value >> 16);
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
u16 => {
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
else => @compileError("VRAM: Unsupported write width"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mirror(address: usize) usize {
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
const addr = address & 0x1FFFF; // repeated in steps of 128KiB
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
return if (addr >= 0x18000) addr & 0x7FFF else addr; // 64K + 32K + 32K (abcc)
|
}
|
||||||
|
|
||||||
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
||||||
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
||||||
|
self.buf[idx] = @truncate(u8, halfword);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
return self.buf[idx];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -541,33 +522,28 @@ const Oam = struct {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn get32(self: *const Self, idx: usize) u32 {
|
||||||
const addr = address & 0x3FF;
|
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32 => (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
|
|
||||||
u16 => (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
|
|
||||||
u8 => self.buf[addr],
|
|
||||||
else => @compileError("OAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
|
pub fn set32(self: *Self, idx: usize, word: u32) void {
|
||||||
const addr = address & 0x3FF;
|
self.buf[idx + 3] = @truncate(u8, word >> 24);
|
||||||
|
self.buf[idx + 2] = @truncate(u8, word >> 16);
|
||||||
switch (T) {
|
self.buf[idx + 1] = @truncate(u8, word >> 8);
|
||||||
u32 => {
|
self.buf[idx] = @truncate(u8, word);
|
||||||
self.buf[addr + 3] = @truncate(u8, value >> 24);
|
|
||||||
self.buf[addr + 2] = @truncate(u8, value >> 16);
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
u16 => {
|
|
||||||
self.buf[addr + 1] = @truncate(u8, value >> 8);
|
|
||||||
self.buf[addr + 0] = @truncate(u8, value >> 0);
|
|
||||||
},
|
|
||||||
else => @compileError("OAM: Unsupported write width"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
||||||
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
||||||
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
||||||
|
self.buf[idx] = @truncate(u8, halfword);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
|
return self.buf[idx];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,11 @@ pub inline fn sext(comptime bits: comptime_int, value: u32) u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See https://godbolt.org/z/W3en9Eche
|
/// See https://godbolt.org/z/W3en9Eche
|
||||||
pub inline fn rotr(comptime T: type, x: T, r: anytype) T {
|
pub inline fn rotr(comptime T: type, value: T, r: anytype) T {
|
||||||
if (@typeInfo(T).Int.signedness == .signed)
|
comptime std.debug.assert(@typeInfo(T).Int.signedness == .unsigned);
|
||||||
@compileError("cannot rotate signed integer");
|
const ar = @truncate(Log2Int(T), r);
|
||||||
|
|
||||||
const ar = @intCast(Log2Int(T), @mod(r, @typeInfo(T).Int.bits));
|
return value >> ar | value << @truncate(Log2Int(T), @typeInfo(T).Int.bits - @as(T, ar));
|
||||||
return x >> ar | x << (1 +% ~ar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const FpsAverage = struct {
|
pub const FpsAverage = struct {
|
||||||
|
|
Loading…
Reference in New Issue