Compare commits
No commits in common. "29da7b294e5a803b18f621add327b1bd15164487" and "37c039fb9235361e65dde41dd634236122dab991" have entirely different histories.
29da7b294e
...
37c039fb92
|
@ -125,7 +125,6 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
|
|||
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword),
|
||||
0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, 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}),
|
||||
|
||||
else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }),
|
||||
}
|
||||
|
|
35
src/apu.zig
35
src/apu.zig
|
@ -43,10 +43,6 @@ pub const Apu = struct {
|
|||
self.ch_vol_cnt.raw = (self.ch_vol_cnt.raw & 0xFF00) | byte;
|
||||
}
|
||||
|
||||
pub fn setSoundCntLHigh(self: *Self, byte: u8) void {
|
||||
self.ch_vol_cnt.raw = @as(u16, byte) << 8 | (self.ch_vol_cnt.raw & 0xFF);
|
||||
}
|
||||
|
||||
pub fn setBiasHigh(self: *Self, byte: u8) void {
|
||||
self.bias.raw = (@as(u16, byte) << 8) | (self.bias.raw & 0xFF);
|
||||
}
|
||||
|
@ -55,13 +51,9 @@ pub const Apu = struct {
|
|||
const ToneSweep = struct {
|
||||
const Self = @This();
|
||||
|
||||
/// NR10
|
||||
sweep: io.Sweep,
|
||||
/// NR11
|
||||
duty: io.Duty,
|
||||
/// NR12
|
||||
envelope: io.Envelope,
|
||||
/// NR13, NR14
|
||||
freq: io.Frequency,
|
||||
|
||||
fn init() Self {
|
||||
|
@ -73,10 +65,6 @@ const ToneSweep = struct {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn setFreqLow(self: *Self, byte: u8) void {
|
||||
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
|
||||
}
|
||||
|
||||
pub fn setFreqHigh(self: *Self, byte: u8) void {
|
||||
self.freq.raw = (@as(u16, byte) << 8) | (self.freq.raw & 0xFF);
|
||||
}
|
||||
|
@ -85,11 +73,8 @@ const ToneSweep = struct {
|
|||
const Tone = struct {
|
||||
const Self = @This();
|
||||
|
||||
/// NR21
|
||||
duty: io.Duty,
|
||||
/// NR22
|
||||
envelope: io.Envelope,
|
||||
/// NR23, NR24
|
||||
freq: io.Frequency,
|
||||
|
||||
fn init() Self {
|
||||
|
@ -100,12 +85,8 @@ const Tone = struct {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn setFreqLow(self: *Self, byte: u8) void {
|
||||
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
|
||||
}
|
||||
|
||||
pub fn setFreqHigh(self: *Self, byte: u8) void {
|
||||
self.freq.raw = @as(u16, byte) << 8 | (self.freq.raw & 0xFF);
|
||||
self.freq.raw = (self.freq.raw & 0x00FF) | (@as(u16, byte) << 8);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -113,13 +94,10 @@ const Wave = struct {
|
|||
const Self = @This();
|
||||
|
||||
/// Write-only
|
||||
/// NR30
|
||||
select: io.WaveSelect,
|
||||
/// NR31
|
||||
length: u8,
|
||||
/// NR32
|
||||
vol: io.WaveVolume,
|
||||
/// NR33, NR34
|
||||
freq: io.Frequency,
|
||||
|
||||
fn init() Self {
|
||||
|
@ -130,14 +108,6 @@ const Wave = struct {
|
|||
.length = 0,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn setFreqLow(self: *Self, byte: u8) void {
|
||||
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
|
||||
}
|
||||
|
||||
pub fn setFreqHigh(self: *Self, byte: u8) void {
|
||||
self.freq.raw = @as(u16, byte) << 8 | (self.freq.raw & 0xFF);
|
||||
}
|
||||
};
|
||||
|
||||
const Noise = struct {
|
||||
|
@ -146,11 +116,8 @@ const Noise = struct {
|
|||
/// Write-only
|
||||
/// NR41
|
||||
len: u6,
|
||||
/// NR42
|
||||
envelope: io.Envelope,
|
||||
/// NR43
|
||||
poly: io.PolyCounter,
|
||||
/// NR44
|
||||
cnt: io.NoiseControl,
|
||||
|
||||
fn init() Self {
|
||||
|
|
|
@ -23,9 +23,6 @@ pub const Backup = struct {
|
|||
title: [12]u8,
|
||||
save_path: ?[]const u8,
|
||||
|
||||
// TODO: Implement EEPROM
|
||||
flash: Flash,
|
||||
|
||||
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self {
|
||||
const buf_len: usize = switch (kind) {
|
||||
.Sram => 0x8000, // 32K
|
||||
|
@ -43,14 +40,15 @@ pub const Backup = struct {
|
|||
.kind = kind,
|
||||
.title = title,
|
||||
.save_path = path,
|
||||
.flash = Flash.init(),
|
||||
};
|
||||
|
||||
if (backup.save_path) |p| backup.loadSaveFromDisk(p) catch |e| log.err("Failed to load save: {}", .{e});
|
||||
|
||||
return backup;
|
||||
}
|
||||
|
||||
pub fn guessKind(rom: []const u8) ?BackupKind {
|
||||
@setRuntimeSafety(false);
|
||||
|
||||
for (backup_kinds) |needle| {
|
||||
const needle_len = needle.str.len;
|
||||
|
||||
|
@ -65,6 +63,7 @@ pub const Backup = struct {
|
|||
|
||||
pub fn deinit(self: Self) void {
|
||||
if (self.save_path) |path| self.writeSaveToDisk(path) catch |e| log.err("Failed to write save: {}", .{e});
|
||||
|
||||
self.alloc.free(self.buf);
|
||||
}
|
||||
|
||||
|
@ -79,7 +78,7 @@ pub const Backup = struct {
|
|||
defer self.alloc.free(file_buf);
|
||||
|
||||
switch (self.kind) {
|
||||
.Sram, .Flash, .Flash1M => {
|
||||
.Sram => {
|
||||
std.mem.copy(u8, self.buf, file_buf);
|
||||
log.info("Loaded Save from {s}", .{file_path});
|
||||
},
|
||||
|
@ -104,70 +103,37 @@ pub const Backup = struct {
|
|||
defer self.alloc.free(file_path);
|
||||
|
||||
switch (self.kind) {
|
||||
.Sram, .Flash, .Flash1M => {
|
||||
.Sram => {
|
||||
const file = try std.fs.createFileAbsolute(file_path, .{});
|
||||
defer file.close();
|
||||
|
||||
try file.writeAll(self.buf);
|
||||
log.info("Wrote Save to {s}", .{file_path});
|
||||
log.info("Dumped SRAM to {s}", .{file_path});
|
||||
},
|
||||
else => return SaveError.UnsupportedBackupKind,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||
// TODO: Implement Flash and EEPROM
|
||||
switch (self.kind) {
|
||||
.Flash => {
|
||||
switch (idx) {
|
||||
0x0000 => if (self.flash.id_mode) return 0x32, // Panasonic manufacturer ID
|
||||
0x0001 => if (self.flash.id_mode) return 0x1B, // Panasonic device ID
|
||||
else => {},
|
||||
}
|
||||
|
||||
return self.flash.read(self.buf, idx);
|
||||
.Flash => return switch (idx) {
|
||||
0x0000 => 0x32, // Panasonic manufacturer ID
|
||||
0x0001 => 0x1B, // Panasonic device ID
|
||||
else => self.buf[idx],
|
||||
},
|
||||
.Flash1M => {
|
||||
switch (idx) {
|
||||
0x0000 => if (self.flash.id_mode) return 0x62, // Sanyo manufacturer ID
|
||||
0x0001 => if (self.flash.id_mode) return 0x13, // Sanyo device ID
|
||||
else => {},
|
||||
}
|
||||
|
||||
return self.flash.read(self.buf, idx);
|
||||
.Flash1M => return switch (idx) {
|
||||
0x0000 => 0x62, // Sanyo manufacturer ID
|
||||
0x0001 => 0x13, // Sanyo device ID
|
||||
else => self.buf[idx],
|
||||
},
|
||||
.Eeprom => return self.buf[idx],
|
||||
.Sram => return self.buf[idx & 0x7FFF], // 32K SRAM chip is mirrored
|
||||
.Sram => return self.buf[idx & 0x7FFF], // 32K SRAM chips are repeated
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set8(self: *Self, idx: usize, byte: u8) void {
|
||||
switch (self.kind) {
|
||||
.Flash, .Flash1M => {
|
||||
if (self.flash.prep_write) return self.flash.write(self.buf, idx, byte);
|
||||
if (self.flash.shouldEraseSector(idx, byte)) return self.flash.eraseSector(self.buf, idx);
|
||||
|
||||
switch (idx) {
|
||||
0x0000 => if (self.kind == .Flash1M and self.flash.set_bank) {
|
||||
self.flash.bank = @truncate(u1, byte);
|
||||
},
|
||||
0x5555 => {
|
||||
if (self.flash.state == .Command) {
|
||||
self.flash.handleCommand(self.buf, byte);
|
||||
} else if (byte == 0xAA and self.flash.state == .Ready) {
|
||||
self.flash.state = .Set;
|
||||
} else if (byte == 0xF0) {
|
||||
self.flash.state = .Ready;
|
||||
}
|
||||
},
|
||||
0x2AAA => if (byte == 0x55 and self.flash.state == .Set) {
|
||||
self.flash.state = .Command;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.Eeprom => self.buf[idx] = byte,
|
||||
.Sram => self.buf[idx & 0x7FFF] = byte,
|
||||
}
|
||||
self.buf[idx] = byte;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -179,12 +145,10 @@ const BackupKind = enum {
|
|||
};
|
||||
|
||||
const Needle = struct {
|
||||
const Self = @This();
|
||||
|
||||
str: []const u8,
|
||||
kind: BackupKind,
|
||||
|
||||
fn init(str: []const u8, kind: BackupKind) Self {
|
||||
fn init(str: []const u8, kind: BackupKind) @This() {
|
||||
return .{
|
||||
.str = str,
|
||||
.kind = kind,
|
||||
|
@ -195,71 +159,3 @@ const Needle = struct {
|
|||
const SaveError = error{
|
||||
UnsupportedBackupKind,
|
||||
};
|
||||
|
||||
const Flash = struct {
|
||||
const Self = @This();
|
||||
|
||||
state: FlashState,
|
||||
|
||||
id_mode: bool,
|
||||
set_bank: bool,
|
||||
prep_erase: bool,
|
||||
prep_write: bool,
|
||||
|
||||
bank: u1,
|
||||
|
||||
fn init() Self {
|
||||
return .{
|
||||
.state = .Ready,
|
||||
.id_mode = false,
|
||||
.set_bank = false,
|
||||
.prep_erase = false,
|
||||
.prep_write = false,
|
||||
.bank = 0,
|
||||
};
|
||||
}
|
||||
|
||||
fn handleCommand(self: *Self, buf: []u8, byte: u8) void {
|
||||
switch (byte) {
|
||||
0x90 => self.id_mode = true,
|
||||
0xF0 => self.id_mode = false,
|
||||
0xB0 => self.set_bank = true,
|
||||
0x80 => self.prep_erase = true,
|
||||
0x10 => {
|
||||
std.mem.set(u8, buf, 0xFF);
|
||||
self.prep_erase = false;
|
||||
},
|
||||
0xA0 => self.prep_write = true,
|
||||
else => std.debug.panic("Unhandled Flash Command: 0x{X:0>2}", .{byte}),
|
||||
}
|
||||
|
||||
self.state = .Ready;
|
||||
}
|
||||
|
||||
fn shouldEraseSector(self: *const Self, idx: usize, byte: u8) bool {
|
||||
return self.prep_erase and idx & 0xFFF == 0x000 and byte == 0x30;
|
||||
}
|
||||
|
||||
fn write(self: *Self, buf: []u8, idx: usize, byte: u8) void {
|
||||
buf[idx + if (self.bank == 1) 0x1000 else @as(usize, 0)] = byte;
|
||||
self.prep_write = false;
|
||||
}
|
||||
|
||||
fn read(self: *const Self, buf: []u8, idx: usize) u8 {
|
||||
return buf[idx + if (self.bank == 1) 0x1000 else @as(usize, 0)];
|
||||
}
|
||||
|
||||
fn eraseSector(self: *Self, buf: []u8, idx: usize) void {
|
||||
const start = (idx & 0xF000) + if (self.bank == 1) 0x1000 else @as(usize, 0);
|
||||
|
||||
std.mem.set(u8, buf[start..][0..0x1000], 0xFF);
|
||||
self.prep_erase = false;
|
||||
self.state = .Ready;
|
||||
}
|
||||
};
|
||||
|
||||
const FlashState = enum {
|
||||
Ready,
|
||||
Set,
|
||||
Command,
|
||||
};
|
||||
|
|
|
@ -124,9 +124,6 @@ pub fn read16(bus: *const Bus, addr: u32) u16 {
|
|||
// Sound
|
||||
0x0400_0088 => bus.apu.bias.raw,
|
||||
|
||||
// DMA Transfers
|
||||
0x0400_00BA => bus.dma._0.cnt.raw,
|
||||
|
||||
// Timers
|
||||
0x0400_0100 => bus.tim._0.counter(),
|
||||
0x0400_0102 => bus.tim._0.cnt.raw,
|
||||
|
@ -224,8 +221,6 @@ pub fn write16(bus: *Bus, addr: u32, halfword: u16) void {
|
|||
|
||||
// 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}),
|
||||
|
||||
// Interrupts
|
||||
0x0400_0200 => bus.io.ie.raw = halfword,
|
||||
|
@ -244,13 +239,6 @@ pub fn read8(bus: *const Bus, addr: u32) u8 {
|
|||
0x0400_0006 => @truncate(u8, bus.ppu.vcount.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(u8, bus.apu.ch_vol_cnt.raw >> 8),
|
||||
0x0400_0089 => @truncate(u8, bus.apu.bias.raw >> 8),
|
||||
|
||||
// Serial Communication 1
|
||||
|
@ -270,26 +258,14 @@ pub fn write8(bus: *Bus, addr: u32, byte: u8) void {
|
|||
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),
|
||||
|
||||
|
|
Loading…
Reference in New Issue