diff --git a/src/core/emu.zig b/src/core/emu.zig index 4a5685c..a005c6d 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -3,6 +3,7 @@ const nds9 = @import("nds9.zig"); const nds7 = @import("nds7.zig"); const Header = @import("cartridge.zig").Header; +const SharedIo = @import("io.zig").Io; const Arm946es = nds9.Arm946es; const Allocator = std.mem.Allocator; @@ -107,3 +108,26 @@ pub fn runFrame(nds7_group: nds7.Group, nds9_group: nds9.Group) void { } } } + +// FIXME: Perf win to allocating on the stack instead? +pub const SharedContext = struct { + const MiB = 0x100000; + + io: *SharedIo, + main: *[4 * MiB]u8, + + pub fn init(allocator: Allocator) !@This() { + const ctx = .{ + .io = try allocator.create(SharedIo), + .main = try allocator.create([4 * MiB]u8), + }; + ctx.io.* = .{}; + + return ctx; + } + + pub fn deinit(self: @This(), allocator: Allocator) void { + allocator.destroy(self.io); + allocator.destroy(self.main); + } +}; diff --git a/src/core/nds7/Bus.zig b/src/core/nds7/Bus.zig index 66c94a9..c474c3e 100644 --- a/src/core/nds7/Bus.zig +++ b/src/core/nds7/Bus.zig @@ -4,6 +4,7 @@ const io = @import("io.zig"); const Scheduler = @import("Scheduler.zig"); const SharedIo = @import("../io.zig").Io; +const SharedContext = @import("../emu.zig").SharedContext; const Allocator = std.mem.Allocator; @@ -18,20 +19,16 @@ main: *[4 * MiB]u8, wram: *[64 * KiB]u8, io: io.Io, -pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_io: *SharedIo) !@This() { - const main_mem = try allocator.create([4 * MiB]u8); - errdefer allocator.destroy(main_mem); - @memset(main_mem, 0); - +pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() { const wram = try allocator.create([64 * KiB]u8); errdefer allocator.destroy(wram); @memset(wram, 0); return .{ - .main = main_mem, + .main = shared_ctx.main, .wram = wram, .scheduler = scheduler, - .io = io.Io.init(shared_io), + .io = io.Io.init(shared_ctx.io), }; } diff --git a/src/core/nds9/Bus.zig b/src/core/nds9/Bus.zig index b52991a..d62e73c 100644 --- a/src/core/nds9/Bus.zig +++ b/src/core/nds9/Bus.zig @@ -2,7 +2,7 @@ const std = @import("std"); const Ppu = @import("../ppu.zig").Ppu; const Scheduler = @import("Scheduler.zig"); -const SharedIo = @import("../io.zig").Io; +const SharedContext = @import("../emu.zig").SharedContext; const io = @import("io.zig"); const Allocator = std.mem.Allocator; @@ -20,11 +20,7 @@ ppu: Ppu, scheduler: *Scheduler, -pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_io: *SharedIo) !@This() { - const main_mem = try allocator.create([4 * MiB]u8); - errdefer allocator.destroy(main_mem); - @memset(main_mem, 0); - +pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() { const vram1_mem = try allocator.create([512 * KiB]u8); errdefer allocator.destroy(vram1_mem); @memset(vram1_mem, 0); @@ -33,11 +29,11 @@ pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_io: *SharedIo) ! scheduler.push(.draw, 256 * dots_per_cycle); return .{ - .main = main_mem, + .main = shared_ctx.main, .vram1 = vram1_mem, .ppu = try Ppu.init(allocator), .scheduler = scheduler, - .io = io.Io.init(shared_io), + .io = io.Io.init(shared_ctx.io), }; } diff --git a/src/main.zig b/src/main.zig index 8a56da9..5035c0f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,7 +8,7 @@ const emu = @import("core/emu.zig"); const IBus = @import("arm32").Bus; const IScheduler = @import("arm32").Scheduler; const Ui = @import("platform.zig").Ui; -const SharedIo = @import("core/io.zig").Io; +const SharedContext = @import("core/emu.zig").SharedContext; const Allocator = std.mem.Allocator; const ClapResult = clap.Result(clap.Help, &cli_params, clap.parsers.default); @@ -36,14 +36,12 @@ pub fn main() !void { const rom_file = try std.fs.cwd().openFile(rom_path, .{}); defer rom_file.close(); - // FIXME: Perf win to allocating on the stack instead? - const shared_io = try allocator.create(SharedIo); - defer allocator.destroy(shared_io); - shared_io.* = .{}; + const shared_ctx = try SharedContext.init(allocator); + defer shared_ctx.deinit(allocator); const nds9_group: nds9.Group = blk: { var scheduler = try nds9.Scheduler.init(allocator); - var bus = try nds9.Bus.init(allocator, &scheduler, shared_io); + var bus = try nds9.Bus.init(allocator, &scheduler, shared_ctx); var arm946es = nds9.Arm946es.init(IScheduler.init(&scheduler), IBus.init(&bus)); break :blk .{ .cpu = &arm946es, .bus = &bus, .scheduler = &scheduler }; @@ -52,7 +50,7 @@ pub fn main() !void { const nds7_group: nds7.Group = blk: { var scheduler = try nds7.Scheduler.init(allocator); - var bus = try nds7.Bus.init(allocator, &scheduler, shared_io); + var bus = try nds7.Bus.init(allocator, &scheduler, shared_ctx); var arm7tdmi = nds7.Arm7tdmi.init(IScheduler.init(&scheduler), IBus.init(&bus)); break :blk .{ .cpu = &arm7tdmi, .bus = &bus, .scheduler = &scheduler };