feat: implement resetting

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-03-10 02:27:31 -06:00
parent bd872ee1c0
commit f8477714ae
7 changed files with 51 additions and 9 deletions

View File

@ -104,7 +104,7 @@ pub fn deinit(self: *Self) void {
pub fn reset(self: *Self) void {
self.bios.reset();
// TODO: deinit ppu
self.ppu.reset();
self.apu.reset();
self.iwram.reset();
self.ewram.reset();

View File

@ -278,16 +278,20 @@ pub const Apu = struct {
.is_buffer_full = false,
};
sched.push(.SampleAudio, apu.interval());
sched.push(.{ .ApuChannel = 0 }, @import("apu/signal/Square.zig").interval);
sched.push(.{ .ApuChannel = 1 }, @import("apu/signal/Square.zig").interval);
sched.push(.{ .ApuChannel = 2 }, @import("apu/signal/Wave.zig").interval);
sched.push(.{ .ApuChannel = 3 }, @import("apu/signal/Lfsr.zig").interval);
sched.push(.FrameSequencer, FrameSequencer.interval);
Self.initEvents(apu.sched, apu.interval());
return apu;
}
fn initEvents(scheduler: *Scheduler, apu_interval: u64) void {
scheduler.push(.SampleAudio, apu_interval);
scheduler.push(.{ .ApuChannel = 0 }, @import("apu/signal/Square.zig").interval);
scheduler.push(.{ .ApuChannel = 1 }, @import("apu/signal/Square.zig").interval);
scheduler.push(.{ .ApuChannel = 2 }, @import("apu/signal/Wave.zig").interval);
scheduler.push(.{ .ApuChannel = 3 }, @import("apu/signal/Lfsr.zig").interval);
scheduler.push(.FrameSequencer, FrameSequencer.interval);
}
/// Used when resetting the emulator
pub fn reset(self: *Self) void {
// FIXME: These reset functions are meant to emulate obscure APU behaviour. Write proper emu reset fns
@ -306,6 +310,8 @@ pub const Apu = struct {
self.sampling_cycle = 0;
self.fs.reset();
Self.initEvents(self.sched, self.interval());
}
/// Emulates the reset behaviour of the APU

View File

@ -290,6 +290,26 @@ pub const Ppu = struct {
};
}
pub fn reset(self: *Self) void {
self.sched.push(.Draw, 240 * 4);
self.vram.reset();
self.palette.reset();
self.oam.reset();
self.framebuf.reset();
self.win = Window.init();
self.bg = [_]Background{Background.init()} ** 4;
self.aff_bg = [_]AffineBackground{AffineBackground.init()} ** 2;
self.bld = Blend.create();
self.dispcnt = .{ .raw = 0x0000 };
self.dispstat = .{ .raw = 0x0000 };
self.vcount = .{ .raw = 0x0000 };
self.scanline.reset();
std.mem.set(?Sprite, self.scanline_sprites, null);
}
pub fn deinit(self: *Self) void {
self.allocator.destroy(self.scanline_sprites);
self.framebuf.deinit();

View File

@ -34,6 +34,10 @@ pub fn init(allocator: Allocator) !Self {
return Self{ .buf = buf, .allocator = allocator };
}
pub fn reset(self: *Self) void {
std.mem.set(u8, self.buf, 0);
}
pub fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;

View File

@ -37,6 +37,10 @@ pub fn init(allocator: Allocator) !Self {
return Self{ .buf = buf, .allocator = allocator };
}
pub fn reset(self: *Self) void {
std.mem.set(u8, self.buf, 0);
}
pub fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;

View File

@ -45,6 +45,10 @@ pub fn init(allocator: Allocator) !Self {
return Self{ .buf = buf, .allocator = allocator };
}
pub fn reset(self: *Self) void {
std.mem.set(u8, self.buf, 0);
}
pub fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;

View File

@ -251,7 +251,7 @@ pub const FrameBuffer = struct {
layers: [2][]u8,
buf: []u8,
current: u1,
current: u1 = 0,
allocator: Allocator,
@ -266,12 +266,16 @@ pub const FrameBuffer = struct {
// Front and Back Framebuffers
.layers = [_][]u8{ buf[0..][0..len], buf[len..][0..len] },
.buf = buf,
.current = 0,
.allocator = allocator,
};
}
pub fn reset(self: *Self) void {
std.mem.set(u8, self.buf, 0);
self.current = 0;
}
pub fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;