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/>.
27 #include "logicsegment.hpp"
29 #include <libsigrokcxx/libsigrokcxx.hpp>
31 using std::lock_guard;
32 using std::recursive_mutex;
36 using std::shared_ptr;
43 const int LogicSegment::MipMapScalePower = 4;
44 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
45 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
46 const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
48 LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate) :
49 Segment(samplerate, logic->unit_size()),
50 last_append_sample_(0)
52 lock_guard<recursive_mutex> lock(mutex_);
53 memset(mip_map_, 0, sizeof(mip_map_));
54 append_payload(logic);
57 LogicSegment::~LogicSegment()
59 lock_guard<recursive_mutex> lock(mutex_);
60 for (MipMapLevel &l : mip_map_)
64 uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
66 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
67 return *(uint64_t*)ptr;
72 value |= ((uint64_t)ptr[7]) << 56;
75 value |= ((uint64_t)ptr[6]) << 48;
78 value |= ((uint64_t)ptr[5]) << 40;
81 value |= ((uint64_t)ptr[4]) << 32;
84 value |= ((uint32_t)ptr[3]) << 24;
87 value |= ((uint32_t)ptr[2]) << 16;
102 void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
104 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
105 *(uint64_t*)ptr = value;
107 switch (unit_size_) {
109 ptr[7] = value >> 56;
112 ptr[6] = value >> 48;
115 ptr[5] = value >> 40;
118 ptr[4] = value >> 32;
121 ptr[3] = value >> 24;
124 ptr[2] = value >> 16;
138 void LogicSegment::append_payload(shared_ptr<Logic> logic)
140 assert(unit_size_ == logic->unit_size());
141 assert((logic->data_length() % unit_size_) == 0);
143 lock_guard<recursive_mutex> lock(mutex_);
145 append_samples(logic->data_pointer(),
146 logic->data_length() / unit_size_);
148 // Generate the first mip-map from the data
149 append_payload_to_mipmap();
152 const uint8_t* LogicSegment::get_samples(int64_t start_sample,
153 int64_t end_sample) const
155 assert(start_sample >= 0);
156 assert(start_sample <= (int64_t)sample_count_);
157 assert(end_sample >= 0);
158 assert(end_sample <= (int64_t)sample_count_);
159 assert(start_sample <= end_sample);
161 lock_guard<recursive_mutex> lock(mutex_);
163 return get_raw_samples(start_sample, (end_sample-start_sample));
166 SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start) const
168 return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
171 void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase) const
173 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
176 void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it) const
178 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
181 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
183 lock_guard<recursive_mutex> lock(mutex_);
185 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
186 MipMapDataUnit) * MipMapDataUnit;
188 if (new_data_length > m.data_length) {
189 m.data_length = new_data_length;
191 // Padding is added to allow for the uint64_t write word
192 m.data = realloc(m.data, new_data_length * unit_size_ +
197 void LogicSegment::append_payload_to_mipmap()
199 MipMapLevel &m0 = mip_map_[0];
200 uint64_t prev_length;
202 SegmentRawDataIterator* it;
203 uint64_t accumulator;
204 unsigned int diff_counter;
206 // Expand the data buffer to fit the new samples
207 prev_length = m0.length;
208 m0.length = sample_count_ / MipMapScaleFactor;
210 // Break off if there are no new samples to compute
211 if (m0.length == prev_length)
214 reallocate_mipmap_level(m0);
216 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
218 // Iterate through the samples to populate the first level mipmap
219 uint64_t start_sample = prev_length * MipMapScaleFactor;
220 uint64_t end_sample = m0.length * MipMapScaleFactor;
222 it = begin_raw_sample_iteration(start_sample);
223 for (uint64_t i = start_sample; i < end_sample;) {
224 // Accumulate transitions which have occurred in this sample
226 diff_counter = MipMapScaleFactor;
227 while (diff_counter-- > 0) {
228 const uint64_t sample = unpack_sample(it->value);
229 accumulator |= last_append_sample_ ^ sample;
230 last_append_sample_ = sample;
231 continue_raw_sample_iteration(it, 1);
235 pack_sample(dest_ptr, accumulator);
236 dest_ptr += unit_size_;
238 end_raw_sample_iteration(it);
240 // Compute higher level mipmaps
241 for (unsigned int level = 1; level < ScaleStepCount; level++) {
242 MipMapLevel &m = mip_map_[level];
243 const MipMapLevel &ml = mip_map_[level-1];
245 // Expand the data buffer to fit the new samples
246 prev_length = m.length;
247 m.length = ml.length / MipMapScaleFactor;
249 // Break off if there are no more samples to be computed
250 if (m.length == prev_length)
253 reallocate_mipmap_level(m);
255 // Subsample the lower level
256 const uint8_t* src_ptr = (uint8_t*)ml.data +
257 unit_size_ * prev_length * MipMapScaleFactor;
258 const uint8_t *const end_dest_ptr =
259 (uint8_t*)m.data + unit_size_ * m.length;
261 for (dest_ptr = (uint8_t*)m.data +
262 unit_size_ * prev_length;
263 dest_ptr < end_dest_ptr;
264 dest_ptr += unit_size_) {
266 diff_counter = MipMapScaleFactor;
267 while (diff_counter-- > 0) {
268 accumulator |= unpack_sample(src_ptr);
269 src_ptr += unit_size_;
272 pack_sample(dest_ptr, accumulator);
277 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
279 assert(index < sample_count_);
281 const uint8_t* data = get_raw_samples(index, 1);
282 uint64_t sample = unpack_sample(data);
288 void LogicSegment::get_subsampled_edges(
289 std::vector<EdgePair> &edges,
290 uint64_t start, uint64_t end,
291 float min_length, int sig_index)
293 uint64_t index = start;
298 assert(end <= get_sample_count());
299 assert(start <= end);
300 assert(min_length > 0);
301 assert(sig_index >= 0);
302 assert(sig_index < 64);
304 lock_guard<recursive_mutex> lock(mutex_);
306 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
307 const unsigned int min_level = max((int)floorf(logf(min_length) /
308 LogMipMapScaleFactor) - 1, 0);
309 const uint64_t sig_mask = 1ULL << sig_index;
311 // Store the initial state
312 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
313 edges.push_back(pair<int64_t, bool>(index++, last_sample));
315 while (index + block_length <= end) {
316 //----- Continue to search -----//
319 // We cannot fast-forward if there is no mip-map data at
320 // at the minimum level.
321 fast_forward = (mip_map_[level].data != nullptr);
323 if (min_length < MipMapScaleFactor) {
324 // Search individual samples up to the beginning of
325 // the next first level mip map block
326 const uint64_t final_index = min(end,
327 pow2_ceil(index, MipMapScalePower));
329 for (; index < final_index &&
330 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
333 (get_unpacked_sample(index) & sig_mask) != 0;
335 // If there was a change we cannot fast forward
336 if (sample != last_sample) {
337 fast_forward = false;
342 // If resolution is less than a mip map block,
343 // round up to the beginning of the mip-map block
344 // for this level of detail
345 const int min_level_scale_power =
346 (level + 1) * MipMapScalePower;
347 index = pow2_ceil(index, min_level_scale_power);
351 // We can fast forward only if there was no change
353 (get_unpacked_sample(index) & sig_mask) != 0;
354 if (last_sample != sample)
355 fast_forward = false;
360 // Fast forward: This involves zooming out to higher
361 // levels of the mip map searching for changes, then
362 // zooming in on them to find the point where the edge
365 // Slide right and zoom out at the beginnings of mip-map
366 // blocks until we encounter a change
368 const int level_scale_power =
369 (level + 1) * MipMapScalePower;
370 const uint64_t offset =
371 index >> level_scale_power;
373 // Check if we reached the last block at this
374 // level, or if there was a change in this block
375 if (offset >= mip_map_[level].length ||
376 (get_subsample(level, offset) &
380 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
381 // If we are now at the beginning of a
382 // higher level mip-map block ascend one
384 if (level + 1 >= ScaleStepCount ||
385 !mip_map_[level + 1].data)
390 // Slide right to the beginning of the
391 // next mip map block
392 index = pow2_ceil(index + 1,
397 // Zoom in, and slide right until we encounter a change,
398 // and repeat until we reach min_level
400 assert(mip_map_[level].data);
402 const int level_scale_power =
403 (level + 1) * MipMapScalePower;
404 const uint64_t offset =
405 index >> level_scale_power;
407 // Check if we reached the last block at this
408 // level, or if there was a change in this block
409 if (offset >= mip_map_[level].length ||
410 (get_subsample(level, offset) &
412 // Zoom in unless we reached the minimum
414 if (level == min_level)
419 // Slide right to the beginning of the
420 // next mip map block
421 index = pow2_ceil(index + 1,
426 // If individual samples within the limit of resolution,
427 // do a linear search for the next transition within the
429 if (min_length < MipMapScaleFactor) {
430 for (; index < end; index++) {
431 const bool sample = (get_unpacked_sample(index) &
433 if (sample != last_sample)
439 //----- Store the edge -----//
441 // Take the last sample of the quanization block
442 const int64_t final_index = index + block_length;
443 if (index + block_length > end)
446 // Store the final state
447 const bool final_sample =
448 (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
449 edges.push_back(pair<int64_t, bool>(index, final_sample));
452 last_sample = final_sample;
455 // Add the final state
456 const bool end_sample = get_unpacked_sample(end) & sig_mask;
457 if (last_sample != end_sample)
458 edges.push_back(pair<int64_t, bool>(end, end_sample));
459 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
462 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
465 assert(mip_map_[level].data);
466 return unpack_sample((uint8_t*)mip_map_[level].data +
467 unit_size_ * offset);
470 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
472 const uint64_t p = 1 << power;
473 return (x + p - 1) / p * p;