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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The cassette port: two lines, each one bit wide.//!//! What goes out is the speaker over again. Touching $c020 flips the output//! line, and a program writes to tape by flipping it in patterns, so the whole//! of what reaches the tape is the timing. The record of that is//! `speaker.Log`, and `speaker.Renderer` turns it into sound, because it is//! the same problem and the same answer.//!//! What comes in is simpler still. The machine's input is a zero crossing//! detector: reading $c060 says which side of zero the signal is on and//! nothing finer than that ever reaches the processor, so a tape is a run of//! sides rather than a waveform.
/// A tape being played into the cassette input.////// One byte a sample, each saying which side of zero the signal was on.pub const Tape = struct { levels: []const u8, sample_rate: u32, /// The cycle playing started on. Where the tape has got to is worked out /// from the machine's own clock, so it plays at the right speed relative to /// the machine however fast the machine is being run: a tape loads at ten /// times the speed on a machine running ten times as fast, exactly as the /// program reading it does. started: u64 = 0,
/// Which side of zero the tape is on, so many cycles into playing it. A /// tape that has run out reads as nothing at all. pub fn levelAt(self: Tape, elapsed: u64, cycles_per_second: u64) bool { const index = elapsed * self.sample_rate / cycles_per_second; if (index >= self.levels.len) return false; return self.levels[index] != 0; }};
const std = @import("std");const testing = std.testing;
test "a tape is read at the machine's own pace" { // Four samples at one a second, alternating sides of zero. const tape: Tape = .{ .levels = &.{ 1, 0, 1, 0 }, .sample_rate = 1 }; const rate = 1_000_000;
try testing.expect(tape.levelAt(0, rate)); try testing.expect(!tape.levelAt(rate, rate)); try testing.expect(tape.levelAt(2 * rate, rate)); try testing.expect(!tape.levelAt(3 * rate, rate)); // Run out, and there is nothing there. try testing.expect(!tape.levelAt(4 * rate, rate)); try testing.expect(!tape.levelAt(400 * rate, rate));}