2017-06-04 20:37:03 +00:00
|
|
|
//
|
|
|
|
// ZX80O.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/06/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
#include "ZX80O81P.hpp"
|
2017-06-09 01:52:13 +00:00
|
|
|
#include "../../Data/ZX8081.hpp"
|
2017-06-04 20:37:03 +00:00
|
|
|
|
|
|
|
using namespace Storage::Tape;
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
ZX80O81P::ZX80O81P(const char *file_name) :
|
2017-06-04 20:37:03 +00:00
|
|
|
Storage::FileHolder(file_name) {
|
|
|
|
|
2017-06-12 23:41:59 +00:00
|
|
|
// Grab the actual file contents
|
|
|
|
data_.resize((size_t)file_stats_.st_size);
|
|
|
|
fread(data_.data(), 1, (size_t)file_stats_.st_size, file_);
|
|
|
|
|
|
|
|
// If it's a ZX81 file, prepend a file name.
|
|
|
|
char type = (char)tolower(file_name[strlen(file_name) - 1]);
|
|
|
|
if(type == 'p' || type == '1') {
|
|
|
|
// TODO, maybe: prefix a proper file name; this is leaving the file nameless.
|
|
|
|
data_.insert(data_.begin(), 0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<::Storage::Data::ZX8081::File> file = Storage::Data::ZX8081::FileFromData(data_);
|
|
|
|
if(!file) throw ErrorNotZX80O81P;
|
2017-06-04 20:59:26 +00:00
|
|
|
|
2017-06-04 20:37:03 +00:00
|
|
|
// then rewind and start again
|
|
|
|
virtual_reset();
|
|
|
|
}
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
void ZX80O81P::virtual_reset() {
|
2017-06-12 01:35:09 +00:00
|
|
|
data_pointer_ = 0;
|
2017-06-04 20:59:26 +00:00
|
|
|
is_past_silence_ = false;
|
2017-06-09 01:31:54 +00:00
|
|
|
has_ended_final_byte_ = false;
|
2017-06-04 20:59:26 +00:00
|
|
|
is_high_ = true;
|
2017-06-06 22:29:15 +00:00
|
|
|
bit_pointer_ = wave_pointer_ = 0;
|
2017-06-04 20:37:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
bool ZX80O81P::has_finished_data() {
|
2017-06-12 01:35:09 +00:00
|
|
|
return (data_pointer_ == data_.size()) && !wave_pointer_ && !bit_pointer_;
|
2017-06-09 01:31:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
bool ZX80O81P::is_at_end() {
|
2017-06-09 01:31:54 +00:00
|
|
|
return has_finished_data() && has_ended_final_byte_;
|
2017-06-04 20:37:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 01:38:32 +00:00
|
|
|
Tape::Pulse ZX80O81P::virtual_get_next_pulse() {
|
2017-06-04 20:37:03 +00:00
|
|
|
Tape::Pulse pulse;
|
|
|
|
|
2017-06-04 20:59:26 +00:00
|
|
|
// Start with 1 second of silence.
|
2017-06-09 01:31:54 +00:00
|
|
|
if(!is_past_silence_ || has_finished_data()) {
|
2017-06-07 21:39:29 +00:00
|
|
|
pulse.type = Pulse::Type::Low;
|
2017-07-08 23:21:33 +00:00
|
|
|
pulse.length.length = 1;
|
2017-06-04 20:59:26 +00:00
|
|
|
pulse.length.clock_rate = 1;
|
|
|
|
is_past_silence_ = true;
|
2017-06-09 01:31:54 +00:00
|
|
|
has_ended_final_byte_ = has_finished_data();
|
2017-06-04 20:59:26 +00:00
|
|
|
return pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each byte, output 8 bits and then silence.
|
2017-06-06 22:29:15 +00:00
|
|
|
if(!bit_pointer_ && !wave_pointer_) {
|
2017-06-12 01:35:09 +00:00
|
|
|
byte_ = data_[data_pointer_];
|
|
|
|
data_pointer_++;
|
2017-06-04 20:59:26 +00:00
|
|
|
bit_pointer_ = 0;
|
|
|
|
wave_pointer_ = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:29:15 +00:00
|
|
|
if(!wave_pointer_) {
|
|
|
|
// post-waves silence (here actually a pre-waves silence) is 1300µs
|
|
|
|
pulse.length.length = 13;
|
|
|
|
pulse.length.clock_rate = 10000;
|
2017-06-07 21:39:29 +00:00
|
|
|
pulse.type = Pulse::Type::Low;
|
2017-06-06 22:29:15 +00:00
|
|
|
|
|
|
|
wave_pointer_ ++;
|
|
|
|
} else {
|
2017-06-07 14:05:16 +00:00
|
|
|
// pulses are of length 150µs
|
2017-06-04 20:59:26 +00:00
|
|
|
pulse.length.length = 3;
|
|
|
|
pulse.length.clock_rate = 20000;
|
|
|
|
|
|
|
|
if(is_high_) {
|
|
|
|
pulse.type = Pulse::Type::High;
|
|
|
|
is_high_ = false;
|
|
|
|
} else {
|
|
|
|
pulse.type = Pulse::Type::Low;
|
|
|
|
is_high_ = true;
|
|
|
|
|
2017-06-06 22:29:15 +00:00
|
|
|
// Bytes are stored MSB first.
|
2017-06-04 20:59:26 +00:00
|
|
|
int wave_count = (byte_ & (0x80 >> bit_pointer_)) ? 9 : 4;
|
|
|
|
wave_pointer_++;
|
2017-06-06 22:29:15 +00:00
|
|
|
if(wave_pointer_ == wave_count + 1) {
|
|
|
|
bit_pointer_ = (bit_pointer_ + 1)&7;
|
2017-06-04 20:59:26 +00:00
|
|
|
wave_pointer_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 20:37:03 +00:00
|
|
|
return pulse;
|
|
|
|
}
|