tmp: implement mechanisms for a emu reset fn (currently crashes)

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-02-23 22:12:06 -06:00
parent 3fff4fd742
commit 6ebd66eb3b
4 changed files with 33 additions and 4 deletions

View File

@ -314,6 +314,14 @@ pub const Arm7tdmi = struct {
};
}
// FIXME: Resetting disables logging (if enabled)
pub fn reset(self: *Self) void {
const bus_ptr = self.bus;
const scheduler_ptr = self.sched;
self.* = Self.init(scheduler_ptr, bus_ptr, null);
}
pub inline fn hasSPSR(self: *const Self) bool {
const mode = getModeChecked(self, self.cpsr.mode.read());
return switch (mode) {

View File

@ -222,3 +222,10 @@ pub const EmuThing = struct {
}
}
};
pub fn reset(cpu: *Arm7tdmi) void {
@breakpoint();
cpu.sched.reset(); // Yes this is order sensitive, see the PPU reset for why
// cpu.bus.reset();
cpu.reset();
}

View File

@ -11,11 +11,11 @@ const log = std.log.scoped(.Scheduler);
pub const Scheduler = struct {
const Self = @This();
tick: u64,
tick: u64 = 0,
queue: PriorityQueue(Event, void, lessThan),
pub fn init(allocator: Allocator) Self {
var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(allocator, {}) };
var sched = Self{ .queue = PriorityQueue(Event, void, lessThan).init(allocator, {}) };
sched.queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable;
return sched;
@ -26,6 +26,17 @@ pub const Scheduler = struct {
self.* = undefined;
}
pub fn reset(self: *Self) void {
// `std.PriorityQueue` provides no reset function, so we will just create a new one
const allocator = self.queue.allocator;
self.queue.deinit();
var new_queue = PriorityQueue(Event, void, lessThan).init(allocator, {});
new_queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable;
self.* = .{ .queue = new_queue };
}
pub inline fn now(self: *const Self) u64 {
return self.tick;
}

View File

@ -48,7 +48,7 @@ pub const State = struct {
}
};
pub fn draw(state: *State, tex_id: GLuint, cpu: *const Arm7tdmi) void {
pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void {
const win_scale = config.config().host.win_scale;
{
@ -77,7 +77,10 @@ pub fn draw(state: *State, tex_id: GLuint, cpu: *const Arm7tdmi) void {
if (zgui.beginMenu("Emulation", true)) {
defer zgui.endMenu();
if (zgui.menuItem("Restart", .{})) log.warn("TODO: Restart Emulator", .{});
if (zgui.menuItem("Restart", .{})) {
@breakpoint();
emu.reset(cpu);
}
}
}