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