2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
27 #include <boost/foreach.hpp>
29 #include "logicdatasnapshot.h"
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
38 LogicDataSnapshot::LogicDataSnapshot(
39 const sr_datafeed_logic &logic) :
40 DataSnapshot(logic.unitsize),
41 _last_append_sample(0)
43 memset(_mip_map, 0, sizeof(_mip_map));
44 append_payload(logic);
47 LogicDataSnapshot::~LogicDataSnapshot()
49 BOOST_FOREACH(MipMapLevel &l, _mip_map)
53 void LogicDataSnapshot::append_payload(
54 const sr_datafeed_logic &logic)
56 assert(_unit_size == logic.unitsize);
58 append_data(logic.data, logic.length);
60 // Generate the first mip-map from the data
61 append_payload_to_mipmap();
64 void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
66 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
67 MipMapDataUnit) * MipMapDataUnit;
68 if(new_data_length > m.data_length)
70 m.data_length = new_data_length;
71 m.data = realloc(m.data, new_data_length * _unit_size);
75 void LogicDataSnapshot::append_payload_to_mipmap()
77 MipMapLevel &m0 = _mip_map[0];
79 const uint8_t *src_ptr;
82 unsigned int diff_counter;
84 // Expand the data buffer to fit the new samples
85 prev_length = m0.length;
86 m0.length = _sample_count / MipMapScaleFactor;
88 // Break off if there are no new samples to compute
89 if(m0.length == prev_length)
92 reallocate_mip_map(m0);
94 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
96 // Iterate through the samples to populate the first level mipmap
98 diff_counter = MipMapScaleFactor;
99 const uint8_t *end_src_ptr = (uint8_t*)_data +
100 m0.length * _unit_size * MipMapScaleFactor;
101 for(src_ptr = (uint8_t*)_data +
102 prev_length * _unit_size * MipMapScaleFactor;
103 src_ptr < end_src_ptr;)
105 // Accumulate transitions which have occurred in this sample
107 diff_counter = MipMapScaleFactor;
108 while(diff_counter-- > 0)
110 const uint64_t sample = *(uint64_t*)src_ptr;
111 accumulator |= _last_append_sample ^ sample;
112 _last_append_sample = sample;
113 src_ptr += _unit_size;
116 *(uint64_t*)dest_ptr = accumulator;
117 dest_ptr += _unit_size;
120 // Compute higher level mipmaps
121 for(int level = 1; level < ScaleStepCount; level++)
123 MipMapLevel &m = _mip_map[level];
124 const MipMapLevel &ml = _mip_map[level-1];
126 // Expand the data buffer to fit the new samples
127 prev_length = m.length;
128 m.length = ml.length / MipMapScaleFactor;
130 // Break off if there are no more samples to computed
131 if(m.length == prev_length)
134 reallocate_mip_map(m);
136 // Subsample the level lower level
137 src_ptr = (uint8_t*)ml.data +
138 _unit_size * prev_length * MipMapScaleFactor;
139 const uint8_t *end_dest_ptr =
140 (uint8_t*)m.data + _unit_size * m.length;
141 for(dest_ptr = (uint8_t*)m.data +
142 _unit_size * prev_length;
143 dest_ptr < end_dest_ptr;
144 dest_ptr += _unit_size)
147 diff_counter = MipMapScaleFactor;
148 while(diff_counter-- > 0)
150 accumulator |= *(uint64_t*)src_ptr;
151 src_ptr += _unit_size;
154 *(uint64_t*)dest_ptr = accumulator;
159 uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
162 assert(index >= 0 && index < _sample_count);
164 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
167 void LogicDataSnapshot::get_subsampled_edges(
168 std::vector<EdgePair> &edges,
169 int64_t start, int64_t end,
170 float min_length, int sig_index)
172 int64_t index = start;
178 assert(end <= get_sample_count());
179 assert(start <= end);
180 assert(min_length > 0);
181 assert(sig_index >= 0);
182 assert(sig_index < SR_MAX_NUM_PROBES);
184 const int64_t block_length = (int64_t)max(min_length, 1.0f);
185 const int min_level = max((int)floorf(logf(min_length) /
186 LogMipMapScaleFactor) - 1, 0);
187 const uint64_t sig_mask = 1ULL << sig_index;
189 // Store the initial state
190 last_sample = (get_sample(start) & sig_mask) != 0;
191 edges.push_back(pair<int64_t, bool>(index++, last_sample));
193 while(index + block_length <= end)
195 //----- Continue to search -----//
199 if(min_length < MipMapScaleFactor)
201 // Search individual samples up to the beginning of
202 // the next first level mip map block
203 const uint64_t final_index = min(end,
204 pow2_ceil(index, MipMapScalePower));
207 index < final_index &&
208 (index & ~(~0 << MipMapScalePower)) != 0;
212 (get_sample(index) & sig_mask) != 0;
214 // If there was a change we cannot fast forward
215 if(sample != last_sample) {
216 fast_forward = false;
223 // If resolution is less than a mip map block,
224 // round up to the beginning of the mip-map block
225 // for this level of detail
226 const int min_level_scale_power =
227 (level + 1) * MipMapScalePower;
228 index = pow2_ceil(index, min_level_scale_power);
232 // We can fast forward only if there was no change
234 (get_sample(index) & sig_mask) != 0;
235 fast_forward = last_sample == sample;
240 // Fast forward: This involves zooming out to higher
241 // levels of the mip map searching for changes, then
242 // zooming in on them to find the point where the edge
245 // Slide right and zoom out at the beginnings of mip-map
246 // blocks until we encounter a change
248 const int level_scale_power =
249 (level + 1) * MipMapScalePower;
250 const uint64_t offset =
251 index >> level_scale_power;
254 // Check if we reached the last block at this
255 // level, or if there was a change in this block
256 if(offset >= _mip_map[level].length ||
257 (get_subsample(level, offset) &
261 if((offset & ~(~0 << MipMapScalePower)) == 0) {
262 // If we are now at the beginning of a
263 // higher level mip-map block ascend one
265 if(level + 1 >= ScaleStepCount ||
266 !_mip_map[level + 1].data)
271 // Slide right to the beginning of the
272 // next mip map block
273 index = pow2_ceil(index + 1,
278 // Zoom in, and slide right until we encounter a change,
279 // and repeat until we reach min_level
281 assert(_mip_map[level].data);
283 const int level_scale_power =
284 (level + 1) * MipMapScalePower;
285 const uint64_t offset =
286 index >> level_scale_power;
289 // Check if we reached the last block at this
290 // level, or if there was a change in this block
291 if(offset >= _mip_map[level].length ||
292 (get_subsample(level, offset) &
294 // Zoom in unless we reached the minimum
296 if(level == min_level)
301 // Slide right to the beginning of the
302 // next mip map block
303 index = pow2_ceil(index + 1,
308 // If individual samples within the limit of resolution,
309 // do a linear search for the next transition within the
311 if(min_length < MipMapScaleFactor) {
312 for(index; index < end; index++) {
313 const bool sample = (get_sample(index) &
315 if(sample != last_sample)
321 //----- Store the edge -----//
323 // Take the last sample of the quanization block
324 const int64_t final_index = index + block_length;
325 if(index + block_length > end)
328 // Store the final state
329 const bool final_sample =
330 (get_sample(final_index - 1) & sig_mask) != 0;
331 edges.push_back(pair<int64_t, bool>(index, final_sample));
334 last_sample = final_sample;
337 // Add the final state
338 edges.push_back(pair<int64_t, bool>(end,
339 get_sample(end) & sig_mask));
342 uint64_t LogicDataSnapshot::get_subsample(int level, uint64_t offset) const
345 assert(_mip_map[level].data);
346 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
347 _unit_size * offset);
350 int64_t LogicDataSnapshot::pow2_ceil(int64_t x, unsigned int power)
352 const int64_t p = 1 << power;
353 return ((x < 0) ? x : (x + p - 1)) / p * p;