]> sigrok.org Git - pulseview.git/blob - pv/data/logicsnapshot.cpp
a89c8f93e36261862c5f4bad70696b80e50e5aba
[pulseview.git] / pv / data / logicsnapshot.cpp
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 "logicsnapshot.hpp"
29
30 #include <libsigrok/libsigrok.hpp>
31
32 using std::lock_guard;
33 using std::recursive_mutex;
34 using std::max;
35 using std::min;
36 using std::pair;
37 using std::shared_ptr;
38
39 using sigrok::Logic;
40
41 namespace pv {
42 namespace data {
43
44 const int LogicSnapshot::MipMapScalePower = 4;
45 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
46 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
47 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
48
49 LogicSnapshot::LogicSnapshot(shared_ptr<Logic> logic, uint64_t samplerate,
50                              const uint64_t expected_num_samples) :
51         Snapshot(samplerate, logic->unit_size()),
52         last_append_sample_(0)
53 {
54         set_capacity(expected_num_samples);
55
56         lock_guard<recursive_mutex> lock(mutex_);
57         memset(mip_map_, 0, sizeof(mip_map_));
58         append_payload(logic);
59 }
60
61 LogicSnapshot::~LogicSnapshot()
62 {
63         lock_guard<recursive_mutex> lock(mutex_);
64         for (MipMapLevel &l : mip_map_)
65                 free(l.data);
66 }
67
68 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
69 {
70 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
71         return *(uint64_t*)ptr;
72 #else
73         uint64_t value = 0;
74         switch(unit_size_) {
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;
103 #endif
104 }
105
106 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
107 {
108 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
109         *(uint64_t*)ptr = value;
110 #else
111         switch(unit_size_) {
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         }
139 #endif
140 }
141
142 void LogicSnapshot::append_payload(shared_ptr<Logic> logic)
143 {
144         assert(unit_size_ == logic->unit_size());
145         assert((logic->data_length() % unit_size_) == 0);
146
147         lock_guard<recursive_mutex> lock(mutex_);
148
149         append_data(logic->data_pointer(),
150                 logic->data_length() / unit_size_);
151
152         // Generate the first mip-map from the data
153         append_payload_to_mipmap();
154 }
155
156 void LogicSnapshot::get_samples(uint8_t *const data,
157         int64_t start_sample, int64_t end_sample) const
158 {
159         assert(data);
160         assert(start_sample >= 0);
161         assert(start_sample <= (int64_t)sample_count_);
162         assert(end_sample >= 0);
163         assert(end_sample <= (int64_t)sample_count_);
164         assert(start_sample <= end_sample);
165
166         lock_guard<recursive_mutex> lock(mutex_);
167
168         const size_t size = (end_sample - start_sample) * unit_size_;
169         memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
170 }
171
172 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
173 {
174         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
175                 MipMapDataUnit) * MipMapDataUnit;
176         if (new_data_length > m.data_length)
177         {
178                 m.data_length = new_data_length;
179
180                 // Padding is added to allow for the uint64_t write word
181                 m.data = realloc(m.data, new_data_length * unit_size_ +
182                         sizeof(uint64_t));
183         }
184 }
185
186 void LogicSnapshot::append_payload_to_mipmap()
187 {
188         MipMapLevel &m0 = mip_map_[0];
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;
197         m0.length = sample_count_ / MipMapScaleFactor;
198
199         // Break off if there are no new samples to compute
200         if (m0.length == prev_length)
201                 return;
202
203         reallocate_mipmap_level(m0);
204
205         dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
206
207         // Iterate through the samples to populate the first level mipmap
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;
212                 src_ptr < end_src_ptr;)
213         {
214                 // Accumulate transitions which have occurred in this sample
215                 accumulator = 0;
216                 diff_counter = MipMapScaleFactor;
217                 while (diff_counter-- > 0)
218                 {
219                         const uint64_t sample = unpack_sample(src_ptr);
220                         accumulator |= last_append_sample_ ^ sample;
221                         last_append_sample_ = sample;
222                         src_ptr += unit_size_;
223                 }
224
225                 pack_sample(dest_ptr, accumulator);
226                 dest_ptr += unit_size_;
227         }
228
229         // Compute higher level mipmaps
230         for (unsigned int level = 1; level < ScaleStepCount; level++)
231         {
232                 MipMapLevel &m = mip_map_[level];
233                 const MipMapLevel &ml = mip_map_[level-1];
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
240                 if (m.length == prev_length)
241                         break;
242
243                 reallocate_mipmap_level(m);
244
245                 // Subsample the level lower level
246                 src_ptr = (uint8_t*)ml.data +
247                         unit_size_ * prev_length * MipMapScaleFactor;
248                 const uint8_t *const end_dest_ptr =
249                         (uint8_t*)m.data + unit_size_ * m.length;
250                 for (dest_ptr = (uint8_t*)m.data +
251                         unit_size_ * prev_length;
252                         dest_ptr < end_dest_ptr;
253                         dest_ptr += unit_size_)
254                 {
255                         accumulator = 0;
256                         diff_counter = MipMapScaleFactor;
257                         while (diff_counter-- > 0)
258                         {
259                                 accumulator |= unpack_sample(src_ptr);
260                                 src_ptr += unit_size_;
261                         }
262
263                         pack_sample(dest_ptr, accumulator);
264                 }
265         }
266 }
267
268 uint64_t LogicSnapshot::get_sample(uint64_t index) const
269 {
270         assert(index < sample_count_);
271
272         return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
273 }
274
275 void LogicSnapshot::get_subsampled_edges(
276         std::vector<EdgePair> &edges,
277         uint64_t start, uint64_t end,
278         float min_length, int sig_index)
279 {
280         uint64_t index = start;
281         unsigned int level;
282         bool last_sample;
283         bool fast_forward;
284
285         assert(end <= get_sample_count());
286         assert(start <= end);
287         assert(min_length > 0);
288         assert(sig_index >= 0);
289         assert(sig_index < 64);
290
291         lock_guard<recursive_mutex> lock(mutex_);
292
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) /
295                 LogMipMapScaleFactor) - 1, 0);
296         const uint64_t sig_mask = 1ULL << sig_index;
297
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));
301
302         while (index + block_length <= end)
303         {
304                 //----- Continue to search -----//
305                 level = min_level;
306
307                 // We cannot fast-forward if there is no mip-map data at
308                 // at the minimum level.
309                 fast_forward = (mip_map_[level].data != NULL);
310
311                 if (min_length < MipMapScaleFactor)
312                 {
313                         // Search individual samples up to the beginning of
314                         // the next first level mip map block
315                         const uint64_t final_index = min(end,
316                                 pow2_ceil(index, MipMapScalePower));
317
318                         for (; index < final_index &&
319                                 (index & ~(~0 << MipMapScalePower)) != 0;
320                                 index++)
321                         {
322                                 const bool sample =
323                                         (get_sample(index) & sig_mask) != 0;
324
325                                 // If there was a change we cannot fast forward
326                                 if (sample != last_sample) {
327                                         fast_forward = false;
328                                         break;
329                                 }
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);
340                         if (index >= end)
341                                 break;
342
343                         // We can fast forward only if there was no change
344                         const bool sample =
345                                 (get_sample(index) & sig_mask) != 0;
346                         if (last_sample != sample)
347                                 fast_forward = false;
348                 }
349
350                 if (fast_forward) {
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
359                         while (1) {
360                                 const int level_scale_power =
361                                         (level + 1) * MipMapScalePower;
362                                 const uint64_t offset =
363                                         index >> level_scale_power;
364
365                                 // Check if we reached the last block at this
366                                 // level, or if there was a change in this block
367                                 if (offset >= mip_map_[level].length ||
368                                         (get_subsample(level, offset) &
369                                                 sig_mask))
370                                         break;
371
372                                 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
373                                         // If we are now at the beginning of a
374                                         // higher level mip-map block ascend one
375                                         // level
376                                         if (level + 1 >= ScaleStepCount ||
377                                                 !mip_map_[level + 1].data)
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                                 }
387                         }
388
389                         // Zoom in, and slide right until we encounter a change,
390                         // and repeat until we reach min_level
391                         while (1) {
392                                 assert(mip_map_[level].data);
393
394                                 const int level_scale_power =
395                                         (level + 1) * MipMapScalePower;
396                                 const uint64_t offset =
397                                         index >> level_scale_power;
398
399                                 // Check if we reached the last block at this
400                                 // level, or if there was a change in this block
401                                 if (offset >= mip_map_[level].length ||
402                                         (get_subsample(level, offset) &
403                                                 sig_mask)) {
404                                         // Zoom in unless we reached the minimum
405                                         // zoom
406                                         if (level == min_level)
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                                 }
416                         }
417
418                         // If individual samples within the limit of resolution,
419                         // do a linear search for the next transition within the
420                         // block
421                         if (min_length < MipMapScaleFactor) {
422                                 for (; index < end; index++) {
423                                         const bool sample = (get_sample(index) &
424                                                 sig_mask) != 0;
425                                         if (sample != last_sample)
426                                                 break;
427                                 }
428                         }
429                 }
430
431                 //----- Store the edge -----//
432
433                 // Take the last sample of the quanization block
434                 const int64_t final_index = index + block_length;
435                 if (index + block_length > end)
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;
445         }
446
447         // Add the final state
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));
452 }
453
454 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
455 {
456         assert(level >= 0);
457         assert(mip_map_[level].data);
458         return unpack_sample((uint8_t*)mip_map_[level].data +
459                 unit_size_ * offset);
460 }
461
462 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
463 {
464         const uint64_t p = 1 << power;
465         return (x + p - 1) / p * p;
466 }
467
468 } // namespace data
469 } // namespace pv