OpenCores
URL https://opencores.org/ocsvn/wbscope/wbscope/trunk

Subversion Repositories wbscope

[/] [wbscope/] [trunk/] [sw/] [scopecls.cpp] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 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 13 dgisselq
                m_holdoff = (v & ((1<<20)-1));
60 12 dgisselq
        } v = (v>>28)&6;
61
        return (v==6);
62
}
63
 
64
void    SCOPE::decode_control(void) {
65
        unsigned        v;
66
 
67
        v = m_fpga->readio(m_addr);
68 13 dgisselq
        printf("\tCNTRL-REG:\t0x%08x\n", v);
69 12 dgisselq
        printf("\t31. RESET:\t%s\n", (v&0x80000000)?"Ongoing":"Complete");
70
        printf("\t30. STOPPED:\t%s\n", (v&0x40000000)?"Yes":"No");
71
        printf("\t29. TRIGGERED:\t%s\n", (v&0x20000000)?"Yes":"No");
72
        printf("\t28. PRIMED:\t%s\n", (v&0x10000000)?"Yes":"No");
73
        printf("\t27. MANUAL:\t%s\n", (v&0x08000000)?"Yes":"No");
74
        printf("\t26. DISABLED:\t%s\n", (v&0x04000000)?"Yes":"No");
75
        printf("\t25. ZERO:\t%s\n", (v&0x02000000)?"Yes":"No");
76
        printf("\tSCOPLEN:\t%08x (%d)\n", m_scoplen, m_scoplen);
77
        printf("\tHOLDOFF:\t%08x\n", (v&0x0fffff));
78
        printf("\tTRIGLOC:\t%d\n", m_scoplen-(v&0x0fffff));
79
}
80
 
81
int     SCOPE::scoplen(void) {
82
        unsigned        v, lgln;
83
 
84
        // If the scope length is zero, then the scope isn't present.
85
        // We use a length of zero here to also represent whether or not we've
86
        // looked up the length by reading from the scope.
87
        if (m_scoplen == 0) {
88
                v = m_fpga->readio(m_addr);
89 13 dgisselq
                m_holdoff = (v & ((1<<20)-1));
90 12 dgisselq
 
91
                // Since the length of the scope memory is a configuration
92
                // parameter internal to the scope, we read it here to find
93
                // out how the scope was configured.
94
                lgln = (v>>20) & 0x1f;
95
 
96
                // If the length is still zero, then there is no scope installed
97
                if (lgln != 0) {
98
                        // Otherwise, the scope length contained in the device
99
                        // control register is the log base 2 of the actual
100
                        // length of what's in the FPGA.  Here, we just convert
101
                        // that to the actual length of the scope.
102
                        m_scoplen = (1<<lgln);
103
                }
104 13 dgisselq
        // else we already know the length of the scope, and don't need to
105 12 dgisselq
        // slow down to read that length from the device a second time.
106
        } return m_scoplen;
107
}
108
 
109
//
110
// rawread
111
//
112
// Read the scope data from the scope.
113
void    SCOPE::rawread(void) {
114
        // If we've already read the data from the scope, then we don't need
115
        // to read it a second time.
116
        if (m_data)
117
                return;
118
 
119
        // Let's get the length of the scope, and check that it is a valid
120
        // length
121
        if (scoplen() <= 4) {
122
                printf("ERR: Scope has less than a minimum length.  Is it truly a scope?\n");
123
                return;
124
        }
125
 
126
        // Now that we know the size of the scopes buffer, let's allocate a
127
        // buffer to hold all this data
128
        m_data = new DEVBUS::BUSW[m_scoplen];
129
 
130
        // There are two means of reading from a DEVBUS interface: The first
131
        // is a vector read, optimized so that the address and read command
132
        // only needs to be sent once.  This is the optimal means.  However,
133
        // if the bus isn't (yet) trustworthy, it may be more reliable to access
134
        // the port by reading one register at a time--hence the second method.
135
        // If the bus works, you'll want to use readz(): read scoplen values
136
        // into the buffer, from the address WBSCOPEDATA, without incrementing
137
        // the address each time (hence the 'z' in readz--for zero increment).
138
        if (m_vector_read) {
139
                m_fpga->readz(m_addr+4, m_scoplen, m_data);
140
        } else {
141
                for(unsigned int i=0; i<m_scoplen; i++)
142
                        m_data[i] = m_fpga->readio(m_addr+4);
143
        }
144
}
145
 
146
void    SCOPE::print(void) {
147 13 dgisselq
        unsigned long addrv = 0, alen;
148
        int     offset;
149 12 dgisselq
 
150
        rawread();
151
 
152 13 dgisselq
        // Count how many values are in our (possibly compressed) buffer.
153
        // If it weren't for the compression, this'd be m_scoplen
154
        alen = getaddresslen();
155
 
156
        // If the holdoff is zero, the triggered item is the very
157
        // last one.
158
        offset = alen - m_holdoff -1;
159
 
160 12 dgisselq
        if(m_compressed) {
161
                for(int i=0; i<(int)m_scoplen; i++) {
162
                        if ((m_data[i]>>31)&1) {
163 13 dgisselq
                                addrv += (m_data[i]&0x7fffffff) + 1;
164 12 dgisselq
                                printf(" ** (+0x%08x = %8d)\n",
165
                                        (m_data[i]&0x07fffffff),
166
                                        (m_data[i]&0x07fffffff));
167
                                continue;
168
                        }
169 13 dgisselq
                        printf("%10ld %08x: ", addrv++, m_data[i]);
170 12 dgisselq
                        decode(m_data[i]);
171 13 dgisselq
                        if ((int)addrv == offset)
172
                                printf(" <--- TRIGGER");
173 12 dgisselq
                        printf("\n");
174
                }
175
        } else {
176
                for(int i=0; i<(int)m_scoplen; i++) {
177
                        if ((i>0)&&(m_data[i] == m_data[i-1])&&(i<(int)(m_scoplen-1))) {
178
                                if ((i>2)&&(m_data[i] != m_data[i-2]))
179
                                        printf(" **** ****\n");
180
                                continue;
181
                        } printf("%9d %08x: ", i, m_data[i]);
182
                        decode(m_data[i]);
183 13 dgisselq
 
184
                        if (i == offset)
185
                                printf(" <--- TRIGGER");
186 12 dgisselq
                        printf("\n");
187
                }
188
        }
189
}
190
 
191
void    SCOPE::write_trace_timescale(FILE *fp) {
192 13 dgisselq
        fprintf(fp, "$timescale 1ns $end\n\n");
193 12 dgisselq
}
194
 
195 13 dgisselq
void    SCOPE::write_trace_timezero(FILE *fp, int offset) {
196
        double          dwhen;
197
        long            when_ns;
198
 
199
        dwhen = 1.0/((double)m_clkfreq_hz) * (offset);
200
        when_ns = (unsigned long)(dwhen * 1e9);
201
        fprintf(fp, "$timezero %ld $end\n\n", -when_ns);
202
}
203
 
204 12 dgisselq
// $dumpoff and $dumpon
205 13 dgisselq
void    SCOPE::write_trace_header(FILE *fp, int offset) {
206 12 dgisselq
        time_t  now;
207
 
208
        time(&now);
209
        fprintf(fp, "$version Generated by WBScope $end\n");
210
        fprintf(fp, "$date %s\n $end\n", ctime(&now));
211
        write_trace_timescale(fp);
212 13 dgisselq
        if (offset != 0)
213
                write_trace_timezero(fp, offset);
214 12 dgisselq
 
215
        fprintf(fp, " $scope module WBSCOPE $end\n");
216
        // Print out all of the various values
217 13 dgisselq
        if (m_compressed) {
218
                fprintf(fp, "  $var wire %2d \'R _raw_data [%d:0] $end\n", 31,
219
                        30);
220
        } else {
221
                fprintf(fp, "  $var wire %2d \'C clk $end\n", 1);
222
                fprintf(fp, "  $var wire %2d \'R _raw_data [%d:0] $end\n", 32,
223
                        31);
224
        }
225 12 dgisselq
 
226 13 dgisselq
        // Add in a fake _trigger variable to the VCD file we are producing,
227
        // so we can see when our trigger took place (assuming the holdoff is
228
        // such that it is within the collect)
229
        fprintf(fp, "  $var wire %2d \'T _trigger $end\n", 1);
230
 
231 12 dgisselq
        for(unsigned i=0; i<m_traces.size(); i++) {
232
                TRACEINFO *info = m_traces[i];
233
                fprintf(fp, "  $var wire %2d %s %s",
234
                        info->m_nbits, info->m_key, info->m_name);
235
                if ((info->m_nbits > 0)&&(NULL == strchr(info->m_name, '[')))
236
                        fprintf(fp, "[%d:0] $end\n", info->m_nbits-1);
237
                else
238
                        fprintf(fp, " $end\n");
239
        }
240
 
241
        fprintf(fp, " $upscope $end\n");
242
        fprintf(fp, "$enddefinitions $end\n");
243
}
244
 
245
void    SCOPE::write_binary_trace(FILE *fp, const int nbits, unsigned val,
246
                const char *str) {
247
        if (nbits <= 1) {
248
                fprintf(fp, "%d%s\n", val&1, str);
249
                return;
250
        }
251
        if ((unsigned)nbits < sizeof(val)*8)
252
                val &= ~(-1<<nbits);
253
        fputs("b", fp);
254
        for(int i=0; i<nbits; i++)
255
                fprintf(fp, "%d", (val>>(nbits-1-i))&1);
256
        fprintf(fp, " %s\n", str);
257
}
258
 
259
void    SCOPE::write_binary_trace(FILE *fp, TRACEINFO *info, unsigned value) {
260
        write_binary_trace(fp, info->m_nbits, (value>>info->m_nshift),
261
                info->m_key);
262
}
263
 
264
void    SCOPE::register_trace(const char *name,
265
                unsigned nbits, unsigned shift) {
266
        TRACEINFO       *info = new TRACEINFO;
267
        int     nkey = m_traces.size();
268
 
269
        info->m_name   = name;
270
        info->m_nbits  = nbits;
271
        info->m_nshift = shift;
272
 
273
        info->m_key[0] = 'v';
274
        if (nkey < 26)
275
                info->m_key[1] = 'a'+nkey;
276
        else if (nkey < 26+26)
277
                info->m_key[1] = 'A'+nkey-26;
278
        else // if (nkey < 26+26+10)    // Should never happen
279
                info->m_key[1] = '0'+nkey-26-26;
280
        info->m_key[2] = '\0';
281
        info->m_key[3] = '\0';
282
 
283
        m_traces.push_back(info);
284
}
285
 
286 13 dgisselq
/*
287
 * getaddresslen(void)
288
 *
289
 * Returns the number of items in the scope's buffer.  For the uncompressed
290
 * scope, this is just the size of hte scope.  For the compressed scope ... this
291
 * is a touch longer.
292
 */
293
unsigned        SCOPE::getaddresslen(void) {
294
        // Find the offset to the trigger
295
        if (m_compressed) {
296
                // First, find the overall length
297
                //
298
                // If we are compressed, then *every* item increments
299
                // the address length
300
                unsigned alen = m_scoplen;
301
                //
302
                // Some items increment it more.
303
                for(int i=0; i<(int)m_scoplen; i++) {
304
                        if ((m_data[i]&0x80000000)&&(i!=0))
305
                                alen += m_data[i] & 0x7fffffff;
306
                }
307
 
308
                return alen;
309
        } return m_scoplen;
310
}
311
 
312
/*
313
 * define_traces
314
 *
315
 * This is a user stub.  User programs should define this function.
316
 */
317 12 dgisselq
void    SCOPE::define_traces(void) {}
318
 
319 13 dgisselq
void    SCOPE::writevcd(FILE *fp) {
320
        unsigned        alen;
321
        int     offset = 0;
322
 
323
        if (!m_data)
324
                rawread();
325
 
326
        // If the traces haven't yet been defined, then define them now.
327
        if (m_traces.size()==0)
328
                define_traces();
329
 
330
        // Count how many values are in our (possibly compressed) buffer.
331
        // If it weren't for the compression, this'd be m_scoplen
332
        alen = getaddresslen();
333
 
334
        // If the holdoff is zero, the triggered item is the very
335
        // last one.
336
        offset = alen - m_holdoff -1;
337
 
338
        // Write the file header.
339
        write_trace_header(fp, offset);
340
 
341
        // And split into two paths--one for compressed scopes (wbscopc), and
342
        // the other for the more normal scopes (wbscope).
343
        if(m_compressed) {
344
                // With compressed scopes, you need to track the address
345
                // relative to the beginning.
346
                unsigned long   addrv = 0;
347
                unsigned long   now_ns;
348
                double          dnow;
349
                bool            last_trigger = true;
350
 
351
                // Loop over each data word read from the scope
352
                for(int i=0; i<(int)m_scoplen; i++) {
353
                        // If the high bit is set, the address jumps by more
354
                        // than an increment
355
                        if ((m_data[i]>>31)&1) {
356
                                if (i!=0) {
357
                                        if (last_trigger) {
358
                                                // If the trigger was valid
359
                                                // on the last clock, then we
360
                                                // need to include the change
361
                                                // to drop it.
362
                                                //
363
                                                dnow   = 1.0/((double)m_clkfreq_hz) * (addrv+1);
364
                                                now_ns = (unsigned long)(dnow * 1e9);
365
                                                fprintf(fp, "#%ld\n", now_ns);
366
                                                fprintf(fp, "0\'T\n");
367
                                        }
368
                                        // But ... with nothing to write out.
369
                                        addrv += (m_data[i]&0x7fffffff) + 1;
370
                                } continue;
371
                        }
372
 
373
                        // Produce a line identifying the time associated with
374
                        // this piece of data.
375
                        //
376
                        // dnow is the current time represented as a double
377
                        dnow = 1.0/((double)m_clkfreq_hz) * addrv;
378
                        // Convert to nanoseconds, and to integers.
379
                        now_ns = (unsigned long)(dnow * 1e9);
380
 
381
                        fprintf(fp, "#%ld\n", now_ns);
382
 
383
                        if ((int)(addrv-alen) == offset) {
384
                                fprintf(fp, "1\'T\n");
385
                                last_trigger = true;
386
                        } else if (last_trigger)
387
                                fprintf(fp, "0\'T\n");
388
 
389
                        // For compressed data, only the lower 31 bits are
390
                        // valid.  Write those bits to the VCD file as a raw
391
                        // value.
392
                        write_binary_trace(fp, 31, m_data[i], "\'R\n");
393
 
394
                        // Finally, walk through all of the user defined traces,
395
                        // writing each to the VCD file.
396
                        for(unsigned k=0; k<m_traces.size(); k++) {
397
                                TRACEINFO *info = m_traces[k];
398
                                write_binary_trace(fp, info, m_data[i]);
399
                        }
400
 
401
                        addrv++;
402
                }
403
        } else {
404
                //
405
                // Uncompressed scope.
406
                //
407
                unsigned now_ns;
408
                double  dnow;
409
 
410
                // We assume a clock signal, and set it to one and zero.
411
                // We also assume everything changes on the positive edge of
412
                // that clock within here.
413
 
414
                // Loop over all data words
415
                for(int i=0; i<(int)m_scoplen; i++) {
416
                        // Positive edge of the clock (everything is assumed to
417
                        // be on the positive edge)
418
 
419
 
420
                        //
421
                        // Clock goes high
422
                        //
423
 
424
                        // Write the current (relative) time of this data word
425
                        dnow = 1.0/((double)m_clkfreq_hz) * i;
426
                        now_ns = (unsigned)(dnow * 1e9 + 0.5);
427
                        fprintf(fp, "#%d\n", now_ns);
428
 
429
                        fprintf(fp, "1\'C\n");
430
                        write_binary_trace(fp, (m_compressed)?31:32,
431
                                m_data[i], "\'R\n");
432
 
433
                        if (i == offset)
434
                                fprintf(fp, "1\'T\n");
435
                        else // if (addrv == offset+1)
436
                                fprintf(fp, "0\'T\n");
437
 
438
                        for(unsigned k=0; k<m_traces.size(); k++) {
439
                                TRACEINFO *info = m_traces[k];
440
                                write_binary_trace(fp, info, m_data[i]);
441
                        }
442
 
443
                        //
444
                        // Clock goes to zero
445
                        //
446
 
447
                        // Add half a clock period to our time
448
                        dnow += 1.0/((double)m_clkfreq_hz)/2.;
449
                        now_ns = (unsigned)(dnow * 1e9 + 0.5);
450
                        fprintf(fp, "#%d\n", now_ns);
451
 
452
                        // Now finally write the clock as zero.
453
                        fprintf(fp, "0\'C\n");
454
                }
455
        }
456
}
457
 
458
/*
459
 * writevcd
460
 *
461
 * Main user entry point for VCD file creation.  This just opens a file of the
462
 * given name, and writes the VCD info to it.  If the file cannot be opened,
463
 * an error is written to the standard error stream, and the routine returns.
464
 */
465 12 dgisselq
void    SCOPE::writevcd(const char *trace_file_name) {
466
        FILE    *fp = fopen(trace_file_name, "w");
467
 
468
        if (fp == NULL) {
469
                fprintf(stderr, "ERR: Cannot open %s for writing!\n", trace_file_name);
470
                fprintf(stderr, "ERR: Trace file not written\n");
471
                return;
472
        }
473
 
474 13 dgisselq
        writevcd(fp);
475 12 dgisselq
 
476 13 dgisselq
        fclose(fp);
477 12 dgisselq
}
478
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.