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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! A Mockingboard: two sound chips, each with a 6522 in front of it, and the//! few wires between them.//!//! The card is two of everything because the chips are one voice short of a//! stereo pair: three squares each, one card, six voices, left and right. The//! machine cannot reach a sound chip directly, so each one hangs off a 6522's//! two ports — the data on port A, and three lines on port B saying whether//! what is on port A is a register number, something to write into it, or//! nothing at all. A program writing a register therefore writes port A, then//! port B twice, and that is what the sequences in every Mockingboard player//! are doing.//!//! The other half of the card is the timers. A tune is a list of what the//! registers should be, a frame at a time; the player sets a timer to the//! length of a frame and lets it interrupt, and the handler writes the next//! frame's worth. Without the timers the card would need the program's//! attention constantly, which is exactly what the speaker needs and what a//! sound card is for not needing.//!//! Unlike the disks and the filing systems, none of this has been checked//! against real software: what it is checked against is the published//! description of the parts and the sound that comes out of them.
const std = @import("std");const testing = std.testing;
const ay3 = @import("ay3.zig");const via = @import("via.zig");const ssi263 = @import("ssi263.zig");
/// Where the SSI 263A speech chip answers, when one is fitted: with A6 set and/// A7 clear, so card offsets $40–$7F, its three register-select lines the low/// three address bits (RS0–RS2 ← A0–A2). The canonical address is $Cn40. The/// chip select comes from A6 and the slot's I/O SELECT, and the register from/// A0–A2, so it mirrors every eight bytes across the range (Arnold, 2010).pub const speech_low = 0x40;pub const speech_high = 0x80;
/// Where the two halves answer. Unlike most cards these are not at $c080: a/// Mockingboard puts its chips in the slot's own ROM page, sixteen registers/// each, one at the bottom and one half way up.pub const first_via = 0x00;pub const second_via = 0x80;
/// What the three lines on port B mean. The low two are the chip's own BC1 and/// BDIR; the third is its reset, which is low to reset.pub const bus_reset = 0x04;pub const bus_inactive = 0x04;pub const bus_read = 0x05;pub const bus_write = 0x06;pub const bus_latch = 0x07;
/// How many register writes a log holds before it starts dropping them. A/// player writes a dozen or so a frame, so this is seconds of room.pub const capacity = 4096;
/// One half of the card: a 6522, a sound chip, and the register number the/// last address latch left behind.pub const Half = struct { via: via.Via = .{}, chip: ay3.Chip = .{}, /// Which register the chip is pointed at, or nothing when the address /// latch holds something that names no register. /// /// The AY's address register takes the whole byte off the bus, and only /// the low four bits of it name one of the fourteen registers; the top /// four are compared against the chip's own select pattern, which on a /// Mockingboard's is all zeroes. Latch anything with a bit set up there /// and the chip stops answering until it is given an address it /// recognises. A reset leaves it in that state too, which is what /// mb-audit's TestAYLatchAddress checks: reset the chip, write without /// latching an address first, and nothing at all should have been /// written. latched: ?u4 = null, /// Which of the two this is, filled in by the card. which: u1 = 0,
/// What the machine has just put on port B, which is the card's orders. /// /// Only a line the 6522 is driving says anything: one left as an input is /// not being held anywhere by the machine, and the card holds the chip's /// reset up rather than down when nothing is driving it. So a program that /// has not set the port's direction yet has not reset anything, which is /// the state a machine starts in. pub fn acted(self: *Half, log: anytype, cycle: u64) void { const driven = self.via.direction[1]; const lines = self.via.port[1]; if (driven & bus_reset != 0 and lines & bus_reset == 0) { self.chip.reset(); self.latched = null; self.release(); log.reset(cycle, self.which); return; } const data = self.via.port[0]; switch (lines & driven & 0x03) { // Both lines high is the address latch, and the data is which // register the chip is to be pointed at — if it names one. 0x03 => { self.latched = if (data & 0xf0 == 0) @truncate(data & 0x0f) else null; self.release(); }, 0x02 => { if (self.latched) |register| { self.chip.write(register, data); log.record(cycle, self.which, register, data); } self.release(); }, // The chip drives the port's pins, which is not the same as // writing the port's output register: a program reading a // register has pointed the data direction register at input // first, and an input pin reads the level on it rather than // whatever the output register happens to hold. Putting the // answer in the output register left it masked out and made every // AY read back as $ff, which is why mb-audit could not find one. 0x01 => if (self.latched) |register| self.via.setInputs(0, 0xff, self.chip.read(register)) else self.release(), else => self.release(), } }
/// Lets go of the data lines. /// /// The chip drives them only while it is being read from a register it is /// pointed at. Every other function leaves the bus in "high impedance", /// and what the 6522 then sees is the pull-ups: mb-audit's TestAYReadHiZ /// walks through a reset, a read with nothing selected, a latch and a /// write, and wants port A to read $ff after each of them. fn release(self: *Half) void { self.via.setInputs(0, 0xff, 0xff); }};
/// A record of what was written to which chip and when.////// The sound has to be made from this rather than from where the registers/// have got to, for the same reason the speaker keeps its flips: a caller that/// runs the machine for a frame and then looks would otherwise hear every/// change in that frame happen at once, at the end.pub fn Log(comptime enabled: bool) type { return if (enabled) struct { const Self = @This();
pub const Entry = struct { cycle: u64, chip: u2, register: u4, value: u8, /// A reset is not a register write, and has to be replayed as /// what it is. reset: bool = false, /// A write to the speech chip rather than one of the sound chips; /// `chip` is meaningless and `register` is the SSI's, nought to four. speech: bool = false, /// A phoneme strobed into a Votrax SC-01, on a Sound/Speech card: /// `value` is the phoneme code and `register` the two inflection /// bits. Only a Sound/Speech card's log ever carries these. votrax: bool = false, };
entries: [capacity]Entry = undefined, len: usize = 0, dropped: u64 = 0,
pub fn record(self: *Self, cycle: u64, chip: u2, register: u4, value: u8) void { self.put(.{ .cycle = cycle, .chip = chip, .register = register, .value = value }); }
pub fn recordSpeech(self: *Self, cycle: u64, register: u3, value: u8) void { self.put(.{ .cycle = cycle, .chip = 0, .register = register, .value = value, .speech = true }); }
pub fn recordVotrax(self: *Self, cycle: u64, phoneme: u6, inflection: u2) void { self.put(.{ .cycle = cycle, .chip = 0, .register = inflection, .value = phoneme, .votrax = true }); }
pub fn reset(self: *Self, cycle: u64, chip: u2) void { self.put(.{ .cycle = cycle, .chip = chip, .register = 0, .value = 0, .reset = true }); }
fn put(self: *Self, entry: Entry) void { if (self.len == capacity) { self.dropped += 1; return; } self.entries[self.len] = entry; self.len += 1; }
pub fn taken(self: *const Self) []const Entry { return self.entries[0..self.len]; }
pub fn clear(self: *Self) void { self.len = 0; }
/// Drops the ones that have been turned into sound, keeping the rest. pub fn consume(self: *Self, count: usize) void { const keep = self.len - count; std.mem.copyForwards(Entry, self.entries[0..keep], self.entries[count..self.len]); self.len = keep; } } else struct { const Self = @This(); pub const Entry = struct { cycle: u64, chip: u2, register: u4, value: u8, reset: bool = false, speech: bool = false, votrax: bool = false }; pub fn record(_: *Self, _: u64, _: u2, _: u4, _: u8) void {} pub fn recordSpeech(_: *Self, _: u64, _: u3, _: u8) void {} pub fn recordVotrax(_: *Self, _: u64, _: u6, _: u2) void {} pub fn reset(_: *Self, _: u64, _: u2) void {} pub fn taken(_: *const Self) []const Entry { return &.{}; } pub fn clear(_: *Self) void {} pub fn consume(_: *Self, _: usize) void {} };}
/// The card itself.////// What it does *not* hold is the log of what was written to it. That belongs/// to the machine, beside the speaker's and the cassette's, for two reasons./// A log with room for four thousand writes is sixty-four kilobytes, which is/// three hundred times the rest of the card and would be paid for once per/// slot in a machine whose slots can each hold one of these. And a log is not/// part of a Mockingboard: it is somebody watching one, which is why `acted`/// has always taken it as a parameter rather than reaching for a field.pub const Card = struct { const Self = @This();
/// Whether there is a card in the slot at all. fitted: bool = false, halves: [2]Half = .{ .{ .which = 0 }, .{ .which = 1 } },
/// The speech chip, and whether one is populated: a plain Mockingboard is /// sound only, while a Mockingboard C, a Phasor or an Echo+ has an SSI 263A /// in the socket. It is memory-mapped on the slot rather than driven /// through a 6522's ports — see `speech_low` — and its Acknowledge/Request /// line is wired to the first 6522's CA1, so "send me the next phoneme" /// arrives as an interrupt as well as being readable in bit seven. speech: ssi263.Chip = .{}, has_speech: bool = false, /// The level last fed to CA1 from the speech chip's A/R line, so that only /// a change is handed to the 6522, whose own edge detector turns it into /// the interrupt flag. It starts low to match the 6522's own CA1, so the /// first sight of A/R high is a real rising edge and the fall after a /// phoneme is a real falling one. speech_line: bool = false,
/// What the slot's RES line does, which is not the same as building /// the card again. /// /// Only the 6522s are wired to it, and even they keep their timer /// latches and counters through it — see `via.reset`. Wiping the card /// wholesale threw those away, and mb-audit checks them by name: it /// leaves $34 in T1's low latch, resets the machine, and wants to find /// it still there. pub fn reset(self: *Self, cycle: u64) void { for (&self.halves) |*half| half.via.reset(cycle); self.speech.reset(); self.speech_line = false; }
/// Whether either 6522 is pulling the interrupt line. /// /// Asked on every memory access the machine makes, so the answer for /// a slot with no card in it is inlined into the caller: the call /// alone was six per cent of a machine booting a disk with no card. pub inline fn interrupting(self: *Self, cycle: u64) bool { if (!self.fitted) return false; return self.viasInterrupting(cycle); }
fn viasInterrupting(self: *Self, cycle: u64) bool { // The speech chip's A/R line drops on its own when a phoneme finishes, // between one access and the next, so it is brought up to date here — // where the interrupt is asked about on every access — rather than only // when the chip is touched. self.updateSpeechLine(cycle); var asking = false; for (&self.halves) |*half| { half.via.catchUp(cycle); asking = asking or half.via.interrupting(); } return asking; }
/// Feeds the speech chip's A/R line to the first 6522's CA1, handing over /// only a change so the 6522's edge detector sees an edge. A/R high is the /// chip busy producing a phoneme; A/R low is it asking for the next, and /// the high-to-low edge is the interrupt. fn updateSpeechLine(self: *Self, cycle: u64) void { if (!self.has_speech) return; const high = !self.speech.asking(cycle); if (high != self.speech_line) { self.speech_line = high; self.halves[0].via.setLine(.ca1, high); } }
/// What the slot's page answers. The two halves are sixteen registers /// each, and everything between them is the first one over again, as /// the card's own decoding leaves it. pub fn access(self: *Self, offset: u8, is_write: bool, value: u8, log: anytype, cycle: u64) u8 { // The speech chip, when one is fitted, answers over the top of the // first 6522's mirror at $40–$7F. Its register is the low three address // bits, and a read hands back the inverted A/R in bit seven — the poll // a program does instead of taking the interrupt. Only bit seven is the // chip's to drive. if (self.has_speech and offset >= speech_low and offset < speech_high) { const register: ssi263.Register = @enumFromInt(@as(u3, @truncate(offset & 0x07))); const answer = if (is_write) blk: { self.speech.write(register, value, cycle); // The voice is made from the log, like the sound chips': a // caller looking a frame later would otherwise hear the whole // frame's phonemes at once, at the end of it. log.recordSpeech(cycle, @truncate(offset & 0x07), value); break :blk @as(u8, 0); } else self.speech.read(cycle); self.updateSpeechLine(cycle); return answer; }
const which: u1 = if (offset >= second_via) 1 else 0; const half = &self.halves[which]; const register: via.Register = @enumFromInt(@as(u4, @truncate(offset))); if (!is_write) return half.via.read(register, cycle);
half.via.write(register, value, cycle); // The orders are on port B: the data port is only data, and it // is put there before the lines are moved. if (register == .orb or register == .ddrb) half.acted(log, cycle); return 0; }};
/// Turning what was written into sound.////// It keeps chips of its own and walks them forward a sample at a time,/// applying each write as the cycle it happened on goes by, which is what/// makes a note begin where it was played rather than where the caller/// happened to look.pub const Renderer = struct { chips: [2]ay3.Chip = .{ .{}, .{} }, /// Where the last sample was taken, in cycles. It is kept as a fraction /// because a sample is not a whole number of them: at 44,100 a second a /// sample is a little over twenty-three cycles, and rounding that off /// every time would drift a tone flat. at: f64 = 0, cycles_per_sample: f64, /// Cycles owed to the chips, which tick sixteen at a time and so do not /// divide evenly into a sample either. owed: f64 = 0,
/// The speech chip and its voice, replayed from the same log as the sound /// chips. The chip's state is exact; the voice is an approximation — see /// `ssi263.Voice` — and rides down the middle of the two channels, since the /// SSI has one audio output. speech: ssi263.Chip = .{}, voice: ssi263.Voice,
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), @floatCast(cycles_per_second)), }; }
/// How many samples are owed by the cycle given. 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 { /// How many samples were written, both channels counted. samples: usize, /// How many of the writes have been dealt with and can be dropped. entries: usize, };
/// Fills as much of `out` as there is sound for, one chip to each channel: /// left, right, left, right. 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;
// Everything written before the end of this sample happens now, // which is what puts a note where it was played rather than where // the caller happened to look. while (used < entries.len and @as(f64, @floatFromInt(entries[used].cycle)) <= next) : (used += 1) { const entry = entries[used]; if (entry.speech) { self.speech.write(@enumFromInt(@as(u3, @truncate(entry.register))), entry.value, entry.cycle); } else if (entry.reset) { self.chips[entry.chip].reset(); } else { self.chips[entry.chip].write(entry.register, entry.value); } }
// Bring the speech chip up to this sample so a phoneme ends where // it should, and take one sample of the voice to lay down the // middle of the two channels. _ = self.speech.asking(@intFromFloat(next)); const speaking = 0.5 * self.voice.sample(&self.speech);
// The chips are stepped through the sample and what they put out // is averaged over it, so a square that turns over inside a sample // comes out half way rather than not at all. var sum: [2]f32 = @splat(0); var ticks: usize = 0; self.owed += self.cycles_per_sample; while (self.owed >= ay3.tick_divisor) : (self.owed -= ay3.tick_divisor) { for (&self.chips, 0..) |*chip, i| { chip.step(); sum[i] += chip.level(); } ticks += 1; }
for (0..2) |i| { const chip = if (ticks > 0) sum[i] / @as(f32, @floatFromInt(ticks)) else self.chips[i].level(); out[written * 2 + i] = chip + speaking; } written += 1; self.at = next; }
return .{ .samples = written * 2, .entries = used }; }};
/// A card and the log watching it, which is what a machine has and what these/// tests need two of.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); }
fn reset(self: *Watched, cycle: u64) void { self.card.reset(cycle); }};
/// Writing a register the way a player does: the number on port A, then the/// orders on port B twice, and the same again for the value.fn ready(card: anytype, half: u8, cycle: u64) void { const base: u8 = if (half == 0) first_via else second_via; _ = card.access(base + 3, true, 0xff, cycle); // port A is all outputs _ = card.access(base + 0, true, bus_inactive, cycle); // nothing being said _ = card.access(base + 2, true, 0x07, cycle); // and three lines of port B}
fn play(card: anytype, half: u8, register: u4, value: u8, cycle: u64) void { const base: u8 = if (half == 0) first_via else second_via; _ = card.access(base + 1, true, register, cycle); _ = card.access(base + 0, true, bus_latch, cycle); _ = card.access(base + 0, true, bus_inactive, cycle); _ = card.access(base + 1, true, value, cycle); _ = card.access(base + 0, true, bus_write, cycle); _ = card.access(base + 0, true, bus_inactive, cycle);}
test "a register written the way a player writes it arrives" { var card: Watched = .{}; ready(&card, 0, 0); ready(&card, 1, 0);
play(&card, 0, @intFromEnum(ay3.Register.a_fine), 0x34, 10); play(&card, 0, @intFromEnum(ay3.Register.a_coarse), 0x01, 20); try testing.expectEqual(@as(u16, 0x134), card.card.halves[0].chip.period(0)); // The other chip is its own: this is a stereo card and that is the point. try testing.expectEqual(@as(u16, 1), card.card.halves[1].chip.period(0));
play(&card, 1, @intFromEnum(ay3.Register.c_fine), 0x77, 30); try testing.expectEqual(@as(u16, 0x77), card.card.halves[1].chip.period(2));
// And what was written is kept, with when and to which chip, so that the // sound can be made from it afterwards. const written = card.log.taken(); try testing.expectEqual(@as(usize, 3), written.len); try testing.expectEqual(@as(u64, 10), written[0].cycle); try testing.expectEqual(@as(u1, 0), written[0].chip); try testing.expectEqual(@as(u8, 0x34), written[0].value); try testing.expectEqual(@as(u1, 1), written[2].chip);}
test "a card can be read back through the same lines" { var card: Watched = .{}; ready(&card, 0, 0); play(&card, 0, @intFromEnum(ay3.Register.b_fine), 0x5a, 10);
// Pointing the chip at a register and asking for a read puts what is // there on port A's pins. Port A has to be turned round to see it: the // chip drives the lines, and a line the 6522 is driving itself reads back // as whatever the 6522 is driving. Turning it round is what a program // does here, and forgetting to is why this once read back the register // number instead. _ = card.access(first_via + 1, true, @intFromEnum(ay3.Register.b_fine), 20); _ = card.access(first_via + 0, true, bus_latch, 20); _ = card.access(first_via + 3, true, 0x00, 20); // port A is all inputs _ = card.access(first_via + 0, true, bus_read, 20); try testing.expectEqual(@as(u8, 0x5a), card.access(first_via + 1, false, 0, 20));}
test "pulling the line low resets the chip it is wired to" { var card: Watched = .{}; ready(&card, 0, 0); play(&card, 0, @intFromEnum(ay3.Register.a_level), 15, 10); try testing.expectEqual(@as(u8, 15), card.card.halves[0].chip.registers[8]);
_ = card.access(first_via + 0, true, 0x00, 20); try testing.expectEqual(@as(u8, 0), card.card.halves[0].chip.registers[8]); // And the reset is in the log too, since the sound has to follow it. try testing.expect(card.log.taken()[card.log.taken().len - 1].reset);}
test "the card asks for attention when either timer runs down" { var card: Watched = .{}; try testing.expect(!card.card.interrupting(0));
// The first chip's timer, set going the way a player sets it: a frame's // worth of cycles, over and over. _ = card.access(first_via + @intFromEnum(via.Register.acr), true, via.acr_t1_continuous, 0); _ = card.access(first_via + @intFromEnum(via.Register.ier), true, 0x80 | via.irq_t1, 0); _ = card.access(first_via + @intFromEnum(via.Register.t1_low), true, 0x00, 0); _ = card.access(first_via + @intFromEnum(via.Register.t1_high), true, 0x40, 0);
try testing.expect(!card.card.interrupting(1000)); // $4000 loaded runs for $4002 cycles: the counter starts on the cycle // after the write, and a timer loaded with N has a period of N+2. try testing.expect(!card.card.interrupting(0x4001)); try testing.expect(card.card.interrupting(0x4002)); // Answering it by reading the counter takes it off the line. _ = card.access(first_via + @intFromEnum(via.Register.t1_low), false, 0, 0x4002); try testing.expect(!card.card.interrupting(0x4002)); // And it comes back round, since it was told to keep going. try testing.expect(card.card.interrupting(0x8010));}
test "a speech chip answers at $40 and polls its request line in bit seven" { var card: Watched = .{}; card.card.has_speech = true;
// Take it out of standby, give it the fastest rate, and load a phoneme of // the shortest duration, which is one frame unit long. _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.control)), true, 0x00, 0); _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.rate_inflection)), true, 0xf0, 0); _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.duration_phoneme)), true, 0xc0, 0); const length = ssi263.frame_unit; // rate 15 -> one unit, duration 3 -> times one
// While it is producing, A/R is high and bit seven reads back clear. try testing.expectEqual(@as(u8, 0x00), card.access(speech_low + 1, false, 0, length - 1)); // Once the phoneme is done it asks for the next: bit seven set. try testing.expectEqual(@as(u8, 0x80), card.access(speech_low + 1, false, 0, length));
// And a write to the phoneme register answers the request. _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.duration_phoneme)), true, 0x01, length); try testing.expectEqual(@as(u8, 0x00), card.access(speech_low + 1, false, 0, length));}
test "a speech chip's A/R line interrupts through the first 6522's CA1" { var card: Watched = .{}; card.card.has_speech = true;
// Enable a CA1 interrupt on the first 6522; the default control register // takes the negative edge, which is the A/R line's high-to-low fall. _ = card.access(first_via + @intFromEnum(via.Register.ier), true, 0x80 | via.irq_ca1, 0);
// A short phoneme, then run past the end of it. _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.control)), true, 0x00, 0); _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.rate_inflection)), true, 0xf0, 0); _ = card.access(speech_low + @as(u8, @intFromEnum(ssi263.Register.duration_phoneme)), true, 0xc0, 0); const length = ssi263.frame_unit;
// Nothing while it is still speaking; the interrupt lands when it finishes. try testing.expect(!card.card.interrupting(length - 1)); try testing.expect(card.card.interrupting(length));
// Reading the 6522's port A takes the CA1 flag off the line, the way // answering the request does. _ = card.access(first_via + @intFromEnum(via.Register.ora), false, 0, length); try testing.expect(!card.card.interrupting(length));}
test "without a speech chip the range is the first 6522 over again" { // The default card has no speech chip, so $40 is the mirror of $00 the // decoding leaves it, and touches the 6522 rather than any speech. var card: Watched = .{}; _ = card.access(speech_low + @as(u8, @intFromEnum(via.Register.ddrb)), true, 0xff, 0); try testing.expectEqual(@as(u8, 0xff), card.access(@intFromEnum(via.Register.ddrb), false, 0, 0));}
test "the speech voice is rendered from the log, down the middle of both channels" { var renderer: Renderer = .init(ay3.cycles_per_second, 44_100, 0); var samples: [4000]f32 = undefined;
// A voiced vowel spoken: out of standby at full amplitude, then a phoneme. // These are speech writes, replayed into the renderer's own SSI chip. const spoken = [_]Log(true).Entry{ .{ .cycle = 0, .chip = 0, .register = 3, .value = 0x0f, .speech = true }, .{ .cycle = 0, .chip = 0, .register = 2, .value = 0x00, .speech = true }, .{ .cycle = 0, .chip = 0, .register = 0, .value = 0x08, .speech = true }, }; const made = renderer.render(&spoken, 1_000_000, &samples); try testing.expectEqual(@as(usize, spoken.len), made.entries);
// The SSI has one audio output, so the voice is the same in both channels, // and there is real energy in it while the phoneme is being spoken. var energy: f32 = 0; var at: usize = 0; while (at < made.samples) : (at += 2) { try testing.expectEqual(samples[at], samples[at + 1]); energy += samples[at] * samples[at]; } try testing.expect(energy > 0);}
test "silence is silent, and a tone is not" { var renderer: Renderer = .init(ay3.cycles_per_second, 44_100, 0); var samples: [2000]f32 = undefined;
// Nothing written: two channels of nothing. const nothing = renderer.render(&[_]Log(true).Entry{}, 1_000_000, &samples); try testing.expect(nothing.samples > 0); for (samples[0..nothing.samples]) |sample| try testing.expectEqual(@as(f32, 0), sample);
// A tone on the first chip's first channel, and nothing on the second. var playing: Renderer = .init(ay3.cycles_per_second, 44_100, 0); const written = [_]Log(true).Entry{ .{ .cycle = 0, .chip = 0, .register = @intFromEnum(ay3.Register.a_fine), .value = 100 }, .{ .cycle = 0, .chip = 0, .register = @intFromEnum(ay3.Register.mixer), .value = 0xfe }, .{ .cycle = 0, .chip = 0, .register = @intFromEnum(ay3.Register.a_level), .value = 15 }, }; const made = playing.render(&written, 1_000_000, &samples); try testing.expectEqual(@as(usize, written.len), made.entries);
var loudest: f32 = 0; var right: f32 = 0; var crossings: usize = 0; var was: bool = false; var at: usize = 0; while (at < made.samples) : (at += 2) { const left = samples[at]; loudest = @max(loudest, left); right = @max(right, samples[at + 1]); const high = left > 0.05; if (high != was) crossings += 1; was = high; } try testing.expect(loudest > 0.2); // The other chip is silent: this is the left one only. try testing.expectEqual(@as(f32, 0), right);
// And it is the frequency the period asked for: a period of a hundred at // a sixteenth of the clock is about 638 hertz, so about that many turns // over each way in the second that was rendered. const seconds = @as(f32, @floatFromInt(made.samples / 2)) / 44_100.0; const hertz = @as(f32, @floatFromInt(crossings)) / 2.0 / seconds; try testing.expect(hertz > 620 and hertz < 655);}
test "the reset line reaches the card, and the timer latches survive it" { var card: Watched = .{}; ready(&card, 0, 0);
// A timer running with its interrupt wanted, which is the state mb-audit // resets a machine in on purpose. _ = card.access(first_via + @intFromEnum(via.Register.ier), true, 0x80 | via.irq_t1, 0); _ = card.access(first_via + @intFromEnum(via.Register.t1_low), true, 0x34, 0); _ = card.access(first_via + @intFromEnum(via.Register.t1_high), true, 0x00, 0); try testing.expect(card.card.interrupting(0x40));
card.reset(0x40);
// "T1 and T2, SR and the interrupt logic are disabled from operation" // (Western Design Center, 2010, p. 36), so it stops asking. try testing.expect(!card.card.interrupting(0x80));
// But the latches are not cleared: "Reset clears all internal registers // (except T1 and T2 counters and latches, and the SR)". mb-audit checks // this one by name — it leaves $34 in T1's low latch, resets, and wants to // find it still there. try testing.expectEqual( @as(u8, 0x34), card.access(first_via + @intFromEnum(via.Register.t1_latch_low), false, 0, 0x80), );}