From bc66be6c0643e6f82efd9d790b094322d7bfe7da Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 22 Feb 2022 17:10:52 -0600 Subject: [PATCH] feat: impelement a barebones SRAM --- src/Bus.zig | 4 ++-- src/bus/GamePak.zig | 13 ++++++++++--- src/bus/Sram.zig | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/bus/Sram.zig diff --git a/src/Bus.zig b/src/Bus.zig index 70ab917..0c6d38d 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -138,7 +138,7 @@ pub fn read8(self: *const Self, addr: u32) u8 { 0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), 0x0A00_0000...0x0BFF_FFFF => self.pak.get8(addr - 0x0A00_0000), 0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000), - 0x0E00_0000...0x0E00_FFFF => std.debug.panic("Read from 0x{X:0>2} in Game Pak SRAM", .{addr}), + 0x0E00_0000...0x0E00_FFFF => self.pak.sram.get8(addr - 0x0E00_0000), else => std.debug.panic("Tried to read from 0x{X:0>2}", .{addr}), }; @@ -153,7 +153,7 @@ pub fn write8(self: *Self, addr: u32, byte: u8) void { 0x0400_0410 => log.info("Wrote 0x{X:0>2} to 0x{X:0>8}. Ignored", .{ byte, addr }), // External Memory (Game Pak) - 0x0E00_0000...0x0E00_FFFF => log.err("Wrote 0x{X:0>2} to 0x{X:0>8} in Game Pak SRAM", .{ byte, addr }), + 0x0E00_0000...0x0E00_FFFF => self.pak.sram.set8(addr - 0x0E00_0000, byte), else => std.debug.panic("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 1697b90..de3deb7 100644 --- a/src/bus/GamePak.zig +++ b/src/bus/GamePak.zig @@ -1,13 +1,14 @@ const std = @import("std"); +const Sram = @import("Sram.zig"); const Allocator = std.mem.Allocator; +const log = std.log.scoped(.GamePak); const Self = @This(); title: [12]u8, buf: []u8, alloc: Allocator, - -const log = std.log.scoped(.GamePak); +sram: Sram, pub fn init(alloc: Allocator, path: []const u8) !Self { const file = try std.fs.cwd().openFile(path, .{}); @@ -17,7 +18,12 @@ pub fn init(alloc: Allocator, path: []const u8) !Self { const buf = try file.readToEndAlloc(alloc, len); const title = parseTitle(buf); - const pak = Self{ .buf = buf, .alloc = alloc, .title = title }; + const pak = Self{ + .buf = buf, + .alloc = alloc, + .title = title, + .sram = try Sram.init(alloc), + }; pak.parseHeader(); return pak; @@ -49,6 +55,7 @@ fn lookupMaker(slice: *const [2]u8) ?[]const u8 { pub fn deinit(self: Self) void { self.alloc.free(self.buf); + self.sram.deinit(); } pub fn get32(self: *const Self, idx: usize) u32 { diff --git a/src/bus/Sram.zig b/src/bus/Sram.zig new file mode 100644 index 0000000..ae6705f --- /dev/null +++ b/src/bus/Sram.zig @@ -0,0 +1,31 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; +const log = std.log.scoped(.SRAM); +const Self = @This(); + +buf: []u8, +alloc: Allocator, + +pub fn init(alloc: Allocator) !Self { + // FIXME: SRAM is more than just a 64KB block of memory + const buf = try alloc.alloc(u8, 0x10000); + std.mem.set(u8, buf, 0); + + return Self{ + .buf = buf, + .alloc = alloc, + }; +} + +pub fn deinit(self: Self) void { + self.alloc.free(self.buf); +} + +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; +}