Compare commits
No commits in common. "aa52bb5917cf88264e4153f91fa706a471236923" and "e5b74417403ab93e5a0b3d91f28235a30bca52ab" have entirely different histories.
aa52bb5917
...
e5b7441740
|
@ -13,12 +13,12 @@ alloc: Allocator,
|
||||||
addr_latch: u32,
|
addr_latch: u32,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
|
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
|
||||||
const buf: ?[]u8 = if (maybe_path) |path| blk: {
|
var buf: ?[]u8 = null;
|
||||||
|
if (maybe_path) |path| {
|
||||||
const file = try std.fs.cwd().openFile(path, .{});
|
const file = try std.fs.cwd().openFile(path, .{});
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
|
||||||
break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
|
}
|
||||||
} else null;
|
|
||||||
|
|
||||||
return Self{
|
return Self{
|
||||||
.buf = buf,
|
.buf = buf,
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub const Backup = struct {
|
||||||
|
|
||||||
buf: []u8,
|
buf: []u8,
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
kind: Kind,
|
kind: BackupKind,
|
||||||
|
|
||||||
title: [12]u8,
|
title: [12]u8,
|
||||||
save_path: ?[]const u8,
|
save_path: ?[]const u8,
|
||||||
|
@ -26,15 +26,7 @@ pub const Backup = struct {
|
||||||
flash: Flash,
|
flash: Flash,
|
||||||
eeprom: Eeprom,
|
eeprom: Eeprom,
|
||||||
|
|
||||||
const Kind = enum {
|
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self {
|
||||||
Eeprom,
|
|
||||||
Sram,
|
|
||||||
Flash,
|
|
||||||
Flash1M,
|
|
||||||
None,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, kind: Kind, title: [12]u8, path: ?[]const u8) !Self {
|
|
||||||
log.info("Kind: {}", .{kind});
|
log.info("Kind: {}", .{kind});
|
||||||
|
|
||||||
const buf_size: usize = switch (kind) {
|
const buf_size: usize = switch (kind) {
|
||||||
|
@ -44,24 +36,24 @@ pub const Backup = struct {
|
||||||
.None, .Eeprom => 0, // EEPROM is handled upon first Read Request to it
|
.None, .Eeprom => 0, // EEPROM is handled upon first Read Request to it
|
||||||
};
|
};
|
||||||
|
|
||||||
const buf = try allocator.alloc(u8, buf_size);
|
const buf = try alloc.alloc(u8, buf_size);
|
||||||
std.mem.set(u8, buf, 0xFF);
|
std.mem.set(u8, buf, 0xFF);
|
||||||
|
|
||||||
var backup = Self{
|
var backup = Self{
|
||||||
.buf = buf,
|
.buf = buf,
|
||||||
.alloc = allocator,
|
.alloc = alloc,
|
||||||
.kind = kind,
|
.kind = kind,
|
||||||
.title = title,
|
.title = title,
|
||||||
.save_path = path,
|
.save_path = path,
|
||||||
.flash = Flash.init(),
|
.flash = Flash.init(),
|
||||||
.eeprom = Eeprom.init(allocator),
|
.eeprom = Eeprom.init(alloc),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (backup.save_path) |p| backup.loadSaveFromDisk(allocator, p) catch |e| log.err("Failed to load save: {}", .{e});
|
if (backup.save_path) |p| backup.loadSaveFromDisk(p) catch |e| log.err("Failed to load save: {}", .{e});
|
||||||
return backup;
|
return backup;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn guessKind(rom: []const u8) ?Kind {
|
pub fn guessKind(rom: []const u8) ?BackupKind {
|
||||||
for (backup_kinds) |needle| {
|
for (backup_kinds) |needle| {
|
||||||
const needle_len = needle.str.len;
|
const needle_len = needle.str.len;
|
||||||
|
|
||||||
|
@ -75,13 +67,13 @@ pub const Backup = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: Self) void {
|
||||||
if (self.save_path) |path| self.writeSaveToDisk(self.alloc, path) catch |e| log.err("Failed to write save: {}", .{e});
|
if (self.save_path) |path| self.writeSaveToDisk(path) catch |e| log.err("Failed to write save: {}", .{e});
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {
|
fn loadSaveFromDisk(self: *Self, path: []const u8) !void {
|
||||||
const file_path = try self.getSaveFilePath(allocator, path);
|
const file_path = try self.getSaveFilePath(self.alloc, path);
|
||||||
defer allocator.free(file_path);
|
defer self.alloc.free(file_path);
|
||||||
|
|
||||||
// FIXME: Don't rely on this lol
|
// FIXME: Don't rely on this lol
|
||||||
if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) {
|
if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) {
|
||||||
|
@ -89,8 +81,8 @@ pub const Backup = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
|
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
|
||||||
const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos());
|
const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos());
|
||||||
defer allocator.free(file_buf);
|
defer self.alloc.free(file_buf);
|
||||||
|
|
||||||
switch (self.kind) {
|
switch (self.kind) {
|
||||||
.Sram, .Flash, .Flash1M => {
|
.Sram, .Flash, .Flash1M => {
|
||||||
|
@ -105,7 +97,7 @@ pub const Backup = struct {
|
||||||
if (file_buf.len == 0x200 or file_buf.len == 0x2000) {
|
if (file_buf.len == 0x200 or file_buf.len == 0x2000) {
|
||||||
self.eeprom.kind = if (file_buf.len == 0x200) .Small else .Large;
|
self.eeprom.kind = if (file_buf.len == 0x200) .Small else .Large;
|
||||||
|
|
||||||
self.buf = try allocator.alloc(u8, file_buf.len);
|
self.buf = try self.alloc.alloc(u8, file_buf.len);
|
||||||
std.mem.copy(u8, self.buf, file_buf);
|
std.mem.copy(u8, self.buf, file_buf);
|
||||||
return log.info("Loaded Save from {s}", .{file_path});
|
return log.info("Loaded Save from {s}", .{file_path});
|
||||||
}
|
}
|
||||||
|
@ -133,9 +125,9 @@ pub const Backup = struct {
|
||||||
return try std.mem.concat(allocator, u8, &[_][]const u8{ name, ".sav" });
|
return try std.mem.concat(allocator, u8, &[_][]const u8{ name, ".sav" });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeSaveToDisk(self: Self, allocator: Allocator, path: []const u8) !void {
|
fn writeSaveToDisk(self: Self, path: []const u8) !void {
|
||||||
const file_path = try self.getSaveFilePath(allocator, path);
|
const file_path = try self.getSaveFilePath(self.alloc, path);
|
||||||
defer allocator.free(file_path);
|
defer self.alloc.free(file_path);
|
||||||
|
|
||||||
switch (self.kind) {
|
switch (self.kind) {
|
||||||
.Sram, .Flash, .Flash1M, .Eeprom => {
|
.Sram, .Flash, .Flash1M, .Eeprom => {
|
||||||
|
@ -209,13 +201,21 @@ pub const Backup = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const BackupKind = enum {
|
||||||
|
Eeprom,
|
||||||
|
Sram,
|
||||||
|
Flash,
|
||||||
|
Flash1M,
|
||||||
|
None,
|
||||||
|
};
|
||||||
|
|
||||||
const Needle = struct {
|
const Needle = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
str: []const u8,
|
str: []const u8,
|
||||||
kind: Backup.Kind,
|
kind: BackupKind,
|
||||||
|
|
||||||
fn init(str: []const u8, kind: Backup.Kind) Self {
|
fn init(str: []const u8, kind: BackupKind) Self {
|
||||||
return .{
|
return .{
|
||||||
.str = str,
|
.str = str,
|
||||||
.kind = kind,
|
.kind = kind,
|
||||||
|
@ -230,7 +230,7 @@ const SaveError = error{
|
||||||
const Flash = struct {
|
const Flash = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
state: State,
|
state: FlashState,
|
||||||
|
|
||||||
id_mode: bool,
|
id_mode: bool,
|
||||||
set_bank: bool,
|
set_bank: bool,
|
||||||
|
@ -239,12 +239,6 @@ const Flash = struct {
|
||||||
|
|
||||||
bank: u1,
|
bank: u1,
|
||||||
|
|
||||||
const State = enum {
|
|
||||||
Ready,
|
|
||||||
Set,
|
|
||||||
Command,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn init() Self {
|
fn init() Self {
|
||||||
return .{
|
return .{
|
||||||
.state = .Ready,
|
.state = .Ready,
|
||||||
|
@ -299,6 +293,12 @@ const Flash = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const FlashState = enum {
|
||||||
|
Ready,
|
||||||
|
Set,
|
||||||
|
Command,
|
||||||
|
};
|
||||||
|
|
||||||
const Eeprom = struct {
|
const Eeprom = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue