zba/src/apu.zig

263 lines
6.5 KiB
Zig
Raw Normal View History

2022-03-15 11:08:07 +00:00
const std = @import("std");
const SDL = @import("sdl2");
const io = @import("bus/io.zig");
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
2022-03-15 11:08:07 +00:00
const SoundFifo = std.fifo.LinearFifo(u8, .{ .Static = 0x20 });
const AudioDeviceId = SDL.SDL_AudioDeviceID;
2022-03-15 11:08:07 +00:00
const intToBytes = @import("util.zig").intToBytes;
const log = std.log.scoped(.APU);
2022-03-15 11:08:07 +00:00
pub const Apu = struct {
const Self = @This();
ch1: ToneSweep,
ch2: Tone,
ch3: Wave,
ch4: Noise,
chA: DmaSound(.A),
chB: DmaSound(.B),
2022-03-15 11:08:07 +00:00
bias: io.SoundBias,
ch_vol_cnt: io.ChannelVolumeControl,
dma_cnt: io.DmaSoundControl,
cnt: io.SoundControl,
dev: AudioDeviceId,
pub fn init(dev: AudioDeviceId) Self {
2022-03-15 11:08:07 +00:00
return .{
.ch1 = ToneSweep.init(),
.ch2 = Tone.init(),
.ch3 = Wave.init(),
.ch4 = Noise.init(),
.chA = DmaSound(.A).init(),
.chB = DmaSound(.B).init(),
2022-03-15 11:08:07 +00:00
.ch_vol_cnt = .{ .raw = 0 },
.dma_cnt = .{ .raw = 0 },
.cnt = .{ .raw = 0 },
.bias = .{ .raw = 0x0200 },
.dev = dev,
2022-03-15 11:08:07 +00:00
};
}
pub fn setDmaCnt(self: *Self, value: u16) void {
const new: io.DmaSoundControl = .{ .raw = value };
// Reinitializing instead of resetting is fine because
// the FIFOs I'm using are stack allocated and 0x20 bytes big
if (new.sa_reset.read()) self.chA.fifo = SoundFifo.init();
if (new.sb_reset.read()) self.chB.fifo = SoundFifo.init();
self.dma_cnt = new;
}
2022-03-15 11:08:07 +00:00
pub fn setSoundCntX(self: *Self, value: bool) void {
self.cnt.apu_enable.write(value);
}
pub fn setSoundCntLLow(self: *Self, byte: u8) void {
self.ch_vol_cnt.raw = (self.ch_vol_cnt.raw & 0xFF00) | byte;
}
2022-03-28 22:40:47 +00:00
pub fn setSoundCntLHigh(self: *Self, byte: u8) void {
self.ch_vol_cnt.raw = @as(u16, byte) << 8 | (self.ch_vol_cnt.raw & 0xFF);
}
2022-03-15 11:08:07 +00:00
pub fn setBiasHigh(self: *Self, byte: u8) void {
self.bias.raw = (@as(u16, byte) << 8) | (self.bias.raw & 0xFF);
}
pub fn handleTimerOverflow(self: *Self, kind: DmaSoundKind, cpu: *Arm7tdmi) void {
if (!self.cnt.apu_enable.read()) return;
const samples = switch (kind) {
.A => blk: {
break :blk self.chA.handleTimerOverflow(cpu, self.dma_cnt);
},
.B => blk: {
break :blk self.chB.handleTimerOverflow(cpu, self.dma_cnt);
},
};
_ = SDL.SDL_QueueAudio(self.dev, &samples, 2);
}
2022-03-15 11:08:07 +00:00
};
const ToneSweep = struct {
const Self = @This();
2022-03-28 22:40:47 +00:00
/// NR10
2022-03-15 11:08:07 +00:00
sweep: io.Sweep,
2022-03-28 22:40:47 +00:00
/// NR11
2022-03-15 11:08:07 +00:00
duty: io.Duty,
2022-03-28 22:40:47 +00:00
/// NR12
2022-03-15 11:08:07 +00:00
envelope: io.Envelope,
2022-03-28 22:40:47 +00:00
/// NR13, NR14
2022-03-15 11:08:07 +00:00
freq: io.Frequency,
fn init() Self {
return .{
.sweep = .{ .raw = 0 },
.duty = .{ .raw = 0 },
.envelope = .{ .raw = 0 },
.freq = .{ .raw = 0 },
};
}
2022-03-28 22:40:47 +00:00
pub fn setFreqLow(self: *Self, byte: u8) void {
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
}
2022-03-15 11:08:07 +00:00
pub fn setFreqHigh(self: *Self, byte: u8) void {
self.freq.raw = (@as(u16, byte) << 8) | (self.freq.raw & 0xFF);
}
};
const Tone = struct {
const Self = @This();
2022-03-28 22:40:47 +00:00
/// NR21
2022-03-15 11:08:07 +00:00
duty: io.Duty,
2022-03-28 22:40:47 +00:00
/// NR22
2022-03-15 11:08:07 +00:00
envelope: io.Envelope,
2022-03-28 22:40:47 +00:00
/// NR23, NR24
2022-03-15 11:08:07 +00:00
freq: io.Frequency,
fn init() Self {
return .{
.duty = .{ .raw = 0 },
.envelope = .{ .raw = 0 },
.freq = .{ .raw = 0 },
};
}
2022-03-28 22:40:47 +00:00
pub fn setFreqLow(self: *Self, byte: u8) void {
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
}
2022-03-15 11:08:07 +00:00
pub fn setFreqHigh(self: *Self, byte: u8) void {
2022-03-28 22:40:47 +00:00
self.freq.raw = @as(u16, byte) << 8 | (self.freq.raw & 0xFF);
2022-03-15 11:08:07 +00:00
}
};
const Wave = struct {
const Self = @This();
/// Write-only
2022-03-28 22:40:47 +00:00
/// NR30
2022-03-15 11:08:07 +00:00
select: io.WaveSelect,
2022-04-08 05:13:58 +00:00
/// NR31
2022-03-15 11:08:07 +00:00
length: u8,
2022-03-28 22:40:47 +00:00
/// NR32
2022-03-15 11:08:07 +00:00
vol: io.WaveVolume,
2022-03-28 22:40:47 +00:00
/// NR33, NR34
2022-03-15 11:08:07 +00:00
freq: io.Frequency,
fn init() Self {
return .{
.select = .{ .raw = 0 },
.vol = .{ .raw = 0 },
.freq = .{ .raw = 0 },
.length = 0,
};
}
2022-03-28 22:40:47 +00:00
pub fn setFreqLow(self: *Self, byte: u8) void {
self.freq.raw = (self.freq.raw & 0xFF00) | byte;
}
pub fn setFreqHigh(self: *Self, byte: u8) void {
self.freq.raw = @as(u16, byte) << 8 | (self.freq.raw & 0xFF);
}
2022-03-15 11:08:07 +00:00
};
const Noise = struct {
const Self = @This();
/// Write-only
/// NR41
len: u6,
2022-03-28 22:40:47 +00:00
/// NR42
2022-03-15 11:08:07 +00:00
envelope: io.Envelope,
2022-03-28 22:40:47 +00:00
/// NR43
2022-03-15 11:08:07 +00:00
poly: io.PolyCounter,
2022-03-28 22:40:47 +00:00
/// NR44
2022-03-15 11:08:07 +00:00
cnt: io.NoiseControl,
fn init() Self {
return .{
.len = 0,
.envelope = .{ .raw = 0 },
.poly = .{ .raw = 0 },
.cnt = .{ .raw = 0 },
};
}
};
pub fn DmaSound(comptime kind: DmaSoundKind) type {
return struct {
const Self = @This();
2022-03-15 11:08:07 +00:00
fifo: SoundFifo,
kind: DmaSoundKind,
fn init() Self {
return .{ .fifo = SoundFifo.init(), .kind = kind };
}
pub fn push(self: *Self, value: u32) void {
self.fifo.write(&intToBytes(u32, value)) catch {};
}
pub fn pop(self: *Self) u8 {
return self.fifo.readItem() orelse 0;
}
pub fn len(self: *const Self) usize {
return self.fifo.readableLength();
}
pub fn handleTimerOverflow(self: *Self, cpu: *Arm7tdmi, cnt: io.DmaSoundControl) [2]u8 {
const sample = self.pop();
var left: u8 = 0;
var right: u8 = 0;
var fifo_addr: u32 = undefined;
switch (kind) {
.A => {
const vol = @boolToInt(!cnt.sa_vol.read()); // if unset, vol is 50%
if (cnt.sa_left_enable.read()) left = sample >> vol;
if (cnt.sa_right_enable.read()) right = sample >> vol;
fifo_addr = 0x0400_00A0;
},
.B => {
const vol = @boolToInt(!cnt.sb_vol.read()); // if unset, vol is 50%
if (cnt.sb_left_enable.read()) left = sample >> vol;
if (cnt.sb_right_enable.read()) right = sample >> vol;
fifo_addr = 0x0400_00A4;
},
}
if (self.len() <= 15) {
cpu.bus.dma._1.enableSoundDma(fifo_addr);
cpu.bus.dma._2.enableSoundDma(fifo_addr);
}
return .{ left, right };
}
};
}
const DmaSoundKind = enum {
A,
B,
2022-03-15 11:08:07 +00:00
};