fix(emu): share same main mem allocation between ARM7 + ARM9

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-09-04 22:22:57 -05:00
parent cef711a497
commit 3bc13b527e
4 changed files with 37 additions and 22 deletions

View File

@ -3,6 +3,7 @@ const nds9 = @import("nds9.zig");
const nds7 = @import("nds7.zig"); const nds7 = @import("nds7.zig");
const Header = @import("cartridge.zig").Header; const Header = @import("cartridge.zig").Header;
const SharedIo = @import("io.zig").Io;
const Arm946es = nds9.Arm946es; const Arm946es = nds9.Arm946es;
const Allocator = std.mem.Allocator; 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);
}
};

View File

@ -4,6 +4,7 @@ const io = @import("io.zig");
const Scheduler = @import("Scheduler.zig"); const Scheduler = @import("Scheduler.zig");
const SharedIo = @import("../io.zig").Io; const SharedIo = @import("../io.zig").Io;
const SharedContext = @import("../emu.zig").SharedContext;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -18,20 +19,16 @@ main: *[4 * MiB]u8,
wram: *[64 * KiB]u8, wram: *[64 * KiB]u8,
io: io.Io, io: io.Io,
pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_io: *SharedIo) !@This() { pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() {
const main_mem = try allocator.create([4 * MiB]u8);
errdefer allocator.destroy(main_mem);
@memset(main_mem, 0);
const wram = try allocator.create([64 * KiB]u8); const wram = try allocator.create([64 * KiB]u8);
errdefer allocator.destroy(wram); errdefer allocator.destroy(wram);
@memset(wram, 0); @memset(wram, 0);
return .{ return .{
.main = main_mem, .main = shared_ctx.main,
.wram = wram, .wram = wram,
.scheduler = scheduler, .scheduler = scheduler,
.io = io.Io.init(shared_io), .io = io.Io.init(shared_ctx.io),
}; };
} }

View File

@ -2,7 +2,7 @@ const std = @import("std");
const Ppu = @import("../ppu.zig").Ppu; const Ppu = @import("../ppu.zig").Ppu;
const Scheduler = @import("Scheduler.zig"); const Scheduler = @import("Scheduler.zig");
const SharedIo = @import("../io.zig").Io; const SharedContext = @import("../emu.zig").SharedContext;
const io = @import("io.zig"); const io = @import("io.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -20,11 +20,7 @@ ppu: Ppu,
scheduler: *Scheduler, scheduler: *Scheduler,
pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_io: *SharedIo) !@This() { pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() {
const main_mem = try allocator.create([4 * MiB]u8);
errdefer allocator.destroy(main_mem);
@memset(main_mem, 0);
const vram1_mem = try allocator.create([512 * KiB]u8); const vram1_mem = try allocator.create([512 * KiB]u8);
errdefer allocator.destroy(vram1_mem); errdefer allocator.destroy(vram1_mem);
@memset(vram1_mem, 0); @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); scheduler.push(.draw, 256 * dots_per_cycle);
return .{ return .{
.main = main_mem, .main = shared_ctx.main,
.vram1 = vram1_mem, .vram1 = vram1_mem,
.ppu = try Ppu.init(allocator), .ppu = try Ppu.init(allocator),
.scheduler = scheduler, .scheduler = scheduler,
.io = io.Io.init(shared_io), .io = io.Io.init(shared_ctx.io),
}; };
} }

View File

@ -8,7 +8,7 @@ const emu = @import("core/emu.zig");
const IBus = @import("arm32").Bus; const IBus = @import("arm32").Bus;
const IScheduler = @import("arm32").Scheduler; const IScheduler = @import("arm32").Scheduler;
const Ui = @import("platform.zig").Ui; 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 Allocator = std.mem.Allocator;
const ClapResult = clap.Result(clap.Help, &cli_params, clap.parsers.default); 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, .{}); const rom_file = try std.fs.cwd().openFile(rom_path, .{});
defer rom_file.close(); defer rom_file.close();
// FIXME: Perf win to allocating on the stack instead? const shared_ctx = try SharedContext.init(allocator);
const shared_io = try allocator.create(SharedIo); defer shared_ctx.deinit(allocator);
defer allocator.destroy(shared_io);
shared_io.* = .{};
const nds9_group: nds9.Group = blk: { const nds9_group: nds9.Group = blk: {
var scheduler = try nds9.Scheduler.init(allocator); 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)); var arm946es = nds9.Arm946es.init(IScheduler.init(&scheduler), IBus.init(&bus));
break :blk .{ .cpu = &arm946es, .bus = &bus, .scheduler = &scheduler }; break :blk .{ .cpu = &arm946es, .bus = &bus, .scheduler = &scheduler };
@ -52,7 +50,7 @@ pub fn main() !void {
const nds7_group: nds7.Group = blk: { const nds7_group: nds7.Group = blk: {
var scheduler = try nds7.Scheduler.init(allocator); 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)); var arm7tdmi = nds7.Arm7tdmi.init(IScheduler.init(&scheduler), IBus.init(&bus));
break :blk .{ .cpu = &arm7tdmi, .bus = &bus, .scheduler = &scheduler }; break :blk .{ .cpu = &arm7tdmi, .bus = &bus, .scheduler = &scheduler };