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,
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();

View File

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

View File

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

View File

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

View File

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

View File

@ -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});
};

View File

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

View File

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