2021-03-07 20:51:25 +00:00
|
|
|
//
|
|
|
|
// Spectrum.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 07/03/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2021-03-07 20:51:25 +00:00
|
|
|
|
|
|
|
#include "TapeParser.hpp"
|
|
|
|
|
2021-03-11 03:02:10 +00:00
|
|
|
#include <array>
|
2021-03-08 01:49:09 +00:00
|
|
|
#include <optional>
|
2021-03-11 03:02:10 +00:00
|
|
|
#include <vector>
|
2021-03-08 01:49:09 +00:00
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Storage::Tape::ZXSpectrum {
|
2021-03-07 20:51:25 +00:00
|
|
|
|
|
|
|
enum class WaveType {
|
|
|
|
// All references to 't-states' below are cycles relative to the
|
|
|
|
// ZX Spectrum's 3.5Mhz processor.
|
|
|
|
|
|
|
|
Pilot, // Nominally 2168 t-states.
|
|
|
|
Zero, // 855 t-states.
|
|
|
|
One, // 1710 t-states.
|
|
|
|
Gap,
|
|
|
|
};
|
|
|
|
|
2021-03-11 03:02:10 +00:00
|
|
|
// Formally, there are two other types of wave:
|
|
|
|
//
|
|
|
|
// Sync1, // 667 t-states.
|
|
|
|
// Sync2, // 735 t-states.
|
|
|
|
//
|
|
|
|
// Non-Spectrum machines often just output a plain zero symbol instead of
|
|
|
|
// a two-step sync; this parser treats anything close enough to a zero
|
|
|
|
// as a sync.
|
|
|
|
|
2021-03-07 20:51:25 +00:00
|
|
|
enum class SymbolType {
|
|
|
|
Zero,
|
|
|
|
One,
|
2021-03-11 03:02:10 +00:00
|
|
|
Pilot,
|
2021-03-07 20:51:25 +00:00
|
|
|
Gap,
|
|
|
|
};
|
|
|
|
|
2021-03-11 03:02:10 +00:00
|
|
|
/// A block is anything that follows a period of pilot tone; on a Spectrum that might be a
|
|
|
|
/// file header or the file contents; on a CPC it might be a file header or a single chunk providing
|
|
|
|
/// partial file contents. The Enterprise seems broadly to follow the Spectrum but the internal
|
|
|
|
/// byte structure differs.
|
|
|
|
struct Block {
|
2021-03-08 01:49:09 +00:00
|
|
|
uint8_t type = 0;
|
|
|
|
};
|
|
|
|
|
2021-03-07 20:51:25 +00:00
|
|
|
class Parser: public Storage::Tape::PulseClassificationParser<WaveType, SymbolType> {
|
2024-12-02 02:44:14 +00:00
|
|
|
public:
|
|
|
|
enum class MachineType {
|
|
|
|
ZXSpectrum,
|
|
|
|
Enterprise,
|
|
|
|
SAMCoupe,
|
|
|
|
AmstradCPC
|
|
|
|
};
|
|
|
|
Parser(MachineType);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Calibrates the expected data speed using a value in the CPC's native tape-speed measurement scale.
|
|
|
|
*/
|
|
|
|
void set_cpc_read_speed(uint8_t);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Finds the next block from the tape, if any.
|
|
|
|
|
|
|
|
Following this call the tape will be positioned immediately after the byte that indicated the block type —
|
|
|
|
in Spectrum-world this seems to be called the flag byte. This call can therefore be followed up with one
|
|
|
|
of the get_ methods.
|
|
|
|
*/
|
|
|
|
std::optional<Block> find_block(const std::shared_ptr<Storage::Tape::Tape> &tape);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Reads the contents of the rest of this block, until the next gap.
|
|
|
|
*/
|
|
|
|
std::vector<uint8_t> get_block_body(const std::shared_ptr<Storage::Tape::Tape> &tape);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Reads a single byte from the tape, if there is one left, updating the internal checksum.
|
|
|
|
|
|
|
|
The checksum is computed as an exclusive OR of all bytes read.
|
|
|
|
*/
|
|
|
|
std::optional<uint8_t> get_byte(const std::shared_ptr<Storage::Tape::Tape> &tape);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Seeds the internal checksum.
|
|
|
|
*/
|
|
|
|
void seed_checksum(uint8_t value = 0x00);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Push a pulse; primarily provided for Storage::Tape::PulseClassificationParser but also potentially useful
|
|
|
|
for picking up fast loading from an ongoing tape.
|
|
|
|
*/
|
2024-12-04 03:28:57 +00:00
|
|
|
void process_pulse(const Storage::Tape::Pulse &pulse) override;
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const MachineType machine_type_;
|
|
|
|
constexpr bool should_flip_bytes() {
|
|
|
|
return machine_type_ == MachineType::Enterprise;
|
|
|
|
}
|
|
|
|
constexpr bool should_detect_speed() {
|
|
|
|
return machine_type_ != MachineType::ZXSpectrum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inspect_waves(const std::vector<WaveType> &waves) override;
|
|
|
|
|
|
|
|
uint8_t checksum_ = 0;
|
|
|
|
|
|
|
|
enum class SpeedDetectionPhase {
|
|
|
|
WaitingForGap,
|
|
|
|
WaitingForPilot,
|
|
|
|
CalibratingPilot,
|
|
|
|
Done
|
|
|
|
} speed_phase_ = SpeedDetectionPhase::Done;
|
|
|
|
|
|
|
|
float too_long_ = 2600.0f;
|
|
|
|
float too_short_ = 600.0f;
|
|
|
|
float is_pilot_ = 1939.0f;
|
|
|
|
float is_one_ = 1282.0f;
|
|
|
|
|
|
|
|
std::array<float, 8> calibration_pulses_;
|
|
|
|
size_t calibration_pulse_pointer_ = 0;
|
|
|
|
|
|
|
|
void set_cpc_one_zero_boundary(float);
|
2021-03-07 20:51:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|