2016-06-01 01:23:44 +00:00
|
|
|
//
|
|
|
|
// CRTMachine.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 31/05/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-06-01 01:23:44 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef CRTMachine_hpp
|
|
|
|
#define CRTMachine_hpp
|
|
|
|
|
2018-11-04 01:54:25 +00:00
|
|
|
#include "../Outputs/ScanTarget.hpp"
|
2017-12-18 02:26:06 +00:00
|
|
|
#include "../Outputs/Speaker/Speaker.hpp"
|
2017-07-26 00:20:55 +00:00
|
|
|
#include "../ClockReceiver/ClockReceiver.hpp"
|
2018-03-22 02:18:13 +00:00
|
|
|
#include "../ClockReceiver/TimeTypes.hpp"
|
2017-11-08 02:19:51 +00:00
|
|
|
#include "ROMMachine.hpp"
|
2016-06-01 01:23:44 +00:00
|
|
|
|
2018-04-01 02:14:34 +00:00
|
|
|
#include "../Configurable/StandardOptions.hpp"
|
|
|
|
|
2020-01-24 01:12:44 +00:00
|
|
|
#include <array>
|
2018-03-22 02:18:13 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-11-04 01:54:25 +00:00
|
|
|
// TODO: rename.
|
2016-06-01 01:23:44 +00:00
|
|
|
namespace CRTMachine {
|
|
|
|
|
2016-06-30 12:46:29 +00:00
|
|
|
/*!
|
|
|
|
A CRTMachine::Machine is a mostly-abstract base class for machines that connect to a CRT,
|
|
|
|
that optionally provide a speaker, and that nominate a clock rate and can announce to a delegate
|
|
|
|
should that clock rate change.
|
|
|
|
*/
|
2018-07-11 00:00:46 +00:00
|
|
|
class Machine {
|
2016-06-01 01:23:44 +00:00
|
|
|
public:
|
2017-07-22 21:31:12 +00:00
|
|
|
/*!
|
2018-11-04 01:54:25 +00:00
|
|
|
Causes the machine to set up its display and, if it has one, speaker.
|
2017-07-22 21:31:12 +00:00
|
|
|
|
2018-11-04 01:54:25 +00:00
|
|
|
The @c scan_target will receive all video output; the caller guarantees
|
|
|
|
that it is non-null.
|
2017-07-22 21:31:12 +00:00
|
|
|
*/
|
2018-11-15 02:52:57 +00:00
|
|
|
virtual void set_scan_target(Outputs::Display::ScanTarget *scan_target) = 0;
|
2017-07-22 21:31:12 +00:00
|
|
|
|
2020-01-21 02:45:10 +00:00
|
|
|
/*!
|
|
|
|
@returns The current scan status.
|
|
|
|
*/
|
2020-01-22 03:28:25 +00:00
|
|
|
virtual Outputs::Display::ScanStatus get_scan_status() const {
|
|
|
|
return get_scaled_scan_status() / float(clock_rate_);
|
|
|
|
}
|
2020-01-21 02:45:10 +00:00
|
|
|
|
2017-07-22 21:31:12 +00:00
|
|
|
/// @returns The speaker that receives this machine's output, or @c nullptr if this machine is mute.
|
2017-12-18 02:26:06 +00:00
|
|
|
virtual Outputs::Speaker::Speaker *get_speaker() = 0;
|
2016-06-01 01:23:44 +00:00
|
|
|
|
2018-02-01 12:53:52 +00:00
|
|
|
/// @returns The confidence that this machine is running content it understands.
|
|
|
|
virtual float get_confidence() { return 0.5f; }
|
2019-03-02 23:07:05 +00:00
|
|
|
virtual std::string debug_type() { return ""; }
|
2018-02-01 12:53:52 +00:00
|
|
|
|
2018-03-22 02:18:13 +00:00
|
|
|
/// Runs the machine for @c duration seconds.
|
2020-01-20 17:12:23 +00:00
|
|
|
virtual void run_for(Time::Seconds duration) {
|
2020-01-26 18:25:23 +00:00
|
|
|
const double cycles = (duration * clock_rate_ * speed_multiplier_) + clock_conversion_error_;
|
2018-03-22 02:18:13 +00:00
|
|
|
clock_conversion_error_ = std::fmod(cycles, 1.0);
|
|
|
|
run_for(Cycles(static_cast<int>(cycles)));
|
2016-08-14 17:33:20 +00:00
|
|
|
}
|
2018-03-22 02:18:13 +00:00
|
|
|
|
2020-01-26 18:25:23 +00:00
|
|
|
/*!
|
|
|
|
Sets a speed multiplier to apply to this machine; e.g. a multiplier of 1.5 will cause the
|
|
|
|
emulated machine to run 50% faster than a real machine. This speed-up is an emulation
|
|
|
|
fiction: it will apply across the system, including to the CRT.
|
|
|
|
*/
|
|
|
|
virtual void set_speed_multiplier(double multiplier) {
|
|
|
|
speed_multiplier_ = multiplier;
|
|
|
|
auto speaker = get_speaker();
|
|
|
|
if(speaker) {
|
|
|
|
speaker->set_input_rate_multiplier(float(multiplier));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns The current speed multiplier.
|
|
|
|
*/
|
|
|
|
virtual double get_speed_multiplier() {
|
|
|
|
return speed_multiplier_;
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:12:23 +00:00
|
|
|
/*!
|
|
|
|
Runs for the machine for at least @c duration seconds, and then until @c condition is true.
|
|
|
|
|
|
|
|
@returns The amount of time run for.
|
|
|
|
*/
|
|
|
|
Time::Seconds run_until(Time::Seconds minimum_duration, std::function<bool()> condition) {
|
|
|
|
Time::Seconds total_runtime = minimum_duration;
|
2020-01-20 04:52:47 +00:00
|
|
|
run_for(minimum_duration);
|
|
|
|
while(!condition()) {
|
2020-01-20 17:12:23 +00:00
|
|
|
// Advance in increments of one 500th of a second until the condition
|
|
|
|
// is true; that's 1/10th of a 50Hz frame, but more like 1/8.33 of a
|
|
|
|
// 60Hz frame. Though most machines aren't exactly 50Hz or 60Hz, and some
|
|
|
|
// are arbitrary other refresh rates. So those observations are merely
|
|
|
|
// for scale.
|
2020-01-20 04:52:47 +00:00
|
|
|
run_for(0.002);
|
2020-01-20 17:12:23 +00:00
|
|
|
total_runtime += 0.002;
|
|
|
|
}
|
|
|
|
return total_runtime;
|
|
|
|
}
|
|
|
|
|
2020-01-20 21:08:21 +00:00
|
|
|
enum MachineEvent: int {
|
2020-01-20 17:12:23 +00:00
|
|
|
/// At least one new packet of audio has been delivered to the spaker's delegate.
|
2020-01-23 03:20:56 +00:00
|
|
|
NewSpeakerSamplesGenerated = 1 << 0,
|
|
|
|
|
|
|
|
/// The next vertical retrace has begun.
|
|
|
|
VerticalSync = 1 << 1,
|
2020-01-20 17:12:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
2020-01-20 21:08:21 +00:00
|
|
|
Runs for at least @c duration seconds, and then every one of the @c events has occurred at least once since this
|
2020-01-20 17:12:23 +00:00
|
|
|
call to @c run_until_event.
|
|
|
|
|
2020-01-20 21:08:21 +00:00
|
|
|
@param events A bitmask comprised of @c MachineEvent flags.
|
2020-01-20 17:12:23 +00:00
|
|
|
@returns The amount of time run for.
|
|
|
|
*/
|
2020-01-20 21:08:21 +00:00
|
|
|
Time::Seconds run_until(Time::Seconds minimum_duration, int events) {
|
|
|
|
// Tie up a wait-for-samples, if requested.
|
|
|
|
const Outputs::Speaker::Speaker *speaker = nullptr;
|
|
|
|
int sample_sets = 0;
|
|
|
|
if(events & MachineEvent::NewSpeakerSamplesGenerated) {
|
|
|
|
speaker = get_speaker();
|
|
|
|
if(!speaker) events &= ~MachineEvent::NewSpeakerSamplesGenerated;
|
|
|
|
sample_sets = speaker->completed_sample_sets();
|
2020-01-20 04:52:47 +00:00
|
|
|
}
|
2020-01-20 21:08:21 +00:00
|
|
|
|
2020-01-23 03:20:56 +00:00
|
|
|
int retraces = 0;
|
|
|
|
if(events & MachineEvent::VerticalSync) {
|
|
|
|
retraces = get_scan_status().hsync_count;
|
|
|
|
}
|
|
|
|
|
2020-01-20 21:08:21 +00:00
|
|
|
// Run until all requested events are satisfied.
|
|
|
|
return run_until(minimum_duration, [=]() {
|
|
|
|
return
|
2020-01-23 03:20:56 +00:00
|
|
|
(!(events & MachineEvent::NewSpeakerSamplesGenerated) || (sample_sets != speaker->completed_sample_sets())) &&
|
|
|
|
(!(events & MachineEvent::VerticalSync) || (retraces != get_scan_status().hsync_count));
|
2020-01-20 21:08:21 +00:00
|
|
|
});
|
2020-01-20 04:52:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:39:46 +00:00
|
|
|
protected:
|
2018-03-22 02:18:13 +00:00
|
|
|
/// Runs the machine for @c cycles.
|
|
|
|
virtual void run_for(const Cycles cycles) = 0;
|
2016-08-14 17:33:20 +00:00
|
|
|
void set_clock_rate(double clock_rate) {
|
2018-03-22 02:18:13 +00:00
|
|
|
clock_rate_ = clock_rate;
|
2016-09-13 02:06:03 +00:00
|
|
|
}
|
2018-03-22 14:01:18 +00:00
|
|
|
double get_clock_rate() {
|
|
|
|
return clock_rate_;
|
|
|
|
}
|
2016-08-14 17:33:20 +00:00
|
|
|
|
2020-01-22 03:28:25 +00:00
|
|
|
virtual Outputs::Display::ScanStatus get_scaled_scan_status() const {
|
|
|
|
// This deliberately sets up an infinite loop if the user hasn't
|
|
|
|
// overridden at least one of this or get_scan_status.
|
|
|
|
//
|
|
|
|
// Most likely you want to override this, and let the base class
|
|
|
|
// throw in a divide-by-clock-rate at the end for you.
|
|
|
|
return get_scan_status();
|
|
|
|
}
|
|
|
|
|
2018-04-04 23:14:42 +00:00
|
|
|
/*!
|
2018-11-04 01:54:25 +00:00
|
|
|
Maps from Configurable::Display to Outputs::Display::VideoSignal and calls
|
2018-11-29 01:53:33 +00:00
|
|
|
@c set_display_type with the result.
|
2018-04-04 23:14:42 +00:00
|
|
|
*/
|
2018-04-01 02:14:34 +00:00
|
|
|
void set_video_signal_configurable(Configurable::Display type) {
|
2018-11-29 01:53:33 +00:00
|
|
|
Outputs::Display::DisplayType display_type;
|
2018-04-01 02:14:34 +00:00
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
case Configurable::Display::RGB:
|
2018-11-29 01:53:33 +00:00
|
|
|
display_type = Outputs::Display::DisplayType::RGB;
|
2018-04-01 02:14:34 +00:00
|
|
|
break;
|
|
|
|
case Configurable::Display::SVideo:
|
2018-11-29 01:53:33 +00:00
|
|
|
display_type = Outputs::Display::DisplayType::SVideo;
|
2018-04-01 02:14:34 +00:00
|
|
|
break;
|
2018-11-29 01:53:33 +00:00
|
|
|
case Configurable::Display::CompositeColour:
|
|
|
|
display_type = Outputs::Display::DisplayType::CompositeColour;
|
|
|
|
break;
|
|
|
|
case Configurable::Display::CompositeMonochrome:
|
|
|
|
display_type = Outputs::Display::DisplayType::CompositeMonochrome;
|
2018-04-01 02:14:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-29 01:53:33 +00:00
|
|
|
set_display_type(display_type);
|
2018-04-04 23:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2018-11-29 01:53:33 +00:00
|
|
|
Forwards the video signal to the target returned by get_crt().
|
2018-04-04 23:14:42 +00:00
|
|
|
*/
|
2018-11-29 01:53:33 +00:00
|
|
|
virtual void set_display_type(Outputs::Display::DisplayType display_type) {}
|
2018-04-01 02:14:34 +00:00
|
|
|
|
2020-01-24 01:12:44 +00:00
|
|
|
|
2016-08-14 17:33:20 +00:00
|
|
|
private:
|
2017-11-11 03:08:40 +00:00
|
|
|
double clock_rate_ = 1.0;
|
2018-03-22 02:18:13 +00:00
|
|
|
double clock_conversion_error_ = 0.0;
|
2020-01-26 18:25:23 +00:00
|
|
|
double speed_multiplier_ = 1.0;
|
2016-06-01 01:23:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CRTMachine_hpp */
|