diff --git a/src/bus.zig b/src/bus.zig index 37b240a..3f73f54 100644 --- a/src/bus.zig +++ b/src/bus.zig @@ -22,6 +22,12 @@ pub const Bus = struct { }; } + pub fn deinit(self: *@This()) void { + self.pak.deinit(); + self.bios.deinit(); + self.ppu.deinit(); + } + pub fn read32(self: *const @This(), addr: u32) u32 { return switch (addr) { // General Internal Memory diff --git a/src/bus/bios.zig b/src/bus/bios.zig index a078ef8..389de16 100644 --- a/src/bus/bios.zig +++ b/src/bus/bios.zig @@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator; pub const Bios = struct { buf: []u8, + alloc: Allocator, pub fn init(alloc: Allocator, path: []const u8) !@This() { const file = try std.fs.cwd().openFile(path, .{ .read = true }); @@ -13,9 +14,14 @@ pub const Bios = struct { return @This(){ .buf = try file.readToEndAlloc(alloc, len), + .alloc = alloc, }; } + pub fn deinit(self: *@This()) void { + self.alloc.free(self.buf); + } + pub inline fn get32(self: *const @This(), idx: usize) u32 { std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{}); return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx])); diff --git a/src/bus/pak.zig b/src/bus/pak.zig index d642917..c9ab442 100644 --- a/src/bus/pak.zig +++ b/src/bus/pak.zig @@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator; pub const GamePak = struct { buf: []u8, + alloc: Allocator, pub fn init(alloc: Allocator, path: []const u8) !@This() { const file = try std.fs.cwd().openFile(path, .{ .read = true }); @@ -13,9 +14,14 @@ pub const GamePak = struct { return @This(){ .buf = try file.readToEndAlloc(alloc, len), + .alloc = alloc, }; } + pub fn deinit(self: *@This()) void { + self.alloc.free(self.buf); + } + pub inline fn get32(self: *const @This(), idx: usize) u32 { return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx])); } diff --git a/src/main.zig b/src/main.zig index a21849e..6dadc7c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,7 +8,7 @@ const Arm7tdmi = @import("cpu.zig").Arm7tdmi; pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); - defer _ = gpa.deinit(); + defer std.debug.assert(!gpa.deinit()); const args = try std.process.argsAlloc(alloc); defer std.process.argsFree(alloc, args); @@ -24,7 +24,11 @@ pub fn main() anyerror!void { } var bus = try Bus.init(alloc, zba_args[0]); + defer bus.deinit(); + var scheduler = Scheduler.init(alloc); + defer scheduler.deinit(); + var cpu = Arm7tdmi.init(&scheduler, &bus); cpu.skipBios(); diff --git a/src/ppu.zig b/src/ppu.zig index 387db69..783ab1f 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -10,17 +10,27 @@ pub const Ppu = struct { .vram = try Vram.init(alloc), }; } + + pub fn deinit(self: *@This()) void { + self.vram.deinit(); + } }; const Vram = struct { buf: []u8, + alloc: Allocator, fn init(alloc: Allocator) !@This() { return @This(){ .buf = try alloc.alloc(u8, 0x18000), + .alloc = alloc, }; } + fn deinit(self: *@This()) void { + self.alloc.free(self.buf); + } + pub inline fn get32(self: *const @This(), idx: usize) u32 { return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx])); } diff --git a/src/scheduler.zig b/src/scheduler.zig index 7ffc95d..5b6d202 100644 --- a/src/scheduler.zig +++ b/src/scheduler.zig @@ -21,6 +21,10 @@ pub const Scheduler = struct { return scheduler; } + pub fn deinit(self: *@This()) void { + self.queue.deinit(); + } + pub fn handleEvent(self: *@This(), _: *Arm7tdmi, _: *Bus) void { const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;