]> sigrok.org Git - pulseview.git/blame - pv/data/logicsnapshot.cpp
Use libsigrok C++ bindings (patch version 7).
[pulseview.git] / pv / data / logicsnapshot.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
cef18fc6 21#include <extdef.h>
28a4c9c5 22
f556bc6a 23#include <assert.h>
4ceab49a 24#include <string.h>
b36d8550 25#include <stdlib.h>
4ceab49a
JH
26#include <math.h>
27
9df8453f 28#include "config.h"
1b1ec774 29#include "logicsnapshot.h"
f556bc6a 30
e8d00928
ML
31#include <libsigrok/libsigrok.hpp>
32
3b68d03d
JH
33using std::lock_guard;
34using std::recursive_mutex;
819f4c25
JH
35using std::max;
36using std::min;
37using std::pair;
e8d00928
ML
38using std::shared_ptr;
39
40using sigrok::Logic;
28a4c9c5 41
51e77110 42namespace pv {
1b1ec774 43namespace data {
51e77110 44
1b1ec774
JH
45const int LogicSnapshot::MipMapScalePower = 4;
46const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
47const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
48const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
4ceab49a 49
e8d00928 50LogicSnapshot::LogicSnapshot(shared_ptr<Logic> logic,
27d7c96b 51 const uint64_t expected_num_samples) :
e8d00928 52 Snapshot(logic->unit_size()),
4ceab49a 53 _last_append_sample(0)
f556bc6a 54{
27d7c96b
DK
55 set_capacity(expected_num_samples);
56
7d29656f 57 lock_guard<recursive_mutex> lock(_mutex);
4ceab49a 58 memset(_mip_map, 0, sizeof(_mip_map));
f556bc6a
JH
59 append_payload(logic);
60}
61
1b1ec774 62LogicSnapshot::~LogicSnapshot()
4ceab49a 63{
7d29656f 64 lock_guard<recursive_mutex> lock(_mutex);
d9aecf1f 65 for (MipMapLevel &l : _mip_map)
4ceab49a
JH
66 free(l.data);
67}
68
8cb71705
MC
69uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
70{
9df8453f
MC
71#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
72 return *(uint64_t*)ptr;
73#else
8cb71705
MC
74 uint64_t value = 0;
75 switch(_unit_size) {
76 default:
77 value |= ((uint64_t)ptr[7]) << 56;
78 /* FALLTHRU */
79 case 7:
80 value |= ((uint64_t)ptr[6]) << 48;
81 /* FALLTHRU */
82 case 6:
83 value |= ((uint64_t)ptr[5]) << 40;
84 /* FALLTHRU */
85 case 5:
86 value |= ((uint64_t)ptr[4]) << 32;
87 /* FALLTHRU */
88 case 4:
89 value |= ((uint32_t)ptr[3]) << 24;
90 /* FALLTHRU */
91 case 3:
92 value |= ((uint32_t)ptr[2]) << 16;
93 /* FALLTHRU */
94 case 2:
95 value |= ptr[1] << 8;
96 /* FALLTHRU */
97 case 1:
98 value |= ptr[0];
99 /* FALLTHRU */
100 case 0:
101 break;
102 }
103 return value;
9df8453f 104#endif
8cb71705
MC
105}
106
107void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
108{
9df8453f
MC
109#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
110 *(uint64_t*)ptr = value;
111#else
8cb71705
MC
112 switch(_unit_size) {
113 default:
114 ptr[7] = value >> 56;
115 /* FALLTHRU */
116 case 7:
117 ptr[6] = value >> 48;
118 /* FALLTHRU */
119 case 6:
120 ptr[5] = value >> 40;
121 /* FALLTHRU */
122 case 5:
123 ptr[4] = value >> 32;
124 /* FALLTHRU */
125 case 4:
126 ptr[3] = value >> 24;
127 /* FALLTHRU */
128 case 3:
129 ptr[2] = value >> 16;
130 /* FALLTHRU */
131 case 2:
132 ptr[1] = value >> 8;
133 /* FALLTHRU */
134 case 1:
135 ptr[0] = value;
136 /* FALLTHRU */
137 case 0:
138 break;
139 }
9df8453f 140#endif
8cb71705
MC
141}
142
e8d00928 143void LogicSnapshot::append_payload(shared_ptr<Logic> logic)
28a4c9c5 144{
e8d00928
ML
145 assert(_unit_size == logic->unit_size());
146 assert((logic->data_length() % _unit_size) == 0);
f556bc6a 147
7d29656f
JH
148 lock_guard<recursive_mutex> lock(_mutex);
149
e8d00928
ML
150 append_data(logic->data_pointer(),
151 logic->data_length() / _unit_size);
4ceab49a
JH
152
153 // Generate the first mip-map from the data
154 append_payload_to_mipmap();
155}
156
ed990f11
JH
157void LogicSnapshot::get_samples(uint8_t *const data,
158 int64_t start_sample, int64_t end_sample) const
159{
160 assert(data);
161 assert(start_sample >= 0);
0fbda3c2 162 assert(start_sample <= (int64_t)_sample_count);
ed990f11 163 assert(end_sample >= 0);
0fbda3c2 164 assert(end_sample <= (int64_t)_sample_count);
ed990f11
JH
165 assert(start_sample <= end_sample);
166
167 lock_guard<recursive_mutex> lock(_mutex);
168
169 const size_t size = (end_sample - start_sample) * _unit_size;
c82c7e4e 170 memcpy(data, (const uint8_t*)_data.data() + start_sample * _unit_size, size);
ed990f11
JH
171}
172
023887ca 173void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
4ceab49a
JH
174{
175 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
176 MipMapDataUnit) * MipMapDataUnit;
333d5bbc 177 if (new_data_length > m.data_length)
4ceab49a
JH
178 {
179 m.data_length = new_data_length;
8fe60279
JH
180
181 // Padding is added to allow for the uint64_t write word
182 m.data = realloc(m.data, new_data_length * _unit_size +
183 sizeof(uint64_t));
4ceab49a
JH
184 }
185}
186
1b1ec774 187void LogicSnapshot::append_payload_to_mipmap()
4ceab49a
JH
188{
189 MipMapLevel &m0 = _mip_map[0];
190 uint64_t prev_length;
191 const uint8_t *src_ptr;
192 uint8_t *dest_ptr;
193 uint64_t accumulator;
194 unsigned int diff_counter;
195
196 // Expand the data buffer to fit the new samples
197 prev_length = m0.length;
77bff0b1 198 m0.length = _sample_count / MipMapScaleFactor;
4ceab49a
JH
199
200 // Break off if there are no new samples to compute
333d5bbc 201 if (m0.length == prev_length)
4ceab49a
JH
202 return;
203
023887ca 204 reallocate_mipmap_level(m0);
4ceab49a
JH
205
206 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
207
208 // Iterate through the samples to populate the first level mipmap
c82c7e4e 209 const uint8_t *const end_src_ptr = (uint8_t*)_data.data() +
4ceab49a 210 m0.length * _unit_size * MipMapScaleFactor;
c82c7e4e 211 for (src_ptr = (uint8_t*)_data.data() +
4ceab49a
JH
212 prev_length * _unit_size * MipMapScaleFactor;
213 src_ptr < end_src_ptr;)
214 {
215 // Accumulate transitions which have occurred in this sample
216 accumulator = 0;
217 diff_counter = MipMapScaleFactor;
333d5bbc 218 while (diff_counter-- > 0)
4ceab49a 219 {
8cb71705 220 const uint64_t sample = unpack_sample(src_ptr);
4ceab49a
JH
221 accumulator |= _last_append_sample ^ sample;
222 _last_append_sample = sample;
223 src_ptr += _unit_size;
224 }
225
8cb71705 226 pack_sample(dest_ptr, accumulator);
4ceab49a
JH
227 dest_ptr += _unit_size;
228 }
229
230 // Compute higher level mipmaps
333d5bbc 231 for (unsigned int level = 1; level < ScaleStepCount; level++)
4ceab49a
JH
232 {
233 MipMapLevel &m = _mip_map[level];
234 const MipMapLevel &ml = _mip_map[level-1];
235
236 // Expand the data buffer to fit the new samples
237 prev_length = m.length;
238 m.length = ml.length / MipMapScaleFactor;
239
240 // Break off if there are no more samples to computed
333d5bbc 241 if (m.length == prev_length)
4ceab49a
JH
242 break;
243
023887ca 244 reallocate_mipmap_level(m);
4ceab49a
JH
245
246 // Subsample the level lower level
247 src_ptr = (uint8_t*)ml.data +
248 _unit_size * prev_length * MipMapScaleFactor;
023887ca 249 const uint8_t *const end_dest_ptr =
4ceab49a 250 (uint8_t*)m.data + _unit_size * m.length;
333d5bbc 251 for (dest_ptr = (uint8_t*)m.data +
4ceab49a
JH
252 _unit_size * prev_length;
253 dest_ptr < end_dest_ptr;
254 dest_ptr += _unit_size)
255 {
256 accumulator = 0;
257 diff_counter = MipMapScaleFactor;
333d5bbc 258 while (diff_counter-- > 0)
4ceab49a 259 {
8cb71705 260 accumulator |= unpack_sample(src_ptr);
4ceab49a
JH
261 src_ptr += _unit_size;
262 }
263
8cb71705 264 pack_sample(dest_ptr, accumulator);
4ceab49a
JH
265 }
266 }
28a4c9c5 267}
2858b391 268
1b1ec774 269uint64_t LogicSnapshot::get_sample(uint64_t index) const
2858b391 270{
3f96c34a 271 assert(index < _sample_count);
2858b391 272
c82c7e4e 273 return unpack_sample((uint8_t*)_data.data() + index * _unit_size);
2858b391
JH
274}
275
1b1ec774 276void LogicSnapshot::get_subsampled_edges(
2858b391 277 std::vector<EdgePair> &edges,
60b0c2da 278 uint64_t start, uint64_t end,
0b02e057 279 float min_length, int sig_index)
2858b391 280{
60b0c2da
JH
281 uint64_t index = start;
282 unsigned int level;
7d0d64f9
JH
283 bool last_sample;
284 bool fast_forward;
0b02e057 285
0b02e057 286 assert(end <= get_sample_count());
2858b391 287 assert(start <= end);
0b02e057 288 assert(min_length > 0);
2858b391 289 assert(sig_index >= 0);
80d50141 290 assert(sig_index < 64);
2858b391 291
7d29656f
JH
292 lock_guard<recursive_mutex> lock(_mutex);
293
60b0c2da
JH
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) /
0b02e057 296 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 297 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 298
7d0d64f9
JH
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));
2858b391 302
333d5bbc 303 while (index + block_length <= end)
2858b391 304 {
7d0d64f9 305 //----- Continue to search -----//
0b02e057 306 level = min_level;
f06ab143
JH
307
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);
2858b391 311
333d5bbc 312 if (min_length < MipMapScaleFactor)
0b02e057
JH
313 {
314 // Search individual samples up to the beginning of
315 // the next first level mip map block
7d0d64f9 316 const uint64_t final_index = min(end,
0b02e057
JH
317 pow2_ceil(index, MipMapScalePower));
318
333d5bbc 319 for (; index < final_index &&
0b02e057
JH
320 (index & ~(~0 << MipMapScalePower)) != 0;
321 index++)
322 {
323 const bool sample =
324 (get_sample(index) & sig_mask) != 0;
7d0d64f9
JH
325
326 // If there was a change we cannot fast forward
333d5bbc 327 if (sample != last_sample) {
7d0d64f9 328 fast_forward = false;
0b02e057 329 break;
7d0d64f9 330 }
0b02e057
JH
331 }
332 }
333 else
334 {
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);
333d5bbc 341 if (index >= end)
0b02e057
JH
342 break;
343
7d0d64f9
JH
344 // We can fast forward only if there was no change
345 const bool sample =
346 (get_sample(index) & sig_mask) != 0;
f06ab143
JH
347 if (last_sample != sample)
348 fast_forward = false;
0b02e057
JH
349 }
350
333d5bbc 351 if (fast_forward) {
7d0d64f9
JH
352
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
356 // begins.
357
358 // Slide right and zoom out at the beginnings of mip-map
359 // blocks until we encounter a change
333d5bbc 360 while (1) {
7d0d64f9
JH
361 const int level_scale_power =
362 (level + 1) * MipMapScalePower;
363 const uint64_t offset =
364 index >> level_scale_power;
7d0d64f9
JH
365
366 // Check if we reached the last block at this
367 // level, or if there was a change in this block
333d5bbc 368 if (offset >= _mip_map[level].length ||
7d0d64f9
JH
369 (get_subsample(level, offset) &
370 sig_mask))
0b02e057
JH
371 break;
372
333d5bbc 373 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
7d0d64f9
JH
374 // If we are now at the beginning of a
375 // higher level mip-map block ascend one
376 // level
333d5bbc 377 if (level + 1 >= ScaleStepCount ||
7d0d64f9
JH
378 !_mip_map[level + 1].data)
379 break;
380
381 level++;
382 } else {
383 // Slide right to the beginning of the
384 // next mip map block
385 index = pow2_ceil(index + 1,
386 level_scale_power);
387 }
0b02e057 388 }
7d0d64f9
JH
389
390 // Zoom in, and slide right until we encounter a change,
391 // and repeat until we reach min_level
333d5bbc 392 while (1) {
7d0d64f9
JH
393 assert(_mip_map[level].data);
394
395 const int level_scale_power =
396 (level + 1) * MipMapScalePower;
397 const uint64_t offset =
398 index >> level_scale_power;
7d0d64f9
JH
399
400 // Check if we reached the last block at this
401 // level, or if there was a change in this block
333d5bbc 402 if (offset >= _mip_map[level].length ||
7d0d64f9
JH
403 (get_subsample(level, offset) &
404 sig_mask)) {
405 // Zoom in unless we reached the minimum
406 // zoom
333d5bbc 407 if (level == min_level)
7d0d64f9
JH
408 break;
409
410 level--;
411 } else {
412 // Slide right to the beginning of the
413 // next mip map block
414 index = pow2_ceil(index + 1,
415 level_scale_power);
416 }
0b02e057 417 }
0b02e057 418
7d0d64f9
JH
419 // If individual samples within the limit of resolution,
420 // do a linear search for the next transition within the
421 // block
333d5bbc
UH
422 if (min_length < MipMapScaleFactor) {
423 for (; index < end; index++) {
7d0d64f9
JH
424 const bool sample = (get_sample(index) &
425 sig_mask) != 0;
333d5bbc 426 if (sample != last_sample)
7d0d64f9
JH
427 break;
428 }
0b02e057
JH
429 }
430 }
431
7d0d64f9
JH
432 //----- Store the edge -----//
433
434 // Take the last sample of the quanization block
435 const int64_t final_index = index + block_length;
333d5bbc 436 if (index + block_length > end)
7d0d64f9
JH
437 break;
438
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));
443
444 index = final_index;
445 last_sample = final_sample;
2858b391
JH
446 }
447
448 // Add the final state
175d6573
JH
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));
2858b391 453}
0b02e057 454
1b1ec774 455uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
b2bcbe51
JH
456{
457 assert(level >= 0);
458 assert(_mip_map[level].data);
8cb71705 459 return unpack_sample((uint8_t*)_mip_map[level].data +
b2bcbe51
JH
460 _unit_size * offset);
461}
462
1b1ec774 463uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 464{
60b0c2da
JH
465 const uint64_t p = 1 << power;
466 return (x + p - 1) / p * p;
0b02e057 467}
51e77110 468
1b1ec774 469} // namespace data
51e77110 470} // namespace pv