2016-11-06 21:13:13 +00:00
|
|
|
//
|
|
|
|
// Acorn.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 06/11/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-11-06 21:13:13 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Acorn.hpp"
|
|
|
|
|
|
|
|
using namespace Storage::Tape::Acorn;
|
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
namespace {
|
2019-12-22 05:22:17 +00:00
|
|
|
constexpr int PLLClockRate = 1920000;
|
2017-07-15 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 00:01:31 +00:00
|
|
|
Parser::Parser(): crc_(0x1021) {
|
2017-07-16 23:24:01 +00:00
|
|
|
shifter_.set_delegate(this);
|
|
|
|
}
|
2016-11-06 21:13:13 +00:00
|
|
|
|
2017-07-14 01:26:05 +00:00
|
|
|
int Parser::get_next_bit(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
2021-03-08 01:49:40 +00:00
|
|
|
const SymbolType symbol = get_next_symbol(tape);
|
2016-11-06 21:13:13 +00:00
|
|
|
return (symbol == SymbolType::One) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2017-07-14 01:26:05 +00:00
|
|
|
int Parser::get_next_byte(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
|
|
|
if(get_next_bit(tape)) {
|
2016-11-06 21:13:13 +00:00
|
|
|
set_error_flag();
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-07 20:56:58 +00:00
|
|
|
|
|
|
|
int value = 0;
|
|
|
|
int c = 8;
|
2017-07-14 01:26:05 +00:00
|
|
|
while(c--) {
|
2016-11-06 21:13:13 +00:00
|
|
|
value = (value >> 1) | (get_next_bit(tape) << 7);
|
|
|
|
}
|
2017-07-14 01:26:05 +00:00
|
|
|
if(!get_next_bit(tape)) {
|
2016-11-06 21:13:13 +00:00
|
|
|
set_error_flag();
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-10 03:00:39 +00:00
|
|
|
crc_.add(uint8_t(value));
|
2016-11-06 21:13:13 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-10-18 02:34:49 +00:00
|
|
|
unsigned int Parser::get_next_short(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
2020-05-10 03:00:39 +00:00
|
|
|
unsigned int result = unsigned(get_next_byte(tape));
|
|
|
|
result |= unsigned(get_next_byte(tape)) << 8;
|
2016-11-06 21:13:13 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-18 02:34:49 +00:00
|
|
|
unsigned int Parser::get_next_word(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
|
|
|
unsigned int result = get_next_short(tape);
|
2016-11-06 21:13:13 +00:00
|
|
|
result |= get_next_short(tape) << 8;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:49:40 +00:00
|
|
|
void Parser::reset_crc() { crc_.reset(); }
|
|
|
|
uint16_t Parser::get_crc() const { return crc_.get_value(); }
|
2016-11-06 21:13:13 +00:00
|
|
|
|
2017-07-16 23:24:01 +00:00
|
|
|
void Parser::acorn_shifter_output_bit(int value) {
|
|
|
|
push_symbol(value ? SymbolType::One : SymbolType::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::process_pulse(const Storage::Tape::Tape::Pulse &pulse) {
|
|
|
|
shifter_.process_pulse(pulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Shifter::Shifter() :
|
2020-01-12 22:45:02 +00:00
|
|
|
pll_(PLLClockRate / 4800, *this),
|
2017-07-16 23:24:01 +00:00
|
|
|
was_high_(false),
|
|
|
|
input_pattern_(0),
|
2020-01-12 22:45:02 +00:00
|
|
|
delegate_(nullptr) {}
|
2017-07-16 23:24:01 +00:00
|
|
|
|
|
|
|
void Shifter::process_pulse(const Storage::Tape::Tape::Pulse &pulse) {
|
2020-05-10 03:00:39 +00:00
|
|
|
pll_.run_for(Cycles(int(float(PLLClockRate) * pulse.length.get<float>())));
|
2017-07-16 23:24:01 +00:00
|
|
|
|
2021-03-07 20:56:58 +00:00
|
|
|
const bool is_high = pulse.type == Storage::Tape::Tape::Pulse::High;
|
2017-07-16 23:24:01 +00:00
|
|
|
if(is_high != was_high_) {
|
|
|
|
pll_.add_pulse();
|
|
|
|
}
|
|
|
|
was_high_ = is_high;
|
|
|
|
}
|
2016-11-06 21:13:13 +00:00
|
|
|
|
2017-07-16 23:24:01 +00:00
|
|
|
void Shifter::digital_phase_locked_loop_output_bit(int value) {
|
2020-05-10 03:00:39 +00:00
|
|
|
input_pattern_ = ((input_pattern_ << 1) | unsigned(value)) & 0xf;
|
2017-07-16 23:24:01 +00:00
|
|
|
switch(input_pattern_) {
|
|
|
|
case 0x5: delegate_->acorn_shifter_output_bit(0); input_pattern_ = 0; break;
|
|
|
|
case 0xf: delegate_->acorn_shifter_output_bit(1); input_pattern_ = 0; break;
|
2017-10-21 23:49:04 +00:00
|
|
|
default: break;
|
2016-11-06 21:13:13 +00:00
|
|
|
}
|
|
|
|
}
|