]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
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 | ||
21 | #include "extdef.h" | |
22 | ||
23 | #include <assert.h> | |
24 | #include <string.h> | |
25 | #include <math.h> | |
26 | ||
27 | #include <boost/foreach.hpp> | |
28 | ||
29 | #include "logicdatasnapshot.h" | |
30 | ||
31 | using namespace std; | |
32 | ||
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 | |
37 | ||
38 | LogicDataSnapshot::LogicDataSnapshot( | |
39 | const sr_datafeed_logic &logic) : | |
40 | DataSnapshot(logic.unitsize), | |
41 | _last_append_sample(0) | |
42 | { | |
43 | memset(_mip_map, 0, sizeof(_mip_map)); | |
44 | append_payload(logic); | |
45 | } | |
46 | ||
47 | LogicDataSnapshot::~LogicDataSnapshot() | |
48 | { | |
49 | BOOST_FOREACH(MipMapLevel &l, _mip_map) | |
50 | free(l.data); | |
51 | } | |
52 | ||
53 | void LogicDataSnapshot::append_payload( | |
54 | const sr_datafeed_logic &logic) | |
55 | { | |
56 | assert(_unit_size == logic.unitsize); | |
57 | ||
58 | const uint64_t prev_length = _data_length; | |
59 | append_data(logic.data, logic.length); | |
60 | ||
61 | // Generate the first mip-map from the data | |
62 | append_payload_to_mipmap(); | |
63 | } | |
64 | ||
65 | void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m) | |
66 | { | |
67 | const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) / | |
68 | MipMapDataUnit) * MipMapDataUnit; | |
69 | if(new_data_length > m.data_length) | |
70 | { | |
71 | m.data_length = new_data_length; | |
72 | m.data = realloc(m.data, new_data_length * _unit_size); | |
73 | } | |
74 | } | |
75 | ||
76 | void LogicDataSnapshot::append_payload_to_mipmap() | |
77 | { | |
78 | MipMapLevel &m0 = _mip_map[0]; | |
79 | uint64_t prev_length; | |
80 | const uint8_t *src_ptr; | |
81 | uint8_t *dest_ptr; | |
82 | uint64_t accumulator; | |
83 | unsigned int diff_counter; | |
84 | ||
85 | // Expand the data buffer to fit the new samples | |
86 | prev_length = m0.length; | |
87 | m0.length = _data_length / MipMapScaleFactor; | |
88 | ||
89 | // Break off if there are no new samples to compute | |
90 | if(m0.length == prev_length) | |
91 | return; | |
92 | ||
93 | reallocate_mip_map(m0); | |
94 | ||
95 | dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size; | |
96 | ||
97 | // Iterate through the samples to populate the first level mipmap | |
98 | accumulator = 0; | |
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;) | |
105 | { | |
106 | // Accumulate transitions which have occurred in this sample | |
107 | accumulator = 0; | |
108 | diff_counter = MipMapScaleFactor; | |
109 | while(diff_counter-- > 0) | |
110 | { | |
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; | |
115 | } | |
116 | ||
117 | *(uint64_t*)dest_ptr = accumulator; | |
118 | dest_ptr += _unit_size; | |
119 | } | |
120 | ||
121 | // Compute higher level mipmaps | |
122 | for(int level = 1; level < ScaleStepCount; level++) | |
123 | { | |
124 | MipMapLevel &m = _mip_map[level]; | |
125 | const MipMapLevel &ml = _mip_map[level-1]; | |
126 | ||
127 | // Expand the data buffer to fit the new samples | |
128 | prev_length = m.length; | |
129 | m.length = ml.length / MipMapScaleFactor; | |
130 | ||
131 | // Break off if there are no more samples to computed | |
132 | if(m.length == prev_length) | |
133 | break; | |
134 | ||
135 | reallocate_mip_map(m); | |
136 | ||
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) | |
146 | { | |
147 | accumulator = 0; | |
148 | diff_counter = MipMapScaleFactor; | |
149 | while(diff_counter-- > 0) | |
150 | { | |
151 | accumulator |= *(uint64_t*)src_ptr; | |
152 | src_ptr += _unit_size; | |
153 | } | |
154 | ||
155 | *(uint64_t*)dest_ptr = accumulator; | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | uint64_t LogicDataSnapshot::get_sample(uint64_t index) const | |
161 | { | |
162 | assert(_data); | |
163 | assert(index >= 0 && index < _data_length); | |
164 | ||
165 | return *(uint64_t*)((uint8_t*)_data + index * _unit_size); | |
166 | } | |
167 | ||
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) | |
172 | { | |
173 | int64_t index; | |
174 | int level; | |
175 | ||
176 | assert(start >= 0); | |
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); | |
182 | ||
183 | const int min_level = max((int)floorf(logf(min_length) / | |
184 | LogMipMapScaleFactor) - 1, 0); | |
185 | const uint64_t sig_mask = 1 << sig_index; | |
186 | ||
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)); | |
190 | ||
191 | index = start + 1; | |
192 | for(index = start + 1; index < end;) | |
193 | { | |
194 | level = min_level; | |
195 | ||
196 | if(min_length < MipMapScaleFactor) | |
197 | { | |
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)); | |
202 | ||
203 | for(index; | |
204 | index < final_sample && | |
205 | (index & ~(~0 << MipMapScalePower)) != 0; | |
206 | index++) | |
207 | { | |
208 | const bool sample = | |
209 | (get_sample(index) & sig_mask) != 0; | |
210 | if(sample != last_sample) | |
211 | break; | |
212 | } | |
213 | } | |
214 | else | |
215 | { | |
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); | |
222 | } | |
223 | ||
224 | // Slide right and zoom out at the beginnings of mip-map | |
225 | // blocks until we encounter a change | |
226 | while(1) | |
227 | { | |
228 | const int level_scale_power = | |
229 | (level + 1) * MipMapScalePower; | |
230 | const uint64_t offset = index >> level_scale_power; | |
231 | assert(offset >= 0); | |
232 | ||
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)) | |
238 | break; | |
239 | ||
240 | if((offset & ~(~0 << MipMapScalePower)) == 0) | |
241 | { | |
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) | |
245 | break; | |
246 | ||
247 | level++; | |
248 | } | |
249 | else | |
250 | { | |
251 | // Slide right to the beginning of the next mip | |
252 | // map block | |
253 | index = pow2_ceil(index, level_scale_power); | |
254 | } | |
255 | } | |
256 | ||
257 | // Zoom in, and slide right until we encounter a change, | |
258 | // and repeat until we reach min_level | |
259 | while(1) | |
260 | { | |
261 | assert(_mip_map[level].data); | |
262 | ||
263 | const int level_scale_power = | |
264 | (level + 1) * MipMapScalePower; | |
265 | const uint64_t offset = index >> level_scale_power; | |
266 | assert(offset >= 0); | |
267 | ||
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)) | |
273 | { | |
274 | // Zoom in unless we reached the minimum zoom | |
275 | if(level == min_level) | |
276 | break; | |
277 | ||
278 | level--; | |
279 | } | |
280 | else | |
281 | { | |
282 | // Slide right to the beginning of the next mip map block | |
283 | index = pow2_ceil(index, level_scale_power); | |
284 | } | |
285 | } | |
286 | ||
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) | |
290 | { | |
291 | for(index; index < end; index++) | |
292 | { | |
293 | const bool sample = | |
294 | (get_sample(index) & sig_mask) != 0; | |
295 | if(sample != last_sample) | |
296 | break; | |
297 | } | |
298 | } | |
299 | ||
300 | if(index < end) | |
301 | { | |
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); | |
307 | ||
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)); | |
312 | ||
313 | // Continue to sample | |
314 | index = final_index; | |
315 | last_sample = final_sample; | |
316 | ||
317 | index++; | |
318 | } | |
319 | } | |
320 | ||
321 | // Add the final state | |
322 | edges.push_back(pair<int64_t, bool>(end, | |
323 | get_sample(end) & sig_mask)); | |
324 | } | |
325 | ||
326 | int64_t LogicDataSnapshot::pow2_ceil(int64_t x, int power) | |
327 | { | |
328 | return ((x >> power) + 1) << power; | |
329 | } |