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