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 {
|
|
|
|
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-05 14:47:42 +00:00
|
|
|
ram_(1024),
|
2017-06-06 13:25:18 +00:00
|
|
|
line_data_(nullptr),
|
2017-06-06 14:13:32 +00:00
|
|
|
key_states_{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
|
|
|
tape_player_(ZX8081ClockRate) {
|
2017-06-04 21:55:19 +00:00
|
|
|
// run at 3.25 Mhz
|
2017-06-06 14:13:32 +00:00
|
|
|
set_clock_rate(ZX8081ClockRate);
|
2017-06-05 14:36:07 +00:00
|
|
|
Memory::Fuzz(ram_);
|
2017-06-06 14:13:32 +00:00
|
|
|
tape_player_.set_motor_control(true);
|
2017-06-04 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Machine::perform_machine_cycle(const CPU::Z80::MachineCycle &cycle) {
|
2017-06-06 12:55:07 +00:00
|
|
|
cycles_since_display_update_ += (unsigned int)cycle.length;
|
2017-06-06 14:13:32 +00:00
|
|
|
tape_player_.run_for_cycles(cycle.length);
|
2017-06-04 22:32:23 +00:00
|
|
|
|
2017-06-06 13:03:09 +00:00
|
|
|
uint16_t refresh = 0;
|
2017-06-05 13:38:49 +00:00
|
|
|
uint16_t address = cycle.address ? *cycle.address : 0;
|
2017-06-04 22:32:23 +00:00
|
|
|
switch(cycle.operation) {
|
|
|
|
case CPU::Z80::BusOperation::Output:
|
2017-06-06 14:13:32 +00:00
|
|
|
// if((address&7) == 7) {
|
2017-06-05 01:54:55 +00:00
|
|
|
set_vsync(false);
|
2017-06-06 14:13:32 +00:00
|
|
|
line_counter_ = 0;
|
|
|
|
// }
|
2017-06-04 22:32:23 +00:00
|
|
|
break;
|
|
|
|
|
2017-06-06 13:25:18 +00:00
|
|
|
case CPU::Z80::BusOperation::Input: {
|
|
|
|
uint8_t value = 0xff;
|
2017-06-06 14:13:32 +00:00
|
|
|
if(!(address&1)) {
|
2017-06-05 01:54:55 +00:00
|
|
|
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
|
|
|
|
|
|
|
mask &= ~(tape_player_.get_input() ? 0x80 : 0);
|
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
|
|
|
|
|
|
|
case CPU::Z80::BusOperation::Interrupt:
|
2017-06-05 01:54:55 +00:00
|
|
|
set_hsync(true);
|
2017-06-06 12:55:07 +00:00
|
|
|
line_counter_ = (line_counter_ + 1) & 7;
|
2017-06-04 22:32:23 +00:00
|
|
|
*cycle.value = 0xff;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::BusOperation::ReadOpcode:
|
2017-06-05 01:54:55 +00:00
|
|
|
set_hsync(false);
|
2017-06-06 13:03:09 +00:00
|
|
|
refresh = get_value_of_register(CPU::Z80::Register::Refresh);
|
|
|
|
set_interrupt_line(!(refresh & 0x40));
|
2017-06-04 22:32:23 +00:00
|
|
|
case CPU::Z80::BusOperation::Read:
|
2017-06-05 13:38:49 +00:00
|
|
|
if((address & 0xc000) == 0x0000) *cycle.value = rom_[address & (rom_.size() - 1)];
|
|
|
|
else if((address & 0x4000) == 0x4000) {
|
|
|
|
uint8_t value = ram_[address & 1023];
|
|
|
|
if(address&0x8000 && !(value & 0x40) && cycle.operation == CPU::Z80::BusOperation::ReadOpcode && !get_halt_line()) {
|
2017-06-06 13:03:09 +00:00
|
|
|
size_t char_address = (size_t)((refresh & 0xff00) | ((value & 0x3f) << 3) | line_counter_);
|
2017-06-06 12:55:07 +00:00
|
|
|
if((char_address & 0xc000) == 0x0000) {
|
2017-06-06 13:03:09 +00:00
|
|
|
uint8_t mask = (value & 0x80) ? 0x00 : 0xff;
|
2017-06-06 12:55:07 +00:00
|
|
|
value = rom_[char_address & (rom_.size() - 1)] ^ mask;
|
|
|
|
}
|
|
|
|
|
2017-06-04 22:32:23 +00:00
|
|
|
// TODO: character lookup.
|
|
|
|
output_byte(value);
|
|
|
|
*cycle.value = 0;
|
2017-06-05 14:36:07 +00:00
|
|
|
} else *cycle.value = value;
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU::Z80::BusOperation::Write:
|
2017-06-05 13:38:49 +00:00
|
|
|
if((address & 0x4000) == 0x4000) ram_[address & 1023] = *cycle.value;
|
2017-06-04 22:32:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:55:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-04 22:08:35 +00:00
|
|
|
void Machine::flush() {
|
2017-06-04 22:32:23 +00:00
|
|
|
update_display();
|
2017-06-04 22:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:55:19 +00:00
|
|
|
void Machine::close_output() {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
|
|
|
|
return crt_;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04 22:32:23 +00:00
|
|
|
// TODO: pay attention to the target
|
|
|
|
rom_ = zx80_rom_;
|
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
|
|
|
|
|
|
|
|
void Machine::update_display() {
|
2017-06-05 14:47:42 +00:00
|
|
|
// cycles_since_display_update_ = 0;
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 01:54:55 +00:00
|
|
|
void Machine::set_vsync(bool sync) {
|
2017-06-05 13:38:49 +00:00
|
|
|
if(sync == vsync_) return;
|
2017-06-05 01:54:55 +00:00
|
|
|
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) {
|
2017-06-05 13:38:49 +00:00
|
|
|
if(sync == hsync_) return;
|
2017-06-05 01:54:55 +00:00
|
|
|
hsync_ = sync;
|
2017-06-05 14:47:42 +00:00
|
|
|
update_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::update_sync() {
|
|
|
|
bool is_sync = hsync_ || vsync_;
|
|
|
|
if(is_sync == is_sync_) return;
|
|
|
|
|
2017-06-06 03:32:49 +00:00
|
|
|
if(line_data_) {
|
|
|
|
output_data();
|
|
|
|
}
|
|
|
|
|
2017-06-05 14:47:42 +00:00
|
|
|
if(is_sync_) {
|
2017-06-06 12:59:00 +00:00
|
|
|
crt_->output_sync(cycles_since_display_update_ << 1);
|
2017-06-05 14:47:42 +00:00
|
|
|
} else {
|
2017-06-06 12:59:00 +00:00
|
|
|
output_level(cycles_since_display_update_ << 1);
|
2017-06-05 14:47:42 +00:00
|
|
|
}
|
|
|
|
cycles_since_display_update_ = 0;
|
|
|
|
is_sync_ = is_sync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::output_level(unsigned int number_of_cycles) {
|
|
|
|
uint8_t *colour_pointer = (uint8_t *)crt_->allocate_write_area(1);
|
2017-06-06 03:50:04 +00:00
|
|
|
if(colour_pointer) *colour_pointer = 0xff;
|
2017-06-05 14:47:42 +00:00
|
|
|
crt_->output_level(number_of_cycles);
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:32:49 +00:00
|
|
|
void Machine::output_data() {
|
2017-06-06 12:59:00 +00:00
|
|
|
unsigned int data_length = (unsigned int)(line_data_pointer_ - line_data_);
|
|
|
|
crt_->output_data(data_length, 1);
|
2017-06-06 03:32:49 +00:00
|
|
|
line_data_pointer_ = line_data_ = nullptr;
|
2017-06-06 12:59:00 +00:00
|
|
|
cycles_since_display_update_ -= data_length >> 1;
|
2017-06-06 03:32:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 22:32:23 +00:00
|
|
|
void Machine::output_byte(uint8_t byte) {
|
2017-06-06 03:32:49 +00:00
|
|
|
if(line_data_) {
|
|
|
|
if(cycles_since_display_update_ > 4) {
|
|
|
|
output_data();
|
|
|
|
}
|
2017-06-06 03:50:04 +00:00
|
|
|
} else {
|
2017-06-06 12:59:00 +00:00
|
|
|
output_level(cycles_since_display_update_ << 1);
|
2017-06-06 03:52:56 +00:00
|
|
|
cycles_since_display_update_ = 0;
|
2017-06-06 03:32:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 14:47:42 +00:00
|
|
|
if(!line_data_) {
|
2017-06-06 03:32:49 +00:00
|
|
|
line_data_pointer_ = line_data_ = crt_->allocate_write_area(320);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(line_data_) {
|
2017-06-06 12:55:07 +00:00
|
|
|
uint8_t mask = 0x80;
|
2017-06-06 12:59:00 +00:00
|
|
|
for(int c = 0; c < 8; c++) {
|
2017-06-06 12:55:07 +00:00
|
|
|
line_data_pointer_[c] = (byte & mask) ? 0xff : 0x00;
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
2017-06-06 12:59:00 +00:00
|
|
|
line_data_pointer_ += 8;
|
|
|
|
|
|
|
|
if(line_data_pointer_ - line_data_ == 320) {
|
|
|
|
output_data();
|
|
|
|
}
|
2017-06-05 14:47:42 +00:00
|
|
|
}
|
2017-06-04 22:32:23 +00:00
|
|
|
}
|
2017-06-06 12:59:00 +00:00
|
|
|
|
|
|
|
void Machine::setup_output(float aspect_ratio) {
|
|
|
|
crt_.reset(new Outputs::CRT::CRT(210 * 2, 1, Outputs::CRT::DisplayType::PAL50, 1));
|
2017-06-06 13:25:18 +00:00
|
|
|
crt_->set_composite_sampling_function(
|
|
|
|
"float composite_sample(usampler2D sampler, vec2 coordinate, vec2 icoordinate, float phase, float amplitude)"
|
2017-06-06 12:59:00 +00:00
|
|
|
"{"
|
2017-06-06 13:25:18 +00:00
|
|
|
"return float(texture(texID, coordinate).r) / 255.0;"
|
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);
|
|
|
|
}
|