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