feat: implement SRAM saving and loading
This commit is contained in:
parent
da4bb17782
commit
bd54cba8a0
|
@ -4,3 +4,6 @@
|
||||||
[submodule "lib/zig-clap"]
|
[submodule "lib/zig-clap"]
|
||||||
path = lib/zig-clap
|
path = lib/zig-clap
|
||||||
url = https://github.com/Hejsil/zig-clap
|
url = https://github.com/Hejsil/zig-clap
|
||||||
|
[submodule "lib/known-folders"]
|
||||||
|
path = lib/known-folders
|
||||||
|
url = https://github.com/ziglibs/known-folders
|
||||||
|
|
|
@ -13,6 +13,9 @@ pub fn build(b: *std.build.Builder) void {
|
||||||
const mode = b.standardReleaseOptions();
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
const exe = b.addExecutable("zba", "src/main.zig");
|
const exe = b.addExecutable("zba", "src/main.zig");
|
||||||
|
|
||||||
|
// Known Folders (%APPDATA%, XDG, etc.)
|
||||||
|
exe.addPackagePath("known_folders", "lib/known-folders/known-folders.zig");
|
||||||
|
|
||||||
// Bitfield type from FlorenceOS: https://github.com/FlorenceOS/
|
// Bitfield type from FlorenceOS: https://github.com/FlorenceOS/
|
||||||
// exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "lib/util/bitfield.zig" } });
|
// exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "lib/util/bitfield.zig" } });
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 9db1b99219c767d5e24994b1525273fe4031e464
|
|
@ -29,10 +29,10 @@ ewram: Ewram,
|
||||||
|
|
||||||
io: Io,
|
io: Io,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, sched: *Scheduler, rom_path: []const u8, maybe_bios: ?[]const u8) !Self {
|
pub fn init(alloc: Allocator, sched: *Scheduler, rom_path: []const u8, bios_path: ?[]const u8, save_path: ?[]const u8) !Self {
|
||||||
return Self{
|
return Self{
|
||||||
.pak = try GamePak.init(alloc, rom_path),
|
.pak = try GamePak.init(alloc, rom_path, save_path),
|
||||||
.bios = try Bios.init(alloc, maybe_bios),
|
.bios = try Bios.init(alloc, bios_path),
|
||||||
.ppu = try Ppu.init(alloc, sched),
|
.ppu = try Ppu.init(alloc, sched),
|
||||||
.apu = Apu.init(),
|
.apu = Apu.init(),
|
||||||
.iwram = try Iwram.init(alloc),
|
.iwram = try Iwram.init(alloc),
|
||||||
|
|
|
@ -10,8 +10,8 @@ buf: []u8,
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
backup: Backup,
|
backup: Backup,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, path: []const u8) !Self {
|
pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self {
|
||||||
const file = try std.fs.cwd().openFile(path, .{});
|
const file = try std.fs.cwd().openFile(rom_path, .{});
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
const len = try file.getEndPos();
|
const len = try file.getEndPos();
|
||||||
|
@ -24,7 +24,7 @@ pub fn init(alloc: Allocator, path: []const u8) !Self {
|
||||||
.buf = buf,
|
.buf = buf,
|
||||||
.alloc = alloc,
|
.alloc = alloc,
|
||||||
.title = title,
|
.title = title,
|
||||||
.backup = try Backup.init(alloc, kind),
|
.backup = try Backup.init(alloc, kind, title, save_path),
|
||||||
};
|
};
|
||||||
pak.parseHeader();
|
pak.parseHeader();
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ fn parseHeader(self: *const Self) void {
|
||||||
log.info("Title: {s}", .{title});
|
log.info("Title: {s}", .{title});
|
||||||
if (version != 0) log.info("Version: {}", .{version});
|
if (version != 0) log.info("Version: {}", .{version});
|
||||||
log.info("Game Code: {s}", .{code});
|
log.info("Game Code: {s}", .{code});
|
||||||
if (lookupMaker(maker)) |c| log.info("Maker Code: {s}", .{c}) else log.info("Maker: {s}", .{maker});
|
if (lookupMaker(maker)) |c| log.info("Maker: {s}", .{c}) else log.info("Maker Code: {s}", .{maker});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseTitle(buf: []u8) [12]u8 {
|
fn parseTitle(buf: []u8) [12]u8 {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const log = std.log.scoped(.Backup);
|
const log = std.log.scoped(.Backup);
|
||||||
|
|
||||||
|
const correctTitle = @import("../util.zig").correctTitle;
|
||||||
|
|
||||||
const backup_kinds = [5]Needle{
|
const backup_kinds = [5]Needle{
|
||||||
.{ .str = "EEPROM_V", .kind = .Eeprom },
|
.{ .str = "EEPROM_V", .kind = .Eeprom },
|
||||||
.{ .str = "SRAM_V", .kind = .Sram },
|
.{ .str = "SRAM_V", .kind = .Sram },
|
||||||
|
@ -18,7 +19,10 @@ pub const Backup = struct {
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
kind: BackupKind,
|
kind: BackupKind,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, kind: BackupKind) !Self {
|
title: [12]u8,
|
||||||
|
save_path: ?[]const u8,
|
||||||
|
|
||||||
|
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self {
|
||||||
const buf_len: usize = switch (kind) {
|
const buf_len: usize = switch (kind) {
|
||||||
.Sram => 0x8000, // 32K
|
.Sram => 0x8000, // 32K
|
||||||
.Flash => 0x10000, // 64K
|
.Flash => 0x10000, // 64K
|
||||||
|
@ -29,11 +33,16 @@ pub const Backup = struct {
|
||||||
const buf = try alloc.alloc(u8, buf_len);
|
const buf = try alloc.alloc(u8, buf_len);
|
||||||
std.mem.set(u8, buf, 0);
|
std.mem.set(u8, buf, 0);
|
||||||
|
|
||||||
return Self{
|
var backup = Self{
|
||||||
.buf = buf,
|
.buf = buf,
|
||||||
.alloc = alloc,
|
.alloc = alloc,
|
||||||
.kind = kind,
|
.kind = kind,
|
||||||
|
.title = title,
|
||||||
|
.save_path = path,
|
||||||
};
|
};
|
||||||
|
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 {
|
pub fn guessKind(rom: []const u8) ?BackupKind {
|
||||||
|
@ -52,9 +61,58 @@ pub const Backup = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: Self) void {
|
||||||
|
if (self.save_path) |path| self.writeSaveToDisk(path) catch |e| log.err("Failed to save {}", .{e});
|
||||||
|
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn loadSaveFromDisk(self: *Self, path: []const u8) !void {
|
||||||
|
const file_path = try self.getSaveFilePath(path);
|
||||||
|
defer self.alloc.free(file_path);
|
||||||
|
|
||||||
|
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
|
||||||
|
|
||||||
|
const len = try file.getEndPos();
|
||||||
|
const file_buf = try file.readToEndAlloc(self.alloc, len);
|
||||||
|
defer self.alloc.free(file_buf);
|
||||||
|
|
||||||
|
switch (self.kind) {
|
||||||
|
.Sram => {
|
||||||
|
std.mem.copy(u8, self.buf, file_buf);
|
||||||
|
log.info("Loaded Save from {s}", .{file_path});
|
||||||
|
},
|
||||||
|
else => return SaveError.UnsupportedBackupKind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getSaveFilePath(self: *const Self, path: []const u8) ![]const u8 {
|
||||||
|
const filename = try self.getSaveFilename();
|
||||||
|
defer self.alloc.free(filename);
|
||||||
|
|
||||||
|
return try std.fs.path.join(self.alloc, &[_][]const u8{ path, filename });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getSaveFilename(self: *const Self) ![]const u8 {
|
||||||
|
const title = correctTitle(self.title);
|
||||||
|
return try std.mem.concat(self.alloc, u8, &[_][]const u8{ title, ".sav" });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writeSaveToDisk(self: Self, path: []const u8) !void {
|
||||||
|
const file_path = try self.getSaveFilePath(path);
|
||||||
|
defer self.alloc.free(file_path);
|
||||||
|
|
||||||
|
const file = try std.fs.createFileAbsolute(file_path, .{});
|
||||||
|
defer file.close();
|
||||||
|
|
||||||
|
switch (self.kind) {
|
||||||
|
.Sram => {
|
||||||
|
try file.writeAll(self.buf);
|
||||||
|
log.info("Dumped SRAM to {s}", .{file_path});
|
||||||
|
},
|
||||||
|
else => return SaveError.UnsupportedBackupKind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get8(self: *const Self, idx: usize) u8 {
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
||||||
// TODO: Implement Flash and EEPROM
|
// TODO: Implement Flash and EEPROM
|
||||||
switch (self.kind) {
|
switch (self.kind) {
|
||||||
|
@ -96,3 +154,7 @@ const Needle = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const SaveError = error{
|
||||||
|
UnsupportedBackupKind,
|
||||||
|
};
|
||||||
|
|
|
@ -11,7 +11,7 @@ const Atomic = std.atomic.Atomic;
|
||||||
|
|
||||||
// 228 Lines which consist of 308 dots (which are 4 cycles long)
|
// 228 Lines which consist of 308 dots (which are 4 cycles long)
|
||||||
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
|
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
|
||||||
const clock_rate: u64 = 1 << 24; // 16.78MHz
|
const clock_rate: u64 = 1 << 24; // 16.78MHz
|
||||||
|
|
||||||
// TODO: Don't truncate this, be more accurate w/ timing
|
// TODO: Don't truncate this, be more accurate w/ timing
|
||||||
// 59.6046447754ns (truncated to just 59ns)
|
// 59.6046447754ns (truncated to just 59ns)
|
||||||
|
@ -19,7 +19,7 @@ const clock_period: u64 = std.time.ns_per_s / clock_rate;
|
||||||
const frame_period = (clock_period * cycles_per_frame);
|
const frame_period = (clock_period * cycles_per_frame);
|
||||||
|
|
||||||
// 59.7275005696Hz
|
// 59.7275005696Hz
|
||||||
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
|
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
|
||||||
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
|
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
|
||||||
|
|
||||||
const log = std.log.scoped(.Emulation);
|
const log = std.log.scoped(.Emulation);
|
||||||
|
|
48
src/main.zig
48
src/main.zig
|
@ -1,6 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
const clap = @import("clap");
|
const clap = @import("clap");
|
||||||
|
const known_folders = @import("known_folders");
|
||||||
|
|
||||||
const emu = @import("emu.zig");
|
const emu = @import("emu.zig");
|
||||||
const Bus = @import("Bus.zig");
|
const Bus = @import("Bus.zig");
|
||||||
|
@ -21,6 +22,9 @@ const expected_rate = @import("emu.zig").frame_rate;
|
||||||
|
|
||||||
pub const enable_logging: bool = false;
|
pub const enable_logging: bool = false;
|
||||||
const is_binary: bool = false;
|
const is_binary: bool = false;
|
||||||
|
const log = std.log.scoped(.GUI);
|
||||||
|
|
||||||
|
const correctTitle = @import("util.zig").correctTitle;
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
// Allocator for Emulator + CLI
|
// Allocator for Emulator + CLI
|
||||||
|
@ -40,7 +44,7 @@ pub fn main() anyerror!void {
|
||||||
|
|
||||||
if (args.flag("--help")) return clap.help(std.io.getStdErr().writer(), ¶ms);
|
if (args.flag("--help")) return clap.help(std.io.getStdErr().writer(), ¶ms);
|
||||||
|
|
||||||
const maybe_bios: ?[]const u8 = if (args.option("--bios")) |p| p else null;
|
const bios_path: ?[]const u8 = if (args.option("--bios")) |p| p else null;
|
||||||
|
|
||||||
const positionals = args.positionals();
|
const positionals = args.positionals();
|
||||||
const stderr = std.io.getStdErr();
|
const stderr = std.io.getStdErr();
|
||||||
|
@ -58,11 +62,16 @@ pub fn main() anyerror!void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Determine Save Directory
|
||||||
|
const save_path = try setupSavePath(alloc);
|
||||||
|
defer if (save_path) |path| alloc.free(path);
|
||||||
|
log.info("Save Path: {s}", .{save_path});
|
||||||
|
|
||||||
// Initialize Emulator
|
// Initialize Emulator
|
||||||
var scheduler = Scheduler.init(alloc);
|
var scheduler = Scheduler.init(alloc);
|
||||||
defer scheduler.deinit();
|
defer scheduler.deinit();
|
||||||
|
|
||||||
var bus = try Bus.init(alloc, &scheduler, rom_path, maybe_bios);
|
var bus = try Bus.init(alloc, &scheduler, rom_path, bios_path, save_path);
|
||||||
defer bus.deinit();
|
defer bus.deinit();
|
||||||
|
|
||||||
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
||||||
|
@ -88,12 +97,13 @@ pub fn main() anyerror!void {
|
||||||
if (status < 0) sdlPanic();
|
if (status < 0) sdlPanic();
|
||||||
defer SDL.SDL_Quit();
|
defer SDL.SDL_Quit();
|
||||||
|
|
||||||
|
const title = correctTitle(bus.pak.title);
|
||||||
|
|
||||||
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
|
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
|
||||||
var title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{bus.pak.title});
|
const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{title});
|
||||||
correctTitleSlice(&title);
|
|
||||||
|
|
||||||
var window = SDL.SDL_CreateWindow(
|
var window = SDL.SDL_CreateWindow(
|
||||||
title.ptr,
|
window_title.ptr,
|
||||||
SDL.SDL_WINDOWPOS_CENTERED,
|
SDL.SDL_WINDOWPOS_CENTERED,
|
||||||
SDL.SDL_WINDOWPOS_CENTERED,
|
SDL.SDL_WINDOWPOS_CENTERED,
|
||||||
gba_width * window_scale,
|
gba_width * window_scale,
|
||||||
|
@ -163,7 +173,7 @@ pub fn main() anyerror!void {
|
||||||
SDL.SDL_RenderPresent(renderer);
|
SDL.SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
const actual = emu_rate.calc();
|
const actual = emu_rate.calc();
|
||||||
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3.2}fps, {d:0>3.2}%] ", .{ title, actual, actual * 100 / expected_rate }) catch unreachable;
|
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3.2}fps, {d:0>3.2}%] ", .{ window_title, actual, actual * 100 / expected_rate }) catch unreachable;
|
||||||
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
|
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,14 +190,22 @@ const CliError = error{
|
||||||
UnneededOptions,
|
UnneededOptions,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The slice considers some null values to be a part of the string
|
// FIXME: Superfluous allocations?
|
||||||
/// so change the length of the slice so that isn't the case
|
fn setupSavePath(alloc: std.mem.Allocator) !?[]const u8 {
|
||||||
// FIXME: This is awful and bad
|
const save_subpath = try std.fs.path.join(alloc, &[_][]const u8{ "zba", "save" });
|
||||||
fn correctTitleSlice(title: *[]u8) void {
|
defer alloc.free(save_subpath);
|
||||||
for (title.*) |char, i| {
|
|
||||||
if (char == 0) {
|
const maybe_data_path = try known_folders.getPath(alloc, .data);
|
||||||
title.len = i;
|
defer if (maybe_data_path) |path| alloc.free(path);
|
||||||
break;
|
|
||||||
}
|
const save_path = if (maybe_data_path) |base| try std.fs.path.join(alloc, &[_][]const u8{ base, save_subpath }) else null;
|
||||||
|
|
||||||
|
if (save_path) |_| {
|
||||||
|
// If we've determined what our save path should be, ensure the prereq directories
|
||||||
|
// are present so that we can successfully write to the path when necessary
|
||||||
|
const maybe_data_dir = try known_folders.open(alloc, .data, .{});
|
||||||
|
if (maybe_data_dir) |data_dir| try data_dir.makePath(save_subpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return save_path;
|
||||||
}
|
}
|
||||||
|
|
47
src/util.zig
47
src/util.zig
|
@ -53,3 +53,50 @@ pub fn intToBytes(comptime T: type, value: anytype) [@sizeOf(T)]u8 {
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Title from the GBA Cartridge may be null padded to a maximum
|
||||||
|
/// length of 12 bytes.
|
||||||
|
///
|
||||||
|
/// This function returns a slice of everything just before the first
|
||||||
|
/// `\0`
|
||||||
|
pub fn correctTitle(title: [12]u8) []const u8 {
|
||||||
|
var len = title.len;
|
||||||
|
for (title) |char, i| {
|
||||||
|
if (char == 0) {
|
||||||
|
len = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return title[0..len];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copies a Title and returns either an identical or similar
|
||||||
|
/// array consisting of ASCII that won't make any file system angry
|
||||||
|
///
|
||||||
|
/// Currently Unused but I assume there's some ROM out there that will require this
|
||||||
|
pub fn safeTitle(title: [12]u8) [12]u8 {
|
||||||
|
var result: [12]u8 = title;
|
||||||
|
|
||||||
|
for (result) |*char| {
|
||||||
|
if (char.* == '-') char.* = '_';
|
||||||
|
if (char.* == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fixTitle(alloc: std.mem.Allocator, title: [12]u8) ![]u8 {
|
||||||
|
var len: usize = 12;
|
||||||
|
for (title) |char, i| {
|
||||||
|
if (char == 0) {
|
||||||
|
len = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const buf = try alloc.alloc(u8, len);
|
||||||
|
std.mem.copy(u8, buf, title[0..len]);
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue