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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Votrax SC-01, the phoneme speech chip on the first Mockingboard's//! "Sound/Speech" side — the voice a generation of Apple II software and arcade//! games spoke with before the SSI 263A.//!//! It is a much simpler interface than the SSI 263A's: no register bus at all.//! A driver puts a **6-bit phoneme code** on P0–P5 and a **2-bit inflection**//! (gross pitch) on I1–I2, and pulses a **strobe** (STB) to load it; the chip//! works through the phoneme and drops its **Acknowledge/Request** (A/R) line//! while it does, raising it again when it is ready for the next. That//! handshake is the whole of the protocol (Votrax International, 1980, "Mode 2 —//! Phoneme Mode", p. 14): put the phoneme up, wait for A/R high, strobe it in,//! and A/R goes low until the phoneme is done.//!//! This is the chip and the timing, not the voice. The phoneme set, the sixty-//! four codes and their nominal durations (Table 1), and the strobe/acknowledge//! handshake are pinned down exactly. What the data sheet does **not** give is//! the chip's phoneme-to-formant detail — that lived in an on-chip ROM Votrax//! never published — so the `Voice` below is an approximation the same way the//! SSI 263A's is: speech-shaped, and legible as an attempt at the phoneme,//! rather than the SC-01's own voice.//!//! On the Mockingboard the SC-01 hangs off the second 6522: the phoneme byte is//! that 6522's port B (P0–P5 in the low six bits, the inflection in bits 6–7),//! the strobe is its CB2 line, and A/R comes back on CB1 to raise the interrupt//! — see `soundspeech.zig`, which wires it up.
const std = @import("std");
/// One phoneme: its symbol, and the nominal duration the data sheet gives it in/// milliseconds (Votrax International, 1980, Table 1). Indexed by the 6-bit/// phoneme code, P0 the least significant bit.pub const Phoneme = struct { symbol: []const u8, ms: u16 };
pub const phonemes = [64]Phoneme{ .{ .symbol = "EH3", .ms = 59 }, // 0x00 jacket .{ .symbol = "EH2", .ms = 71 }, // 0x01 enlist .{ .symbol = "EH1", .ms = 121 }, // 0x02 heavy .{ .symbol = "PA0", .ms = 47 }, // 0x03 no sound .{ .symbol = "DT", .ms = 47 }, // 0x04 butter .{ .symbol = "A2", .ms = 71 }, // 0x05 made .{ .symbol = "A1", .ms = 103 }, // 0x06 made .{ .symbol = "ZH", .ms = 90 }, // 0x07 azure .{ .symbol = "AH2", .ms = 71 }, // 0x08 honest .{ .symbol = "I3", .ms = 55 }, // 0x09 inhibit .{ .symbol = "I2", .ms = 80 }, // 0x0A inhibit .{ .symbol = "I1", .ms = 121 }, // 0x0B inhibit .{ .symbol = "M", .ms = 103 }, // 0x0C mat .{ .symbol = "N", .ms = 80 }, // 0x0D sun .{ .symbol = "B", .ms = 71 }, // 0x0E bag .{ .symbol = "V", .ms = 71 }, // 0x0F van .{ .symbol = "CH", .ms = 71 }, // 0x10 chip (T must precede) .{ .symbol = "SH", .ms = 121 }, // 0x11 shop .{ .symbol = "Z", .ms = 71 }, // 0x12 zoo .{ .symbol = "AW1", .ms = 146 }, // 0x13 lawful .{ .symbol = "NG", .ms = 121 }, // 0x14 thing .{ .symbol = "AH1", .ms = 146 }, // 0x15 father .{ .symbol = "OO1", .ms = 103 }, // 0x16 looking .{ .symbol = "OO", .ms = 185 }, // 0x17 book .{ .symbol = "L", .ms = 103 }, // 0x18 land .{ .symbol = "K", .ms = 80 }, // 0x19 trick .{ .symbol = "J", .ms = 47 }, // 0x1A judge (D must precede) .{ .symbol = "H", .ms = 71 }, // 0x1B hello .{ .symbol = "G", .ms = 71 }, // 0x1C get .{ .symbol = "F", .ms = 103 }, // 0x1D fast .{ .symbol = "D", .ms = 55 }, // 0x1E paid .{ .symbol = "S", .ms = 90 }, // 0x1F pass .{ .symbol = "A", .ms = 185 }, // 0x20 day .{ .symbol = "AY", .ms = 65 }, // 0x21 day .{ .symbol = "Y1", .ms = 80 }, // 0x22 yard .{ .symbol = "UH3", .ms = 47 }, // 0x23 mission .{ .symbol = "AH", .ms = 250 }, // 0x24 mop .{ .symbol = "P", .ms = 103 }, // 0x25 past .{ .symbol = "O", .ms = 185 }, // 0x26 cold .{ .symbol = "I", .ms = 185 }, // 0x27 pin .{ .symbol = "U", .ms = 185 }, // 0x28 move .{ .symbol = "Y", .ms = 103 }, // 0x29 any .{ .symbol = "T", .ms = 71 }, // 0x2A tap .{ .symbol = "R", .ms = 90 }, // 0x2B red .{ .symbol = "E", .ms = 185 }, // 0x2C meet .{ .symbol = "W", .ms = 80 }, // 0x2D win .{ .symbol = "AE", .ms = 185 }, // 0x2E dad .{ .symbol = "AE1", .ms = 103 }, // 0x2F after .{ .symbol = "AW2", .ms = 90 }, // 0x30 salty .{ .symbol = "UH2", .ms = 71 }, // 0x31 about .{ .symbol = "UH1", .ms = 103 }, // 0x32 uncle .{ .symbol = "UH", .ms = 185 }, // 0x33 cup .{ .symbol = "O2", .ms = 80 }, // 0x34 for .{ .symbol = "O1", .ms = 121 }, // 0x35 aboard .{ .symbol = "IU", .ms = 59 }, // 0x36 you .{ .symbol = "U1", .ms = 90 }, // 0x37 you .{ .symbol = "THV", .ms = 80 }, // 0x38 the .{ .symbol = "TH", .ms = 71 }, // 0x39 thin .{ .symbol = "ER", .ms = 146 }, // 0x3A bird .{ .symbol = "EH", .ms = 185 }, // 0x3B get .{ .symbol = "E1", .ms = 121 }, // 0x3C be .{ .symbol = "AW", .ms = 250 }, // 0x3D call .{ .symbol = "PA1", .ms = 185 }, // 0x3E no sound .{ .symbol = "STOP", .ms = 47 }, // 0x3F no sound};
/// The SC-01 has its own master clock; the nominal durations above hold at it,/// so they are turned into machine cycles against a nominal 1.02 MHz — the/// scale the rest of the machine's cycle counts are on — rather than the CPU's/// exact rate, which the chip does not share.const nominal_hz: u64 = 1_020_484;
pub const Chip = struct { /// The phoneme now loaded, and the two-bit inflection (gross pitch). phoneme: u6 = 0x3f, // STOP: silent, which is where a cold chip rests inflection: u2 = 0,
/// The machine cycle the chip has been brought up to, and the cycle the /// phoneme now being spoken finishes on; speaking while `finish_at` is /// ahead of `now`. now: u64 = 0, finish_at: u64 = 0, producing: bool = false,
/// The slot's RES line quiets the voice; it does not wipe the loaded /// phoneme. pub fn reset(self: *Chip) void { self.producing = false; }
/// How long a phoneme lasts, in machine cycles. pub fn phonemeCycles(phoneme: u6) u64 { return @as(u64, phonemes[phoneme].ms) * nominal_hz / 1000; }
/// The strobe (STB): loads the phoneme on P0–P5 and the inflection, and /// starts it. A/R drops (busy) here and rises again when it finishes. On /// the Mockingboard this is CB2's falling edge. pub fn strobe(self: *Chip, phoneme: u6, inflection: u2, cycle: u64) void { self.catchUp(cycle); self.phoneme = phoneme; self.inflection = inflection; self.producing = true; self.finish_at = self.now + phonemeCycles(phoneme); }
/// Whether A/R is high, which the data sheet's Mode 2 has meaning "ready to /// receive a phoneme" — the chip is idle or has finished the last one. The /// card wires this to the 6522's CB1, and a driver waits on it before it /// strobes the next phoneme. Brings the timing up to date first. pub fn ready(self: *Chip, cycle: u64) bool { self.catchUp(cycle); return !self.producing; }
fn catchUp(self: *Chip, cycle: u64) void { if (cycle > self.now) self.now = cycle; if (self.producing and self.now >= self.finish_at) self.producing = false; }};
// -- the voice, an approximation --------------------------------------------
/// A phoneme's rough sound: the two formant frequencies that place a voiced/// phoneme, whether it is voiced (a pitched buzz) or unvoiced (noise), and/// whether it is a pause. **Not from the data sheet** — the SC-01's own/// phoneme-to-formant ROM was never published — but standard English formant/// values by the class each phoneme's example word puts it in, the same honest/// approximation `ssi263.zig` makes.const Formant = struct { f1: f32, f2: f32, voiced: bool = true, silent: bool = false };
const formants = [64]Formant{ .{ .f1 = 530, .f2 = 1840 }, // 00 EH3 .{ .f1 = 530, .f2 = 1840 }, // 01 EH2 .{ .f1 = 530, .f2 = 1840 }, // 02 EH1 .{ .f1 = 0, .f2 = 0, .silent = true }, // 03 PA0 .{ .f1 = 500, .f2 = 1700, .voiced = false }, // 04 DT .{ .f1 = 500, .f2 = 2000 }, // 05 A2 .{ .f1 = 500, .f2 = 2000 }, // 06 A1 .{ .f1 = 300, .f2 = 1800 }, // 07 ZH .{ .f1 = 730, .f2 = 1100 }, // 08 AH2 .{ .f1 = 400, .f2 = 2000 }, // 09 I3 .{ .f1 = 400, .f2 = 2000 }, // 0A I2 .{ .f1 = 400, .f2 = 2000 }, // 0B I1 .{ .f1 = 300, .f2 = 1000 }, // 0C M .{ .f1 = 300, .f2 = 1000 }, // 0D N .{ .f1 = 300, .f2 = 1200 }, // 0E B .{ .f1 = 300, .f2 = 1400 }, // 0F V .{ .f1 = 400, .f2 = 2500, .voiced = false }, // 10 CH .{ .f1 = 400, .f2 = 4000, .voiced = false }, // 11 SH .{ .f1 = 300, .f2 = 1400 }, // 12 Z .{ .f1 = 570, .f2 = 840 }, // 13 AW1 .{ .f1 = 300, .f2 = 1000 }, // 14 NG .{ .f1 = 730, .f2 = 1100 }, // 15 AH1 .{ .f1 = 300, .f2 = 870 }, // 16 OO1 .{ .f1 = 300, .f2 = 870 }, // 17 OO .{ .f1 = 360, .f2 = 1300 }, // 18 L .{ .f1 = 500, .f2 = 1700, .voiced = false }, // 19 K .{ .f1 = 300, .f2 = 1800 }, // 1A J .{ .f1 = 500, .f2 = 1500, .voiced = false }, // 1B H .{ .f1 = 300, .f2 = 1200 }, // 1C G .{ .f1 = 400, .f2 = 2400, .voiced = false }, // 1D F .{ .f1 = 300, .f2 = 1200 }, // 1E D .{ .f1 = 400, .f2 = 4000, .voiced = false }, // 1F S .{ .f1 = 500, .f2 = 2000 }, // 20 A .{ .f1 = 500, .f2 = 2000 }, // 21 AY .{ .f1 = 300, .f2 = 2200 }, // 22 Y1 .{ .f1 = 350, .f2 = 1250 }, // 23 UH3 .{ .f1 = 730, .f2 = 1100 }, // 24 AH .{ .f1 = 500, .f2 = 1700, .voiced = false }, // 25 P .{ .f1 = 360, .f2 = 640 }, // 26 O .{ .f1 = 400, .f2 = 2000 }, // 27 I .{ .f1 = 350, .f2 = 1250 }, // 28 U .{ .f1 = 300, .f2 = 2200 }, // 29 Y .{ .f1 = 500, .f2 = 1700, .voiced = false }, // 2A T .{ .f1 = 350, .f2 = 1300 }, // 2B R .{ .f1 = 270, .f2 = 2300 }, // 2C E .{ .f1 = 300, .f2 = 610 }, // 2D W .{ .f1 = 660, .f2 = 1700 }, // 2E AE .{ .f1 = 660, .f2 = 1700 }, // 2F AE1 .{ .f1 = 570, .f2 = 840 }, // 30 AW2 .{ .f1 = 350, .f2 = 1250 }, // 31 UH2 .{ .f1 = 350, .f2 = 1250 }, // 32 UH1 .{ .f1 = 600, .f2 = 1200 }, // 33 UH .{ .f1 = 360, .f2 = 640 }, // 34 O2 .{ .f1 = 360, .f2 = 640 }, // 35 O1 .{ .f1 = 350, .f2 = 1250 }, // 36 IU .{ .f1 = 350, .f2 = 1250 }, // 37 U1 .{ .f1 = 300, .f2 = 1400 }, // 38 THV .{ .f1 = 400, .f2 = 2400, .voiced = false }, // 39 TH .{ .f1 = 490, .f2 = 1350 }, // 3A ER .{ .f1 = 530, .f2 = 1840 }, // 3B EH .{ .f1 = 270, .f2 = 2300 }, // 3C E1 .{ .f1 = 570, .f2 = 840 }, // 3D AW .{ .f1 = 0, .f2 = 0, .silent = true }, // 3E PA1 .{ .f1 = 0, .f2 = 0, .silent = true }, // 3F STOP};
/// The four gross pitch levels the two inflection bits pick, in hertz.const pitches = [4]f32{ 88, 104, 120, 140 };
/// A two-pole resonator, one formant of the little synthesizer below — the same/// shape `ssi263.zig` uses.const Resonator = struct { y1: f32 = 0, y2: f32 = 0,
fn run(self: *Resonator, x: f32, freq: f32, rate: f32) f32 { const r = 0.97; const theta = 2.0 * std.math.pi * freq / rate; const y = x * (1.0 - r) + 2.0 * r * @cos(theta) * self.y1 - r * r * self.y2; self.y2 = self.y1; self.y1 = y; return y; }};
/// The SC-01's voice, an approximation. **Nothing here is from the data/// sheet.** A glottal buzz at the inflection pitch, or a hiss, through two/// resonators placed by the phoneme's class: speech-shaped, and the honest best/// that can be done without the chip's own formant ROM.pub const Voice = struct { phase: f32 = 0, noise: u32 = 0x1357_9bdf, resonators: [2]Resonator = .{ .{}, .{} }, sample_rate: f32,
pub fn init(sample_rate: f32) Voice { return .{ .sample_rate = sample_rate }; }
/// One sample of the voice, given the chip's current state. Zero unless the /// chip is speaking; the resonators are still run so a note does not click. pub fn sample(self: *Voice, chip: *const Chip) f32 { const shape = formants[chip.phoneme]; const on = chip.producing and !shape.silent;
var excitation: f32 = 0; if (on) { if (shape.voiced) { const pitch = pitches[chip.inflection]; self.phase += pitch / self.sample_rate; if (self.phase >= 1.0) self.phase -= 1.0; excitation = 2.0 * self.phase - 1.0; // a sawtooth, rich in harmonics } else { self.noise = self.noise *% 1664525 +% 1013904223; excitation = @as(f32, @floatFromInt(self.noise >> 9)) / 8_388_608.0 - 1.0; } }
const out = self.resonators[0].run(excitation, shape.f1, self.sample_rate) + 0.7 * self.resonators[1].run(excitation, shape.f2, self.sample_rate); return out * 0.5; }};
const testing = std.testing;
test "a strobed phoneme is spoken for its nominal duration, then A/R goes ready" { var chip: Chip = .{}; // A cold chip rests on STOP and is ready. try testing.expect(chip.ready(0));
// Strobe "AH" (0x24, 250 ms): the chip is busy — A/R low — for the whole // of its nominal duration and ready again after. chip.strobe(0x24, 0, 0); try testing.expect(!chip.ready(0)); const lasts = Chip.phonemeCycles(0x24); try testing.expect(!chip.ready(lasts - 1)); try testing.expect(chip.ready(lasts)); // 250 ms at the nominal clock. try testing.expectEqual(@as(u64, 250 * nominal_hz / 1000), lasts);}
test "the phoneme and inflection are taken low six bits, high two" { var chip: Chip = .{}; // A driver writes the byte P5..P0 in the low six and the inflection in the // top two; the card splits it, but the chip is handed the pieces. chip.strobe(0x2c, 0b10, 100); // "E" at inflection 2 try testing.expectEqual(@as(u6, 0x2c), chip.phoneme); try testing.expectEqual(@as(u2, 0b10), chip.inflection); try testing.expectEqualStrings("E", phonemes[chip.phoneme].symbol);}
test "every phoneme code has a symbol and a formant" { for (phonemes) |p| try testing.expect(p.symbol.len > 0); // The three pauses are the only silent ones. var silent: usize = 0; for (formants) |f| if (f.silent) { silent += 1; }; try testing.expectEqual(@as(usize, 3), silent); // PA0, PA1, STOP}