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 uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024; // bytes
37 LogicDataSnapshot::LogicDataSnapshot(
38 const sr_datafeed_logic &logic) :
39 DataSnapshot(logic.unitsize),
40 _last_append_sample(0)
42 memset(_mip_map, 0, sizeof(_mip_map));
43 append_payload(logic);
46 LogicDataSnapshot::~LogicDataSnapshot()
48 BOOST_FOREACH(MipMapLevel &l, _mip_map)
52 void LogicDataSnapshot::append_payload(
53 const sr_datafeed_logic &logic)
55 assert(_unit_size == logic.unitsize);
57 const uint64_t prev_length = _data_length;
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 = _data_length / 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 < _data_length);
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 int64_t quantization_length, int sig_index)
173 assert(end < get_sample_count());
174 assert(start <= end);
175 assert(quantization_length > 0);
176 assert(sig_index >= 0);
177 assert(sig_index < SR_MAX_NUM_PROBES);
179 const uint64_t sig_mask = 1 << sig_index;
181 // Add the initial state
182 bool last_sample = get_sample(start) & sig_mask;
183 edges.push_back(pair<int64_t, bool>(start, last_sample));
185 for(int64_t i = start + 1; i < end; i++)
187 const bool sample = get_sample(i) & sig_mask;
189 // Check if we hit an edge
190 if(sample != last_sample)
192 // Take the last sample of the quanization block
193 const int64_t final_index =
194 min((i - (i % quantization_length) +
195 quantization_length - 1), end);
197 // Store the final state
198 const bool final_sample = get_sample(final_index) & sig_mask;
199 edges.push_back(pair<int64_t, bool>(
200 final_index, final_sample));
202 // Continue to sampling
204 last_sample = final_sample;
208 // Add the final state
209 edges.push_back(pair<int64_t, bool>(end,
210 get_sample(end) & sig_mask));