2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include "logicsegment.hpp"
30 #include <libsigrokcxx/libsigrokcxx.hpp>
32 using std::lock_guard;
33 using std::recursive_mutex;
37 using std::shared_ptr;
45 const int LogicSegment::MipMapScalePower = 4;
46 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
47 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
48 const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
50 LogicSegment::LogicSegment(pv::data::Logic& owner, unsigned int unit_size,
51 uint64_t samplerate) :
52 Segment(samplerate, unit_size),
54 last_append_sample_(0)
56 memset(mip_map_, 0, sizeof(mip_map_));
59 LogicSegment::~LogicSegment()
61 lock_guard<recursive_mutex> lock(mutex_);
62 for (MipMapLevel &l : mip_map_)
66 uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
68 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
69 return *(uint64_t*)ptr;
74 value |= ((uint64_t)ptr[7]) << 56;
77 value |= ((uint64_t)ptr[6]) << 48;
80 value |= ((uint64_t)ptr[5]) << 40;
83 value |= ((uint64_t)ptr[4]) << 32;
86 value |= ((uint32_t)ptr[3]) << 24;
89 value |= ((uint32_t)ptr[2]) << 16;
104 void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
106 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
107 *(uint64_t*)ptr = value;
109 switch (unit_size_) {
111 ptr[7] = value >> 56;
114 ptr[6] = value >> 48;
117 ptr[5] = value >> 40;
120 ptr[4] = value >> 32;
123 ptr[3] = value >> 24;
126 ptr[2] = value >> 16;
140 void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
142 assert(unit_size_ == logic->unit_size());
143 assert((logic->data_length() % unit_size_) == 0);
145 append_payload(logic->data_pointer(), logic->data_length());
148 void LogicSegment::append_payload(void *data, uint64_t data_size)
150 assert((data_size % unit_size_) == 0);
152 lock_guard<recursive_mutex> lock(mutex_);
154 uint64_t prev_sample_count = sample_count_;
155 uint64_t sample_count = data_size / unit_size_;
157 append_samples(data, sample_count);
159 // Generate the first mip-map from the data
160 append_payload_to_mipmap();
162 if (sample_count > 1)
163 owner_.notify_samples_added(this, prev_sample_count + 1,
164 prev_sample_count + 1 + sample_count);
166 owner_.notify_samples_added(this, prev_sample_count + 1,
167 prev_sample_count + 1);
170 const uint8_t* LogicSegment::get_samples(int64_t start_sample,
171 int64_t end_sample) const
173 assert(start_sample >= 0);
174 assert(start_sample <= (int64_t)sample_count_);
175 assert(end_sample >= 0);
176 assert(end_sample <= (int64_t)sample_count_);
177 assert(start_sample <= end_sample);
179 lock_guard<recursive_mutex> lock(mutex_);
181 return get_raw_samples(start_sample, (end_sample-start_sample));
184 SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start)
186 return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
189 void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase)
191 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
194 void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it)
196 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
199 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
201 lock_guard<recursive_mutex> lock(mutex_);
203 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
204 MipMapDataUnit) * MipMapDataUnit;
206 if (new_data_length > m.data_length) {
207 m.data_length = new_data_length;
209 // Padding is added to allow for the uint64_t write word
210 m.data = realloc(m.data, new_data_length * unit_size_ +
215 void LogicSegment::append_payload_to_mipmap()
217 MipMapLevel &m0 = mip_map_[0];
218 uint64_t prev_length;
220 SegmentRawDataIterator* it;
221 uint64_t accumulator;
222 unsigned int diff_counter;
224 // Expand the data buffer to fit the new samples
225 prev_length = m0.length;
226 m0.length = sample_count_ / MipMapScaleFactor;
228 // Break off if there are no new samples to compute
229 if (m0.length == prev_length)
232 reallocate_mipmap_level(m0);
234 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
236 // Iterate through the samples to populate the first level mipmap
237 uint64_t start_sample = prev_length * MipMapScaleFactor;
238 uint64_t end_sample = m0.length * MipMapScaleFactor;
240 it = begin_raw_sample_iteration(start_sample);
241 for (uint64_t i = start_sample; i < end_sample;) {
242 // Accumulate transitions which have occurred in this sample
244 diff_counter = MipMapScaleFactor;
245 while (diff_counter-- > 0) {
246 const uint64_t sample = unpack_sample(it->value);
247 accumulator |= last_append_sample_ ^ sample;
248 last_append_sample_ = sample;
249 continue_raw_sample_iteration(it, 1);
253 pack_sample(dest_ptr, accumulator);
254 dest_ptr += unit_size_;
256 end_raw_sample_iteration(it);
258 // Compute higher level mipmaps
259 for (unsigned int level = 1; level < ScaleStepCount; level++) {
260 MipMapLevel &m = mip_map_[level];
261 const MipMapLevel &ml = mip_map_[level-1];
263 // Expand the data buffer to fit the new samples
264 prev_length = m.length;
265 m.length = ml.length / MipMapScaleFactor;
267 // Break off if there are no more samples to be computed
268 if (m.length == prev_length)
271 reallocate_mipmap_level(m);
273 // Subsample the lower level
274 const uint8_t* src_ptr = (uint8_t*)ml.data +
275 unit_size_ * prev_length * MipMapScaleFactor;
276 const uint8_t *const end_dest_ptr =
277 (uint8_t*)m.data + unit_size_ * m.length;
279 for (dest_ptr = (uint8_t*)m.data +
280 unit_size_ * prev_length;
281 dest_ptr < end_dest_ptr;
282 dest_ptr += unit_size_) {
284 diff_counter = MipMapScaleFactor;
285 while (diff_counter-- > 0) {
286 accumulator |= unpack_sample(src_ptr);
287 src_ptr += unit_size_;
290 pack_sample(dest_ptr, accumulator);
295 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
297 assert(index < sample_count_);
299 const uint8_t* data = get_raw_samples(index, 1);
300 uint64_t sample = unpack_sample(data);
306 void LogicSegment::get_subsampled_edges(
307 vector<EdgePair> &edges,
308 uint64_t start, uint64_t end,
309 float min_length, int sig_index)
311 uint64_t index = start;
316 assert(end <= get_sample_count());
317 assert(start <= end);
318 assert(min_length > 0);
319 assert(sig_index >= 0);
320 assert(sig_index < 64);
322 lock_guard<recursive_mutex> lock(mutex_);
324 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
325 const unsigned int min_level = max((int)floorf(logf(min_length) /
326 LogMipMapScaleFactor) - 1, 0);
327 const uint64_t sig_mask = 1ULL << sig_index;
329 // Store the initial state
330 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
331 edges.emplace_back(index++, last_sample);
333 while (index + block_length <= end) {
334 //----- Continue to search -----//
337 // We cannot fast-forward if there is no mip-map data at
338 // at the minimum level.
339 fast_forward = (mip_map_[level].data != nullptr);
341 if (min_length < MipMapScaleFactor) {
342 // Search individual samples up to the beginning of
343 // the next first level mip map block
344 const uint64_t final_index = min(end,
345 pow2_ceil(index, MipMapScalePower));
347 for (; index < final_index &&
348 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
351 (get_unpacked_sample(index) & sig_mask) != 0;
353 // If there was a change we cannot fast forward
354 if (sample != last_sample) {
355 fast_forward = false;
360 // If resolution is less than a mip map block,
361 // round up to the beginning of the mip-map block
362 // for this level of detail
363 const int min_level_scale_power =
364 (level + 1) * MipMapScalePower;
365 index = pow2_ceil(index, min_level_scale_power);
369 // We can fast forward only if there was no change
371 (get_unpacked_sample(index) & sig_mask) != 0;
372 if (last_sample != sample)
373 fast_forward = false;
378 // Fast forward: This involves zooming out to higher
379 // levels of the mip map searching for changes, then
380 // zooming in on them to find the point where the edge
383 // Slide right and zoom out at the beginnings of mip-map
384 // blocks until we encounter a change
386 const int level_scale_power =
387 (level + 1) * MipMapScalePower;
388 const uint64_t offset =
389 index >> level_scale_power;
391 // Check if we reached the last block at this
392 // level, or if there was a change in this block
393 if (offset >= mip_map_[level].length ||
394 (get_subsample(level, offset) &
398 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
399 // If we are now at the beginning of a
400 // higher level mip-map block ascend one
402 if (level + 1 >= ScaleStepCount ||
403 !mip_map_[level + 1].data)
408 // Slide right to the beginning of the
409 // next mip map block
410 index = pow2_ceil(index + 1,
415 // Zoom in, and slide right until we encounter a change,
416 // and repeat until we reach min_level
418 assert(mip_map_[level].data);
420 const int level_scale_power =
421 (level + 1) * MipMapScalePower;
422 const uint64_t offset =
423 index >> level_scale_power;
425 // Check if we reached the last block at this
426 // level, or if there was a change in this block
427 if (offset >= mip_map_[level].length ||
428 (get_subsample(level, offset) &
430 // Zoom in unless we reached the minimum
432 if (level == min_level)
437 // Slide right to the beginning of the
438 // next mip map block
439 index = pow2_ceil(index + 1,
444 // If individual samples within the limit of resolution,
445 // do a linear search for the next transition within the
447 if (min_length < MipMapScaleFactor) {
448 for (; index < end; index++) {
449 const bool sample = (get_unpacked_sample(index) &
451 if (sample != last_sample)
457 //----- Store the edge -----//
459 // Take the last sample of the quanization block
460 const int64_t final_index = index + block_length;
461 if (index + block_length > end)
464 // Store the final state
465 const bool final_sample =
466 (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
467 edges.emplace_back(index, final_sample);
470 last_sample = final_sample;
473 // Add the final state
474 const bool end_sample = get_unpacked_sample(end) & sig_mask;
475 if (last_sample != end_sample)
476 edges.emplace_back(end, end_sample);
477 edges.emplace_back(end + 1, end_sample);
480 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
483 assert(mip_map_[level].data);
484 return unpack_sample((uint8_t*)mip_map_[level].data +
485 unit_size_ * offset);
488 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
490 const uint64_t p = 1 << power;
491 return (x + p - 1) / p * p;