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