]> sigrok.org Git - pulseview.git/blob - pv/logicdatasnapshot.cpp
Initial real time display during capture
[pulseview.git] / pv / logicdatasnapshot.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 <boost/foreach.hpp>
29
30 #include "logicdatasnapshot.h"
31
32 using namespace std;
33
34 namespace pv {
35
36 const int LogicDataSnapshot::MipMapScalePower = 4;
37 const int LogicDataSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
38 const float LogicDataSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
39 const uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024;     // bytes
40
41 LogicDataSnapshot::LogicDataSnapshot(
42         const sr_datafeed_logic &logic) :
43         DataSnapshot(logic.unitsize),
44         _last_append_sample(0)
45 {
46         memset(_mip_map, 0, sizeof(_mip_map));
47         append_payload(logic);
48 }
49
50 LogicDataSnapshot::~LogicDataSnapshot()
51 {
52         BOOST_FOREACH(MipMapLevel &l, _mip_map)
53                 free(l.data);
54 }
55
56 void LogicDataSnapshot::append_payload(
57         const sr_datafeed_logic &logic)
58 {
59         assert(_unit_size == logic.unitsize);
60
61         append_data(logic.data, logic.length);
62
63         // Generate the first mip-map from the data
64         append_payload_to_mipmap();
65 }
66
67 void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
68 {
69         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
70                 MipMapDataUnit) * MipMapDataUnit;
71         if(new_data_length > m.data_length)
72         {
73                 m.data_length = new_data_length;
74                 m.data = realloc(m.data, new_data_length * _unit_size);
75         }
76 }
77
78 void LogicDataSnapshot::append_payload_to_mipmap()
79 {
80         MipMapLevel &m0 = _mip_map[0];
81         uint64_t prev_length;
82         const uint8_t *src_ptr;
83         uint8_t *dest_ptr;
84         uint64_t accumulator;
85         unsigned int diff_counter;
86
87         // Expand the data buffer to fit the new samples
88         prev_length = m0.length;
89         m0.length = _sample_count / MipMapScaleFactor;
90
91         // Break off if there are no new samples to compute
92         if(m0.length == prev_length)
93                 return;
94
95         reallocate_mip_map(m0);
96
97         dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
98
99         // Iterate through the samples to populate the first level mipmap
100         accumulator = 0;
101         diff_counter = MipMapScaleFactor;
102         const uint8_t *end_src_ptr = (uint8_t*)_data +
103                 m0.length * _unit_size * MipMapScaleFactor;
104         for(src_ptr = (uint8_t*)_data +
105                 prev_length * _unit_size * MipMapScaleFactor;
106                 src_ptr < end_src_ptr;)
107         {
108                 // Accumulate transitions which have occurred in this sample
109                 accumulator = 0;
110                 diff_counter = MipMapScaleFactor;
111                 while(diff_counter-- > 0)
112                 {
113                         const uint64_t sample = *(uint64_t*)src_ptr;
114                         accumulator |= _last_append_sample ^ sample;
115                         _last_append_sample = sample;
116                         src_ptr += _unit_size;
117                 }
118
119                 *(uint64_t*)dest_ptr = accumulator;
120                 dest_ptr += _unit_size;
121         }
122
123         // Compute higher level mipmaps
124         for(unsigned int level = 1; level < ScaleStepCount; level++)
125         {
126                 MipMapLevel &m = _mip_map[level];
127                 const MipMapLevel &ml = _mip_map[level-1];
128
129                 // Expand the data buffer to fit the new samples
130                 prev_length = m.length;
131                 m.length = ml.length / MipMapScaleFactor;
132
133                 // Break off if there are no more samples to computed
134                 if(m.length == prev_length)
135                         break;
136
137                 reallocate_mip_map(m);
138
139                 // Subsample the level lower level
140                 src_ptr = (uint8_t*)ml.data +
141                         _unit_size * prev_length * MipMapScaleFactor;
142                 const uint8_t *end_dest_ptr =
143                         (uint8_t*)m.data + _unit_size * m.length;
144                 for(dest_ptr = (uint8_t*)m.data +
145                         _unit_size * prev_length;
146                         dest_ptr < end_dest_ptr;
147                         dest_ptr += _unit_size)
148                 {
149                         accumulator = 0;
150                         diff_counter = MipMapScaleFactor;
151                         while(diff_counter-- > 0)
152                         {
153                                 accumulator |= *(uint64_t*)src_ptr;
154                                 src_ptr += _unit_size;
155                         }
156
157                         *(uint64_t*)dest_ptr = accumulator;
158                 }
159         }
160 }
161
162 uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
163 {
164         assert(_data);
165         assert(index >= 0 && index < _sample_count);
166
167         return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
168 }
169
170 void LogicDataSnapshot::get_subsampled_edges(
171         std::vector<EdgePair> &edges,
172         uint64_t start, uint64_t end,
173         float min_length, int sig_index)
174 {
175         uint64_t index = start;
176         unsigned int level;
177         bool last_sample;
178         bool fast_forward;
179
180         assert(start >= 0);
181         assert(end <= get_sample_count());
182         assert(start <= end);
183         assert(min_length > 0);
184         assert(sig_index >= 0);
185         assert(sig_index < SR_MAX_NUM_PROBES);
186
187         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
188         const unsigned int min_level = max((int)floorf(logf(min_length) /
189                 LogMipMapScaleFactor) - 1, 0);
190         const uint64_t sig_mask = 1ULL << sig_index;
191
192         // Store the initial state
193         last_sample = (get_sample(start) & sig_mask) != 0;
194         edges.push_back(pair<int64_t, bool>(index++, last_sample));
195
196         while(index + block_length <= end)
197         {
198                 //----- Continue to search -----//
199                 level = min_level;
200                 fast_forward = true;
201
202                 if(min_length < MipMapScaleFactor)
203                 {
204                         // Search individual samples up to the beginning of
205                         // the next first level mip map block
206                         const uint64_t final_index = min(end,
207                                 pow2_ceil(index, MipMapScalePower));
208
209                         for(; index < final_index &&
210                                 (index & ~(~0 << MipMapScalePower)) != 0;
211                                 index++)
212                         {
213                                 const bool sample =
214                                         (get_sample(index) & sig_mask) != 0;
215
216                                 // If there was a change we cannot fast forward
217                                 if(sample != last_sample) {
218                                         fast_forward = false;
219                                         break;
220                                 }
221                         }
222                 }
223                 else
224                 {
225                         // If resolution is less than a mip map block,
226                         // round up to the beginning of the mip-map block
227                         // for this level of detail
228                         const int min_level_scale_power =
229                                 (level + 1) * MipMapScalePower;
230                         index = pow2_ceil(index, min_level_scale_power);
231                         if(index >= end)
232                                 break;
233
234                         // We can fast forward only if there was no change
235                         const bool sample =
236                                 (get_sample(index) & sig_mask) != 0;
237                         fast_forward = last_sample == sample;
238                 }
239
240                 if(fast_forward) {
241
242                         // Fast forward: This involves zooming out to higher
243                         // levels of the mip map searching for changes, then
244                         // zooming in on them to find the point where the edge
245                         // begins.
246
247                         // Slide right and zoom out at the beginnings of mip-map
248                         // blocks until we encounter a change
249                         while(1) {
250                                 const int level_scale_power =
251                                         (level + 1) * MipMapScalePower;
252                                 const uint64_t offset =
253                                         index >> level_scale_power;
254                                 assert(offset >= 0);
255
256                                 // Check if we reached the last block at this
257                                 // level, or if there was a change in this block
258                                 if(offset >= _mip_map[level].length ||
259                                         (get_subsample(level, offset) &
260                                                 sig_mask))
261                                         break;
262
263                                 if((offset & ~(~0 << MipMapScalePower)) == 0) {
264                                         // If we are now at the beginning of a
265                                         // higher level mip-map block ascend one
266                                         // level
267                                         if(level + 1 >= ScaleStepCount ||
268                                                 !_mip_map[level + 1].data)
269                                                 break;
270
271                                         level++;
272                                 } else {
273                                         // Slide right to the beginning of the
274                                         // next mip map block
275                                         index = pow2_ceil(index + 1,
276                                                 level_scale_power);
277                                 }
278                         }
279
280                         // Zoom in, and slide right until we encounter a change,
281                         // and repeat until we reach min_level
282                         while(1) {
283                                 assert(_mip_map[level].data);
284
285                                 const int level_scale_power =
286                                         (level + 1) * MipMapScalePower;
287                                 const uint64_t offset =
288                                         index >> level_scale_power;
289                                 assert(offset >= 0);
290
291                                 // Check if we reached the last block at this
292                                 // level, or if there was a change in this block
293                                 if(offset >= _mip_map[level].length ||
294                                         (get_subsample(level, offset) &
295                                                 sig_mask)) {
296                                         // Zoom in unless we reached the minimum
297                                         // zoom
298                                         if(level == min_level)
299                                                 break;
300
301                                         level--;
302                                 } else {
303                                         // Slide right to the beginning of the
304                                         // next mip map block
305                                         index = pow2_ceil(index + 1,
306                                                 level_scale_power);
307                                 }
308                         }
309
310                         // If individual samples within the limit of resolution,
311                         // do a linear search for the next transition within the
312                         // block
313                         if(min_length < MipMapScaleFactor) {
314                                 for(; index < end; index++) {
315                                         const bool sample = (get_sample(index) &
316                                                 sig_mask) != 0;
317                                         if(sample != last_sample)
318                                                 break;
319                                 }
320                         }
321                 }
322
323                 //----- Store the edge -----//
324
325                 // Take the last sample of the quanization block
326                 const int64_t final_index = index + block_length;
327                 if(index + block_length > end)
328                         break;
329
330                 // Store the final state
331                 const bool final_sample =
332                         (get_sample(final_index - 1) & sig_mask) != 0;
333                 edges.push_back(pair<int64_t, bool>(index, final_sample));
334
335                 index = final_index;
336                 last_sample = final_sample;
337         }
338
339         // Add the final state
340         edges.push_back(pair<int64_t, bool>(end,
341                 get_sample(end) & sig_mask));
342 }
343
344 uint64_t LogicDataSnapshot::get_subsample(int level, uint64_t offset) const
345 {
346         assert(level >= 0);
347         assert(_mip_map[level].data);
348         return *(uint64_t*)((uint8_t*)_mip_map[level].data +
349                 _unit_size * offset);
350 }
351
352 uint64_t LogicDataSnapshot::pow2_ceil(uint64_t x, unsigned int power)
353 {
354         const uint64_t p = 1 << power;
355         return (x + p - 1) / p * p;
356 }
357
358 } // namespace pv