chore: remove magic numbers

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:35 -03:00
parent 6c61eb3537
commit a92989ed24
6 changed files with 17 additions and 29 deletions

View File

@ -12,9 +12,7 @@ pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
if (maybe_path) |path| { 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());
const len = try file.getEndPos();
buf = try file.readToEndAlloc(alloc, len);
} }
return Self{ return Self{

View File

@ -1,13 +1,14 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ewram_size = 0x40000;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
pub fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{

View File

@ -1,13 +1,14 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const iwram_size = 0x8000;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
pub fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{

View File

@ -27,7 +27,7 @@ pub const Backup = struct {
flash: Flash, flash: Flash,
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self { 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 .Sram => 0x8000, // 32K
.Flash => 0x10000, // 64K .Flash => 0x10000, // 64K
.Flash1M => 0x20000, // 128K .Flash1M => 0x20000, // 128K
@ -35,7 +35,7 @@ pub const Backup = struct {
.None => 0, .None => 0,
}; };
const buf = try alloc.alloc(u8, buf_len); 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{
@ -74,17 +74,17 @@ pub const Backup = struct {
defer self.alloc.free(file_path); defer self.alloc.free(file_path);
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(self.alloc, try file.getEndPos());
const len = try file.getEndPos();
const file_buf = try file.readToEndAlloc(self.alloc, len);
defer self.alloc.free(file_buf); defer self.alloc.free(file_buf);
switch (self.kind) { switch (self.kind) {
.Sram, .Flash, .Flash1M => { .Sram, .Flash, .Flash1M => {
if (self.buf.len == file_buf.len) { if (self.buf.len == file_buf.len) {
std.mem.copy(u8, self.buf, file_buf); 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, else => return SaveError.UnsupportedBackupKind,
} }

View File

@ -413,13 +413,14 @@ pub const Ppu = struct {
}; };
const Palette = struct { const Palette = struct {
const palram_size = 0x400;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{
@ -474,13 +475,14 @@ const Palette = struct {
}; };
const Vram = struct { const Vram = struct {
const vram_size = 0x18000;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{
@ -543,13 +545,14 @@ const Vram = struct {
}; };
const Oam = struct { const Oam = struct {
const oam_size = 0x400;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{

View File

@ -87,21 +87,6 @@ pub fn safeTitle(title: [12]u8) [12]u8 {
return result; 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 { pub const FilePaths = struct {
rom: []const u8, rom: []const u8,
bios: ?[]const u8, bios: ?[]const u8,