PulseView  0.3.0
A Qt-based sigrok GUI
logicsegment.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the PulseView 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 <stdlib.h>
26 #include <cmath>
27 
28 #include "logicsegment.hpp"
29 
30 #include <libsigrokcxx/libsigrokcxx.hpp>
31 
32 using std::lock_guard;
33 using std::recursive_mutex;
34 using std::max;
35 using std::min;
36 using std::pair;
37 using std::shared_ptr;
38 
39 using sigrok::Logic;
40 
41 namespace pv {
42 namespace data {
43 
44 const int LogicSegment::MipMapScalePower = 4;
45 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
46 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
47 const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
48 
49 LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate,
50  const uint64_t expected_num_samples) :
51  Segment(samplerate, logic->unit_size()),
52  last_append_sample_(0)
53 {
54  set_capacity(expected_num_samples);
55 
56  lock_guard<recursive_mutex> lock(mutex_);
57  memset(mip_map_, 0, sizeof(mip_map_));
58  append_payload(logic);
59 }
60 
62 {
63  lock_guard<recursive_mutex> lock(mutex_);
64  for (MipMapLevel &l : mip_map_)
65  free(l.data);
66 }
67 
68 uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
69 {
70 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
71  return *(uint64_t*)ptr;
72 #else
73  uint64_t value = 0;
74  switch (unit_size_) {
75  default:
76  value |= ((uint64_t)ptr[7]) << 56;
77  /* FALLTHRU */
78  case 7:
79  value |= ((uint64_t)ptr[6]) << 48;
80  /* FALLTHRU */
81  case 6:
82  value |= ((uint64_t)ptr[5]) << 40;
83  /* FALLTHRU */
84  case 5:
85  value |= ((uint64_t)ptr[4]) << 32;
86  /* FALLTHRU */
87  case 4:
88  value |= ((uint32_t)ptr[3]) << 24;
89  /* FALLTHRU */
90  case 3:
91  value |= ((uint32_t)ptr[2]) << 16;
92  /* FALLTHRU */
93  case 2:
94  value |= ptr[1] << 8;
95  /* FALLTHRU */
96  case 1:
97  value |= ptr[0];
98  /* FALLTHRU */
99  case 0:
100  break;
101  }
102  return value;
103 #endif
104 }
105 
106 void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
107 {
108 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
109  *(uint64_t*)ptr = value;
110 #else
111  switch (unit_size_) {
112  default:
113  ptr[7] = value >> 56;
114  /* FALLTHRU */
115  case 7:
116  ptr[6] = value >> 48;
117  /* FALLTHRU */
118  case 6:
119  ptr[5] = value >> 40;
120  /* FALLTHRU */
121  case 5:
122  ptr[4] = value >> 32;
123  /* FALLTHRU */
124  case 4:
125  ptr[3] = value >> 24;
126  /* FALLTHRU */
127  case 3:
128  ptr[2] = value >> 16;
129  /* FALLTHRU */
130  case 2:
131  ptr[1] = value >> 8;
132  /* FALLTHRU */
133  case 1:
134  ptr[0] = value;
135  /* FALLTHRU */
136  case 0:
137  break;
138  }
139 #endif
140 }
141 
142 void LogicSegment::append_payload(shared_ptr<Logic> logic)
143 {
144  assert(unit_size_ == logic->unit_size());
145  assert((logic->data_length() % unit_size_) == 0);
146 
147  lock_guard<recursive_mutex> lock(mutex_);
148 
149  append_data(logic->data_pointer(),
150  logic->data_length() / unit_size_);
151 
152  // Generate the first mip-map from the data
154 }
155 
156 void LogicSegment::get_samples(uint8_t *const data,
157  int64_t start_sample, int64_t end_sample) const
158 {
159  assert(data);
160  assert(start_sample >= 0);
161  assert(start_sample <= (int64_t)sample_count_);
162  assert(end_sample >= 0);
163  assert(end_sample <= (int64_t)sample_count_);
164  assert(start_sample <= end_sample);
165 
166  lock_guard<recursive_mutex> lock(mutex_);
167 
168  const size_t size = (end_sample - start_sample) * unit_size_;
169  memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
170 }
171 
173 {
174  const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
176  if (new_data_length > m.data_length) {
177  m.data_length = new_data_length;
178 
179  // Padding is added to allow for the uint64_t write word
180  m.data = realloc(m.data, new_data_length * unit_size_ +
181  sizeof(uint64_t));
182  }
183 }
184 
186 {
187  MipMapLevel &m0 = mip_map_[0];
188  uint64_t prev_length;
189  const uint8_t *src_ptr;
190  uint8_t *dest_ptr;
191  uint64_t accumulator;
192  unsigned int diff_counter;
193 
194  // Expand the data buffer to fit the new samples
195  prev_length = m0.length;
197 
198  // Break off if there are no new samples to compute
199  if (m0.length == prev_length)
200  return;
201 
203 
204  dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
205 
206  // Iterate through the samples to populate the first level mipmap
207  const uint8_t *const end_src_ptr = (uint8_t*)data_.data() +
209  for (src_ptr = (uint8_t*)data_.data() +
210  prev_length * unit_size_ * MipMapScaleFactor;
211  src_ptr < end_src_ptr;) {
212  // Accumulate transitions which have occurred in this sample
213  accumulator = 0;
214  diff_counter = MipMapScaleFactor;
215  while (diff_counter-- > 0) {
216  const uint64_t sample = unpack_sample(src_ptr);
217  accumulator |= last_append_sample_ ^ sample;
218  last_append_sample_ = sample;
219  src_ptr += unit_size_;
220  }
221 
222  pack_sample(dest_ptr, accumulator);
223  dest_ptr += unit_size_;
224  }
225 
226  // Compute higher level mipmaps
227  for (unsigned int level = 1; level < ScaleStepCount; level++) {
228  MipMapLevel &m = mip_map_[level];
229  const MipMapLevel &ml = mip_map_[level-1];
230 
231  // Expand the data buffer to fit the new samples
232  prev_length = m.length;
234 
235  // Break off if there are no more samples to computed
236  if (m.length == prev_length)
237  break;
238 
240 
241  // Subsample the level lower level
242  src_ptr = (uint8_t*)ml.data +
243  unit_size_ * prev_length * MipMapScaleFactor;
244  const uint8_t *const end_dest_ptr =
245  (uint8_t*)m.data + unit_size_ * m.length;
246  for (dest_ptr = (uint8_t*)m.data +
247  unit_size_ * prev_length;
248  dest_ptr < end_dest_ptr;
249  dest_ptr += unit_size_) {
250  accumulator = 0;
251  diff_counter = MipMapScaleFactor;
252  while (diff_counter-- > 0) {
253  accumulator |= unpack_sample(src_ptr);
254  src_ptr += unit_size_;
255  }
256 
257  pack_sample(dest_ptr, accumulator);
258  }
259  }
260 }
261 
262 uint64_t LogicSegment::get_sample(uint64_t index) const
263 {
264  assert(index < sample_count_);
265 
266  return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
267 }
268 
270  std::vector<EdgePair> &edges,
271  uint64_t start, uint64_t end,
272  float min_length, int sig_index)
273 {
274  uint64_t index = start;
275  unsigned int level;
276  bool last_sample;
277  bool fast_forward;
278 
279  assert(end <= get_sample_count());
280  assert(start <= end);
281  assert(min_length > 0);
282  assert(sig_index >= 0);
283  assert(sig_index < 64);
284 
285  lock_guard<recursive_mutex> lock(mutex_);
286 
287  const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
288  const unsigned int min_level = max((int)floorf(logf(min_length) /
289  LogMipMapScaleFactor) - 1, 0);
290  const uint64_t sig_mask = 1ULL << sig_index;
291 
292  // Store the initial state
293  last_sample = (get_sample(start) & sig_mask) != 0;
294  edges.push_back(pair<int64_t, bool>(index++, last_sample));
295 
296  while (index + block_length <= end) {
297  //----- Continue to search -----//
298  level = min_level;
299 
300  // We cannot fast-forward if there is no mip-map data at
301  // at the minimum level.
302  fast_forward = (mip_map_[level].data != nullptr);
303 
304  if (min_length < MipMapScaleFactor) {
305  // Search individual samples up to the beginning of
306  // the next first level mip map block
307  const uint64_t final_index = min(end,
308  pow2_ceil(index, MipMapScalePower));
309 
310  for (; index < final_index &&
311  (index & ~(~0 << MipMapScalePower)) != 0;
312  index++) {
313  const bool sample =
314  (get_sample(index) & sig_mask) != 0;
315 
316  // If there was a change we cannot fast forward
317  if (sample != last_sample) {
318  fast_forward = false;
319  break;
320  }
321  }
322  } else {
323  // If resolution is less than a mip map block,
324  // round up to the beginning of the mip-map block
325  // for this level of detail
326  const int min_level_scale_power =
327  (level + 1) * MipMapScalePower;
328  index = pow2_ceil(index, min_level_scale_power);
329  if (index >= end)
330  break;
331 
332  // We can fast forward only if there was no change
333  const bool sample =
334  (get_sample(index) & sig_mask) != 0;
335  if (last_sample != sample)
336  fast_forward = false;
337  }
338 
339  if (fast_forward) {
340 
341  // Fast forward: This involves zooming out to higher
342  // levels of the mip map searching for changes, then
343  // zooming in on them to find the point where the edge
344  // begins.
345 
346  // Slide right and zoom out at the beginnings of mip-map
347  // blocks until we encounter a change
348  while (1) {
349  const int level_scale_power =
350  (level + 1) * MipMapScalePower;
351  const uint64_t offset =
352  index >> level_scale_power;
353 
354  // Check if we reached the last block at this
355  // level, or if there was a change in this block
356  if (offset >= mip_map_[level].length ||
357  (get_subsample(level, offset) &
358  sig_mask))
359  break;
360 
361  if ((offset & ~(~0 << MipMapScalePower)) == 0) {
362  // If we are now at the beginning of a
363  // higher level mip-map block ascend one
364  // level
365  if (level + 1 >= ScaleStepCount ||
366  !mip_map_[level + 1].data)
367  break;
368 
369  level++;
370  } else {
371  // Slide right to the beginning of the
372  // next mip map block
373  index = pow2_ceil(index + 1,
374  level_scale_power);
375  }
376  }
377 
378  // Zoom in, and slide right until we encounter a change,
379  // and repeat until we reach min_level
380  while (1) {
381  assert(mip_map_[level].data);
382 
383  const int level_scale_power =
384  (level + 1) * MipMapScalePower;
385  const uint64_t offset =
386  index >> level_scale_power;
387 
388  // Check if we reached the last block at this
389  // level, or if there was a change in this block
390  if (offset >= mip_map_[level].length ||
391  (get_subsample(level, offset) &
392  sig_mask)) {
393  // Zoom in unless we reached the minimum
394  // zoom
395  if (level == min_level)
396  break;
397 
398  level--;
399  } else {
400  // Slide right to the beginning of the
401  // next mip map block
402  index = pow2_ceil(index + 1,
403  level_scale_power);
404  }
405  }
406 
407  // If individual samples within the limit of resolution,
408  // do a linear search for the next transition within the
409  // block
410  if (min_length < MipMapScaleFactor) {
411  for (; index < end; index++) {
412  const bool sample = (get_sample(index) &
413  sig_mask) != 0;
414  if (sample != last_sample)
415  break;
416  }
417  }
418  }
419 
420  //----- Store the edge -----//
421 
422  // Take the last sample of the quanization block
423  const int64_t final_index = index + block_length;
424  if (index + block_length > end)
425  break;
426 
427  // Store the final state
428  const bool final_sample =
429  (get_sample(final_index - 1) & sig_mask) != 0;
430  edges.push_back(pair<int64_t, bool>(index, final_sample));
431 
432  index = final_index;
433  last_sample = final_sample;
434  }
435 
436  // Add the final state
437  const bool end_sample = get_sample(end) & sig_mask;
438  if (last_sample != end_sample)
439  edges.push_back(pair<int64_t, bool>(end, end_sample));
440  edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
441 }
442 
443 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
444 {
445  assert(level >= 0);
446  assert(mip_map_[level].data);
447  return unpack_sample((uint8_t*)mip_map_[level].data +
448  unit_size_ * offset);
449 }
450 
451 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
452 {
453  const uint64_t p = 1 << power;
454  return (x + p - 1) / p * p;
455 }
456 
457 } // namespace data
458 } // namespace pv
void reallocate_mipmap_level(MipMapLevel &m)
uint64_t get_sample(uint64_t index) const
static const float LogMipMapScaleFactor
static const uint64_t MipMapDataUnit
static const int MipMapScalePower
uint64_t sample_count_
Definition: segment.hpp:82
void set_capacity(uint64_t new_capacity)
Increase the capacity of the segment.
Definition: segment.cpp:75
std::vector< uint8_t > data_
Definition: segment.hpp:81
void get_subsampled_edges(std::vector< EdgePair > &edges, uint64_t start, uint64_t end, float min_length, int sig_index)
void append_data(void *data, uint64_t samples)
Definition: segment.cpp:93
uint64_t get_subsample(int level, uint64_t offset) const
LogicSegment(std::shared_ptr< sigrok::Logic > logic, uint64_t samplerate, uint64_t expected_num_samples=0)
uint64_t get_sample_count() const
Definition: segment.cpp:49
static const int MipMapScaleFactor
static uint64_t pow2_ceil(uint64_t x, unsigned int power)
struct MipMapLevel mip_map_[ScaleStepCount]
std::recursive_mutex mutex_
Definition: segment.hpp:80
void get_samples(uint8_t *const data, int64_t start_sample, int64_t end_sample) const
void pack_sample(uint8_t *ptr, uint64_t value)
unsigned int unit_size_
Definition: segment.hpp:86
static const unsigned int ScaleStepCount
void append_payload(std::shared_ptr< sigrok::Logic > logic)
uint64_t unpack_sample(const uint8_t *ptr) const