From aa52bb5917cf88264e4153f91fa706a471236923 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 26 Aug 2022 14:05:04 -0500 Subject: [PATCH] chore: reorganize some code --- src/core/bus/Bios.zig | 8 ++++---- src/core/bus/backup.zig | 40 ++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/core/bus/Bios.zig b/src/core/bus/Bios.zig index 9c1cc7b..d17a1f2 100644 --- a/src/core/bus/Bios.zig +++ b/src/core/bus/Bios.zig @@ -13,12 +13,12 @@ alloc: Allocator, addr_latch: u32, pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self { - var buf: ?[]u8 = null; - if (maybe_path) |path| { + const buf: ?[]u8 = if (maybe_path) |path| blk: { const file = try std.fs.cwd().openFile(path, .{}); defer file.close(); - buf = try file.readToEndAlloc(alloc, try file.getEndPos()); - } + + break :blk try file.readToEndAlloc(alloc, try file.getEndPos()); + } else null; return Self{ .buf = buf, diff --git a/src/core/bus/backup.zig b/src/core/bus/backup.zig index 6d16bf3..3ffda65 100644 --- a/src/core/bus/backup.zig +++ b/src/core/bus/backup.zig @@ -18,7 +18,7 @@ pub const Backup = struct { buf: []u8, alloc: Allocator, - kind: BackupKind, + kind: Kind, title: [12]u8, save_path: ?[]const u8, @@ -26,7 +26,15 @@ pub const Backup = struct { flash: Flash, eeprom: Eeprom, - pub fn init(allocator: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self { + const Kind = enum { + Eeprom, + Sram, + Flash, + Flash1M, + None, + }; + + pub fn init(allocator: Allocator, kind: Kind, title: [12]u8, path: ?[]const u8) !Self { log.info("Kind: {}", .{kind}); const buf_size: usize = switch (kind) { @@ -53,7 +61,7 @@ pub const Backup = struct { return backup; } - pub fn guessKind(rom: []const u8) ?BackupKind { + pub fn guessKind(rom: []const u8) ?Kind { for (backup_kinds) |needle| { const needle_len = needle.str.len; @@ -201,21 +209,13 @@ pub const Backup = struct { } }; -const BackupKind = enum { - Eeprom, - Sram, - Flash, - Flash1M, - None, -}; - const Needle = struct { const Self = @This(); str: []const u8, - kind: BackupKind, + kind: Backup.Kind, - fn init(str: []const u8, kind: BackupKind) Self { + fn init(str: []const u8, kind: Backup.Kind) Self { return .{ .str = str, .kind = kind, @@ -230,7 +230,7 @@ const SaveError = error{ const Flash = struct { const Self = @This(); - state: FlashState, + state: State, id_mode: bool, set_bank: bool, @@ -239,6 +239,12 @@ const Flash = struct { bank: u1, + const State = enum { + Ready, + Set, + Command, + }; + fn init() Self { return .{ .state = .Ready, @@ -293,12 +299,6 @@ const Flash = struct { } }; -const FlashState = enum { - Ready, - Set, - Command, -}; - const Eeprom = struct { const Self = @This();