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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "logicsegment.hpp"
30 #include <libsigrok/libsigrok.hpp>
32 using std::lock_guard;
33 using std::recursive_mutex;
37 using std::shared_ptr;
44 const int LogicSegment::MipMapScalePower = 4;
45 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
46 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
47 const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
49 LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate,
50 const uint64_t expected_num_samples) :
51 Segment(samplerate, logic->unit_size()),
52 last_append_sample_(0)
54 set_capacity(expected_num_samples);
56 lock_guard<recursive_mutex> lock(mutex_);
57 memset(mip_map_, 0, sizeof(mip_map_));
58 append_payload(logic);
61 LogicSegment::~LogicSegment()
63 lock_guard<recursive_mutex> lock(mutex_);
64 for (MipMapLevel &l : mip_map_)
68 uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
70 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
71 return *(uint64_t*)ptr;
76 value |= ((uint64_t)ptr[7]) << 56;
79 value |= ((uint64_t)ptr[6]) << 48;
82 value |= ((uint64_t)ptr[5]) << 40;
85 value |= ((uint64_t)ptr[4]) << 32;
88 value |= ((uint32_t)ptr[3]) << 24;
91 value |= ((uint32_t)ptr[2]) << 16;
106 void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
108 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
109 *(uint64_t*)ptr = value;
113 ptr[7] = value >> 56;
116 ptr[6] = value >> 48;
119 ptr[5] = value >> 40;
122 ptr[4] = value >> 32;
125 ptr[3] = value >> 24;
128 ptr[2] = value >> 16;
142 void LogicSegment::append_payload(shared_ptr<Logic> logic)
144 assert(unit_size_ == logic->unit_size());
145 assert((logic->data_length() % unit_size_) == 0);
147 lock_guard<recursive_mutex> lock(mutex_);
149 append_data(logic->data_pointer(),
150 logic->data_length() / unit_size_);
152 // Generate the first mip-map from the data
153 append_payload_to_mipmap();
156 void LogicSegment::get_samples(uint8_t *const data,
157 int64_t start_sample, int64_t end_sample) const
160 assert(start_sample >= 0);
161 assert(start_sample <= (int64_t)sample_count_);
162 assert(end_sample >= 0);
163 assert(end_sample <= (int64_t)sample_count_);
164 assert(start_sample <= end_sample);
166 lock_guard<recursive_mutex> lock(mutex_);
168 const size_t size = (end_sample - start_sample) * unit_size_;
169 memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
172 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
174 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
175 MipMapDataUnit) * MipMapDataUnit;
176 if (new_data_length > m.data_length)
178 m.data_length = new_data_length;
180 // Padding is added to allow for the uint64_t write word
181 m.data = realloc(m.data, new_data_length * unit_size_ +
186 void LogicSegment::append_payload_to_mipmap()
188 MipMapLevel &m0 = mip_map_[0];
189 uint64_t prev_length;
190 const uint8_t *src_ptr;
192 uint64_t accumulator;
193 unsigned int diff_counter;
195 // Expand the data buffer to fit the new samples
196 prev_length = m0.length;
197 m0.length = sample_count_ / MipMapScaleFactor;
199 // Break off if there are no new samples to compute
200 if (m0.length == prev_length)
203 reallocate_mipmap_level(m0);
205 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
207 // Iterate through the samples to populate the first level mipmap
208 const uint8_t *const end_src_ptr = (uint8_t*)data_.data() +
209 m0.length * unit_size_ * MipMapScaleFactor;
210 for (src_ptr = (uint8_t*)data_.data() +
211 prev_length * unit_size_ * MipMapScaleFactor;
212 src_ptr < end_src_ptr;)
214 // Accumulate transitions which have occurred in this sample
216 diff_counter = MipMapScaleFactor;
217 while (diff_counter-- > 0)
219 const uint64_t sample = unpack_sample(src_ptr);
220 accumulator |= last_append_sample_ ^ sample;
221 last_append_sample_ = sample;
222 src_ptr += unit_size_;
225 pack_sample(dest_ptr, accumulator);
226 dest_ptr += unit_size_;
229 // Compute higher level mipmaps
230 for (unsigned int level = 1; level < ScaleStepCount; level++)
232 MipMapLevel &m = mip_map_[level];
233 const MipMapLevel &ml = mip_map_[level-1];
235 // Expand the data buffer to fit the new samples
236 prev_length = m.length;
237 m.length = ml.length / MipMapScaleFactor;
239 // Break off if there are no more samples to computed
240 if (m.length == prev_length)
243 reallocate_mipmap_level(m);
245 // Subsample the level lower level
246 src_ptr = (uint8_t*)ml.data +
247 unit_size_ * prev_length * MipMapScaleFactor;
248 const uint8_t *const end_dest_ptr =
249 (uint8_t*)m.data + unit_size_ * m.length;
250 for (dest_ptr = (uint8_t*)m.data +
251 unit_size_ * prev_length;
252 dest_ptr < end_dest_ptr;
253 dest_ptr += unit_size_)
256 diff_counter = MipMapScaleFactor;
257 while (diff_counter-- > 0)
259 accumulator |= unpack_sample(src_ptr);
260 src_ptr += unit_size_;
263 pack_sample(dest_ptr, accumulator);
268 uint64_t LogicSegment::get_sample(uint64_t index) const
270 assert(index < sample_count_);
272 return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
275 void LogicSegment::get_subsampled_edges(
276 std::vector<EdgePair> &edges,
277 uint64_t start, uint64_t end,
278 float min_length, int sig_index)
280 uint64_t index = start;
285 assert(end <= get_sample_count());
286 assert(start <= end);
287 assert(min_length > 0);
288 assert(sig_index >= 0);
289 assert(sig_index < 64);
291 lock_guard<recursive_mutex> lock(mutex_);
293 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
294 const unsigned int min_level = max((int)floorf(logf(min_length) /
295 LogMipMapScaleFactor) - 1, 0);
296 const uint64_t sig_mask = 1ULL << sig_index;
298 // Store the initial state
299 last_sample = (get_sample(start) & sig_mask) != 0;
300 edges.push_back(pair<int64_t, bool>(index++, last_sample));
302 while (index + block_length <= end)
304 //----- Continue to search -----//
307 // We cannot fast-forward if there is no mip-map data at
308 // at the minimum level.
309 fast_forward = (mip_map_[level].data != NULL);
311 if (min_length < MipMapScaleFactor)
313 // Search individual samples up to the beginning of
314 // the next first level mip map block
315 const uint64_t final_index = min(end,
316 pow2_ceil(index, MipMapScalePower));
318 for (; index < final_index &&
319 (index & ~(~0 << MipMapScalePower)) != 0;
323 (get_sample(index) & sig_mask) != 0;
325 // If there was a change we cannot fast forward
326 if (sample != last_sample) {
327 fast_forward = false;
334 // If resolution is less than a mip map block,
335 // round up to the beginning of the mip-map block
336 // for this level of detail
337 const int min_level_scale_power =
338 (level + 1) * MipMapScalePower;
339 index = pow2_ceil(index, min_level_scale_power);
343 // We can fast forward only if there was no change
345 (get_sample(index) & sig_mask) != 0;
346 if (last_sample != sample)
347 fast_forward = false;
352 // Fast forward: This involves zooming out to higher
353 // levels of the mip map searching for changes, then
354 // zooming in on them to find the point where the edge
357 // Slide right and zoom out at the beginnings of mip-map
358 // blocks until we encounter a change
360 const int level_scale_power =
361 (level + 1) * MipMapScalePower;
362 const uint64_t offset =
363 index >> level_scale_power;
365 // Check if we reached the last block at this
366 // level, or if there was a change in this block
367 if (offset >= mip_map_[level].length ||
368 (get_subsample(level, offset) &
372 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
373 // If we are now at the beginning of a
374 // higher level mip-map block ascend one
376 if (level + 1 >= ScaleStepCount ||
377 !mip_map_[level + 1].data)
382 // Slide right to the beginning of the
383 // next mip map block
384 index = pow2_ceil(index + 1,
389 // Zoom in, and slide right until we encounter a change,
390 // and repeat until we reach min_level
392 assert(mip_map_[level].data);
394 const int level_scale_power =
395 (level + 1) * MipMapScalePower;
396 const uint64_t offset =
397 index >> level_scale_power;
399 // Check if we reached the last block at this
400 // level, or if there was a change in this block
401 if (offset >= mip_map_[level].length ||
402 (get_subsample(level, offset) &
404 // Zoom in unless we reached the minimum
406 if (level == min_level)
411 // Slide right to the beginning of the
412 // next mip map block
413 index = pow2_ceil(index + 1,
418 // If individual samples within the limit of resolution,
419 // do a linear search for the next transition within the
421 if (min_length < MipMapScaleFactor) {
422 for (; index < end; index++) {
423 const bool sample = (get_sample(index) &
425 if (sample != last_sample)
431 //----- Store the edge -----//
433 // Take the last sample of the quanization block
434 const int64_t final_index = index + block_length;
435 if (index + block_length > end)
438 // Store the final state
439 const bool final_sample =
440 (get_sample(final_index - 1) & sig_mask) != 0;
441 edges.push_back(pair<int64_t, bool>(index, final_sample));
444 last_sample = final_sample;
447 // Add the final state
448 const bool end_sample = get_sample(end) & sig_mask;
449 if (last_sample != end_sample)
450 edges.push_back(pair<int64_t, bool>(end, end_sample));
451 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
454 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
457 assert(mip_map_[level].data);
458 return unpack_sample((uint8_t*)mip_map_[level].data +
459 unit_size_ * offset);
462 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
464 const uint64_t p = 1 << power;
465 return (x + p - 1) / p * p;