chore: remove magic numbers

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-04-13 21:39:35 -03:00
parent ffbb31c767
commit dfe94fb931
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| {
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{

View File

@ -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{

View File

@ -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{

View File

@ -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,
}

View File

@ -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{

View File

@ -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,