feat: impelement a barebones SRAM

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-02-22 17:10:52 -06:00
parent 5368ff912d
commit bc66be6c06
3 changed files with 43 additions and 5 deletions

View File

@ -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 }),
}
}

View File

@ -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 {

31
src/bus/Sram.zig Normal file
View File

@ -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;
}