From 9c87b9820e4a2a339676856b88c997deeb8ffc1f Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:32 -0300 Subject: [PATCH] fix: pass none.gba and kind of sram.gba from jsmolka test suite --- src/Bus.zig | 16 ++++++++++++++-- src/bus/GamePak.zig | 3 ++- src/bus/backup.zig | 12 +++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index db4ff99..7e56660 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -14,6 +14,9 @@ const Scheduler = @import("scheduler.zig").Scheduler; const io = @import("bus/io.zig"); const Allocator = std.mem.Allocator; const log = std.log.scoped(.Bus); + +const rotr = @import("util.zig").rotr; + const Self = @This(); const panic_on_und_bus: bool = false; @@ -68,6 +71,7 @@ pub fn read32(self: *const Self, addr: u32) u32 { 0x0800_0000...0x09FF_FFFF => self.pak.read(u32, addr), 0x0A00_0000...0x0BFF_FFFF => self.pak.read(u32, addr), 0x0C00_0000...0x0DFF_FFFF => self.pak.read(u32, addr), + 0x0E00_0000...0x0FFF_FFFF => @as(u32, self.pak.backup.read(addr)) * 0x01010101, else => undRead("Tried to read from 0x{X:0>8}", .{addr}), }; @@ -86,6 +90,10 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { 0x0500_0000...0x05FF_FFFF => self.ppu.palette.write(u32, addr, word), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u32, addr, word), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u32, addr, word), + 0x0E00_0000...0x0FFF_FFFF => { + const value = rotr(u32, word, 8 * (addr & 3)); + self.pak.backup.write(addr, @truncate(u8, value)); + }, else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), } @@ -108,6 +116,7 @@ pub fn read16(self: *const Self, addr: u32) u16 { 0x0800_0000...0x09FF_FFFF => self.pak.read(u16, addr), 0x0A00_0000...0x0BFF_FFFF => self.pak.read(u16, addr), 0x0C00_0000...0x0DFF_FFFF => self.pak.read(u16, addr), + 0x0E00_0000...0x0FFF_FFFF => @as(u16, self.pak.backup.read(addr)) * 0x0101, else => undRead("Tried to read from 0x{X:0>8}", .{addr}), }; @@ -127,6 +136,9 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { 0x0700_0000...0x07FF_FFFF => self.ppu.oam.write(u16, addr, halfword), 0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}), + // External Memory (Game Pak) + 0x0E00_0000...0x0FFF_FFFF => self.pak.backup.write(addr, @truncate(u8, rotr(u16, halfword, 8 * (addr & 1)))), + else => undWrite("Tried to write 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }), } } @@ -148,7 +160,7 @@ pub fn read8(self: *const Self, addr: u32) u8 { 0x0800_0000...0x09FF_FFFF => self.pak.read(u8, addr), 0x0A00_0000...0x0BFF_FFFF => self.pak.read(u8, addr), 0x0C00_0000...0x0DFF_FFFF => self.pak.read(u8, addr), - 0x0E00_0000...0x0E00_FFFF => self.pak.backup.read(addr), + 0x0E00_0000...0x0FFF_FFFF => self.pak.backup.read(addr), else => undRead("Tried to read from 0x{X:0>2}", .{addr}), }; @@ -163,7 +175,7 @@ pub fn write8(self: *Self, addr: u32, byte: u8) void { 0x0400_0410 => log.info("Ignored write of 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), // External Memory (Game Pak) - 0x0E00_0000...0x0E00_FFFF => self.pak.backup.write(addr, byte), + 0x0E00_0000...0x0FFF_FFFF => self.pak.backup.write(addr, byte), else => undWrite("Tried to write 0x{X:0>2} to 0x{X:0>8}", .{ byte, addr }), } } diff --git a/src/bus/GamePak.zig b/src/bus/GamePak.zig index a87e678..ec3423f 100644 --- a/src/bus/GamePak.zig +++ b/src/bus/GamePak.zig @@ -21,7 +21,7 @@ pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Sel defer alloc.free(file_buf); const title = parseTitle(file_buf); - const kind = Backup.guessKind(file_buf) orelse .Sram; + const kind = Backup.guessKind(file_buf) orelse .None; const buf = try alloc.alloc(u8, 0x200_0000); // 32MiB @@ -40,6 +40,7 @@ pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Sel .backup = try Backup.init(alloc, kind, title, save_path), }; pak.parseHeader(); + log.info("Backup: {}", .{kind}); return pak; } diff --git a/src/bus/backup.zig b/src/bus/backup.zig index 505c617..c9e988d 100644 --- a/src/bus/backup.zig +++ b/src/bus/backup.zig @@ -32,10 +32,11 @@ pub const Backup = struct { .Flash => 0x10000, // 64K .Flash1M => 0x20000, // 128K .Eeprom => 0x2000, // FIXME: We assume 8K here + .None => 0, }; const buf = try alloc.alloc(u8, buf_len); - std.mem.set(u8, buf, 0); + std.mem.set(u8, buf, 0xFF); var backup = Self{ .buf = buf, @@ -80,8 +81,10 @@ pub const Backup = struct { switch (self.kind) { .Sram, .Flash, .Flash1M => { - std.mem.copy(u8, self.buf, file_buf); - log.info("Loaded Save from {s}", .{file_path}); + if (self.buf.len == file_buf.len) { + std.mem.copy(u8, self.buf, file_buf); + log.info("Loaded Save from {s}", .{file_path}); + } }, else => return SaveError.UnsupportedBackupKind, } @@ -139,6 +142,7 @@ pub const Backup = struct { }, .Eeprom => return self.buf[addr], .Sram => return self.buf[addr & 0x7FFF], // 32K SRAM chip is mirrored + .None => return 0xFF, } } @@ -171,6 +175,7 @@ pub const Backup = struct { }, .Eeprom => self.buf[addr] = byte, .Sram => self.buf[addr & 0x7FFF] = byte, + .None => {}, } } }; @@ -180,6 +185,7 @@ const BackupKind = enum { Sram, Flash, Flash1M, + None, }; const Needle = struct {