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
29 #include "logicsnapshot.h"
31 #include <libsigrok/libsigrok.hpp>
33 using std::lock_guard;
34 using std::recursive_mutex;
38 using std::shared_ptr;
45 const int LogicSnapshot::MipMapScalePower = 4;
46 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
47 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
48 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
50 LogicSnapshot::LogicSnapshot(shared_ptr<Logic> logic,
51 const uint64_t expected_num_samples) :
52 Snapshot(logic->unit_size()),
53 _last_append_sample(0)
55 set_capacity(expected_num_samples);
57 lock_guard<recursive_mutex> lock(_mutex);
58 memset(_mip_map, 0, sizeof(_mip_map));
59 append_payload(logic);
62 LogicSnapshot::~LogicSnapshot()
64 lock_guard<recursive_mutex> lock(_mutex);
65 for (MipMapLevel &l : _mip_map)
69 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
71 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
72 return *(uint64_t*)ptr;
77 value |= ((uint64_t)ptr[7]) << 56;
80 value |= ((uint64_t)ptr[6]) << 48;
83 value |= ((uint64_t)ptr[5]) << 40;
86 value |= ((uint64_t)ptr[4]) << 32;
89 value |= ((uint32_t)ptr[3]) << 24;
92 value |= ((uint32_t)ptr[2]) << 16;
107 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
109 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
110 *(uint64_t*)ptr = value;
114 ptr[7] = value >> 56;
117 ptr[6] = value >> 48;
120 ptr[5] = value >> 40;
123 ptr[4] = value >> 32;
126 ptr[3] = value >> 24;
129 ptr[2] = value >> 16;
143 void LogicSnapshot::append_payload(shared_ptr<Logic> logic)
145 assert(_unit_size == logic->unit_size());
146 assert((logic->data_length() % _unit_size) == 0);
148 lock_guard<recursive_mutex> lock(_mutex);
150 append_data(logic->data_pointer(),
151 logic->data_length() / _unit_size);
153 // Generate the first mip-map from the data
154 append_payload_to_mipmap();
157 void LogicSnapshot::get_samples(uint8_t *const data,
158 int64_t start_sample, int64_t end_sample) const
161 assert(start_sample >= 0);
162 assert(start_sample <= (int64_t)_sample_count);
163 assert(end_sample >= 0);
164 assert(end_sample <= (int64_t)_sample_count);
165 assert(start_sample <= end_sample);
167 lock_guard<recursive_mutex> lock(_mutex);
169 const size_t size = (end_sample - start_sample) * _unit_size;
170 memcpy(data, (const uint8_t*)_data.data() + start_sample * _unit_size, size);
173 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
175 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
176 MipMapDataUnit) * MipMapDataUnit;
177 if (new_data_length > m.data_length)
179 m.data_length = new_data_length;
181 // Padding is added to allow for the uint64_t write word
182 m.data = realloc(m.data, new_data_length * _unit_size +
187 void LogicSnapshot::append_payload_to_mipmap()
189 MipMapLevel &m0 = _mip_map[0];
190 uint64_t prev_length;
191 const uint8_t *src_ptr;
193 uint64_t accumulator;
194 unsigned int diff_counter;
196 // Expand the data buffer to fit the new samples
197 prev_length = m0.length;
198 m0.length = _sample_count / MipMapScaleFactor;
200 // Break off if there are no new samples to compute
201 if (m0.length == prev_length)
204 reallocate_mipmap_level(m0);
206 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
208 // Iterate through the samples to populate the first level mipmap
209 const uint8_t *const end_src_ptr = (uint8_t*)_data.data() +
210 m0.length * _unit_size * MipMapScaleFactor;
211 for (src_ptr = (uint8_t*)_data.data() +
212 prev_length * _unit_size * MipMapScaleFactor;
213 src_ptr < end_src_ptr;)
215 // Accumulate transitions which have occurred in this sample
217 diff_counter = MipMapScaleFactor;
218 while (diff_counter-- > 0)
220 const uint64_t sample = unpack_sample(src_ptr);
221 accumulator |= _last_append_sample ^ sample;
222 _last_append_sample = sample;
223 src_ptr += _unit_size;
226 pack_sample(dest_ptr, accumulator);
227 dest_ptr += _unit_size;
230 // Compute higher level mipmaps
231 for (unsigned int level = 1; level < ScaleStepCount; level++)
233 MipMapLevel &m = _mip_map[level];
234 const MipMapLevel &ml = _mip_map[level-1];
236 // Expand the data buffer to fit the new samples
237 prev_length = m.length;
238 m.length = ml.length / MipMapScaleFactor;
240 // Break off if there are no more samples to computed
241 if (m.length == prev_length)
244 reallocate_mipmap_level(m);
246 // Subsample the level lower level
247 src_ptr = (uint8_t*)ml.data +
248 _unit_size * prev_length * MipMapScaleFactor;
249 const uint8_t *const end_dest_ptr =
250 (uint8_t*)m.data + _unit_size * m.length;
251 for (dest_ptr = (uint8_t*)m.data +
252 _unit_size * prev_length;
253 dest_ptr < end_dest_ptr;
254 dest_ptr += _unit_size)
257 diff_counter = MipMapScaleFactor;
258 while (diff_counter-- > 0)
260 accumulator |= unpack_sample(src_ptr);
261 src_ptr += _unit_size;
264 pack_sample(dest_ptr, accumulator);
269 uint64_t LogicSnapshot::get_sample(uint64_t index) const
271 assert(index < _sample_count);
273 return unpack_sample((uint8_t*)_data.data() + index * _unit_size);
276 void LogicSnapshot::get_subsampled_edges(
277 std::vector<EdgePair> &edges,
278 uint64_t start, uint64_t end,
279 float min_length, int sig_index)
281 uint64_t index = start;
286 assert(end <= get_sample_count());
287 assert(start <= end);
288 assert(min_length > 0);
289 assert(sig_index >= 0);
290 assert(sig_index < 64);
292 lock_guard<recursive_mutex> lock(_mutex);
294 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
295 const unsigned int min_level = max((int)floorf(logf(min_length) /
296 LogMipMapScaleFactor) - 1, 0);
297 const uint64_t sig_mask = 1ULL << sig_index;
299 // Store the initial state
300 last_sample = (get_sample(start) & sig_mask) != 0;
301 edges.push_back(pair<int64_t, bool>(index++, last_sample));
303 while (index + block_length <= end)
305 //----- Continue to search -----//
308 // We cannot fast-forward if there is no mip-map data at
309 // at the minimum level.
310 fast_forward = (_mip_map[level].data != NULL);
312 if (min_length < MipMapScaleFactor)
314 // Search individual samples up to the beginning of
315 // the next first level mip map block
316 const uint64_t final_index = min(end,
317 pow2_ceil(index, MipMapScalePower));
319 for (; index < final_index &&
320 (index & ~(~0 << MipMapScalePower)) != 0;
324 (get_sample(index) & sig_mask) != 0;
326 // If there was a change we cannot fast forward
327 if (sample != last_sample) {
328 fast_forward = false;
335 // If resolution is less than a mip map block,
336 // round up to the beginning of the mip-map block
337 // for this level of detail
338 const int min_level_scale_power =
339 (level + 1) * MipMapScalePower;
340 index = pow2_ceil(index, min_level_scale_power);
344 // We can fast forward only if there was no change
346 (get_sample(index) & sig_mask) != 0;
347 if (last_sample != sample)
348 fast_forward = false;
353 // Fast forward: This involves zooming out to higher
354 // levels of the mip map searching for changes, then
355 // zooming in on them to find the point where the edge
358 // Slide right and zoom out at the beginnings of mip-map
359 // blocks until we encounter a change
361 const int level_scale_power =
362 (level + 1) * MipMapScalePower;
363 const uint64_t offset =
364 index >> level_scale_power;
366 // Check if we reached the last block at this
367 // level, or if there was a change in this block
368 if (offset >= _mip_map[level].length ||
369 (get_subsample(level, offset) &
373 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
374 // If we are now at the beginning of a
375 // higher level mip-map block ascend one
377 if (level + 1 >= ScaleStepCount ||
378 !_mip_map[level + 1].data)
383 // Slide right to the beginning of the
384 // next mip map block
385 index = pow2_ceil(index + 1,
390 // Zoom in, and slide right until we encounter a change,
391 // and repeat until we reach min_level
393 assert(_mip_map[level].data);
395 const int level_scale_power =
396 (level + 1) * MipMapScalePower;
397 const uint64_t offset =
398 index >> level_scale_power;
400 // Check if we reached the last block at this
401 // level, or if there was a change in this block
402 if (offset >= _mip_map[level].length ||
403 (get_subsample(level, offset) &
405 // Zoom in unless we reached the minimum
407 if (level == min_level)
412 // Slide right to the beginning of the
413 // next mip map block
414 index = pow2_ceil(index + 1,
419 // If individual samples within the limit of resolution,
420 // do a linear search for the next transition within the
422 if (min_length < MipMapScaleFactor) {
423 for (; index < end; index++) {
424 const bool sample = (get_sample(index) &
426 if (sample != last_sample)
432 //----- Store the edge -----//
434 // Take the last sample of the quanization block
435 const int64_t final_index = index + block_length;
436 if (index + block_length > end)
439 // Store the final state
440 const bool final_sample =
441 (get_sample(final_index - 1) & sig_mask) != 0;
442 edges.push_back(pair<int64_t, bool>(index, final_sample));
445 last_sample = final_sample;
448 // Add the final state
449 const bool end_sample = get_sample(end) & sig_mask;
450 if (last_sample != end_sample)
451 edges.push_back(pair<int64_t, bool>(end, end_sample));
452 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
455 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
458 assert(_mip_map[level].data);
459 return unpack_sample((uint8_t*)_mip_map[level].data +
460 _unit_size * offset);
463 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
465 const uint64_t p = 1 << power;
466 return (x + p - 1) / p * p;