Compare commits

..

9 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka 1c7202aa0e fix: advance r15, even when the pipeline is reloaded from the scheduler
The PC would fall behind whenever an IRQ was called because the pipeline
was reloaded (+8 to PC), however that was never actually done by any code

Now, the PC is always incremented when the pipeline is reloaded
2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka b599a77d20 chore: dump pipeline state on cpu panic 2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka fb5d96abca fix: reimpl THUMB.5 instructions
pipeline branch now passes arm.gba and thumb.gba again

(TODO: Stop rewriting my commits away)
2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 95865bfcae fix: impl workaround for stage2 miscompilation 2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 9b6118fffb chore: instantly refill the pipeline on flush
I believe this to be necessary in order to get hardware interrupts
working.

thumb.gba test 108 fails but I'm committing anyways (despite the
regression) because this is kind of rebase/merge hell and I have
something that at least sort of works rn
2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 51bcffefd7 fix: reimpl handleInterrupt code 2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 783c41ce40 feat: implement basic pipeline
passes arm.gba, thumb.gb and armwrestler, fails in actual games
TODO: run FuzzARM debug specific titles
2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 7ffb11e619 feat: resolve off-by-{word, halfword} errors when printing debug info 2022-09-05 22:52:40 -03:00
Rekai Nyangadzayi Musuka 51296a8a8b feat: reimplement cpu logging 2022-09-05 22:52:40 -03:00
4 changed files with 47 additions and 74 deletions

View File

@ -180,11 +180,11 @@ const Audio = struct {
export fn callback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void { export fn callback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {
const apu = @ptrCast(*Apu, @alignCast(@alignOf(*Apu), userdata)); const apu = @ptrCast(*Apu, @alignCast(@alignOf(*Apu), userdata));
_ = SDL.SDL_AudioStreamGet(apu.stream, stream, len); const written = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
// If we don't write anything, play silence otherwise garbage will be played // If we don't write anything, play silence otherwise garbage will be played
// FIXME: I don't think this hack to remove DC Offset is acceptable :thinking: // FIXME: I don't think this hack to remove DC Offset is acceptable :thinking:
// if (written == 0) std.mem.set(u8, stream[0..@intCast(usize, len)], 0x40); if (written == 0) std.mem.set(u8, stream[0..@intCast(usize, len)], 0x40);
} }
}; };

View File

@ -163,8 +163,6 @@ pub const Apu = struct {
fs: FrameSequencer, fs: FrameSequencer,
capacitor: f32, capacitor: f32,
is_buffer_full: bool,
pub fn init(sched: *Scheduler) Self { pub fn init(sched: *Scheduler) Self {
const apu: Self = .{ const apu: Self = .{
.ch1 = ToneSweep.init(sched), .ch1 = ToneSweep.init(sched),
@ -180,12 +178,11 @@ pub const Apu = struct {
.bias = .{ .raw = 0x0200 }, .bias = .{ .raw = 0x0200 },
.sampling_cycle = 0b00, .sampling_cycle = 0b00,
.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, 1 << 15, SDL.AUDIO_U16, 2, host_sample_rate).?, .stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, 1 << 15, SDL.AUDIO_U16, 2, host_sample_rate) orelse unreachable,
.sched = sched, .sched = sched,
.capacitor = 0, .capacitor = 0,
.fs = FrameSequencer.init(), .fs = FrameSequencer.init(),
.is_buffer_full = false,
}; };
sched.push(.SampleAudio, apu.sampleTicks()); sched.push(.SampleAudio, apu.sampleTicks());
@ -280,13 +277,6 @@ pub const Apu = struct {
} }
pub fn sampleAudio(self: *Self, late: u64) void { pub fn sampleAudio(self: *Self, late: u64) void {
self.sched.push(.SampleAudio, self.sampleTicks() -| late);
// Whether the APU is busy or not is determined by the main loop in emu.zig
// This should only ever be true (because this side of the emu is single threaded)
// When audio sync is disaabled
if (self.is_buffer_full) return;
var left: i16 = 0; var left: i16 = 0;
var right: i16 = 0; var right: i16 = 0;
@ -335,30 +325,28 @@ pub const Apu = struct {
left += bias; left += bias;
right += bias; right += bias;
const clamped_left = std.math.clamp(@bitCast(u16, left), std.math.minInt(u11), std.math.maxInt(u11)); const tmp_left = std.math.clamp(@bitCast(u16, left), std.math.minInt(u11), std.math.maxInt(u11));
const clamped_right = std.math.clamp(@bitCast(u16, right), std.math.minInt(u11), std.math.maxInt(u11)); const tmp_right = std.math.clamp(@bitCast(u16, right), std.math.minInt(u11), std.math.maxInt(u11));
// Extend to 16-bit signed audio samples // Extend to 16-bit signed audio samples
const ext_left = (clamped_left << 5) | (clamped_left >> 6); const final_left = (tmp_left << 5) | (tmp_left >> 6);
const ext_right = (clamped_right << 5) | (clamped_right >> 6); const final_right = (tmp_right << 5) | (tmp_right >> 6);
// FIXME: This rarely happens if (self.sampling_cycle != self.bias.sampling_cycle.read()) {
if (self.sampling_cycle != self.bias.sampling_cycle.read()) self.replaceSDLResampler(); const new_sample_rate = Self.sampleRate(self.bias.sampling_cycle.read());
log.info("Sample Rate changed from {}Hz to {}Hz", .{ Self.sampleRate(self.sampling_cycle), new_sample_rate });
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ ext_left, ext_right }, 2 * @sizeOf(u16)); // Sample Rate Changed, Create a new Resampler since i can't figure out how to change
} // the parameters of the old one
const old = self.stream;
fn replaceSDLResampler(self: *Self) void { defer SDL.SDL_FreeAudioStream(old);
const sample_rate = Self.sampleRate(self.bias.sampling_cycle.read());
log.info("Sample Rate changed from {}Hz to {}Hz", .{ Self.sampleRate(self.sampling_cycle), sample_rate });
// Sampling Cycle (Sample Rate) changed, Craete a new SDL Audio Resampler
// FIXME: Replace SDL's Audio Resampler with either a custom or more reliable one
const old_stream = self.stream;
defer SDL.SDL_FreeAudioStream(old_stream);
self.sampling_cycle = self.bias.sampling_cycle.read(); self.sampling_cycle = self.bias.sampling_cycle.read();
self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, sample_rate), SDL.AUDIO_U16, 2, host_sample_rate).?; self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, new_sample_rate), SDL.AUDIO_U16, 2, host_sample_rate) orelse unreachable;
}
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ final_left, final_right }, 2 * @sizeOf(u16));
self.sched.push(.SampleAudio, self.sampleTicks() -| late);
} }
fn sampleTicks(self: *const Self) u64 { fn sampleTicks(self: *const Self) u64 {

View File

@ -28,14 +28,9 @@ pub const arm = struct {
const multiply = @import("cpu/arm/multiply.zig").multiply; const multiply = @import("cpu/arm/multiply.zig").multiply;
const multiplyLong = @import("cpu/arm/multiply.zig").multiplyLong; const multiplyLong = @import("cpu/arm/multiply.zig").multiplyLong;
/// Determine index into ARM InstrFn LUT
fn idx(opcode: u32) u12 {
return @truncate(u12, opcode >> 20 & 0xFF) << 4 | @truncate(u12, opcode >> 4 & 0xF);
}
// Undefined ARM Instruction handler // Undefined ARM Instruction handler
fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void { fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
const id = idx(opcode); const id = armIdx(opcode);
cpu.panic("[CPU/Decode] ID: 0x{X:0>3} 0x{X:0>8} is an illegal opcode", .{ id, opcode }); cpu.panic("[CPU/Decode] ID: 0x{X:0>3} 0x{X:0>8} is an illegal opcode", .{ id, opcode });
} }
@ -125,14 +120,9 @@ pub const thumb = struct {
const swi = @import("cpu/thumb/software_interrupt.zig").fmt17; const swi = @import("cpu/thumb/software_interrupt.zig").fmt17;
const branch = @import("cpu/thumb/branch.zig"); const branch = @import("cpu/thumb/branch.zig");
/// Determine index into THUMB InstrFn LUT
fn idx(opcode: u16) u10 {
return @truncate(u10, opcode >> 6);
}
/// Undefined THUMB Instruction Handler /// Undefined THUMB Instruction Handler
fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void { fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
const id = idx(opcode); const id = thumbIdx(opcode);
cpu.panic("[CPU/Decode] ID: 0b{b:0>10} 0x{X:0>2} is an illegal opcode", .{ id, opcode }); cpu.panic("[CPU/Decode] ID: 0b{b:0>10} 0x{X:0>2} is an illegal opcode", .{ id, opcode });
} }
@ -446,13 +436,13 @@ pub const Arm7tdmi = struct {
const opcode = @truncate(u16, self.pipe.step(self, u16) orelse break :blk); const opcode = @truncate(u16, self.pipe.step(self, u16) orelse break :blk);
if (cpu_logging) self.logger.?.mgbaLog(self, opcode); if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
thumb.lut[thumb.idx(opcode)](self, self.bus, opcode); thumb.lut[thumbIdx(opcode)](self, self.bus, opcode);
} else blk: { } else blk: {
const opcode = self.pipe.step(self, u32) orelse break :blk; const opcode = self.pipe.step(self, u32) orelse break :blk;
if (cpu_logging) self.logger.?.mgbaLog(self, opcode); if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) { if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) {
arm.lut[arm.idx(opcode)](self, self.bus, opcode); arm.lut[armIdx(opcode)](self, self.bus, opcode);
} }
} }
@ -551,11 +541,11 @@ pub const Arm7tdmi = struct {
if (self.cpsr.t.read()) { if (self.cpsr.t.read()) {
const opcode = self.bus.dbgRead(u16, self.r[15] - 4); const opcode = self.bus.dbgRead(u16, self.r[15] - 4);
const id = thumb.idx(opcode); const id = thumbIdx(opcode);
std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode });
} else { } else {
const opcode = self.bus.dbgRead(u32, self.r[15] - 4); const opcode = self.bus.dbgRead(u32, self.r[15] - 4);
const id = arm.idx(opcode); const id = armIdx(opcode);
std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode });
} }
@ -635,6 +625,14 @@ pub const Arm7tdmi = struct {
} }
}; };
inline fn armIdx(opcode: u32) u12 {
return @truncate(u12, opcode >> 20 & 0xFF) << 4 | @truncate(u12, opcode >> 4 & 0xF);
}
inline fn thumbIdx(opcode: u16) u10 {
return @truncate(u10, opcode >> 6);
}
pub fn checkCond(cpsr: PSR, cond: u4) bool { pub fn checkCond(cpsr: PSR, cond: u4) bool {
return switch (cond) { return switch (cond) {
0x0 => cpsr.z.read(), // EQ - Equal 0x0 => cpsr.z.read(), // EQ - Equal

View File

@ -70,21 +70,12 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
} }
} }
fn syncToAudio(stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void { fn syncToAudio(cpu: *const Arm7tdmi) void {
const sample_size = 2 * @sizeOf(u16); const stream = cpu.bus.apu.stream;
const max_buf_size: c_int = 0x400; const min_sample_count = 0x800;
// Determine whether the APU is busy right at this moment // Busy Loop while we wait for the Audio system to catch up
var still_full: bool = SDL.SDL_AudioStreamAvailable(stream) > sample_size * if (is_buffer_full.*) max_buf_size >> 1 else max_buf_size; while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {}
defer is_buffer_full.* = still_full; // Update APU Busy status right before exiting scope
// If Busy is false, there's no need to sync here
if (!still_full) return;
while (true) {
still_full = SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1;
if (!sync_audio or !still_full) break;
}
} }
pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void { pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
@ -95,21 +86,21 @@ pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi,
while (!quit.load(.SeqCst)) { while (!quit.load(.SeqCst)) {
runFrame(sched, cpu); runFrame(sched, cpu);
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full); if (sync_audio) syncToAudio(cpu);
tracker.tick(); tracker.tick();
} }
} else { } else {
while (!quit.load(.SeqCst)) { while (!quit.load(.SeqCst)) {
runFrame(sched, cpu); runFrame(sched, cpu);
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full); if (sync_audio) syncToAudio(cpu);
} }
} }
} }
pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void { pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
log.info("Emulation thread w/ video sync", .{}); log.info("Emulation thread w/ video sync", .{});
var timer = Timer.start() catch std.debug.panic("Failed to initialize std.timer.Timer", .{}); var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period; var wake_time: u64 = frame_period;
if (fps) |tracker| { if (fps) |tracker| {
@ -117,14 +108,13 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
while (!quit.load(.SeqCst)) { while (!quit.load(.SeqCst)) {
runFrame(sched, cpu); runFrame(sched, cpu);
const new_wake_time = blockOnVideo(&timer, wake_time); const new_wake_time = syncToVideo(&timer, wake_time);
// Spin to make up the difference of OS scheduler innacuracies // Spin to make up the difference of OS scheduler innacuracies
// If we happen to also be syncing to audio, we choose to spin on // If we happen to also be syncing to audio, we choose to spin on
// the amount of time needed for audio to catch up rather than // the amount of time needed for audio to catch up rather than
// our expected wake-up time // our expected wake-up time
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full); if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
if (!sync_audio) spinLoop(&timer, wake_time);
wake_time = new_wake_time; wake_time = new_wake_time;
tracker.tick(); tracker.tick();
@ -132,17 +122,16 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
} else { } else {
while (!quit.load(.SeqCst)) { while (!quit.load(.SeqCst)) {
runFrame(sched, cpu); runFrame(sched, cpu);
const new_wake_time = blockOnVideo(&timer, wake_time); const new_wake_time = syncToVideo(&timer, wake_time);
// see above comment // see above comment
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full); if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
if (!sync_audio) spinLoop(&timer, wake_time);
wake_time = new_wake_time; wake_time = new_wake_time;
} }
} }
} }
inline fn blockOnVideo(timer: *Timer, wake_time: u64) u64 { inline fn syncToVideo(timer: *Timer, wake_time: u64) u64 {
// Use the OS scheduler to put the emulation thread to sleep // Use the OS scheduler to put the emulation thread to sleep
const maybe_recalc_wake_time = sleep(timer, wake_time); const maybe_recalc_wake_time = sleep(timer, wake_time);
@ -160,8 +149,6 @@ pub fn runBusyLoop(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void
runFrame(sched, cpu); runFrame(sched, cpu);
spinLoop(&timer, wake_time); spinLoop(&timer, wake_time);
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
// Update to the new wake time // Update to the new wake time
wake_time += frame_period; wake_time += frame_period;
} }