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