2017-06-04 21:55:19 +00:00
|
|
|
//
|
|
|
|
// ZX8081.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/06/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ZX8081.hpp"
|
|
|
|
|
2017-06-05 14:36:07 +00:00
|
|
|
#include "../MemoryFuzzer.hpp"
|
2017-06-05 13:38:49 +00:00
|
|
|
|
2017-06-04 21:55:19 +00:00
|
|
|
using namespace ZX8081;
|
|
|
|
|
2017-06-06 14:13:32 +00:00
|
|
|
namespace {
|
2017-06-11 20:42:49 +00:00
|
|
|
// The clock rate is 3.25Mhz.
|
2017-06-06 14:13:32 +00:00
|
|
|
const unsigned int ZX8081ClockRate = 3250000;
|
|
|
|
}
|
|
|
|
|
2017-06-04 22:32:23 +00:00
|
|
|
Machine::Machine() :
|
2017-06-05 01:54:55 +00:00
|
|
|
vsync_(false),
|
|
|
|
hsync_(false),
|
2017-06-14 01:25:55 +00:00
|
|
|
nmi_is_enabled_(false),
|
2017-06-23 00:20:31 +00:00
|
|
|
tape_player_(ZX8081ClockRate),
|
2017-07-07 02:33:54 +00:00
|
|
|
use_fast_tape_hack_(false),
|
2017-07-08 23:31:20 +00:00
|
|
|
tape_advance_delay_(0),
|
|
|
|
tape_is_automatically_playing_(false),
|
2017-07-09 00:40:19 +00:00
|
|
|
tape_is_playing_(false),
|
|
|
|
has_latched_video_byte_(false) {
|
2017-06-06 14:13:32 +00:00
|
|
|
set_clock_rate(ZX8081ClockRate);
|
|
|
|
tape_player_.set_motor_control(true);
|
2017-06-12 01:21:26 +00:00
|
|
|
clear_all_keys();
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
int Machine::perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
2017-06-13 02:28:30 +00:00
|
|
|
int previous_counter = horizontal_counter_;
|
|
|
|
horizontal_counter_ += cycle.length;
|
2017-06-14 00:09:09 +00:00
|
|
|
|
2017-06-14 01:25:55 +00:00
|
|
|
if(previous_counter < vsync_start_cycle_ && horizontal_counter_ >= vsync_start_cycle_) {
|
|
|
|
video_->run_for_cycles(vsync_start_cycle_ - previous_counter);
|
|
|
|
set_hsync(true);
|
2017-06-23 02:44:06 +00:00
|
|
|
line_counter_ = (line_counter_ + 1) & 7;
|
2017-06-14 01:25:55 +00:00
|
|
|
if(nmi_is_enabled_) {
|
|
|
|
set_non_maskable_interrupt_line(true);
|
2017-06-14 00:09:09 +00:00
|
|
|
}
|
2017-06-22 01:28:14 +00:00
|
|
|
video_->run_for_cycles(horizontal_counter_ - vsync_start_cycle_);
|
2017-06-22 01:44:42 +00:00
|
|
|
} else if(previous_counter < vsync_end_cycle_ && horizontal_counter_ >= vsync_end_cycle_) {
|
2017-06-14 01:25:55 +00:00
|
|
|
video_->run_for_cycles(vsync_end_cycle_ - previous_counter);
|
|
|
|
set_hsync(false);
|
2017-06-22 01:28:14 +00:00
|
|
|
if(nmi_is_enabled_) {
|
|
|
|
set_non_maskable_interrupt_line(false);
|
|
|
|
set_wait_line(false);
|
|
|
|
}
|
2017-06-14 01:25:55 +00:00
|
|
|
video_->run_for_cycles(horizontal_counter_ - vsync_end_cycle_);
|
|
|
|
} else {
|
|
|
|
video_->run_for_cycles(cycle.length);
|
2017-06-13 02:28:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 01:25:55 +00:00
|
|
|
if(is_zx81_) horizontal_counter_ %= 207;
|
2017-07-07 02:33:54 +00:00
|
|
|
if(!tape_advance_delay_) {
|
2017-07-08 23:21:12 +00:00
|
|
|
if(tape_is_automatically_playing_ || tape_is_playing_) tape_player_.run_for_cycles(cycle.length);
|
2017-07-07 02:33:54 +00:00
|
|
|
} else {
|
|
|
|
tape_advance_delay_ = std::max(tape_advance_delay_ - cycle.length, 0);
|
|
|
|
}
|
2017-06-04 22:32:23 +00:00
|
|
|
|
2017-06-23 00:11:19 +00:00
|
|
|
if(nmi_is_enabled_ && !get_halt_line() && get_non_maskable_interrupt_line()) {
|
|
|
|
set_wait_line(true);
|
|
|
|
}
|
|
|
|
|
2017-06-22 01:03:39 +00:00
|
|
|
if(!cycle.is_terminal()) {
|
2017-06-22 01:28:14 +00:00
|
|
|
return 0;
|
2017-06-22 01:03:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:38:49 +00:00
|
|
|
uint16_t address = cycle.address ? *cycle.address : 0;
|
2017-06-22 00:32:08 +00:00
|
|
|
bool is_opcode_read = false;
|
2017-06-04 22:32:23 +00:00
|
|
|
switch(cycle.operation) {
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Output:
|
2017-06-15 02:24:44 +00:00
|
|
|
if(!(address & 2)) nmi_is_enabled_ = false;
|
|
|
|
if(!(address & 1)) nmi_is_enabled_ = is_zx81_;
|
2017-07-09 01:01:52 +00:00
|
|
|
if(!nmi_is_enabled_) {
|
2017-07-09 04:05:30 +00:00
|
|
|
// Line counter reset is held low while vsync is active; simulate that lazily by performing
|
|
|
|
// an instant reset upon the transition from active to inactive.
|
|
|
|
if(vsync_) line_counter_ = 0;
|
2017-07-09 01:01:52 +00:00
|
|
|
set_vsync(false);
|
2017-06-23 02:44:06 +00:00
|
|
|
}
|
2017-06-04 22:32:23 +00:00
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Input: {
|
2017-06-06 13:25:18 +00:00
|
|
|
uint8_t value = 0xff;
|
2017-06-06 14:13:32 +00:00
|
|
|
if(!(address&1)) {
|
2017-06-23 02:44:06 +00:00
|
|
|
if(!nmi_is_enabled_) set_vsync(true);
|
2017-06-06 13:25:18 +00:00
|
|
|
|
|
|
|
uint16_t mask = 0x100;
|
|
|
|
for(int c = 0; c < 8; c++) {
|
|
|
|
if(!(address & mask)) value &= key_states_[c];
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
2017-06-06 14:13:32 +00:00
|
|
|
|
2017-06-06 22:38:05 +00:00
|
|
|
value &= ~(tape_player_.get_input() ? 0x00 : 0x80);
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
2017-06-06 13:25:18 +00:00
|
|
|
*cycle.value = value;
|
|
|
|
} break;
|
2017-06-04 22:32:23 +00:00
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Interrupt:
|
2017-06-04 22:32:23 +00:00
|
|
|
*cycle.value = 0xff;
|
2017-06-14 01:25:55 +00:00
|
|
|
horizontal_counter_ = 0;
|
2017-06-04 22:32:23 +00:00
|
|
|
break;
|
|
|
|
|
2017-06-22 01:03:39 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Refresh:
|
2017-06-22 01:18:09 +00:00
|
|
|
// The ZX80 and 81 signal an interrupt while refresh is active and bit 6 of the refresh
|
|
|
|
// address is low. The Z80 signals a refresh, providing the refresh address during the
|
|
|
|
// final two cycles of an opcode fetch. Therefore communicate a transient signalling
|
|
|
|
// of the IRQ line if necessary.
|
2017-06-22 01:03:39 +00:00
|
|
|
if(!(address & 0x40)) {
|
|
|
|
set_interrupt_line(true, -2);
|
|
|
|
set_interrupt_line(false);
|
|
|
|
}
|
2017-07-09 00:40:19 +00:00
|
|
|
if(has_latched_video_byte_) {
|
2017-07-07 02:48:48 +00:00
|
|
|
size_t char_address = (size_t)((address & 0xfe00) | ((latched_video_byte_ & 0x3f) << 3) | line_counter_);
|
2017-06-23 02:48:17 +00:00
|
|
|
uint8_t mask = (latched_video_byte_ & 0x80) ? 0x00 : 0xff;
|
2017-06-22 01:18:09 +00:00
|
|
|
if(char_address < ram_base_) {
|
|
|
|
latched_video_byte_ = rom_[char_address & rom_mask_] ^ mask;
|
2017-06-23 02:48:17 +00:00
|
|
|
} else {
|
|
|
|
latched_video_byte_ = ram_[address & ram_mask_] ^ mask;
|
2017-06-22 01:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
video_->output_byte(latched_video_byte_);
|
2017-07-09 00:40:19 +00:00
|
|
|
has_latched_video_byte_ = false;
|
2017-06-22 01:18:09 +00:00
|
|
|
}
|
2017-06-22 01:03:39 +00:00
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::ReadOpcodeStart:
|
|
|
|
case CPU::Z80::PartialMachineCycle::ReadOpcodeWait:
|
2017-06-11 20:42:49 +00:00
|
|
|
// Check for use of the fast tape hack.
|
2017-06-24 00:18:19 +00:00
|
|
|
if(use_fast_tape_hack_ && address == tape_trap_address_ && tape_player_.has_tape()) {
|
|
|
|
Storage::Time time = tape_player_.get_tape()->get_current_time();
|
2017-06-11 22:31:43 +00:00
|
|
|
int next_byte = parser_.get_next_byte(tape_player_.get_tape());
|
2017-06-11 20:42:49 +00:00
|
|
|
if(next_byte != -1) {
|
|
|
|
uint16_t hl = get_value_of_register(CPU::Z80::Register::HL);
|
2017-06-12 01:27:46 +00:00
|
|
|
ram_[hl & ram_mask_] = (uint8_t)next_byte;
|
2017-06-11 20:42:49 +00:00
|
|
|
*cycle.value = 0x00;
|
2017-06-11 21:28:47 +00:00
|
|
|
set_value_of_register(CPU::Z80::Register::ProgramCounter, tape_return_address_ - 1);
|
2017-07-07 02:33:54 +00:00
|
|
|
|
|
|
|
// Assume that having read one byte quickly, we're probably going to be asked to read
|
|
|
|
// another shortly. Therefore, temporarily disable the tape motor for 1000 cycles in order
|
|
|
|
// to avoid fighting with real time. This is a stop-gap fix.
|
|
|
|
tape_advance_delay_ = 1000;
|
2017-06-11 20:42:49 +00:00
|
|
|
return 0;
|
2017-06-24 00:18:19 +00:00
|
|
|
} else {
|
|
|
|
tape_player_.get_tape()->seek(time);
|
2017-06-11 20:42:49 +00:00
|
|
|
}
|
2017-06-23 00:20:31 +00:00
|
|
|
}
|
2017-07-08 23:31:20 +00:00
|
|
|
|
|
|
|
// Check for automatic tape control.
|
|
|
|
tape_is_automatically_playing_ = use_automatic_tape_motor_control_ && (address >= automatic_tape_motor_start_address_) && (address < automatic_tape_motor_end_address_);
|
2017-06-22 00:32:08 +00:00
|
|
|
is_opcode_read = true;
|
2017-06-11 20:42:49 +00:00
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Read:
|
2017-06-11 23:29:02 +00:00
|
|
|
if(address < ram_base_) {
|
|
|
|
*cycle.value = rom_[address & rom_mask_];
|
|
|
|
} else {
|
|
|
|
uint8_t value = ram_[address & ram_mask_];
|
|
|
|
|
|
|
|
// If this is an M1 cycle reading from above the 32kb mark and HALT is not
|
2017-07-07 02:48:48 +00:00
|
|
|
// currently active, latch for video output and return a NOP. Otherwise,
|
2017-06-11 23:29:02 +00:00
|
|
|
// just return the value as read.
|
2017-06-22 00:32:08 +00:00
|
|
|
if(is_opcode_read && address&0x8000 && !(value & 0x40) && !get_halt_line()) {
|
2017-06-22 01:18:09 +00:00
|
|
|
latched_video_byte_ = value;
|
2017-07-09 00:40:19 +00:00
|
|
|
has_latched_video_byte_ = true;
|
2017-06-04 22:32:23 +00:00
|
|
|
*cycle.value = 0;
|
2017-06-05 14:36:07 +00:00
|
|
|
} else *cycle.value = value;
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Write:
|
2017-06-11 23:29:02 +00:00
|
|
|
if(address >= ram_base_) {
|
|
|
|
ram_[address & ram_mask_] = *cycle.value;
|
|
|
|
}
|
2017-06-04 22:32:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2017-06-22 01:28:14 +00:00
|
|
|
return 0;
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 22:08:35 +00:00
|
|
|
void Machine::flush() {
|
2017-06-06 21:53:23 +00:00
|
|
|
video_->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::setup_output(float aspect_ratio) {
|
|
|
|
video_.reset(new Video);
|
2017-06-04 22:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:55:19 +00:00
|
|
|
void Machine::close_output() {
|
2017-06-06 21:53:23 +00:00
|
|
|
video_.reset();
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
|
2017-06-06 21:53:23 +00:00
|
|
|
return video_->get_crt();
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Outputs::Speaker> Machine::get_speaker() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::run_for_cycles(int number_of_cycles) {
|
2017-06-04 22:37:13 +00:00
|
|
|
CPU::Z80::Processor<Machine>::run_for_cycles(number_of_cycles);
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
|
2017-06-13 01:33:16 +00:00
|
|
|
is_zx81_ = target.zx8081.isZX81;
|
|
|
|
if(is_zx81_) {
|
2017-06-11 20:42:49 +00:00
|
|
|
rom_ = zx81_rom_;
|
|
|
|
tape_trap_address_ = 0x37c;
|
|
|
|
tape_return_address_ = 0x380;
|
2017-06-14 01:48:17 +00:00
|
|
|
vsync_start_cycle_ = 16;
|
|
|
|
vsync_end_cycle_ = 32;
|
2017-07-08 23:31:20 +00:00
|
|
|
automatic_tape_motor_start_address_ = 0x0340;
|
|
|
|
automatic_tape_motor_end_address_ = 0x03c3;
|
2017-06-11 23:29:02 +00:00
|
|
|
} else {
|
|
|
|
rom_ = zx80_rom_;
|
|
|
|
tape_trap_address_ = 0x220;
|
|
|
|
tape_return_address_ = 0x248;
|
2017-06-14 01:48:17 +00:00
|
|
|
vsync_start_cycle_ = 13;
|
|
|
|
vsync_end_cycle_ = 33;
|
2017-07-08 23:31:20 +00:00
|
|
|
automatic_tape_motor_start_address_ = 0x0206;
|
|
|
|
automatic_tape_motor_end_address_ = 0x024d;
|
2017-06-11 23:29:02 +00:00
|
|
|
}
|
|
|
|
rom_mask_ = (uint16_t)(rom_.size() - 1);
|
|
|
|
|
|
|
|
switch(target.zx8081.memory_model) {
|
|
|
|
case StaticAnalyser::ZX8081MemoryModel::Unexpanded:
|
|
|
|
ram_.resize(1024);
|
|
|
|
ram_base_ = 16384;
|
|
|
|
ram_mask_ = 1023;
|
|
|
|
break;
|
|
|
|
case StaticAnalyser::ZX8081MemoryModel::SixteenKB:
|
|
|
|
ram_.resize(16384);
|
|
|
|
ram_base_ = 16384;
|
|
|
|
ram_mask_ = 16383;
|
|
|
|
break;
|
|
|
|
case StaticAnalyser::ZX8081MemoryModel::SixtyFourKB:
|
|
|
|
ram_.resize(65536);
|
|
|
|
ram_base_ = 8192;
|
2017-06-12 01:21:26 +00:00
|
|
|
ram_mask_ = 65535;
|
2017-06-11 23:29:02 +00:00
|
|
|
break;
|
2017-06-11 20:42:49 +00:00
|
|
|
}
|
2017-06-12 01:21:26 +00:00
|
|
|
Memory::Fuzz(ram_);
|
2017-06-06 14:13:32 +00:00
|
|
|
|
|
|
|
if(target.tapes.size()) {
|
|
|
|
tape_player_.set_tape(target.tapes.front());
|
|
|
|
}
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
2017-06-04 22:08:35 +00:00
|
|
|
|
|
|
|
void Machine::set_rom(ROMType type, std::vector<uint8_t> data) {
|
|
|
|
switch(type) {
|
|
|
|
case ZX80: zx80_rom_ = data; break;
|
|
|
|
case ZX81: zx81_rom_ = data; break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 22:32:23 +00:00
|
|
|
|
|
|
|
#pragma mark - Video
|
|
|
|
|
2017-06-05 01:54:55 +00:00
|
|
|
void Machine::set_vsync(bool sync) {
|
|
|
|
vsync_ = sync;
|
2017-06-05 14:47:42 +00:00
|
|
|
update_sync();
|
2017-06-05 01:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::set_hsync(bool sync) {
|
|
|
|
hsync_ = sync;
|
2017-06-05 14:47:42 +00:00
|
|
|
update_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::update_sync() {
|
2017-06-06 21:53:23 +00:00
|
|
|
video_->set_sync(vsync_ || hsync_);
|
2017-06-06 12:59:00 +00:00
|
|
|
}
|
2017-06-06 13:25:18 +00:00
|
|
|
|
|
|
|
#pragma mark - Keyboard
|
|
|
|
|
|
|
|
void Machine::set_key_state(uint16_t key, bool isPressed) {
|
|
|
|
if(isPressed)
|
|
|
|
key_states_[key >> 8] &= (uint8_t)(~key);
|
|
|
|
else
|
|
|
|
key_states_[key >> 8] |= (uint8_t)key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::clear_all_keys() {
|
|
|
|
memset(key_states_, 0xff, 8);
|
|
|
|
}
|