]> sigrok.org Git - pulseview.git/blame - pv/logicdatasnapshot.cpp
Added a context menu for set name
[pulseview.git] / pv / logicdatasnapshot.cpp
CommitLineData
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
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
cef18fc6 21#include <extdef.h>
28a4c9c5 22
f556bc6a 23#include <assert.h>
4ceab49a 24#include <string.h>
b36d8550 25#include <stdlib.h>
4ceab49a
JH
26#include <math.h>
27
28#include <boost/foreach.hpp>
29
30#include "logicdatasnapshot.h"
f556bc6a 31
2858b391 32using namespace std;
28a4c9c5 33
51e77110
JH
34namespace pv {
35
4ceab49a
JH
36const int LogicDataSnapshot::MipMapScalePower = 4;
37const int LogicDataSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
0b02e057 38const float LogicDataSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
4ceab49a
JH
39const uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024; // bytes
40
f556bc6a
JH
41LogicDataSnapshot::LogicDataSnapshot(
42 const sr_datafeed_logic &logic) :
4ceab49a
JH
43 DataSnapshot(logic.unitsize),
44 _last_append_sample(0)
f556bc6a 45{
4ceab49a 46 memset(_mip_map, 0, sizeof(_mip_map));
f556bc6a
JH
47 append_payload(logic);
48}
49
4ceab49a
JH
50LogicDataSnapshot::~LogicDataSnapshot()
51{
52 BOOST_FOREACH(MipMapLevel &l, _mip_map)
53 free(l.data);
54}
55
28a4c9c5
JH
56void LogicDataSnapshot::append_payload(
57 const sr_datafeed_logic &logic)
58{
f556bc6a
JH
59 assert(_unit_size == logic.unitsize);
60
f556bc6a 61 append_data(logic.data, logic.length);
4ceab49a
JH
62
63 // Generate the first mip-map from the data
64 append_payload_to_mipmap();
65}
66
67void 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
78void 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;
77bff0b1 89 m0.length = _sample_count / MipMapScaleFactor;
4ceab49a
JH
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
60b0c2da 124 for(unsigned int level = 1; level < ScaleStepCount; level++)
4ceab49a
JH
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 }
28a4c9c5 160}
2858b391
JH
161
162uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
163{
164 assert(_data);
77bff0b1 165 assert(index >= 0 && index < _sample_count);
2858b391
JH
166
167 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
168}
169
170void LogicDataSnapshot::get_subsampled_edges(
171 std::vector<EdgePair> &edges,
60b0c2da 172 uint64_t start, uint64_t end,
0b02e057 173 float min_length, int sig_index)
2858b391 174{
60b0c2da
JH
175 uint64_t index = start;
176 unsigned int level;
7d0d64f9
JH
177 bool last_sample;
178 bool fast_forward;
0b02e057 179
2858b391 180 assert(start >= 0);
0b02e057 181 assert(end <= get_sample_count());
2858b391 182 assert(start <= end);
0b02e057 183 assert(min_length > 0);
2858b391
JH
184 assert(sig_index >= 0);
185 assert(sig_index < SR_MAX_NUM_PROBES);
186
60b0c2da
JH
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) /
0b02e057 189 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 190 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 191
7d0d64f9
JH
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));
2858b391 195
7d0d64f9 196 while(index + block_length <= end)
2858b391 197 {
7d0d64f9 198 //----- Continue to search -----//
0b02e057 199 level = min_level;
7d0d64f9 200 fast_forward = true;
2858b391 201
0b02e057
JH
202 if(min_length < MipMapScaleFactor)
203 {
204 // Search individual samples up to the beginning of
205 // the next first level mip map block
7d0d64f9 206 const uint64_t final_index = min(end,
0b02e057
JH
207 pow2_ceil(index, MipMapScalePower));
208
f82670ac 209 for(; index < final_index &&
0b02e057
JH
210 (index & ~(~0 << MipMapScalePower)) != 0;
211 index++)
212 {
213 const bool sample =
214 (get_sample(index) & sig_mask) != 0;
7d0d64f9
JH
215
216 // If there was a change we cannot fast forward
217 if(sample != last_sample) {
218 fast_forward = false;
0b02e057 219 break;
7d0d64f9 220 }
0b02e057
JH
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);
7d0d64f9 231 if(index >= end)
0b02e057
JH
232 break;
233
7d0d64f9
JH
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;
0b02e057
JH
238 }
239
7d0d64f9
JH
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))
0b02e057
JH
261 break;
262
7d0d64f9
JH
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 }
0b02e057 278 }
7d0d64f9
JH
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 }
0b02e057 308 }
0b02e057 309
7d0d64f9
JH
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) {
f82670ac 314 for(; index < end; index++) {
7d0d64f9
JH
315 const bool sample = (get_sample(index) &
316 sig_mask) != 0;
317 if(sample != last_sample)
318 break;
319 }
0b02e057
JH
320 }
321 }
322
7d0d64f9
JH
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;
2858b391
JH
337 }
338
339 // Add the final state
652010ea
JH
340 edges.push_back(pair<int64_t, bool>(end,
341 get_sample(end) & sig_mask));
2858b391 342}
0b02e057 343
b2bcbe51
JH
344uint64_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
60b0c2da 352uint64_t LogicDataSnapshot::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 353{
60b0c2da
JH
354 const uint64_t p = 1 << power;
355 return (x + p - 1) / p * p;
0b02e057 356}
51e77110
JH
357
358} // namespace pv