2017-11-25 18:18:24 +00:00
|
|
|
//
|
|
|
|
// CAS.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 25/11/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CAS.hpp"
|
|
|
|
|
2017-12-23 23:41:50 +00:00
|
|
|
#include <cassert>
|
2017-12-22 03:34:03 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2017-11-25 18:18:24 +00:00
|
|
|
using namespace Storage::Tape;
|
|
|
|
|
2017-12-22 03:34:03 +00:00
|
|
|
namespace {
|
|
|
|
const uint8_t header_signature[8] = {0x1f, 0xa6, 0xde, 0xba, 0xcc, 0x13, 0x7d, 0x74};
|
|
|
|
}
|
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
CAS::CAS(const std::string &file_name) {
|
2017-12-23 23:41:50 +00:00
|
|
|
Storage::FileHolder file(file_name);
|
2017-12-29 14:56:58 +00:00
|
|
|
uint8_t lookahead[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
2017-12-23 23:41:50 +00:00
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
// Entirely fill the lookahead and verify that its start matches the header signature.
|
|
|
|
get_next(file, lookahead, 10);
|
2017-12-23 23:41:50 +00:00
|
|
|
if(std::memcmp(lookahead, header_signature, sizeof(header_signature))) throw ErrorNotCAS;
|
|
|
|
|
|
|
|
while(!file.eof()) {
|
|
|
|
// Just found a header, so flush the lookahead.
|
|
|
|
get_next(file, lookahead, 8);
|
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
// Create a new chunk
|
|
|
|
chunks_.emplace_back();
|
|
|
|
Chunk &chunk = chunks_.back();
|
2017-12-23 23:41:50 +00:00
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
// Decide whether to award a long header and/or a gap.
|
|
|
|
bool bytes_are_equal = true;
|
|
|
|
for(std::size_t index = 0; index < sizeof(lookahead); index++)
|
|
|
|
bytes_are_equal &= (lookahead[index] == lookahead[0]);
|
2017-12-23 23:41:50 +00:00
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
chunk.long_header = bytes_are_equal && ((lookahead[0] == 0xd3) || (lookahead[0] == 0xd0) || (lookahead[0] == 0xea));
|
|
|
|
chunk.has_gap = chunk.long_header && (chunks_.size() > 1);
|
2017-12-23 23:41:50 +00:00
|
|
|
|
2017-12-29 15:42:18 +00:00
|
|
|
// Keep going until another header arrives or the file ends. Headers require the magic byte sequence,
|
|
|
|
// and also must be eight-byte aligned within the file.
|
|
|
|
while( !file.eof() &&
|
|
|
|
(std::memcmp(lookahead, header_signature, sizeof(header_signature)) || ((file.tell()-10)&7))) {
|
2017-12-29 14:56:58 +00:00
|
|
|
chunk.data.push_back(lookahead[0]);
|
2017-12-23 23:41:50 +00:00
|
|
|
get_next(file, lookahead, 1);
|
|
|
|
}
|
|
|
|
|
2017-12-29 15:42:18 +00:00
|
|
|
// If the file ended, flush the lookahead. The final thing in it will be a 0xff from the read that
|
|
|
|
// triggered the eof, so don't include that.
|
2017-12-24 00:54:42 +00:00
|
|
|
if(file.eof()) {
|
2017-12-29 15:42:18 +00:00
|
|
|
for(std::size_t index = 0; index < sizeof(lookahead) - 1; index++)
|
2017-12-29 14:56:58 +00:00
|
|
|
chunk.data.push_back(lookahead[index]);
|
2017-12-23 23:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:20:04 +00:00
|
|
|
/*!
|
|
|
|
Treating @c buffer as a sliding lookahead, shifts it @c quantity elements to the left and
|
|
|
|
populates the new empty area to the right from @c file.
|
|
|
|
*/
|
2017-12-29 14:56:58 +00:00
|
|
|
void CAS::get_next(Storage::FileHolder &file, uint8_t (&buffer)[10], std::size_t quantity) {
|
|
|
|
assert(quantity <= sizeof(buffer));
|
2017-12-23 23:41:50 +00:00
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
if(quantity < sizeof(buffer))
|
|
|
|
std::memmove(buffer, &buffer[quantity], sizeof(buffer) - quantity);
|
2017-12-23 23:41:50 +00:00
|
|
|
|
|
|
|
while(quantity--) {
|
2017-12-29 14:56:58 +00:00
|
|
|
buffer[sizeof(buffer) - 1 - quantity] = file.get8();
|
2017-12-23 23:41:50 +00:00
|
|
|
}
|
2017-11-25 18:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAS::is_at_end() {
|
2017-12-23 23:41:50 +00:00
|
|
|
return phase_ == Phase::EndOfFile;
|
2017-11-25 18:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAS::virtual_reset() {
|
2017-12-22 03:34:03 +00:00
|
|
|
phase_ = Phase::Header;
|
2017-12-23 23:41:50 +00:00
|
|
|
chunk_pointer_ = 0;
|
2017-12-22 03:34:03 +00:00
|
|
|
distance_into_phase_ = 0;
|
2017-12-23 23:41:50 +00:00
|
|
|
distance_into_bit_ = 0;
|
2017-11-25 18:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Tape::Pulse CAS::virtual_get_next_pulse() {
|
2017-12-22 03:34:03 +00:00
|
|
|
Pulse pulse;
|
2017-12-24 00:20:04 +00:00
|
|
|
pulse.length.clock_rate = 9600;
|
|
|
|
// Clock rate is four times the baud rate (of 2400), because the quickest thing that might need
|
|
|
|
// to be communicated is a '1', which is two cycles at the baud rate, i.e. four events:
|
|
|
|
// high, low, high, low.
|
2017-12-23 23:41:50 +00:00
|
|
|
|
|
|
|
// If this is a gap, then that terminates a file. If this is already the end
|
|
|
|
// of the file then perpetual gaps await.
|
|
|
|
if(phase_ == Phase::Gap || phase_ == Phase::EndOfFile) {
|
|
|
|
pulse.length.length = pulse.length.clock_rate;
|
|
|
|
pulse.type = Pulse::Type::Zero;
|
|
|
|
|
|
|
|
if(phase_ == Phase::Gap) {
|
|
|
|
phase_ = Phase::Header;
|
|
|
|
distance_into_phase_ = 0;
|
|
|
|
}
|
2017-12-22 03:34:03 +00:00
|
|
|
|
2017-12-23 23:41:50 +00:00
|
|
|
return pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine which bit is now forthcoming.
|
|
|
|
int bit = 1;
|
|
|
|
|
|
|
|
switch(phase_) {
|
|
|
|
default: break;
|
2017-12-22 03:34:03 +00:00
|
|
|
|
|
|
|
case Phase::Header: {
|
2017-12-24 00:20:04 +00:00
|
|
|
// In the header, all bits are 1s, so let the default value stand. Just check whether the
|
|
|
|
// header is ended and, if so, move on to bytes.
|
2017-12-23 23:41:50 +00:00
|
|
|
distance_into_bit_++;
|
|
|
|
if(distance_into_bit_ == 2) {
|
|
|
|
distance_into_phase_++;
|
2017-12-24 00:20:04 +00:00
|
|
|
distance_into_bit_ = 0;
|
|
|
|
|
|
|
|
// This code always produces a 2400 baud signal; so use the appropriate Red Book-supplied
|
|
|
|
// constants to check whether the header has come to an end.
|
2017-12-29 14:56:58 +00:00
|
|
|
if(distance_into_phase_ == (chunks_[chunk_pointer_].long_header ? 31744 : 7936)) {
|
2017-12-23 23:41:50 +00:00
|
|
|
phase_ = Phase::Bytes;
|
|
|
|
distance_into_phase_ = 0;
|
|
|
|
distance_into_bit_ = 0;
|
2017-12-22 03:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Phase::Bytes: {
|
2017-12-24 00:20:04 +00:00
|
|
|
// Provide bits with a single '0' start bit and two '1' stop bits.
|
2017-12-29 14:56:58 +00:00
|
|
|
uint8_t byte_value = chunks_[chunk_pointer_].data[distance_into_phase_ / 11];
|
2017-12-23 23:41:50 +00:00
|
|
|
int bit_offset = distance_into_phase_ % 11;
|
2017-12-22 03:34:03 +00:00
|
|
|
switch(bit_offset) {
|
|
|
|
case 0: bit = 0; break;
|
2017-12-23 23:41:50 +00:00
|
|
|
default: bit = (byte_value >> (bit_offset - 1)) & 1; break;
|
2017-12-22 03:34:03 +00:00
|
|
|
case 9:
|
|
|
|
case 10: bit = 1; break;
|
|
|
|
}
|
|
|
|
|
2017-12-29 14:56:58 +00:00
|
|
|
// If bit is finished, and if all bytes in chunk have been posted then:
|
|
|
|
// - if this is the final chunk then note end of file.
|
|
|
|
// - otherwise, roll onto the next header or gap, depending on whether the next chunk has a gap.
|
2017-12-23 23:41:50 +00:00
|
|
|
distance_into_bit_++;
|
|
|
|
if(distance_into_bit_ == (bit ? 4 : 2)) {
|
|
|
|
distance_into_bit_ = 0;
|
|
|
|
distance_into_phase_++;
|
2017-12-29 14:56:58 +00:00
|
|
|
if(distance_into_phase_ == chunks_[chunk_pointer_].data.size() * 11) {
|
2017-12-23 23:41:50 +00:00
|
|
|
distance_into_phase_ = 0;
|
|
|
|
chunk_pointer_++;
|
2017-12-29 14:56:58 +00:00
|
|
|
if(chunk_pointer_ == chunks_.size()) {
|
|
|
|
phase_ = Phase::EndOfFile;
|
2017-12-24 00:20:04 +00:00
|
|
|
} else {
|
2017-12-29 14:56:58 +00:00
|
|
|
phase_ = chunks_[chunk_pointer_].has_gap ? Phase::Gap : Phase::Header;
|
2017-12-23 23:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 03:34:03 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:20:04 +00:00
|
|
|
// A '1' is encoded with twice the frequency of a '0'.
|
2017-12-23 23:41:50 +00:00
|
|
|
pulse.length.length = static_cast<unsigned int>(2 - bit);
|
|
|
|
pulse.type = (distance_into_bit_ & 1) ? Pulse::Type::High : Pulse::Type::Low;
|
2017-12-22 03:34:03 +00:00
|
|
|
|
|
|
|
return pulse;
|
2017-11-25 18:18:24 +00:00
|
|
|
}
|