]> sigrok.org Git - pulseview.git/blame - pv/data/logicsegment.cpp
Confirm with user when trying to close sessions with unsaved data
[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
f3d66e52 27#include "logicsegment.hpp"
f556bc6a 28
fe3a1c21 29#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 30
3b68d03d
JH
31using std::lock_guard;
32using std::recursive_mutex;
819f4c25
JH
33using std::max;
34using std::min;
35using std::pair;
e8d00928
ML
36using std::shared_ptr;
37
38using sigrok::Logic;
28a4c9c5 39
51e77110 40namespace pv {
1b1ec774 41namespace data {
51e77110 42
f3d66e52
JH
43const int LogicSegment::MipMapScalePower = 4;
44const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
45const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
46const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
4ceab49a 47
f3d66e52 48LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate,
3d79f521 49 const uint64_t expected_num_samples) :
f3d66e52 50 Segment(samplerate, logic->unit_size()),
8dbbc7f0 51 last_append_sample_(0)
f556bc6a 52{
27d7c96b
DK
53 set_capacity(expected_num_samples);
54
8dbbc7f0
JH
55 lock_guard<recursive_mutex> lock(mutex_);
56 memset(mip_map_, 0, sizeof(mip_map_));
f556bc6a
JH
57 append_payload(logic);
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
f3d66e52 141void LogicSegment::append_payload(shared_ptr<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
e8d00928 148 append_data(logic->data_pointer(),
8dbbc7f0 149 logic->data_length() / unit_size_);
4ceab49a
JH
150
151 // Generate the first mip-map from the data
152 append_payload_to_mipmap();
153}
154
f3d66e52 155void LogicSegment::get_samples(uint8_t *const data,
ed990f11
JH
156 int64_t start_sample, int64_t end_sample) const
157{
158 assert(data);
159 assert(start_sample >= 0);
8dbbc7f0 160 assert(start_sample <= (int64_t)sample_count_);
ed990f11 161 assert(end_sample >= 0);
8dbbc7f0 162 assert(end_sample <= (int64_t)sample_count_);
ed990f11
JH
163 assert(start_sample <= end_sample);
164
8dbbc7f0 165 lock_guard<recursive_mutex> lock(mutex_);
ed990f11 166
8dbbc7f0
JH
167 const size_t size = (end_sample - start_sample) * unit_size_;
168 memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
ed990f11
JH
169}
170
f3d66e52 171void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
4ceab49a
JH
172{
173 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
174 MipMapDataUnit) * MipMapDataUnit;
2ad82c2e 175 if (new_data_length > m.data_length) {
4ceab49a 176 m.data_length = new_data_length;
8fe60279
JH
177
178 // Padding is added to allow for the uint64_t write word
8dbbc7f0 179 m.data = realloc(m.data, new_data_length * unit_size_ +
8fe60279 180 sizeof(uint64_t));
4ceab49a
JH
181 }
182}
183
f3d66e52 184void LogicSegment::append_payload_to_mipmap()
4ceab49a 185{
8dbbc7f0 186 MipMapLevel &m0 = mip_map_[0];
4ceab49a
JH
187 uint64_t prev_length;
188 const uint8_t *src_ptr;
189 uint8_t *dest_ptr;
190 uint64_t accumulator;
191 unsigned int diff_counter;
192
193 // Expand the data buffer to fit the new samples
194 prev_length = m0.length;
8dbbc7f0 195 m0.length = sample_count_ / MipMapScaleFactor;
4ceab49a
JH
196
197 // Break off if there are no new samples to compute
333d5bbc 198 if (m0.length == prev_length)
4ceab49a
JH
199 return;
200
023887ca 201 reallocate_mipmap_level(m0);
4ceab49a 202
8dbbc7f0 203 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
4ceab49a
JH
204
205 // Iterate through the samples to populate the first level mipmap
8dbbc7f0
JH
206 const uint8_t *const end_src_ptr = (uint8_t*)data_.data() +
207 m0.length * unit_size_ * MipMapScaleFactor;
208 for (src_ptr = (uint8_t*)data_.data() +
2ad82c2e
UH
209 prev_length * unit_size_ * MipMapScaleFactor;
210 src_ptr < end_src_ptr;) {
4ceab49a
JH
211 // Accumulate transitions which have occurred in this sample
212 accumulator = 0;
213 diff_counter = MipMapScaleFactor;
2ad82c2e 214 while (diff_counter-- > 0) {
8cb71705 215 const uint64_t sample = unpack_sample(src_ptr);
8dbbc7f0
JH
216 accumulator |= last_append_sample_ ^ sample;
217 last_append_sample_ = sample;
218 src_ptr += unit_size_;
4ceab49a
JH
219 }
220
8cb71705 221 pack_sample(dest_ptr, accumulator);
8dbbc7f0 222 dest_ptr += unit_size_;
4ceab49a
JH
223 }
224
225 // Compute higher level mipmaps
2ad82c2e 226 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
227 MipMapLevel &m = mip_map_[level];
228 const MipMapLevel &ml = mip_map_[level-1];
4ceab49a
JH
229
230 // Expand the data buffer to fit the new samples
231 prev_length = m.length;
232 m.length = ml.length / MipMapScaleFactor;
233
234 // Break off if there are no more samples to computed
333d5bbc 235 if (m.length == prev_length)
4ceab49a
JH
236 break;
237
023887ca 238 reallocate_mipmap_level(m);
4ceab49a
JH
239
240 // Subsample the level lower level
241 src_ptr = (uint8_t*)ml.data +
8dbbc7f0 242 unit_size_ * prev_length * MipMapScaleFactor;
023887ca 243 const uint8_t *const end_dest_ptr =
8dbbc7f0 244 (uint8_t*)m.data + unit_size_ * m.length;
333d5bbc 245 for (dest_ptr = (uint8_t*)m.data +
2ad82c2e
UH
246 unit_size_ * prev_length;
247 dest_ptr < end_dest_ptr;
248 dest_ptr += unit_size_) {
4ceab49a
JH
249 accumulator = 0;
250 diff_counter = MipMapScaleFactor;
2ad82c2e 251 while (diff_counter-- > 0) {
8cb71705 252 accumulator |= unpack_sample(src_ptr);
8dbbc7f0 253 src_ptr += unit_size_;
4ceab49a
JH
254 }
255
8cb71705 256 pack_sample(dest_ptr, accumulator);
4ceab49a
JH
257 }
258 }
28a4c9c5 259}
2858b391 260
f3d66e52 261uint64_t LogicSegment::get_sample(uint64_t index) const
2858b391 262{
8dbbc7f0 263 assert(index < sample_count_);
2858b391 264
8dbbc7f0 265 return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
2858b391
JH
266}
267
f3d66e52 268void LogicSegment::get_subsampled_edges(
2858b391 269 std::vector<EdgePair> &edges,
60b0c2da 270 uint64_t start, uint64_t end,
0b02e057 271 float min_length, int sig_index)
2858b391 272{
60b0c2da
JH
273 uint64_t index = start;
274 unsigned int level;
7d0d64f9
JH
275 bool last_sample;
276 bool fast_forward;
0b02e057 277
0b02e057 278 assert(end <= get_sample_count());
2858b391 279 assert(start <= end);
0b02e057 280 assert(min_length > 0);
2858b391 281 assert(sig_index >= 0);
80d50141 282 assert(sig_index < 64);
2858b391 283
8dbbc7f0 284 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 285
60b0c2da
JH
286 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
287 const unsigned int min_level = max((int)floorf(logf(min_length) /
0b02e057 288 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 289 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 290
7d0d64f9
JH
291 // Store the initial state
292 last_sample = (get_sample(start) & sig_mask) != 0;
293 edges.push_back(pair<int64_t, bool>(index++, last_sample));
2858b391 294
2ad82c2e 295 while (index + block_length <= end) {
7d0d64f9 296 //----- Continue to search -----//
0b02e057 297 level = min_level;
f06ab143
JH
298
299 // We cannot fast-forward if there is no mip-map data at
300 // at the minimum level.
4c60462b 301 fast_forward = (mip_map_[level].data != nullptr);
2858b391 302
2ad82c2e 303 if (min_length < MipMapScaleFactor) {
0b02e057
JH
304 // Search individual samples up to the beginning of
305 // the next first level mip map block
7d0d64f9 306 const uint64_t final_index = min(end,
0b02e057
JH
307 pow2_ceil(index, MipMapScalePower));
308
333d5bbc 309 for (; index < final_index &&
c28fa62b 310 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
2ad82c2e 311 index++) {
0b02e057
JH
312 const bool sample =
313 (get_sample(index) & sig_mask) != 0;
7d0d64f9
JH
314
315 // If there was a change we cannot fast forward
333d5bbc 316 if (sample != last_sample) {
7d0d64f9 317 fast_forward = false;
0b02e057 318 break;
7d0d64f9 319 }
0b02e057 320 }
2ad82c2e 321 } else {
0b02e057
JH
322 // If resolution is less than a mip map block,
323 // round up to the beginning of the mip-map block
324 // for this level of detail
325 const int min_level_scale_power =
326 (level + 1) * MipMapScalePower;
327 index = pow2_ceil(index, min_level_scale_power);
333d5bbc 328 if (index >= end)
0b02e057
JH
329 break;
330
7d0d64f9
JH
331 // We can fast forward only if there was no change
332 const bool sample =
333 (get_sample(index) & sig_mask) != 0;
f06ab143
JH
334 if (last_sample != sample)
335 fast_forward = false;
0b02e057
JH
336 }
337
333d5bbc 338 if (fast_forward) {
7d0d64f9
JH
339
340 // Fast forward: This involves zooming out to higher
341 // levels of the mip map searching for changes, then
342 // zooming in on them to find the point where the edge
343 // begins.
344
345 // Slide right and zoom out at the beginnings of mip-map
346 // blocks until we encounter a change
333d5bbc 347 while (1) {
7d0d64f9
JH
348 const int level_scale_power =
349 (level + 1) * MipMapScalePower;
350 const uint64_t offset =
351 index >> level_scale_power;
7d0d64f9
JH
352
353 // Check if we reached the last block at this
354 // level, or if there was a change in this block
8dbbc7f0 355 if (offset >= mip_map_[level].length ||
7d0d64f9
JH
356 (get_subsample(level, offset) &
357 sig_mask))
0b02e057
JH
358 break;
359
c28fa62b 360 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
7d0d64f9
JH
361 // If we are now at the beginning of a
362 // higher level mip-map block ascend one
363 // level
333d5bbc 364 if (level + 1 >= ScaleStepCount ||
8dbbc7f0 365 !mip_map_[level + 1].data)
7d0d64f9
JH
366 break;
367
368 level++;
369 } else {
370 // Slide right to the beginning of the
371 // next mip map block
372 index = pow2_ceil(index + 1,
373 level_scale_power);
374 }
0b02e057 375 }
7d0d64f9
JH
376
377 // Zoom in, and slide right until we encounter a change,
378 // and repeat until we reach min_level
333d5bbc 379 while (1) {
8dbbc7f0 380 assert(mip_map_[level].data);
7d0d64f9
JH
381
382 const int level_scale_power =
383 (level + 1) * MipMapScalePower;
384 const uint64_t offset =
385 index >> level_scale_power;
7d0d64f9
JH
386
387 // Check if we reached the last block at this
388 // level, or if there was a change in this block
8dbbc7f0 389 if (offset >= mip_map_[level].length ||
2ad82c2e 390 (get_subsample(level, offset) &
7d0d64f9
JH
391 sig_mask)) {
392 // Zoom in unless we reached the minimum
393 // zoom
333d5bbc 394 if (level == min_level)
7d0d64f9
JH
395 break;
396
397 level--;
398 } else {
399 // Slide right to the beginning of the
400 // next mip map block
401 index = pow2_ceil(index + 1,
402 level_scale_power);
403 }
0b02e057 404 }
0b02e057 405
7d0d64f9
JH
406 // If individual samples within the limit of resolution,
407 // do a linear search for the next transition within the
408 // block
333d5bbc
UH
409 if (min_length < MipMapScaleFactor) {
410 for (; index < end; index++) {
7d0d64f9
JH
411 const bool sample = (get_sample(index) &
412 sig_mask) != 0;
333d5bbc 413 if (sample != last_sample)
7d0d64f9
JH
414 break;
415 }
0b02e057
JH
416 }
417 }
418
7d0d64f9
JH
419 //----- Store the edge -----//
420
421 // Take the last sample of the quanization block
422 const int64_t final_index = index + block_length;
333d5bbc 423 if (index + block_length > end)
7d0d64f9
JH
424 break;
425
426 // Store the final state
427 const bool final_sample =
428 (get_sample(final_index - 1) & sig_mask) != 0;
429 edges.push_back(pair<int64_t, bool>(index, final_sample));
430
431 index = final_index;
432 last_sample = final_sample;
2858b391
JH
433 }
434
435 // Add the final state
175d6573
JH
436 const bool end_sample = get_sample(end) & sig_mask;
437 if (last_sample != end_sample)
438 edges.push_back(pair<int64_t, bool>(end, end_sample));
439 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
2858b391 440}
0b02e057 441
f3d66e52 442uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
b2bcbe51
JH
443{
444 assert(level >= 0);
8dbbc7f0
JH
445 assert(mip_map_[level].data);
446 return unpack_sample((uint8_t*)mip_map_[level].data +
447 unit_size_ * offset);
b2bcbe51
JH
448}
449
f3d66e52 450uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 451{
60b0c2da
JH
452 const uint64_t p = 1 << power;
453 return (x + p - 1) / p * p;
0b02e057 454}
51e77110 455
1b1ec774 456} // namespace data
51e77110 457} // namespace pv