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