From dfe94fb9316be87ed57ab2a2c5702b8d0152e2e9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 13 Apr 2022 21:39:35 -0300 Subject: [PATCH] chore: remove magic numbers --- src/bus/Bios.zig | 4 +--- src/bus/Ewram.zig | 3 ++- src/bus/Iwram.zig | 3 ++- src/bus/backup.zig | 12 ++++++------ src/ppu.zig | 9 ++++++--- src/util.zig | 15 --------------- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/bus/Bios.zig b/src/bus/Bios.zig index 489ee2b..c14c23b 100644 --- a/src/bus/Bios.zig +++ b/src/bus/Bios.zig @@ -12,9 +12,7 @@ pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self { if (maybe_path) |path| { const file = try std.fs.cwd().openFile(path, .{}); defer file.close(); - - const len = try file.getEndPos(); - buf = try file.readToEndAlloc(alloc, len); + buf = try file.readToEndAlloc(alloc, try file.getEndPos()); } return Self{ diff --git a/src/bus/Ewram.zig b/src/bus/Ewram.zig index 07a7a41..757a16d 100644 --- a/src/bus/Ewram.zig +++ b/src/bus/Ewram.zig @@ -1,13 +1,14 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const ewram_size = 0x40000; const Self = @This(); buf: []u8, alloc: Allocator, pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x40000); + const buf = try alloc.alloc(u8, ewram_size); std.mem.set(u8, buf, 0); return Self{ diff --git a/src/bus/Iwram.zig b/src/bus/Iwram.zig index 76c15b5..9b5d699 100644 --- a/src/bus/Iwram.zig +++ b/src/bus/Iwram.zig @@ -1,13 +1,14 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const iwram_size = 0x8000; const Self = @This(); buf: []u8, alloc: Allocator, pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x8000); + const buf = try alloc.alloc(u8, iwram_size); std.mem.set(u8, buf, 0); return Self{ diff --git a/src/bus/backup.zig b/src/bus/backup.zig index c9e988d..60e9eb5 100644 --- a/src/bus/backup.zig +++ b/src/bus/backup.zig @@ -27,7 +27,7 @@ pub const Backup = struct { flash: Flash, pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self { - const buf_len: usize = switch (kind) { + const buf_size: usize = switch (kind) { .Sram => 0x8000, // 32K .Flash => 0x10000, // 64K .Flash1M => 0x20000, // 128K @@ -35,7 +35,7 @@ pub const Backup = struct { .None => 0, }; - const buf = try alloc.alloc(u8, buf_len); + const buf = try alloc.alloc(u8, buf_size); std.mem.set(u8, buf, 0xFF); var backup = Self{ @@ -74,17 +74,17 @@ pub const Backup = struct { 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); + const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos()); defer self.alloc.free(file_buf); switch (self.kind) { .Sram, .Flash, .Flash1M => { if (self.buf.len == file_buf.len) { std.mem.copy(u8, self.buf, file_buf); - log.info("Loaded Save from {s}", .{file_path}); + return log.info("Loaded Save from {s}", .{file_path}); } + + log.err("{s} is {} bytes, but we expected {} bytes", .{ file_path, file_buf.len, self.buf.len }); }, else => return SaveError.UnsupportedBackupKind, } diff --git a/src/ppu.zig b/src/ppu.zig index ec9a13a..94dabc5 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -413,13 +413,14 @@ pub const Ppu = struct { }; const Palette = struct { + const palram_size = 0x400; const Self = @This(); buf: []u8, alloc: Allocator, fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x400); + const buf = try alloc.alloc(u8, palram_size); std.mem.set(u8, buf, 0); return Self{ @@ -474,13 +475,14 @@ const Palette = struct { }; const Vram = struct { + const vram_size = 0x18000; const Self = @This(); buf: []u8, alloc: Allocator, fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x18000); + const buf = try alloc.alloc(u8, vram_size); std.mem.set(u8, buf, 0); return Self{ @@ -543,13 +545,14 @@ const Vram = struct { }; const Oam = struct { + const oam_size = 0x400; const Self = @This(); buf: []u8, alloc: Allocator, fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x400); + const buf = try alloc.alloc(u8, oam_size); std.mem.set(u8, buf, 0); return Self{ diff --git a/src/util.zig b/src/util.zig index f70df7c..6a7352e 100644 --- a/src/util.zig +++ b/src/util.zig @@ -87,21 +87,6 @@ pub fn safeTitle(title: [12]u8) [12]u8 { 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; -} - pub const FilePaths = struct { rom: []const u8, bios: ?[]const u8,