Compare commits
6 Commits
dd52b8f6c1
...
891087dd0b
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 891087dd0b | |
Rekai Nyangadzayi Musuka | 9dc8b52d13 | |
Rekai Nyangadzayi Musuka | 5eb12e9765 | |
Rekai Nyangadzayi Musuka | 84f814f661 | |
Rekai Nyangadzayi Musuka | 65cfc97f28 | |
Rekai Nyangadzayi Musuka | fa862f095a |
|
@ -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));
|
||||||
const written = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
_ = 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -163,6 +163,8 @@ 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),
|
||||||
|
@ -178,11 +180,12 @@ 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) orelse unreachable,
|
.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, 1 << 15, SDL.AUDIO_U16, 2, host_sample_rate).?,
|
||||||
.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());
|
||||||
|
@ -277,6 +280,13 @@ 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;
|
||||||
|
|
||||||
|
@ -325,28 +335,30 @@ pub const Apu = struct {
|
||||||
left += bias;
|
left += bias;
|
||||||
right += bias;
|
right += bias;
|
||||||
|
|
||||||
const tmp_left = std.math.clamp(@bitCast(u16, left), std.math.minInt(u11), std.math.maxInt(u11));
|
const clamped_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));
|
const clamped_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 final_left = (tmp_left << 5) | (tmp_left >> 6);
|
const ext_left = (clamped_left << 5) | (clamped_left >> 6);
|
||||||
const final_right = (tmp_right << 5) | (tmp_right >> 6);
|
const ext_right = (clamped_right << 5) | (clamped_right >> 6);
|
||||||
|
|
||||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) {
|
// FIXME: This rarely happens
|
||||||
const new_sample_rate = Self.sampleRate(self.bias.sampling_cycle.read());
|
if (self.sampling_cycle != self.bias.sampling_cycle.read()) self.replaceSDLResampler();
|
||||||
log.info("Sample Rate changed from {}Hz to {}Hz", .{ Self.sampleRate(self.sampling_cycle), new_sample_rate });
|
|
||||||
|
|
||||||
// Sample Rate Changed, Create a new Resampler since i can't figure out how to change
|
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ ext_left, ext_right }, 2 * @sizeOf(u16));
|
||||||
// the parameters of the old one
|
}
|
||||||
const old = self.stream;
|
|
||||||
defer SDL.SDL_FreeAudioStream(old);
|
|
||||||
|
|
||||||
self.sampling_cycle = self.bias.sampling_cycle.read();
|
fn replaceSDLResampler(self: *Self) void {
|
||||||
self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, new_sample_rate), SDL.AUDIO_U16, 2, host_sample_rate) orelse unreachable;
|
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 });
|
||||||
|
|
||||||
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ final_left, final_right }, 2 * @sizeOf(u16));
|
// Sampling Cycle (Sample Rate) changed, Craete a new SDL Audio Resampler
|
||||||
self.sched.push(.SampleAudio, self.sampleTicks() -| late);
|
// 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.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, sample_rate), SDL.AUDIO_U16, 2, host_sample_rate).?;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sampleTicks(self: *const Self) u64 {
|
fn sampleTicks(self: *const Self) u64 {
|
||||||
|
|
|
@ -28,9 +28,14 @@ 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 = armIdx(opcode);
|
const id = idx(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 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,9 +125,14 @@ 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 = thumbIdx(opcode);
|
const id = idx(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 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,13 +430,13 @@ pub const Arm7tdmi = struct {
|
||||||
const opcode = self.fetch(u16);
|
const opcode = self.fetch(u16);
|
||||||
if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
|
if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
|
||||||
|
|
||||||
thumb.lut[thumbIdx(opcode)](self, self.bus, opcode);
|
thumb.lut[thumb.idx(opcode)](self, self.bus, opcode);
|
||||||
} else {
|
} else {
|
||||||
const opcode = self.fetch(u32);
|
const opcode = self.fetch(u32);
|
||||||
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[armIdx(opcode)](self, self.bus, opcode);
|
arm.lut[arm.idx(opcode)](self, self.bus, opcode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -517,11 +527,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 = thumbIdx(opcode);
|
const id = thumb.idx(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 = armIdx(opcode);
|
const id = arm.idx(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 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -601,14 +611,6 @@ 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
|
||||||
|
|
|
@ -70,12 +70,21 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syncToAudio(cpu: *const Arm7tdmi) void {
|
fn syncToAudio(stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void {
|
||||||
const stream = cpu.bus.apu.stream;
|
const sample_size = 2 * @sizeOf(u16);
|
||||||
const min_sample_count = 0x800;
|
const max_buf_size: c_int = 0x400;
|
||||||
|
|
||||||
// Busy Loop while we wait for the Audio system to catch up
|
// Determine whether the APU is busy right at this moment
|
||||||
while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {}
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -86,21 +95,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);
|
||||||
if (sync_audio) syncToAudio(cpu);
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||||
|
|
||||||
tracker.tick();
|
tracker.tick();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while (!quit.load(.SeqCst)) {
|
while (!quit.load(.SeqCst)) {
|
||||||
runFrame(sched, cpu);
|
runFrame(sched, cpu);
|
||||||
if (sync_audio) syncToAudio(cpu);
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 unreachable;
|
var timer = Timer.start() catch std.debug.panic("Failed to initialize std.timer.Timer", .{});
|
||||||
var wake_time: u64 = frame_period;
|
var wake_time: u64 = frame_period;
|
||||||
|
|
||||||
if (fps) |tracker| {
|
if (fps) |tracker| {
|
||||||
|
@ -108,13 +117,14 @@ 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 = syncToVideo(&timer, wake_time);
|
const new_wake_time = blockOnVideo(&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
|
||||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||||
|
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||||
wake_time = new_wake_time;
|
wake_time = new_wake_time;
|
||||||
|
|
||||||
tracker.tick();
|
tracker.tick();
|
||||||
|
@ -122,16 +132,17 @@ 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 = syncToVideo(&timer, wake_time);
|
const new_wake_time = blockOnVideo(&timer, wake_time);
|
||||||
// see above comment
|
|
||||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
|
|
||||||
|
|
||||||
|
// see above comment
|
||||||
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||||
|
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||||
wake_time = new_wake_time;
|
wake_time = new_wake_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn syncToVideo(timer: *Timer, wake_time: u64) u64 {
|
inline fn blockOnVideo(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);
|
||||||
|
|
||||||
|
@ -149,6 +160,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue