Compare commits
4 Commits
891087dd0b
...
dd52b8f6c1
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | dd52b8f6c1 | |
Rekai Nyangadzayi Musuka | 07a0a9ac2b | |
Rekai Nyangadzayi Musuka | b1fa3cccb7 | |
Rekai Nyangadzayi Musuka | 8f6746dcc6 |
|
@ -180,11 +180,11 @@ const Audio = struct {
|
|||
|
||||
export fn callback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {
|
||||
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
|
||||
// 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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -163,8 +163,6 @@ pub const Apu = struct {
|
|||
fs: FrameSequencer,
|
||||
capacitor: f32,
|
||||
|
||||
is_buffer_full: bool,
|
||||
|
||||
pub fn init(sched: *Scheduler) Self {
|
||||
const apu: Self = .{
|
||||
.ch1 = ToneSweep.init(sched),
|
||||
|
@ -180,12 +178,11 @@ pub const Apu = struct {
|
|||
.bias = .{ .raw = 0x0200 },
|
||||
|
||||
.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,
|
||||
|
||||
.capacitor = 0,
|
||||
.fs = FrameSequencer.init(),
|
||||
.is_buffer_full = false,
|
||||
};
|
||||
|
||||
sched.push(.SampleAudio, apu.sampleTicks());
|
||||
|
@ -280,13 +277,6 @@ pub const Apu = struct {
|
|||
}
|
||||
|
||||
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 right: i16 = 0;
|
||||
|
||||
|
@ -335,30 +325,28 @@ pub const Apu = struct {
|
|||
left += bias;
|
||||
right += bias;
|
||||
|
||||
const clamped_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_left = std.math.clamp(@bitCast(u16, left), 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
|
||||
const ext_left = (clamped_left << 5) | (clamped_left >> 6);
|
||||
const ext_right = (clamped_right << 5) | (clamped_right >> 6);
|
||||
const final_left = (tmp_left << 5) | (tmp_left >> 6);
|
||||
const final_right = (tmp_right << 5) | (tmp_right >> 6);
|
||||
|
||||
// FIXME: This rarely happens
|
||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) self.replaceSDLResampler();
|
||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) {
|
||||
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));
|
||||
}
|
||||
|
||||
fn replaceSDLResampler(self: *Self) void {
|
||||
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);
|
||||
// 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;
|
||||
defer SDL.SDL_FreeAudioStream(old);
|
||||
|
||||
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 {
|
||||
|
|
|
@ -28,14 +28,9 @@ pub const arm = struct {
|
|||
const multiply = @import("cpu/arm/multiply.zig").multiply;
|
||||
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
|
||||
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 });
|
||||
}
|
||||
|
||||
|
@ -125,14 +120,9 @@ pub const thumb = struct {
|
|||
const swi = @import("cpu/thumb/software_interrupt.zig").fmt17;
|
||||
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
|
||||
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 });
|
||||
}
|
||||
|
||||
|
@ -430,13 +420,13 @@ pub const Arm7tdmi = struct {
|
|||
const opcode = self.fetch(u16);
|
||||
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 {
|
||||
const opcode = self.fetch(u32);
|
||||
if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -527,11 +517,11 @@ pub const Arm7tdmi = struct {
|
|||
|
||||
if (self.cpsr.t.read()) {
|
||||
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 });
|
||||
} else {
|
||||
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 });
|
||||
}
|
||||
|
||||
|
@ -611,6 +601,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 {
|
||||
return switch (cond) {
|
||||
0x0 => cpsr.z.read(), // EQ - Equal
|
||||
|
|
|
@ -70,21 +70,12 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
|||
}
|
||||
}
|
||||
|
||||
fn syncToAudio(stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void {
|
||||
const sample_size = 2 * @sizeOf(u16);
|
||||
const max_buf_size: c_int = 0x400;
|
||||
fn syncToAudio(cpu: *const Arm7tdmi) void {
|
||||
const stream = cpu.bus.apu.stream;
|
||||
const min_sample_count = 0x800;
|
||||
|
||||
// Determine whether the APU is busy right at this moment
|
||||
var still_full: bool = SDL.SDL_AudioStreamAvailable(stream) > sample_size * if (is_buffer_full.*) max_buf_size >> 1 else max_buf_size;
|
||||
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;
|
||||
}
|
||||
// Busy Loop while we wait for the Audio system to catch up
|
||||
while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {}
|
||||
}
|
||||
|
||||
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)) {
|
||||
runFrame(sched, cpu);
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
if (sync_audio) syncToAudio(cpu);
|
||||
|
||||
tracker.tick();
|
||||
}
|
||||
} else {
|
||||
while (!quit.load(.SeqCst)) {
|
||||
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 {
|
||||
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;
|
||||
|
||||
if (fps) |tracker| {
|
||||
|
@ -117,14 +108,13 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
|
|||
|
||||
while (!quit.load(.SeqCst)) {
|
||||
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
|
||||
// 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
|
||||
// our expected wake-up time
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
|
||||
wake_time = new_wake_time;
|
||||
|
||||
tracker.tick();
|
||||
|
@ -132,17 +122,16 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
|
|||
} else {
|
||||
while (!quit.load(.SeqCst)) {
|
||||
runFrame(sched, cpu);
|
||||
const new_wake_time = blockOnVideo(&timer, wake_time);
|
||||
|
||||
const new_wake_time = syncToVideo(&timer, wake_time);
|
||||
// see above comment
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, 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
|
||||
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);
|
||||
spinLoop(&timer, wake_time);
|
||||
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
|
||||
// Update to the new wake time
|
||||
wake_time += frame_period;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue