2021-03-18 03:38:55 +00:00
|
|
|
|
//
|
|
|
|
|
// ZXSpectrum.cpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 17/03/2021.
|
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "ZXSpectrum.hpp"
|
|
|
|
|
|
2021-04-25 17:00:43 +00:00
|
|
|
|
#include "State.hpp"
|
2021-03-18 16:47:48 +00:00
|
|
|
|
#include "Video.hpp"
|
2021-04-25 17:00:43 +00:00
|
|
|
|
#include "../Keyboard/Keyboard.hpp"
|
2021-03-18 16:14:48 +00:00
|
|
|
|
|
2021-03-23 14:32:22 +00:00
|
|
|
|
#include "../../../Activity/Source.hpp"
|
2021-03-18 03:38:55 +00:00
|
|
|
|
#include "../../MachineTypes.hpp"
|
|
|
|
|
|
2021-03-18 14:43:51 +00:00
|
|
|
|
#include "../../../Processors/Z80/Z80.hpp"
|
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
#include "../../../Components/AudioToggle/AudioToggle.hpp"
|
|
|
|
|
#include "../../../Components/AY38910/AY38910.hpp"
|
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// TODO: possibly there's a better factoring than this, but for now
|
|
|
|
|
// just grab the CPC's version of an FDC.
|
|
|
|
|
#include "../../AmstradCPC/FDC.hpp"
|
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
#include "../../../Outputs/Log.hpp"
|
2021-04-25 17:00:43 +00:00
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
#include "../../../Outputs/Speaker/Implementation/CompoundSource.hpp"
|
|
|
|
|
#include "../../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
2024-02-09 19:25:40 +00:00
|
|
|
|
#include "../../../Outputs/Speaker/Implementation/BufferSource.hpp"
|
2021-03-18 16:14:48 +00:00
|
|
|
|
|
2021-03-22 23:04:38 +00:00
|
|
|
|
#include "../../../Storage/Tape/Tape.hpp"
|
|
|
|
|
#include "../../../Storage/Tape/Parsers/Spectrum.hpp"
|
|
|
|
|
|
2021-03-18 14:18:17 +00:00
|
|
|
|
#include "../../../Analyser/Static/ZXSpectrum/Target.hpp"
|
2021-03-18 03:38:55 +00:00
|
|
|
|
|
2021-03-19 03:07:51 +00:00
|
|
|
|
#include "../../Utility/MemoryFuzzer.hpp"
|
2021-03-23 14:44:43 +00:00
|
|
|
|
#include "../../Utility/Typer.hpp"
|
2021-03-19 03:07:51 +00:00
|
|
|
|
|
2021-03-18 16:47:48 +00:00
|
|
|
|
#include "../../../ClockReceiver/JustInTime.hpp"
|
|
|
|
|
|
2021-03-19 03:51:21 +00:00
|
|
|
|
#include <array>
|
2021-03-18 14:18:17 +00:00
|
|
|
|
|
2021-04-29 00:19:01 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Provides a simultaneous Kempston and Interface 2-style joystick.
|
|
|
|
|
*/
|
|
|
|
|
class Joystick: public Inputs::ConcreteJoystick {
|
|
|
|
|
public:
|
|
|
|
|
Joystick() :
|
|
|
|
|
ConcreteJoystick({
|
|
|
|
|
Input(Input::Up),
|
|
|
|
|
Input(Input::Down),
|
|
|
|
|
Input(Input::Left),
|
|
|
|
|
Input(Input::Right),
|
|
|
|
|
Input(Input::Fire)
|
|
|
|
|
}) {}
|
|
|
|
|
|
|
|
|
|
void did_set_input(const Input &digital_input, bool is_active) final {
|
2024-01-16 19:26:55 +00:00
|
|
|
|
const auto apply_kempston = [&](uint8_t mask) {
|
|
|
|
|
if(is_active) kempston_ |= mask; else kempston_ &= ~mask;
|
|
|
|
|
};
|
|
|
|
|
const auto apply_sinclair = [&](uint16_t mask) {
|
|
|
|
|
if(is_active) sinclair_ &= ~mask; else sinclair_ |= mask;
|
|
|
|
|
};
|
2021-04-29 00:19:01 +00:00
|
|
|
|
|
|
|
|
|
switch(digital_input.type) {
|
|
|
|
|
default: return;
|
|
|
|
|
|
|
|
|
|
case Input::Right:
|
2024-01-16 19:26:55 +00:00
|
|
|
|
apply_kempston(0x01);
|
|
|
|
|
apply_sinclair(0x0208);
|
2021-04-29 00:19:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case Input::Left:
|
2024-01-16 19:26:55 +00:00
|
|
|
|
apply_kempston(0x02);
|
|
|
|
|
apply_sinclair(0x0110);
|
2021-04-29 00:19:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case Input::Down:
|
2024-01-16 19:26:55 +00:00
|
|
|
|
apply_kempston(0x04);
|
|
|
|
|
apply_sinclair(0x0404);
|
2021-04-29 00:19:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case Input::Up:
|
2024-01-16 19:26:55 +00:00
|
|
|
|
apply_kempston(0x08);
|
|
|
|
|
apply_sinclair(0x0802);
|
2021-04-29 00:19:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case Input::Fire:
|
2024-01-16 19:26:55 +00:00
|
|
|
|
apply_kempston(0x10);
|
|
|
|
|
apply_sinclair(0x1001);
|
2021-04-29 00:19:01 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @returns The value that a Kempston joystick interface would report if this joystick
|
|
|
|
|
/// were plugged into it.
|
|
|
|
|
uint8_t get_kempston() {
|
|
|
|
|
return kempston_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @returns The value that a Sinclair interface would report if this joystick
|
|
|
|
|
/// were plugged into it via @c port (which should be either 0 or 1, for ports 1 or 2).
|
|
|
|
|
uint8_t get_sinclair(int port) {
|
|
|
|
|
return uint8_t(sinclair_ >> (port * 8));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t kempston_ = 0x00;
|
|
|
|
|
uint16_t sinclair_ = 0xffff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 14:18:17 +00:00
|
|
|
|
namespace Sinclair {
|
|
|
|
|
namespace ZXSpectrum {
|
|
|
|
|
|
|
|
|
|
using Model = Analyser::Static::ZXSpectrum::Target::Model;
|
2021-03-23 14:44:43 +00:00
|
|
|
|
using CharacterMapper = Sinclair::ZX::Keyboard::CharacterMapper;
|
|
|
|
|
|
2021-03-18 14:18:17 +00:00
|
|
|
|
template<Model model> class ConcreteMachine:
|
2021-03-23 14:32:22 +00:00
|
|
|
|
public Activity::Source,
|
2023-05-12 18:14:45 +00:00
|
|
|
|
public ClockingHint::Observer,
|
2021-03-20 02:17:20 +00:00
|
|
|
|
public Configurable::Device,
|
2021-03-23 14:44:43 +00:00
|
|
|
|
public CPU::Z80::BusHandler,
|
2021-03-18 14:43:51 +00:00
|
|
|
|
public Machine,
|
2021-03-20 03:33:46 +00:00
|
|
|
|
public MachineTypes::AudioProducer,
|
2021-04-29 00:19:01 +00:00
|
|
|
|
public MachineTypes::JoystickMachine,
|
2021-03-19 03:51:21 +00:00
|
|
|
|
public MachineTypes::MappedKeyboardMachine,
|
2021-03-19 15:12:50 +00:00
|
|
|
|
public MachineTypes::MediaTarget,
|
2021-03-18 14:18:17 +00:00
|
|
|
|
public MachineTypes::ScanProducer,
|
|
|
|
|
public MachineTypes::TimedMachine,
|
2021-03-23 14:44:43 +00:00
|
|
|
|
public Utility::TypeRecipient<CharacterMapper> {
|
2021-03-18 14:18:17 +00:00
|
|
|
|
public:
|
2021-03-18 14:43:51 +00:00
|
|
|
|
ConcreteMachine(const Analyser::Static::ZXSpectrum::Target &target, const ROMMachine::ROMFetcher &rom_fetcher) :
|
2021-03-23 14:44:43 +00:00
|
|
|
|
Utility::TypeRecipient<CharacterMapper>(Sinclair::ZX::Keyboard::Machine::ZXSpectrum),
|
2021-03-18 16:14:48 +00:00
|
|
|
|
z80_(*this),
|
|
|
|
|
ay_(GI::AY38910::Personality::AY38910, audio_queue_),
|
|
|
|
|
audio_toggle_(audio_queue_),
|
|
|
|
|
mixer_(ay_, audio_toggle_),
|
2021-03-19 03:51:21 +00:00
|
|
|
|
speaker_(mixer_),
|
2021-03-19 14:36:08 +00:00
|
|
|
|
keyboard_(Sinclair::ZX::Keyboard::Machine::ZXSpectrum),
|
2021-03-19 15:12:50 +00:00
|
|
|
|
keyboard_mapper_(Sinclair::ZX::Keyboard::Machine::ZXSpectrum),
|
2021-03-22 23:36:05 +00:00
|
|
|
|
tape_player_(clock_rate() * 2),
|
|
|
|
|
fdc_(clock_rate() * 2)
|
2021-03-18 14:18:17 +00:00
|
|
|
|
{
|
2021-03-18 16:41:24 +00:00
|
|
|
|
set_clock_rate(clock_rate());
|
|
|
|
|
speaker_.set_input_rate(float(clock_rate()) / 2.0f);
|
2021-03-18 14:18:17 +00:00
|
|
|
|
|
2021-06-04 01:55:59 +00:00
|
|
|
|
ROM::Name rom_name;
|
2021-04-15 01:37:10 +00:00
|
|
|
|
switch(model) {
|
|
|
|
|
case Model::SixteenK:
|
2021-06-04 01:55:59 +00:00
|
|
|
|
case Model::FortyEightK: rom_name = ROM::Name::Spectrum48k; break;
|
|
|
|
|
case Model::OneTwoEightK: rom_name = ROM::Name::Spectrum128k; break;
|
|
|
|
|
case Model::Plus2: rom_name = ROM::Name::SpecrumPlus2; break;
|
2021-04-15 01:37:10 +00:00
|
|
|
|
case Model::Plus2a:
|
2023-05-12 18:14:45 +00:00
|
|
|
|
case Model::Plus3: rom_name = ROM::Name::SpectrumPlus3; break;
|
2021-06-04 01:55:59 +00:00
|
|
|
|
// TODO: possibly accept the +3 ROM in multiple parts?
|
2021-04-15 01:37:10 +00:00
|
|
|
|
}
|
2021-06-04 01:55:59 +00:00
|
|
|
|
const auto request = ROM::Request(rom_name);
|
2021-06-04 22:54:50 +00:00
|
|
|
|
auto roms = rom_fetcher(request);
|
2021-06-04 01:55:59 +00:00
|
|
|
|
if(!request.validate(roms)) {
|
|
|
|
|
throw ROMMachine::Error::MissingROMs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto &rom = roms.find(rom_name)->second;
|
|
|
|
|
memcpy(rom_.data(), rom.data(), std::min(rom_.size(), rom.size()));
|
2021-03-18 14:18:17 +00:00
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// Register for sleeping notifications.
|
|
|
|
|
tape_player_.set_clocking_hint_observer(this);
|
|
|
|
|
|
2021-04-29 00:19:01 +00:00
|
|
|
|
// Attach a couple of joysticks.
|
|
|
|
|
joysticks_.emplace_back(new Joystick);
|
|
|
|
|
joysticks_.emplace_back(new Joystick);
|
|
|
|
|
|
2021-03-18 14:43:51 +00:00
|
|
|
|
// Set up initial memory map.
|
|
|
|
|
update_memory_map();
|
2021-03-22 00:23:00 +00:00
|
|
|
|
set_video_address();
|
2021-03-19 03:07:51 +00:00
|
|
|
|
Memory::Fuzz(ram_);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
2021-03-19 15:12:50 +00:00
|
|
|
|
// Insert media.
|
|
|
|
|
insert_media(target.media);
|
2021-03-23 00:12:03 +00:00
|
|
|
|
|
|
|
|
|
// Possibly depress the enter key.
|
|
|
|
|
if(target.should_hold_enter) {
|
|
|
|
|
// Hold it for five seconds, more or less.
|
|
|
|
|
duration_to_press_enter_ = Cycles(5 * clock_rate());
|
|
|
|
|
keyboard_.set_key_state(ZX::Keyboard::KeyEnter, true);
|
|
|
|
|
}
|
2021-04-25 17:00:43 +00:00
|
|
|
|
|
|
|
|
|
// Install state if supplied.
|
|
|
|
|
if(target.state) {
|
2021-04-25 17:03:24 +00:00
|
|
|
|
const auto state = static_cast<State *>(target.state.get());
|
|
|
|
|
state->z80.apply(z80_);
|
2021-04-25 18:16:35 +00:00
|
|
|
|
state->video.apply(*video_.last_valid());
|
2021-04-26 21:39:11 +00:00
|
|
|
|
state->ay.apply(ay_);
|
2021-04-25 17:27:11 +00:00
|
|
|
|
|
|
|
|
|
// If this is a 48k or 16k machine, remap source data from its original
|
|
|
|
|
// linear form to whatever the banks end up being; otherwise copy as is.
|
|
|
|
|
if(model <= Model::FortyEightK) {
|
2021-04-25 20:51:07 +00:00
|
|
|
|
const size_t num_banks = std::min(size_t(48*1024), state->ram.size()) >> 14;
|
|
|
|
|
for(size_t c = 0; c < num_banks; c++) {
|
2024-01-17 14:44:07 +00:00
|
|
|
|
memcpy(&banks_[c + 1].write[(c+1) * 0x4000], &state->ram[c * 0x4000], 0x4000);
|
2021-04-25 17:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
memcpy(ram_.data(), state->ram.data(), std::min(ram_.size(), state->ram.size()));
|
2021-04-26 00:46:49 +00:00
|
|
|
|
|
|
|
|
|
port1ffd_ = state->last_1ffd;
|
|
|
|
|
port7ffd_ = state->last_7ffd;
|
|
|
|
|
update_memory_map();
|
2021-04-25 17:27:11 +00:00
|
|
|
|
}
|
2021-04-25 17:00:43 +00:00
|
|
|
|
}
|
2021-03-18 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:23:54 +00:00
|
|
|
|
~ConcreteMachine() {
|
|
|
|
|
audio_queue_.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:41:24 +00:00
|
|
|
|
static constexpr unsigned int clock_rate() {
|
2021-04-15 01:37:10 +00:00
|
|
|
|
constexpr unsigned int OriginalClockRate = 3'500'000;
|
2021-03-20 15:19:44 +00:00
|
|
|
|
constexpr unsigned int Plus3ClockRate = 3'546'875; // See notes below; this is a guess.
|
|
|
|
|
|
|
|
|
|
// Notes on timing for the +2a and +3:
|
|
|
|
|
//
|
|
|
|
|
// Standard PAL produces 283.7516 colour cycles per line, each line being 64µs.
|
|
|
|
|
// The oft-quoted 3.5469 Mhz would seem to imply 227.0016 clock cycles per line.
|
|
|
|
|
// Since those Spectrums actually produce 228 cycles per line, but software like
|
|
|
|
|
// Chromatrons seems to assume a fixed phase relationship, I guess that the real
|
|
|
|
|
// clock speed is whatever gives:
|
|
|
|
|
//
|
|
|
|
|
// 228 / [cycles per line] * 283.7516 = [an integer].
|
|
|
|
|
//
|
|
|
|
|
// i.e. 228 * 283.7516 = [an integer] * [cycles per line], such that cycles per line ~= 227
|
|
|
|
|
// ... which would imply that 'an integer' is probably 285, i.e.
|
|
|
|
|
//
|
|
|
|
|
// 228 / [cycles per line] * 283.7516 = 285
|
|
|
|
|
// => 227.00128 = [cycles per line]
|
|
|
|
|
// => clock rate = 3.546895 Mhz?
|
|
|
|
|
//
|
|
|
|
|
// That is... unless I'm mistaken about the PAL colour subcarrier and it's actually 283.75,
|
|
|
|
|
// which would give exactly 227 cycles/line and therefore 3.546875 Mhz.
|
|
|
|
|
//
|
|
|
|
|
// A real TV would be likely to accept either, I guess. But it does seem like
|
|
|
|
|
// the Spectrum is a PAL machine with a fixed colour phase relationship. For
|
|
|
|
|
// this emulator's world, that's a first!
|
|
|
|
|
|
2021-04-15 01:37:10 +00:00
|
|
|
|
return model < Model::OneTwoEightK ? OriginalClockRate : Plus3ClockRate;
|
2021-03-18 16:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - TimedMachine.
|
2021-03-18 14:18:17 +00:00
|
|
|
|
|
|
|
|
|
void run_for(const Cycles cycles) override {
|
2021-03-18 14:43:51 +00:00
|
|
|
|
z80_.run_for(cycles);
|
2021-03-23 00:12:03 +00:00
|
|
|
|
|
|
|
|
|
// Use this very broad timing base for the automatic enter depression.
|
|
|
|
|
// It's not worth polluting the main loop.
|
|
|
|
|
if(duration_to_press_enter_ > Cycles(0)) {
|
|
|
|
|
if(duration_to_press_enter_ < cycles) {
|
|
|
|
|
duration_to_press_enter_ = Cycles(0);
|
|
|
|
|
keyboard_.set_key_state(ZX::Keyboard::KeyEnter, false);
|
|
|
|
|
} else {
|
|
|
|
|
duration_to_press_enter_ -= cycles;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-09 17:33:46 +00:00
|
|
|
|
void flush_output(int outputs) override {
|
|
|
|
|
if(outputs & Output::Video) {
|
2022-07-08 20:04:32 +00:00
|
|
|
|
video_.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-09 17:33:46 +00:00
|
|
|
|
if(outputs & Output::Audio) {
|
2022-07-08 20:04:32 +00:00
|
|
|
|
update_audio();
|
|
|
|
|
audio_queue_.perform();
|
|
|
|
|
}
|
2021-03-22 23:36:05 +00:00
|
|
|
|
|
|
|
|
|
if constexpr (model == Model::Plus3) {
|
|
|
|
|
fdc_.flush();
|
|
|
|
|
}
|
2021-03-18 16:23:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - ScanProducer.
|
2021-03-18 14:18:17 +00:00
|
|
|
|
|
2021-03-20 02:17:20 +00:00
|
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target) override {
|
2021-03-19 01:54:42 +00:00
|
|
|
|
video_->set_scan_target(scan_target);
|
2021-03-18 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 02:17:20 +00:00
|
|
|
|
Outputs::Display::ScanStatus get_scaled_scan_status() const override {
|
2021-03-19 01:54:42 +00:00
|
|
|
|
return video_->get_scaled_scan_status();
|
2021-03-18 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 02:17:20 +00:00
|
|
|
|
void set_display_type(Outputs::Display::DisplayType display_type) override {
|
|
|
|
|
video_->set_display_type(display_type);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 01:16:22 +00:00
|
|
|
|
Outputs::Display::DisplayType get_display_type() const override {
|
|
|
|
|
return video_->get_display_type();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - BusHandler.
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
|
|
|
|
forceinline HalfCycles perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
2021-03-22 03:04:20 +00:00
|
|
|
|
using PartialMachineCycle = CPU::Z80::PartialMachineCycle;
|
2021-03-18 16:14:48 +00:00
|
|
|
|
|
2021-03-22 03:04:20 +00:00
|
|
|
|
const uint16_t address = cycle.address ? *cycle.address : 0x0000;
|
2021-04-04 23:52:38 +00:00
|
|
|
|
|
|
|
|
|
// Apply contention if necessary.
|
2021-04-15 01:37:10 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
2021-04-15 22:04:16 +00:00
|
|
|
|
// Model applied: the trigger for the ULA inserting a delay is the falling edge
|
|
|
|
|
// of MREQ, which is always half a cycle into a read or write.
|
2021-04-15 01:37:10 +00:00
|
|
|
|
if(
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[address >> 14].is_contended &&
|
2021-04-15 01:37:10 +00:00
|
|
|
|
cycle.operation >= PartialMachineCycle::ReadOpcodeStart &&
|
|
|
|
|
cycle.operation <= PartialMachineCycle::WriteStart) {
|
|
|
|
|
|
2021-04-21 23:18:07 +00:00
|
|
|
|
const auto delay = video_.last_valid()->access_delay(video_.time_since_flush());
|
2021-04-15 01:37:10 +00:00
|
|
|
|
advance(cycle.length + delay);
|
|
|
|
|
return delay;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-04-15 22:04:16 +00:00
|
|
|
|
switch(cycle.operation) {
|
2021-04-18 15:56:00 +00:00
|
|
|
|
case CPU::Z80::PartialMachineCycle::Input:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Output:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Read:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Write:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::ReadOpcode:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Interrupt:
|
|
|
|
|
// For these, carry on into the actual handler, below.
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// For anything else that isn't listed below, just advance
|
|
|
|
|
// time and conclude here.
|
2021-04-15 22:04:16 +00:00
|
|
|
|
default:
|
|
|
|
|
advance(cycle.length);
|
|
|
|
|
return HalfCycles(0);
|
|
|
|
|
|
2021-04-15 22:57:34 +00:00
|
|
|
|
case CPU::Z80::PartialMachineCycle::InputStart:
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::OutputStart: {
|
|
|
|
|
// The port address is loaded prior to IOREQ being visible; a contention
|
|
|
|
|
// always occurs if it is in the $4000–$8000 range regardless of current
|
|
|
|
|
// memory mapping.
|
|
|
|
|
HalfCycles delay;
|
2021-04-18 22:41:24 +00:00
|
|
|
|
HalfCycles time = video_.time_since_flush();
|
2021-04-15 22:57:34 +00:00
|
|
|
|
|
|
|
|
|
if((address & 0xc000) == 0x4000) {
|
|
|
|
|
for(int c = 0; c < ((address & 1) ? 4 : 2); c++) {
|
|
|
|
|
const auto next_delay = video_.last_valid()->access_delay(time);
|
|
|
|
|
delay += next_delay;
|
|
|
|
|
time += next_delay + 2;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(!(address & 1)) {
|
|
|
|
|
delay = video_.last_valid()->access_delay(time + HalfCycles(2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
advance(cycle.length + delay);
|
|
|
|
|
return delay;
|
2021-04-18 15:56:00 +00:00
|
|
|
|
} break;
|
2021-04-15 22:57:34 +00:00
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
case PartialMachineCycle::ReadOpcodeStart:
|
|
|
|
|
case PartialMachineCycle::ReadStart:
|
2021-04-15 22:57:34 +00:00
|
|
|
|
case PartialMachineCycle::WriteStart: {
|
|
|
|
|
// These all start by loading the address bus, then set MREQ
|
|
|
|
|
// half a cycle later.
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(banks_[address >> 14].is_contended) {
|
2021-04-21 23:18:07 +00:00
|
|
|
|
const auto delay = video_.last_valid()->access_delay(video_.time_since_flush());
|
2021-04-15 22:57:34 +00:00
|
|
|
|
|
|
|
|
|
advance(cycle.length + delay);
|
|
|
|
|
return delay;
|
|
|
|
|
}
|
2021-04-18 15:56:00 +00:00
|
|
|
|
} break;
|
2021-04-15 22:04:16 +00:00
|
|
|
|
|
2021-04-15 22:57:34 +00:00
|
|
|
|
case PartialMachineCycle::Internal: {
|
|
|
|
|
// Whatever's on the address bus will remain there, without IOREQ or
|
|
|
|
|
// MREQ interceding, for this entire bus cycle. So apply contentions
|
|
|
|
|
// all the way along.
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(banks_[address >> 14].is_contended) {
|
2021-04-15 22:57:34 +00:00
|
|
|
|
const auto half_cycles = cycle.length.as<int>();
|
|
|
|
|
assert(!(half_cycles & 1));
|
|
|
|
|
|
2021-04-18 22:41:24 +00:00
|
|
|
|
HalfCycles time = video_.time_since_flush();
|
2021-04-15 22:57:34 +00:00
|
|
|
|
HalfCycles delay;
|
|
|
|
|
for(int c = 0; c < half_cycles; c += 2) {
|
|
|
|
|
const auto next_delay = video_.last_valid()->access_delay(time);
|
|
|
|
|
delay += next_delay;
|
|
|
|
|
time += next_delay + 2;
|
|
|
|
|
}
|
2021-04-15 22:04:16 +00:00
|
|
|
|
|
2021-04-15 23:17:11 +00:00
|
|
|
|
advance(cycle.length + delay);
|
2021-04-15 22:57:34 +00:00
|
|
|
|
return delay;
|
|
|
|
|
}
|
2021-04-18 15:56:00 +00:00
|
|
|
|
} break;
|
2021-04-15 22:04:16 +00:00
|
|
|
|
}
|
2021-04-01 02:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 23:52:38 +00:00
|
|
|
|
// For all other machine cycles, model the action as happening at the end of the machine cycle;
|
|
|
|
|
// that means advancing time now.
|
|
|
|
|
advance(cycle.length);
|
2021-04-01 02:52:41 +00:00
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
switch(cycle.operation) {
|
|
|
|
|
default: break;
|
2021-03-22 03:04:20 +00:00
|
|
|
|
|
|
|
|
|
case PartialMachineCycle::ReadOpcode:
|
2021-03-22 23:04:38 +00:00
|
|
|
|
// Fast loading: ROM version.
|
|
|
|
|
//
|
2021-04-05 02:39:30 +00:00
|
|
|
|
// The below patches over part of the 'LD-BYTES' routine from the 48kb ROM.
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(use_fast_tape_hack_ && address == 0x056b && banks_[0].read == &rom_[classic_rom_offset()]) {
|
2021-04-05 02:39:30 +00:00
|
|
|
|
// Stop pressing enter, if neccessry.
|
|
|
|
|
if(duration_to_press_enter_ > Cycles(0)) {
|
|
|
|
|
duration_to_press_enter_ = Cycles(0);
|
|
|
|
|
keyboard_.set_key_state(ZX::Keyboard::KeyEnter, false);
|
|
|
|
|
}
|
2021-03-23 02:42:10 +00:00
|
|
|
|
|
2021-04-05 02:39:30 +00:00
|
|
|
|
if(perform_rom_ld_bytes_56b()) {
|
2021-03-22 23:04:38 +00:00
|
|
|
|
*cycle.value = 0xc9; // i.e. RET.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-04 00:38:50 +00:00
|
|
|
|
[[fallthrough]];
|
2021-03-22 23:04:38 +00:00
|
|
|
|
|
2021-03-22 03:04:20 +00:00
|
|
|
|
case PartialMachineCycle::Read:
|
2021-04-16 01:19:21 +00:00
|
|
|
|
if constexpr (model == Model::SixteenK) {
|
|
|
|
|
// Assumption: with nothing mapped above 0x8000 on the 16kb Spectrum,
|
|
|
|
|
// read the floating bus.
|
|
|
|
|
if(address >= 0x8000) {
|
|
|
|
|
*cycle.value = video_->get_floating_value();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 14:44:07 +00:00
|
|
|
|
*cycle.value = banks_[address >> 14].read[address];
|
2021-03-25 00:23:33 +00:00
|
|
|
|
|
2021-04-16 01:13:06 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(banks_[address >> 14].is_contended) {
|
2021-04-16 01:13:06 +00:00
|
|
|
|
video_->set_last_contended_area_access(*cycle.value);
|
|
|
|
|
}
|
2021-03-25 00:23:33 +00:00
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PartialMachineCycle::Write:
|
2021-03-22 13:02:49 +00:00
|
|
|
|
// Flush video if this access modifies screen contents.
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(banks_[address >> 14].is_video && (address & 0x3fff) < 6912) {
|
2021-03-22 13:02:49 +00:00
|
|
|
|
video_.flush();
|
|
|
|
|
}
|
2021-03-25 00:23:33 +00:00
|
|
|
|
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[address >> 14].write[address] = *cycle.value;
|
2021-03-25 00:23:33 +00:00
|
|
|
|
|
2021-04-16 01:13:06 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
|
|
|
|
// Fill the floating bus buffer if this write is within the contended area.
|
2024-01-17 14:44:07 +00:00
|
|
|
|
if(banks_[address >> 14].is_contended) {
|
2021-04-16 01:13:06 +00:00
|
|
|
|
video_->set_last_contended_area_access(*cycle.value);
|
|
|
|
|
}
|
2021-03-25 00:23:33 +00:00
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2021-04-17 01:54:52 +00:00
|
|
|
|
// Partial port decodings here and in ::Input are as documented
|
|
|
|
|
// at https://worldofspectrum.org/faq/reference/ports.htm
|
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
case PartialMachineCycle::Output:
|
2021-03-18 16:41:24 +00:00
|
|
|
|
// Test for port FE.
|
2021-03-18 16:14:48 +00:00
|
|
|
|
if(!(address&1)) {
|
2021-03-18 16:23:54 +00:00
|
|
|
|
update_audio();
|
|
|
|
|
audio_toggle_.set_output(*cycle.value & 0x10);
|
2021-03-18 16:32:54 +00:00
|
|
|
|
|
2021-03-19 02:29:24 +00:00
|
|
|
|
video_->set_border_colour(*cycle.value & 7);
|
|
|
|
|
|
2021-03-18 16:32:54 +00:00
|
|
|
|
// b0–b2: border colour
|
|
|
|
|
// b3: enable tape input (?)
|
|
|
|
|
// b4: tape and speaker output
|
2021-03-18 16:14:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// Test for classic 128kb paging register (i.e. port 7ffd).
|
2021-04-17 01:54:52 +00:00
|
|
|
|
if (
|
|
|
|
|
(model >= Model::OneTwoEightK && model <= Model::Plus2 && (address & 0x8002) == 0x0000) ||
|
|
|
|
|
(model >= Model::Plus2a && (address & 0xc002) == 0x4000)
|
|
|
|
|
) {
|
|
|
|
|
port7ffd_ = *cycle.value;
|
|
|
|
|
update_memory_map();
|
|
|
|
|
|
|
|
|
|
// Set the proper video base pointer.
|
|
|
|
|
set_video_address();
|
2021-03-22 00:23:00 +00:00
|
|
|
|
}
|
2021-03-18 16:41:24 +00:00
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// Test for +2a/+3 paging (i.e. port 1ffd).
|
2021-04-15 01:37:10 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
|
|
|
|
if((address & 0xf002) == 0x1000) {
|
|
|
|
|
port1ffd_ = *cycle.value;
|
|
|
|
|
update_memory_map();
|
|
|
|
|
update_video_base();
|
|
|
|
|
|
|
|
|
|
if constexpr (model == Model::Plus3) {
|
|
|
|
|
fdc_->set_motor_on(*cycle.value & 0x08);
|
|
|
|
|
}
|
2021-03-22 23:36:05 +00:00
|
|
|
|
}
|
2021-03-22 00:23:00 +00:00
|
|
|
|
}
|
2021-03-18 16:41:24 +00:00
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
// Route to the AY if one is fitted.
|
2021-04-15 01:37:10 +00:00
|
|
|
|
if constexpr (model >= Model::OneTwoEightK) {
|
2021-04-17 01:54:52 +00:00
|
|
|
|
switch(address & 0xc002) {
|
|
|
|
|
case 0xc000:
|
|
|
|
|
// Select AY register.
|
|
|
|
|
update_audio();
|
|
|
|
|
GI::AY38910::Utility::select_register(ay_, *cycle.value);
|
|
|
|
|
break;
|
2021-03-22 01:03:35 +00:00
|
|
|
|
|
2021-04-17 01:54:52 +00:00
|
|
|
|
case 0x8000:
|
|
|
|
|
// Write to AY register.
|
|
|
|
|
update_audio();
|
|
|
|
|
GI::AY38910::Utility::write_data(ay_, *cycle.value);
|
|
|
|
|
break;
|
2021-04-15 01:37:10 +00:00
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
}
|
2021-03-22 13:15:00 +00:00
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
// Check for FDC accesses.
|
2021-03-22 13:15:00 +00:00
|
|
|
|
if constexpr (model == Model::Plus3) {
|
2021-04-17 01:54:52 +00:00
|
|
|
|
switch(address & 0xf002) {
|
2021-03-22 13:15:00 +00:00
|
|
|
|
default: break;
|
2021-04-17 01:54:52 +00:00
|
|
|
|
case 0x3000: case 0x2000:
|
2021-03-22 23:36:05 +00:00
|
|
|
|
fdc_->write((address >> 12) & 1, *cycle.value);
|
2021-03-22 13:15:00 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2021-04-16 01:19:21 +00:00
|
|
|
|
case PartialMachineCycle::Input: {
|
2023-05-15 14:17:04 +00:00
|
|
|
|
[[maybe_unused]] bool did_match = false;
|
2021-03-19 03:14:39 +00:00
|
|
|
|
*cycle.value = 0xff;
|
|
|
|
|
|
2021-04-29 00:19:01 +00:00
|
|
|
|
if(!(address&32)) {
|
|
|
|
|
did_match = true;
|
|
|
|
|
*cycle.value &= static_cast<Joystick *>(joysticks_[0].get())->get_kempston();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
if(!(address&1)) {
|
2021-04-16 01:19:21 +00:00
|
|
|
|
did_match = true;
|
|
|
|
|
|
2021-03-19 15:12:50 +00:00
|
|
|
|
// Port FE:
|
|
|
|
|
//
|
2021-03-18 16:32:54 +00:00
|
|
|
|
// address b8+: mask of keyboard lines to select
|
|
|
|
|
// result: b0–b4: mask of keys pressed
|
|
|
|
|
// b6: tape input
|
2021-03-19 15:12:50 +00:00
|
|
|
|
|
|
|
|
|
*cycle.value &= keyboard_.read(address);
|
|
|
|
|
*cycle.value &= tape_player_.get_input() ? 0xbf : 0xff;
|
2021-03-20 02:43:48 +00:00
|
|
|
|
|
2021-04-29 00:19:01 +00:00
|
|
|
|
// Add Joystick input on top.
|
|
|
|
|
if(!(address&0x1000)) *cycle.value &= static_cast<Joystick *>(joysticks_[0].get())->get_sinclair(0);
|
|
|
|
|
if(!(address&0x0800)) *cycle.value &= static_cast<Joystick *>(joysticks_[1].get())->get_sinclair(1);
|
|
|
|
|
|
2021-05-08 21:34:59 +00:00
|
|
|
|
// If this read is between 50 and 200 cycles since the
|
|
|
|
|
// previous, count it as an adjacent hit; if 20 of those
|
|
|
|
|
// have occurred then start the tape motor.
|
2021-03-20 02:43:48 +00:00
|
|
|
|
if(use_automatic_tape_motor_control_) {
|
2021-05-08 21:34:59 +00:00
|
|
|
|
if(cycles_since_tape_input_read_ >= HalfCycles(100) && cycles_since_tape_input_read_ < HalfCycles(200)) {
|
2021-03-20 02:43:48 +00:00
|
|
|
|
++recent_tape_hits_;
|
|
|
|
|
|
|
|
|
|
if(recent_tape_hits_ == 20) {
|
|
|
|
|
tape_player_.set_motor_control(true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
recent_tape_hits_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cycles_since_tape_input_read_ = HalfCycles(0);
|
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
}
|
2021-03-18 16:23:54 +00:00
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
if constexpr (model >= Model::OneTwoEightK) {
|
|
|
|
|
if((address & 0xc002) == 0xc000) {
|
2021-04-16 01:19:21 +00:00
|
|
|
|
did_match = true;
|
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
// Read from AY register.
|
|
|
|
|
update_audio();
|
|
|
|
|
*cycle.value &= GI::AY38910::Utility::read(ay_);
|
|
|
|
|
}
|
2021-03-18 16:23:54 +00:00
|
|
|
|
}
|
2021-03-22 23:36:05 +00:00
|
|
|
|
|
2021-04-15 22:04:16 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
|
|
|
|
// Check for a +2a/+3 floating bus read; these are particularly arcane.
|
|
|
|
|
// See footnote to https://spectrumforeveryone.com/technical/memory-contention-floating-bus/
|
|
|
|
|
// and, much more rigorously, http://sky.relative-path.com/zx/floating_bus.html
|
|
|
|
|
if(!disable_paging_ && (address & 0xf003) == 0x0001) {
|
|
|
|
|
*cycle.value &= video_->get_floating_value();
|
|
|
|
|
}
|
2021-03-23 21:09:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
if constexpr (model == Model::Plus3) {
|
2021-04-17 01:54:52 +00:00
|
|
|
|
switch(address & 0xf002) {
|
2021-03-22 23:36:05 +00:00
|
|
|
|
default: break;
|
2021-04-17 01:54:52 +00:00
|
|
|
|
case 0x3000: case 0x2000:
|
2021-03-22 23:36:05 +00:00
|
|
|
|
*cycle.value &= fdc_->read((address >> 12) & 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-16 01:19:21 +00:00
|
|
|
|
|
2021-04-17 01:54:52 +00:00
|
|
|
|
if constexpr (model <= Model::Plus2) {
|
2021-04-16 01:19:21 +00:00
|
|
|
|
if(!did_match) {
|
|
|
|
|
*cycle.value = video_->get_floating_value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} break;
|
2021-04-18 15:56:00 +00:00
|
|
|
|
|
|
|
|
|
case PartialMachineCycle::Interrupt:
|
|
|
|
|
// At least one piece of Spectrum software, Escape from M.O.N.J.A.S. explicitly
|
|
|
|
|
// assumes that a 0xff value will be on the bus during an interrupt acknowledgment.
|
|
|
|
|
// I wasn't otherwise aware that this value is reliable.
|
|
|
|
|
*cycle.value = 0xff;
|
|
|
|
|
break;
|
2021-03-18 16:14:48 +00:00
|
|
|
|
}
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
2021-04-01 02:52:41 +00:00
|
|
|
|
return HalfCycles(0);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 12:49:56 +00:00
|
|
|
|
private:
|
|
|
|
|
void advance(HalfCycles duration) {
|
|
|
|
|
time_since_audio_update_ += duration;
|
|
|
|
|
|
|
|
|
|
video_ += duration;
|
|
|
|
|
if(video_.did_flush()) {
|
2021-04-06 16:06:13 +00:00
|
|
|
|
z80_.set_interrupt_line(video_.last_valid()->get_interrupt_line(), video_.last_sequence_point_overrun());
|
2021-03-19 12:49:56 +00:00
|
|
|
|
}
|
2021-03-19 15:12:50 +00:00
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
if(!tape_player_is_sleeping_) tape_player_.run_for(duration.as_integral());
|
2021-03-20 02:43:48 +00:00
|
|
|
|
|
|
|
|
|
// Update automatic tape motor control, if enabled; if it's been
|
2021-05-08 21:34:59 +00:00
|
|
|
|
// 0.5 seconds since software last possibly polled the tape, stop it.
|
|
|
|
|
if(use_automatic_tape_motor_control_ && cycles_since_tape_input_read_ < HalfCycles(clock_rate())) {
|
2021-03-20 02:43:48 +00:00
|
|
|
|
cycles_since_tape_input_read_ += duration;
|
|
|
|
|
|
2021-05-08 21:34:59 +00:00
|
|
|
|
if(cycles_since_tape_input_read_ >= HalfCycles(clock_rate())) {
|
2021-03-20 02:43:48 +00:00
|
|
|
|
tape_player_.set_motor_control(false);
|
|
|
|
|
recent_tape_hits_ = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-22 23:36:05 +00:00
|
|
|
|
|
|
|
|
|
if constexpr (model == Model::Plus3) {
|
2021-03-27 03:54:08 +00:00
|
|
|
|
fdc_ += Cycles(duration.as_integral());
|
2021-03-22 23:36:05 +00:00
|
|
|
|
}
|
2021-03-23 14:44:43 +00:00
|
|
|
|
|
|
|
|
|
if(typer_) typer_->run_for(duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void type_string(const std::string &string) override {
|
|
|
|
|
Utility::TypeRecipient<CharacterMapper>::add_typer(string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool can_type(char c) const override {
|
|
|
|
|
return Utility::TypeRecipient<CharacterMapper>::can_type(c);
|
2021-03-19 12:49:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - Typer.
|
2021-03-23 14:44:43 +00:00
|
|
|
|
HalfCycles get_typer_delay(const std::string &) const override {
|
|
|
|
|
return z80_.get_is_resetting() ? Cycles(7'000'000) : Cycles(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HalfCycles get_typer_frequency() const override{
|
2021-03-23 20:59:43 +00:00
|
|
|
|
return Cycles(70'908);
|
2021-03-23 14:44:43 +00:00
|
|
|
|
}
|
2021-03-19 03:51:21 +00:00
|
|
|
|
|
|
|
|
|
KeyboardMapper *get_keyboard_mapper() override {
|
|
|
|
|
return &keyboard_mapper_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - Keyboard.
|
2021-03-19 03:51:21 +00:00
|
|
|
|
void set_key_state(uint16_t key, bool is_pressed) override {
|
|
|
|
|
keyboard_.set_key_state(key, is_pressed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear_all_keys() override {
|
|
|
|
|
keyboard_.clear_all_keys();
|
2021-03-23 00:12:03 +00:00
|
|
|
|
|
|
|
|
|
// Caveat: if holding enter synthetically, continue to do so.
|
|
|
|
|
if(duration_to_press_enter_ > Cycles(0)) {
|
|
|
|
|
keyboard_.set_key_state(ZX::Keyboard::KeyEnter, true);
|
|
|
|
|
}
|
2021-03-19 03:51:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 15:12:50 +00:00
|
|
|
|
// MARK: - MediaTarget.
|
|
|
|
|
bool insert_media(const Analyser::Static::Media &media) override {
|
|
|
|
|
// If there are any tapes supplied, use the first of them.
|
|
|
|
|
if(!media.tapes.empty()) {
|
|
|
|
|
tape_player_.set_tape(media.tapes.front());
|
2021-03-27 22:08:46 +00:00
|
|
|
|
set_use_fast_tape();
|
2021-03-19 15:12:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// Insert up to four disks.
|
|
|
|
|
int c = 0;
|
|
|
|
|
for(auto &disk : media.disks) {
|
|
|
|
|
fdc_->set_disk(disk, c);
|
|
|
|
|
c++;
|
|
|
|
|
if(c == 4) break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 20:40:09 +00:00
|
|
|
|
return !media.tapes.empty() || (!media.disks.empty() && model == Model::Plus3);
|
2021-03-19 15:12:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - ClockingHint::Observer.
|
|
|
|
|
|
|
|
|
|
void set_component_prefers_clocking(ClockingHint::Source *, ClockingHint::Preference) override {
|
|
|
|
|
tape_player_is_sleeping_ = tape_player_.preferred_clocking() == ClockingHint::Preference::None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Tape control.
|
2021-03-20 02:17:20 +00:00
|
|
|
|
|
|
|
|
|
void set_use_automatic_tape_motor_control(bool enabled) {
|
|
|
|
|
use_automatic_tape_motor_control_ = enabled;
|
|
|
|
|
if(!enabled) {
|
|
|
|
|
tape_player_.set_motor_control(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_tape_is_playing(bool is_playing) final {
|
|
|
|
|
tape_player_.set_motor_control(is_playing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool get_tape_is_playing() final {
|
|
|
|
|
return tape_player_.get_motor_control();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Configuration options.
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Reflection::Struct> get_options() override {
|
|
|
|
|
auto options = std::make_unique<Options>(Configurable::OptionsType::UserFriendly); // OptionsType is arbitrary, but not optional.
|
|
|
|
|
options->automatic_tape_motor_control = use_automatic_tape_motor_control_;
|
|
|
|
|
options->quickload = allow_fast_tape_hack_;
|
2021-04-26 01:16:22 +00:00
|
|
|
|
options->output = get_video_signal_configurable();
|
2021-03-20 02:17:20 +00:00
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_options(const std::unique_ptr<Reflection::Struct> &str) override {
|
|
|
|
|
const auto options = dynamic_cast<Options *>(str.get());
|
|
|
|
|
set_video_signal_configurable(options->output);
|
|
|
|
|
set_use_automatic_tape_motor_control(options->automatic_tape_motor_control);
|
|
|
|
|
allow_fast_tape_hack_ = options->quickload;
|
|
|
|
|
set_use_fast_tape();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 03:33:46 +00:00
|
|
|
|
// MARK: - AudioProducer.
|
|
|
|
|
|
|
|
|
|
Outputs::Speaker::Speaker *get_speaker() override {
|
|
|
|
|
return &speaker_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 20:38:04 +00:00
|
|
|
|
// MARK: - Activity Source.
|
2021-03-23 14:32:22 +00:00
|
|
|
|
void set_activity_observer(Activity::Observer *observer) override {
|
|
|
|
|
if constexpr (model == Model::Plus3) fdc_->set_activity_observer(observer);
|
|
|
|
|
tape_player_.set_activity_observer(observer);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 14:18:17 +00:00
|
|
|
|
private:
|
2021-03-18 14:43:51 +00:00
|
|
|
|
CPU::Z80::Processor<ConcreteMachine, false, false> z80_;
|
|
|
|
|
|
|
|
|
|
// MARK: - Memory.
|
2021-03-18 14:18:17 +00:00
|
|
|
|
std::array<uint8_t, 64*1024> rom_;
|
|
|
|
|
std::array<uint8_t, 128*1024> ram_;
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
|
|
|
|
std::array<uint8_t, 16*1024> scratch_;
|
2024-01-17 14:44:07 +00:00
|
|
|
|
struct Bank {
|
|
|
|
|
const uint8_t * read;
|
|
|
|
|
uint8_t *write;
|
|
|
|
|
uint8_t page;
|
|
|
|
|
bool is_contended;
|
|
|
|
|
bool is_video;
|
|
|
|
|
};
|
|
|
|
|
std::array<Bank, 4> banks_;
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
|
|
|
|
uint8_t port1ffd_ = 0;
|
|
|
|
|
uint8_t port7ffd_ = 0;
|
|
|
|
|
bool disable_paging_ = false;
|
|
|
|
|
|
|
|
|
|
void update_memory_map() {
|
2021-03-18 16:14:48 +00:00
|
|
|
|
// If paging is permanently disabled, don't react.
|
2021-03-18 14:43:51 +00:00
|
|
|
|
if(disable_paging_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 00:23:00 +00:00
|
|
|
|
if(port1ffd_ & 0x01) {
|
2021-03-18 14:43:51 +00:00
|
|
|
|
// "Special paging mode", i.e. one of four fixed
|
|
|
|
|
// RAM configurations, port 7ffd doesn't matter.
|
|
|
|
|
|
2021-03-22 00:23:00 +00:00
|
|
|
|
switch(port1ffd_ & 0x06) {
|
2021-03-18 14:43:51 +00:00
|
|
|
|
default:
|
|
|
|
|
case 0x00:
|
2021-03-22 13:02:49 +00:00
|
|
|
|
set_memory(0, 0);
|
|
|
|
|
set_memory(1, 1);
|
|
|
|
|
set_memory(2, 2);
|
|
|
|
|
set_memory(3, 3);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x02:
|
2021-03-22 13:02:49 +00:00
|
|
|
|
set_memory(0, 4);
|
|
|
|
|
set_memory(1, 5);
|
|
|
|
|
set_memory(2, 6);
|
|
|
|
|
set_memory(3, 7);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x04:
|
2021-03-22 13:02:49 +00:00
|
|
|
|
set_memory(0, 4);
|
|
|
|
|
set_memory(1, 5);
|
|
|
|
|
set_memory(2, 6);
|
|
|
|
|
set_memory(3, 3);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x06:
|
2021-03-22 13:02:49 +00:00
|
|
|
|
set_memory(0, 4);
|
|
|
|
|
set_memory(1, 7);
|
|
|
|
|
set_memory(2, 6);
|
|
|
|
|
set_memory(3, 3);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-03-22 13:02:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Apply standard 128kb-esque mapping (albeit with extra ROM to pick from).
|
|
|
|
|
set_memory(0, 0x80 | ((port1ffd_ >> 1) & 2) | ((port7ffd_ >> 4) & 1));
|
|
|
|
|
set_memory(1, 5);
|
|
|
|
|
set_memory(2, 2);
|
|
|
|
|
set_memory(3, port7ffd_ & 7);
|
2021-03-18 14:43:51 +00:00
|
|
|
|
}
|
2021-04-26 00:46:49 +00:00
|
|
|
|
|
|
|
|
|
// Potentially lock paging, _after_ the current
|
|
|
|
|
// port values have taken effect.
|
|
|
|
|
disable_paging_ = port7ffd_ & 0x20;
|
2021-03-22 13:02:49 +00:00
|
|
|
|
}
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
2024-01-17 14:44:07 +00:00
|
|
|
|
void set_memory(std::size_t bank, uint8_t source) {
|
2021-04-15 22:57:34 +00:00
|
|
|
|
if constexpr (model >= Model::Plus2a) {
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[bank].is_contended = source >= 4 && source < 8;
|
2021-04-15 22:57:34 +00:00
|
|
|
|
} else {
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[bank].is_contended = source < 0x80 && source & 1;
|
2021-04-15 22:57:34 +00:00
|
|
|
|
}
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[bank].page = source;
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
2021-04-15 01:37:10 +00:00
|
|
|
|
uint8_t *const read = (source < 0x80) ? &ram_[source * 16384] : &rom_[(source & 0x7f) * 16384];
|
2021-03-22 13:02:49 +00:00
|
|
|
|
const auto offset = bank*16384;
|
2021-03-18 14:43:51 +00:00
|
|
|
|
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[bank].read = read - offset;
|
|
|
|
|
banks_[bank].write = ((source < 0x80) ? read : scratch_.data()) - offset;
|
2021-03-18 14:43:51 +00:00
|
|
|
|
}
|
2021-03-18 16:14:48 +00:00
|
|
|
|
|
2021-03-22 00:23:00 +00:00
|
|
|
|
void set_video_address() {
|
|
|
|
|
video_->set_video_source(&ram_[((port7ffd_ & 0x08) ? 7 : 5) * 16384]);
|
2021-03-22 13:02:49 +00:00
|
|
|
|
update_video_base();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_video_base() {
|
|
|
|
|
const uint8_t video_page = (port7ffd_ & 0x08) ? 7 : 5;
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[0].is_video = banks_[0].page == video_page;
|
|
|
|
|
banks_[1].is_video = banks_[1].page == video_page;
|
|
|
|
|
banks_[2].is_video = banks_[2].page == video_page;
|
|
|
|
|
banks_[3].is_video = banks_[3].page == video_page;
|
2021-03-22 00:23:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:14:48 +00:00
|
|
|
|
// MARK: - Audio.
|
2022-07-16 18:41:04 +00:00
|
|
|
|
Concurrency::AsyncTaskQueue<false> audio_queue_;
|
2021-03-18 16:14:48 +00:00
|
|
|
|
GI::AY38910::AY38910<false> ay_;
|
|
|
|
|
Audio::Toggle audio_toggle_;
|
|
|
|
|
Outputs::Speaker::CompoundSource<GI::AY38910::AY38910<false>, Audio::Toggle> mixer_;
|
2021-11-21 20:37:29 +00:00
|
|
|
|
Outputs::Speaker::PullLowpass<Outputs::Speaker::CompoundSource<GI::AY38910::AY38910<false>, Audio::Toggle>> speaker_;
|
2021-03-18 16:23:54 +00:00
|
|
|
|
|
|
|
|
|
HalfCycles time_since_audio_update_;
|
|
|
|
|
void update_audio() {
|
|
|
|
|
speaker_.run_for(audio_queue_, time_since_audio_update_.divide_cycles(Cycles(2)));
|
|
|
|
|
}
|
2021-03-18 16:47:48 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - Video.
|
2021-04-15 02:23:27 +00:00
|
|
|
|
using VideoType =
|
|
|
|
|
std::conditional_t<
|
2021-04-25 18:00:12 +00:00
|
|
|
|
model <= Model::FortyEightK, Video::Video<Video::Timing::FortyEightK>,
|
2021-04-15 02:23:27 +00:00
|
|
|
|
std::conditional_t<
|
2021-04-25 18:00:12 +00:00
|
|
|
|
model <= Model::Plus2, Video::Video<Video::Timing::OneTwoEightK>,
|
|
|
|
|
Video::Video<Video::Timing::Plus3>
|
2021-04-15 02:23:27 +00:00
|
|
|
|
>
|
|
|
|
|
>;
|
|
|
|
|
JustInTimeActor<VideoType> video_;
|
2021-03-19 03:51:21 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - Keyboard.
|
2021-03-19 14:36:08 +00:00
|
|
|
|
Sinclair::ZX::Keyboard::Keyboard keyboard_;
|
|
|
|
|
Sinclair::ZX::Keyboard::KeyboardMapper keyboard_mapper_;
|
2021-03-19 15:12:50 +00:00
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// MARK: - Tape.
|
2021-03-19 15:12:50 +00:00
|
|
|
|
Storage::Tape::BinaryTapePlayer tape_player_;
|
2021-03-23 20:38:04 +00:00
|
|
|
|
bool tape_player_is_sleeping_ = false;
|
2021-03-20 02:17:20 +00:00
|
|
|
|
|
2021-03-20 02:43:48 +00:00
|
|
|
|
bool use_automatic_tape_motor_control_ = true;
|
2021-03-20 02:17:20 +00:00
|
|
|
|
HalfCycles cycles_since_tape_input_read_;
|
2021-03-20 02:43:48 +00:00
|
|
|
|
int recent_tape_hits_ = 0;
|
2021-03-20 02:17:20 +00:00
|
|
|
|
|
|
|
|
|
bool allow_fast_tape_hack_ = false;
|
2021-03-22 23:04:38 +00:00
|
|
|
|
bool use_fast_tape_hack_ = false;
|
2021-03-20 02:17:20 +00:00
|
|
|
|
void set_use_fast_tape() {
|
2021-03-22 23:04:38 +00:00
|
|
|
|
use_fast_tape_hack_ = allow_fast_tape_hack_ && tape_player_.has_tape();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reimplements the 'LD-BYTES' routine, as documented at
|
2021-04-05 02:39:30 +00:00
|
|
|
|
// https://skoolkid.github.io/rom/asm/0556.html but picking
|
|
|
|
|
// up from address 56b i.e.
|
2021-03-22 23:04:38 +00:00
|
|
|
|
//
|
|
|
|
|
// In:
|
2021-04-05 02:39:30 +00:00
|
|
|
|
// A': 0x00 or 0xff for block type;
|
|
|
|
|
// F': carry set if loading, clear if verifying;
|
2021-03-22 23:04:38 +00:00
|
|
|
|
// DE: block length;
|
|
|
|
|
// IX: start address.
|
|
|
|
|
//
|
|
|
|
|
// Out:
|
|
|
|
|
// F: carry set for success, clear for error.
|
2021-04-05 02:39:30 +00:00
|
|
|
|
//
|
|
|
|
|
// And, empirically:
|
|
|
|
|
// IX: one beyond final address written;
|
|
|
|
|
// DE: 0;
|
|
|
|
|
// L: parity byte;
|
|
|
|
|
// H: 0 for no error, 0xff for error;
|
|
|
|
|
// A: same as H.
|
|
|
|
|
// BC: ???
|
|
|
|
|
bool perform_rom_ld_bytes_56b() {
|
2021-03-22 23:04:38 +00:00
|
|
|
|
using Parser = Storage::Tape::ZXSpectrum::Parser;
|
|
|
|
|
Parser parser(Parser::MachineType::ZXSpectrum);
|
|
|
|
|
|
|
|
|
|
using Register = CPU::Z80::Register;
|
2023-05-10 23:42:19 +00:00
|
|
|
|
uint8_t flags = uint8_t(z80_.value_of(Register::FlagsDash));
|
2021-03-22 23:04:38 +00:00
|
|
|
|
if(!(flags & 1)) return false;
|
|
|
|
|
|
2023-05-10 23:42:19 +00:00
|
|
|
|
const uint8_t block_type = uint8_t(z80_.value_of(Register::ADash));
|
2021-03-22 23:04:38 +00:00
|
|
|
|
const auto block = parser.find_block(tape_player_.get_tape());
|
|
|
|
|
if(!block || block_type != (*block).type) return false;
|
|
|
|
|
|
2023-05-10 23:42:19 +00:00
|
|
|
|
uint16_t length = z80_.value_of(Register::DE);
|
|
|
|
|
uint16_t target = z80_.value_of(Register::IX);
|
2021-03-22 23:04:38 +00:00
|
|
|
|
|
2021-04-05 02:39:30 +00:00
|
|
|
|
flags = 0x93;
|
|
|
|
|
uint8_t parity = 0x00;
|
2021-03-22 23:04:38 +00:00
|
|
|
|
while(length--) {
|
|
|
|
|
auto next = parser.get_byte(tape_player_.get_tape());
|
|
|
|
|
if(!next) {
|
|
|
|
|
flags &= ~1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 14:44:07 +00:00
|
|
|
|
banks_[target >> 14].write[target] = *next;
|
2021-04-05 02:39:30 +00:00
|
|
|
|
parity ^= *next;
|
2021-03-22 23:04:38 +00:00
|
|
|
|
++target;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 02:39:30 +00:00
|
|
|
|
auto stored_parity = parser.get_byte(tape_player_.get_tape());
|
|
|
|
|
if(!stored_parity) {
|
|
|
|
|
flags &= ~1;
|
|
|
|
|
} else {
|
2023-05-10 23:42:19 +00:00
|
|
|
|
z80_.set_value_of(Register::L, *stored_parity);
|
2021-04-05 02:39:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:42:19 +00:00
|
|
|
|
z80_.set_value_of(Register::Flags, flags);
|
|
|
|
|
z80_.set_value_of(Register::DE, length);
|
|
|
|
|
z80_.set_value_of(Register::IX, target);
|
2021-04-05 02:39:30 +00:00
|
|
|
|
|
|
|
|
|
const uint8_t h = (flags & 1) ? 0x00 : 0xff;
|
2023-05-10 23:42:19 +00:00
|
|
|
|
z80_.set_value_of(Register::H, h);
|
|
|
|
|
z80_.set_value_of(Register::A, h);
|
2021-04-05 02:39:30 +00:00
|
|
|
|
|
2021-03-22 23:04:38 +00:00
|
|
|
|
return true;
|
2021-03-20 02:17:20 +00:00
|
|
|
|
}
|
2021-03-22 23:36:05 +00:00
|
|
|
|
|
2021-04-15 21:31:42 +00:00
|
|
|
|
static constexpr int classic_rom_offset() {
|
|
|
|
|
switch(model) {
|
|
|
|
|
case Model::SixteenK:
|
|
|
|
|
case Model::FortyEightK:
|
|
|
|
|
return 0x0000;
|
|
|
|
|
|
|
|
|
|
case Model::OneTwoEightK:
|
|
|
|
|
case Model::Plus2:
|
|
|
|
|
return 0x4000;
|
|
|
|
|
|
|
|
|
|
case Model::Plus2a:
|
|
|
|
|
case Model::Plus3:
|
|
|
|
|
return 0xc000;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 23:36:05 +00:00
|
|
|
|
// MARK: - Disc.
|
2021-04-04 21:33:49 +00:00
|
|
|
|
JustInTimeActor<Amstrad::FDC, Cycles> fdc_;
|
2021-03-23 00:12:03 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - Automatic startup.
|
|
|
|
|
Cycles duration_to_press_enter_;
|
2021-04-29 00:19:01 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - Joysticks
|
|
|
|
|
std::vector<std::unique_ptr<Inputs::Joystick>> joysticks_;
|
|
|
|
|
const std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() override {
|
|
|
|
|
return joysticks_;
|
|
|
|
|
}
|
2021-03-18 14:18:17 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 03:38:55 +00:00
|
|
|
|
|
|
|
|
|
using namespace Sinclair::ZXSpectrum;
|
|
|
|
|
|
2024-01-13 03:03:19 +00:00
|
|
|
|
std::unique_ptr<Machine> Machine::ZXSpectrum(const Analyser::Static::Target *target, const ROMMachine::ROMFetcher &rom_fetcher) {
|
2021-03-18 14:18:17 +00:00
|
|
|
|
const auto zx_target = dynamic_cast<const Analyser::Static::ZXSpectrum::Target *>(target);
|
|
|
|
|
|
|
|
|
|
switch(zx_target->model) {
|
2024-01-13 03:03:19 +00:00
|
|
|
|
case Model::SixteenK: return std::make_unique<ConcreteMachine<Model::SixteenK>>(*zx_target, rom_fetcher);
|
|
|
|
|
case Model::FortyEightK: return std::make_unique<ConcreteMachine<Model::FortyEightK>>(*zx_target, rom_fetcher);
|
|
|
|
|
case Model::OneTwoEightK: return std::make_unique<ConcreteMachine<Model::OneTwoEightK>>(*zx_target, rom_fetcher);
|
|
|
|
|
case Model::Plus2: return std::make_unique<ConcreteMachine<Model::Plus2>>(*zx_target, rom_fetcher);
|
|
|
|
|
case Model::Plus2a: return std::make_unique<ConcreteMachine<Model::Plus2a>>(*zx_target, rom_fetcher);
|
|
|
|
|
case Model::Plus3: return std::make_unique<ConcreteMachine<Model::Plus3>>(*zx_target, rom_fetcher);
|
2021-03-18 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 03:38:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|