2016-12-18 02:13:57 +00:00
|
|
|
//
|
|
|
|
// PCMSegment.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/12/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-12-18 02:13:57 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "PCMSegment.hpp"
|
|
|
|
|
2018-06-11 01:02:19 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-12-18 02:13:57 +00:00
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
|
|
|
PCMSegmentEventSource::PCMSegmentEventSource(const PCMSegment &segment) :
|
2017-03-26 18:34:47 +00:00
|
|
|
segment_(new PCMSegment(segment)) {
|
2016-12-19 03:53:24 +00:00
|
|
|
// add an extra bit of storage at the bottom if one is going to be needed;
|
|
|
|
// events returned are going to be in integral multiples of the length of a bit
|
|
|
|
// other than the very first and very last which will include a half bit length
|
2017-03-26 18:34:47 +00:00
|
|
|
if(segment_->length_of_a_bit.length&1) {
|
2016-12-30 19:23:26 +00:00
|
|
|
segment_->length_of_a_bit.length <<= 1;
|
|
|
|
segment_->length_of_a_bit.clock_rate <<= 1;
|
2016-12-18 02:13:57 +00:00
|
|
|
}
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
// load up the clock rate once only
|
2016-12-30 19:23:26 +00:00
|
|
|
next_event_.length.clock_rate = segment_->length_of_a_bit.clock_rate;
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
// set initial conditions
|
2016-12-18 02:13:57 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
PCMSegmentEventSource::PCMSegmentEventSource(const PCMSegmentEventSource &original) {
|
2016-12-30 19:23:26 +00:00
|
|
|
// share underlying data with the original
|
|
|
|
segment_ = original.segment_;
|
2016-12-30 22:55:46 +00:00
|
|
|
|
|
|
|
// load up the clock rate and set initial conditions
|
|
|
|
next_event_.length.clock_rate = segment_->length_of_a_bit.clock_rate;
|
2016-12-30 19:23:26 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void PCMSegmentEventSource::reset() {
|
2016-12-19 03:53:24 +00:00
|
|
|
// start with the first bit to be considered the zeroth, and assume that it'll be
|
|
|
|
// flux transitions for the foreseeable
|
2016-12-18 02:13:57 +00:00
|
|
|
bit_pointer_ = 0;
|
|
|
|
next_event_.type = Track::Event::FluxTransition;
|
|
|
|
}
|
|
|
|
|
2018-05-02 00:31:42 +00:00
|
|
|
PCMSegment &PCMSegment::operator +=(const PCMSegment &rhs) {
|
2018-07-01 16:05:41 +00:00
|
|
|
data.insert(data.end(), rhs.data.begin(), rhs.data.end());
|
2018-05-02 00:31:42 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Disk::Track::Event PCMSegmentEventSource::get_next_event() {
|
2016-12-19 03:53:24 +00:00
|
|
|
// track the initial bit pointer for potentially considering whether this was an
|
|
|
|
// initial index hole or a subsequent one later on
|
2018-07-01 19:38:42 +00:00
|
|
|
const std::size_t initial_bit_pointer = bit_pointer_;
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
// if starting from the beginning, pull half a bit backward, as if the initial bit
|
|
|
|
// is set, it should be in the centre of its window
|
2016-12-30 19:23:26 +00:00
|
|
|
next_event_.length.length = bit_pointer_ ? 0 : -(segment_->length_of_a_bit.length >> 1);
|
2016-12-18 02:13:57 +00:00
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
// search for the next bit that is set, if any
|
2018-07-01 16:05:41 +00:00
|
|
|
while(bit_pointer_ < segment_->data.size()) {
|
|
|
|
bool bit = segment_->data[bit_pointer_];
|
2016-12-19 03:53:24 +00:00
|
|
|
bit_pointer_++; // so this always points one beyond the most recent bit returned
|
2016-12-30 19:23:26 +00:00
|
|
|
next_event_.length.length += segment_->length_of_a_bit.length;
|
2016-12-18 02:13:57 +00:00
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
// if this bit is set, return the event
|
2016-12-18 02:13:57 +00:00
|
|
|
if(bit) return next_event_;
|
|
|
|
}
|
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
// if the end is reached without a bit being set, it'll be index holes from now on
|
2016-12-18 02:13:57 +00:00
|
|
|
next_event_.type = Track::Event::IndexHole;
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
// test whether this is the very first time that bits have been exhausted. If so then
|
|
|
|
// allow an extra half bit's length to run from the position of the potential final transition
|
|
|
|
// event to the end of the segment. Otherwise don't allow any extra time, as it's already
|
|
|
|
// been consumed
|
2018-07-01 16:05:41 +00:00
|
|
|
if(initial_bit_pointer <= segment_->data.size()) {
|
2016-12-30 19:23:26 +00:00
|
|
|
next_event_.length.length += (segment_->length_of_a_bit.length >> 1);
|
2016-12-19 03:53:24 +00:00
|
|
|
bit_pointer_++;
|
|
|
|
}
|
2016-12-18 02:47:13 +00:00
|
|
|
return next_event_;
|
2016-12-18 02:13:57 +00:00
|
|
|
}
|
2016-12-18 03:44:33 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Time PCMSegmentEventSource::get_length() {
|
2018-07-01 16:05:41 +00:00
|
|
|
return segment_->length_of_a_bit * static_cast<unsigned int>(segment_->data.size());
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start) {
|
2016-12-18 03:44:33 +00:00
|
|
|
// test for requested time being beyond the end
|
2018-07-01 19:38:42 +00:00
|
|
|
const Time length = get_length();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(time_from_start >= length) {
|
2016-12-18 03:44:33 +00:00
|
|
|
next_event_.type = Track::Event::IndexHole;
|
2018-07-01 16:05:41 +00:00
|
|
|
bit_pointer_ = segment_->data.size()+1;
|
2016-12-18 03:44:33 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not beyond the end then make an initial assumption that the next thing encountered will be a flux transition
|
|
|
|
next_event_.type = Track::Event::FluxTransition;
|
|
|
|
|
|
|
|
// test for requested time being before the first bit
|
2016-12-30 19:23:26 +00:00
|
|
|
Time half_bit_length = segment_->length_of_a_bit;
|
2016-12-18 03:44:33 +00:00
|
|
|
half_bit_length.length >>= 1;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(time_from_start < half_bit_length) {
|
2016-12-18 03:44:33 +00:00
|
|
|
bit_pointer_ = 0;
|
2018-07-01 19:38:42 +00:00
|
|
|
return Storage::Time(0);
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 15:19:24 +00:00
|
|
|
// adjust for time to get to bit zero and determine number of bits in;
|
|
|
|
// bit_pointer_ always records _the next bit_ that might trigger an event,
|
|
|
|
// so should be one beyond the one reached by a seek.
|
2018-07-01 19:38:42 +00:00
|
|
|
const Time relative_time = time_from_start - half_bit_length;
|
2018-04-25 23:54:39 +00:00
|
|
|
bit_pointer_ = 1 + (relative_time / segment_->length_of_a_bit).get<unsigned int>();
|
2016-12-18 03:44:33 +00:00
|
|
|
|
|
|
|
// map up to the correct amount of time
|
2017-10-21 23:49:04 +00:00
|
|
|
return half_bit_length + segment_->length_of_a_bit * static_cast<unsigned int>(bit_pointer_ - 1);
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
2018-07-01 16:05:41 +00:00
|
|
|
|
|
|
|
const PCMSegment &PCMSegmentEventSource::segment() const {
|
|
|
|
return *segment_;
|
|
|
|
}
|
2018-07-01 22:28:25 +00:00
|
|
|
|
|
|
|
PCMSegment &PCMSegmentEventSource::segment() {
|
|
|
|
return *segment_;
|
|
|
|
}
|