2 * This file is part of the PulseView 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, see <http://www.gnu.org/licenses/>.
27 #include "logicsegment.hpp"
29 #include <libsigrokcxx/libsigrokcxx.hpp>
31 using std::lock_guard;
32 using std::recursive_mutex;
36 using std::shared_ptr;
43 const int LogicSegment::MipMapScalePower = 4;
44 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
45 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
46 const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
48 LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate,
49 const uint64_t expected_num_samples) :
50 Segment(samplerate, logic->unit_size()),
51 last_append_sample_(0)
53 set_capacity(expected_num_samples);
55 lock_guard<recursive_mutex> lock(mutex_);
56 memset(mip_map_, 0, sizeof(mip_map_));
57 append_payload(logic);
60 LogicSegment::~LogicSegment()
62 lock_guard<recursive_mutex> lock(mutex_);
63 for (MipMapLevel &l : mip_map_)
67 uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
69 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
70 return *(uint64_t*)ptr;
75 value |= ((uint64_t)ptr[7]) << 56;
78 value |= ((uint64_t)ptr[6]) << 48;
81 value |= ((uint64_t)ptr[5]) << 40;
84 value |= ((uint64_t)ptr[4]) << 32;
87 value |= ((uint32_t)ptr[3]) << 24;
90 value |= ((uint32_t)ptr[2]) << 16;
105 void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
107 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
108 *(uint64_t*)ptr = value;
110 switch (unit_size_) {
112 ptr[7] = value >> 56;
115 ptr[6] = value >> 48;
118 ptr[5] = value >> 40;
121 ptr[4] = value >> 32;
124 ptr[3] = value >> 24;
127 ptr[2] = value >> 16;
141 void LogicSegment::append_payload(shared_ptr<Logic> logic)
143 assert(unit_size_ == logic->unit_size());
144 assert((logic->data_length() % unit_size_) == 0);
146 lock_guard<recursive_mutex> lock(mutex_);
148 append_data(logic->data_pointer(),
149 logic->data_length() / unit_size_);
151 // Generate the first mip-map from the data
152 append_payload_to_mipmap();
155 void LogicSegment::get_samples(uint8_t *const data,
156 int64_t start_sample, int64_t end_sample) const
159 assert(start_sample >= 0);
160 assert(start_sample <= (int64_t)sample_count_);
161 assert(end_sample >= 0);
162 assert(end_sample <= (int64_t)sample_count_);
163 assert(start_sample <= end_sample);
165 lock_guard<recursive_mutex> lock(mutex_);
167 const size_t size = (end_sample - start_sample) * unit_size_;
168 memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
171 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
173 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
174 MipMapDataUnit) * MipMapDataUnit;
175 if (new_data_length > m.data_length) {
176 m.data_length = new_data_length;
178 // Padding is added to allow for the uint64_t write word
179 m.data = realloc(m.data, new_data_length * unit_size_ +
184 void LogicSegment::append_payload_to_mipmap()
186 MipMapLevel &m0 = mip_map_[0];
187 uint64_t prev_length;
188 const uint8_t *src_ptr;
190 uint64_t accumulator;
191 unsigned int diff_counter;
193 // Expand the data buffer to fit the new samples
194 prev_length = m0.length;
195 m0.length = sample_count_ / MipMapScaleFactor;
197 // Break off if there are no new samples to compute
198 if (m0.length == prev_length)
201 reallocate_mipmap_level(m0);
203 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
205 // Iterate through the samples to populate the first level mipmap
206 const uint8_t *const end_src_ptr = (uint8_t*)data_.data() +
207 m0.length * unit_size_ * MipMapScaleFactor;
208 for (src_ptr = (uint8_t*)data_.data() +
209 prev_length * unit_size_ * MipMapScaleFactor;
210 src_ptr < end_src_ptr;) {
211 // Accumulate transitions which have occurred in this sample
213 diff_counter = MipMapScaleFactor;
214 while (diff_counter-- > 0) {
215 const uint64_t sample = unpack_sample(src_ptr);
216 accumulator |= last_append_sample_ ^ sample;
217 last_append_sample_ = sample;
218 src_ptr += unit_size_;
221 pack_sample(dest_ptr, accumulator);
222 dest_ptr += unit_size_;
225 // Compute higher level mipmaps
226 for (unsigned int level = 1; level < ScaleStepCount; level++) {
227 MipMapLevel &m = mip_map_[level];
228 const MipMapLevel &ml = mip_map_[level-1];
230 // Expand the data buffer to fit the new samples
231 prev_length = m.length;
232 m.length = ml.length / MipMapScaleFactor;
234 // Break off if there are no more samples to computed
235 if (m.length == prev_length)
238 reallocate_mipmap_level(m);
240 // Subsample the level lower level
241 src_ptr = (uint8_t*)ml.data +
242 unit_size_ * prev_length * MipMapScaleFactor;
243 const uint8_t *const end_dest_ptr =
244 (uint8_t*)m.data + unit_size_ * m.length;
245 for (dest_ptr = (uint8_t*)m.data +
246 unit_size_ * prev_length;
247 dest_ptr < end_dest_ptr;
248 dest_ptr += unit_size_) {
250 diff_counter = MipMapScaleFactor;
251 while (diff_counter-- > 0) {
252 accumulator |= unpack_sample(src_ptr);
253 src_ptr += unit_size_;
256 pack_sample(dest_ptr, accumulator);
261 uint64_t LogicSegment::get_sample(uint64_t index) const
263 assert(index < sample_count_);
265 return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
268 void LogicSegment::get_subsampled_edges(
269 std::vector<EdgePair> &edges,
270 uint64_t start, uint64_t end,
271 float min_length, int sig_index)
273 uint64_t index = start;
278 assert(end <= get_sample_count());
279 assert(start <= end);
280 assert(min_length > 0);
281 assert(sig_index >= 0);
282 assert(sig_index < 64);
284 lock_guard<recursive_mutex> lock(mutex_);
286 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
287 const unsigned int min_level = max((int)floorf(logf(min_length) /
288 LogMipMapScaleFactor) - 1, 0);
289 const uint64_t sig_mask = 1ULL << sig_index;
291 // Store the initial state
292 last_sample = (get_sample(start) & sig_mask) != 0;
293 edges.push_back(pair<int64_t, bool>(index++, last_sample));
295 while (index + block_length <= end) {
296 //----- Continue to search -----//
299 // We cannot fast-forward if there is no mip-map data at
300 // at the minimum level.
301 fast_forward = (mip_map_[level].data != nullptr);
303 if (min_length < MipMapScaleFactor) {
304 // Search individual samples up to the beginning of
305 // the next first level mip map block
306 const uint64_t final_index = min(end,
307 pow2_ceil(index, MipMapScalePower));
309 for (; index < final_index &&
310 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
313 (get_sample(index) & sig_mask) != 0;
315 // If there was a change we cannot fast forward
316 if (sample != last_sample) {
317 fast_forward = false;
322 // If resolution is less than a mip map block,
323 // round up to the beginning of the mip-map block
324 // for this level of detail
325 const int min_level_scale_power =
326 (level + 1) * MipMapScalePower;
327 index = pow2_ceil(index, min_level_scale_power);
331 // We can fast forward only if there was no change
333 (get_sample(index) & sig_mask) != 0;
334 if (last_sample != sample)
335 fast_forward = false;
340 // Fast forward: This involves zooming out to higher
341 // levels of the mip map searching for changes, then
342 // zooming in on them to find the point where the edge
345 // Slide right and zoom out at the beginnings of mip-map
346 // blocks until we encounter a change
348 const int level_scale_power =
349 (level + 1) * MipMapScalePower;
350 const uint64_t offset =
351 index >> level_scale_power;
353 // Check if we reached the last block at this
354 // level, or if there was a change in this block
355 if (offset >= mip_map_[level].length ||
356 (get_subsample(level, offset) &
360 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
361 // If we are now at the beginning of a
362 // higher level mip-map block ascend one
364 if (level + 1 >= ScaleStepCount ||
365 !mip_map_[level + 1].data)
370 // Slide right to the beginning of the
371 // next mip map block
372 index = pow2_ceil(index + 1,
377 // Zoom in, and slide right until we encounter a change,
378 // and repeat until we reach min_level
380 assert(mip_map_[level].data);
382 const int level_scale_power =
383 (level + 1) * MipMapScalePower;
384 const uint64_t offset =
385 index >> level_scale_power;
387 // Check if we reached the last block at this
388 // level, or if there was a change in this block
389 if (offset >= mip_map_[level].length ||
390 (get_subsample(level, offset) &
392 // Zoom in unless we reached the minimum
394 if (level == min_level)
399 // Slide right to the beginning of the
400 // next mip map block
401 index = pow2_ceil(index + 1,
406 // If individual samples within the limit of resolution,
407 // do a linear search for the next transition within the
409 if (min_length < MipMapScaleFactor) {
410 for (; index < end; index++) {
411 const bool sample = (get_sample(index) &
413 if (sample != last_sample)
419 //----- Store the edge -----//
421 // Take the last sample of the quanization block
422 const int64_t final_index = index + block_length;
423 if (index + block_length > end)
426 // Store the final state
427 const bool final_sample =
428 (get_sample(final_index - 1) & sig_mask) != 0;
429 edges.push_back(pair<int64_t, bool>(index, final_sample));
432 last_sample = final_sample;
435 // Add the final state
436 const bool end_sample = get_sample(end) & sig_mask;
437 if (last_sample != end_sample)
438 edges.push_back(pair<int64_t, bool>(end, end_sample));
439 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
442 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
445 assert(mip_map_[level].data);
446 return unpack_sample((uint8_t*)mip_map_[level].data +
447 unit_size_ * offset);
450 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
452 const uint64_t p = 1 << power;
453 return (x + p - 1) / p * p;