2017-07-31 02:05:29 +00:00
|
|
|
//
|
|
|
|
// AmstradCPC.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 30/07/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-07-31 02:05:29 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "AmstradCPC.hpp"
|
|
|
|
|
2017-10-21 14:52:35 +00:00
|
|
|
#include "Keyboard.hpp"
|
2017-08-11 15:21:07 +00:00
|
|
|
|
2017-08-01 02:32:04 +00:00
|
|
|
#include "../../Processors/Z80/Z80.hpp"
|
2017-08-01 20:15:19 +00:00
|
|
|
|
2017-08-05 23:45:52 +00:00
|
|
|
#include "../../Components/6845/CRTC6845.hpp"
|
2017-08-01 20:15:19 +00:00
|
|
|
#include "../../Components/8255/i8255.hpp"
|
2017-08-05 23:45:52 +00:00
|
|
|
#include "../../Components/8272/i8272.hpp"
|
2017-08-01 02:32:04 +00:00
|
|
|
#include "../../Components/AY38910/AY38910.hpp"
|
|
|
|
|
2017-10-21 14:30:02 +00:00
|
|
|
#include "../Utility/MemoryFuzzer.hpp"
|
|
|
|
#include "../Utility/Typer.hpp"
|
2017-08-11 15:21:07 +00:00
|
|
|
|
2018-05-12 01:45:46 +00:00
|
|
|
#include "../../Activity/Source.hpp"
|
2018-07-11 01:32:28 +00:00
|
|
|
#include "../MediaTarget.hpp"
|
2018-03-09 20:19:02 +00:00
|
|
|
#include "../CRTMachine.hpp"
|
2018-06-22 02:46:10 +00:00
|
|
|
#include "../JoystickMachine.hpp"
|
2018-03-09 20:19:02 +00:00
|
|
|
#include "../KeyboardMachine.hpp"
|
|
|
|
|
2017-08-02 17:50:14 +00:00
|
|
|
#include "../../Storage/Tape/Tape.hpp"
|
|
|
|
|
2017-08-22 01:07:10 +00:00
|
|
|
#include "../../ClockReceiver/ForceInline.hpp"
|
2017-12-19 02:39:23 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
2018-11-04 01:54:25 +00:00
|
|
|
#include "../../Outputs/CRT/CRT.hpp"
|
2017-08-22 01:07:10 +00:00
|
|
|
|
2018-03-09 21:07:29 +00:00
|
|
|
#include "../../Analyser/Static/AmstradCPC/Target.hpp"
|
|
|
|
|
2017-11-24 22:55:28 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-08-03 02:11:03 +00:00
|
|
|
namespace AmstradCPC {
|
2017-07-31 02:05:29 +00:00
|
|
|
|
2018-06-22 00:00:49 +00:00
|
|
|
std::vector<std::unique_ptr<Configurable::Option>> get_options() {
|
|
|
|
return Configurable::standard_options(
|
2018-11-29 01:53:33 +00:00
|
|
|
static_cast<Configurable::StandardOptions>(Configurable::DisplayRGB | Configurable::DisplayCompositeColour)
|
2018-06-22 00:00:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
Models the CPC's interrupt timer. Inputs are vsync, hsync, interrupt acknowledge and reset, and its output
|
|
|
|
is simply yes or no on whether an interupt is currently requested. Internally it uses a counter with a period
|
|
|
|
of 52 and occasionally adjusts or makes decisions based on bit 4.
|
|
|
|
|
|
|
|
Hsync and vsync signals are expected to come directly from the CRTC; they are not decoded from a composite stream.
|
|
|
|
*/
|
2017-08-02 02:15:39 +00:00
|
|
|
class InterruptTimer {
|
|
|
|
public:
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
2017-08-04 12:56:09 +00:00
|
|
|
Indicates that a new hsync pulse has been recognised. This should be
|
|
|
|
supplied on the falling edge of the CRTC HSYNC signal, which is the
|
|
|
|
trailing edge because it is active high.
|
2017-08-03 00:37:26 +00:00
|
|
|
*/
|
|
|
|
inline void signal_hsync() {
|
|
|
|
// Increment the timer and if it has hit 52 then reset it and
|
|
|
|
// set the interrupt request line to true.
|
2017-08-02 02:15:39 +00:00
|
|
|
timer_++;
|
|
|
|
if(timer_ == 52) {
|
|
|
|
timer_ = 0;
|
|
|
|
interrupt_request_ = true;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// If a vertical sync has previously been indicated then after two
|
|
|
|
// further horizontal syncs the timer should either (i) set the interrupt
|
|
|
|
// line, if bit 4 is clear; or (ii) reset the timer.
|
2017-08-02 02:15:39 +00:00
|
|
|
if(reset_counter_) {
|
|
|
|
reset_counter_--;
|
|
|
|
if(!reset_counter_) {
|
2017-08-04 12:56:09 +00:00
|
|
|
if(timer_ & 32) {
|
2017-08-02 02:15:39 +00:00
|
|
|
interrupt_request_ = true;
|
|
|
|
}
|
|
|
|
timer_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Indicates the leading edge of a new vertical sync.
|
2017-08-02 02:15:39 +00:00
|
|
|
inline void signal_vsync() {
|
|
|
|
reset_counter_ = 2;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Indicates that an interrupt acknowledge has been received from the Z80.
|
|
|
|
inline void signal_interrupt_acknowledge() {
|
2017-08-02 02:15:39 +00:00
|
|
|
interrupt_request_ = false;
|
|
|
|
timer_ &= ~32;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// @returns @c true if an interrupt is currently requested; @c false otherwise.
|
2017-08-02 02:15:39 +00:00
|
|
|
inline bool get_request() {
|
2017-08-20 16:05:00 +00:00
|
|
|
return last_interrupt_request_ = interrupt_request_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asks whether the interrupt status has changed.
|
|
|
|
inline bool request_has_changed() {
|
|
|
|
return last_interrupt_request_ != interrupt_request_;
|
2017-08-02 02:15:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Resets the timer.
|
2017-08-02 02:15:39 +00:00
|
|
|
inline void reset_count() {
|
|
|
|
timer_ = 0;
|
2017-08-04 12:56:09 +00:00
|
|
|
interrupt_request_ = false;
|
2017-08-02 02:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-18 02:40:32 +00:00
|
|
|
int reset_counter_ = 0;
|
|
|
|
bool interrupt_request_ = false;
|
|
|
|
bool last_interrupt_request_ = false;
|
|
|
|
int timer_ = 0;
|
2017-08-02 02:15:39 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
Provides a holder for an AY-3-8910 and its current cycles-since-updated count.
|
|
|
|
Therefore acts both to store an AY and to bookkeep this emulator's idiomatic
|
|
|
|
deferred clocking for this component.
|
|
|
|
*/
|
2017-08-02 11:21:33 +00:00
|
|
|
class AYDeferrer {
|
|
|
|
public:
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Constructs a new AY instance and sets its clock rate.
|
2017-12-18 02:26:06 +00:00
|
|
|
AYDeferrer() : ay_(audio_queue_), speaker_(ay_) {
|
|
|
|
speaker_.set_input_rate(1000000);
|
2017-08-02 11:21:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 01:59:19 +00:00
|
|
|
~AYDeferrer() {
|
|
|
|
audio_queue_.flush();
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Adds @c half_cycles half cycles to the amount of time that has passed.
|
2017-08-02 11:21:33 +00:00
|
|
|
inline void run_for(HalfCycles half_cycles) {
|
|
|
|
cycles_since_update_ += half_cycles;
|
|
|
|
}
|
|
|
|
|
2017-08-05 13:12:17 +00:00
|
|
|
/// Enqueues an update-to-now into the AY's deferred queue.
|
|
|
|
inline void update() {
|
2017-12-18 02:26:06 +00:00
|
|
|
speaker_.run_for(audio_queue_, cycles_since_update_.divide_cycles(Cycles(4)));
|
2017-08-05 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Issues a request to the AY to perform all processing up to the current time.
|
2017-08-02 11:21:33 +00:00
|
|
|
inline void flush() {
|
2017-12-18 02:26:06 +00:00
|
|
|
audio_queue_.perform();
|
2017-08-02 11:21:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// @returns the speaker the AY is using for output.
|
2017-12-18 02:26:06 +00:00
|
|
|
Outputs::Speaker::Speaker *get_speaker() {
|
|
|
|
return &speaker_;
|
2017-08-02 11:21:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// @returns the AY itself.
|
2017-12-18 02:26:06 +00:00
|
|
|
GI::AY38910::AY38910 &ay() {
|
|
|
|
return ay_;
|
2017-08-02 11:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-12-18 02:26:06 +00:00
|
|
|
Concurrency::DeferringAsyncTaskQueue audio_queue_;
|
|
|
|
GI::AY38910::AY38910 ay_;
|
|
|
|
Outputs::Speaker::LowpassSpeaker<GI::AY38910::AY38910> speaker_;
|
2017-08-02 11:21:33 +00:00
|
|
|
HalfCycles cycles_since_update_;
|
|
|
|
};
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
Provides the mechanism of receipt for the CRTC outputs. In practice has the gate array's
|
|
|
|
video fetching and serialisation logic built in. So this is responsible for all video
|
|
|
|
generation and therefore owns details such as the current palette.
|
|
|
|
*/
|
2017-08-01 20:34:13 +00:00
|
|
|
class CRTCBusHandler {
|
2017-08-01 02:32:04 +00:00
|
|
|
public:
|
2017-08-02 02:15:39 +00:00
|
|
|
CRTCBusHandler(uint8_t *ram, InterruptTimer &interrupt_timer) :
|
2018-11-24 21:56:41 +00:00
|
|
|
crt_(1024, 1, Outputs::Display::Type::PAL50, Outputs::Display::InputDataType::Red2Green2Blue2),
|
2017-08-01 19:49:16 +00:00
|
|
|
ram_(ram),
|
2017-10-18 00:50:46 +00:00
|
|
|
interrupt_timer_(interrupt_timer) {
|
2017-08-20 14:13:23 +00:00
|
|
|
establish_palette_hits();
|
2017-08-20 02:19:46 +00:00
|
|
|
build_mode_table();
|
2018-11-16 02:32:22 +00:00
|
|
|
crt_.set_visible_area(Outputs::Display::Rect(0.1072f, 0.1f, 0.842105263157895f, 0.842105263157895f));
|
2017-08-02 02:18:42 +00:00
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
2017-08-26 18:07:51 +00:00
|
|
|
The CRTC entry function for the main part of each clock cycle; takes the current
|
|
|
|
bus state and determines what output to produce based on the current palette and mode.
|
2017-08-03 00:37:26 +00:00
|
|
|
*/
|
2017-08-26 18:07:51 +00:00
|
|
|
forceinline void perform_bus_cycle_phase1(const Motorola::CRTC::BusState &state) {
|
2018-05-13 19:46:14 +00:00
|
|
|
// The gate array waits 2us to react to the CRTC's vsync signal, and then
|
|
|
|
// caps output at 4us. Since the clock rate is 1Mhz, that's 2 and 4 cycles,
|
2017-08-04 20:36:55 +00:00
|
|
|
// respectively.
|
|
|
|
if(state.hsync) {
|
|
|
|
cycles_into_hsync_++;
|
|
|
|
} else {
|
|
|
|
cycles_into_hsync_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_hsync = (cycles_into_hsync_ >= 2 && cycles_into_hsync_ < 6);
|
2018-06-21 02:38:54 +00:00
|
|
|
bool is_colour_burst = (cycles_into_hsync_ >= 7 && cycles_into_hsync_ < 11);
|
2017-08-04 20:36:55 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// Sync is taken to override pixels, and is combined as a simple OR.
|
2017-08-04 20:36:55 +00:00
|
|
|
bool is_sync = is_hsync || state.vsync;
|
2018-06-21 02:38:54 +00:00
|
|
|
bool is_blank = !is_sync && state.hsync;
|
|
|
|
|
|
|
|
OutputMode output_mode;
|
|
|
|
if(is_sync) {
|
|
|
|
output_mode = OutputMode::Sync;
|
|
|
|
} else if(is_colour_burst) {
|
|
|
|
output_mode = OutputMode::ColourBurst;
|
|
|
|
} else if(is_blank) {
|
|
|
|
output_mode = OutputMode::Blank;
|
|
|
|
} else if(state.display_enable) {
|
|
|
|
output_mode = OutputMode::Pixels;
|
|
|
|
} else {
|
|
|
|
output_mode = OutputMode::Border;
|
|
|
|
}
|
2017-08-01 19:49:16 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// If a transition between sync/border/pixels just occurred, flush whatever was
|
|
|
|
// in progress to the CRT and reset counting.
|
2018-06-21 02:38:54 +00:00
|
|
|
if(output_mode != previous_output_mode_) {
|
|
|
|
if(cycles_) {
|
|
|
|
switch(previous_output_mode_) {
|
|
|
|
default:
|
2018-11-16 02:32:22 +00:00
|
|
|
case OutputMode::Blank: crt_.output_blank(cycles_ * 16); break;
|
|
|
|
case OutputMode::Sync: crt_.output_sync(cycles_ * 16); break;
|
2018-06-21 02:38:54 +00:00
|
|
|
case OutputMode::Border: output_border(cycles_); break;
|
2018-11-16 02:32:22 +00:00
|
|
|
case OutputMode::ColourBurst: crt_.output_default_colour_burst(cycles_ * 16); break;
|
2018-06-21 02:38:54 +00:00
|
|
|
case OutputMode::Pixels:
|
2018-11-16 02:32:22 +00:00
|
|
|
crt_.output_data(cycles_ * 16, size_t(cycles_ * 16 / pixel_divider_));
|
2017-08-01 19:49:16 +00:00
|
|
|
pixel_pointer_ = pixel_data_ = nullptr;
|
2018-06-21 02:38:54 +00:00
|
|
|
break;
|
2017-08-01 19:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cycles_ = 0;
|
2018-06-21 02:38:54 +00:00
|
|
|
previous_output_mode_ = output_mode;
|
2017-08-01 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// increment cycles since state changed
|
2017-08-01 02:32:04 +00:00
|
|
|
cycles_++;
|
2017-08-01 11:24:29 +00:00
|
|
|
|
|
|
|
// collect some more pixels if output is ongoing
|
2018-06-23 20:18:33 +00:00
|
|
|
if(previous_output_mode_ == OutputMode::Pixels) {
|
2017-08-01 11:24:29 +00:00
|
|
|
if(!pixel_data_) {
|
2018-11-16 02:32:22 +00:00
|
|
|
pixel_pointer_ = pixel_data_ = crt_.begin_data(320, 8);
|
2017-08-01 11:24:29 +00:00
|
|
|
}
|
|
|
|
if(pixel_pointer_) {
|
2017-08-01 11:34:12 +00:00
|
|
|
// the CPC shuffles output lines as:
|
2017-08-01 11:51:13 +00:00
|
|
|
// MA13 MA12 RA2 RA1 RA0 MA9 MA8 MA7 MA6 MA5 MA4 MA3 MA2 MA1 MA0 CCLK
|
2017-08-03 00:37:26 +00:00
|
|
|
// ... so form the real access address.
|
2017-08-01 11:34:12 +00:00
|
|
|
uint16_t address =
|
2017-10-04 02:04:15 +00:00
|
|
|
static_cast<uint16_t>(
|
2017-08-01 11:51:13 +00:00
|
|
|
((state.refresh_address & 0x3ff) << 1) |
|
|
|
|
((state.row_address & 0x7) << 11) |
|
|
|
|
((state.refresh_address & 0x3000) << 2)
|
2017-08-01 11:34:12 +00:00
|
|
|
);
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// fetch two bytes and translate into pixels
|
2017-08-01 19:16:13 +00:00
|
|
|
switch(mode_) {
|
|
|
|
case 0:
|
2017-10-22 02:30:15 +00:00
|
|
|
reinterpret_cast<uint16_t *>(pixel_pointer_)[0] = mode0_output_[ram_[address]];
|
|
|
|
reinterpret_cast<uint16_t *>(pixel_pointer_)[1] = mode0_output_[ram_[address+1]];
|
2017-08-01 19:16:13 +00:00
|
|
|
pixel_pointer_ += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2017-10-22 02:30:15 +00:00
|
|
|
reinterpret_cast<uint32_t *>(pixel_pointer_)[0] = mode1_output_[ram_[address]];
|
|
|
|
reinterpret_cast<uint32_t *>(pixel_pointer_)[1] = mode1_output_[ram_[address+1]];
|
2017-08-01 19:16:13 +00:00
|
|
|
pixel_pointer_ += 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2017-10-22 02:30:15 +00:00
|
|
|
reinterpret_cast<uint64_t *>(pixel_pointer_)[0] = mode2_output_[ram_[address]];
|
|
|
|
reinterpret_cast<uint64_t *>(pixel_pointer_)[1] = mode2_output_[ram_[address+1]];
|
2017-08-01 19:16:13 +00:00
|
|
|
pixel_pointer_ += 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2017-10-22 02:30:15 +00:00
|
|
|
reinterpret_cast<uint16_t *>(pixel_pointer_)[0] = mode3_output_[ram_[address]];
|
|
|
|
reinterpret_cast<uint16_t *>(pixel_pointer_)[1] = mode3_output_[ram_[address+1]];
|
2017-08-02 01:51:41 +00:00
|
|
|
pixel_pointer_ += 4;
|
2017-08-01 19:16:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2017-08-01 11:24:29 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// flush the current buffer pixel if full; the CRTC allows many different display
|
|
|
|
// widths so it's not necessarily possible to predict the correct number in advance
|
|
|
|
// and using the upper bound could lead to inefficient behaviour
|
2017-08-01 11:24:29 +00:00
|
|
|
if(pixel_pointer_ == pixel_data_ + 320) {
|
2018-11-16 02:32:22 +00:00
|
|
|
crt_.output_data(cycles_ * 16, size_t(cycles_ * 16 / pixel_divider_));
|
2017-08-01 11:24:29 +00:00
|
|
|
pixel_pointer_ = pixel_data_ = nullptr;
|
2017-08-01 19:16:13 +00:00
|
|
|
cycles_ = 0;
|
2017-08-01 11:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-26 18:07:51 +00:00
|
|
|
}
|
2017-08-01 11:24:29 +00:00
|
|
|
|
2017-08-26 18:07:51 +00:00
|
|
|
/*!
|
2018-05-13 19:34:31 +00:00
|
|
|
The CRTC entry function for phase 2 of each bus cycle, in which the next sync line state becomes
|
2017-08-26 18:07:51 +00:00
|
|
|
visible early. The CPC uses changes in sync to clock the interrupt timer.
|
|
|
|
*/
|
|
|
|
void perform_bus_cycle_phase2(const Motorola::CRTC::BusState &state) {
|
2017-08-04 20:36:55 +00:00
|
|
|
// check for a trailing CRTC hsync; if one occurred then that's the trigger potentially to change
|
2017-08-03 00:37:26 +00:00
|
|
|
// modes, and should also be sent on to the interrupt timer
|
2017-08-01 19:49:16 +00:00
|
|
|
if(was_hsync_ && !state.hsync) {
|
2017-08-01 19:16:13 +00:00
|
|
|
if(mode_ != next_mode_) {
|
|
|
|
mode_ = next_mode_;
|
|
|
|
switch(mode_) {
|
|
|
|
default:
|
|
|
|
case 0: pixel_divider_ = 4; break;
|
|
|
|
case 1: pixel_divider_ = 2; break;
|
|
|
|
case 2: pixel_divider_ = 1; break;
|
|
|
|
}
|
2017-08-20 02:19:46 +00:00
|
|
|
build_mode_table();
|
2017-08-01 19:16:13 +00:00
|
|
|
}
|
2017-08-01 19:49:16 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
interrupt_timer_.signal_hsync();
|
2017-08-01 19:16:13 +00:00
|
|
|
}
|
2017-08-01 19:49:16 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// check for a leading vsync; that also needs to be communicated to the interrupt timer
|
2017-08-01 19:49:16 +00:00
|
|
|
if(!was_vsync_ && state.vsync) {
|
2017-08-02 02:15:39 +00:00
|
|
|
interrupt_timer_.signal_vsync();
|
2017-08-01 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// update current state for edge detection next time around
|
2017-08-01 19:49:16 +00:00
|
|
|
was_vsync_ = state.vsync;
|
2017-08-01 19:16:13 +00:00
|
|
|
was_hsync_ = state.hsync;
|
2017-08-01 02:32:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 04:44:21 +00:00
|
|
|
/// Sets the destination for output.
|
2018-11-15 02:52:57 +00:00
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target) {
|
2018-11-16 02:32:22 +00:00
|
|
|
crt_.set_scan_target(scan_target);
|
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2018-11-30 04:44:21 +00:00
|
|
|
/// Sets the type of display.
|
|
|
|
void set_display_type(Outputs::Display::DisplayType display_type) {
|
|
|
|
crt_.set_display_type(display_type);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
Sets the next video mode. Per the documentation, mode changes take effect only at the end of line,
|
|
|
|
not immediately. So next means "as of the end of this line".
|
|
|
|
*/
|
2017-08-01 19:16:13 +00:00
|
|
|
void set_next_mode(int mode) {
|
|
|
|
next_mode_ = mode;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Palette management: selects a pen to modify.
|
2017-08-01 19:16:13 +00:00
|
|
|
void select_pen(int pen) {
|
|
|
|
pen_ = pen;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Palette management: sets the colour of the selected pen.
|
2017-08-01 19:16:13 +00:00
|
|
|
void set_colour(uint8_t colour) {
|
|
|
|
if(pen_ & 16) {
|
2017-08-04 16:13:05 +00:00
|
|
|
// If border is[/was] currently being output, flush what should have been
|
|
|
|
// drawn in the old colour.
|
2018-06-21 02:38:54 +00:00
|
|
|
if(previous_output_mode_ == OutputMode::Border) {
|
2017-08-04 16:13:05 +00:00
|
|
|
output_border(cycles_);
|
|
|
|
cycles_ = 0;
|
|
|
|
}
|
2017-08-01 19:16:13 +00:00
|
|
|
border_ = mapped_palette_value(colour);
|
|
|
|
} else {
|
|
|
|
palette_[pen_] = mapped_palette_value(colour);
|
2017-08-20 02:19:46 +00:00
|
|
|
patch_mode_table(pen_);
|
2017-08-01 19:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:32:04 +00:00
|
|
|
private:
|
2018-11-03 23:58:44 +00:00
|
|
|
void output_border(int length) {
|
2018-11-16 02:32:22 +00:00
|
|
|
uint8_t *colour_pointer = static_cast<uint8_t *>(crt_.begin_data(1));
|
2017-08-04 16:13:05 +00:00
|
|
|
if(colour_pointer) *colour_pointer = border_;
|
2018-11-16 02:32:22 +00:00
|
|
|
crt_.output_level(length * 16);
|
2017-08-04 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 14:13:23 +00:00
|
|
|
#define Mode0Colour0(c) ((c & 0x80) >> 7) | ((c & 0x20) >> 3) | ((c & 0x08) >> 2) | ((c & 0x02) << 2)
|
|
|
|
#define Mode0Colour1(c) ((c & 0x40) >> 6) | ((c & 0x10) >> 2) | ((c & 0x04) >> 1) | ((c & 0x01) << 3)
|
|
|
|
|
|
|
|
#define Mode1Colour0(c) ((c & 0x80) >> 7) | ((c & 0x08) >> 2)
|
|
|
|
#define Mode1Colour1(c) ((c & 0x40) >> 6) | ((c & 0x04) >> 1)
|
|
|
|
#define Mode1Colour2(c) ((c & 0x20) >> 5) | ((c & 0x02) >> 0)
|
|
|
|
#define Mode1Colour3(c) ((c & 0x10) >> 4) | ((c & 0x01) << 1)
|
|
|
|
|
|
|
|
#define Mode3Colour0(c) ((c & 0x80) >> 7) | ((c & 0x08) >> 2)
|
|
|
|
#define Mode3Colour1(c) ((c & 0x40) >> 6) | ((c & 0x04) >> 1)
|
|
|
|
|
|
|
|
void establish_palette_hits() {
|
|
|
|
for(int c = 0; c < 256; c++) {
|
2017-10-04 02:04:15 +00:00
|
|
|
mode0_palette_hits_[Mode0Colour0(c)].push_back(static_cast<uint8_t>(c));
|
|
|
|
mode0_palette_hits_[Mode0Colour1(c)].push_back(static_cast<uint8_t>(c));
|
2017-08-20 14:13:23 +00:00
|
|
|
|
2017-10-04 02:04:15 +00:00
|
|
|
mode1_palette_hits_[Mode1Colour0(c)].push_back(static_cast<uint8_t>(c));
|
|
|
|
mode1_palette_hits_[Mode1Colour1(c)].push_back(static_cast<uint8_t>(c));
|
|
|
|
mode1_palette_hits_[Mode1Colour2(c)].push_back(static_cast<uint8_t>(c));
|
|
|
|
mode1_palette_hits_[Mode1Colour3(c)].push_back(static_cast<uint8_t>(c));
|
2017-08-20 14:13:23 +00:00
|
|
|
|
2017-10-04 02:04:15 +00:00
|
|
|
mode3_palette_hits_[Mode3Colour0(c)].push_back(static_cast<uint8_t>(c));
|
|
|
|
mode3_palette_hits_[Mode3Colour1(c)].push_back(static_cast<uint8_t>(c));
|
2017-08-20 02:22:51 +00:00
|
|
|
}
|
2017-08-20 02:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void build_mode_table() {
|
|
|
|
switch(mode_) {
|
|
|
|
case 0:
|
2017-08-20 14:13:23 +00:00
|
|
|
// Mode 0: abcdefgh -> [gcea] [hdfb]
|
2017-08-20 02:19:46 +00:00
|
|
|
for(int c = 0; c < 256; c++) {
|
|
|
|
// prepare mode 0
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode0_pixels = reinterpret_cast<uint8_t *>(&mode0_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode0_pixels[0] = palette_[Mode0Colour0(c)];
|
|
|
|
mode0_pixels[1] = palette_[Mode0Colour1(c)];
|
2017-08-20 02:19:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
for(int c = 0; c < 256; c++) {
|
|
|
|
// prepare mode 1
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode1_pixels = reinterpret_cast<uint8_t *>(&mode1_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode1_pixels[0] = palette_[Mode1Colour0(c)];
|
|
|
|
mode1_pixels[1] = palette_[Mode1Colour1(c)];
|
|
|
|
mode1_pixels[2] = palette_[Mode1Colour2(c)];
|
|
|
|
mode1_pixels[3] = palette_[Mode1Colour3(c)];
|
2017-08-20 02:19:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
for(int c = 0; c < 256; c++) {
|
|
|
|
// prepare mode 2
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode2_pixels = reinterpret_cast<uint8_t *>(&mode2_output_[c]);
|
2017-08-20 02:19:46 +00:00
|
|
|
mode2_pixels[0] = palette_[((c & 0x80) >> 7)];
|
|
|
|
mode2_pixels[1] = palette_[((c & 0x40) >> 6)];
|
|
|
|
mode2_pixels[2] = palette_[((c & 0x20) >> 5)];
|
|
|
|
mode2_pixels[3] = palette_[((c & 0x10) >> 4)];
|
|
|
|
mode2_pixels[4] = palette_[((c & 0x08) >> 3)];
|
|
|
|
mode2_pixels[5] = palette_[((c & 0x04) >> 2)];
|
|
|
|
mode2_pixels[6] = palette_[((c & 0x03) >> 1)];
|
|
|
|
mode2_pixels[7] = palette_[((c & 0x01) >> 0)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
for(int c = 0; c < 256; c++) {
|
|
|
|
// prepare mode 3
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode3_pixels = reinterpret_cast<uint8_t *>(&mode3_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode3_pixels[0] = palette_[Mode3Colour0(c)];
|
|
|
|
mode3_pixels[1] = palette_[Mode3Colour1(c)];
|
2017-08-20 02:19:46 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-08-02 02:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 14:13:23 +00:00
|
|
|
void patch_mode_table(int pen) {
|
|
|
|
switch(mode_) {
|
|
|
|
case 0: {
|
|
|
|
for(uint8_t c : mode0_palette_hits_[pen]) {
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode0_pixels = reinterpret_cast<uint8_t *>(&mode0_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode0_pixels[0] = palette_[Mode0Colour0(c)];
|
|
|
|
mode0_pixels[1] = palette_[Mode0Colour1(c)];
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 1:
|
|
|
|
if(pen > 3) return;
|
|
|
|
for(uint8_t c : mode1_palette_hits_[pen]) {
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode1_pixels = reinterpret_cast<uint8_t *>(&mode1_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode1_pixels[0] = palette_[Mode1Colour0(c)];
|
|
|
|
mode1_pixels[1] = palette_[Mode1Colour1(c)];
|
|
|
|
mode1_pixels[2] = palette_[Mode1Colour2(c)];
|
|
|
|
mode1_pixels[3] = palette_[Mode1Colour3(c)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(pen > 1) return;
|
|
|
|
// Whichever pen this is, there's only one table entry it doesn't touch, so just
|
|
|
|
// rebuild the whole thing.
|
|
|
|
build_mode_table();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if(pen > 3) return;
|
|
|
|
// Same argument applies here as to case 1, as the unused bits aren't masked out.
|
|
|
|
for(uint8_t c : mode3_palette_hits_[pen]) {
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *mode3_pixels = reinterpret_cast<uint8_t *>(&mode3_output_[c]);
|
2017-08-20 14:13:23 +00:00
|
|
|
mode3_pixels[0] = palette_[Mode3Colour0(c)];
|
|
|
|
mode3_pixels[1] = palette_[Mode3Colour1(c)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef Mode0Colour0
|
|
|
|
#undef Mode0Colour1
|
|
|
|
|
|
|
|
#undef Mode1Colour0
|
|
|
|
#undef Mode1Colour1
|
|
|
|
#undef Mode1Colour2
|
|
|
|
#undef Mode1Colour3
|
|
|
|
|
|
|
|
#undef Mode3Colour0
|
|
|
|
#undef Mode3Colour1
|
|
|
|
|
2017-08-01 19:16:13 +00:00
|
|
|
uint8_t mapped_palette_value(uint8_t colour) {
|
2017-08-02 01:47:52 +00:00
|
|
|
#define COL(r, g, b) (r << 4) | (g << 2) | b
|
|
|
|
static const uint8_t mapping[32] = {
|
|
|
|
COL(1, 1, 1), COL(1, 1, 1), COL(0, 2, 1), COL(2, 2, 1),
|
|
|
|
COL(0, 0, 1), COL(2, 0, 1), COL(0, 1, 1), COL(2, 1, 1),
|
|
|
|
COL(2, 0, 1), COL(2, 2, 1), COL(2, 2, 0), COL(2, 2, 2),
|
2017-08-04 16:13:05 +00:00
|
|
|
COL(2, 0, 0), COL(2, 0, 2), COL(2, 1, 0), COL(2, 1, 2),
|
2017-08-02 01:47:52 +00:00
|
|
|
COL(0, 0, 1), COL(0, 2, 1), COL(0, 2, 0), COL(0, 2, 2),
|
|
|
|
COL(0, 0, 0), COL(0, 0, 2), COL(0, 1, 0), COL(0, 1, 2),
|
|
|
|
COL(1, 0, 1), COL(1, 2, 1), COL(1, 2, 0), COL(1, 2, 2),
|
|
|
|
COL(1, 0, 0), COL(1, 0, 2), COL(1, 1, 0), COL(1, 1, 2),
|
|
|
|
};
|
|
|
|
#undef COL
|
|
|
|
return mapping[colour];
|
2017-08-01 19:16:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 02:38:54 +00:00
|
|
|
enum class OutputMode {
|
|
|
|
Sync,
|
|
|
|
Blank,
|
|
|
|
ColourBurst,
|
|
|
|
Border,
|
|
|
|
Pixels
|
|
|
|
} previous_output_mode_ = OutputMode::Sync;
|
2018-11-03 23:58:44 +00:00
|
|
|
int cycles_ = 0;
|
2017-08-04 20:36:55 +00:00
|
|
|
|
2018-06-21 02:38:54 +00:00
|
|
|
bool was_hsync_ = false, was_vsync_ = false;
|
2017-10-18 00:50:46 +00:00
|
|
|
int cycles_into_hsync_ = 0;
|
2017-08-04 20:36:55 +00:00
|
|
|
|
2018-11-16 02:32:22 +00:00
|
|
|
Outputs::CRT::CRT crt_;
|
2017-10-18 00:50:46 +00:00
|
|
|
uint8_t *pixel_data_ = nullptr, *pixel_pointer_ = nullptr;
|
2017-08-01 11:34:12 +00:00
|
|
|
|
2017-10-18 00:50:46 +00:00
|
|
|
uint8_t *ram_ = nullptr;
|
2017-08-01 19:16:13 +00:00
|
|
|
|
2017-10-18 00:50:46 +00:00
|
|
|
int next_mode_ = 2, mode_ = 2;
|
2017-08-01 19:16:13 +00:00
|
|
|
|
2018-11-03 23:58:44 +00:00
|
|
|
int pixel_divider_ = 1;
|
2017-08-01 19:16:13 +00:00
|
|
|
uint16_t mode0_output_[256];
|
|
|
|
uint32_t mode1_output_[256];
|
|
|
|
uint64_t mode2_output_[256];
|
2017-08-02 01:51:41 +00:00
|
|
|
uint16_t mode3_output_[256];
|
2017-08-01 19:16:13 +00:00
|
|
|
|
2017-08-20 14:13:23 +00:00
|
|
|
std::vector<uint8_t> mode0_palette_hits_[16];
|
|
|
|
std::vector<uint8_t> mode1_palette_hits_[4];
|
|
|
|
std::vector<uint8_t> mode3_palette_hits_[4];
|
|
|
|
|
2017-10-18 00:50:46 +00:00
|
|
|
int pen_ = 0;
|
2017-08-01 19:16:13 +00:00
|
|
|
uint8_t palette_[16];
|
2017-10-18 00:50:46 +00:00
|
|
|
uint8_t border_ = 0;
|
2017-08-01 19:49:16 +00:00
|
|
|
|
2017-08-02 02:15:39 +00:00
|
|
|
InterruptTimer &interrupt_timer_;
|
2017-08-01 02:32:04 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
2017-08-16 02:47:17 +00:00
|
|
|
Holds and vends the current keyboard state, acting as the AY's port handler.
|
2018-06-22 02:46:10 +00:00
|
|
|
Also owns the joysticks.
|
2017-08-03 00:37:26 +00:00
|
|
|
*/
|
2017-08-16 02:47:17 +00:00
|
|
|
class KeyboardState: public GI::AY38910::PortHandler {
|
|
|
|
public:
|
2018-06-22 02:46:10 +00:00
|
|
|
KeyboardState() {
|
|
|
|
joysticks_.emplace_back(new Joystick(rows_[9]));
|
|
|
|
joysticks_.emplace_back(new Joystick(joy2_state_));
|
|
|
|
}
|
2017-08-16 02:47:17 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the row currently being reported to the AY.
|
|
|
|
*/
|
|
|
|
void set_row(int row) {
|
2018-06-14 21:23:47 +00:00
|
|
|
row_ = static_cast<size_t>(row);
|
2017-08-16 02:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Reports the state of the currently-selected row as Port A to the AY.
|
|
|
|
*/
|
|
|
|
uint8_t get_port_input(bool port_b) {
|
2018-06-14 21:23:47 +00:00
|
|
|
if(!port_b && row_ < sizeof(rows_)) {
|
2018-06-22 02:46:10 +00:00
|
|
|
return (row_ == 6) ? rows_[row_] & joy2_state_ : rows_[row_];
|
2017-08-16 02:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets whether @c key on line @c line is currently pressed.
|
|
|
|
*/
|
|
|
|
void set_is_pressed(bool is_pressed, int line, int key) {
|
|
|
|
int mask = 1 << key;
|
2018-06-14 21:23:47 +00:00
|
|
|
assert(static_cast<size_t>(line) < sizeof(rows_));
|
2017-08-16 02:47:17 +00:00
|
|
|
if(is_pressed) rows_[line] &= ~mask; else rows_[line] |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets all keys as currently unpressed.
|
|
|
|
*/
|
|
|
|
void clear_all_keys() {
|
2018-06-14 21:23:47 +00:00
|
|
|
memset(rows_, 0xff, sizeof(rows_));
|
2017-08-16 02:47:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:46:10 +00:00
|
|
|
std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() {
|
|
|
|
return joysticks_;
|
|
|
|
}
|
|
|
|
|
2017-08-16 02:47:17 +00:00
|
|
|
private:
|
2018-06-22 02:46:10 +00:00
|
|
|
uint8_t joy2_state_ = 0xff;
|
|
|
|
uint8_t rows_[10] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|
|
|
size_t row_ = 0;
|
|
|
|
std::vector<std::unique_ptr<Inputs::Joystick>> joysticks_;
|
|
|
|
|
|
|
|
class Joystick: public Inputs::ConcreteJoystick {
|
|
|
|
public:
|
|
|
|
Joystick(uint8_t &state) :
|
|
|
|
ConcreteJoystick({
|
|
|
|
Input(Input::Up),
|
|
|
|
Input(Input::Down),
|
|
|
|
Input(Input::Left),
|
|
|
|
Input(Input::Right),
|
|
|
|
Input(Input::Fire, 0),
|
|
|
|
Input(Input::Fire, 1),
|
|
|
|
}),
|
|
|
|
state_(state) {}
|
|
|
|
|
|
|
|
void did_set_input(const Input &input, bool is_active) override {
|
|
|
|
uint8_t mask = 0;
|
|
|
|
switch(input.type) {
|
|
|
|
default: return;
|
|
|
|
case Input::Up: mask = 0x01; break;
|
|
|
|
case Input::Down: mask = 0x02; break;
|
|
|
|
case Input::Left: mask = 0x04; break;
|
|
|
|
case Input::Right: mask = 0x08; break;
|
|
|
|
case Input::Fire:
|
|
|
|
if(input.info.control.index >= 2) return;
|
|
|
|
mask = input.info.control.index ? 0x20 : 0x10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_active) state_ &= ~mask; else state_ |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t &state_;
|
|
|
|
};
|
2017-08-01 21:52:05 +00:00
|
|
|
};
|
|
|
|
|
2017-08-06 13:45:16 +00:00
|
|
|
/*!
|
|
|
|
Wraps the 8272 so as to provide proper clocking and RPM counts, and just directly
|
|
|
|
exposes motor control, applying the same value to all drives.
|
|
|
|
*/
|
2017-08-14 12:38:00 +00:00
|
|
|
class FDC: public Intel::i8272::i8272 {
|
|
|
|
private:
|
|
|
|
Intel::i8272::BusHandler bus_handler_;
|
2017-09-12 01:25:26 +00:00
|
|
|
std::shared_ptr<Storage::Disk::Drive> drive_;
|
2017-08-14 12:38:00 +00:00
|
|
|
|
2017-08-06 13:45:16 +00:00
|
|
|
public:
|
2017-09-12 01:25:26 +00:00
|
|
|
FDC() :
|
|
|
|
i8272(bus_handler_, Cycles(8000000)),
|
2017-09-16 01:18:36 +00:00
|
|
|
drive_(new Storage::Disk::Drive(8000000, 300, 1)) {
|
2017-09-12 01:25:26 +00:00
|
|
|
set_drive(drive_);
|
|
|
|
}
|
2017-08-06 13:45:16 +00:00
|
|
|
|
|
|
|
void set_motor_on(bool on) {
|
2017-09-12 01:25:26 +00:00
|
|
|
drive_->set_motor_on(on);
|
|
|
|
}
|
|
|
|
|
|
|
|
void select_drive(int c) {
|
|
|
|
// TODO: support more than one drive.
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_disk(std::shared_ptr<Storage::Disk::Disk> disk, int drive) {
|
|
|
|
drive_->set_disk(disk);
|
2017-08-06 13:45:16 +00:00
|
|
|
}
|
2018-05-12 01:45:46 +00:00
|
|
|
|
|
|
|
void set_activity_observer(Activity::Observer *observer) {
|
|
|
|
drive_->set_activity_observer(observer, "Drive 1", true);
|
|
|
|
}
|
2017-08-06 13:45:16 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
Provides the mechanism of receipt for input and output of the 8255's various ports.
|
|
|
|
*/
|
2017-08-01 20:34:13 +00:00
|
|
|
class i8255PortHandler : public Intel::i8255::PortHandler {
|
|
|
|
public:
|
2017-08-03 00:37:26 +00:00
|
|
|
i8255PortHandler(
|
2017-08-16 02:47:17 +00:00
|
|
|
KeyboardState &key_state,
|
2017-08-26 16:59:59 +00:00
|
|
|
const Motorola::CRTC::CRTC6845<CRTCBusHandler> &crtc,
|
2017-08-03 00:37:26 +00:00
|
|
|
AYDeferrer &ay,
|
|
|
|
Storage::Tape::BinaryTapePlayer &tape_player) :
|
|
|
|
ay_(ay),
|
2017-11-11 03:47:10 +00:00
|
|
|
crtc_(crtc),
|
|
|
|
key_state_(key_state),
|
2017-08-03 00:37:26 +00:00
|
|
|
tape_player_(tape_player) {}
|
|
|
|
|
|
|
|
/// The i8255 will call this to set a new output value of @c value for @c port.
|
2017-08-01 20:34:13 +00:00
|
|
|
void set_value(int port, uint8_t value) {
|
|
|
|
switch(port) {
|
2017-08-01 21:01:58 +00:00
|
|
|
case 0:
|
2017-08-03 00:37:26 +00:00
|
|
|
// Port A is connected to the AY's data bus.
|
2017-08-05 13:12:17 +00:00
|
|
|
ay_.update();
|
2017-12-18 02:26:06 +00:00
|
|
|
ay_.ay().set_data_input(value);
|
2017-08-01 21:01:58 +00:00
|
|
|
break;
|
2017-08-02 00:39:10 +00:00
|
|
|
case 1:
|
2017-08-03 00:37:26 +00:00
|
|
|
// Port B is an input only. So output goes nowehere.
|
2017-08-02 00:39:10 +00:00
|
|
|
break;
|
2017-08-01 21:52:05 +00:00
|
|
|
case 2: {
|
2017-08-03 00:37:26 +00:00
|
|
|
// The low four bits of the value sent to Port C select a keyboard line.
|
2017-08-01 21:52:05 +00:00
|
|
|
int key_row = value & 15;
|
2017-08-16 02:47:17 +00:00
|
|
|
key_state_.set_row(key_row);
|
2017-08-03 00:37:26 +00:00
|
|
|
|
|
|
|
// Bit 4 sets the tape motor on or off.
|
|
|
|
tape_player_.set_motor_control((value & 0x10) ? true : false);
|
|
|
|
// Bit 5 sets the current tape output level
|
|
|
|
tape_player_.set_tape_output((value & 0x20) ? true : false);
|
|
|
|
|
|
|
|
// Bits 6 and 7 set BDIR and BC1 for the AY.
|
2017-12-18 02:26:06 +00:00
|
|
|
ay_.ay().set_control_lines(
|
2017-08-01 21:01:58 +00:00
|
|
|
(GI::AY38910::ControlLines)(
|
2017-08-01 21:06:57 +00:00
|
|
|
((value & 0x80) ? GI::AY38910::BDIR : 0) |
|
2017-08-01 21:05:11 +00:00
|
|
|
((value & 0x40) ? GI::AY38910::BC1 : 0) |
|
|
|
|
GI::AY38910::BC2
|
2017-08-01 21:01:58 +00:00
|
|
|
));
|
2017-08-01 21:52:05 +00:00
|
|
|
} break;
|
2017-08-01 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// The i8255 will call this to obtain a new input for @c port.
|
2017-08-01 20:34:13 +00:00
|
|
|
uint8_t get_value(int port) {
|
|
|
|
switch(port) {
|
2017-12-18 02:26:06 +00:00
|
|
|
case 0: return ay_.ay().get_data_output(); // Port A is wired to the AY
|
2017-08-02 17:50:14 +00:00
|
|
|
case 1: return
|
2017-08-26 16:59:59 +00:00
|
|
|
(crtc_.get_bus_state().vsync ? 0x01 : 0x00) | // Bit 0 returns CRTC vsync.
|
2017-08-03 00:37:26 +00:00
|
|
|
(tape_player_.get_input() ? 0x80 : 0x00) | // Bit 7 returns cassette input.
|
|
|
|
0x7e; // Bits unimplemented:
|
|
|
|
//
|
|
|
|
// Bit 6: printer ready (1 = not)
|
|
|
|
// Bit 5: the expansion port /EXP pin, so depends on connected hardware
|
|
|
|
// Bit 4: 50/60Hz switch (1 = 50Hz)
|
2018-05-13 19:43:03 +00:00
|
|
|
// Bits 1-3: distributor ID (111 = Amstrad)
|
2017-08-03 00:37:26 +00:00
|
|
|
default: return 0xff;
|
2017-08-01 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 21:01:58 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-02 11:21:33 +00:00
|
|
|
AYDeferrer &ay_;
|
2017-08-26 16:59:59 +00:00
|
|
|
const Motorola::CRTC::CRTC6845<CRTCBusHandler> &crtc_;
|
2017-11-11 03:47:10 +00:00
|
|
|
KeyboardState &key_state_;
|
2017-08-02 17:50:14 +00:00
|
|
|
Storage::Tape::BinaryTapePlayer &tape_player_;
|
2017-08-01 20:15:19 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/*!
|
|
|
|
The actual Amstrad CPC implementation; tying the 8255, 6845 and AY to the Z80.
|
|
|
|
*/
|
2018-07-11 00:00:46 +00:00
|
|
|
template <bool has_fdc> class ConcreteMachine:
|
2018-03-09 20:19:02 +00:00
|
|
|
public CRTMachine::Machine,
|
2018-07-11 01:32:28 +00:00
|
|
|
public MediaTarget::Machine,
|
2018-10-25 01:59:30 +00:00
|
|
|
public KeyboardMachine::MappedMachine,
|
2017-08-11 15:21:07 +00:00
|
|
|
public Utility::TypeRecipient,
|
2017-08-03 02:11:03 +00:00
|
|
|
public CPU::Z80::BusHandler,
|
2018-05-28 03:17:06 +00:00
|
|
|
public ClockingHint::Observer,
|
2018-06-22 00:00:49 +00:00
|
|
|
public Configurable::Device,
|
2018-06-22 02:46:10 +00:00
|
|
|
public JoystickMachine::Machine,
|
2018-05-12 01:45:46 +00:00
|
|
|
public Machine,
|
|
|
|
public Activity::Source {
|
2017-08-01 02:32:04 +00:00
|
|
|
public:
|
2018-07-11 00:00:46 +00:00
|
|
|
ConcreteMachine(const Analyser::Static::AmstradCPC::Target &target, const ROMMachine::ROMFetcher &rom_fetcher) :
|
2017-08-03 02:11:03 +00:00
|
|
|
z80_(*this),
|
2017-08-02 02:15:39 +00:00
|
|
|
crtc_bus_handler_(ram_, interrupt_timer_),
|
2017-11-11 03:47:10 +00:00
|
|
|
crtc_(Motorola::CRTC::HD6845S, crtc_bus_handler_),
|
2017-08-26 16:59:59 +00:00
|
|
|
i8255_port_handler_(key_state_, crtc_, ay_, tape_player_),
|
2017-11-11 03:47:10 +00:00
|
|
|
i8255_(i8255_port_handler_),
|
2017-11-11 03:57:03 +00:00
|
|
|
tape_player_(8000000),
|
|
|
|
crtc_counter_(HalfCycles(4)) // This starts the CRTC exactly out of phase with the CPU's memory accesses
|
|
|
|
{
|
2017-08-01 02:39:25 +00:00
|
|
|
// primary clock is 4Mhz
|
|
|
|
set_clock_rate(4000000);
|
2017-08-11 15:21:07 +00:00
|
|
|
|
|
|
|
// ensure memory starts in a random state
|
|
|
|
Memory::Fuzz(ram_, sizeof(ram_));
|
2017-08-20 16:05:00 +00:00
|
|
|
|
2017-08-20 16:21:02 +00:00
|
|
|
// register this class as the sleep observer for the FDC and tape
|
2018-05-28 03:17:06 +00:00
|
|
|
fdc_.set_clocking_hint_observer(this);
|
|
|
|
tape_player_.set_clocking_hint_observer(this);
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2018-07-11 00:00:46 +00:00
|
|
|
// install the keyboard state class as the AY port handler
|
2017-12-18 02:26:06 +00:00
|
|
|
ay_.ay().set_port_handler(&key_state_);
|
2018-07-11 00:00:46 +00:00
|
|
|
|
|
|
|
// construct the list of necessary ROMs
|
|
|
|
std::vector<std::string> required_roms = {"amsdos.rom"};
|
|
|
|
std::string model_number;
|
|
|
|
switch(target.model) {
|
|
|
|
default:
|
|
|
|
model_number = "6128";
|
|
|
|
has_128k_ = true;
|
|
|
|
break;
|
|
|
|
case Analyser::Static::AmstradCPC::Target::Model::CPC464:
|
|
|
|
model_number = "464";
|
|
|
|
has_128k_ = false;
|
|
|
|
break;
|
|
|
|
case Analyser::Static::AmstradCPC::Target::Model::CPC664:
|
|
|
|
model_number = "664";
|
|
|
|
has_128k_ = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
required_roms.push_back("os" + model_number + ".rom");
|
|
|
|
required_roms.push_back("basic" + model_number + ".rom");
|
|
|
|
|
|
|
|
// fetch and verify the ROMs
|
|
|
|
const auto roms = rom_fetcher("AmstradCPC", required_roms);
|
|
|
|
|
|
|
|
for(std::size_t index = 0; index < roms.size(); ++index) {
|
|
|
|
auto &data = roms[index];
|
|
|
|
if(!data) throw ROMMachine::Error::MissingROMs;
|
|
|
|
roms_[static_cast<int>(index)] = std::move(*data);
|
|
|
|
roms_[static_cast<int>(index)].resize(16384);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Establish default memory map
|
|
|
|
upper_rom_is_paged_ = true;
|
|
|
|
upper_rom_ = ROMType::BASIC;
|
|
|
|
|
|
|
|
write_pointers_[0] = &ram_[0];
|
|
|
|
write_pointers_[1] = &ram_[16384];
|
|
|
|
write_pointers_[2] = &ram_[32768];
|
|
|
|
write_pointers_[3] = &ram_[49152];
|
|
|
|
|
|
|
|
read_pointers_[0] = roms_[ROMType::OS].data();
|
|
|
|
read_pointers_[1] = write_pointers_[1];
|
|
|
|
read_pointers_[2] = write_pointers_[2];
|
|
|
|
read_pointers_[3] = roms_[upper_rom_].data();
|
|
|
|
|
|
|
|
// Type whatever is required.
|
|
|
|
if(!target.loading_command.empty()) {
|
|
|
|
type_string(target.loading_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
insert_media(target.media);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// The entry point for performing a partial Z80 machine cycle.
|
2017-08-22 01:07:10 +00:00
|
|
|
forceinline HalfCycles perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
2017-08-01 02:39:25 +00:00
|
|
|
// Amstrad CPC timing scheme: assert WAIT for three out of four cycles
|
|
|
|
clock_offset_ = (clock_offset_ + cycle.length) & HalfCycles(7);
|
2017-08-03 02:11:03 +00:00
|
|
|
z80_.set_wait_line(clock_offset_ >= HalfCycles(2));
|
2017-08-01 02:39:25 +00:00
|
|
|
|
|
|
|
// Update the CRTC once every eight half cycles; aiming for half-cycle 4 as
|
|
|
|
// per the initial seed to the crtc_counter_, but any time in the final four
|
|
|
|
// will do as it's safe to conclude that nobody else has touched video RAM
|
|
|
|
// during that whole window
|
|
|
|
crtc_counter_ += cycle.length;
|
2017-08-04 02:00:30 +00:00
|
|
|
Cycles crtc_cycles = crtc_counter_.divide_cycles(Cycles(4));
|
|
|
|
if(crtc_cycles > Cycles(0)) crtc_.run_for(crtc_cycles);
|
2017-08-26 18:07:51 +00:00
|
|
|
|
|
|
|
// Check whether that prompted a change in the interrupt line. If so then date
|
|
|
|
// it to whenever the cycle was triggered.
|
|
|
|
if(interrupt_timer_.request_has_changed()) z80_.set_interrupt_line(interrupt_timer_.get_request(), -crtc_counter_);
|
2017-08-01 02:39:25 +00:00
|
|
|
|
2017-08-02 17:50:14 +00:00
|
|
|
// TODO (in the player, not here): adapt it to accept an input clock rate and
|
|
|
|
// run_for as HalfCycles
|
2017-08-20 16:21:02 +00:00
|
|
|
if(!tape_player_is_sleeping_) tape_player_.run_for(cycle.length.as_int());
|
2017-08-02 17:50:14 +00:00
|
|
|
|
2017-08-11 15:21:07 +00:00
|
|
|
// Pump the AY
|
2017-08-02 11:21:33 +00:00
|
|
|
ay_.run_for(cycle.length);
|
|
|
|
|
2017-08-06 13:45:16 +00:00
|
|
|
// Clock the FDC, if connected, using a lazy scale by two
|
2018-05-28 03:55:04 +00:00
|
|
|
time_since_fdc_update_ += cycle.length;
|
2017-08-06 13:45:16 +00:00
|
|
|
|
2017-08-11 15:21:07 +00:00
|
|
|
// Update typing activity
|
|
|
|
if(typer_) typer_->run_for(cycle.length);
|
|
|
|
|
2017-08-01 02:39:25 +00:00
|
|
|
// Stop now if no action is strictly required.
|
|
|
|
if(!cycle.is_terminal()) return HalfCycles(0);
|
|
|
|
|
|
|
|
uint16_t address = cycle.address ? *cycle.address : 0x0000;
|
|
|
|
switch(cycle.operation) {
|
|
|
|
case CPU::Z80::PartialMachineCycle::ReadOpcode:
|
|
|
|
case CPU::Z80::PartialMachineCycle::Read:
|
|
|
|
*cycle.value = read_pointers_[address >> 14][address & 16383];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Write:
|
|
|
|
write_pointers_[address >> 14][address & 16383] = *cycle.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Output:
|
|
|
|
// Check for a gate array access.
|
|
|
|
if((address & 0xc000) == 0x4000) {
|
2017-08-10 16:39:19 +00:00
|
|
|
write_to_gate_array(*cycle.value);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 23:20:38 +00:00
|
|
|
// Check for an upper ROM selection
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc && !(address&0x2000)) {
|
|
|
|
upper_rom_ = (*cycle.value == 7) ? ROMType::AMSDOS : ROMType::BASIC;
|
2017-08-05 23:20:38 +00:00
|
|
|
if(upper_rom_is_paged_) read_pointers_[3] = roms_[upper_rom_].data();
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:39:25 +00:00
|
|
|
// Check for a CRTC access
|
|
|
|
if(!(address & 0x4000)) {
|
|
|
|
switch((address >> 8) & 3) {
|
|
|
|
case 0: crtc_.select_register(*cycle.value); break;
|
|
|
|
case 1: crtc_.set_register(*cycle.value); break;
|
2017-08-10 16:39:19 +00:00
|
|
|
default: break;
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// Check for an 8255 PIO access
|
2017-08-01 02:39:25 +00:00
|
|
|
if(!(address & 0x800)) {
|
2017-08-01 20:15:19 +00:00
|
|
|
i8255_.set_register((address >> 8) & 3, *cycle.value);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
2017-08-05 23:45:52 +00:00
|
|
|
|
|
|
|
// Check for an FDC access
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc && (address & 0x580) == 0x100) {
|
2018-05-28 03:55:04 +00:00
|
|
|
flush_fdc();
|
2017-08-06 13:45:16 +00:00
|
|
|
fdc_.set_register(address & 1, *cycle.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a disk motor access
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc && !(address & 0x580)) {
|
2018-05-28 03:55:04 +00:00
|
|
|
flush_fdc();
|
2017-08-06 13:45:16 +00:00
|
|
|
fdc_.set_motor_on(!!(*cycle.value));
|
2017-08-05 23:45:52 +00:00
|
|
|
}
|
2017-08-01 02:39:25 +00:00
|
|
|
break;
|
|
|
|
case CPU::Z80::PartialMachineCycle::Input:
|
2017-08-03 00:37:26 +00:00
|
|
|
// Default to nothing answering
|
2017-08-02 00:19:02 +00:00
|
|
|
*cycle.value = 0xff;
|
|
|
|
|
2017-08-01 02:39:25 +00:00
|
|
|
// Check for a PIO access
|
|
|
|
if(!(address & 0x800)) {
|
2017-08-10 16:39:19 +00:00
|
|
|
*cycle.value &= i8255_.get_register((address >> 8) & 3);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
2017-08-05 23:45:52 +00:00
|
|
|
|
|
|
|
// Check for an FDC access
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc && (address & 0x580) == 0x100) {
|
2018-05-28 03:55:04 +00:00
|
|
|
flush_fdc();
|
2017-08-10 16:39:19 +00:00
|
|
|
*cycle.value &= fdc_.get_register(address & 1);
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:34:31 +00:00
|
|
|
// Check for a CRTC access; the below is not a typo, the CRTC can be selected
|
2017-08-10 16:39:19 +00:00
|
|
|
// for writing via an input, and will sample whatever happens to be available
|
|
|
|
if(!(address & 0x4000)) {
|
|
|
|
switch((address >> 8) & 3) {
|
|
|
|
case 0: crtc_.select_register(*cycle.value); break;
|
|
|
|
case 1: crtc_.set_register(*cycle.value); break;
|
|
|
|
case 2: *cycle.value &= crtc_.get_status(); break;
|
|
|
|
case 3: *cycle.value &= crtc_.get_register(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As with the CRTC, the gate array will sample the bus if the address decoding
|
|
|
|
// implies that it should, unaware of data direction
|
|
|
|
if((address & 0xc000) == 0x4000) {
|
|
|
|
write_to_gate_array(*cycle.value);
|
2017-08-05 23:45:52 +00:00
|
|
|
}
|
2017-08-01 02:39:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Interrupt:
|
2017-08-03 00:37:26 +00:00
|
|
|
// Nothing is loaded onto the bus during an interrupt acknowledge, but
|
|
|
|
// the fact of the acknowledge needs to be posted on to the interrupt timer.
|
2017-08-01 02:39:25 +00:00
|
|
|
*cycle.value = 0xff;
|
2017-08-03 00:37:26 +00:00
|
|
|
interrupt_timer_.signal_interrupt_acknowledge();
|
2017-08-01 02:39:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// This implementation doesn't use time-stuffing; once in-phase waits won't be longer
|
|
|
|
// than a single cycle so there's no real performance benefit to trying to find the
|
|
|
|
// next non-wait when a wait cycle comes in, and there'd be no benefit to reproducing
|
|
|
|
// the Z80's knowledge of where wait cycles occur here.
|
2017-08-01 02:39:25 +00:00
|
|
|
return HalfCycles(0);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Another Z80 entry point; indicates that a partcular run request has concluded.
|
2017-08-01 21:01:58 +00:00
|
|
|
void flush() {
|
2017-08-03 00:37:26 +00:00
|
|
|
// Just flush the AY.
|
2017-08-05 13:18:55 +00:00
|
|
|
ay_.update();
|
2017-08-02 11:21:33 +00:00
|
|
|
ay_.flush();
|
2018-05-28 03:55:04 +00:00
|
|
|
flush_fdc();
|
2017-08-01 21:01:58 +00:00
|
|
|
}
|
2017-08-01 02:39:25 +00:00
|
|
|
|
2018-11-30 04:44:21 +00:00
|
|
|
/// A CRTMachine function; sets the destination for video.
|
2018-11-15 02:52:57 +00:00
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target) override final {
|
|
|
|
crtc_bus_handler_.set_scan_target(scan_target);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2018-11-30 04:44:21 +00:00
|
|
|
/// A CRTMachine function; sets the output display type.
|
|
|
|
void set_display_type(Outputs::Display::DisplayType display_type) override final {
|
|
|
|
crtc_bus_handler_.set_display_type(display_type);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// @returns the speaker in use.
|
2017-12-18 02:26:06 +00:00
|
|
|
Outputs::Speaker::Speaker *get_speaker() override final {
|
2017-08-02 11:21:33 +00:00
|
|
|
return ay_.get_speaker();
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
/// Wires virtual-dispatched CRTMachine run_for requests to the static Z80 method.
|
2017-08-22 01:56:42 +00:00
|
|
|
void run_for(const Cycles cycles) override final {
|
2017-08-03 02:11:03 +00:00
|
|
|
z80_.run_for(cycles);
|
2017-08-01 02:39:25 +00:00
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2018-01-25 02:48:44 +00:00
|
|
|
bool insert_media(const Analyser::Static::Media &media) override final {
|
2017-08-03 00:37:26 +00:00
|
|
|
// If there are any tapes supplied, use the first of them.
|
2017-08-17 14:48:29 +00:00
|
|
|
if(!media.tapes.empty()) {
|
|
|
|
tape_player_.set_tape(media.tapes.front());
|
2017-08-02 17:50:14 +00:00
|
|
|
}
|
2017-08-05 23:20:38 +00:00
|
|
|
|
2017-08-06 17:24:14 +00:00
|
|
|
// Insert up to four disks.
|
|
|
|
int c = 0;
|
2017-08-17 14:48:29 +00:00
|
|
|
for(auto &disk : media.disks) {
|
2017-08-06 17:24:14 +00:00
|
|
|
fdc_.set_disk(disk, c);
|
|
|
|
c++;
|
|
|
|
if(c == 4) break;
|
|
|
|
}
|
2017-08-11 15:21:07 +00:00
|
|
|
|
2018-07-11 00:00:46 +00:00
|
|
|
return !media.tapes.empty() || (!media.disks.empty() && has_fdc);
|
2017-11-06 01:12:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 03:17:06 +00:00
|
|
|
void set_component_prefers_clocking(ClockingHint::Source *component, ClockingHint::Preference clocking) override final {
|
|
|
|
fdc_is_sleeping_ = fdc_.preferred_clocking() == ClockingHint::Preference::None;
|
|
|
|
tape_player_is_sleeping_ = tape_player_.preferred_clocking() == ClockingHint::Preference::None;
|
2017-08-20 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:00:49 +00:00
|
|
|
// MARK: - Keyboard
|
2017-12-29 20:26:03 +00:00
|
|
|
void type_string(const std::string &string) override final {
|
2017-08-11 15:21:07 +00:00
|
|
|
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper());
|
2017-12-29 20:26:03 +00:00
|
|
|
Utility::TypeRecipient::add_typer(string, std::move(mapper));
|
2017-08-11 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:56:42 +00:00
|
|
|
HalfCycles get_typer_delay() override final {
|
2017-08-11 15:36:03 +00:00
|
|
|
return Cycles(4000000); // Wait 1 second before typing.
|
2017-08-11 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:56:42 +00:00
|
|
|
HalfCycles get_typer_frequency() override final {
|
2017-08-17 02:12:16 +00:00
|
|
|
return Cycles(160000); // Type one character per frame.
|
2017-08-11 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// See header; sets a key as either pressed or released.
|
2017-08-22 01:56:42 +00:00
|
|
|
void set_key_state(uint16_t key, bool isPressed) override final {
|
2017-08-16 02:47:17 +00:00
|
|
|
key_state_.set_is_pressed(isPressed, key >> 4, key & 7);
|
2017-08-01 21:31:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// See header; sets all keys to released.
|
2017-08-22 01:56:42 +00:00
|
|
|
void clear_all_keys() override final {
|
2017-08-16 02:47:17 +00:00
|
|
|
key_state_.clear_all_keys();
|
2017-08-01 21:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 21:31:05 +00:00
|
|
|
KeyboardMapper *get_keyboard_mapper() override {
|
|
|
|
return &keyboard_mapper_;
|
2017-10-13 02:25:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:45:46 +00:00
|
|
|
// MARK: - Activity Source
|
|
|
|
void set_activity_observer(Activity::Observer *observer) override {
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc) fdc_.set_activity_observer(observer);
|
2018-05-12 01:45:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:00:49 +00:00
|
|
|
// MARK: - Configuration options.
|
|
|
|
std::vector<std::unique_ptr<Configurable::Option>> get_options() override {
|
|
|
|
return AmstradCPC::get_options();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_selections(const Configurable::SelectionSet &selections_by_option) override {
|
|
|
|
Configurable::Display display;
|
|
|
|
if(Configurable::get_display(selections_by_option, display)) {
|
|
|
|
set_video_signal_configurable(display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Configurable::SelectionSet get_accurate_selections() override {
|
|
|
|
Configurable::SelectionSet selection_set;
|
|
|
|
Configurable::append_display_selection(selection_set, Configurable::Display::RGB);
|
|
|
|
return selection_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
Configurable::SelectionSet get_user_friendly_selections() override {
|
|
|
|
Configurable::SelectionSet selection_set;
|
|
|
|
Configurable::append_display_selection(selection_set, Configurable::Display::RGB);
|
|
|
|
return selection_set;
|
|
|
|
}
|
2018-05-12 01:45:46 +00:00
|
|
|
|
2018-06-22 02:46:10 +00:00
|
|
|
// MARK: - Joysticks
|
|
|
|
std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() override {
|
|
|
|
return key_state_.get_joysticks();
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:32:04 +00:00
|
|
|
private:
|
2017-08-10 16:39:19 +00:00
|
|
|
inline void write_to_gate_array(uint8_t value) {
|
|
|
|
switch(value >> 6) {
|
|
|
|
case 0: crtc_bus_handler_.select_pen(value & 0x1f); break;
|
|
|
|
case 1: crtc_bus_handler_.set_colour(value & 0x1f); break;
|
|
|
|
case 2:
|
|
|
|
// Perform ROM paging.
|
2018-07-11 00:00:46 +00:00
|
|
|
read_pointers_[0] = (value & 4) ? write_pointers_[0] : roms_[ROMType::OS].data();
|
2017-08-10 16:39:19 +00:00
|
|
|
|
|
|
|
upper_rom_is_paged_ = !(value & 8);
|
|
|
|
read_pointers_[3] = upper_rom_is_paged_ ? roms_[upper_rom_].data() : write_pointers_[3];
|
|
|
|
|
|
|
|
// Reset the interrupt timer if requested.
|
|
|
|
if(value & 0x10) interrupt_timer_.reset_count();
|
|
|
|
|
|
|
|
// Post the next mode.
|
|
|
|
crtc_bus_handler_.set_next_mode(value & 3);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// Perform RAM paging, if 128kb is permitted.
|
|
|
|
if(has_128k_) {
|
|
|
|
bool adjust_low_read_pointer = read_pointers_[0] == write_pointers_[0];
|
|
|
|
bool adjust_high_read_pointer = read_pointers_[3] == write_pointers_[3];
|
|
|
|
#define RAM_BANK(x) &ram_[x * 16384]
|
|
|
|
#define RAM_CONFIG(a, b, c, d) write_pointers_[0] = RAM_BANK(a); write_pointers_[1] = RAM_BANK(b); write_pointers_[2] = RAM_BANK(c); write_pointers_[3] = RAM_BANK(d);
|
|
|
|
switch(value & 7) {
|
|
|
|
case 0: RAM_CONFIG(0, 1, 2, 3); break;
|
|
|
|
case 1: RAM_CONFIG(0, 1, 2, 7); break;
|
|
|
|
case 2: RAM_CONFIG(4, 5, 6, 7); break;
|
|
|
|
case 3: RAM_CONFIG(0, 3, 2, 7); break;
|
|
|
|
case 4: RAM_CONFIG(0, 4, 2, 3); break;
|
|
|
|
case 5: RAM_CONFIG(0, 5, 2, 3); break;
|
|
|
|
case 6: RAM_CONFIG(0, 6, 2, 3); break;
|
|
|
|
case 7: RAM_CONFIG(0, 7, 2, 3); break;
|
|
|
|
}
|
|
|
|
#undef RAM_CONFIG
|
|
|
|
#undef RAM_BANK
|
|
|
|
if(adjust_low_read_pointer) read_pointers_[0] = write_pointers_[0];
|
|
|
|
read_pointers_[1] = write_pointers_[1];
|
|
|
|
read_pointers_[2] = write_pointers_[2];
|
|
|
|
if(adjust_high_read_pointer) read_pointers_[3] = write_pointers_[3];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-27 03:08:57 +00:00
|
|
|
CPU::Z80::Processor<ConcreteMachine, false, true> z80_;
|
2017-08-03 02:11:03 +00:00
|
|
|
|
2017-08-01 02:32:04 +00:00
|
|
|
CRTCBusHandler crtc_bus_handler_;
|
|
|
|
Motorola::CRTC::CRTC6845<CRTCBusHandler> crtc_;
|
|
|
|
|
2017-08-02 11:21:33 +00:00
|
|
|
AYDeferrer ay_;
|
2017-08-01 20:15:19 +00:00
|
|
|
i8255PortHandler i8255_port_handler_;
|
|
|
|
Intel::i8255::i8255<i8255PortHandler> i8255_;
|
|
|
|
|
2017-08-06 13:45:16 +00:00
|
|
|
FDC fdc_;
|
2018-05-28 03:55:04 +00:00
|
|
|
HalfCycles time_since_fdc_update_;
|
|
|
|
void flush_fdc() {
|
|
|
|
// Clock the FDC, if connected, using a lazy scale by two
|
2018-07-11 00:00:46 +00:00
|
|
|
if(has_fdc && !fdc_is_sleeping_) {
|
2018-05-28 03:55:04 +00:00
|
|
|
fdc_.run_for(Cycles(time_since_fdc_update_.as_int()));
|
|
|
|
}
|
|
|
|
time_since_fdc_update_ = HalfCycles(0);
|
|
|
|
}
|
2017-08-05 23:45:52 +00:00
|
|
|
|
2017-08-02 02:15:39 +00:00
|
|
|
InterruptTimer interrupt_timer_;
|
2017-08-02 17:50:14 +00:00
|
|
|
Storage::Tape::BinaryTapePlayer tape_player_;
|
2017-08-02 02:15:39 +00:00
|
|
|
|
2017-08-01 02:32:04 +00:00
|
|
|
HalfCycles clock_offset_;
|
|
|
|
HalfCycles crtc_counter_;
|
2017-08-01 21:01:58 +00:00
|
|
|
HalfCycles half_cycles_since_ay_update_;
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2017-08-05 23:20:38 +00:00
|
|
|
uint8_t ram_[128 * 1024];
|
2017-08-08 20:01:56 +00:00
|
|
|
|
2018-07-11 00:00:46 +00:00
|
|
|
bool fdc_is_sleeping_;
|
2017-08-20 16:21:02 +00:00
|
|
|
bool tape_player_is_sleeping_;
|
2017-08-08 20:01:56 +00:00
|
|
|
bool has_128k_;
|
2018-07-11 00:00:46 +00:00
|
|
|
|
|
|
|
enum ROMType: int {
|
|
|
|
AMSDOS = 0, OS = 1, BASIC = 2
|
|
|
|
};
|
|
|
|
std::vector<uint8_t> roms_[3];
|
2017-08-05 23:20:38 +00:00
|
|
|
bool upper_rom_is_paged_;
|
2018-07-11 00:00:46 +00:00
|
|
|
ROMType upper_rom_;
|
2017-08-01 02:32:04 +00:00
|
|
|
|
2017-08-08 20:01:56 +00:00
|
|
|
uint8_t *ram_pages_[4];
|
2017-08-01 02:32:04 +00:00
|
|
|
uint8_t *read_pointers_[4];
|
|
|
|
uint8_t *write_pointers_[4];
|
2017-08-01 21:52:05 +00:00
|
|
|
|
|
|
|
KeyboardState key_state_;
|
2017-10-13 02:25:02 +00:00
|
|
|
AmstradCPC::KeyboardMapper keyboard_mapper_;
|
2017-08-01 02:32:04 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 02:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace AmstradCPC;
|
|
|
|
|
2017-08-03 00:37:26 +00:00
|
|
|
// See header; constructs and returns an instance of the Amstrad CPC.
|
2018-07-11 00:00:46 +00:00
|
|
|
Machine *Machine::AmstradCPC(const Analyser::Static::Target *target, const ROMMachine::ROMFetcher &rom_fetcher) {
|
|
|
|
using Target = Analyser::Static::AmstradCPC::Target;
|
|
|
|
const Target *const cpc_target = dynamic_cast<const Target *>(target);
|
|
|
|
switch(cpc_target->model) {
|
2018-11-24 03:32:32 +00:00
|
|
|
default: return new AmstradCPC::ConcreteMachine<true>(*cpc_target, rom_fetcher);
|
2018-07-11 00:00:46 +00:00
|
|
|
case Target::Model::CPC464: return new AmstradCPC::ConcreteMachine<false>(*cpc_target, rom_fetcher);
|
|
|
|
}
|
2017-08-01 02:32:04 +00:00
|
|
|
}
|
2017-08-11 16:07:48 +00:00
|
|
|
|
|
|
|
Machine::~Machine() {}
|