2017-11-25 01:43:26 +00:00
|
|
|
//
|
|
|
|
// MSX.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 24/11/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-11-25 01:43:26 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "MSX.hpp"
|
|
|
|
|
2018-09-12 01:55:03 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-01-08 00:12:52 +00:00
|
|
|
#include "DiskROM.hpp"
|
2017-11-30 03:07:30 +00:00
|
|
|
#include "Keyboard.hpp"
|
2023-01-13 04:01:11 +00:00
|
|
|
#include "MemorySlotHandler.hpp"
|
2018-01-05 03:18:18 +00:00
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
#include "../../Analyser/Static/MSX/Cartridge.hpp"
|
2018-01-05 03:18:18 +00:00
|
|
|
#include "Cartridges/ASCII8kb.hpp"
|
|
|
|
#include "Cartridges/ASCII16kb.hpp"
|
|
|
|
#include "Cartridges/Konami.hpp"
|
|
|
|
#include "Cartridges/KonamiWithSCC.hpp"
|
2017-11-30 03:07:30 +00:00
|
|
|
|
2017-11-25 02:59:54 +00:00
|
|
|
#include "../../Processors/Z80/Z80.hpp"
|
|
|
|
|
2017-11-25 03:05:50 +00:00
|
|
|
#include "../../Components/1770/1770.hpp"
|
|
|
|
#include "../../Components/8255/i8255.hpp"
|
2023-01-14 19:52:07 +00:00
|
|
|
#include "../../Components/9918/9918.hpp"
|
2018-04-18 02:28:13 +00:00
|
|
|
#include "../../Components/AudioToggle/AudioToggle.hpp"
|
2017-11-25 03:05:50 +00:00
|
|
|
#include "../../Components/AY38910/AY38910.hpp"
|
2023-01-14 19:52:07 +00:00
|
|
|
#include "../../Components/RP5C01/RP5C01.hpp"
|
2018-01-07 01:15:55 +00:00
|
|
|
#include "../../Components/KonamiSCC/KonamiSCC.hpp"
|
2017-11-25 03:05:50 +00:00
|
|
|
|
2017-12-27 03:31:34 +00:00
|
|
|
#include "../../Storage/Tape/Parsers/MSX.hpp"
|
2017-12-20 02:53:04 +00:00
|
|
|
#include "../../Storage/Tape/Tape.hpp"
|
|
|
|
|
2018-05-12 21:32:53 +00:00
|
|
|
#include "../../Activity/Source.hpp"
|
2020-04-02 03:19:34 +00:00
|
|
|
#include "../MachineTypes.hpp"
|
2020-03-16 03:48:53 +00:00
|
|
|
#include "../../Configurable/Configurable.hpp"
|
2017-11-25 18:33:51 +00:00
|
|
|
|
2019-03-02 23:07:05 +00:00
|
|
|
#include "../../Outputs/Log.hpp"
|
2017-12-20 02:08:10 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/CompoundSource.hpp"
|
2017-12-19 02:39:23 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
2017-12-20 02:08:10 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/SampleSource.hpp"
|
2017-12-19 02:39:23 +00:00
|
|
|
|
2017-12-29 23:36:42 +00:00
|
|
|
#include "../../Configurable/StandardOptions.hpp"
|
2018-02-20 00:54:42 +00:00
|
|
|
#include "../../ClockReceiver/ForceInline.hpp"
|
2019-07-30 01:22:31 +00:00
|
|
|
#include "../../ClockReceiver/JustInTime.hpp"
|
2017-12-29 23:36:42 +00:00
|
|
|
|
2018-04-03 02:42:41 +00:00
|
|
|
#include "../../Analyser/Static/MSX/Target.hpp"
|
|
|
|
|
2017-11-25 01:43:26 +00:00
|
|
|
namespace MSX {
|
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
class AYPortHandler: public GI::AY38910::PortHandler {
|
|
|
|
public:
|
2018-07-13 01:42:47 +00:00
|
|
|
AYPortHandler(Storage::Tape::BinaryTapePlayer &tape_player) : tape_player_(tape_player) {
|
|
|
|
joysticks_.emplace_back(new Joystick);
|
|
|
|
joysticks_.emplace_back(new Joystick);
|
|
|
|
}
|
2017-12-20 02:53:04 +00:00
|
|
|
|
|
|
|
void set_port_output(bool port_b, uint8_t value) {
|
|
|
|
if(port_b) {
|
2018-05-13 19:34:31 +00:00
|
|
|
// Bits 0-3: touchpad handshaking (?)
|
|
|
|
// Bit 4-5: monostable timer pulses
|
2018-07-13 01:42:47 +00:00
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
// Bit 6: joystick select
|
2018-07-13 01:42:47 +00:00
|
|
|
selected_joystick_ = (value >> 6) & 1;
|
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
// Bit 7: code LED, if any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t get_port_input(bool port_b) {
|
|
|
|
if(!port_b) {
|
2018-05-13 19:43:03 +00:00
|
|
|
// Bits 0-5: Joystick (up, down, left, right, A, B)
|
2017-12-20 02:53:04 +00:00
|
|
|
// Bit 6: keyboard switch (not universal)
|
|
|
|
// Bit 7: tape input
|
2018-07-13 01:42:47 +00:00
|
|
|
return
|
2018-07-13 02:10:05 +00:00
|
|
|
(static_cast<Joystick *>(joysticks_[selected_joystick_].get())->get_state() & 0x3f) |
|
2018-07-13 01:42:47 +00:00
|
|
|
0x40 |
|
|
|
|
(tape_player_.get_input() ? 0x00 : 0x80);
|
2017-12-20 02:53:04 +00:00
|
|
|
}
|
|
|
|
return 0xff;
|
|
|
|
}
|
2017-11-30 01:31:55 +00:00
|
|
|
|
2019-11-09 23:19:05 +00:00
|
|
|
const std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() {
|
2018-07-13 01:42:47 +00:00
|
|
|
return joysticks_;
|
|
|
|
}
|
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
private:
|
|
|
|
Storage::Tape::BinaryTapePlayer &tape_player_;
|
2018-07-13 01:42:47 +00:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Inputs::Joystick>> joysticks_;
|
|
|
|
size_t selected_joystick_ = 0;
|
|
|
|
class Joystick: public Inputs::ConcreteJoystick {
|
|
|
|
public:
|
|
|
|
Joystick() :
|
|
|
|
ConcreteJoystick({
|
|
|
|
Input(Input::Up),
|
|
|
|
Input(Input::Down),
|
|
|
|
Input(Input::Left),
|
|
|
|
Input(Input::Right),
|
|
|
|
Input(Input::Fire, 0),
|
|
|
|
Input(Input::Fire, 1),
|
|
|
|
}) {}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void did_set_input(const Input &input, bool is_active) final {
|
2018-07-13 01:42:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t get_state() {
|
|
|
|
return state_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t state_ = 0xff;
|
|
|
|
};
|
2017-11-26 21:47:59 +00:00
|
|
|
};
|
|
|
|
|
2023-01-10 19:55:57 +00:00
|
|
|
using Target = Analyser::Static::MSX::Target;
|
|
|
|
|
|
|
|
template <Target::Model model>
|
2017-11-25 01:43:26 +00:00
|
|
|
class ConcreteMachine:
|
2017-11-25 18:33:51 +00:00
|
|
|
public Machine,
|
2017-11-25 02:59:54 +00:00
|
|
|
public CPU::Z80::BusHandler,
|
2020-04-02 03:19:34 +00:00
|
|
|
public MachineTypes::TimedMachine,
|
|
|
|
public MachineTypes::AudioProducer,
|
|
|
|
public MachineTypes::ScanProducer,
|
|
|
|
public MachineTypes::MediaTarget,
|
|
|
|
public MachineTypes::MappedKeyboardMachine,
|
|
|
|
public MachineTypes::JoystickMachine,
|
2018-01-05 03:18:18 +00:00
|
|
|
public Configurable::Device,
|
2018-05-28 03:17:06 +00:00
|
|
|
public ClockingHint::Observer,
|
2023-01-14 02:54:59 +00:00
|
|
|
public Activity::Source,
|
|
|
|
public MSX::MemorySlotChangeHandler {
|
2023-01-13 19:07:54 +00:00
|
|
|
private:
|
2023-01-13 19:16:12 +00:00
|
|
|
// Provide 512kb of memory for an MSX 2; 64kb for an MSX 1. 'Slightly' arbitrary.
|
|
|
|
static constexpr size_t RAMSize = model == Target::Model::MSX2 ? 512 * 1024 : 64 * 1024;
|
2023-01-13 19:07:54 +00:00
|
|
|
|
2023-01-14 19:52:07 +00:00
|
|
|
static constexpr int ClockRate = 3579545;
|
|
|
|
|
2017-11-25 02:59:54 +00:00
|
|
|
public:
|
2019-03-01 01:54:43 +00:00
|
|
|
ConcreteMachine(const Target &target, const ROMMachine::ROMFetcher &rom_fetcher):
|
2017-11-26 21:47:59 +00:00
|
|
|
z80_(*this),
|
2017-11-29 02:56:15 +00:00
|
|
|
i8255_(i8255_port_handler_),
|
2019-12-19 00:28:41 +00:00
|
|
|
ay_(GI::AY38910::Personality::AY38910, audio_queue_),
|
2017-12-20 02:08:10 +00:00
|
|
|
audio_toggle_(audio_queue_),
|
2018-01-07 01:15:55 +00:00
|
|
|
scc_(audio_queue_),
|
|
|
|
mixer_(ay_, audio_toggle_, scc_),
|
2017-12-20 02:08:10 +00:00
|
|
|
speaker_(mixer_),
|
2017-12-20 02:53:04 +00:00
|
|
|
tape_player_(3579545 * 2),
|
|
|
|
i8255_port_handler_(*this, audio_toggle_, tape_player_),
|
2023-01-14 02:54:59 +00:00
|
|
|
ay_port_handler_(tape_player_),
|
2023-01-14 19:52:07 +00:00
|
|
|
memory_slots_{{*this}, {*this}, {*this}, {*this}},
|
|
|
|
clock_(ClockRate) {
|
|
|
|
set_clock_rate(ClockRate);
|
2017-11-30 03:07:30 +00:00
|
|
|
clear_all_keys();
|
2017-12-18 02:26:06 +00:00
|
|
|
|
|
|
|
ay_.set_port_handler(&ay_port_handler_);
|
|
|
|
speaker_.set_input_rate(3579545.0f / 2.0f);
|
2018-05-28 03:17:06 +00:00
|
|
|
tape_player_.set_clocking_hint_observer(this);
|
2018-04-07 18:30:02 +00:00
|
|
|
|
|
|
|
// Set the AY to 50% of available volume, the toggle to 10% and leave 40% for an SCC.
|
|
|
|
mixer_.set_relative_volumes({0.5f, 0.1f, 0.4f});
|
2018-07-11 00:00:46 +00:00
|
|
|
|
2019-03-01 01:54:43 +00:00
|
|
|
// Install the proper TV standard and select an ideal BIOS name.
|
2019-07-23 01:14:21 +00:00
|
|
|
const std::string machine_name = "MSX";
|
2023-01-11 03:15:01 +00:00
|
|
|
constexpr ROM::Name bios_name = model == Target::Model::MSX1 ? ROM::Name::MSXGenericBIOS : ROM::Name::MSX2GenericBIOS;
|
2023-01-11 17:56:09 +00:00
|
|
|
|
2023-01-11 03:15:01 +00:00
|
|
|
ROM::Request bios_request = ROM::Request(bios_name);
|
2023-01-11 17:56:09 +00:00
|
|
|
if constexpr (model == Target::Model::MSX2) {
|
2023-01-13 19:18:39 +00:00
|
|
|
bios_request = bios_request && ROM::Request(ROM::Name::MSX2Extension);
|
2023-01-11 17:56:09 +00:00
|
|
|
}
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
bool is_ntsc = true;
|
|
|
|
uint8_t character_generator = 1; /* 0 = Japan, 1 = USA, etc, 2 = USSR */
|
|
|
|
uint8_t date_format = 1; /* 0 = Y/M/D, 1 = M/D/Y, 2 = D/M/Y */
|
|
|
|
uint8_t keyboard = 1; /* 0 = Japan, 1 = USA, 2 = France, 3 = UK, 4 = Germany, 5 = USSR, 6 = Spain */
|
2021-06-04 01:55:59 +00:00
|
|
|
ROM::Name regional_bios_name;
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
switch(target.region) {
|
2021-06-04 02:46:47 +00:00
|
|
|
default:
|
2019-03-01 01:54:43 +00:00
|
|
|
case Target::Region::Japan:
|
2023-01-11 03:15:01 +00:00
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
regional_bios_name = ROM::Name::MSXJapaneseBIOS;
|
|
|
|
}
|
2019-07-30 01:22:31 +00:00
|
|
|
vdp_->set_tv_standard(TI::TMS::TVStandard::NTSC);
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
is_ntsc = true;
|
|
|
|
character_generator = 0;
|
|
|
|
date_format = 0;
|
|
|
|
break;
|
|
|
|
case Target::Region::USA:
|
2023-01-11 03:15:01 +00:00
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
regional_bios_name = ROM::Name::MSXAmericanBIOS;
|
|
|
|
}
|
2019-07-30 01:22:31 +00:00
|
|
|
vdp_->set_tv_standard(TI::TMS::TVStandard::NTSC);
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
is_ntsc = true;
|
|
|
|
character_generator = 1;
|
|
|
|
date_format = 1;
|
|
|
|
break;
|
|
|
|
case Target::Region::Europe:
|
2023-01-11 03:15:01 +00:00
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
regional_bios_name = ROM::Name::MSXEuropeanBIOS;
|
|
|
|
}
|
2019-07-30 01:22:31 +00:00
|
|
|
vdp_->set_tv_standard(TI::TMS::TVStandard::PAL);
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
is_ntsc = false;
|
|
|
|
character_generator = 1;
|
|
|
|
date_format = 2;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-11 03:15:01 +00:00
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
bios_request = bios_request || ROM::Request(regional_bios_name);
|
|
|
|
}
|
2019-03-01 01:54:43 +00:00
|
|
|
|
|
|
|
// Fetch the necessary ROMs; try the region-specific ROM first,
|
|
|
|
// but failing that fall back on patching the main one.
|
2021-06-04 01:55:59 +00:00
|
|
|
ROM::Request request;
|
2018-07-11 00:00:46 +00:00
|
|
|
if(target.has_disk_drive) {
|
2021-06-04 01:55:59 +00:00
|
|
|
request = ROM::Request(ROM::Name::MSXDOS) && bios_request;
|
|
|
|
} else {
|
|
|
|
request = bios_request;
|
2018-07-11 00:00:46 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
2018-07-11 00:00:46 +00:00
|
|
|
throw ROMMachine::Error::MissingROMs;
|
|
|
|
}
|
|
|
|
|
2019-03-01 01:54:43 +00:00
|
|
|
// Figure out which BIOS to use, either a specific one or the generic
|
|
|
|
// one appropriately patched.
|
2023-01-13 19:07:54 +00:00
|
|
|
bool has_bios = false;
|
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
const auto regional_bios = roms.find(regional_bios_name);
|
|
|
|
if(regional_bios != roms.end()) {
|
|
|
|
regional_bios->second.resize(32768);
|
2023-01-16 03:51:17 +00:00
|
|
|
bios_slot().set_source(regional_bios->second);
|
2023-01-13 19:07:54 +00:00
|
|
|
has_bios = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!has_bios) {
|
2023-01-13 04:01:11 +00:00
|
|
|
std::vector<uint8_t> &bios = roms.find(bios_name)->second;
|
|
|
|
|
|
|
|
bios.resize(32768);
|
2019-03-01 01:54:43 +00:00
|
|
|
|
2023-01-11 03:15:01 +00:00
|
|
|
// Modify the generic ROM to reflect the selected region, date format, etc.
|
2023-01-13 04:01:11 +00:00
|
|
|
bios[0x2b] = uint8_t(
|
2019-03-01 01:54:43 +00:00
|
|
|
(is_ntsc ? 0x00 : 0x80) |
|
|
|
|
(date_format << 4) |
|
|
|
|
character_generator
|
|
|
|
);
|
2023-01-13 04:01:11 +00:00
|
|
|
bios[0x2c] = keyboard;
|
2018-07-11 00:00:46 +00:00
|
|
|
|
2023-01-16 03:51:17 +00:00
|
|
|
bios_slot().set_source(bios);
|
2018-07-11 00:00:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:51:17 +00:00
|
|
|
bios_slot().map(0, 0, 32768);
|
2023-01-13 19:07:54 +00:00
|
|
|
|
2023-01-16 03:51:17 +00:00
|
|
|
ram_slot().resize_source(RAMSize);
|
|
|
|
ram_slot().template map<MemorySlot::AccessType::ReadWrite>(0, 0, 65536);
|
2023-01-13 19:16:12 +00:00
|
|
|
|
2023-01-16 03:51:17 +00:00
|
|
|
if constexpr (model == Target::Model::MSX2) {
|
|
|
|
memory_slots_[3].supports_secondary_paging = true;
|
2023-01-13 19:16:12 +00:00
|
|
|
|
|
|
|
const auto extension = roms.find(ROM::Name::MSX2Extension);
|
2023-01-16 03:51:17 +00:00
|
|
|
extension->second.resize(32768);
|
|
|
|
extension_rom_slot().set_source(extension->second);
|
|
|
|
extension_rom_slot().map(0, 0, 32768);
|
2023-01-13 19:16:12 +00:00
|
|
|
}
|
2018-07-11 00:00:46 +00:00
|
|
|
|
|
|
|
// Add a disk cartridge if any disks were supplied.
|
|
|
|
if(target.has_disk_drive) {
|
2023-01-16 03:51:17 +00:00
|
|
|
disk_primary().handler = std::make_unique<DiskROM>(disk_slot());
|
2023-01-13 04:01:11 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> &dos = roms.find(ROM::Name::MSXDOS)->second;
|
|
|
|
dos.resize(16384);
|
2023-01-16 03:51:17 +00:00
|
|
|
disk_slot().set_source(dos);
|
2018-07-11 00:00:46 +00:00
|
|
|
|
2023-01-16 03:51:17 +00:00
|
|
|
disk_slot().map(0, 0x4000, 0x2000);
|
|
|
|
disk_slot().unmap(0x6000, 0x2000);
|
2018-07-11 00:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the media.
|
|
|
|
insert_media(target.media);
|
|
|
|
|
|
|
|
// Type whatever has been requested.
|
|
|
|
if(!target.loading_command.empty()) {
|
|
|
|
type_string(target.loading_command);
|
|
|
|
}
|
2023-01-13 04:01:11 +00:00
|
|
|
|
|
|
|
// Establish default paging.
|
|
|
|
page_primary(0);
|
2017-11-25 18:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 01:59:19 +00:00
|
|
|
~ConcreteMachine() {
|
|
|
|
audio_queue_.flush();
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target) final {
|
2019-07-30 01:22:31 +00:00
|
|
|
vdp_->set_scan_target(scan_target);
|
2017-11-25 18:33:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 03:28:25 +00:00
|
|
|
Outputs::Display::ScanStatus get_scaled_scan_status() const final {
|
|
|
|
return vdp_->get_scaled_scan_status();
|
2020-01-21 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void set_display_type(Outputs::Display::DisplayType display_type) final {
|
2019-07-30 01:22:31 +00:00
|
|
|
vdp_->set_display_type(display_type);
|
2018-11-30 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
Outputs::Display::DisplayType get_display_type() const final {
|
2020-03-18 22:26:22 +00:00
|
|
|
return vdp_->get_display_type();
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
Outputs::Speaker::Speaker *get_speaker() final {
|
2017-12-18 02:26:06 +00:00
|
|
|
return &speaker_;
|
2017-11-25 18:33:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void run_for(const Cycles cycles) final {
|
2017-11-25 18:33:51 +00:00
|
|
|
z80_.run_for(cycles);
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
float get_confidence() final {
|
2018-02-09 14:10:56 +00:00
|
|
|
if(performed_unmapped_access_ || pc_zero_accesses_ > 1) return 0.0f;
|
2023-01-16 03:51:17 +00:00
|
|
|
if(cartridge_primary().handler) {
|
|
|
|
return cartridge_primary().handler->get_confidence();
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
|
|
|
return 0.5f;
|
|
|
|
}
|
2018-01-23 02:50:56 +00:00
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
std::string debug_type() final {
|
2023-01-16 03:51:17 +00:00
|
|
|
if(cartridge_primary().handler) {
|
|
|
|
return "MSX:" + cartridge_primary().handler->debug_type();
|
2018-01-23 02:50:56 +00:00
|
|
|
}
|
2019-03-02 23:07:05 +00:00
|
|
|
return "MSX";
|
2017-11-25 18:33:51 +00:00
|
|
|
}
|
2017-11-27 01:07:30 +00:00
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
bool insert_media(const Analyser::Static::Media &media) final {
|
2017-12-02 21:06:04 +00:00
|
|
|
if(!media.cartridges.empty()) {
|
|
|
|
const auto &segment = media.cartridges.front()->get_segments().front();
|
2023-01-16 03:51:17 +00:00
|
|
|
auto &slot = cartridge_slot();
|
2023-01-13 04:01:11 +00:00
|
|
|
|
|
|
|
slot.set_source(segment.data);
|
2023-01-16 03:51:17 +00:00
|
|
|
slot.map(0, uint16_t(segment.start_address), std::min(segment.data.size(), 65536 - segment.start_address));
|
2018-01-26 03:16:46 +00:00
|
|
|
|
|
|
|
auto msx_cartridge = dynamic_cast<Analyser::Static::MSX::Cartridge *>(media.cartridges.front().get());
|
|
|
|
if(msx_cartridge) {
|
|
|
|
switch(msx_cartridge->type) {
|
|
|
|
default: break;
|
|
|
|
case Analyser::Static::MSX::Cartridge::Konami:
|
2023-01-16 03:51:17 +00:00
|
|
|
cartridge_primary().handler = std::make_unique<Cartridge::KonamiROMSlotHandler>(static_cast<MSX::MemorySlot &>(slot));
|
2018-01-26 03:16:46 +00:00
|
|
|
break;
|
|
|
|
case Analyser::Static::MSX::Cartridge::KonamiWithSCC:
|
2023-01-16 03:51:17 +00:00
|
|
|
cartridge_primary().handler = std::make_unique<Cartridge::KonamiWithSCCROMSlotHandler>(static_cast<MSX::MemorySlot &>(slot), scc_);
|
2018-01-26 03:16:46 +00:00
|
|
|
break;
|
|
|
|
case Analyser::Static::MSX::Cartridge::ASCII8kb:
|
2023-01-16 03:51:17 +00:00
|
|
|
cartridge_primary().handler = std::make_unique<Cartridge::ASCII8kbROMSlotHandler>(static_cast<MSX::MemorySlot &>(slot));
|
2018-01-26 03:16:46 +00:00
|
|
|
break;
|
|
|
|
case Analyser::Static::MSX::Cartridge::ASCII16kb:
|
2023-01-16 03:51:17 +00:00
|
|
|
cartridge_primary().handler = std::make_unique<Cartridge::ASCII16kbROMSlotHandler>(static_cast<MSX::MemorySlot &>(slot));
|
2018-01-26 03:16:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 21:06:04 +00:00
|
|
|
}
|
2017-12-20 02:53:04 +00:00
|
|
|
|
|
|
|
if(!media.tapes.empty()) {
|
|
|
|
tape_player_.set_tape(media.tapes.front());
|
|
|
|
}
|
|
|
|
|
2018-01-08 00:12:52 +00:00
|
|
|
if(!media.disks.empty()) {
|
2023-01-16 03:51:17 +00:00
|
|
|
DiskROM *const handler = disk_handler();
|
|
|
|
if(handler) {
|
2018-05-12 21:32:53 +00:00
|
|
|
size_t drive = 0;
|
|
|
|
for(auto &disk : media.disks) {
|
2023-01-16 03:51:17 +00:00
|
|
|
handler->set_disk(disk, drive);
|
2018-05-12 21:32:53 +00:00
|
|
|
drive++;
|
|
|
|
if(drive == 2) break;
|
|
|
|
}
|
2018-01-08 00:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 21:24:47 +00:00
|
|
|
set_use_fast_tape();
|
|
|
|
|
2017-11-26 18:28:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-25 02:59:54 +00:00
|
|
|
|
2020-01-24 03:35:39 +00:00
|
|
|
void type_string(const std::string &string) final {
|
2018-09-12 01:46:28 +00:00
|
|
|
std::transform(
|
|
|
|
string.begin(),
|
|
|
|
string.end(),
|
|
|
|
std::back_inserter(input_text_),
|
|
|
|
[](unsigned char c) -> unsigned char { return (c == '\n') ? '\r' : c; }
|
|
|
|
);
|
2017-12-29 23:30:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
bool can_type(char c) const final {
|
2020-03-02 01:25:12 +00:00
|
|
|
// Make an effort to type the entire printable ASCII range.
|
|
|
|
return c >= 32 && c < 127;
|
|
|
|
}
|
|
|
|
|
2023-01-10 23:07:31 +00:00
|
|
|
// MARK: Memory paging.
|
|
|
|
void page_primary(uint8_t value) {
|
2023-01-11 18:15:00 +00:00
|
|
|
primary_slots_ = value;
|
2023-01-10 23:07:31 +00:00
|
|
|
update_paging();
|
|
|
|
}
|
|
|
|
|
2023-01-14 02:54:59 +00:00
|
|
|
void did_page() final {
|
|
|
|
update_paging();
|
|
|
|
}
|
|
|
|
|
2023-01-10 23:07:31 +00:00
|
|
|
void update_paging() {
|
2023-01-11 18:15:00 +00:00
|
|
|
uint8_t primary = primary_slots_;
|
|
|
|
|
2023-01-13 04:01:11 +00:00
|
|
|
// Update final slot; this direct pointer will be used for
|
|
|
|
// secondary slot communication.
|
2023-01-11 18:15:00 +00:00
|
|
|
final_slot_ = &memory_slots_[primary >> 6];
|
2023-01-10 23:07:31 +00:00
|
|
|
|
2023-01-13 04:08:01 +00:00
|
|
|
for(int c = 0; c < 8; c += 2) {
|
2023-01-16 03:51:17 +00:00
|
|
|
const PrimarySlot &slot = memory_slots_[primary & 3];
|
2023-01-10 23:07:31 +00:00
|
|
|
primary >>= 2;
|
|
|
|
|
2023-01-13 04:01:11 +00:00
|
|
|
read_pointers_[c] = slot.read_pointer(c);
|
|
|
|
write_pointers_[c] = slot.write_pointer(c);
|
|
|
|
read_pointers_[c+1] = slot.read_pointer(c+1);
|
|
|
|
write_pointers_[c+1] = slot.write_pointer(c+1);
|
2017-11-29 02:56:15 +00:00
|
|
|
}
|
2019-03-02 19:54:26 +00:00
|
|
|
set_use_fast_tape();
|
2017-11-29 02:56:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 20:51:29 +00:00
|
|
|
// MARK: Z80::BusHandler
|
2018-02-20 00:54:42 +00:00
|
|
|
forceinline HalfCycles perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
2018-03-07 22:00:18 +00:00
|
|
|
// Per the best information I currently have, the MSX inserts an extra cycle into each opcode read,
|
|
|
|
// but otherwise runs without pause.
|
|
|
|
const HalfCycles addition((cycle.operation == CPU::Z80::PartialMachineCycle::ReadOpcode) ? 2 : 0);
|
|
|
|
const HalfCycles total_length = addition + cycle.length;
|
2021-04-06 01:02:37 +00:00
|
|
|
if(vdp_ += total_length) {
|
|
|
|
z80_.set_interrupt_line(vdp_->get_interrupt_line(), vdp_.last_sequence_point_overrun());
|
|
|
|
}
|
2018-03-07 22:00:18 +00:00
|
|
|
time_since_ay_update_ += total_length;
|
|
|
|
memory_slots_[0].cycles_since_update += total_length;
|
|
|
|
memory_slots_[1].cycles_since_update += total_length;
|
|
|
|
memory_slots_[2].cycles_since_update += total_length;
|
|
|
|
memory_slots_[3].cycles_since_update += total_length;
|
|
|
|
|
2023-01-14 19:52:07 +00:00
|
|
|
if constexpr (model >= Target::Model::MSX2) {
|
|
|
|
clock_.run_for(total_length);
|
|
|
|
}
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
if(cycle.is_terminal()) {
|
|
|
|
uint16_t address = cycle.address ? *cycle.address : 0x0000;
|
|
|
|
switch(cycle.operation) {
|
|
|
|
case CPU::Z80::PartialMachineCycle::ReadOpcode:
|
|
|
|
if(use_fast_tape_) {
|
2019-03-02 19:54:26 +00:00
|
|
|
if(address == 0x1a63) {
|
2018-09-22 02:53:35 +00:00
|
|
|
// TAPION
|
|
|
|
|
|
|
|
// Enable the tape motor.
|
2020-01-05 18:40:02 +00:00
|
|
|
i8255_.write(0xab, 0x8);
|
2018-09-22 02:53:35 +00:00
|
|
|
|
|
|
|
// Disable interrupts.
|
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::IFF1, 0);
|
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::IFF2, 0);
|
|
|
|
|
|
|
|
// Use the parser to find a header, and if one is found then populate
|
|
|
|
// LOWLIM and WINWID, and reset carry. Otherwise set carry.
|
|
|
|
using Parser = Storage::Tape::MSX::Parser;
|
|
|
|
std::unique_ptr<Parser::FileSpeed> new_speed = Parser::find_header(tape_player_);
|
|
|
|
if(new_speed) {
|
2023-01-13 19:07:54 +00:00
|
|
|
ram()[0xfca4] = new_speed->minimum_start_bit_duration;
|
|
|
|
ram()[0xfca5] = new_speed->low_high_disrimination_duration;
|
2018-09-22 02:53:35 +00:00
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::Flags, 0);
|
|
|
|
} else {
|
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::Flags, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// RET.
|
|
|
|
*cycle.value = 0xc9;
|
|
|
|
break;
|
2017-12-29 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:54:26 +00:00
|
|
|
if(address == 0x1abc) {
|
2018-09-22 02:53:35 +00:00
|
|
|
// TAPIN
|
|
|
|
|
|
|
|
// Grab the current values of LOWLIM and WINWID.
|
|
|
|
using Parser = Storage::Tape::MSX::Parser;
|
|
|
|
Parser::FileSpeed tape_speed;
|
2023-01-13 19:07:54 +00:00
|
|
|
tape_speed.minimum_start_bit_duration = ram()[0xfca4];
|
|
|
|
tape_speed.low_high_disrimination_duration = ram()[0xfca5];
|
2018-09-22 02:53:35 +00:00
|
|
|
|
|
|
|
// Ask the tape parser to grab a byte.
|
|
|
|
int next_byte = Parser::get_byte(tape_speed, tape_player_);
|
|
|
|
|
|
|
|
// If a byte was found, return it with carry unset. Otherwise set carry to
|
|
|
|
// indicate error.
|
|
|
|
if(next_byte >= 0) {
|
2020-05-10 03:00:39 +00:00
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::A, uint16_t(next_byte));
|
2018-09-22 02:53:35 +00:00
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::Flags, 0);
|
|
|
|
} else {
|
|
|
|
z80_.set_value_of_register(CPU::Z80::Register::Flags, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// RET.
|
|
|
|
*cycle.value = 0xc9;
|
|
|
|
break;
|
2017-12-29 23:36:42 +00:00
|
|
|
}
|
2017-12-29 03:48:04 +00:00
|
|
|
}
|
2017-11-26 21:47:59 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
if(!address) {
|
|
|
|
pc_zero_accesses_++;
|
|
|
|
}
|
2023-01-13 19:07:54 +00:00
|
|
|
|
|
|
|
// TODO: below relates to confidence measurements. Reinstate, somehow.
|
|
|
|
// if(is_unpopulated_[address >> 13] == unpopulated_) {
|
|
|
|
// performed_unmapped_access_ = true;
|
|
|
|
// }
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
pc_address_ = address; // This is retained so as to be able to name the source of an access to cartridge handlers.
|
2020-06-20 03:36:51 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Read:
|
2023-01-11 18:15:00 +00:00
|
|
|
if(address == 0xffff && final_slot_->supports_secondary_paging) {
|
2023-01-13 04:01:11 +00:00
|
|
|
*cycle.value = final_slot_->secondary_paging() ^ 0xff;
|
2023-01-11 18:15:00 +00:00
|
|
|
break;
|
2023-01-11 03:24:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
if(read_pointers_[address >> 13]) {
|
|
|
|
*cycle.value = read_pointers_[address >> 13][address & 8191];
|
|
|
|
} else {
|
2023-01-11 18:15:00 +00:00
|
|
|
const int slot_hit = (primary_slots_ >> ((address >> 14) * 2)) & 3;
|
2023-01-10 19:55:57 +00:00
|
|
|
memory_slots_[slot_hit].handler->run_for(memory_slots_[slot_hit].cycles_since_update.template flush<HalfCycles>());
|
2018-09-22 02:53:35 +00:00
|
|
|
*cycle.value = memory_slots_[slot_hit].handler->read(address);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-05 03:18:18 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Write: {
|
2023-01-11 18:15:00 +00:00
|
|
|
if(address == 0xffff && final_slot_->supports_secondary_paging) {
|
2023-01-13 04:01:11 +00:00
|
|
|
final_slot_->set_secondary_paging(*cycle.value);
|
2023-01-11 18:15:00 +00:00
|
|
|
update_paging();
|
|
|
|
break;
|
2023-01-11 03:24:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 18:15:00 +00:00
|
|
|
const int slot_hit = (primary_slots_ >> ((address >> 14) * 2)) & 3;
|
2018-09-22 02:53:35 +00:00
|
|
|
if(memory_slots_[slot_hit].handler) {
|
2017-12-20 02:08:10 +00:00
|
|
|
update_audio();
|
2023-01-10 19:55:57 +00:00
|
|
|
memory_slots_[slot_hit].handler->run_for(memory_slots_[slot_hit].cycles_since_update.template flush<HalfCycles>());
|
2023-01-13 04:01:11 +00:00
|
|
|
memory_slots_[slot_hit].handler->write(
|
|
|
|
address,
|
|
|
|
*cycle.value,
|
|
|
|
read_pointers_[pc_address_ >> 13] != memory_slots_[0].read_pointer(pc_address_ >> 13));
|
2023-01-11 18:15:00 +00:00
|
|
|
} else {
|
|
|
|
write_pointers_[address >> 13][address & 8191] = *cycle.value;
|
2018-09-22 02:53:35 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Input:
|
|
|
|
switch(address & 0xff) {
|
|
|
|
case 0x98: case 0x99:
|
2020-01-05 18:40:02 +00:00
|
|
|
*cycle.value = vdp_->read(address);
|
2019-07-30 01:22:31 +00:00
|
|
|
z80_.set_interrupt_line(vdp_->get_interrupt_line());
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-11-26 21:47:59 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case 0xa2:
|
|
|
|
update_audio();
|
2021-03-27 03:19:47 +00:00
|
|
|
*cycle.value = GI::AY38910::Utility::read(ay_);
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-12-04 03:25:18 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case 0xa8: case 0xa9:
|
|
|
|
case 0xaa: case 0xab:
|
2020-01-05 18:40:02 +00:00
|
|
|
*cycle.value = i8255_.read(address);
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-11-26 21:47:59 +00:00
|
|
|
|
2023-01-14 19:52:07 +00:00
|
|
|
case 0xb5:
|
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*cycle.value = clock_.read(next_clock_register_);
|
|
|
|
break;
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
default:
|
2023-01-14 19:52:07 +00:00
|
|
|
printf("Unhandled read %02x\n", address & 0xff);
|
2018-09-22 02:53:35 +00:00
|
|
|
*cycle.value = 0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Output: {
|
|
|
|
const int port = address & 0xff;
|
|
|
|
switch(port) {
|
|
|
|
case 0x98: case 0x99:
|
2020-01-05 18:40:02 +00:00
|
|
|
vdp_->write(address, *cycle.value);
|
2019-07-30 01:22:31 +00:00
|
|
|
z80_.set_interrupt_line(vdp_->get_interrupt_line());
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-11-26 21:47:59 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case 0xa0: case 0xa1:
|
|
|
|
update_audio();
|
2021-03-27 03:19:47 +00:00
|
|
|
GI::AY38910::Utility::write(ay_, port == 0xa1, *cycle.value);
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-12-12 02:09:53 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case 0xa8: case 0xa9:
|
|
|
|
case 0xaa: case 0xab:
|
2020-01-05 18:40:02 +00:00
|
|
|
i8255_.write(address, *cycle.value);
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
|
|
|
|
2023-01-14 19:52:07 +00:00
|
|
|
case 0xb4:
|
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_clock_register_ = *cycle.value;
|
|
|
|
break;
|
|
|
|
case 0xb5:
|
|
|
|
if constexpr (model == Target::Model::MSX1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clock_.write(next_clock_register_, *cycle.value);
|
|
|
|
break;
|
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
case 0xfc: case 0xfd: case 0xfe: case 0xff:
|
2023-01-14 03:22:58 +00:00
|
|
|
// 1. Propagate to all handlers.
|
|
|
|
// 2. Apply to RAM.
|
2023-01-14 19:52:07 +00:00
|
|
|
printf("RAM banking %02x: %02x\n", port, *cycle.value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Unhandled write %02x of %02x\n", address & 0xff, *cycle.value);
|
2018-09-22 02:53:35 +00:00
|
|
|
break;
|
2017-12-29 23:30:46 +00:00
|
|
|
}
|
2018-09-22 02:53:35 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case CPU::Z80::PartialMachineCycle::Interrupt:
|
|
|
|
*cycle.value = 0xff;
|
|
|
|
|
|
|
|
// Take this as a convenient moment to jump into the keyboard buffer, if desired.
|
|
|
|
if(!input_text_.empty()) {
|
|
|
|
// The following are KEYBUF per the Red Book; its address and its definition as DEFS 40.
|
|
|
|
const int buffer_start = 0xfbf0;
|
|
|
|
const int buffer_size = 40;
|
|
|
|
|
|
|
|
// Also from the Red Book: GETPNT is at F3FAH and PUTPNT is at F3F8H.
|
2023-01-13 19:07:54 +00:00
|
|
|
int read_address = ram()[0xf3fa] | (ram()[0xf3fb] << 8);
|
|
|
|
int write_address = ram()[0xf3f8] | (ram()[0xf3f9] << 8);
|
2018-09-22 02:53:35 +00:00
|
|
|
|
|
|
|
// Write until either the string is exhausted or the write_pointer is immediately
|
|
|
|
// behind the read pointer; temporarily map write_address and read_address into
|
|
|
|
// buffer-relative values.
|
|
|
|
std::size_t characters_written = 0;
|
|
|
|
write_address -= buffer_start;
|
|
|
|
read_address -= buffer_start;
|
|
|
|
while(characters_written < input_text_.size()) {
|
|
|
|
const int next_write_address = (write_address + 1) % buffer_size;
|
|
|
|
if(next_write_address == read_address) break;
|
2023-01-13 19:07:54 +00:00
|
|
|
ram()[write_address + buffer_start] = uint8_t(input_text_[characters_written]);
|
2018-09-22 02:53:35 +00:00
|
|
|
++characters_written;
|
|
|
|
write_address = next_write_address;
|
|
|
|
}
|
2020-05-10 03:00:39 +00:00
|
|
|
input_text_.erase(input_text_.begin(), input_text_.begin() + std::string::difference_type(characters_written));
|
2017-12-29 23:30:46 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
// Map the write address back into absolute terms and write it out again as PUTPNT.
|
|
|
|
write_address += buffer_start;
|
2023-01-13 19:07:54 +00:00
|
|
|
ram()[0xf3f8] = uint8_t(write_address);
|
|
|
|
ram()[0xf3f9] = uint8_t(write_address >> 8);
|
2018-09-22 02:53:35 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-12-14 03:44:03 +00:00
|
|
|
|
2018-09-22 02:53:35 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2017-11-26 21:47:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 23:36:31 +00:00
|
|
|
if(!tape_player_is_sleeping_)
|
2019-10-30 02:36:29 +00:00
|
|
|
tape_player_.run_for(int(cycle.length.as_integral()));
|
2017-12-20 02:53:04 +00:00
|
|
|
|
2017-11-27 01:07:30 +00:00
|
|
|
return addition;
|
|
|
|
}
|
|
|
|
|
2022-07-09 17:33:46 +00:00
|
|
|
void flush_output(int outputs) final {
|
|
|
|
if(outputs & Output::Video) {
|
2022-07-08 20:04:32 +00:00
|
|
|
vdp_.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();
|
|
|
|
}
|
2017-11-26 21:47:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 03:07:30 +00:00
|
|
|
void set_keyboard_line(int line) {
|
|
|
|
selected_key_line_ = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t read_keyboard() {
|
|
|
|
return key_states_[selected_key_line_];
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void clear_all_keys() final {
|
2017-11-30 03:07:30 +00:00
|
|
|
std::memset(key_states_, 0xff, sizeof(key_states_));
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
void set_key_state(uint16_t key, bool is_pressed) final {
|
2017-11-30 03:07:30 +00:00
|
|
|
int mask = 1 << (key & 7);
|
|
|
|
int line = key >> 4;
|
|
|
|
if(is_pressed) key_states_[line] &= ~mask; else key_states_[line] |= mask;
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:57:51 +00:00
|
|
|
KeyboardMapper *get_keyboard_mapper() final {
|
2018-02-09 21:31:05 +00:00
|
|
|
return &keyboard_mapper_;
|
2017-11-30 03:07:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 23:36:42 +00:00
|
|
|
// MARK: - Configuration options.
|
2020-03-17 03:25:05 +00:00
|
|
|
std::unique_ptr<Reflection::Struct> get_options() final {
|
2020-03-18 22:26:22 +00:00
|
|
|
auto options = std::make_unique<Options>(Configurable::OptionsType::UserFriendly);
|
|
|
|
options->output = get_video_signal_configurable();
|
|
|
|
options->quickload = allow_fast_tape_;
|
|
|
|
return options;
|
2017-12-29 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:26:22 +00:00
|
|
|
void set_options(const std::unique_ptr<Reflection::Struct> &str) final {
|
|
|
|
const auto options = dynamic_cast<Options *>(str.get());
|
|
|
|
set_video_signal_configurable(options->output);
|
|
|
|
allow_fast_tape_ = options->quickload;
|
|
|
|
set_use_fast_tape();
|
2017-12-29 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 23:36:31 +00:00
|
|
|
// MARK: - Sleeper
|
2020-05-30 04:37:06 +00:00
|
|
|
void set_component_prefers_clocking(ClockingHint::Source *, ClockingHint::Preference) final {
|
2018-05-28 03:17:06 +00:00
|
|
|
tape_player_is_sleeping_ = tape_player_.preferred_clocking() == ClockingHint::Preference::None;
|
2018-02-20 00:54:42 +00:00
|
|
|
set_use_fast_tape();
|
2018-02-19 23:36:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 21:32:53 +00:00
|
|
|
// MARK: - Activity::Source
|
2020-01-24 03:57:51 +00:00
|
|
|
void set_activity_observer(Activity::Observer *observer) final {
|
2023-01-16 03:51:17 +00:00
|
|
|
DiskROM *handler = disk_handler();
|
|
|
|
if(handler) {
|
|
|
|
handler->set_activity_observer(observer);
|
2018-05-12 21:32:53 +00:00
|
|
|
}
|
2019-03-01 23:49:21 +00:00
|
|
|
i8255_port_handler_.set_activity_observer(observer);
|
2018-05-12 21:32:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 01:42:47 +00:00
|
|
|
// MARK: - Joysticks
|
2020-01-24 03:57:51 +00:00
|
|
|
const std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() final {
|
2018-07-13 01:42:47 +00:00
|
|
|
return ay_port_handler_.get_joysticks();
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:59:54 +00:00
|
|
|
private:
|
2017-12-20 02:08:10 +00:00
|
|
|
void update_audio() {
|
|
|
|
speaker_.run_for(audio_queue_, time_since_ay_update_.divide_cycles(Cycles(2)));
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:56:15 +00:00
|
|
|
class i8255PortHandler: public Intel::i8255::PortHandler {
|
|
|
|
public:
|
2018-04-18 02:28:13 +00:00
|
|
|
i8255PortHandler(ConcreteMachine &machine, Audio::Toggle &audio_toggle, Storage::Tape::BinaryTapePlayer &tape_player) :
|
2017-12-20 02:53:04 +00:00
|
|
|
machine_(machine), audio_toggle_(audio_toggle), tape_player_(tape_player) {}
|
2017-11-29 02:56:15 +00:00
|
|
|
|
|
|
|
void set_value(int port, uint8_t value) {
|
|
|
|
switch(port) {
|
2023-01-10 23:07:31 +00:00
|
|
|
case 0: machine_.page_primary(value); break;
|
2017-12-20 02:08:10 +00:00
|
|
|
case 2: {
|
2017-11-30 03:07:30 +00:00
|
|
|
// TODO:
|
|
|
|
// b6 caps lock LED
|
2018-11-24 03:32:32 +00:00
|
|
|
// b5 audio output
|
2017-12-20 02:53:04 +00:00
|
|
|
|
|
|
|
// b4: cassette motor relay
|
2017-12-20 03:17:42 +00:00
|
|
|
tape_player_.set_motor_control(!(value & 0x10));
|
2019-03-02 22:37:56 +00:00
|
|
|
if(activity_observer_) activity_observer_->set_led_status("Tape motor", !(value & 0x10));
|
2017-12-20 02:08:10 +00:00
|
|
|
|
|
|
|
// b7: keyboard click
|
|
|
|
bool new_audio_level = !!(value & 0x80);
|
|
|
|
if(audio_toggle_.get_output() != new_audio_level) {
|
|
|
|
machine_.update_audio();
|
|
|
|
audio_toggle_.set_output(new_audio_level);
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
// b0-b3: keyboard line
|
2017-11-30 03:07:30 +00:00
|
|
|
machine_.set_keyboard_line(value & 0xf);
|
2017-12-20 02:08:10 +00:00
|
|
|
} break;
|
2019-03-02 23:07:05 +00:00
|
|
|
default: LOG("Unrecognised: MSX set 8255 output port " << port << " to value " << PADHEX(2) << value); break;
|
2017-11-29 02:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t get_value(int port) {
|
|
|
|
if(port == 1) {
|
2017-11-30 03:07:30 +00:00
|
|
|
return machine_.read_keyboard();
|
2019-03-02 23:07:05 +00:00
|
|
|
} else LOG("MSX attempted to read from 8255 port " << port);
|
2017-11-29 02:56:15 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2019-03-01 23:49:21 +00:00
|
|
|
void set_activity_observer(Activity::Observer *observer) {
|
|
|
|
activity_observer_ = observer;
|
|
|
|
if(activity_observer_) {
|
|
|
|
activity_observer_->register_led("Tape motor");
|
|
|
|
activity_observer_->set_led_status("Tape motor", tape_player_.get_motor_control());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:56:15 +00:00
|
|
|
private:
|
|
|
|
ConcreteMachine &machine_;
|
2018-04-18 02:28:13 +00:00
|
|
|
Audio::Toggle &audio_toggle_;
|
2017-12-20 02:53:04 +00:00
|
|
|
Storage::Tape::BinaryTapePlayer &tape_player_;
|
2019-03-01 23:49:21 +00:00
|
|
|
Activity::Observer *activity_observer_ = nullptr;
|
2017-11-29 02:56:15 +00:00
|
|
|
};
|
|
|
|
|
2023-01-15 03:05:59 +00:00
|
|
|
static constexpr TI::TMS::Personality vdp_model() {
|
|
|
|
switch(model) {
|
|
|
|
case Target::Model::MSX1: return TI::TMS::Personality::TMS9918A;
|
|
|
|
case Target::Model::MSX2: return TI::TMS::Personality::V9938;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:59:54 +00:00
|
|
|
CPU::Z80::Processor<ConcreteMachine, false, false> z80_;
|
2023-01-15 03:05:59 +00:00
|
|
|
JustInTimeActor<TI::TMS::TMS9918<vdp_model()>> vdp_;
|
2017-11-26 21:47:59 +00:00
|
|
|
Intel::i8255::i8255<i8255PortHandler> i8255_;
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2022-07-16 18:41:04 +00:00
|
|
|
Concurrency::AsyncTaskQueue<false> audio_queue_;
|
2020-02-16 23:31:45 +00:00
|
|
|
GI::AY38910::AY38910<false> ay_;
|
2018-04-18 02:28:13 +00:00
|
|
|
Audio::Toggle audio_toggle_;
|
2018-01-07 01:15:55 +00:00
|
|
|
Konami::SCC scc_;
|
2020-02-16 23:31:45 +00:00
|
|
|
Outputs::Speaker::CompoundSource<GI::AY38910::AY38910<false>, Audio::Toggle, Konami::SCC> mixer_;
|
2021-11-21 20:37:29 +00:00
|
|
|
Outputs::Speaker::PullLowpass<Outputs::Speaker::CompoundSource<GI::AY38910::AY38910<false>, Audio::Toggle, Konami::SCC>> speaker_;
|
2017-11-26 21:47:59 +00:00
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
Storage::Tape::BinaryTapePlayer tape_player_;
|
2018-02-19 23:36:31 +00:00
|
|
|
bool tape_player_is_sleeping_ = false;
|
2018-02-19 21:24:47 +00:00
|
|
|
bool allow_fast_tape_ = false;
|
2017-12-29 23:36:42 +00:00
|
|
|
bool use_fast_tape_ = false;
|
2018-02-19 21:24:47 +00:00
|
|
|
void set_use_fast_tape() {
|
2023-01-11 18:15:00 +00:00
|
|
|
use_fast_tape_ =
|
|
|
|
!tape_player_is_sleeping_ &&
|
|
|
|
allow_fast_tape_ &&
|
|
|
|
tape_player_.has_tape() &&
|
|
|
|
!(primary_slots_ & 3) &&
|
2023-01-13 04:01:11 +00:00
|
|
|
!(memory_slots_[0].secondary_paging() & 3);
|
2018-02-19 21:24:47 +00:00
|
|
|
}
|
2017-12-20 02:53:04 +00:00
|
|
|
|
2017-11-26 21:47:59 +00:00
|
|
|
i8255PortHandler i8255_port_handler_;
|
|
|
|
AYPortHandler ay_port_handler_;
|
|
|
|
|
2023-01-10 23:07:31 +00:00
|
|
|
/// The current primary and secondary slot selections; the former retains whatever was written
|
2023-01-11 18:15:00 +00:00
|
|
|
/// last to the 8255 PPI via port A8 and the latter — if enabled — captures 0xffff on a per-slot basis.
|
|
|
|
uint8_t primary_slots_ = 0;
|
2023-01-10 19:40:03 +00:00
|
|
|
|
|
|
|
// Divides the current 64kb address space into 8kb chunks.
|
|
|
|
// 8kb resolution is used by some cartride titles.
|
2023-01-13 04:01:11 +00:00
|
|
|
const uint8_t *read_pointers_[8];
|
2018-01-05 03:18:18 +00:00
|
|
|
uint8_t *write_pointers_[8];
|
2017-11-30 01:49:02 +00:00
|
|
|
|
2023-01-10 19:40:03 +00:00
|
|
|
/// Optionally attaches non-default logic to any of the four things selectable
|
|
|
|
/// via the primary slot register.
|
2023-01-16 04:12:36 +00:00
|
|
|
///
|
|
|
|
/// In principle one might want to attach a handler to a secondary slot rather
|
|
|
|
/// than a primary, but in practice that isn't required in the slot allocation used
|
|
|
|
/// by this emulator.
|
2023-01-16 03:51:17 +00:00
|
|
|
struct PrimarySlot: public MSX::PrimarySlot {
|
|
|
|
using MSX::PrimarySlot::PrimarySlot;
|
|
|
|
|
|
|
|
/// Storage for a slot-specialised handler.
|
2023-01-13 04:02:24 +00:00
|
|
|
std::unique_ptr<MemorySlotHandler> handler;
|
2023-01-16 04:12:36 +00:00
|
|
|
|
|
|
|
/// The handler is updated just-in-time.
|
|
|
|
HalfCycles cycles_since_update;
|
2023-01-10 19:40:03 +00:00
|
|
|
};
|
2023-01-16 03:51:17 +00:00
|
|
|
PrimarySlot memory_slots_[4];
|
|
|
|
PrimarySlot *final_slot_ = nullptr;
|
2023-01-10 19:40:03 +00:00
|
|
|
|
2017-12-02 21:30:43 +00:00
|
|
|
HalfCycles time_since_ay_update_;
|
2017-11-30 03:07:30 +00:00
|
|
|
|
|
|
|
uint8_t key_states_[16];
|
|
|
|
int selected_key_line_ = 0;
|
2017-12-29 23:30:46 +00:00
|
|
|
std::string input_text_;
|
2017-11-30 03:07:30 +00:00
|
|
|
|
|
|
|
MSX::KeyboardMapper keyboard_mapper_;
|
2018-02-09 14:10:56 +00:00
|
|
|
|
|
|
|
int pc_zero_accesses_ = 0;
|
|
|
|
bool performed_unmapped_access_ = false;
|
2018-02-10 22:11:16 +00:00
|
|
|
uint16_t pc_address_;
|
2023-01-14 19:52:07 +00:00
|
|
|
|
|
|
|
Ricoh::RP5C01::RP5C01 clock_;
|
|
|
|
int next_clock_register_ = 0;
|
2023-01-16 03:51:17 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Various helpers that dictate the slot arrangement used by this emulator.
|
|
|
|
//
|
2023-01-16 04:12:36 +00:00
|
|
|
// That arrangement is:
|
|
|
|
//
|
|
|
|
// Slot 0 is the BIOS, and does not support secondary paging.
|
|
|
|
// Slot 1 holds a [game, probably] cartridge, if inserted. No secondary paging.
|
|
|
|
// Slot 2 holds the disk cartridge, if inserted.
|
|
|
|
//
|
|
|
|
// On an MSX 1, Slot 3 holds 64kb of RAM.
|
|
|
|
//
|
|
|
|
// On an MSX 2:
|
|
|
|
//
|
|
|
|
// Slot 3-0 holds a larger amount of RAM (cf. RAMSize) that is subject to the
|
|
|
|
// FC-FF paging selections.
|
|
|
|
//
|
|
|
|
// Slot 3-1 holds the BIOS extension ROM.
|
|
|
|
//
|
|
|
|
// [Slot 3-2 will likely hold MSX-MUSIC, but that's TODO]
|
|
|
|
//
|
2023-01-16 03:51:17 +00:00
|
|
|
MemorySlot &bios_slot() {
|
|
|
|
return memory_slots_[0].subslot(0);
|
|
|
|
}
|
|
|
|
MemorySlot &ram_slot() {
|
|
|
|
return memory_slots_[3].subslot(0);
|
|
|
|
}
|
|
|
|
MemorySlot &extension_rom_slot() {
|
|
|
|
return memory_slots_[3].subslot(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemorySlot &cartridge_slot() {
|
|
|
|
return cartridge_primary().subslot(0);
|
|
|
|
}
|
|
|
|
MemorySlot &disk_slot() {
|
|
|
|
return disk_primary().subslot(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimarySlot &cartridge_primary() {
|
|
|
|
return memory_slots_[1];
|
|
|
|
}
|
|
|
|
PrimarySlot &disk_primary() {
|
|
|
|
return memory_slots_[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *ram() {
|
|
|
|
return ram_slot().source().data();
|
|
|
|
}
|
|
|
|
DiskROM *disk_handler() {
|
|
|
|
return dynamic_cast<DiskROM *>(disk_primary().handler.get());
|
|
|
|
}};
|
2017-11-25 01:43:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace MSX;
|
|
|
|
|
2018-07-11 00:00:46 +00:00
|
|
|
Machine *Machine::MSX(const Analyser::Static::Target *target, const ROMMachine::ROMFetcher &rom_fetcher) {
|
2023-01-10 19:55:57 +00:00
|
|
|
const auto msx_target = dynamic_cast<const Target *>(target);
|
|
|
|
switch(msx_target->model) {
|
|
|
|
default: return nullptr;
|
|
|
|
case Target::Model::MSX1: return new ConcreteMachine<Target::Model::MSX1>(*msx_target, rom_fetcher);
|
|
|
|
case Target::Model::MSX2: return new ConcreteMachine<Target::Model::MSX2>(*msx_target, rom_fetcher);
|
|
|
|
}
|
2017-11-25 01:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Machine::~Machine() {}
|