Skip to content

Commit

Permalink
minor code cleanup, start with audio beeper
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Jul 31, 2024
1 parent d310449 commit 7009fc7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/chips/ay3891.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ pub const Model = enum {
AY38913, // no IO ports
};

pub const Config = struct {
pub const TypeConfig = struct {
model: Model = .AY38910,
pins: Pins,
bus: type,
dcadjust_buf_len: u32 = 128,
enable_lowpass_filter: bool = true,
};

pub fn Type(comptime cfg: Config) type {
pub fn Type(comptime cfg: TypeConfig) type {
return struct {
const Self = @This();
const Bus = cfg.bus;
Expand Down Expand Up @@ -177,7 +177,7 @@ pub fn Type(comptime cfg: Config) type {
period: i32 = 0,
counter: i32 = 0,
volume: f32 = 0.0,
value: f32 = 0.0,
out: f32 = 0.0, // output sample value
ready: bool = false, // true if a new sample value is ready
filter: filter.Type(.{
.enable_dcadjust = true,
Expand All @@ -186,7 +186,7 @@ pub fn Type(comptime cfg: Config) type {
}) = .{},
};

tick_count: u38 = 0, // tick counter for internal clock division
tick_count: u32 = 0, // tick counter for internal clock division
cs_mask: u8 = 0, // hi: 4-bit chip-select (options.chip_select << 4)
active: bool = false, // true if upper chip-select matches when writing address
addr: u4 = 0, // current register index
Expand Down Expand Up @@ -329,6 +329,7 @@ pub fn Type(comptime cfg: Config) type {
self.env.shape.state = env_shapes[self.regs[REG.ENV_SHAPE_CYCLE]][self.env.shape.counter];
}

// FIXME: add some oversampling for anti-aliasing(?)
// generate sample
self.sample.counter -= FIXEDPOINT_SCALE;
if (self.sample.counter <= 0) {
Expand All @@ -348,7 +349,7 @@ pub fn Type(comptime cfg: Config) type {
}
}
}
self.sample.value = self.sample.filter.put(sm) * self.sample.volume;
self.sample.out = self.sample.filter.put(sm) * self.sample.volume;
self.sample.ready = true;
} else {
self.sample.ready = false;
Expand Down
4 changes: 2 additions & 2 deletions src/chips/z80.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ const indirect_table = init: {
};
// zig fmt: on

pub const Config = struct {
pub const TypeConfig = struct {
pins: Pins,
bus: type,
};

pub fn Type(comptime cfg: Config) type {
pub fn Type(comptime cfg: TypeConfig) type {
const Bus = cfg.bus;
return struct {
const Self = @This();
Expand Down
4 changes: 2 additions & 2 deletions src/chips/z80ctc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub const DefaultPins = Pins{
.IEIO = 23,
};

pub const Config = struct { pins: Pins, bus: type };
pub const TypeConfig = struct { pins: Pins, bus: type };

pub fn Type(comptime cfg: Config) type {
pub fn Type(comptime cfg: TypeConfig) type {
assert(cfg.pins.CS[1] == cfg.pins.CS[0] + 1);
assert(cfg.pins.ZCTO[1] == cfg.pins.ZCTO[0] + 1);
assert(cfg.pins.ZCTO[2] == cfg.pins.ZCTO[1] + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/chips/z80irq.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub const Pins = struct {
IEIO: comptime_int,
};

pub const Config = struct {
pub const TypeConfig = struct {
pins: Pins,
bus: type,
};

pub fn Type(comptime cfg: Config) type {
pub fn Type(comptime cfg: TypeConfig) type {
const Bus = cfg.bus;
return struct {
const Self = @This();
Expand Down
4 changes: 2 additions & 2 deletions src/chips/z80pio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ pub const DefaultPins = Pins{
.IEIO = 36,
};

pub const Config = struct {
pub const TypeConfig = struct {
pins: Pins,
bus: type,
};

pub fn Type(cfg: Config) type {
pub fn Type(cfg: TypeConfig) type {
const Bus = cfg.bus;
const Z80IRQ = z80irq.Type(.{
.pins = .{
Expand Down
61 changes: 61 additions & 0 deletions src/common/beeper.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! a simple square wave beeper sound device
const filter = @import("filter.zig");

pub const TypeConfig = struct {
dcadjust_buf_len: u32 = 128,
enable_lowpass_filter: bool = true,
};

pub fn Type(comptime cfg: TypeConfig) type {
return struct {
const Self = @This();

pub const Options = struct {
tick_hz: u32,
sound_hz: u32,
base_volume: f32 = 1.0,
};

const OVERSAMPLE = 8; // anti-aliasing oversampling
const FIXEDPOINT_SCALE = 16; // error-reduction for sample period counter

state: u1 = 0,
period: i32 = 0,
counter: i32 = 0,
base_volume: f32 = 1.0,
volume: f32 = 1.0,
sample: struct {
ready: bool = false,
out: f32 = 0.0,
accum: f32 = 0.0, // oversampling accumulator for anti-aliasing
div: f32 = 0.0, // oversampling divider
},
filter: filter.Type(.{
.enable_dcadjust = true,
.enable_lowpass_filter = cfg.enable_lowpass_filter,
.dcadjust_buf_len = cfg.dcadjust_buf_len,
}),

pub fn init(opts: Options) Self {
_ = opts; // autofix
// FIXME
}

pub fn setVolume(self: *Self, v: f32) void {
self.volume = v;
}

pub fn toggle(self: *Self) void {
self.state ^= 1;
}

pub fn set(self: *Self, state: u1) void {
self.state = state;
}

pub fn tick(self: *Self) bool {
_ = self; // autofix
return false;
}
};
}
4 changes: 2 additions & 2 deletions src/common/filter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const std = @import("std");
const assert = std.debug.assert;
const isPowerOfTwo = std.math.isPowerOfTwo;

pub const Config = struct {
pub const TypeConfig = struct {
enable_dcadjust: bool,
enable_lowpass_filter: bool,
dcadjust_buf_len: u32,
};

pub fn Type(cfg: Config) type {
pub fn Type(cfg: TypeConfig) type {
assert(isPowerOfTwo(cfg.dcadjust_buf_len));
return struct {
const Self = @This();
Expand Down
4 changes: 2 additions & 2 deletions src/common/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub const Page = struct {
write: [*]u8,
};

pub const Config = struct {
pub const TypeConfig = struct {
page_size: comptime_int,
};

pub fn Type(comptime cfg: Config) type {
pub fn Type(comptime cfg: TypeConfig) type {
assert(std.math.isPowerOfTwo(cfg.page_size));

return struct {
Expand Down
2 changes: 1 addition & 1 deletion src/systems/bombjack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ pub const Bombjack = struct {
bus &= ~(PSG0.BDIR | PSG0.BC1 | PSG1.BDIR | PSG1.BC1 | PSG2.BDIR | PSG2.BC1);

if (board.psg0.sample.ready) {
const s = board.psg0.sample.value + board.psg1.sample.value + board.psg2.sample.value;
const s = board.psg0.sample.out + board.psg1.sample.out + board.psg2.sample.out;
self.audio.sample_buffer[self.audio.sample_pos] = s * self.audio.volume;
self.audio.sample_pos += 1;
if (self.audio.sample_pos == self.audio.num_samples) {
Expand Down

0 comments on commit 7009fc7

Please sign in to comment.