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 const uint64_t prev_length = _data_length;
59 append_data(logic.data, logic.length);
61 // Generate the first mip-map from the data
62 append_payload_to_mipmap();
65 void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
67 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
68 MipMapDataUnit) * MipMapDataUnit;
69 if(new_data_length > m.data_length)
71 m.data_length = new_data_length;
72 m.data = realloc(m.data, new_data_length * _unit_size);
76 void LogicDataSnapshot::append_payload_to_mipmap()
78 MipMapLevel &m0 = _mip_map[0];
80 const uint8_t *src_ptr;
83 unsigned int diff_counter;
85 // Expand the data buffer to fit the new samples
86 prev_length = m0.length;
87 m0.length = _data_length / MipMapScaleFactor;
89 // Break off if there are no new samples to compute
90 if(m0.length == prev_length)
93 reallocate_mip_map(m0);
95 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
97 // Iterate through the samples to populate the first level mipmap
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;)
106 // Accumulate transitions which have occurred in this sample
108 diff_counter = MipMapScaleFactor;
109 while(diff_counter-- > 0)
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;
117 *(uint64_t*)dest_ptr = accumulator;
118 dest_ptr += _unit_size;
121 // Compute higher level mipmaps
122 for(int level = 1; level < ScaleStepCount; level++)
124 MipMapLevel &m = _mip_map[level];
125 const MipMapLevel &ml = _mip_map[level-1];
127 // Expand the data buffer to fit the new samples
128 prev_length = m.length;
129 m.length = ml.length / MipMapScaleFactor;
131 // Break off if there are no more samples to computed
132 if(m.length == prev_length)
135 reallocate_mip_map(m);
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)
148 diff_counter = MipMapScaleFactor;
149 while(diff_counter-- > 0)
151 accumulator |= *(uint64_t*)src_ptr;
152 src_ptr += _unit_size;
155 *(uint64_t*)dest_ptr = accumulator;
160 uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
163 assert(index >= 0 && index < _data_length);
165 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
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)
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);
183 const int min_level = max((int)floorf(logf(min_length) /
184 LogMipMapScaleFactor) - 1, 0);
185 const uint64_t sig_mask = 1 << sig_index;
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));
192 for(index = start + 1; index < end;)
196 if(min_length < MipMapScaleFactor)
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));
204 index < final_sample &&
205 (index & ~(~0 << MipMapScalePower)) != 0;
209 (get_sample(index) & sig_mask) != 0;
210 if(sample != last_sample)
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);
224 // Slide right and zoom out at the beginnings of mip-map
225 // blocks until we encounter a change
228 const int level_scale_power =
229 (level + 1) * MipMapScalePower;
230 const uint64_t offset = index >> level_scale_power;
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 (*(uint64_t*)((uint8_t*)_mip_map[level].data +
237 _unit_size * offset) & sig_mask))
240 if((offset & ~(~0 << MipMapScalePower)) == 0)
242 // If we are now at the beginning of a higher
243 // level mip-map block ascend one level
244 if(!_mip_map[level + 1].data)
251 // Slide right to the beginning of the next mip
253 index = pow2_ceil(index, level_scale_power);
257 // Zoom in, and slide right until we encounter a change,
258 // and repeat until we reach min_level
261 assert(_mip_map[level].data);
263 const int level_scale_power =
264 (level + 1) * MipMapScalePower;
265 const uint64_t offset = index >> level_scale_power;
268 // Check if we reached the last block at this level,
269 // or if there was a change in this block
270 if(offset >= _mip_map[level].length ||
271 (*(uint64_t*)((uint8_t*)_mip_map[level].data +
272 _unit_size * offset) & sig_mask))
274 // Zoom in unless we reached the minimum zoom
275 if(level == min_level)
282 // Slide right to the beginning of the next mip map block
283 index = pow2_ceil(index, level_scale_power);
287 // If individual samples within the limit of resolution,
288 // do a linear search for the next transition within the block
289 if(min_length < MipMapScaleFactor)
291 for(index; index < end; index++)
294 (get_sample(index) & sig_mask) != 0;
295 if(sample != last_sample)
302 // Take the last sample of the quanization block
303 const int64_t block_length = (int64_t)max(min_length, 1.0f);
304 const int64_t rem = index % block_length;
305 const int64_t final_index = min(index + (rem == 0 ? 0 :
306 block_length - rem), end);
308 // Store the final state
309 const bool final_sample = get_sample(final_index) & sig_mask;
310 edges.push_back(pair<int64_t, bool>(
311 final_index, final_sample));
313 // Continue to sample
315 last_sample = final_sample;
321 // Add the final state
322 edges.push_back(pair<int64_t, bool>(end,
323 get_sample(end) & sig_mask));
326 int64_t LogicDataSnapshot::pow2_ceil(int64_t x, int power)
328 return ((x >> power) + 1) << power;