2016-07-10 22:36:52 +00:00
|
|
|
//
|
|
|
|
// PCMTrack.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "PCMTrack.hpp"
|
2016-07-29 09:19:01 +00:00
|
|
|
#include "../../NumberTheory/Factors.hpp"
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2016-08-27 21:15:09 +00:00
|
|
|
using namespace Storage::Disk;
|
2016-07-10 22:36:52 +00:00
|
|
|
|
|
|
|
PCMTrack::PCMTrack(std::vector<PCMSegment> segments)
|
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
segments_ = std::move(segments);
|
2016-07-10 22:36:52 +00:00
|
|
|
fix_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
PCMTrack::PCMTrack(PCMSegment segment)
|
|
|
|
{
|
2016-08-01 12:41:16 +00:00
|
|
|
segment.length_of_a_bit.length = 1;
|
|
|
|
segment.length_of_a_bit.clock_rate = 1;
|
2016-12-03 16:59:28 +00:00
|
|
|
segments_.push_back(std::move(segment));
|
2016-07-10 22:36:52 +00:00
|
|
|
fix_length();
|
|
|
|
}
|
|
|
|
|
2016-12-17 21:26:45 +00:00
|
|
|
Track::Event PCMTrack::get_next_event()
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
|
|
|
// find the next 1 in the input stream, keeping count of length as we go, and assuming it's going
|
|
|
|
// to be a flux transition
|
2016-12-03 16:59:28 +00:00
|
|
|
next_event_.type = Track::Event::FluxTransition;
|
|
|
|
next_event_.length.length = 0;
|
|
|
|
while(segment_pointer_ < segments_.size())
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
unsigned int clock_multiplier = track_clock_rate_ / segments_[segment_pointer_].length_of_a_bit.clock_rate;
|
|
|
|
unsigned int bit_length = clock_multiplier * segments_[segment_pointer_].length_of_a_bit.length;
|
2016-07-31 17:32:30 +00:00
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
const uint8_t *segment_data = &segments_[segment_pointer_].data[0];
|
|
|
|
while(bit_pointer_ < segments_[segment_pointer_].number_of_bits)
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
|
|
|
// for timing simplicity, bits are modelled as happening at the end of their window
|
|
|
|
// TODO: should I account for the converse bit ordering? Or can I assume MSB first?
|
2016-12-03 16:59:28 +00:00
|
|
|
int bit = segment_data[bit_pointer_ >> 3] & (0x80 >> (bit_pointer_&7));
|
|
|
|
bit_pointer_++;
|
|
|
|
next_event_.length.length += bit_length;
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
if(bit) return next_event_;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
2016-12-03 16:59:28 +00:00
|
|
|
bit_pointer_ = 0;
|
|
|
|
segment_pointer_++;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check whether we actually reached the index hole
|
2016-12-03 16:59:28 +00:00
|
|
|
if(segment_pointer_ == segments_.size())
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
segment_pointer_ = 0;
|
|
|
|
next_event_.type = Track::Event::IndexHole;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
return next_event_;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 21:26:45 +00:00
|
|
|
Storage::Time PCMTrack::seek_to(const Time &time_since_index_hole)
|
2016-08-03 10:59:45 +00:00
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
segment_pointer_ = 0;
|
2016-08-03 10:59:45 +00:00
|
|
|
|
|
|
|
// pick a common clock rate for counting time on this track and multiply up the time being sought appropriately
|
|
|
|
Time time_so_far;
|
2016-12-17 21:26:45 +00:00
|
|
|
Time target_time = time_since_index_hole;
|
|
|
|
time_so_far.clock_rate = NumberTheory::least_common_multiple(next_event_.length.clock_rate, target_time.clock_rate);
|
|
|
|
target_time.length *= time_so_far.clock_rate / target_time.clock_rate;
|
|
|
|
target_time.clock_rate = time_so_far.clock_rate;
|
2016-08-03 10:59:45 +00:00
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
while(segment_pointer_ < segments_.size())
|
2016-08-03 10:59:45 +00:00
|
|
|
{
|
|
|
|
// determine how long this segment is in terms of the master clock
|
2016-12-03 16:59:28 +00:00
|
|
|
unsigned int clock_multiplier = time_so_far.clock_rate / next_event_.length.clock_rate;
|
|
|
|
unsigned int bit_length = ((clock_multiplier / track_clock_rate_) / segments_[segment_pointer_].length_of_a_bit.clock_rate) * segments_[segment_pointer_].length_of_a_bit.length;
|
|
|
|
unsigned int time_in_this_segment = bit_length * segments_[segment_pointer_].number_of_bits;
|
2016-08-03 10:59:45 +00:00
|
|
|
|
|
|
|
// if this segment goes on longer than the time being sought, end here
|
2016-12-17 21:26:45 +00:00
|
|
|
unsigned int time_remaining = target_time.length - time_so_far.length;
|
2016-08-03 10:59:45 +00:00
|
|
|
if(time_in_this_segment >= time_remaining)
|
|
|
|
{
|
|
|
|
// get the amount of time actually to move into this segment
|
2016-08-04 02:33:00 +00:00
|
|
|
unsigned int time_found = time_remaining - (time_remaining % bit_length);
|
2016-08-03 10:59:45 +00:00
|
|
|
|
|
|
|
// resolve that into the stateful bit count
|
2016-12-03 16:59:28 +00:00
|
|
|
bit_pointer_ = 1 + (time_remaining / bit_length);
|
2016-08-03 10:59:45 +00:00
|
|
|
|
|
|
|
// update and return the time sought to
|
|
|
|
time_so_far.length += time_found;
|
|
|
|
return time_so_far;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, accumulate time and keep moving
|
|
|
|
time_so_far.length += time_in_this_segment;
|
2016-12-03 16:59:28 +00:00
|
|
|
segment_pointer_++;
|
2016-08-03 10:59:45 +00:00
|
|
|
}
|
2016-12-17 21:26:45 +00:00
|
|
|
return target_time;
|
2016-08-03 10:59:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 22:36:52 +00:00
|
|
|
void PCMTrack::fix_length()
|
|
|
|
{
|
|
|
|
// find the least common multiple of all segment clock rates
|
2016-12-03 16:59:28 +00:00
|
|
|
track_clock_rate_ = segments_[0].length_of_a_bit.clock_rate;
|
|
|
|
for(size_t c = 1; c < segments_.size(); c++)
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
track_clock_rate_ = NumberTheory::least_common_multiple(track_clock_rate_, segments_[c].length_of_a_bit.clock_rate);
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 22:52:50 +00:00
|
|
|
// thereby determine the total length, storing it to next_event as the track-total divisor
|
2016-12-03 16:59:28 +00:00
|
|
|
next_event_.length.clock_rate = 0;
|
|
|
|
for(size_t c = 0; c < segments_.size(); c++)
|
2016-07-10 22:36:52 +00:00
|
|
|
{
|
2016-12-03 16:59:28 +00:00
|
|
|
unsigned int multiplier = track_clock_rate_ / segments_[c].length_of_a_bit.clock_rate;
|
|
|
|
next_event_.length.clock_rate += segments_[c].length_of_a_bit.length * segments_[c].number_of_bits * multiplier;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
segment_pointer_ = bit_pointer_ = 0;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|