Merge pull request #1923 from TomHarte/MacintoshPush
Transition the Macintosh to push audio.
This commit is contained in:
8 files changed
+219
-207
No files matched your search
@@ -13,88 +13,100 @@
|
||||
using namespace Apple::Macintosh;
|
||||
|
||||
namespace {
|
||||
constexpr HalfCycles MasterClockDivider = 8; // Period at which to generate output samples; higher than the sampling
|
||||
// rate to capture VIA activity — volume changes and the enable/disable
|
||||
// toggle — which might occur at higher frequencies.
|
||||
|
||||
// The sample_length is coupled with the clock rate selected within the Macintosh proper;
|
||||
// as per the header-declaration a divide-by-two clock is expected to arrive here.
|
||||
const std::size_t sample_length = 352 / 2;
|
||||
|
||||
constexpr auto MaxBufferSize = 1024; // Maximum number of samples to accumulate on the emulator thread before
|
||||
// pushing to the speaker.
|
||||
}
|
||||
|
||||
Audio::Audio(Concurrency::AsyncTaskQueue<false> &task_queue) : task_queue_(task_queue) {}
|
||||
// MARK: - Setup
|
||||
Audio::Audio(HalfCycles clock_rate, const Video &video, DriveSpeedAccumulator &drive_speed_accumulator) :
|
||||
phase_(video.audio_phase()),
|
||||
drive_speed_accumulator_(drive_speed_accumulator) {
|
||||
|
||||
speaker_.speaker().set_input_rate(clock_rate.as<float>() / MasterClockDivider.as<float>());
|
||||
}
|
||||
|
||||
void Audio::set_ram(const uint16_t *const ram, const uint32_t mask) {
|
||||
ram_ = ram;
|
||||
ram_mask_ = mask;
|
||||
}
|
||||
|
||||
void Audio::set_use_alternate_buffer(const bool use_alternate_buffer) {
|
||||
use_alternate_buffer_ = use_alternate_buffer;
|
||||
}
|
||||
|
||||
// MARK: - Inputs
|
||||
|
||||
void Audio::post_sample(uint8_t sample) {
|
||||
// Store sample directly indexed by current write pointer; this ensures that collected samples
|
||||
// directly map to volume and enabled/disabled states.
|
||||
sample_queue_.buffer[sample_queue_.write_pointer].store(sample, std::memory_order::relaxed);
|
||||
sample_queue_.write_pointer = (sample_queue_.write_pointer + 1) % sample_queue_.buffer.size();
|
||||
void Audio::operator += (const HalfCycles duration) {
|
||||
HalfCycles remaining = duration;
|
||||
while(remaining > HalfCycles(0)) {
|
||||
const auto next = std::min(remaining.as<unsigned>(), phase_.sample_phase.remaining());
|
||||
remaining -= HalfCycles(next);
|
||||
time_ += HalfCycles(next);
|
||||
|
||||
if(phase_.sample_phase.advance_divide(duration.as<unsigned>())) {
|
||||
const size_t base =
|
||||
use_alternate_buffer_ ? (0xffffa100 >> 1) : (0xfffffd00 >> 1);
|
||||
const uint16_t audio_word = ram_[(address_ + base) & ram_mask_];
|
||||
++address_;
|
||||
|
||||
post_sample(audio_word >> 8);
|
||||
drive_speed_accumulator_.post_sample(audio_word & 0xff);
|
||||
|
||||
if(phase_.buffer_phase.advance_divide(1)) {
|
||||
address_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr auto MaxBufferDuration = HalfCycles(Cycles(MaxBufferSize)) * MasterClockDivider;
|
||||
if(time_ >= MaxBufferDuration) {
|
||||
generate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::set_volume(int volume) {
|
||||
// Do nothing if the volume hasn't changed.
|
||||
if(posted_volume_ == volume) return;
|
||||
posted_volume_ = volume;
|
||||
void Audio::generate() {
|
||||
const auto samples = time_.divide(MasterClockDivider).as<size_t>();
|
||||
const auto level = int16_t(sample_ * volume_multiplier_);
|
||||
|
||||
// Post the volume change as a deferred event.
|
||||
task_queue_.enqueue([this, volume] () {
|
||||
volume_ = volume;
|
||||
set_volume_multiplier();
|
||||
});
|
||||
std::fill_n(std::back_inserter(samples_), samples, level);
|
||||
if(samples_.size() >= MaxBufferSize) {
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::set_enabled(bool on) {
|
||||
// Do nothing if the mask hasn't changed.
|
||||
if(posted_enable_mask_ == int(on)) return;
|
||||
posted_enable_mask_ = int(on);
|
||||
|
||||
// Post the enabled mask change as a deferred event.
|
||||
task_queue_.enqueue([this, on] () {
|
||||
enabled_mask_ = int(on);
|
||||
set_volume_multiplier();
|
||||
});
|
||||
void Audio::flush() {
|
||||
speaker_.push_swap(samples_);
|
||||
}
|
||||
|
||||
// MARK: - Output generation
|
||||
// MARK: - Setters.
|
||||
|
||||
bool Audio::is_zero_level() const {
|
||||
return !volume_ || !enabled_mask_;
|
||||
void Audio::post_sample(const uint8_t sample) {
|
||||
if(sample_ == sample) return;
|
||||
generate();
|
||||
|
||||
sample_ = sample;
|
||||
}
|
||||
|
||||
void Audio::set_sample_volume_range(std::int16_t range) {
|
||||
// Some underflow here doesn't really matter.
|
||||
output_volume_ = range / (7 * 255);
|
||||
void Audio::set_volume(const int volume) {
|
||||
if(volume_ == volume) return;
|
||||
generate();
|
||||
|
||||
volume_ = volume;
|
||||
set_volume_multiplier();
|
||||
}
|
||||
|
||||
void Audio::set_enabled(const bool on) {
|
||||
if(enable_mask_ == int(on)) return;
|
||||
generate();
|
||||
|
||||
enable_mask_ = int(on);
|
||||
set_volume_multiplier();
|
||||
}
|
||||
|
||||
void Audio::set_volume_multiplier() {
|
||||
volume_multiplier_ = int16_t(output_volume_ * volume_ * enabled_mask_);
|
||||
volume_multiplier_ = int16_t(volume_ * enable_mask_ * (127 / 7));
|
||||
}
|
||||
|
||||
template <Outputs::Speaker::Action action>
|
||||
void Audio::apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target) {
|
||||
// TODO: the implementation below acts as if the hardware uses pulse-amplitude modulation;
|
||||
// in fact it uses pulse-width modulation. But the scale for pulses isn't specified, so
|
||||
// that's something to return to.
|
||||
|
||||
while(number_of_samples) {
|
||||
// Determine how many output samples will be at the same level.
|
||||
const auto cycles_left_in_sample = std::min(number_of_samples, sample_length - subcycle_offset_);
|
||||
|
||||
// Determine the output level, and output that many samples.
|
||||
const int16_t output_level = volume_multiplier_ * (int16_t(sample_queue_.buffer[sample_queue_.read_pointer].load(std::memory_order::relaxed)) - 128);
|
||||
Outputs::Speaker::fill<action>(target, target + cycles_left_in_sample, output_level);
|
||||
target += cycles_left_in_sample;
|
||||
|
||||
// Advance the sample pointer.
|
||||
subcycle_offset_ += cycles_left_in_sample;
|
||||
sample_queue_.read_pointer = (sample_queue_.read_pointer + (subcycle_offset_ / sample_length)) % sample_queue_.buffer.size();
|
||||
subcycle_offset_ %= sample_length;
|
||||
|
||||
// Decreate the number of samples left to write.
|
||||
number_of_samples -= cycles_left_in_sample;
|
||||
}
|
||||
}
|
||||
template void Audio::apply_samples<Outputs::Speaker::Action::Mix>(std::size_t, Outputs::Speaker::MonoSample *);
|
||||
template void Audio::apply_samples<Outputs::Speaker::Action::Store>(std::size_t, Outputs::Speaker::MonoSample *);
|
||||
template void Audio::apply_samples<Outputs::Speaker::Action::Ignore>(std::size_t, Outputs::Speaker::MonoSample *);
|
||||
@@ -8,12 +8,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Concurrency/AsyncTaskQueue.hpp"
|
||||
#include "ClockReceiver/ClockReceiver.hpp"
|
||||
#include "Outputs/Speaker/Implementation/BufferSource.hpp"
|
||||
#include "DriveSpeedAccumulator.hpp"
|
||||
#include "Video.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include "Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
||||
|
||||
namespace Apple::Macintosh {
|
||||
|
||||
@@ -23,9 +21,11 @@ namespace Apple::Macintosh {
|
||||
Designed to be clocked at half the rate of the real hardware — i.e.
|
||||
a shade less than 4Mhz.
|
||||
*/
|
||||
class Audio: public ::Outputs::Speaker::BufferSource<Audio, false> {
|
||||
class Audio {
|
||||
public:
|
||||
Audio(Concurrency::AsyncTaskQueue<false> &task_queue);
|
||||
Audio(HalfCycles clock_rate, const Video &, DriveSpeedAccumulator &);
|
||||
void set_ram(const uint16_t *ram, uint32_t mask);
|
||||
void set_use_alternate_buffer(bool);
|
||||
|
||||
/*!
|
||||
Macintosh audio is (partly) sourced by the same scanning
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
|
||||
Use this method to add a newly-collected sample to the queue.
|
||||
*/
|
||||
void post_sample(uint8_t sample);
|
||||
void post_sample(uint8_t);
|
||||
|
||||
/*!
|
||||
Macintosh audio also separately receives an output volume
|
||||
@@ -42,43 +42,45 @@ public:
|
||||
|
||||
Use this method to set the current output volume.
|
||||
*/
|
||||
void set_volume(int volume);
|
||||
void set_volume(int);
|
||||
|
||||
/*!
|
||||
A further factor in audio output is the on-off toggle.
|
||||
*/
|
||||
void set_enabled(bool on);
|
||||
void set_enabled(bool);
|
||||
|
||||
// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter.
|
||||
template <Outputs::Speaker::Action action>
|
||||
void apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target);
|
||||
bool is_zero_level() const;
|
||||
void set_sample_volume_range(std::int16_t range);
|
||||
Outputs::Speaker::Speaker &speaker() {
|
||||
return speaker_.speaker();
|
||||
}
|
||||
|
||||
void operator += (const HalfCycles);
|
||||
|
||||
void flush();
|
||||
|
||||
private:
|
||||
Concurrency::AsyncTaskQueue<false> &task_queue_;
|
||||
Outputs::Speaker::AsyncPushLowpass<false> speaker_;
|
||||
std::vector<int16_t> samples_;
|
||||
HalfCycles time_;
|
||||
|
||||
// A queue of fetched samples; read from by one thread,
|
||||
// written to by another.
|
||||
struct {
|
||||
std::array<std::atomic<uint8_t>, 740> buffer;
|
||||
size_t read_pointer = 0, write_pointer = 0;
|
||||
} sample_queue_;
|
||||
// Fetch state.
|
||||
size_t address_ = 0;
|
||||
bool use_alternate_buffer_ = false;
|
||||
|
||||
AudioPhase phase_;
|
||||
const uint16_t *ram_ = nullptr;
|
||||
uint32_t ram_mask_ = 0;
|
||||
DriveSpeedAccumulator &drive_speed_accumulator_;
|
||||
|
||||
// Emulator-thread stateful variables, to avoid work posting
|
||||
// deferral updates if possible.
|
||||
int posted_volume_ = 0;
|
||||
int posted_enable_mask_ = 0;
|
||||
|
||||
// Stateful variables, modified from the audio generation
|
||||
// thread only.
|
||||
int volume_ = 0;
|
||||
int enabled_mask_ = 0;
|
||||
std::int16_t output_volume_ = 0;
|
||||
int enable_mask_ = 0;
|
||||
|
||||
std::int16_t volume_multiplier_ = 0;
|
||||
std::size_t subcycle_offset_ = 0;
|
||||
int volume_multiplier_ = 0;
|
||||
uint8_t sample_ = 0;
|
||||
void set_volume_multiplier();
|
||||
|
||||
void generate();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
//
|
||||
// DeferredAudio.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 01/06/2019.
|
||||
// Copyright © 2019 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Audio.hpp"
|
||||
#include "Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
||||
|
||||
namespace Apple::Macintosh {
|
||||
|
||||
struct DeferredAudio {
|
||||
Concurrency::AsyncTaskQueue<false> queue;
|
||||
Audio audio;
|
||||
Outputs::Speaker::PullLowpass<Audio> speaker;
|
||||
HalfCycles time_since_update;
|
||||
|
||||
DeferredAudio() : audio(queue), speaker(audio) {}
|
||||
|
||||
void flush() {
|
||||
speaker.run_for(queue, time_since_update.flush<Cycles>());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "DeferredAudio.hpp"
|
||||
#include "Audio.hpp"
|
||||
#include "DriveSpeedAccumulator.hpp"
|
||||
#include "Keyboard.hpp"
|
||||
#include "Video.hpp"
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
}),
|
||||
mc68000_(*this),
|
||||
iwm_(CLOCK_RATE),
|
||||
video_(audio_, drive_speed_accumulator_),
|
||||
audio_(HalfCycles(Cycles(CLOCK_RATE)), video_, drive_speed_accumulator_),
|
||||
via_port_handler_(*this, clock_, keyboard_, audio_, iwm_, mouse_),
|
||||
via_(via_port_handler_),
|
||||
scsi_bus_(CLOCK_RATE * 2),
|
||||
@@ -107,25 +107,24 @@ public:
|
||||
|
||||
// Select a ROM name and determine the proper ROM and RAM sizes
|
||||
// based on the machine model.
|
||||
using Model = Analyser::Static::Macintosh::Target::Model;
|
||||
const std::string machine_name = "Macintosh";
|
||||
uint32_t ram_size, rom_size;
|
||||
ROM::Name rom_name;
|
||||
switch(model) {
|
||||
using enum Analyser::Static::Macintosh::Target::Model;
|
||||
default:
|
||||
case Model::Mac128k:
|
||||
case Mac128k:
|
||||
ram_size = 128*1024;
|
||||
rom_size = 64*1024;
|
||||
rom_name = ROM::Name::Macintosh128k;
|
||||
break;
|
||||
case Model::Mac512k:
|
||||
case Mac512k:
|
||||
ram_size = 512*1024;
|
||||
rom_size = 64*1024;
|
||||
rom_name = ROM::Name::Macintosh512k;
|
||||
break;
|
||||
case Model::Mac512ke:
|
||||
case Model::MacPlus: {
|
||||
ram_size = ((model == Model::MacPlus) ? 4096 : 512)*1024;
|
||||
case Mac512ke:
|
||||
case MacPlus: {
|
||||
ram_size = ((model == MacPlus) ? 4096 : 512)*1024;
|
||||
rom_size = 128*1024;
|
||||
rom_name = ROM::Name::MacintoshPlus;
|
||||
} break;
|
||||
@@ -134,6 +133,7 @@ public:
|
||||
rom_mask_ = rom_size - 1;
|
||||
ram_.resize(ram_size);
|
||||
video_.set_ram(reinterpret_cast<uint16_t *>(ram_.data()), ram_mask_ >> 1);
|
||||
audio_.set_ram(reinterpret_cast<uint16_t *>(ram_.data()), ram_mask_ >> 1);
|
||||
|
||||
// Grab a copy of the ROM and convert it into big-endian data.
|
||||
ROM::Request request(rom_name);
|
||||
@@ -165,7 +165,6 @@ public:
|
||||
|
||||
// The Mac runs at 7.8336mHz.
|
||||
set_clock_rate(double(CLOCK_RATE));
|
||||
audio_.speaker.set_input_rate(float(CLOCK_RATE) / 2.0f);
|
||||
|
||||
// Insert any supplied media.
|
||||
insert_media(target.media);
|
||||
@@ -174,10 +173,6 @@ public:
|
||||
setup_memory_map();
|
||||
}
|
||||
|
||||
~ConcreteMachine() {
|
||||
audio_.queue.lock_flush();
|
||||
}
|
||||
|
||||
void set_scan_target(Outputs::Display::ScanTarget *scan_target) final {
|
||||
video_.set_scan_target(scan_target);
|
||||
}
|
||||
@@ -187,7 +182,7 @@ public:
|
||||
}
|
||||
|
||||
Outputs::Speaker::Speaker *get_speaker() final {
|
||||
return &audio_.speaker;
|
||||
return &audio_.speaker();
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) final {
|
||||
@@ -385,16 +380,14 @@ public:
|
||||
return delay;
|
||||
}
|
||||
|
||||
void flush_output(int) {
|
||||
// Flush the video before the audio queue; in a Mac the
|
||||
// video is responsible for providing part of the
|
||||
// audio signal, so the two aren't as distinct as in
|
||||
// most machines.
|
||||
update_video();
|
||||
void flush_output(const int outputs) final {
|
||||
if(outputs & Output::Video) {
|
||||
update_video();
|
||||
}
|
||||
|
||||
// As above: flush audio after video.
|
||||
via_.flush();
|
||||
audio_.queue.perform();
|
||||
if(outputs & Output::Audio) {
|
||||
audio_.flush();
|
||||
}
|
||||
|
||||
// This avoids deferring IWM costs indefinitely, until
|
||||
// they become artbitrarily large.
|
||||
@@ -451,9 +444,9 @@ public:
|
||||
return video_.is_outputting(time_since_video_update_);
|
||||
}
|
||||
|
||||
void set_use_alternate_buffers(bool use_alternate_screen_buffer, bool use_alternate_audio_buffer) {
|
||||
void set_use_alternate_video_buffer(const bool use_alternate_buffer) {
|
||||
update_video();
|
||||
video_.set_use_alternate_buffers(use_alternate_screen_buffer, use_alternate_audio_buffer);
|
||||
video_.set_use_alternate_buffer(use_alternate_buffer);
|
||||
}
|
||||
|
||||
bool insert_media(const Analyser::Static::Media &media) final {
|
||||
@@ -573,10 +566,11 @@ private:
|
||||
}
|
||||
|
||||
/// Advances all non-CPU components by @c duration half cycles.
|
||||
forceinline void advance_time(HalfCycles duration) {
|
||||
forceinline void advance_time(const HalfCycles duration) {
|
||||
time_since_video_update_ += duration;
|
||||
iwm_ += duration;
|
||||
ram_subcycle_ = (ram_subcycle_ + duration.get()) & 15;
|
||||
audio_ += duration;
|
||||
|
||||
// The VIA runs at one-tenth of the 68000's clock speed, in sync with the E clock.
|
||||
// See: Guide to the Macintosh Hardware Family p149 (PDF p188). Some extra division
|
||||
@@ -661,7 +655,14 @@ private:
|
||||
|
||||
class VIAPortHandler: public MOS::MOS6522::PortHandler {
|
||||
public:
|
||||
VIAPortHandler(ConcreteMachine &machine, Apple::Clock::SerialClock &clock, Keyboard &keyboard, DeferredAudio &audio, IWMActor &iwm, Inputs::QuadratureMouse &mouse) :
|
||||
VIAPortHandler(
|
||||
ConcreteMachine &machine,
|
||||
Apple::Clock::SerialClock &clock,
|
||||
Keyboard &keyboard,
|
||||
Audio &audio,
|
||||
IWMActor &iwm,
|
||||
Inputs::QuadratureMouse &mouse
|
||||
) :
|
||||
machine_(machine), clock_(clock), keyboard_(keyboard), audio_(audio), iwm_(iwm), mouse_(mouse) {}
|
||||
|
||||
using Port = MOS::MOS6522::Port;
|
||||
@@ -685,11 +686,11 @@ private:
|
||||
*/
|
||||
iwm_->set_select(value & 0x20);
|
||||
|
||||
machine_.set_use_alternate_buffers(!(value & 0x40), !(value&0x08));
|
||||
machine_.set_use_alternate_video_buffer(!(value & 0x40));
|
||||
machine_.set_rom_is_overlay(value & 0x10);
|
||||
|
||||
audio_.flush();
|
||||
audio_.audio.set_volume(value & 7);
|
||||
audio_.set_use_alternate_buffer(!(value&0x08));
|
||||
audio_.set_volume(value & 7);
|
||||
break;
|
||||
|
||||
case Port::B:
|
||||
@@ -707,8 +708,7 @@ private:
|
||||
if(value & 0x4) clock_.abort();
|
||||
else clock_.set_input(value & 0x2, value & 0x1);
|
||||
|
||||
audio_.flush();
|
||||
audio_.audio.set_enabled(!(value & 0x80));
|
||||
audio_.set_enabled(!(value & 0x80));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -748,16 +748,6 @@ private:
|
||||
else Logger::error().append("Unhandled 6522 control line output: %c%d", port ? 'B' : 'A', int(line));
|
||||
}
|
||||
|
||||
void run_for(HalfCycles duration) {
|
||||
// The 6522 enjoys a divide-by-ten, so multiply back up here to make the
|
||||
// divided-by-two clock the audio works on.
|
||||
audio_.time_since_update += HalfCycles(duration.get() * 5);
|
||||
}
|
||||
|
||||
void flush() {
|
||||
audio_.flush();
|
||||
}
|
||||
|
||||
void set_interrupt_status(bool) {
|
||||
machine_.update_interrupt_input();
|
||||
}
|
||||
@@ -766,7 +756,7 @@ private:
|
||||
ConcreteMachine &machine_;
|
||||
Apple::Clock::SerialClock &clock_;
|
||||
Keyboard &keyboard_;
|
||||
DeferredAudio &audio_;
|
||||
Audio &audio_;
|
||||
IWMActor &iwm_;
|
||||
Inputs::QuadratureMouse &mouse_;
|
||||
};
|
||||
@@ -776,8 +766,8 @@ private:
|
||||
DriveSpeedAccumulator drive_speed_accumulator_;
|
||||
IWMActor iwm_;
|
||||
|
||||
DeferredAudio audio_;
|
||||
Video video_;
|
||||
Audio audio_;
|
||||
|
||||
Apple::Clock::SerialClock clock_;
|
||||
Keyboard keyboard_;
|
||||
|
||||
@@ -28,11 +28,7 @@ constexpr uint64_t PixelMask = std::endian::native == std::endian::little ? 0x01
|
||||
// "The visible portion of a full-screen display consists of 342 horizontal scan lines...
|
||||
// During the vertical blanking interval, the turned-off beam ... traces out an additional 28 scan lines,"
|
||||
//
|
||||
Video::Video(DeferredAudio &audio, DriveSpeedAccumulator &drive_speed_accumulator) :
|
||||
audio_(audio),
|
||||
drive_speed_accumulator_(drive_speed_accumulator),
|
||||
crt_(704, 1, 370, 6, Outputs::Display::InputDataType::Luminance1) {
|
||||
|
||||
Video::Video() : crt_(704, 1, 370, 6, Outputs::Display::InputDataType::Luminance1) {
|
||||
crt_.set_display_type(Outputs::Display::DisplayType::RGB);
|
||||
crt_.set_aspect_ratio(1.73f); // The Mac uses a non-standard scanning area.
|
||||
}
|
||||
@@ -47,8 +43,7 @@ Outputs::Display::ScanStatus Video::get_scaled_scan_status() const {
|
||||
|
||||
void Video::run_for(HalfCycles duration) {
|
||||
// Determine the current video and audio bases. These values don't appear to be latched, they apply immediately.
|
||||
const size_t video_base = (use_alternate_screen_buffer_ ? (0xffff2700 >> 1) : (0xffffa700 >> 1)) & ram_mask_;
|
||||
const size_t audio_base = (use_alternate_audio_buffer_ ? (0xffffa100 >> 1) : (0xfffffd00 >> 1)) & ram_mask_;
|
||||
const size_t video_base = (use_alternate_buffer_ ? (0xffff2700 >> 1) : (0xffffa700 >> 1)) & ram_mask_;
|
||||
|
||||
// The number of HalfCycles is literally the number of pixel clocks to move through,
|
||||
// since pixel output occurs at twice the processor clock. So divide by 16 to get
|
||||
@@ -121,14 +116,6 @@ void Video::run_for(HalfCycles duration) {
|
||||
crt_.output_blank((44 - sync_end) * 16);
|
||||
}
|
||||
}
|
||||
|
||||
// Audio and disk fetches occur "just before video data".
|
||||
if(final_word == 44) {
|
||||
const uint16_t audio_word = ram_[audio_address_ + audio_base];
|
||||
++audio_address_;
|
||||
audio_.audio.post_sample(audio_word >> 8);
|
||||
drive_speed_accumulator_.post_sample(audio_word & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
duration -= cycles_left_in_line;
|
||||
@@ -144,7 +131,7 @@ void Video::run_for(HalfCycles duration) {
|
||||
"The main sound buffer is at $1FD00 in a 128K Macintosh, and the alternate buffer is at $1A100;
|
||||
for a 512K Macintosh, add $60000 to these values."
|
||||
*/
|
||||
audio_address_ = 0;
|
||||
// audio_address_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,9 +157,8 @@ HalfCycles Video::next_sequence_point() {
|
||||
}
|
||||
}
|
||||
|
||||
void Video::set_use_alternate_buffers(bool use_alternate_screen_buffer, bool use_alternate_audio_buffer) {
|
||||
use_alternate_screen_buffer_ = use_alternate_screen_buffer;
|
||||
use_alternate_audio_buffer_ = use_alternate_audio_buffer;
|
||||
void Video::set_use_alternate_buffer(const bool use_alternate_buffer) {
|
||||
use_alternate_buffer_ = use_alternate_buffer;
|
||||
}
|
||||
|
||||
void Video::set_ram(const uint16_t *const ram, const uint32_t mask) {
|
||||
@@ -184,3 +170,15 @@ void Video::set_ram(const uint16_t *const ram, const uint32_t mask) {
|
||||
run_for(Cycles(10'000));
|
||||
});
|
||||
}
|
||||
|
||||
AudioPhase Video::audio_phase() const {
|
||||
AudioPhase phase;
|
||||
|
||||
// Audio and disk fetches occur "just before video data". So put that at cycle 44.
|
||||
const auto relative_frame_position = (frame_position_ + frame_length - 44) % frame_length;
|
||||
|
||||
phase.sample_phase = (relative_frame_position % line_length).as<unsigned>();
|
||||
phase.buffer_phase = (relative_frame_position / line_length).as<unsigned>();
|
||||
|
||||
return phase;
|
||||
}
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Outputs/CRT/CRT.hpp"
|
||||
#include "ClockReceiver/ClockReceiver.hpp"
|
||||
#include "DeferredAudio.hpp"
|
||||
#include "DriveSpeedAccumulator.hpp"
|
||||
#include "Outputs/CRT/CRT.hpp"
|
||||
|
||||
#include "Numeric/CircularCounter.hpp"
|
||||
|
||||
namespace Apple::Macintosh {
|
||||
|
||||
@@ -21,6 +21,24 @@ constexpr HalfCycles frame_length(line_length * HalfCycles(number_of_lines));
|
||||
constexpr int sync_start = 36;
|
||||
constexpr int sync_end = 38;
|
||||
|
||||
/*!
|
||||
In a real Macintosh the video collector is also responsible for audio collection.
|
||||
|
||||
This emulator has opted to decouple video activity from audio collection, which
|
||||
means the audio collector needs to be able to learn about video phase in order to
|
||||
known _when_ to collect audio.
|
||||
|
||||
The struct below captures that information.
|
||||
*/
|
||||
struct AudioPhase {
|
||||
// A phase of 0 = collect audio now. This should increment with the CPU clock.
|
||||
Numeric::CircularCounter<unsigned, line_length.as<int>()> sample_phase;
|
||||
|
||||
// A phase of 0 = reset audio collection address. This should increment each time
|
||||
// sample phase wraps around.
|
||||
Numeric::CircularCounter<unsigned, number_of_lines> buffer_phase;
|
||||
};
|
||||
|
||||
/*!
|
||||
Models the 68000-era Macintosh video hardware, producing a 512x348 pixel image,
|
||||
within a total scanning area of 370 lines, at 352 cycles per line.
|
||||
@@ -29,11 +47,7 @@ constexpr int sync_end = 38;
|
||||
*/
|
||||
class Video {
|
||||
public:
|
||||
/*!
|
||||
Constructs an instance of @c Video sourcing its pixel data from @c ram and
|
||||
providing audio and drive-speed bytes to @c audio and @c drive_speed_accumulator.
|
||||
*/
|
||||
Video(DeferredAudio &audio, DriveSpeedAccumulator &drive_speed_accumulator);
|
||||
Video();
|
||||
|
||||
/*!
|
||||
Sets the target device for video data.
|
||||
@@ -46,12 +60,17 @@ public:
|
||||
/*!
|
||||
Produces the next @c duration period of pixels.
|
||||
*/
|
||||
void run_for(HalfCycles duration);
|
||||
void run_for(HalfCycles);
|
||||
|
||||
/*!
|
||||
@returns The current audio phase.
|
||||
*/
|
||||
AudioPhase audio_phase() const;
|
||||
|
||||
/*!
|
||||
Sets whether the alternate screen and/or audio buffers should be used to source data.
|
||||
*/
|
||||
void set_use_alternate_buffers(bool use_alternate_screen_buffer, bool use_alternate_audio_buffer);
|
||||
void set_use_alternate_buffer(bool);
|
||||
|
||||
/*!
|
||||
Provides a base address and a mask indicating which parts of the generated video and audio/drive addresses are
|
||||
@@ -82,9 +101,6 @@ public:
|
||||
HalfCycles next_sequence_point();
|
||||
|
||||
private:
|
||||
DeferredAudio &audio_;
|
||||
DriveSpeedAccumulator &drive_speed_accumulator_;
|
||||
|
||||
Outputs::CRT::CRT crt_;
|
||||
const uint16_t *ram_ = nullptr;
|
||||
uint32_t ram_mask_ = 0;
|
||||
@@ -92,12 +108,10 @@ private:
|
||||
HalfCycles frame_position_;
|
||||
|
||||
size_t video_address_ = 0;
|
||||
size_t audio_address_ = 0;
|
||||
|
||||
uint64_t *pixel_buffer_ = nullptr;
|
||||
|
||||
bool use_alternate_screen_buffer_ = false;
|
||||
bool use_alternate_audio_buffer_ = false;
|
||||
bool use_alternate_buffer_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ constexpr bool is_power_of_two(const IntT value) {
|
||||
return !(value & (value - 1));
|
||||
}
|
||||
|
||||
template <typename IntT, IntT limit>
|
||||
template <std::unsigned_integral IntT, IntT limit>
|
||||
class CircularCounter {
|
||||
public:
|
||||
constexpr CircularCounter() noexcept = default;
|
||||
@@ -26,6 +26,33 @@ public:
|
||||
assert(value < limit);
|
||||
}
|
||||
|
||||
/*!
|
||||
Advances the counter by @c step units.
|
||||
@returns The number of times the counter wrapped around.
|
||||
*/
|
||||
int advance_divide(const IntT step) {
|
||||
int result{};
|
||||
|
||||
value_ += step;
|
||||
if constexpr (is_power_of_two(limit)) {
|
||||
result = value_ >> log2(limit);
|
||||
value_ &= limit - 1;
|
||||
} else {
|
||||
while(value_ >= limit) [[unlikely]] {
|
||||
++result;
|
||||
value_ -= limit;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @returns The number of ticks remaining until the counter next rolls over.
|
||||
IntT remaining() const {
|
||||
return limit - value_;
|
||||
}
|
||||
|
||||
/// Steps the counter once, possibly rolling over. Cheaper than an @c advance_divide.
|
||||
CircularCounter &operator ++() {
|
||||
++value_;
|
||||
|
||||
|
||||
@@ -2405,7 +2405,6 @@
|
||||
4BB2A9AE1E13367E001A5C23 /* CRCTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CRCTests.mm; sourceTree = "<group>"; };
|
||||
4BB307B9235001C300457D33 /* 6850.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 6850.hpp; sourceTree = "<group>"; };
|
||||
4BB307BA235001C300457D33 /* 6850.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 6850.cpp; sourceTree = "<group>"; };
|
||||
4BB4BFAA22A300710069048D /* DeferredAudio.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DeferredAudio.hpp; sourceTree = "<group>"; };
|
||||
4BB4BFAB22A33D710069048D /* DriveSpeedAccumulator.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DriveSpeedAccumulator.hpp; sourceTree = "<group>"; };
|
||||
4BB4BFAC22A33DE50069048D /* DriveSpeedAccumulator.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = DriveSpeedAccumulator.cpp; sourceTree = "<group>"; };
|
||||
4BB4BFAE22A42F290069048D /* MacintoshIMG.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MacintoshIMG.cpp; sourceTree = "<group>"; };
|
||||
@@ -5580,7 +5579,6 @@
|
||||
4BCE0058227CFFCA000CA200 /* Macintosh.cpp */,
|
||||
4BCE005E227D39AB000CA200 /* Video.cpp */,
|
||||
4B9378E322A199C600973513 /* Audio.hpp */,
|
||||
4BB4BFAA22A300710069048D /* DeferredAudio.hpp */,
|
||||
4BB4BFAB22A33D710069048D /* DriveSpeedAccumulator.hpp */,
|
||||
4BDB3D8522833321002D3CEE /* Keyboard.hpp */,
|
||||
4BCE0059227CFFCA000CA200 /* Macintosh.hpp */,
|
||||
|
||||
Reference in new issue
Block a user