]>
Commit | Line | Data |
---|---|---|
28a4c9c5 JH |
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 | ||
4ceab49a | 21 | #include "extdef.h" |
28a4c9c5 | 22 | |
f556bc6a | 23 | #include <assert.h> |
4ceab49a JH |
24 | #include <string.h> |
25 | #include <math.h> | |
26 | ||
27 | #include <boost/foreach.hpp> | |
28 | ||
29 | #include "logicdatasnapshot.h" | |
f556bc6a | 30 | |
2858b391 | 31 | using namespace std; |
28a4c9c5 | 32 | |
4ceab49a JH |
33 | const int LogicDataSnapshot::MipMapScalePower = 4; |
34 | const int LogicDataSnapshot::MipMapScaleFactor = 1 << MipMapScalePower; | |
35 | const uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024; // bytes | |
36 | ||
f556bc6a JH |
37 | LogicDataSnapshot::LogicDataSnapshot( |
38 | const sr_datafeed_logic &logic) : | |
4ceab49a JH |
39 | DataSnapshot(logic.unitsize), |
40 | _last_append_sample(0) | |
f556bc6a | 41 | { |
4ceab49a | 42 | memset(_mip_map, 0, sizeof(_mip_map)); |
f556bc6a JH |
43 | append_payload(logic); |
44 | } | |
45 | ||
4ceab49a JH |
46 | LogicDataSnapshot::~LogicDataSnapshot() |
47 | { | |
48 | BOOST_FOREACH(MipMapLevel &l, _mip_map) | |
49 | free(l.data); | |
50 | } | |
51 | ||
28a4c9c5 JH |
52 | void LogicDataSnapshot::append_payload( |
53 | const sr_datafeed_logic &logic) | |
54 | { | |
f556bc6a JH |
55 | assert(_unit_size == logic.unitsize); |
56 | ||
4ceab49a | 57 | const uint64_t prev_length = _data_length; |
f556bc6a | 58 | append_data(logic.data, logic.length); |
4ceab49a JH |
59 | |
60 | // Generate the first mip-map from the data | |
61 | append_payload_to_mipmap(); | |
62 | } | |
63 | ||
64 | void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m) | |
65 | { | |
66 | const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) / | |
67 | MipMapDataUnit) * MipMapDataUnit; | |
68 | if(new_data_length > m.data_length) | |
69 | { | |
70 | m.data_length = new_data_length; | |
71 | m.data = realloc(m.data, new_data_length * _unit_size); | |
72 | } | |
73 | } | |
74 | ||
75 | void LogicDataSnapshot::append_payload_to_mipmap() | |
76 | { | |
77 | MipMapLevel &m0 = _mip_map[0]; | |
78 | uint64_t prev_length; | |
79 | const uint8_t *src_ptr; | |
80 | uint8_t *dest_ptr; | |
81 | uint64_t accumulator; | |
82 | unsigned int diff_counter; | |
83 | ||
84 | // Expand the data buffer to fit the new samples | |
85 | prev_length = m0.length; | |
86 | m0.length = _data_length / MipMapScaleFactor; | |
87 | ||
88 | // Break off if there are no new samples to compute | |
89 | if(m0.length == prev_length) | |
90 | return; | |
91 | ||
92 | reallocate_mip_map(m0); | |
93 | ||
94 | dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size; | |
95 | ||
96 | // Iterate through the samples to populate the first level mipmap | |
97 | accumulator = 0; | |
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;) | |
104 | { | |
105 | // Accumulate transitions which have occurred in this sample | |
106 | accumulator = 0; | |
107 | diff_counter = MipMapScaleFactor; | |
108 | while(diff_counter-- > 0) | |
109 | { | |
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; | |
114 | } | |
115 | ||
116 | *(uint64_t*)dest_ptr = accumulator; | |
117 | dest_ptr += _unit_size; | |
118 | } | |
119 | ||
120 | // Compute higher level mipmaps | |
121 | for(int level = 1; level < ScaleStepCount; level++) | |
122 | { | |
123 | MipMapLevel &m = _mip_map[level]; | |
124 | const MipMapLevel &ml = _mip_map[level-1]; | |
125 | ||
126 | // Expand the data buffer to fit the new samples | |
127 | prev_length = m.length; | |
128 | m.length = ml.length / MipMapScaleFactor; | |
129 | ||
130 | // Break off if there are no more samples to computed | |
131 | if(m.length == prev_length) | |
132 | break; | |
133 | ||
134 | reallocate_mip_map(m); | |
135 | ||
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) | |
145 | { | |
146 | accumulator = 0; | |
147 | diff_counter = MipMapScaleFactor; | |
148 | while(diff_counter-- > 0) | |
149 | { | |
150 | accumulator |= *(uint64_t*)src_ptr; | |
151 | src_ptr += _unit_size; | |
152 | } | |
153 | ||
154 | *(uint64_t*)dest_ptr = accumulator; | |
155 | } | |
156 | } | |
28a4c9c5 | 157 | } |
2858b391 JH |
158 | |
159 | uint64_t LogicDataSnapshot::get_sample(uint64_t index) const | |
160 | { | |
161 | assert(_data); | |
162 | assert(index >= 0 && index < _data_length); | |
163 | ||
164 | return *(uint64_t*)((uint8_t*)_data + index * _unit_size); | |
165 | } | |
166 | ||
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) | |
171 | { | |
172 | assert(start >= 0); | |
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); | |
178 | ||
179 | const uint64_t sig_mask = 1 << sig_index; | |
180 | ||
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)); | |
184 | ||
652010ea | 185 | for(int64_t i = start + 1; i < end; i++) |
2858b391 JH |
186 | { |
187 | const bool sample = get_sample(i) & sig_mask; | |
188 | ||
189 | // Check if we hit an edge | |
190 | if(sample != last_sample) | |
191 | { | |
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); | |
196 | ||
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)); | |
201 | ||
202 | // Continue to sampling | |
203 | i = final_index; | |
204 | last_sample = final_sample; | |
205 | } | |
206 | } | |
207 | ||
208 | // Add the final state | |
652010ea JH |
209 | edges.push_back(pair<int64_t, bool>(end, |
210 | get_sample(end) & sig_mask)); | |
2858b391 | 211 | } |