Compare commits
No commits in common. "1945b0f5717dd5244b6d717434b887ee2e336411" and "172a59aefb1988963c4becbe8e4f6e51fa150c32" have entirely different histories.
1945b0f571
...
172a59aefb
39
src/apu.zig
39
src/apu.zig
|
@ -11,6 +11,9 @@ const AudioDeviceId = SDL.SDL_AudioDeviceID;
|
||||||
const intToBytes = @import("util.zig").intToBytes;
|
const intToBytes = @import("util.zig").intToBytes;
|
||||||
const log = std.log.scoped(.APU);
|
const log = std.log.scoped(.APU);
|
||||||
|
|
||||||
|
const sample_rate = 32768;
|
||||||
|
const sample_ticks = 280896 * 60 / sample_rate;
|
||||||
|
|
||||||
pub const Apu = struct {
|
pub const Apu = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
@ -26,8 +29,7 @@ pub const Apu = struct {
|
||||||
dma_cnt: io.DmaSoundControl,
|
dma_cnt: io.DmaSoundControl,
|
||||||
cnt: io.SoundControl,
|
cnt: io.SoundControl,
|
||||||
|
|
||||||
sampling_cycle: u2,
|
dev: ?AudioDeviceId,
|
||||||
stream: *SDL.SDL_AudioStream,
|
|
||||||
sched: *Scheduler,
|
sched: *Scheduler,
|
||||||
|
|
||||||
pub fn init(sched: *Scheduler) Self {
|
pub fn init(sched: *Scheduler) Self {
|
||||||
|
@ -44,16 +46,18 @@ pub const Apu = struct {
|
||||||
.cnt = .{ .raw = 0 },
|
.cnt = .{ .raw = 0 },
|
||||||
.bias = .{ .raw = 0x0200 },
|
.bias = .{ .raw = 0x0200 },
|
||||||
|
|
||||||
.sampling_cycle = 0b00,
|
.dev = null,
|
||||||
.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_F32, 2, 1 << 15, SDL.AUDIO_F32, 2, 48000) orelse unreachable,
|
|
||||||
.sched = sched,
|
.sched = sched,
|
||||||
};
|
};
|
||||||
|
sched.push(.SampleAudio, sched.now() + sample_ticks);
|
||||||
sched.push(.SampleAudio, sched.now() + apu.sampleTicks());
|
|
||||||
|
|
||||||
return apu;
|
return apu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn attachAudioDevice(self: *Self, dev: AudioDeviceId) void {
|
||||||
|
self.dev = dev;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn setDmaCnt(self: *Self, value: u16) void {
|
pub fn setDmaCnt(self: *Self, value: u16) void {
|
||||||
const new: io.DmaSoundControl = .{ .raw = value };
|
const new: io.DmaSoundControl = .{ .raw = value };
|
||||||
|
|
||||||
|
@ -93,28 +97,9 @@ pub const Apu = struct {
|
||||||
const left = (chA_left + chB_left) / 2;
|
const left = (chA_left + chB_left) / 2;
|
||||||
const right = (chA_right + chB_right) / 2;
|
const right = (chA_right + chB_right) / 2;
|
||||||
|
|
||||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) {
|
if (self.dev) |dev| _ = SDL.SDL_QueueAudio(dev, &[2]f32{ left, right }, 2 * @sizeOf(f32));
|
||||||
// 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.sched.push(.SampleAudio, self.sched.now() + sample_ticks - late);
|
||||||
self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_F32, 2, @intCast(c_int, self.sampleRate()), SDL.AUDIO_F32, 2, 48000) orelse unreachable;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (SDL.SDL_AudioStreamAvailable(self.stream) > self.sampleRate() / 2) {} // Really Naive Audio Sync
|
|
||||||
|
|
||||||
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]f32{ left, right }, 2 * @sizeOf(f32));
|
|
||||||
self.sched.push(.SampleAudio, self.sched.now() + self.sampleTicks() - late);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fn sampleTicks(self: *const Self) u64 {
|
|
||||||
return (1 << 24) / self.sampleRate();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fn sampleRate(self: *const Self) u64 {
|
|
||||||
return @as(u64, 1) << (15 + @as(u6, self.bias.sampling_cycle.read()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handleTimerOverflow(self: *Self, cpu: *Arm7tdmi, tim_id: u3) void {
|
pub fn handleTimerOverflow(self: *Self, cpu: *Arm7tdmi, tim_id: u3) void {
|
||||||
|
|
39
src/main.zig
39
src/main.zig
|
@ -5,7 +5,6 @@ const known_folders = @import("known_folders");
|
||||||
|
|
||||||
const emu = @import("emu.zig");
|
const emu = @import("emu.zig");
|
||||||
const Bus = @import("Bus.zig");
|
const Bus = @import("Bus.zig");
|
||||||
const Apu = @import("apu.zig").Apu;
|
|
||||||
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
||||||
const FpsAverage = @import("util.zig").FpsAverage;
|
const FpsAverage = @import("util.zig").FpsAverage;
|
||||||
|
@ -71,6 +70,10 @@ pub fn main() anyerror!void {
|
||||||
_ = initSdl2();
|
_ = initSdl2();
|
||||||
defer SDL.SDL_Quit();
|
defer SDL.SDL_Quit();
|
||||||
|
|
||||||
|
// Initialize SDL Audio
|
||||||
|
const audio_dev = initAudio();
|
||||||
|
defer SDL.SDL_CloseAudioDevice(audio_dev);
|
||||||
|
|
||||||
// Initialize Emulator
|
// Initialize Emulator
|
||||||
var scheduler = Scheduler.init(alloc);
|
var scheduler = Scheduler.init(alloc);
|
||||||
defer scheduler.deinit();
|
defer scheduler.deinit();
|
||||||
|
@ -78,11 +81,9 @@ pub fn main() anyerror!void {
|
||||||
const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_path };
|
const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_path };
|
||||||
var cpu = try Arm7tdmi.init(alloc, &scheduler, paths);
|
var cpu = try Arm7tdmi.init(alloc, &scheduler, paths);
|
||||||
defer cpu.deinit();
|
defer cpu.deinit();
|
||||||
cpu.fastBoot();
|
|
||||||
|
|
||||||
// Initialize SDL Audio
|
cpu.bus.apu.attachAudioDevice(audio_dev);
|
||||||
const audio_dev = initAudio(&cpu.bus.apu);
|
cpu.fastBoot();
|
||||||
defer SDL.SDL_CloseAudioDevice(audio_dev);
|
|
||||||
|
|
||||||
const log_file: ?File = if (enable_logging) blk: {
|
const log_file: ?File = if (enable_logging) blk: {
|
||||||
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
|
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
|
||||||
|
@ -237,31 +238,19 @@ fn createTexture(renderer: *SDL.SDL_Renderer, width: c_int, height: c_int) *SDL.
|
||||||
) orelse sdlPanic();
|
) orelse sdlPanic();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initAudio(apu: *Apu) SDL.SDL_AudioDeviceID {
|
fn initAudio() SDL.SDL_AudioDeviceID {
|
||||||
var have: SDL.SDL_AudioSpec = undefined;
|
var have: SDL.SDL_AudioSpec = undefined;
|
||||||
var want: SDL.SDL_AudioSpec = .{
|
var want = std.mem.zeroes(SDL.SDL_AudioSpec);
|
||||||
.freq = 48000,
|
want.freq = 32768;
|
||||||
.format = SDL.AUDIO_F32,
|
want.format = SDL.AUDIO_F32;
|
||||||
.channels = 2,
|
want.channels = 2;
|
||||||
.samples = 0x100,
|
want.samples = 0x100;
|
||||||
.callback = audioCallback,
|
want.callback = null;
|
||||||
.userdata = apu,
|
|
||||||
.silence = undefined,
|
|
||||||
.size = undefined,
|
|
||||||
.padding = undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const dev = SDL.SDL_OpenAudioDevice(null, 0, &want, &have, 0);
|
const dev = SDL.SDL_OpenAudioDevice(null, 0, &want, &have, 0);
|
||||||
if (dev == 0) sdlPanic();
|
if (dev == 0) sdlPanic();
|
||||||
|
|
||||||
// Start Playback on the Audio device
|
// Start Playback on the Audio evice
|
||||||
SDL.SDL_PauseAudioDevice(dev, 0);
|
SDL.SDL_PauseAudioDevice(dev, 0);
|
||||||
return dev;
|
return dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn audioCallback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {
|
|
||||||
const apu = @ptrCast(*Apu, @alignCast(8, userdata));
|
|
||||||
const result = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
|
||||||
|
|
||||||
if (result < 0) log.err("Audio Callback Underflow", .{});
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue