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