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