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