mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-11 08:30:55 +00:00
If asynchronous background processing is to occur on tracks then, given that they inherently have state, they'll need to be copyable, and ideally 'cheaply' (though it's not too great a priority). So started implementing appropriate copy constructors. Also introduced an extra level of indirection to PCMSegmentEventSource
so that it can copy itself without copying the underlying PCMSegment
, which is 95% of the heft of a track in all currently-implemented cases.
This commit is contained in:
parent
f88f3c65e9
commit
71dbd78cf2
@ -11,24 +11,33 @@
|
||||
using namespace Storage::Disk;
|
||||
|
||||
PCMSegmentEventSource::PCMSegmentEventSource(const PCMSegment &segment) :
|
||||
segment_(segment)
|
||||
segment_(new PCMSegment)
|
||||
{
|
||||
*segment_ = segment;
|
||||
|
||||
// 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
|
||||
if(segment_.length_of_a_bit.length&1)
|
||||
if(segment_->length_of_a_bit.length&1)
|
||||
{
|
||||
segment_.length_of_a_bit.length <<= 1;
|
||||
segment_.length_of_a_bit.clock_rate <<= 1;
|
||||
segment_->length_of_a_bit.length <<= 1;
|
||||
segment_->length_of_a_bit.clock_rate <<= 1;
|
||||
}
|
||||
|
||||
// load up the clock rate once only
|
||||
next_event_.length.clock_rate = segment_.length_of_a_bit.clock_rate;
|
||||
next_event_.length.clock_rate = segment_->length_of_a_bit.clock_rate;
|
||||
|
||||
// set initial conditions
|
||||
reset();
|
||||
}
|
||||
|
||||
PCMSegmentEventSource::PCMSegmentEventSource(const PCMSegmentEventSource &original)
|
||||
{
|
||||
// share underlying data with the original
|
||||
segment_ = original.segment_;
|
||||
reset();
|
||||
}
|
||||
|
||||
void PCMSegmentEventSource::reset()
|
||||
{
|
||||
// start with the first bit to be considered the zeroth, and assume that it'll be
|
||||
@ -45,15 +54,15 @@ Storage::Disk::Track::Event PCMSegmentEventSource::get_next_event()
|
||||
|
||||
// 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
|
||||
next_event_.length.length = bit_pointer_ ? 0 : -(segment_.length_of_a_bit.length >> 1);
|
||||
next_event_.length.length = bit_pointer_ ? 0 : -(segment_->length_of_a_bit.length >> 1);
|
||||
|
||||
// search for the next bit that is set, if any
|
||||
const uint8_t *segment_data = segment_.data.data();
|
||||
while(bit_pointer_ < segment_.number_of_bits)
|
||||
const uint8_t *segment_data = segment_->data.data();
|
||||
while(bit_pointer_ < segment_->number_of_bits)
|
||||
{
|
||||
int bit = segment_data[bit_pointer_ >> 3] & (0x80 >> (bit_pointer_&7));
|
||||
bit_pointer_++; // so this always points one beyond the most recent bit returned
|
||||
next_event_.length.length += segment_.length_of_a_bit.length;
|
||||
next_event_.length.length += segment_->length_of_a_bit.length;
|
||||
|
||||
// if this bit is set, return the event
|
||||
if(bit) return next_event_;
|
||||
@ -66,9 +75,9 @@ Storage::Disk::Track::Event PCMSegmentEventSource::get_next_event()
|
||||
// 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
|
||||
if(initial_bit_pointer <= segment_.number_of_bits)
|
||||
if(initial_bit_pointer <= segment_->number_of_bits)
|
||||
{
|
||||
next_event_.length.length += (segment_.length_of_a_bit.length >> 1);
|
||||
next_event_.length.length += (segment_->length_of_a_bit.length >> 1);
|
||||
bit_pointer_++;
|
||||
}
|
||||
return next_event_;
|
||||
@ -76,7 +85,7 @@ Storage::Disk::Track::Event PCMSegmentEventSource::get_next_event()
|
||||
|
||||
Storage::Time PCMSegmentEventSource::get_length()
|
||||
{
|
||||
return segment_.length_of_a_bit * segment_.number_of_bits;
|
||||
return segment_->length_of_a_bit * segment_->number_of_bits;
|
||||
}
|
||||
|
||||
Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start)
|
||||
@ -86,7 +95,7 @@ Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start)
|
||||
if(time_from_start >= length)
|
||||
{
|
||||
next_event_.type = Track::Event::IndexHole;
|
||||
bit_pointer_ = segment_.number_of_bits+1;
|
||||
bit_pointer_ = segment_->number_of_bits+1;
|
||||
return length;
|
||||
}
|
||||
|
||||
@ -94,7 +103,7 @@ Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start)
|
||||
next_event_.type = Track::Event::FluxTransition;
|
||||
|
||||
// test for requested time being before the first bit
|
||||
Time half_bit_length = segment_.length_of_a_bit;
|
||||
Time half_bit_length = segment_->length_of_a_bit;
|
||||
half_bit_length.length >>= 1;
|
||||
if(time_from_start < half_bit_length)
|
||||
{
|
||||
@ -107,8 +116,8 @@ Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start)
|
||||
// bit_pointer_ always records _the next bit_ that might trigger an event,
|
||||
// so should be one beyond the one reached by a seek.
|
||||
Time relative_time = time_from_start - half_bit_length;
|
||||
bit_pointer_ = 1 + (relative_time / segment_.length_of_a_bit).get_unsigned_int();
|
||||
bit_pointer_ = 1 + (relative_time / segment_->length_of_a_bit).get_unsigned_int();
|
||||
|
||||
// map up to the correct amount of time
|
||||
return half_bit_length + segment_.length_of_a_bit * (unsigned int)(bit_pointer_ - 1);
|
||||
return half_bit_length + segment_->length_of_a_bit * (unsigned int)(bit_pointer_ - 1);
|
||||
}
|
||||
|
@ -42,7 +42,13 @@ class PCMSegmentEventSource {
|
||||
Constructs a @c PCMSegmentEventSource that will derive events from @c segment.
|
||||
The event source is initially @c reset.
|
||||
*/
|
||||
PCMSegmentEventSource(const PCMSegment &segment);
|
||||
PCMSegmentEventSource(const PCMSegment &);
|
||||
|
||||
/*!
|
||||
Copy constructor; produces a segment event source with the same underlying segment
|
||||
but a unique pointer into it.
|
||||
*/
|
||||
PCMSegmentEventSource(const PCMSegmentEventSource &);
|
||||
|
||||
/*!
|
||||
@returns the next event that will occur in this event stream.
|
||||
@ -69,7 +75,7 @@ class PCMSegmentEventSource {
|
||||
Time get_length();
|
||||
|
||||
private:
|
||||
PCMSegment segment_;
|
||||
std::shared_ptr<PCMSegment> segment_;
|
||||
size_t bit_pointer_;
|
||||
Track::Event next_event_;
|
||||
};
|
||||
|
@ -47,6 +47,11 @@ PCMTrack::PCMTrack(const PCMSegment &segment) : PCMTrack()
|
||||
segment_event_sources_.emplace_back(length_adjusted_segment);
|
||||
}
|
||||
|
||||
PCMTrack::PCMTrack(const PCMTrack &original) : PCMTrack()
|
||||
{
|
||||
segment_event_sources_ = original.segment_event_sources_;
|
||||
}
|
||||
|
||||
Track::Event PCMTrack::get_next_event()
|
||||
{
|
||||
// ask the current segment for a new event
|
||||
|
@ -28,13 +28,18 @@ class PCMTrack: public Track {
|
||||
/*!
|
||||
Creates a @c PCMTrack consisting of multiple segments of data, permitting multiple clock rates.
|
||||
*/
|
||||
PCMTrack(const std::vector<PCMSegment> &segments);
|
||||
PCMTrack(const std::vector<PCMSegment> &);
|
||||
|
||||
/*!
|
||||
Creates a @c PCMTrack consisting of a single continuous run of data, implying a constant clock rate.
|
||||
The segment's @c length_of_a_bit will be ignored and therefore need not be filled in.
|
||||
*/
|
||||
PCMTrack(const PCMSegment &segment);
|
||||
PCMTrack(const PCMSegment &);
|
||||
|
||||
/*!
|
||||
Copy constructor; required for Tracks in order to support modifiable disks.
|
||||
*/
|
||||
PCMTrack(const PCMTrack &);
|
||||
|
||||
// as per @c Track
|
||||
Event get_next_event();
|
||||
|
Loading…
x
Reference in New Issue
Block a user