Compare commits

..

2 Commits

5 changed files with 39 additions and 25 deletions

View File

@ -1,6 +1,6 @@
const std = @import("std");
/// For use with withe tiniest ROM
/// For use with with the tiniest ROM
pub const Header = extern struct {
title: [12]u8,
game_code: [4]u8,
@ -79,8 +79,7 @@ pub const Header = extern struct {
// note, we're missing some debug_ prefixed fields here
// but we want the header struct to be 0x160 bytes so that
// the smallest NDS rom's header can be read without any speicifc
// workarounds
// the smallest NDS rom's header can be read without any specific workarounds
// TODO: Determine if we ever will need those debug fields, and if so: Implement them
comptime {

View File

@ -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);
}
};

View File

@ -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),
};
}

View File

@ -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),
};
}

View File

@ -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 };