Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! A Sound/Speech Mockingboard: the first of Sweet Micro Systems' cards, an//! AY-3-8910 for sound and a Votrax SC-01 for speech, each behind its own 6522.//!//! It is the ancestor of the Mockingboard this project already models. The//! sound side is exactly one half of that card — a 6522 driving an AY the usual//! way, port A the data and port B the chip's BC1/BDIR/reset — so this reuses//! `mockingboard.Half` for it unchanged. The speech side is what is different,//! and different from the SSI 263A a later Mockingboard carries: the SC-01 is//! not memory-mapped, it hangs off the *second* 6522's port. The driver puts//! the phoneme in port B (P0–P5 in the low six bits, the two inflection bits in//! the top two), pulses CB2 as the strobe, and reads the chip's Acknowledge///! Request back on CB1, which raises the interrupt (Votrax International, 1980;//! the 6522 wiring is AppleWin's and MAME's account of the card).//!//! The sound is real; the voice is an approximation, for the reason `sc01.zig`//! sets out — the chip's own formant ROM was never published.
const std = @import("std");
const ay3 = @import("ay3.zig");const via = @import("via.zig");const sc01 = @import("sc01.zig");const mockingboard = @import("mockingboard.zig");
/// The write log is the Mockingboard's, which already carries the AY writes and/// now the SC-01 phonemes too (its `votrax` entries).pub const Log = mockingboard.Log;
/// Where the two 6522s answer, the same places a Mockingboard's do: the sound/// 6522 at the bottom of the slot page, the speech 6522 half way up.pub const sound_via = 0x00;pub const speech_via = 0x80;
pub const Card = struct { const Self = @This();
fitted: bool = false,
/// The sound side: a 6522 and an AY-3-8910, exactly the sound half of a /// Mockingboard. sound: mockingboard.Half = .{ .which = 0 },
/// The speech side: the second 6522, and the SC-01 hanging off it. via: via.Via = .{}, chip: sc01.Chip = .{},
/// The last CB2 level seen, so the strobe's falling edge can be caught, and /// the last A/R level fed to CB1, so only a change is handed on. strobe_line: bool = false, ar_line: bool = false,
/// The reset line reaches both 6522s and quiets the speech chip; the 6522s /// keep their timer state, the same as a Mockingboard. pub fn reset(self: *Self, cycle: u64) void { self.sound.via.reset(cycle); self.via.reset(cycle); self.chip.reset(); self.strobe_line = false; self.ar_line = false; }
/// Whether either 6522 is pulling the interrupt line. Asked on every access, /// so a slot with no card in it answers false inline. pub inline fn interrupting(self: *Self, cycle: u64) bool { if (!self.fitted) return false; self.updateArLine(cycle); self.sound.via.catchUp(cycle); self.via.catchUp(cycle); return self.sound.via.interrupting() or self.via.interrupting(); }
/// Feeds the SC-01's A/R line to the speech 6522's CB1, handing over only a /// change so the edge detector sees an edge. A/R high is the chip ready for /// the next phoneme; the rise as a phoneme finishes is the interrupt. fn updateArLine(self: *Self, cycle: u64) void { const high = self.chip.ready(cycle); if (high != self.ar_line) { self.ar_line = high; self.via.setLine(.cb1, high); } }
/// What the slot's page answers. The sound 6522 is the low sixteen /// registers, the speech 6522 the sixteen half way up, and everything /// between is the first again, as the card's decoding leaves it. pub fn access(self: *Self, offset: u8, is_write: bool, value: u8, log: anytype, cycle: u64) u8 { if (offset >= speech_via) { const register: via.Register = @enumFromInt(@as(u4, @truncate(offset))); if (!is_write) { self.updateArLine(cycle); return self.via.read(register, cycle); } self.via.write(register, value, cycle); self.speak(log, cycle); self.updateArLine(cycle); return 0; }
const register: via.Register = @enumFromInt(@as(u4, @truncate(offset))); if (!is_write) return self.sound.via.read(register, cycle); self.sound.via.write(register, value, cycle); if (register == .orb or register == .ddrb) self.sound.acted(log, cycle); return 0; }
/// A write to the speech 6522 may have moved CB2, the SC-01's strobe. Its /// falling edge loads the phoneme byte now on port B — the low six bits the /// phoneme, the top two the inflection — and starts it. fn speak(self: *Self, log: anytype, cycle: u64) void { const cb2 = self.via.lines.get(.cb2); if (self.strobe_line and !cb2) { const byte = self.via.port[1]; const phoneme: u6 = @truncate(byte); const inflection: u2 = @truncate(byte >> 6); self.chip.strobe(phoneme, inflection, cycle); log.recordVotrax(cycle, phoneme, inflection); } self.strobe_line = cb2; }};
/// Turning the log into sound: one AY chip and the SC-01's voice, mixed to one/// channel, since the card has one of each.pub const Renderer = struct { chip: ay3.Chip = .{}, speech: sc01.Chip = .{}, voice: sc01.Voice,
at: f64 = 0, cycles_per_sample: f64, owed: f64 = 0,
pub fn init(cycles_per_second: f64, sample_rate: f64, start_cycle: u64) Renderer { return .{ .at = @floatFromInt(start_cycle), .cycles_per_sample = cycles_per_second / sample_rate, .voice = .init(@floatCast(sample_rate)), }; }
pub fn samplesDue(self: *const Renderer, until: u64) usize { const end: f64 = @floatFromInt(until); if (end <= self.at) return 0; return @intFromFloat((end - self.at) / self.cycles_per_sample); }
pub const Rendered = struct { samples: usize, entries: usize };
/// Fills `out`, left then right — the same mono mix in both, since the card /// has one AY and one voice. pub fn render(self: *Renderer, entries: anytype, until: u64, out: []f32) Rendered { var written: usize = 0; var used: usize = 0; const frames = out.len / 2; const end: f64 = @floatFromInt(until);
while (written < frames) { const next = self.at + self.cycles_per_sample; if (next > end) break;
while (used < entries.len and @as(f64, @floatFromInt(entries[used].cycle)) <= next) : (used += 1) { const entry = entries[used]; if (entry.votrax) { self.speech.strobe(@truncate(entry.value), @truncate(entry.register), entry.cycle); } else if (entry.reset) { self.chip.reset(); } else if (!entry.speech) { self.chip.write(entry.register, entry.value); } }
// Bring the speech chip up to this sample so a phoneme ends where it // should, and take a sample of its voice. _ = self.speech.ready(@intFromFloat(next)); const speaking = self.voice.sample(&self.speech);
// Step the AY through the sample and average what it put out. var sum: f32 = 0; var ticks: usize = 0; self.owed += self.cycles_per_sample; while (self.owed >= ay3.tick_divisor) : (self.owed -= ay3.tick_divisor) { self.chip.step(); sum += self.chip.level(); ticks += 1; } const tone = if (ticks > 0) sum / @as(f32, @floatFromInt(ticks)) else self.chip.level();
const mix = tone + speaking; out[written * 2] = mix; out[written * 2 + 1] = mix; written += 1; self.at = next; }
return .{ .samples = written * 2, .entries = used }; }};
// -- tests ------------------------------------------------------------------
const testing = std.testing;
const Watched = struct { card: Card = .{ .fitted = true }, log: Log(true) = .{},
fn access(self: *Watched, offset: u8, is_write: bool, value: u8, cycle: u64) u8 { return self.card.access(offset, is_write, value, &self.log, cycle); }};
test "a phoneme strobed through the speech 6522's CB2 reaches the SC-01" { var card: Watched = .{};
// Set CB2 to a driven high by the peripheral control register (manual // output mode, high), then put the phoneme byte on port B as an output. _ = card.access(speech_via + @as(u8, @intFromEnum(via.Register.pcr)), true, 0xe0, 0); // CB2 = high (manual output) _ = card.access(speech_via + @as(u8, @intFromEnum(via.Register.ddrb)), true, 0xff, 0); // port B all outputs _ = card.access(speech_via + @as(u8, @intFromEnum(via.Register.orb)), true, 0b10_101100, 0); // inflection 2, phoneme 0x2c ("E")
// The strobe is CB2's falling edge: drive it low. _ = card.access(speech_via + @as(u8, @intFromEnum(via.Register.pcr)), true, 0xc0, 10); // CB2 = low (the strobe's falling edge)
try testing.expectEqual(@as(u6, 0x2c), card.card.chip.phoneme); try testing.expectEqual(@as(u2, 0b10), card.card.chip.inflection); // It is busy for the phoneme's duration, then ready again. try testing.expect(!card.card.chip.ready(11)); try testing.expect(card.card.chip.ready(sc01.Chip.phonemeCycles(0x2c) + 10));
// And the write was logged as a Votrax phoneme for the voice to replay. const written = card.log.taken(); try testing.expectEqual(@as(usize, 1), written.len); try testing.expect(written[0].votrax); try testing.expectEqual(@as(u8, 0x2c), written[0].value);}
test "the sound side is an ordinary Mockingboard AY" { var card: Watched = .{}; // Port A all outputs, and the three port-B control lines. _ = card.access(sound_via + 3, true, 0xff, 0); _ = card.access(sound_via + 2, true, 0x07, 0); // Latch the A-fine register (0), then write it: address then the value, // each with a latch/inactive pair on port B, the way a player does. _ = card.access(sound_via + 1, true, @intFromEnum(ay3.Register.a_fine), 0); _ = card.access(sound_via + 0, true, mockingboard.bus_latch, 0); _ = card.access(sound_via + 0, true, mockingboard.bus_inactive, 0); _ = card.access(sound_via + 1, true, 0x42, 0); _ = card.access(sound_via + 0, true, mockingboard.bus_write, 0); _ = card.access(sound_via + 0, true, mockingboard.bus_inactive, 0); try testing.expectEqual(@as(u16, 0x42), card.card.sound.chip.period(0));}