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