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