chore: better conform to zig idioms

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-09-03 18:30:48 -03:00
parent 59669ba3a5
commit 3fb7f2f814
8 changed files with 55 additions and 71 deletions

View File

@ -49,14 +49,14 @@ io: Io,
cpu: ?*Arm7tdmi, cpu: ?*Arm7tdmi,
sched: *Scheduler, 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.* = .{ self.* = .{
.pak = try GamePak.init(alloc, paths.rom, paths.save), .pak = try GamePak.init(allocator, paths.rom, paths.save),
.bios = try Bios.init(alloc, paths.bios), .bios = try Bios.init(allocator, paths.bios),
.ppu = try Ppu.init(alloc, sched), .ppu = try Ppu.init(allocator, sched),
.apu = Apu.init(sched), .apu = Apu.init(sched),
.iwram = try Iwram.init(alloc), .iwram = try Iwram.init(allocator),
.ewram = try Ewram.init(alloc), .ewram = try Ewram.init(allocator),
.dma = createDmaTuple(), .dma = createDmaTuple(),
.tim = createTimerTuple(sched), .tim = createTimerTuple(sched),
.io = Io.init(), .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 { pub fn deinit(self: *Self) void {
self.iwram.deinit(); self.iwram.deinit();
self.ewram.deinit(); self.ewram.deinit();

View File

@ -8,27 +8,27 @@ pub const size = 0x4000;
const Self = @This(); const Self = @This();
buf: ?[]u8, buf: ?[]u8,
alloc: Allocator, allocator: Allocator,
addr_latch: u32, 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 buf: ?[]u8 = if (maybe_path) |path| blk: {
const file = try std.fs.cwd().openFile(path, .{}); const file = try std.fs.cwd().openFile(path, .{});
defer file.close(); defer file.close();
break :blk try file.readToEndAlloc(alloc, try file.getEndPos()); break :blk try file.readToEndAlloc(allocator, try file.getEndPos());
} else null; } else null;
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
.addr_latch = 0, .addr_latch = 0,
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
if (self.buf) |buf| self.alloc.free(buf); if (self.buf) |buf| self.allocator.free(buf);
self.* = undefined; self.* = undefined;
} }

View File

@ -5,20 +5,20 @@ const ewram_size = 0x40000;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
pub fn init(alloc: Allocator) !Self { pub fn init(allocator: Allocator) !Self {
const buf = try alloc.alloc(u8, ewram_size); const buf = try allocator.alloc(u8, ewram_size);
std.mem.set(u8, buf, 0); std.mem.set(u8, buf, 0);
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }

View File

@ -8,22 +8,22 @@ const Self = @This();
title: [12]u8, title: [12]u8,
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
backup: Backup, 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, .{}); const file = try std.fs.cwd().openFile(rom_path, .{});
defer file.close(); 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 title = parseTitle(file_buf);
const kind = Backup.guessKind(file_buf) orelse .None; const kind = Backup.guessKind(file_buf) orelse .None;
const pak = Self{ const pak = Self{
.buf = file_buf, .buf = file_buf,
.alloc = alloc, .allocator = allocator,
.title = title, .title = title,
.backup = try Backup.init(alloc, kind, title, save_path), .backup = try Backup.init(allocator, kind, title, save_path),
}; };
pak.parseHeader(); pak.parseHeader();
@ -60,7 +60,7 @@ inline fn isLarge(self: *const Self) bool {
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.backup.deinit(); self.backup.deinit();
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }

View File

@ -5,20 +5,20 @@ const iwram_size = 0x8000;
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
pub fn init(alloc: Allocator) !Self { pub fn init(allocator: Allocator) !Self {
const buf = try alloc.alloc(u8, iwram_size); const buf = try allocator.alloc(u8, iwram_size);
std.mem.set(u8, buf, 0); std.mem.set(u8, buf, 0);
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }

View File

@ -17,7 +17,7 @@ pub const Backup = struct {
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
kind: Kind, kind: Kind,
title: [12]u8, title: [12]u8,
@ -49,7 +49,7 @@ pub const Backup = struct {
var backup = Self{ var backup = Self{
.buf = buf, .buf = buf,
.alloc = allocator, .allocator = allocator,
.kind = kind, .kind = kind,
.title = title, .title = title,
.save_path = path, .save_path = path,
@ -75,8 +75,8 @@ pub const Backup = struct {
} }
pub fn deinit(self: *Self) void { 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}); if (self.save_path) |path| self.writeSaveToDisk(self.allocator, path) catch |e| log.err("Failed to write save: {}", .{e});
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }
@ -310,7 +310,7 @@ const Eeprom = struct {
writer: Writer, writer: Writer,
reader: Reader, reader: Reader,
alloc: Allocator, allocator: Allocator,
const Kind = enum { const Kind = enum {
Unknown, Unknown,
@ -326,14 +326,14 @@ const Eeprom = struct {
RequestEnd, RequestEnd,
}; };
fn init(alloc: Allocator) Self { fn init(allocator: Allocator) Self {
return .{ return .{
.kind = .Unknown, .kind = .Unknown,
.state = .Ready, .state = .Ready,
.writer = Writer.init(), .writer = Writer.init(),
.reader = Reader.init(), .reader = Reader.init(),
.addr = 0, .addr = 0,
.alloc = alloc, .allocator = allocator,
}; };
} }
@ -361,7 +361,7 @@ const Eeprom = struct {
else => unreachable, 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}); log.err("Failed to resize EEPROM buf to {} bytes", .{len});
std.debug.panic("EEPROM entered irrecoverable state {}", .{e}); std.debug.panic("EEPROM entered irrecoverable state {}", .{e});
}; };

View File

@ -41,7 +41,7 @@ pub const Ppu = struct {
oam: Oam, oam: Oam,
sched: *Scheduler, sched: *Scheduler,
framebuf: FrameBuffer, framebuf: FrameBuffer,
alloc: Allocator, allocator: Allocator,
scanline_sprites: *[128]?Sprite, scanline_sprites: *[128]?Sprite,
scanline: Scanline, scanline: Scanline,
@ -59,7 +59,7 @@ pub const Ppu = struct {
.oam = try Oam.init(allocator), .oam = try Oam.init(allocator),
.sched = sched, .sched = sched,
.framebuf = try FrameBuffer.init(allocator), .framebuf = try FrameBuffer.init(allocator),
.alloc = allocator, .allocator = allocator,
// Registers // Registers
.win = Window.init(), .win = Window.init(),
@ -78,7 +78,7 @@ pub const Ppu = struct {
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.alloc.destroy(self.scanline_sprites); self.allocator.destroy(self.scanline_sprites);
self.framebuf.deinit(); self.framebuf.deinit();
self.scanline.deinit(); self.scanline.deinit();
self.vram.deinit(); self.vram.deinit();
@ -628,20 +628,20 @@ const Palette = struct {
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
fn init(alloc: Allocator) !Self { fn init(allocator: Allocator) !Self {
const buf = try alloc.alloc(u8, palram_size); const buf = try allocator.alloc(u8, palram_size);
std.mem.set(u8, buf, 0); std.mem.set(u8, buf, 0);
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
}; };
} }
fn deinit(self: *Self) void { fn deinit(self: *Self) void {
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }
@ -677,20 +677,20 @@ const Vram = struct {
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
fn init(alloc: Allocator) !Self { fn init(allocator: Allocator) !Self {
const buf = try alloc.alloc(u8, vram_size); const buf = try allocator.alloc(u8, vram_size);
std.mem.set(u8, buf, 0); std.mem.set(u8, buf, 0);
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
}; };
} }
fn deinit(self: *Self) void { fn deinit(self: *Self) void {
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }
@ -738,20 +738,20 @@ const Oam = struct {
const Self = @This(); const Self = @This();
buf: []u8, buf: []u8,
alloc: Allocator, allocator: Allocator,
fn init(alloc: Allocator) !Self { fn init(allocator: Allocator) !Self {
const buf = try alloc.alloc(u8, oam_size); const buf = try allocator.alloc(u8, oam_size);
std.mem.set(u8, buf, 0); std.mem.set(u8, buf, 0);
return Self{ return Self{
.buf = buf, .buf = buf,
.alloc = alloc, .allocator = allocator,
}; };
} }
fn deinit(self: *Self) void { fn deinit(self: *Self) void {
self.alloc.free(self.buf); self.allocator.free(self.buf);
self.* = undefined; self.* = undefined;
} }

View File

@ -14,8 +14,8 @@ pub const Scheduler = struct {
tick: u64, tick: u64,
queue: PriorityQueue(Event, void, lessThan), queue: PriorityQueue(Event, void, lessThan),
pub fn init(alloc: Allocator) Self { pub fn init(allocator: Allocator) Self {
var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(alloc, {}) }; var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(allocator, {}) };
sched.queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable; sched.queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable;
return sched; return sched;