fix(emu): share same main mem allocation between ARM7 + ARM9
This commit is contained in:
		@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -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),
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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),
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user