From 3fb7f2f81495bbb2476ef634e4fcfe23039a0046 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sat, 3 Sep 2022 18:30:48 -0300 Subject: [PATCH] chore: better conform to zig idioms --- src/core/Bus.zig | 28 ++++++---------------------- src/core/bus/Bios.zig | 10 +++++----- src/core/bus/Ewram.zig | 10 +++++----- src/core/bus/GamePak.zig | 12 ++++++------ src/core/bus/Iwram.zig | 10 +++++----- src/core/bus/backup.zig | 16 ++++++++-------- src/core/ppu.zig | 36 ++++++++++++++++++------------------ src/core/scheduler.zig | 4 ++-- 8 files changed, 55 insertions(+), 71 deletions(-) diff --git a/src/core/Bus.zig b/src/core/Bus.zig index 4ea4343..fee57bc 100644 --- a/src/core/Bus.zig +++ b/src/core/Bus.zig @@ -49,14 +49,14 @@ io: Io, cpu: ?*Arm7tdmi, sched: *Scheduler, -pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void { +pub fn init(self: *Self, allocator: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void { self.* = .{ - .pak = try GamePak.init(alloc, paths.rom, paths.save), - .bios = try Bios.init(alloc, paths.bios), - .ppu = try Ppu.init(alloc, sched), + .pak = try GamePak.init(allocator, paths.rom, paths.save), + .bios = try Bios.init(allocator, paths.bios), + .ppu = try Ppu.init(allocator, sched), .apu = Apu.init(sched), - .iwram = try Iwram.init(alloc), - .ewram = try Ewram.init(alloc), + .iwram = try Iwram.init(allocator), + .ewram = try Ewram.init(allocator), .dma = createDmaTuple(), .tim = createTimerTuple(sched), .io = Io.init(), @@ -65,22 +65,6 @@ pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, pa }; } -// pub fn init(alloc: Allocator, sched: *Scheduler, paths: FilePaths) !Self { -// return Self{ -// .pak = try GamePak.init(alloc, paths.rom, paths.save), -// .bios = try Bios.init(alloc, paths.bios), -// .ppu = try Ppu.init(alloc, sched), -// .apu = Apu.init(sched), -// .iwram = try Iwram.init(alloc), -// .ewram = try Ewram.init(alloc), -// .dma = createDmaTuple(), -// .tim = createTimerTuple(sched), -// .io = Io.init(), -// .cpu = null, -// .sched = sched, -// }; -// } - pub fn deinit(self: *Self) void { self.iwram.deinit(); self.ewram.deinit(); diff --git a/src/core/bus/Bios.zig b/src/core/bus/Bios.zig index 09dfc6b..8515cdb 100644 --- a/src/core/bus/Bios.zig +++ b/src/core/bus/Bios.zig @@ -8,27 +8,27 @@ pub const size = 0x4000; const Self = @This(); buf: ?[]u8, -alloc: Allocator, +allocator: Allocator, addr_latch: u32, -pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self { +pub fn init(allocator: Allocator, maybe_path: ?[]const u8) !Self { const buf: ?[]u8 = if (maybe_path) |path| blk: { const file = try std.fs.cwd().openFile(path, .{}); defer file.close(); - break :blk try file.readToEndAlloc(alloc, try file.getEndPos()); + break :blk try file.readToEndAlloc(allocator, try file.getEndPos()); } else null; return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, .addr_latch = 0, }; } pub fn deinit(self: *Self) void { - if (self.buf) |buf| self.alloc.free(buf); + if (self.buf) |buf| self.allocator.free(buf); self.* = undefined; } diff --git a/src/core/bus/Ewram.zig b/src/core/bus/Ewram.zig index ce764a7..f3d952c 100644 --- a/src/core/bus/Ewram.zig +++ b/src/core/bus/Ewram.zig @@ -5,20 +5,20 @@ const ewram_size = 0x40000; const Self = @This(); buf: []u8, -alloc: Allocator, +allocator: Allocator, -pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, ewram_size); +pub fn init(allocator: Allocator) !Self { + const buf = try allocator.alloc(u8, ewram_size); std.mem.set(u8, buf, 0); return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, }; } pub fn deinit(self: *Self) void { - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } diff --git a/src/core/bus/GamePak.zig b/src/core/bus/GamePak.zig index 995298a..b208f75 100644 --- a/src/core/bus/GamePak.zig +++ b/src/core/bus/GamePak.zig @@ -8,22 +8,22 @@ const Self = @This(); title: [12]u8, buf: []u8, -alloc: Allocator, +allocator: Allocator, backup: Backup, -pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self { +pub fn init(allocator: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self { const file = try std.fs.cwd().openFile(rom_path, .{}); defer file.close(); - const file_buf = try file.readToEndAlloc(alloc, try file.getEndPos()); + const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos()); const title = parseTitle(file_buf); const kind = Backup.guessKind(file_buf) orelse .None; const pak = Self{ .buf = file_buf, - .alloc = alloc, + .allocator = allocator, .title = title, - .backup = try Backup.init(alloc, kind, title, save_path), + .backup = try Backup.init(allocator, kind, title, save_path), }; pak.parseHeader(); @@ -60,7 +60,7 @@ inline fn isLarge(self: *const Self) bool { pub fn deinit(self: *Self) void { self.backup.deinit(); - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } diff --git a/src/core/bus/Iwram.zig b/src/core/bus/Iwram.zig index ef247b3..d58ccb7 100644 --- a/src/core/bus/Iwram.zig +++ b/src/core/bus/Iwram.zig @@ -5,20 +5,20 @@ const iwram_size = 0x8000; const Self = @This(); buf: []u8, -alloc: Allocator, +allocator: Allocator, -pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, iwram_size); +pub fn init(allocator: Allocator) !Self { + const buf = try allocator.alloc(u8, iwram_size); std.mem.set(u8, buf, 0); return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, }; } pub fn deinit(self: *Self) void { - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } diff --git a/src/core/bus/backup.zig b/src/core/bus/backup.zig index 327a78f..09d6b91 100644 --- a/src/core/bus/backup.zig +++ b/src/core/bus/backup.zig @@ -17,7 +17,7 @@ pub const Backup = struct { const Self = @This(); buf: []u8, - alloc: Allocator, + allocator: Allocator, kind: Kind, title: [12]u8, @@ -49,7 +49,7 @@ pub const Backup = struct { var backup = Self{ .buf = buf, - .alloc = allocator, + .allocator = allocator, .kind = kind, .title = title, .save_path = path, @@ -75,8 +75,8 @@ pub const Backup = struct { } 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}); - self.alloc.free(self.buf); + if (self.save_path) |path| self.writeSaveToDisk(self.allocator, path) catch |e| log.err("Failed to write save: {}", .{e}); + self.allocator.free(self.buf); self.* = undefined; } @@ -310,7 +310,7 @@ const Eeprom = struct { writer: Writer, reader: Reader, - alloc: Allocator, + allocator: Allocator, const Kind = enum { Unknown, @@ -326,14 +326,14 @@ const Eeprom = struct { RequestEnd, }; - fn init(alloc: Allocator) Self { + fn init(allocator: Allocator) Self { return .{ .kind = .Unknown, .state = .Ready, .writer = Writer.init(), .reader = Reader.init(), .addr = 0, - .alloc = alloc, + .allocator = allocator, }; } @@ -361,7 +361,7 @@ const Eeprom = struct { else => unreachable, }; - buf.* = self.alloc.alloc(u8, len) catch |e| { + buf.* = self.allocator.alloc(u8, len) catch |e| { log.err("Failed to resize EEPROM buf to {} bytes", .{len}); std.debug.panic("EEPROM entered irrecoverable state {}", .{e}); }; diff --git a/src/core/ppu.zig b/src/core/ppu.zig index 6211c43..c3aace5 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -41,7 +41,7 @@ pub const Ppu = struct { oam: Oam, sched: *Scheduler, framebuf: FrameBuffer, - alloc: Allocator, + allocator: Allocator, scanline_sprites: *[128]?Sprite, scanline: Scanline, @@ -59,7 +59,7 @@ pub const Ppu = struct { .oam = try Oam.init(allocator), .sched = sched, .framebuf = try FrameBuffer.init(allocator), - .alloc = allocator, + .allocator = allocator, // Registers .win = Window.init(), @@ -78,7 +78,7 @@ pub const Ppu = struct { } pub fn deinit(self: *Self) void { - self.alloc.destroy(self.scanline_sprites); + self.allocator.destroy(self.scanline_sprites); self.framebuf.deinit(); self.scanline.deinit(); self.vram.deinit(); @@ -628,20 +628,20 @@ const Palette = struct { const Self = @This(); buf: []u8, - alloc: Allocator, + allocator: Allocator, - fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, palram_size); + fn init(allocator: Allocator) !Self { + const buf = try allocator.alloc(u8, palram_size); std.mem.set(u8, buf, 0); return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, }; } fn deinit(self: *Self) void { - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } @@ -677,20 +677,20 @@ const Vram = struct { const Self = @This(); buf: []u8, - alloc: Allocator, + allocator: Allocator, - fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, vram_size); + fn init(allocator: Allocator) !Self { + const buf = try allocator.alloc(u8, vram_size); std.mem.set(u8, buf, 0); return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, }; } fn deinit(self: *Self) void { - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } @@ -738,20 +738,20 @@ const Oam = struct { const Self = @This(); buf: []u8, - alloc: Allocator, + allocator: Allocator, - fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, oam_size); + fn init(allocator: Allocator) !Self { + const buf = try allocator.alloc(u8, oam_size); std.mem.set(u8, buf, 0); return Self{ .buf = buf, - .alloc = alloc, + .allocator = allocator, }; } fn deinit(self: *Self) void { - self.alloc.free(self.buf); + self.allocator.free(self.buf); self.* = undefined; } diff --git a/src/core/scheduler.zig b/src/core/scheduler.zig index 8112262..8a4b517 100644 --- a/src/core/scheduler.zig +++ b/src/core/scheduler.zig @@ -14,8 +14,8 @@ pub const Scheduler = struct { tick: u64, queue: PriorityQueue(Event, void, lessThan), - pub fn init(alloc: Allocator) Self { - var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(alloc, {}) }; + pub fn init(allocator: Allocator) Self { + var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(allocator, {}) }; sched.queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable; return sched;