Compare commits
4 Commits
23f5d676d4
...
apu-things
Author | SHA1 | Date | |
---|---|---|---|
2f2c03b96d | |||
9a2b7a48c0 | |||
fe908a6ea9 | |||
bf95eee3f1 |
@@ -107,7 +107,7 @@ pub fn deinit(self: *Self) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fillReadTable(bus: *Self, table: *[table_len]?*const anyopaque) void {
|
fn fillReadTable(bus: *Self, table: *[table_len]?*const anyopaque) void {
|
||||||
const vramMirror = @import("ppu/Vram.zig").mirror;
|
const vramMirror = @import("ppu.zig").Vram.mirror;
|
||||||
|
|
||||||
for (table) |*ptr, i| {
|
for (table) |*ptr, i| {
|
||||||
const addr = page_size * i;
|
const addr = page_size * i;
|
||||||
@@ -134,7 +134,7 @@ fn fillReadTable(bus: *Self, table: *[table_len]?*const anyopaque) void {
|
|||||||
|
|
||||||
fn fillWriteTable(comptime T: type, bus: *Self, table: *[table_len]?*const anyopaque) void {
|
fn fillWriteTable(comptime T: type, bus: *Self, table: *[table_len]?*const anyopaque) void {
|
||||||
comptime std.debug.assert(T == u32 or T == u16 or T == u8);
|
comptime std.debug.assert(T == u32 or T == u16 or T == u8);
|
||||||
const vramMirror = @import("ppu/Vram.zig").mirror;
|
const vramMirror = @import("ppu.zig").Vram.mirror;
|
||||||
|
|
||||||
for (table) |*ptr, i| {
|
for (table) |*ptr, i| {
|
||||||
const addr = page_size * i;
|
const addr = page_size * i;
|
||||||
|
@@ -15,12 +15,10 @@ const SoundFifo = std.fifo.LinearFifo(u8, .{ .Static = 0x20 });
|
|||||||
const getHalf = util.getHalf;
|
const getHalf = util.getHalf;
|
||||||
const setHalf = util.setHalf;
|
const setHalf = util.setHalf;
|
||||||
const intToBytes = util.intToBytes;
|
const intToBytes = util.intToBytes;
|
||||||
|
const RingBuffer = util.RingBuffer;
|
||||||
|
|
||||||
const log = std.log.scoped(.APU);
|
const log = std.log.scoped(.APU);
|
||||||
|
|
||||||
pub const host_rate = @import("../platform.zig").sample_rate;
|
|
||||||
pub const host_format = @import("../platform.zig").sample_format;
|
|
||||||
|
|
||||||
pub fn read(comptime T: type, apu: *const Apu, addr: u32) ?T {
|
pub fn read(comptime T: type, apu: *const Apu, addr: u32) ?T {
|
||||||
const byte_addr = @truncate(u8, addr);
|
const byte_addr = @truncate(u8, addr);
|
||||||
|
|
||||||
@@ -246,17 +244,20 @@ pub const Apu = struct {
|
|||||||
|
|
||||||
sampling_cycle: u2,
|
sampling_cycle: u2,
|
||||||
|
|
||||||
stream: *SDL.SDL_AudioStream,
|
sample_queue: RingBuffer(u16),
|
||||||
sched: *Scheduler,
|
sched: *Scheduler,
|
||||||
|
|
||||||
fs: FrameSequencer,
|
fs: FrameSequencer,
|
||||||
capacitor: f32,
|
capacitor: f32,
|
||||||
|
|
||||||
is_buffer_full: bool,
|
|
||||||
|
|
||||||
pub const Tick = enum { Length, Envelope, Sweep };
|
pub const Tick = enum { Length, Envelope, Sweep };
|
||||||
|
|
||||||
pub fn init(sched: *Scheduler) Self {
|
pub fn init(sched: *Scheduler) Self {
|
||||||
|
const NUM_CHANNELS: usize = 2;
|
||||||
|
|
||||||
|
const allocator = std.heap.c_allocator;
|
||||||
|
const sample_buf = allocator.alloc(u16, 0x800 * NUM_CHANNELS) catch @panic("failed to allocate sample buffer");
|
||||||
|
|
||||||
const apu: Self = .{
|
const apu: Self = .{
|
||||||
.ch1 = ToneSweep.init(sched),
|
.ch1 = ToneSweep.init(sched),
|
||||||
.ch2 = Tone.init(sched),
|
.ch2 = Tone.init(sched),
|
||||||
@@ -271,12 +272,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, host_format, 2, host_rate).?,
|
.sample_queue = RingBuffer(u16).init(sample_buf),
|
||||||
.sched = sched,
|
.sched = sched,
|
||||||
|
|
||||||
.capacitor = 0,
|
.capacitor = 0,
|
||||||
.fs = FrameSequencer.init(),
|
.fs = FrameSequencer.init(),
|
||||||
.is_buffer_full = false,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sched.push(.SampleAudio, apu.interval());
|
sched.push(.SampleAudio, apu.interval());
|
||||||
@@ -370,11 +370,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.interval() -| late);
|
self.sched.push(.SampleAudio, self.interval() -| 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;
|
||||||
|
|
||||||
@@ -430,23 +425,7 @@ pub const Apu = struct {
|
|||||||
const ext_left = (clamped_left << 5) | (clamped_left >> 6);
|
const ext_left = (clamped_left << 5) | (clamped_left >> 6);
|
||||||
const ext_right = (clamped_right << 5) | (clamped_right >> 6);
|
const ext_right = (clamped_right << 5) | (clamped_right >> 6);
|
||||||
|
|
||||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) self.replaceSDLResampler();
|
self.sample_queue.push(ext_left, ext_right) catch {};
|
||||||
|
|
||||||
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ ext_left, ext_right }, 2 * @sizeOf(u16));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn replaceSDLResampler(self: *Self) void {
|
|
||||||
@setCold(true);
|
|
||||||
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.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, sample_rate), host_format, 2, host_rate).?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn interval(self: *const Self) u64 {
|
fn interval(self: *const Self) u64 {
|
||||||
|
@@ -94,10 +94,9 @@ pub fn sound1CntL(self: *const Self) u8 {
|
|||||||
pub fn setSound1CntL(self: *Self, value: u8) void {
|
pub fn setSound1CntL(self: *Self, value: u8) void {
|
||||||
const new = io.Sweep{ .raw = value };
|
const new = io.Sweep{ .raw = value };
|
||||||
|
|
||||||
if (self.sweep.direction.read() and !new.direction.read()) {
|
if (!new.direction.read()) {
|
||||||
// Sweep Negate bit has been cleared
|
// If at least one (1) sweep calculation has been made with
|
||||||
// If At least 1 Sweep Calculation has been made since
|
// the negate bit set (since last trigger), disable the channel
|
||||||
// the last trigger, the channel is immediately disabled
|
|
||||||
|
|
||||||
if (self.sweep_dev.calc_performed) self.enabled = false;
|
if (self.sweep_dev.calc_performed) self.enabled = false;
|
||||||
}
|
}
|
||||||
|
@@ -31,7 +31,6 @@ pub fn tick(self: *Self, ch1: *ToneSweep) void {
|
|||||||
if (self.timer == 0) {
|
if (self.timer == 0) {
|
||||||
const period = ch1.sweep.period.read();
|
const period = ch1.sweep.period.read();
|
||||||
self.timer = if (period == 0) 8 else period;
|
self.timer = if (period == 0) 8 else period;
|
||||||
if (!self.calc_performed) self.calc_performed = true;
|
|
||||||
|
|
||||||
if (self.enabled and period != 0) {
|
if (self.enabled and period != 0) {
|
||||||
const new_freq = self.calculate(ch1.sweep, &ch1.enabled);
|
const new_freq = self.calculate(ch1.sweep, &ch1.enabled);
|
||||||
@@ -52,7 +51,10 @@ pub fn calculate(self: *Self, sweep: io.Sweep, ch_enable: *bool) u12 {
|
|||||||
const shadow_shifted = shadow >> sweep.shift.read();
|
const shadow_shifted = shadow >> sweep.shift.read();
|
||||||
const decrease = sweep.direction.read();
|
const decrease = sweep.direction.read();
|
||||||
|
|
||||||
const freq = if (decrease) shadow - shadow_shifted else shadow + shadow_shifted;
|
const freq = if (decrease) blk: {
|
||||||
|
self.calc_performed = true;
|
||||||
|
break :blk shadow - shadow_shifted;
|
||||||
|
} else shadow + shadow_shifted;
|
||||||
if (freq > 0x7FF) ch_enable.* = false;
|
if (freq > 0x7FF) ch_enable.* = false;
|
||||||
|
|
||||||
return freq;
|
return freq;
|
||||||
|
@@ -338,7 +338,7 @@ fn DmaController(comptime id: u2) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn onBlanking(bus: *Bus, comptime kind: DmaKind) void {
|
pub fn pollDmaOnBlank(bus: *Bus, comptime kind: DmaKind) void {
|
||||||
comptime var i: usize = 0;
|
comptime var i: usize = 0;
|
||||||
inline while (i < 4) : (i += 1) {
|
inline while (i < 4) : (i += 1) {
|
||||||
bus.dma[i].poll(kind);
|
bus.dma[i].poll(kind);
|
||||||
|
@@ -449,8 +449,6 @@ pub const BldY = extern union {
|
|||||||
raw: u16,
|
raw: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
const u8WriteKind = enum { Hi, Lo };
|
|
||||||
|
|
||||||
/// Write-only
|
/// Write-only
|
||||||
pub const WinH = extern union {
|
pub const WinH = extern union {
|
||||||
x2: Bitfield(u16, 0, 8),
|
x2: Bitfield(u16, 0, 8),
|
||||||
@@ -460,8 +458,6 @@ pub const WinH = extern union {
|
|||||||
|
|
||||||
/// Write-only
|
/// Write-only
|
||||||
pub const WinV = extern union {
|
pub const WinV = extern union {
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
y2: Bitfield(u16, 0, 8),
|
y2: Bitfield(u16, 0, 8),
|
||||||
y1: Bitfield(u16, 8, 8),
|
y1: Bitfield(u16, 8, 8),
|
||||||
raw: u16,
|
raw: u16,
|
||||||
@@ -470,20 +466,20 @@ pub const WinV = extern union {
|
|||||||
pub const WinIn = extern union {
|
pub const WinIn = extern union {
|
||||||
w0_bg: Bitfield(u16, 0, 4),
|
w0_bg: Bitfield(u16, 0, 4),
|
||||||
w0_obj: Bit(u16, 4),
|
w0_obj: Bit(u16, 4),
|
||||||
w0_bld: Bit(u16, 5),
|
w0_colour: Bit(u16, 5),
|
||||||
w1_bg: Bitfield(u16, 8, 4),
|
w1_bg: Bitfield(u16, 8, 4),
|
||||||
w1_obj: Bit(u16, 12),
|
w1_obj: Bit(u16, 12),
|
||||||
w1_bld: Bit(u16, 13),
|
w1_colour: Bit(u16, 13),
|
||||||
raw: u16,
|
raw: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const WinOut = extern union {
|
pub const WinOut = extern union {
|
||||||
out_bg: Bitfield(u16, 0, 4),
|
out_bg: Bitfield(u16, 0, 4),
|
||||||
out_obj: Bit(u16, 4),
|
out_obj: Bit(u16, 4),
|
||||||
out_bld: Bit(u16, 5),
|
out_colour: Bit(u16, 5),
|
||||||
obj_bg: Bitfield(u16, 8, 4),
|
obj_bg: Bitfield(u16, 8, 4),
|
||||||
obj_obj: Bit(u16, 12),
|
obj_obj: Bit(u16, 12),
|
||||||
obj_bld: Bit(u16, 13),
|
obj_colour: Bit(u16, 13),
|
||||||
raw: u16,
|
raw: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -5,6 +5,7 @@ const config = @import("../config.zig");
|
|||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
||||||
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
||||||
const FpsTracker = @import("../util.zig").FpsTracker;
|
const FpsTracker = @import("../util.zig").FpsTracker;
|
||||||
|
const RingBuffer = @import("../util.zig").RingBuffer;
|
||||||
|
|
||||||
const Timer = std.time.Timer;
|
const Timer = std.time.Timer;
|
||||||
const Atomic = std.atomic.Atomic;
|
const Atomic = std.atomic.Atomic;
|
||||||
@@ -58,7 +59,7 @@ fn inner(comptime kind: RunKind, audio_sync: bool, quit: *Atomic(bool), schedule
|
|||||||
|
|
||||||
while (!quit.load(.Monotonic)) {
|
while (!quit.load(.Monotonic)) {
|
||||||
runFrame(scheduler, cpu);
|
runFrame(scheduler, cpu);
|
||||||
audioSync(audio_sync, cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
audioSync(audio_sync, &cpu.bus.apu.sample_queue);
|
||||||
|
|
||||||
if (kind == .UnlimitedFPS) tracker.?.tick();
|
if (kind == .UnlimitedFPS) tracker.?.tick();
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,7 @@ fn inner(comptime kind: RunKind, audio_sync: bool, quit: *Atomic(bool), schedule
|
|||||||
// 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
|
||||||
|
|
||||||
audioSync(audio_sync, cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
audioSync(audio_sync, &cpu.bus.apu.sample_queue);
|
||||||
if (!audio_sync) spinLoop(&timer, wake_time);
|
if (!audio_sync) spinLoop(&timer, wake_time);
|
||||||
wake_time = new_wake_time;
|
wake_time = new_wake_time;
|
||||||
|
|
||||||
@@ -104,22 +105,13 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn audioSync(audio_sync: bool, stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void {
|
fn audioSync(audio_sync: bool, sample_queue: *RingBuffer(u16)) void {
|
||||||
comptime std.debug.assert(@import("../platform.zig").sample_format == SDL.AUDIO_U16);
|
comptime std.debug.assert(@import("../platform.zig").sample_format == SDL.AUDIO_U16);
|
||||||
const sample_size = 2 * @sizeOf(u16);
|
// const sample_size = 2 * @sizeOf(u16);
|
||||||
const max_buf_size: c_int = 0x400;
|
// const max_buf_size: c_int = 0x400;
|
||||||
|
|
||||||
// Determine whether the APU is busy right at this moment
|
_ = audio_sync;
|
||||||
var still_full: bool = SDL.SDL_AudioStreamAvailable(stream) > sample_size * if (is_buffer_full.*) max_buf_size >> 1 else max_buf_size;
|
_ = sample_queue;
|
||||||
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 (!audio_sync or !still_full) break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn videoSync(timer: *Timer, wake_time: u64) u64 {
|
fn videoSync(timer: *Timer, wake_time: u64) u64 {
|
||||||
|
786
src/core/ppu.zig
786
src/core/ppu.zig
File diff suppressed because it is too large
Load Diff
@@ -1,40 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
const buf_len = 0x400;
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
buf: []u8,
|
|
||||||
allocator: Allocator,
|
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
|
||||||
const addr = address & 0x3FF;
|
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32, u16, u8 => std.mem.readIntSliceLittle(T, self.buf[addr..][0..@sizeOf(T)]),
|
|
||||||
else => @compileError("OAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
|
|
||||||
const addr = address & 0x3FF;
|
|
||||||
|
|
||||||
switch (T) {
|
|
||||||
u32, u16 => std.mem.writeIntSliceLittle(T, self.buf[addr..][0..@sizeOf(T)], value),
|
|
||||||
u8 => return, // 8-bit writes are explicitly ignored
|
|
||||||
else => @compileError("OAM: Unsupported write width"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !Self {
|
|
||||||
const buf = try allocator.alloc(u8, buf_len);
|
|
||||||
std.mem.set(u8, buf, 0);
|
|
||||||
|
|
||||||
return Self{ .buf = buf, .allocator = allocator };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
|
||||||
self.allocator.free(self.buf);
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
@@ -1,47 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
const buf_len = 0x400;
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
buf: []u8,
|
|
||||||
allocator: Allocator,
|
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
|
||||||
const addr = address & 0x3FF;
|
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32, u16, u8 => std.mem.readIntSliceLittle(T, self.buf[addr..][0..@sizeOf(T)]),
|
|
||||||
else => @compileError("PALRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
|
|
||||||
const addr = address & 0x3FF;
|
|
||||||
|
|
||||||
switch (T) {
|
|
||||||
u32, u16 => std.mem.writeIntSliceLittle(T, self.buf[addr..][0..@sizeOf(T)], value),
|
|
||||||
u8 => {
|
|
||||||
const align_addr = addr & ~@as(u32, 1); // Aligned to Halfword boundary
|
|
||||||
std.mem.writeIntSliceLittle(u16, self.buf[align_addr..][0..@sizeOf(u16)], @as(u16, value) * 0x101);
|
|
||||||
},
|
|
||||||
else => @compileError("PALRAM: Unsupported write width"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !Self {
|
|
||||||
const buf = try allocator.alloc(u8, buf_len);
|
|
||||||
std.mem.set(u8, buf, 0);
|
|
||||||
|
|
||||||
return Self{ .buf = buf, .allocator = allocator };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
|
||||||
self.allocator.free(self.buf);
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub inline fn backdrop(self: *const Self) u16 {
|
|
||||||
return std.mem.readIntNative(u16, self.buf[0..2]);
|
|
||||||
}
|
|
@@ -1,60 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const io = @import("../bus/io.zig");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
const buf_len = 0x18000;
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
buf: []u8,
|
|
||||||
allocator: Allocator,
|
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
|
||||||
const addr = Self.mirror(address);
|
|
||||||
|
|
||||||
return switch (T) {
|
|
||||||
u32, u16, u8 => std.mem.readIntSliceLittle(T, self.buf[addr..][0..@sizeOf(T)]),
|
|
||||||
else => @compileError("VRAM: Unsupported read width"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(self: *Self, comptime T: type, dispcnt: io.DisplayControl, address: usize, value: T) void {
|
|
||||||
const mode: u3 = dispcnt.bg_mode.read();
|
|
||||||
const idx = Self.mirror(address);
|
|
||||||
|
|
||||||
switch (T) {
|
|
||||||
u32, u16 => std.mem.writeIntSliceLittle(T, self.buf[idx..][0..@sizeOf(T)], value),
|
|
||||||
u8 => {
|
|
||||||
// Ignore write if it falls within the boundaries of OBJ VRAM
|
|
||||||
switch (mode) {
|
|
||||||
0, 1, 2 => if (0x0001_0000 <= idx) return,
|
|
||||||
else => if (0x0001_4000 <= idx) return,
|
|
||||||
}
|
|
||||||
|
|
||||||
const align_idx = idx & ~@as(u32, 1); // Aligned to a halfword boundary
|
|
||||||
std.mem.writeIntSliceLittle(u16, self.buf[align_idx..][0..@sizeOf(u16)], @as(u16, value) * 0x101);
|
|
||||||
},
|
|
||||||
else => @compileError("VRAM: Unsupported write width"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !Self {
|
|
||||||
const buf = try allocator.alloc(u8, buf_len);
|
|
||||||
std.mem.set(u8, buf, 0);
|
|
||||||
|
|
||||||
return Self{ .buf = buf, .allocator = allocator };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
|
||||||
self.allocator.free(self.buf);
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mirror(address: usize) usize {
|
|
||||||
// Mirrored in steps of 128K (64K + 32K + 32K) (abcc)
|
|
||||||
const addr = address & 0x1FFFF;
|
|
||||||
|
|
||||||
// If the address is within 96K we don't do anything,
|
|
||||||
// otherwise we want to mirror the last 32K (addresses between 64K and 96K)
|
|
||||||
return if (addr < buf_len) addr else 0x10000 + (addr & 0x7FFF);
|
|
||||||
}
|
|
@@ -12,7 +12,7 @@ const FpsTracker = @import("util.zig").FpsTracker;
|
|||||||
const gba_width = @import("core/ppu.zig").width;
|
const gba_width = @import("core/ppu.zig").width;
|
||||||
const gba_height = @import("core/ppu.zig").height;
|
const gba_height = @import("core/ppu.zig").height;
|
||||||
|
|
||||||
pub const sample_rate = 1 << 15;
|
pub const sample_rate = 1 << 16;
|
||||||
pub const sample_format = SDL.AUDIO_U16;
|
pub const sample_format = SDL.AUDIO_U16;
|
||||||
|
|
||||||
const default_title = "ZBA";
|
const default_title = "ZBA";
|
||||||
@@ -216,7 +216,7 @@ pub const Gui = struct {
|
|||||||
SDL.SDLK_RSHIFT => keyinput.select.set(),
|
SDL.SDLK_RSHIFT => keyinput.select.set(),
|
||||||
SDL.SDLK_i => {
|
SDL.SDLK_i => {
|
||||||
comptime std.debug.assert(sample_format == SDL.AUDIO_U16);
|
comptime std.debug.assert(sample_format == SDL.AUDIO_U16);
|
||||||
log.err("Sample Count: {}", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(cpu.bus.apu.stream)) / (2 * @sizeOf(u16))});
|
log.err("Sample Count: {}", .{cpu.bus.apu.sample_queue.len() / 2});
|
||||||
},
|
},
|
||||||
// SDL.SDLK_j => log.err("Scheduler Capacity: {} | Scheduler Event Count: {}", .{ scheduler.queue.capacity(), scheduler.queue.count() }),
|
// SDL.SDLK_j => log.err("Scheduler Capacity: {} | Scheduler Event Count: {}", .{ scheduler.queue.capacity(), scheduler.queue.count() }),
|
||||||
SDL.SDLK_k => {},
|
SDL.SDLK_k => {},
|
||||||
@@ -299,7 +299,15 @@ const Audio = struct {
|
|||||||
const T = *Apu;
|
const T = *Apu;
|
||||||
const apu = @ptrCast(T, @alignCast(@alignOf(T), userdata));
|
const apu = @ptrCast(T, @alignCast(@alignOf(T), userdata));
|
||||||
|
|
||||||
_ = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
comptime std.debug.assert(sample_format == SDL.AUDIO_U16);
|
||||||
|
const sample_buf = @ptrCast([*]u16, @alignCast(@alignOf(u16), stream))[0 .. @intCast(u32, len) / @sizeOf(u16)];
|
||||||
|
|
||||||
|
var previous: u16 = 0x8000;
|
||||||
|
for (sample_buf) |*sample| {
|
||||||
|
if (apu.sample_queue.pop()) |value| previous = value;
|
||||||
|
|
||||||
|
sample.* = previous;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
119
src/util.zig
119
src/util.zig
@@ -5,8 +5,6 @@ const config = @import("config.zig");
|
|||||||
const Log2Int = std.math.Log2Int;
|
const Log2Int = std.math.Log2Int;
|
||||||
const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi;
|
const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi;
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
// Sign-Extend value of type `T` to type `U`
|
// Sign-Extend value of type `T` to type `U`
|
||||||
pub fn sext(comptime T: type, comptime U: type, value: T) T {
|
pub fn sext(comptime T: type, comptime U: type, value: T) T {
|
||||||
// U must have less bits than T
|
// U must have less bits than T
|
||||||
@@ -125,7 +123,6 @@ pub const io = struct {
|
|||||||
|
|
||||||
pub const Logger = struct {
|
pub const Logger = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const FmtArgTuple = std.meta.Tuple(&.{ u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 });
|
|
||||||
|
|
||||||
buf: std.io.BufferedWriter(4096 << 2, std.fs.File.Writer),
|
buf: std.io.BufferedWriter(4096 << 2, std.fs.File.Writer),
|
||||||
|
|
||||||
@@ -184,6 +181,8 @@ pub const Logger = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const FmtArgTuple = struct { u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 };
|
||||||
|
|
||||||
pub const audio = struct {
|
pub const audio = struct {
|
||||||
const _io = @import("core/bus/io.zig");
|
const _io = @import("core/bus/io.zig");
|
||||||
|
|
||||||
@@ -277,43 +276,109 @@ fn HalfInt(comptime T: type) type {
|
|||||||
return std.meta.Int(type_info.Int.signedness, type_info.Int.bits >> 1);
|
return std.meta.Int(type_info.Int.signedness, type_info.Int.bits >> 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Double Buffering Implementation
|
const Mutex = std.Thread.Mutex;
|
||||||
pub const FrameBuffer = struct {
|
|
||||||
|
pub fn RingBuffer(comptime T: type) type {
|
||||||
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
const Index = usize;
|
||||||
|
const max_capacity = (@as(Index, 1) << @typeInfo(Index).Int.bits - 1) - 1; // half the range of index type
|
||||||
|
|
||||||
layers: [2][]u8,
|
const log = std.log.scoped(.RingBuffer);
|
||||||
buf: []u8,
|
|
||||||
current: u1,
|
|
||||||
|
|
||||||
allocator: Allocator,
|
read: Index,
|
||||||
|
write: Index,
|
||||||
|
|
||||||
// TODO: Rename
|
buf: []T,
|
||||||
const Device = enum { Emulator, Renderer };
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, comptime len: comptime_int) !Self {
|
mutex: Mutex,
|
||||||
const buf = try allocator.alloc(u8, len * 2);
|
|
||||||
std.mem.set(u8, buf, 0);
|
|
||||||
|
|
||||||
return .{
|
const Error = error{buffer_full};
|
||||||
// Front and Back Framebuffers
|
|
||||||
.layers = [_][]u8{ buf[0..][0..len], buf[len..][0..len] },
|
|
||||||
.buf = buf,
|
|
||||||
.current = 0,
|
|
||||||
|
|
||||||
.allocator = allocator,
|
pub fn init(buf: []T) Self {
|
||||||
|
std.mem.set(T, buf, 0);
|
||||||
|
|
||||||
|
std.debug.assert(std.math.isPowerOfTwo(buf.len)); // capacity must be a power of two
|
||||||
|
std.debug.assert(buf.len <= max_capacity);
|
||||||
|
|
||||||
|
return .{ .read = 0, .write = 0, .buf = buf, .mutex = .{} };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(self: *Self, left: T, right: T) Error!void {
|
||||||
|
self.mutex.lock();
|
||||||
|
defer self.mutex.unlock();
|
||||||
|
|
||||||
|
try self._push(left);
|
||||||
|
self._push(right) catch |e| {
|
||||||
|
self.write -= 1; // undo the previous write;
|
||||||
|
return e;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn pop(self: *Self) ?T {
|
||||||
self.allocator.free(self.buf);
|
self.mutex.lock();
|
||||||
self.* = undefined;
|
defer self.mutex.unlock();
|
||||||
|
|
||||||
|
return self._pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn swap(self: *Self) void {
|
pub fn len(self: *Self) Index {
|
||||||
self.current = ~self.current;
|
self.mutex.lock();
|
||||||
|
defer self.mutex.unlock();
|
||||||
|
|
||||||
|
return self._len();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: *Self, comptime dev: Device) []u8 {
|
fn _push(self: *Self, value: T) Error!void {
|
||||||
return self.layers[if (dev == .Emulator) self.current else ~self.current];
|
if (self.isFull()) return error.buffer_full;
|
||||||
|
defer self.write += 1;
|
||||||
|
|
||||||
|
self.buf[self.mask(self.write)] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _pop(self: *Self) ?T {
|
||||||
|
if (self.isEmpty()) return null;
|
||||||
|
defer self.read += 1;
|
||||||
|
|
||||||
|
return self.buf[self.mask(self.read)];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _len(self: *const Self) Index {
|
||||||
|
return self.write - self.read;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isFull(self: *const Self) bool {
|
||||||
|
return self._len() == self.buf.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isEmpty(self: *const Self) bool {
|
||||||
|
return self.read == self.write;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mask(self: *const Self, idx: Index) Index {
|
||||||
|
return idx & (self.buf.len - 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test "RingBuffer" {
|
||||||
|
const Queue = RingBuffer(u8);
|
||||||
|
|
||||||
|
var buf: [4]u8 = undefined;
|
||||||
|
var queue = Queue.init(&buf);
|
||||||
|
|
||||||
|
try queue.push(1, 2);
|
||||||
|
try std.testing.expectEqual(@as(?u8, 1), queue.pop());
|
||||||
|
|
||||||
|
try queue.push(3, 4);
|
||||||
|
try std.testing.expectError(Queue.Error.buffer_full, queue.push(5, 6));
|
||||||
|
try std.testing.expectEqual(@as(?u8, 2), queue.pop());
|
||||||
|
|
||||||
|
try queue.push(7, 8);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(?u8, 3), queue.pop());
|
||||||
|
try std.testing.expectEqual(@as(?u8, 4), queue.pop());
|
||||||
|
try std.testing.expectEqual(@as(?u8, 7), queue.pop());
|
||||||
|
try std.testing.expectEqual(@as(?u8, 8), queue.pop());
|
||||||
|
try std.testing.expectEqual(@as(?u8, null), queue.pop());
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user