From 75ba9a4bf942171f7c0a0eacbcab4a7f8f27f32b Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 21 Apr 2022 07:56:17 -0300 Subject: [PATCH] fix: improper lifetime for *Arm7tdmi ptr in Bus *Arm7tdmi ptr is now assigned one scope up so that it lives as least as long as Bus does --- src/Bus.zig | 4 ++++ src/cpu.zig | 4 +--- src/main.zig | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index 37c8d7e..5b8e912 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -60,6 +60,10 @@ pub fn deinit(self: Self) void { self.ppu.deinit(); } +pub fn attach(self: *Self, cpu: *Arm7tdmi) void { + self.cpu = cpu; +} + pub fn handleDMATransfers(self: *Self) void { while (self.isDmaRunning()) { if (self.dma._1.step(self)) continue; diff --git a/src/cpu.zig b/src/cpu.zig index 665b840..06e8871 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -80,7 +80,7 @@ pub const Arm7tdmi = struct { binary_log: bool, pub fn init(alloc: Allocator, sched: *Scheduler, paths: FilePaths) !Self { - var cpu: Arm7tdmi = .{ + return Self{ .r = [_]u32{0x00} ** 16, .sched = sched, .bus = try Bus.init(alloc, sched, paths), @@ -93,8 +93,6 @@ pub const Arm7tdmi = struct { .log_buf = undefined, .binary_log = false, }; - cpu.bus.cpu = &cpu; - return cpu; } pub fn deinit(self: Self) void { diff --git a/src/main.zig b/src/main.zig index 90a8266..3cad404 100644 --- a/src/main.zig +++ b/src/main.zig @@ -80,6 +80,8 @@ pub fn main() anyerror!void { const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_path }; var cpu = try Arm7tdmi.init(alloc, &scheduler, paths); defer cpu.deinit(); + + cpu.bus.attach(&cpu); cpu.fastBoot(); // Initialize SDL Audio