1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-25 18:30:21 +00:00

Merge pull request #203 from TomHarte/ElectronInline

Adds the Electron to the pantheon of machines that reveal very little in their public interface
This commit is contained in:
Thomas Harte 2017-08-16 15:35:11 -04:00 committed by GitHub
commit 3c50903a2b
5 changed files with 535 additions and 561 deletions

View File

@ -54,7 +54,7 @@ class Machine:
public:
virtual ~Machine();
/// Creates and returns an Amstrad CPC on the heap.
/// Creates and returns an Amstrad CPC.
static Machine *AmstradCPC();
/// Sets the contents of rom @c type to @c data. Assumed to be a setup step; has no effect once a machine is running.

View File

@ -8,412 +8,465 @@
#include "Electron.hpp"
#include "../../Processors/6502/6502.hpp"
#include "../../Storage/Tape/Tape.hpp"
#include "../../ClockReceiver/ClockReceiver.hpp"
#include "../Typer.hpp"
#include "CharacterMapper.hpp"
#include "Interrupts.hpp"
#include "Plus3.hpp"
#include "Speaker.hpp"
#include "Tape.hpp"
#include "Video.hpp"
namespace Electron {
class ConcreteMachine:
public Machine,
public CPU::MOS6502::BusHandler,
public Tape::Delegate,
public Utility::TypeRecipient {
public:
ConcreteMachine() :
m6502_(*this),
interrupt_control_(0),
interrupt_status_(Interrupt::PowerOnReset | Interrupt::TransmitDataEmpty | 0x80),
cycles_since_audio_update_(0),
use_fast_tape_hack_(false),
cycles_until_display_interrupt_(0) {
memset(key_states_, 0, sizeof(key_states_));
for(int c = 0; c < 16; c++)
memset(roms_[c], 0xff, 16384);
tape_.set_delegate(this);
set_clock_rate(2000000);
}
void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) {
uint8_t *target = nullptr;
switch(slot) {
case ROMSlotDFS: dfs_ = data; return;
case ROMSlotADFS: adfs_ = data; return;
case ROMSlotOS: target = os_; break;
default:
target = roms_[slot];
rom_write_masks_[slot] = is_writeable;
break;
}
memcpy(target, &data[0], std::min((size_t)16384, data.size()));
}
void set_key_state(uint16_t key, bool isPressed) {
if(key == KeyBreak) {
m6502_.set_reset_line(isPressed);
} else {
if(isPressed)
key_states_[key >> 4] |= key&0xf;
else
key_states_[key >> 4] &= ~(key&0xf);
}
}
void clear_all_keys() {
memset(key_states_, 0, sizeof(key_states_));
if(is_holding_shift_) set_key_state(KeyShift, true);
}
void set_use_fast_tape_hack(bool activate) {
use_fast_tape_hack_ = activate;
}
void configure_as_target(const StaticAnalyser::Target &target) {
if(target.tapes.size()) {
tape_.set_tape(target.tapes.front());
}
if(target.disks.size()) {
plus3_.reset(new Plus3);
if(target.acorn.has_dfs) {
set_rom(ROMSlot0, dfs_, true);
}
if(target.acorn.has_adfs) {
set_rom(ROMSlot4, adfs_, true);
set_rom(ROMSlot5, std::vector<uint8_t>(adfs_.begin() + 16384, adfs_.end()), true);
}
plus3_->set_disk(target.disks.front(), 0);
}
ROMSlot slot = ROMSlot12;
for(std::shared_ptr<Storage::Cartridge::Cartridge> cartridge : target.cartridges) {
set_rom(slot, cartridge->get_segments().front().data, false);
slot = (ROMSlot)(((int)slot + 1)&15);
}
if(target.loadingCommand.length()) {
set_typer_for_string(target.loadingCommand.c_str());
}
if(target.acorn.should_shift_restart) {
shift_restart_counter_ = 1000000;
}
}
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
unsigned int cycles = 1;
if(address < 0x8000) {
if(isReadOperation(operation)) {
*value = ram_[address];
} else {
if(address >= video_access_range_.low_address && address <= video_access_range_.high_address) update_display();
ram_[address] = *value;
}
// for the entire frame, RAM is accessible only on odd cycles; in modes below 4
// it's also accessible only outside of the pixel regions
cycles += video_output_->get_cycles_until_next_ram_availability(cycles_since_display_update_.as_int() + 1);
} else {
switch(address & 0xff0f) {
case 0xfe00:
if(isReadOperation(operation)) {
*value = interrupt_status_;
interrupt_status_ &= ~PowerOnReset;
} else {
interrupt_control_ = (*value) & ~1;
evaluate_interrupts();
}
break;
case 0xfe07:
if(!isReadOperation(operation)) {
// update speaker mode
bool new_speaker_is_enabled = (*value & 6) == 2;
if(new_speaker_is_enabled != speaker_is_enabled_) {
update_audio();
speaker_->set_is_enabled(new_speaker_is_enabled);
speaker_is_enabled_ = new_speaker_is_enabled;
}
tape_.set_is_enabled((*value & 6) != 6);
tape_.set_is_in_input_mode((*value & 6) == 0);
tape_.set_is_running(((*value)&0x40) ? true : false);
// TODO: caps lock LED
}
// deliberate fallthrough
case 0xfe02: case 0xfe03:
case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b:
case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
if(!isReadOperation(operation)) {
update_display();
video_output_->set_register(address, *value);
video_access_range_ = video_output_->get_memory_access_range();
queue_next_display_interrupt();
}
break;
case 0xfe04:
if(isReadOperation(operation)) {
*value = tape_.get_data_register();
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
} else {
tape_.set_data_register(*value);
tape_.clear_interrupts(Interrupt::TransmitDataEmpty);
}
break;
case 0xfe05:
if(!isReadOperation(operation)) {
const uint8_t interruptDisable = (*value)&0xf0;
if( interruptDisable ) {
if( interruptDisable&0x10 ) interrupt_status_ &= ~Interrupt::DisplayEnd;
if( interruptDisable&0x20 ) interrupt_status_ &= ~Interrupt::RealTimeClock;
if( interruptDisable&0x40 ) interrupt_status_ &= ~Interrupt::HighToneDetect;
evaluate_interrupts();
// TODO: NMI
}
// latch the paged ROM in case external hardware is being emulated
active_rom_ = (Electron::ROMSlot)(*value & 0xf);
// apply the ULA's test
if(*value & 0x08) {
if(*value & 0x04) {
keyboard_is_active_ = false;
basic_is_active_ = false;
} else {
keyboard_is_active_ = !(*value & 0x02);
basic_is_active_ = !keyboard_is_active_;
}
}
}
break;
case 0xfe06:
if(!isReadOperation(operation)) {
update_audio();
speaker_->set_divider(*value);
tape_.set_counter(*value);
}
break;
case 0xfc04: case 0xfc05: case 0xfc06: case 0xfc07:
if(plus3_ && (address&0x00f0) == 0x00c0) {
if(is_holding_shift_ && address == 0xfcc4) {
is_holding_shift_ = false;
set_key_state(KeyShift, false);
}
if(isReadOperation(operation))
*value = plus3_->get_register(address);
else
plus3_->set_register(address, *value);
}
break;
case 0xfc00:
if(plus3_ && (address&0x00f0) == 0x00c0) {
if(!isReadOperation(operation)) {
plus3_->set_control_register(*value);
} else *value = 1;
}
break;
default:
if(address >= 0xc000) {
if(isReadOperation(operation)) {
if(
use_fast_tape_hack_ &&
tape_.has_tape() &&
(operation == CPU::MOS6502::BusOperation::ReadOpcode) &&
(
(address == 0xf4e5) || (address == 0xf4e6) || // double NOPs at 0xf4e5, 0xf6de, 0xf6fa and 0xfa51
(address == 0xf6de) || (address == 0xf6df) || // act to disable the normal branch into tape-handling
(address == 0xf6fa) || (address == 0xf6fb) || // code, forcing the OS along the serially-accessed ROM
(address == 0xfa51) || (address == 0xfa52) || // pathway.
(address == 0xf0a8) // 0xf0a8 is from where a service call would normally be
// dispatched; we can check whether it would be call 14
// (i.e. read byte) and, if so, whether the OS was about to
// issue a read byte call to a ROM despite being the tape
// FS being selected. If so then this is a get byte that
// we should service synthetically. Put the byte into Y
// and set A to zero to report that action was taken, then
// allow the PC read to return an RTS.
)
) {
uint8_t service_call = (uint8_t)m6502_.get_value_of_register(CPU::MOS6502::Register::X);
if(address == 0xf0a8) {
if(!ram_[0x247] && service_call == 14) {
tape_.set_delegate(nullptr);
// TODO: handle tape wrap around.
int cycles_left_while_plausibly_in_data = 50;
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
while(!tape_.get_tape()->is_at_end()) {
tape_.run_for_input_pulse();
cycles_left_while_plausibly_in_data--;
if(!cycles_left_while_plausibly_in_data) fast_load_is_in_data_ = false;
if( (tape_.get_interrupt_status() & Interrupt::ReceiveDataFull) &&
(fast_load_is_in_data_ || tape_.get_data_register() == 0x2a)
) break;
}
tape_.set_delegate(this);
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
interrupt_status_ |= tape_.get_interrupt_status();
fast_load_is_in_data_ = true;
m6502_.set_value_of_register(CPU::MOS6502::Register::A, 0);
m6502_.set_value_of_register(CPU::MOS6502::Register::Y, tape_.get_data_register());
*value = 0x60; // 0x60 is RTS
}
else *value = os_[address & 16383];
}
else *value = 0xea;
} else {
*value = os_[address & 16383];
}
}
} else {
if(isReadOperation(operation)) {
*value = roms_[active_rom_][address & 16383];
if(keyboard_is_active_) {
*value &= 0xf0;
for(int address_line = 0; address_line < 14; address_line++) {
if(!(address&(1 << address_line))) *value |= key_states_[address_line];
}
}
if(basic_is_active_) {
*value &= roms_[ROMSlotBASIC][address & 16383];
}
} else if(rom_write_masks_[active_rom_]) {
roms_[active_rom_][address & 16383] = *value;
}
}
break;
}
}
cycles_since_display_update_ += Cycles((int)cycles);
cycles_since_audio_update_ += Cycles((int)cycles);
if(cycles_since_audio_update_ > Cycles(16384)) update_audio();
tape_.run_for(Cycles((int)cycles));
cycles_until_display_interrupt_ -= cycles;
if(cycles_until_display_interrupt_ < 0) {
signal_interrupt(next_display_interrupt_);
update_display();
queue_next_display_interrupt();
}
if(typer_) typer_->run_for(Cycles((int)cycles));
if(plus3_) plus3_->run_for(Cycles(4*(int)cycles));
if(shift_restart_counter_) {
shift_restart_counter_ -= cycles;
if(shift_restart_counter_ <= 0) {
shift_restart_counter_ = 0;
m6502_.set_power_on(true);
set_key_state(KeyShift, true);
is_holding_shift_ = true;
}
}
return Cycles((int)cycles);
}
void flush() {
update_display();
update_audio();
speaker_->flush();
}
void setup_output(float aspect_ratio) {
video_output_.reset(new VideoOutput(ram_));
// The maximum output frequency is 62500Hz and all other permitted output frequencies are integral divisions of that;
// however setting the speaker on or off can happen on any 2Mhz cycle, and probably (?) takes effect immediately. So
// run the speaker at a 2000000Hz input rate, at least for the time being.
speaker_.reset(new Speaker);
speaker_->set_input_rate(2000000 / Speaker::clock_rate_divider);
}
void close_output() {
video_output_.reset();
}
std::shared_ptr<Outputs::CRT::CRT> get_crt() {
return video_output_->get_crt();
}
std::shared_ptr<Outputs::Speaker> get_speaker() {
return speaker_;
}
virtual void run_for(const Cycles cycles) {
m6502_.run_for(cycles);
}
void tape_did_change_interrupt_status(Tape *tape) {
interrupt_status_ = (interrupt_status_ & ~(Interrupt::TransmitDataEmpty | Interrupt::ReceiveDataFull | Interrupt::HighToneDetect)) | tape_.get_interrupt_status();
evaluate_interrupts();
}
HalfCycles get_typer_delay() {
return m6502_.get_is_resetting() ? Cycles(625*25*128) : Cycles(0); // wait one second if resetting
}
HalfCycles get_typer_frequency() {
return Cycles(625*128*2); // accept a new character every two frames
}
void set_typer_for_string(const char *string) {
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper());
Utility::TypeRecipient::set_typer_for_string(string, std::move(mapper));
}
private:
inline void update_display() {
if(cycles_since_display_update_ > 0) {
video_output_->run_for(cycles_since_display_update_.flush());
}
}
inline void queue_next_display_interrupt() {
VideoOutput::Interrupt next_interrupt = video_output_->get_next_interrupt();
cycles_until_display_interrupt_ = next_interrupt.cycles;
next_display_interrupt_ = next_interrupt.interrupt;
}
inline void update_audio() {
if(cycles_since_audio_update_ > 0) {
speaker_->run_for(cycles_since_audio_update_.divide(Cycles(Speaker::clock_rate_divider)));
}
}
inline void signal_interrupt(Interrupt interrupt) {
interrupt_status_ |= interrupt;
evaluate_interrupts();
}
inline void clear_interrupt(Interrupt interrupt) {
interrupt_status_ &= ~interrupt;
evaluate_interrupts();
}
inline void evaluate_interrupts() {
if(interrupt_status_ & interrupt_control_) {
interrupt_status_ |= 1;
} else {
interrupt_status_ &= ~1;
}
m6502_.set_irq_line(interrupt_status_ & 1);
}
CPU::MOS6502::Processor<ConcreteMachine> m6502_;
// Things that directly constitute the memory map.
uint8_t roms_[16][16384];
bool rom_write_masks_[16];
uint8_t os_[16384], ram_[32768];
std::vector<uint8_t> dfs_, adfs_;
// Paging
ROMSlot active_rom_;
bool keyboard_is_active_, basic_is_active_;
// Interrupt and keyboard state
uint8_t interrupt_status_, interrupt_control_;
uint8_t key_states_[14];
// Counters related to simultaneous subsystems
Cycles cycles_since_display_update_;
Cycles cycles_since_audio_update_;
int cycles_until_display_interrupt_;
Interrupt next_display_interrupt_;
VideoOutput::Range video_access_range_;
// Tape
Tape tape_;
bool use_fast_tape_hack_;
bool fast_load_is_in_data_;
// Disk
std::unique_ptr<Plus3> plus3_;
bool is_holding_shift_;
int shift_restart_counter_;
// Outputs
std::unique_ptr<VideoOutput> video_output_;
std::shared_ptr<Speaker> speaker_;
bool speaker_is_enabled_;
};
}
using namespace Electron;
#pragma mark - Lifecycle
Machine::Machine() :
m6502_(*this),
interrupt_control_(0),
interrupt_status_(Interrupt::PowerOnReset | Interrupt::TransmitDataEmpty | 0x80),
cycles_since_audio_update_(0),
use_fast_tape_hack_(false),
cycles_until_display_interrupt_(0) {
memset(key_states_, 0, sizeof(key_states_));
for(int c = 0; c < 16; c++)
memset(roms_[c], 0xff, 16384);
tape_.set_delegate(this);
set_clock_rate(2000000);
Machine *Machine::Electron() {
return new Electron::ConcreteMachine;
}
#pragma mark - Output
void Machine::setup_output(float aspect_ratio) {
video_output_.reset(new VideoOutput(ram_));
// The maximum output frequency is 62500Hz and all other permitted output frequencies are integral divisions of that;
// however setting the speaker on or off can happen on any 2Mhz cycle, and probably (?) takes effect immediately. So
// run the speaker at a 2000000Hz input rate, at least for the time being.
speaker_.reset(new Speaker);
speaker_->set_input_rate(2000000 / Speaker::clock_rate_divider);
}
void Machine::close_output() {
video_output_.reset();
}
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
return video_output_->get_crt();
}
std::shared_ptr<Outputs::Speaker> Machine::get_speaker() {
return speaker_;
}
#pragma mark - The keyboard
void Machine::clear_all_keys() {
memset(key_states_, 0, sizeof(key_states_));
if(is_holding_shift_) set_key_state(KeyShift, true);
}
void Machine::set_key_state(uint16_t key, bool isPressed) {
if(key == KeyBreak) {
m6502_.set_reset_line(isPressed);
} else {
if(isPressed)
key_states_[key >> 4] |= key&0xf;
else
key_states_[key >> 4] &= ~(key&0xf);
}
}
#pragma mark - Machine configuration
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
if(target.tapes.size()) {
tape_.set_tape(target.tapes.front());
}
if(target.disks.size()) {
plus3_.reset(new Plus3);
if(target.acorn.has_dfs) {
set_rom(ROMSlot0, dfs_, true);
}
if(target.acorn.has_adfs) {
set_rom(ROMSlot4, adfs_, true);
set_rom(ROMSlot5, std::vector<uint8_t>(adfs_.begin() + 16384, adfs_.end()), true);
}
plus3_->set_disk(target.disks.front(), 0);
}
ROMSlot slot = ROMSlot12;
for(std::shared_ptr<Storage::Cartridge::Cartridge> cartridge : target.cartridges) {
set_rom(slot, cartridge->get_segments().front().data, false);
slot = (ROMSlot)(((int)slot + 1)&15);
}
if(target.loadingCommand.length()) {
set_typer_for_string(target.loadingCommand.c_str());
}
if(target.acorn.should_shift_restart) {
shift_restart_counter_ = 1000000;
}
}
void Machine::set_typer_for_string(const char *string) {
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper());
Utility::TypeRecipient::set_typer_for_string(string, std::move(mapper));
}
void Machine::set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) {
uint8_t *target = nullptr;
switch(slot) {
case ROMSlotDFS: dfs_ = data; return;
case ROMSlotADFS: adfs_ = data; return;
case ROMSlotOS: target = os_; break;
default:
target = roms_[slot];
rom_write_masks_[slot] = is_writeable;
break;
}
memcpy(target, &data[0], std::min((size_t)16384, data.size()));
}
#pragma mark - The bus
Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
unsigned int cycles = 1;
if(address < 0x8000) {
if(isReadOperation(operation)) {
*value = ram_[address];
} else {
if(address >= video_access_range_.low_address && address <= video_access_range_.high_address) update_display();
ram_[address] = *value;
}
// for the entire frame, RAM is accessible only on odd cycles; in modes below 4
// it's also accessible only outside of the pixel regions
cycles += video_output_->get_cycles_until_next_ram_availability(cycles_since_display_update_.as_int() + 1);
} else {
switch(address & 0xff0f) {
case 0xfe00:
if(isReadOperation(operation)) {
*value = interrupt_status_;
interrupt_status_ &= ~PowerOnReset;
} else {
interrupt_control_ = (*value) & ~1;
evaluate_interrupts();
}
break;
case 0xfe07:
if(!isReadOperation(operation)) {
// update speaker mode
bool new_speaker_is_enabled = (*value & 6) == 2;
if(new_speaker_is_enabled != speaker_is_enabled_) {
update_audio();
speaker_->set_is_enabled(new_speaker_is_enabled);
speaker_is_enabled_ = new_speaker_is_enabled;
}
tape_.set_is_enabled((*value & 6) != 6);
tape_.set_is_in_input_mode((*value & 6) == 0);
tape_.set_is_running(((*value)&0x40) ? true : false);
// TODO: caps lock LED
}
// deliberate fallthrough
case 0xfe02: case 0xfe03:
case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b:
case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
if(!isReadOperation(operation)) {
update_display();
video_output_->set_register(address, *value);
video_access_range_ = video_output_->get_memory_access_range();
queue_next_display_interrupt();
}
break;
case 0xfe04:
if(isReadOperation(operation)) {
*value = tape_.get_data_register();
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
} else {
tape_.set_data_register(*value);
tape_.clear_interrupts(Interrupt::TransmitDataEmpty);
}
break;
case 0xfe05:
if(!isReadOperation(operation)) {
const uint8_t interruptDisable = (*value)&0xf0;
if( interruptDisable ) {
if( interruptDisable&0x10 ) interrupt_status_ &= ~Interrupt::DisplayEnd;
if( interruptDisable&0x20 ) interrupt_status_ &= ~Interrupt::RealTimeClock;
if( interruptDisable&0x40 ) interrupt_status_ &= ~Interrupt::HighToneDetect;
evaluate_interrupts();
// TODO: NMI
}
// latch the paged ROM in case external hardware is being emulated
active_rom_ = (Electron::ROMSlot)(*value & 0xf);
// apply the ULA's test
if(*value & 0x08) {
if(*value & 0x04) {
keyboard_is_active_ = false;
basic_is_active_ = false;
} else {
keyboard_is_active_ = !(*value & 0x02);
basic_is_active_ = !keyboard_is_active_;
}
}
}
break;
case 0xfe06:
if(!isReadOperation(operation)) {
update_audio();
speaker_->set_divider(*value);
tape_.set_counter(*value);
}
break;
case 0xfc04: case 0xfc05: case 0xfc06: case 0xfc07:
if(plus3_ && (address&0x00f0) == 0x00c0) {
if(is_holding_shift_ && address == 0xfcc4) {
is_holding_shift_ = false;
set_key_state(KeyShift, false);
}
if(isReadOperation(operation))
*value = plus3_->get_register(address);
else
plus3_->set_register(address, *value);
}
break;
case 0xfc00:
if(plus3_ && (address&0x00f0) == 0x00c0) {
if(!isReadOperation(operation)) {
plus3_->set_control_register(*value);
} else *value = 1;
}
break;
default:
if(address >= 0xc000) {
if(isReadOperation(operation)) {
if(
use_fast_tape_hack_ &&
tape_.has_tape() &&
(operation == CPU::MOS6502::BusOperation::ReadOpcode) &&
(
(address == 0xf4e5) || (address == 0xf4e6) || // double NOPs at 0xf4e5, 0xf6de, 0xf6fa and 0xfa51
(address == 0xf6de) || (address == 0xf6df) || // act to disable the normal branch into tape-handling
(address == 0xf6fa) || (address == 0xf6fb) || // code, forcing the OS along the serially-accessed ROM
(address == 0xfa51) || (address == 0xfa52) || // pathway.
(address == 0xf0a8) // 0xf0a8 is from where a service call would normally be
// dispatched; we can check whether it would be call 14
// (i.e. read byte) and, if so, whether the OS was about to
// issue a read byte call to a ROM despite being the tape
// FS being selected. If so then this is a get byte that
// we should service synthetically. Put the byte into Y
// and set A to zero to report that action was taken, then
// allow the PC read to return an RTS.
)
) {
uint8_t service_call = (uint8_t)m6502_.get_value_of_register(CPU::MOS6502::Register::X);
if(address == 0xf0a8) {
if(!ram_[0x247] && service_call == 14) {
tape_.set_delegate(nullptr);
// TODO: handle tape wrap around.
int cycles_left_while_plausibly_in_data = 50;
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
while(!tape_.get_tape()->is_at_end()) {
tape_.run_for_input_pulse();
cycles_left_while_plausibly_in_data--;
if(!cycles_left_while_plausibly_in_data) fast_load_is_in_data_ = false;
if( (tape_.get_interrupt_status() & Interrupt::ReceiveDataFull) &&
(fast_load_is_in_data_ || tape_.get_data_register() == 0x2a)
) break;
}
tape_.set_delegate(this);
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
interrupt_status_ |= tape_.get_interrupt_status();
fast_load_is_in_data_ = true;
m6502_.set_value_of_register(CPU::MOS6502::Register::A, 0);
m6502_.set_value_of_register(CPU::MOS6502::Register::Y, tape_.get_data_register());
*value = 0x60; // 0x60 is RTS
}
else *value = os_[address & 16383];
}
else *value = 0xea;
} else {
*value = os_[address & 16383];
}
}
} else {
if(isReadOperation(operation)) {
*value = roms_[active_rom_][address & 16383];
if(keyboard_is_active_) {
*value &= 0xf0;
for(int address_line = 0; address_line < 14; address_line++) {
if(!(address&(1 << address_line))) *value |= key_states_[address_line];
}
}
if(basic_is_active_) {
*value &= roms_[ROMSlotBASIC][address & 16383];
}
} else if(rom_write_masks_[active_rom_]) {
roms_[active_rom_][address & 16383] = *value;
}
}
break;
}
}
cycles_since_display_update_ += Cycles((int)cycles);
cycles_since_audio_update_ += Cycles((int)cycles);
if(cycles_since_audio_update_ > Cycles(16384)) update_audio();
tape_.run_for(Cycles((int)cycles));
cycles_until_display_interrupt_ -= cycles;
if(cycles_until_display_interrupt_ < 0) {
signal_interrupt(next_display_interrupt_);
update_display();
queue_next_display_interrupt();
}
if(typer_) typer_->run_for(Cycles((int)cycles));
if(plus3_) plus3_->run_for(Cycles(4*(int)cycles));
if(shift_restart_counter_) {
shift_restart_counter_ -= cycles;
if(shift_restart_counter_ <= 0) {
shift_restart_counter_ = 0;
m6502_.set_power_on(true);
set_key_state(KeyShift, true);
is_holding_shift_ = true;
}
}
return Cycles((int)cycles);
}
void Machine::flush() {
update_display();
update_audio();
speaker_->flush();
}
void Machine::run_for(const Cycles cycles) {
m6502_.run_for(cycles);
}
#pragma mark - Deferred scheduling
inline void Machine::update_display() {
if(cycles_since_display_update_ > 0) {
video_output_->run_for(cycles_since_display_update_.flush());
}
}
inline void Machine::queue_next_display_interrupt() {
VideoOutput::Interrupt next_interrupt = video_output_->get_next_interrupt();
cycles_until_display_interrupt_ = next_interrupt.cycles;
next_display_interrupt_ = next_interrupt.interrupt;
}
inline void Machine::update_audio() {
if(cycles_since_audio_update_ > 0) {
speaker_->run_for(cycles_since_audio_update_.divide(Cycles(Speaker::clock_rate_divider)));
}
}
#pragma mark - Interrupts
inline void Machine::signal_interrupt(Electron::Interrupt interrupt) {
interrupt_status_ |= interrupt;
evaluate_interrupts();
}
inline void Machine::clear_interrupt(Electron::Interrupt interrupt) {
interrupt_status_ &= ~interrupt;
evaluate_interrupts();
}
inline void Machine::evaluate_interrupts() {
if(interrupt_status_ & interrupt_control_) {
interrupt_status_ |= 1;
} else {
interrupt_status_ &= ~1;
}
m6502_.set_irq_line(interrupt_status_ & 1);
}
#pragma mark - Tape::Delegate
void Machine::tape_did_change_interrupt_status(Tape *tape) {
interrupt_status_ = (interrupt_status_ & ~(Interrupt::TransmitDataEmpty | Interrupt::ReceiveDataFull | Interrupt::HighToneDetect)) | tape_.get_interrupt_status();
evaluate_interrupts();
}
#pragma mark - Typer timing
HalfCycles Electron::Machine::get_typer_delay() {
return m6502_.get_is_resetting() ? Cycles(625*25*128) : Cycles(0); // wait one second if resetting
}
HalfCycles Electron::Machine::get_typer_frequency() {
return Cycles(625*128*2); // accept a new character every two frames
}
Machine::~Machine() {}

View File

@ -9,19 +9,9 @@
#ifndef Electron_hpp
#define Electron_hpp
#include "../../Processors/6502/6502.hpp"
#include "../../Storage/Tape/Tape.hpp"
#include "../../ClockReceiver/ClockReceiver.hpp"
#include "../ConfigurationTarget.hpp"
#include "../CRTMachine.hpp"
#include "../Typer.hpp"
#include "Interrupts.hpp"
#include "Plus3.hpp"
#include "Speaker.hpp"
#include "Tape.hpp"
#include "Video.hpp"
#include "../KeyboardMachine.hpp"
#include <cstdint>
#include <vector>
@ -67,90 +57,23 @@ enum Key: uint16_t {
Acorn Electron.
*/
class Machine:
public CPU::MOS6502::BusHandler,
public CRTMachine::Machine,
public Tape::Delegate,
public Utility::TypeRecipient,
public ConfigurationTarget::Machine {
public ConfigurationTarget::Machine,
public KeyboardMachine::Machine {
public:
Machine();
virtual ~Machine();
void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable);
/// Creates and returns an Electron.
static Machine *Electron();
void set_key_state(uint16_t key, bool isPressed);
void clear_all_keys();
/*!
Sets the contents of @c slot to @c data. If @c is_writeable is @c true then writing to the slot
is enabled it acts as if it were sideways RAM. Otherwise the slot is modelled as containing ROM.
*/
virtual void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) = 0;
inline void set_use_fast_tape_hack(bool activate) { use_fast_tape_hack_ = activate; }
// to satisfy ConfigurationTarget::Machine
void configure_as_target(const StaticAnalyser::Target &target);
// to satisfy CPU::MOS6502::Processor
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
void flush();
// to satisfy CRTMachine::Machine
virtual void setup_output(float aspect_ratio);
virtual void close_output();
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt();
virtual std::shared_ptr<Outputs::Speaker> get_speaker();
virtual void run_for(const Cycles cycles);
// to satisfy Tape::Delegate
virtual void tape_did_change_interrupt_status(Tape *tape);
// for Utility::TypeRecipient
virtual HalfCycles get_typer_delay();
virtual HalfCycles get_typer_frequency();
virtual void set_typer_for_string(const char *string);
private:
inline void update_display();
inline void queue_next_display_interrupt();
inline void update_audio();
inline void signal_interrupt(Interrupt interrupt);
inline void clear_interrupt(Interrupt interrupt);
inline void evaluate_interrupts();
CPU::MOS6502::Processor<Machine> m6502_;
// Things that directly constitute the memory map.
uint8_t roms_[16][16384];
bool rom_write_masks_[16];
uint8_t os_[16384], ram_[32768];
std::vector<uint8_t> dfs_, adfs_;
// Paging
ROMSlot active_rom_;
bool keyboard_is_active_, basic_is_active_;
// Interrupt and keyboard state
uint8_t interrupt_status_, interrupt_control_;
uint8_t key_states_[14];
// Counters related to simultaneous subsystems
Cycles cycles_since_display_update_;
Cycles cycles_since_audio_update_;
int cycles_until_display_interrupt_;
Interrupt next_display_interrupt_;
VideoOutput::Range video_access_range_;
// Tape
Tape tape_;
bool use_fast_tape_hack_;
bool fast_load_is_in_data_;
// Disk
std::unique_ptr<Plus3> plus3_;
bool is_holding_shift_;
int shift_restart_counter_;
// Outputs
std::unique_ptr<VideoOutput> video_output_;
std::shared_ptr<Speaker> speaker_;
bool speaker_is_enabled_;
/// Enables or disables turbo-speed tape loading.
virtual void set_use_fast_tape_hack(bool activate) = 0;
};
}

View File

@ -53,7 +53,7 @@ class Machine:
public:
virtual ~Machine();
/// Creates an returns an Oric on the heap.
/// Creates and returns an Oric.
static Machine *Oric();
/// Sets the contents of @c rom to @c data. Assumed to be a setup step; has no effect once a machine is running.

View File

@ -16,11 +16,14 @@
#import "NSBundle+DataResource.h"
@implementation CSElectron {
Electron::Machine _electron;
std::unique_ptr<Electron::Machine> _electron;
}
- (CRTMachine::Machine * const)machine {
return &_electron;
if(!_electron) {
_electron.reset(Electron::Machine::Electron());
}
return _electron.get();
}
- (instancetype)init {
@ -52,7 +55,7 @@
if(rom)
{
@synchronized(self) {
_electron.set_rom((Electron::ROMSlot)slot, rom.stdVector8, false);
_electron->set_rom((Electron::ROMSlot)slot, rom.stdVector8, false);
}
}
}
@ -61,7 +64,7 @@
- (void)clearAllKeys {
@synchronized(self) {
_electron.clear_all_keys();
_electron->clear_all_keys();
}
}
@ -69,74 +72,74 @@
@synchronized(self) {
switch(key)
{
case VK_ANSI_0: _electron.set_key_state(Electron::Key::Key0, isPressed); break;
case VK_ANSI_1: _electron.set_key_state(Electron::Key::Key1, isPressed); break;
case VK_ANSI_2: _electron.set_key_state(Electron::Key::Key2, isPressed); break;
case VK_ANSI_3: _electron.set_key_state(Electron::Key::Key3, isPressed); break;
case VK_ANSI_4: _electron.set_key_state(Electron::Key::Key4, isPressed); break;
case VK_ANSI_5: _electron.set_key_state(Electron::Key::Key5, isPressed); break;
case VK_ANSI_6: _electron.set_key_state(Electron::Key::Key6, isPressed); break;
case VK_ANSI_7: _electron.set_key_state(Electron::Key::Key7, isPressed); break;
case VK_ANSI_8: _electron.set_key_state(Electron::Key::Key8, isPressed); break;
case VK_ANSI_9: _electron.set_key_state(Electron::Key::Key9, isPressed); break;
case VK_ANSI_0: _electron->set_key_state(Electron::Key::Key0, isPressed); break;
case VK_ANSI_1: _electron->set_key_state(Electron::Key::Key1, isPressed); break;
case VK_ANSI_2: _electron->set_key_state(Electron::Key::Key2, isPressed); break;
case VK_ANSI_3: _electron->set_key_state(Electron::Key::Key3, isPressed); break;
case VK_ANSI_4: _electron->set_key_state(Electron::Key::Key4, isPressed); break;
case VK_ANSI_5: _electron->set_key_state(Electron::Key::Key5, isPressed); break;
case VK_ANSI_6: _electron->set_key_state(Electron::Key::Key6, isPressed); break;
case VK_ANSI_7: _electron->set_key_state(Electron::Key::Key7, isPressed); break;
case VK_ANSI_8: _electron->set_key_state(Electron::Key::Key8, isPressed); break;
case VK_ANSI_9: _electron->set_key_state(Electron::Key::Key9, isPressed); break;
case VK_ANSI_Q: _electron.set_key_state(Electron::Key::KeyQ, isPressed); break;
case VK_ANSI_W: _electron.set_key_state(Electron::Key::KeyW, isPressed); break;
case VK_ANSI_E: _electron.set_key_state(Electron::Key::KeyE, isPressed); break;
case VK_ANSI_R: _electron.set_key_state(Electron::Key::KeyR, isPressed); break;
case VK_ANSI_T: _electron.set_key_state(Electron::Key::KeyT, isPressed); break;
case VK_ANSI_Y: _electron.set_key_state(Electron::Key::KeyY, isPressed); break;
case VK_ANSI_U: _electron.set_key_state(Electron::Key::KeyU, isPressed); break;
case VK_ANSI_I: _electron.set_key_state(Electron::Key::KeyI, isPressed); break;
case VK_ANSI_O: _electron.set_key_state(Electron::Key::KeyO, isPressed); break;
case VK_ANSI_P: _electron.set_key_state(Electron::Key::KeyP, isPressed); break;
case VK_ANSI_A: _electron.set_key_state(Electron::Key::KeyA, isPressed); break;
case VK_ANSI_S: _electron.set_key_state(Electron::Key::KeyS, isPressed); break;
case VK_ANSI_D: _electron.set_key_state(Electron::Key::KeyD, isPressed); break;
case VK_ANSI_F: _electron.set_key_state(Electron::Key::KeyF, isPressed); break;
case VK_ANSI_G: _electron.set_key_state(Electron::Key::KeyG, isPressed); break;
case VK_ANSI_H: _electron.set_key_state(Electron::Key::KeyH, isPressed); break;
case VK_ANSI_J: _electron.set_key_state(Electron::Key::KeyJ, isPressed); break;
case VK_ANSI_K: _electron.set_key_state(Electron::Key::KeyK, isPressed); break;
case VK_ANSI_L: _electron.set_key_state(Electron::Key::KeyL, isPressed); break;
case VK_ANSI_Z: _electron.set_key_state(Electron::Key::KeyZ, isPressed); break;
case VK_ANSI_X: _electron.set_key_state(Electron::Key::KeyX, isPressed); break;
case VK_ANSI_C: _electron.set_key_state(Electron::Key::KeyC, isPressed); break;
case VK_ANSI_V: _electron.set_key_state(Electron::Key::KeyV, isPressed); break;
case VK_ANSI_B: _electron.set_key_state(Electron::Key::KeyB, isPressed); break;
case VK_ANSI_N: _electron.set_key_state(Electron::Key::KeyN, isPressed); break;
case VK_ANSI_M: _electron.set_key_state(Electron::Key::KeyM, isPressed); break;
case VK_ANSI_Q: _electron->set_key_state(Electron::Key::KeyQ, isPressed); break;
case VK_ANSI_W: _electron->set_key_state(Electron::Key::KeyW, isPressed); break;
case VK_ANSI_E: _electron->set_key_state(Electron::Key::KeyE, isPressed); break;
case VK_ANSI_R: _electron->set_key_state(Electron::Key::KeyR, isPressed); break;
case VK_ANSI_T: _electron->set_key_state(Electron::Key::KeyT, isPressed); break;
case VK_ANSI_Y: _electron->set_key_state(Electron::Key::KeyY, isPressed); break;
case VK_ANSI_U: _electron->set_key_state(Electron::Key::KeyU, isPressed); break;
case VK_ANSI_I: _electron->set_key_state(Electron::Key::KeyI, isPressed); break;
case VK_ANSI_O: _electron->set_key_state(Electron::Key::KeyO, isPressed); break;
case VK_ANSI_P: _electron->set_key_state(Electron::Key::KeyP, isPressed); break;
case VK_ANSI_A: _electron->set_key_state(Electron::Key::KeyA, isPressed); break;
case VK_ANSI_S: _electron->set_key_state(Electron::Key::KeyS, isPressed); break;
case VK_ANSI_D: _electron->set_key_state(Electron::Key::KeyD, isPressed); break;
case VK_ANSI_F: _electron->set_key_state(Electron::Key::KeyF, isPressed); break;
case VK_ANSI_G: _electron->set_key_state(Electron::Key::KeyG, isPressed); break;
case VK_ANSI_H: _electron->set_key_state(Electron::Key::KeyH, isPressed); break;
case VK_ANSI_J: _electron->set_key_state(Electron::Key::KeyJ, isPressed); break;
case VK_ANSI_K: _electron->set_key_state(Electron::Key::KeyK, isPressed); break;
case VK_ANSI_L: _electron->set_key_state(Electron::Key::KeyL, isPressed); break;
case VK_ANSI_Z: _electron->set_key_state(Electron::Key::KeyZ, isPressed); break;
case VK_ANSI_X: _electron->set_key_state(Electron::Key::KeyX, isPressed); break;
case VK_ANSI_C: _electron->set_key_state(Electron::Key::KeyC, isPressed); break;
case VK_ANSI_V: _electron->set_key_state(Electron::Key::KeyV, isPressed); break;
case VK_ANSI_B: _electron->set_key_state(Electron::Key::KeyB, isPressed); break;
case VK_ANSI_N: _electron->set_key_state(Electron::Key::KeyN, isPressed); break;
case VK_ANSI_M: _electron->set_key_state(Electron::Key::KeyM, isPressed); break;
case VK_Space: _electron.set_key_state(Electron::Key::KeySpace, isPressed); break;
case VK_Space: _electron->set_key_state(Electron::Key::KeySpace, isPressed); break;
case VK_ANSI_Grave:
case VK_ANSI_Backslash:
_electron.set_key_state(Electron::Key::KeyCopy, isPressed); break;
case VK_Return: _electron.set_key_state(Electron::Key::KeyReturn, isPressed); break;
case VK_ANSI_Minus: _electron.set_key_state(Electron::Key::KeyMinus, isPressed); break;
_electron->set_key_state(Electron::Key::KeyCopy, isPressed); break;
case VK_Return: _electron->set_key_state(Electron::Key::KeyReturn, isPressed); break;
case VK_ANSI_Minus: _electron->set_key_state(Electron::Key::KeyMinus, isPressed); break;
case VK_RightArrow: _electron.set_key_state(Electron::Key::KeyRight, isPressed); break;
case VK_LeftArrow: _electron.set_key_state(Electron::Key::KeyLeft, isPressed); break;
case VK_DownArrow: _electron.set_key_state(Electron::Key::KeyDown, isPressed); break;
case VK_UpArrow: _electron.set_key_state(Electron::Key::KeyUp, isPressed); break;
case VK_RightArrow: _electron->set_key_state(Electron::Key::KeyRight, isPressed); break;
case VK_LeftArrow: _electron->set_key_state(Electron::Key::KeyLeft, isPressed); break;
case VK_DownArrow: _electron->set_key_state(Electron::Key::KeyDown, isPressed); break;
case VK_UpArrow: _electron->set_key_state(Electron::Key::KeyUp, isPressed); break;
case VK_Delete: _electron.set_key_state(Electron::Key::KeyDelete, isPressed); break;
case VK_Escape: _electron.set_key_state(Electron::Key::KeyEscape, isPressed); break;
case VK_Delete: _electron->set_key_state(Electron::Key::KeyDelete, isPressed); break;
case VK_Escape: _electron->set_key_state(Electron::Key::KeyEscape, isPressed); break;
case VK_ANSI_Comma: _electron.set_key_state(Electron::Key::KeyComma, isPressed); break;
case VK_ANSI_Period: _electron.set_key_state(Electron::Key::KeyFullStop, isPressed); break;
case VK_ANSI_Comma: _electron->set_key_state(Electron::Key::KeyComma, isPressed); break;
case VK_ANSI_Period: _electron->set_key_state(Electron::Key::KeyFullStop, isPressed); break;
case VK_ANSI_Semicolon:
_electron.set_key_state(Electron::Key::KeySemiColon, isPressed); break;
case VK_ANSI_Quote: _electron.set_key_state(Electron::Key::KeyColon, isPressed); break;
_electron->set_key_state(Electron::Key::KeySemiColon, isPressed); break;
case VK_ANSI_Quote: _electron->set_key_state(Electron::Key::KeyColon, isPressed); break;
case VK_ANSI_Slash: _electron.set_key_state(Electron::Key::KeySlash, isPressed); break;
case VK_ANSI_Slash: _electron->set_key_state(Electron::Key::KeySlash, isPressed); break;
case VK_Shift: _electron.set_key_state(Electron::Key::KeyShift, isPressed); break;
case VK_Control: _electron.set_key_state(Electron::Key::KeyControl, isPressed); break;
case VK_Shift: _electron->set_key_state(Electron::Key::KeyShift, isPressed); break;
case VK_Control: _electron->set_key_state(Electron::Key::KeyControl, isPressed); break;
case VK_Command:
case VK_Option: _electron.set_key_state(Electron::Key::KeyFunc, isPressed); break;
case VK_Option: _electron->set_key_state(Electron::Key::KeyFunc, isPressed); break;
case VK_F12: _electron.set_key_state(Electron::Key::KeyBreak, isPressed); break;
case VK_F12: _electron->set_key_state(Electron::Key::KeyBreak, isPressed); break;
default:
// printf("%02x\n", key);
@ -152,19 +155,14 @@
- (void)setUseFastLoadingHack:(BOOL)useFastLoadingHack {
@synchronized(self) {
_useFastLoadingHack = useFastLoadingHack;
_electron.set_use_fast_tape_hack(useFastLoadingHack ? true : false);
_electron->set_use_fast_tape_hack(useFastLoadingHack ? true : false);
}
}
- (void)setUseTelevisionOutput:(BOOL)useTelevisionOutput {
@synchronized(self) {
_useTelevisionOutput = useTelevisionOutput;
_electron.get_crt()->set_output_device(useTelevisionOutput ? Outputs::CRT::Television : Outputs::CRT::Monitor);
_electron->get_crt()->set_output_device(useTelevisionOutput ? Outputs::CRT::Television : Outputs::CRT::Monitor);
}
}
//override func aspectRatio() -> NSSize {
// return NSSize(width: 11.0, height: 10.0)
// }
@end