1 |
52 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: scopecls.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: WBScope, a wishbone hosted scope
|
6 |
|
|
//
|
7 |
|
|
// Purpose: After rebuilding the same code over and over again for every
|
8 |
|
|
// "scope" I tried to interact with, I thought it would be simpler
|
9 |
|
|
// to try to make a more generic interface, that other things could plug
|
10 |
|
|
// into. This is that more generic interface.
|
11 |
|
|
//
|
12 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
13 |
|
|
// Gisselquist Technology, LLC
|
14 |
|
|
//
|
15 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
16 |
|
|
//
|
17 |
|
|
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
18 |
|
|
//
|
19 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
20 |
|
|
// modify it under the terms of the GNU General Public License as published
|
21 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
22 |
|
|
// your option) any later version.
|
23 |
|
|
//
|
24 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
25 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
26 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
27 |
|
|
// for more details.
|
28 |
|
|
//
|
29 |
|
|
// You should have received a copy of the GNU General Public License along
|
30 |
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
31 |
|
|
// target there if the PDF file isn't present.) If not, see
|
32 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
33 |
|
|
//
|
34 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
35 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
//
|
40 |
|
|
//
|
41 |
|
|
#include <stdio.h>
|
42 |
|
|
#include <stdlib.h>
|
43 |
|
|
#include <unistd.h>
|
44 |
|
|
#include <strings.h>
|
45 |
|
|
#include <ctype.h>
|
46 |
|
|
#include <string.h>
|
47 |
|
|
#include <signal.h>
|
48 |
|
|
#include <assert.h>
|
49 |
|
|
#include <time.h>
|
50 |
|
|
|
51 |
|
|
#include "devbus.h"
|
52 |
|
|
#include "scopecls.h"
|
53 |
|
|
|
54 |
|
|
bool SCOPE::ready() {
|
55 |
|
|
unsigned v;
|
56 |
|
|
v = m_fpga->readio(m_addr);
|
57 |
|
|
if (m_scoplen == 0) {
|
58 |
|
|
m_scoplen = (1<<((v>>20)&0x01f));
|
59 |
|
|
} v = (v>>28)&6;
|
60 |
|
|
return (v==6);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
void SCOPE::decode_control(void) {
|
64 |
|
|
unsigned v;
|
65 |
|
|
|
66 |
|
|
v = m_fpga->readio(m_addr);
|
67 |
|
|
printf("\t31. RESET:\t%s\n", (v&0x80000000)?"Ongoing":"Complete");
|
68 |
|
|
printf("\t30. STOPPED:\t%s\n", (v&0x40000000)?"Yes":"No");
|
69 |
|
|
printf("\t29. TRIGGERED:\t%s\n", (v&0x20000000)?"Yes":"No");
|
70 |
|
|
printf("\t28. PRIMED:\t%s\n", (v&0x10000000)?"Yes":"No");
|
71 |
|
|
printf("\t27. MANUAL:\t%s\n", (v&0x08000000)?"Yes":"No");
|
72 |
|
|
printf("\t26. DISABLED:\t%s\n", (v&0x04000000)?"Yes":"No");
|
73 |
|
|
printf("\t25. ZERO:\t%s\n", (v&0x02000000)?"Yes":"No");
|
74 |
|
|
printf("\tSCOPLEN:\t%08x (%d)\n", m_scoplen, m_scoplen);
|
75 |
|
|
printf("\tHOLDOFF:\t%08x\n", (v&0x0fffff));
|
76 |
|
|
printf("\tTRIGLOC:\t%d\n", m_scoplen-(v&0x0fffff));
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
int SCOPE::scoplen(void) {
|
80 |
|
|
unsigned v, lgln;
|
81 |
|
|
|
82 |
|
|
// If the scope length is zero, then the scope isn't present.
|
83 |
|
|
// We use a length of zero here to also represent whether or not we've
|
84 |
|
|
// looked up the length by reading from the scope.
|
85 |
|
|
if (m_scoplen == 0) {
|
86 |
|
|
v = m_fpga->readio(m_addr);
|
87 |
|
|
|
88 |
|
|
// Since the length of the scope memory is a configuration
|
89 |
|
|
// parameter internal to the scope, we read it here to find
|
90 |
|
|
// out how the scope was configured.
|
91 |
|
|
lgln = (v>>20) & 0x1f;
|
92 |
|
|
|
93 |
|
|
// If the length is still zero, then there is no scope installed
|
94 |
|
|
if (lgln != 0) {
|
95 |
|
|
// Otherwise, the scope length contained in the device
|
96 |
|
|
// control register is the log base 2 of the actual
|
97 |
|
|
// length of what's in the FPGA. Here, we just convert
|
98 |
|
|
// that to the actual length of the scope.
|
99 |
|
|
m_scoplen = (1<<lgln);
|
100 |
|
|
}
|
101 |
|
|
// else we already know the length of the scope, and don't need to
|
102 |
|
|
// slow down to read that length from the device a second time.
|
103 |
|
|
} return m_scoplen;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
//
|
107 |
|
|
// rawread
|
108 |
|
|
//
|
109 |
|
|
// Read the scope data from the scope.
|
110 |
|
|
void SCOPE::rawread(void) {
|
111 |
|
|
// If we've already read the data from the scope, then we don't need
|
112 |
|
|
// to read it a second time.
|
113 |
|
|
if (m_data)
|
114 |
|
|
return;
|
115 |
|
|
|
116 |
|
|
// Let's get the length of the scope, and check that it is a valid
|
117 |
|
|
// length
|
118 |
|
|
if (scoplen() <= 4) {
|
119 |
|
|
printf("ERR: Scope has less than a minimum length. Is it truly a scope?\n");
|
120 |
|
|
return;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
// Now that we know the size of the scopes buffer, let's allocate a
|
124 |
|
|
// buffer to hold all this data
|
125 |
|
|
m_data = new DEVBUS::BUSW[m_scoplen];
|
126 |
|
|
|
127 |
|
|
// There are two means of reading from a DEVBUS interface: The first
|
128 |
|
|
// is a vector read, optimized so that the address and read command
|
129 |
|
|
// only needs to be sent once. This is the optimal means. However,
|
130 |
|
|
// if the bus isn't (yet) trustworthy, it may be more reliable to access
|
131 |
|
|
// the port by reading one register at a time--hence the second method.
|
132 |
|
|
// If the bus works, you'll want to use readz(): read scoplen values
|
133 |
|
|
// into the buffer, from the address WBSCOPEDATA, without incrementing
|
134 |
|
|
// the address each time (hence the 'z' in readz--for zero increment).
|
135 |
|
|
if (m_vector_read) {
|
136 |
|
|
m_fpga->readz(m_addr+4, m_scoplen, m_data);
|
137 |
|
|
} else {
|
138 |
|
|
for(unsigned int i=0; i<m_scoplen; i++)
|
139 |
|
|
m_data[i] = m_fpga->readio(m_addr+4);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
void SCOPE::print(void) {
|
144 |
|
|
DEVBUS::BUSW addrv = 0;
|
145 |
|
|
|
146 |
|
|
rawread();
|
147 |
|
|
|
148 |
|
|
if(m_compressed) {
|
149 |
|
|
for(int i=0; i<(int)m_scoplen; i++) {
|
150 |
|
|
if ((m_data[i]>>31)&1) {
|
151 |
|
|
addrv += (m_data[i]&0x7fffffff);
|
152 |
|
|
printf(" ** (+0x%08x = %8d)\n",
|
153 |
|
|
(m_data[i]&0x07fffffff),
|
154 |
|
|
(m_data[i]&0x07fffffff));
|
155 |
|
|
continue;
|
156 |
|
|
}
|
157 |
|
|
printf("%10d %08x: ", addrv++, m_data[i]);
|
158 |
|
|
decode(m_data[i]);
|
159 |
|
|
printf("\n");
|
160 |
|
|
}
|
161 |
|
|
} else {
|
162 |
|
|
for(int i=0; i<(int)m_scoplen; i++) {
|
163 |
|
|
if ((i>0)&&(m_data[i] == m_data[i-1])&&(i<(int)(m_scoplen-1))) {
|
164 |
|
|
if ((i>2)&&(m_data[i] != m_data[i-2]))
|
165 |
|
|
printf(" **** ****\n");
|
166 |
|
|
continue;
|
167 |
|
|
} printf("%9d %08x: ", i, m_data[i]);
|
168 |
|
|
decode(m_data[i]);
|
169 |
|
|
printf("\n");
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
void SCOPE::write_trace_timescale(FILE *fp) {
|
175 |
|
|
fprintf(fp, "$timescle 1ns $end\n\n");
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
// $dumpoff and $dumpon
|
179 |
|
|
void SCOPE::write_trace_header(FILE *fp) {
|
180 |
|
|
time_t now;
|
181 |
|
|
|
182 |
|
|
time(&now);
|
183 |
|
|
fprintf(fp, "$version Generated by WBScope $end\n");
|
184 |
|
|
fprintf(fp, "$date %s\n $end\n", ctime(&now));
|
185 |
|
|
write_trace_timescale(fp);
|
186 |
|
|
|
187 |
|
|
fprintf(fp, " $scope module WBSCOPE $end\n");
|
188 |
|
|
// Print out all of the various values
|
189 |
|
|
fprintf(fp, " $var wire %2d \'C clk $end\n", 1);
|
190 |
|
|
fprintf(fp, " $var wire %2d \'R _raw_data [%d:0] $end\n",
|
191 |
|
|
(m_compressed)?31:32,
|
192 |
|
|
(m_compressed)?30:31);
|
193 |
|
|
|
194 |
|
|
for(unsigned i=0; i<m_traces.size(); i++) {
|
195 |
|
|
TRACEINFO *info = m_traces[i];
|
196 |
|
|
fprintf(fp, " $var wire %2d %s %s",
|
197 |
|
|
info->m_nbits, info->m_key, info->m_name);
|
198 |
|
|
if ((info->m_nbits > 0)&&(NULL == strchr(info->m_name, '[')))
|
199 |
|
|
fprintf(fp, "[%d:0] $end\n", info->m_nbits-1);
|
200 |
|
|
else
|
201 |
|
|
fprintf(fp, " $end\n");
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
fprintf(fp, " $upscope $end\n");
|
205 |
|
|
fprintf(fp, "$enddefinitions $end\n");
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
void SCOPE::write_binary_trace(FILE *fp, const int nbits, unsigned val,
|
209 |
|
|
const char *str) {
|
210 |
|
|
if (nbits <= 1) {
|
211 |
|
|
fprintf(fp, "%d%s\n", val&1, str);
|
212 |
|
|
return;
|
213 |
|
|
}
|
214 |
|
|
if ((unsigned)nbits < sizeof(val)*8)
|
215 |
|
|
val &= ~(-1<<nbits);
|
216 |
|
|
fputs("b", fp);
|
217 |
|
|
for(int i=0; i<nbits; i++)
|
218 |
|
|
fprintf(fp, "%d", (val>>(nbits-1-i))&1);
|
219 |
|
|
fprintf(fp, " %s\n", str);
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
void SCOPE::write_binary_trace(FILE *fp, TRACEINFO *info, unsigned value) {
|
223 |
|
|
write_binary_trace(fp, info->m_nbits, (value>>info->m_nshift),
|
224 |
|
|
info->m_key);
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
void SCOPE::register_trace(const char *name,
|
228 |
|
|
unsigned nbits, unsigned shift) {
|
229 |
|
|
TRACEINFO *info = new TRACEINFO;
|
230 |
|
|
int nkey = m_traces.size();
|
231 |
|
|
|
232 |
|
|
info->m_name = name;
|
233 |
|
|
info->m_nbits = nbits;
|
234 |
|
|
info->m_nshift = shift;
|
235 |
|
|
|
236 |
|
|
info->m_key[0] = 'v';
|
237 |
|
|
if (nkey < 26)
|
238 |
|
|
info->m_key[1] = 'a'+nkey;
|
239 |
|
|
else if (nkey < 26+26)
|
240 |
|
|
info->m_key[1] = 'A'+nkey-26;
|
241 |
|
|
else // if (nkey < 26+26+10) // Should never happen
|
242 |
|
|
info->m_key[1] = '0'+nkey-26-26;
|
243 |
|
|
info->m_key[2] = '\0';
|
244 |
|
|
info->m_key[3] = '\0';
|
245 |
|
|
|
246 |
|
|
m_traces.push_back(info);
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
void SCOPE::define_traces(void) {}
|
250 |
|
|
|
251 |
|
|
void SCOPE::writevcd(const char *trace_file_name) {
|
252 |
|
|
FILE *fp = fopen(trace_file_name, "w");
|
253 |
|
|
|
254 |
|
|
if (fp == NULL) {
|
255 |
|
|
fprintf(stderr, "ERR: Cannot open %s for writing!\n", trace_file_name);
|
256 |
|
|
fprintf(stderr, "ERR: Trace file not written\n");
|
257 |
|
|
return;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
if (!m_data)
|
261 |
|
|
rawread();
|
262 |
|
|
|
263 |
|
|
write_trace_header(fp);
|
264 |
|
|
|
265 |
|
|
for(int i=0; i<(int)m_scoplen; i++) {
|
266 |
|
|
// Positive edge of the clock (everything is assumed to
|
267 |
|
|
// be on the positive edge)
|
268 |
|
|
fprintf(fp, "#%d\n", i * 10);
|
269 |
|
|
fprintf(fp, "1\'C\n");
|
270 |
|
|
write_binary_trace(fp, (m_compressed)?31:32, m_data[i], "\'R");
|
271 |
|
|
|
272 |
|
|
for(unsigned k=0; k<m_traces.size(); k++) {
|
273 |
|
|
TRACEINFO *info = m_traces[k];
|
274 |
|
|
write_binary_trace(fp, info, m_data[i]);
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
// Clock goes to zero
|
278 |
|
|
fprintf(fp, "#%d\n", i * 10 + 5);
|
279 |
|
|
fprintf(fp, "0\'C\n");
|
280 |
|
|
}
|
281 |
|
|
}
|
282 |
|
|
|