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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [wbscope.v] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    wbscope.v
4
//
5 46 dgisselq
// Project:     WBScope, a wishbone hosted scope
6 2 dgisselq
//
7
// Purpose:     This is a generic/library routine for providing a bus accessed
8 46 dgisselq
//      'scope' or (perhaps more appropriately) a bus accessed logic analyzer.
9
//      The general operation is such that this 'scope' can record and report
10
//      on any 32 bit value transiting through the FPGA.  Once started and
11
//      reset, the scope records a copy of the input data every time the clock
12
//      ticks with the circuit enabled.  That is, it records these values up
13
//      until the trigger.  Once the trigger goes high, the scope will record
14 51 dgisselq
//      for br_holdoff more counts before stopping.  Values may then be read
15 46 dgisselq
//      from the buffer, oldest to most recent.  After reading, the scope may
16
//      then be reset for another run.
17 2 dgisselq
//
18 46 dgisselq
//      In general, therefore, operation happens in this fashion:
19 2 dgisselq
//              1. A reset is issued.
20
//              2. Recording starts, in a circular buffer, and continues until
21
//              3. The trigger line is asserted.
22
//                      The scope registers the asserted trigger by setting
23
//                      the 'o_triggered' output flag.
24
//              4. A counter then ticks until the last value is written
25
//                      The scope registers that it has stopped recording by
26
//                      setting the 'o_stopped' output flag.
27
//              5. The scope recording is then paused until the next reset.
28
//              6. While stopped, the CPU can read the data from the scope
29
//              7. -- oldest to most recent
30
//              8. -- one value per i_rd&i_clk
31
//              9. Writes to the data register reset the address to the
32
//                      beginning of the buffer
33
//
34
//      Although the data width DW is parameterized, it is not very changable,
35
//      since the width is tied to the width of the data bus, as is the 
36
//      control word.  Therefore changing the data width would require changing
37
//      the interface.  It's doable, but it would be a change to the interface.
38
//
39
//      The SYNCHRONOUS parameter turns on and off meta-stability
40
//      synchronization.  Ideally a wishbone scope able to handle one or two
41
//      clocks would have a changing number of ports as this SYNCHRONOUS
42
//      parameter changed.  Other than running another script to modify
43
//      this, I don't know how to do that so ... we'll just leave it running
44
//      off of two clocks or not.
45
//
46
//
47
//      Internal to this routine, registers and wires are named with one of the
48
//      following prefixes:
49
//
50
//      i_      An input port to the routine
51
//      o_      An output port of the routine
52
//      br_     A register, controlled by the bus clock
53
//      dr_     A register, controlled by the data clock
54
//      bw_     A wire/net, controlled by the bus clock
55
//      dw_     A wire/net, controlled by the data clock
56
//
57
// Creator:     Dan Gisselquist, Ph.D.
58
//              Gisselquist Technology, LLC
59
//
60 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
61 2 dgisselq
//
62 46 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
63 2 dgisselq
//
64
// This program is free software (firmware): you can redistribute it and/or
65
// modify it under the terms of  the GNU General Public License as published
66
// by the Free Software Foundation, either version 3 of the License, or (at
67
// your option) any later version.
68
//
69
// This program is distributed in the hope that it will be useful, but WITHOUT
70
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
71
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
72
// for more details.
73
//
74
// You should have received a copy of the GNU General Public License along
75 46 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
76 2 dgisselq
// target there if the PDF file isn't present.)  If not, see
77
// <http://www.gnu.org/licenses/> for a copy.
78
//
79
// License:     GPL, v3, as defined and found on www.gnu.org,
80
//              http://www.gnu.org/licenses/gpl.html
81
//
82
//
83 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
84
//
85
//
86 2 dgisselq
module wbscope(i_clk, i_ce, i_trigger, i_data,
87
        i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
88
        o_wb_ack, o_wb_stall, o_wb_data,
89
        o_interrupt);
90 51 dgisselq
        parameter [4:0]                  LGMEM = 5'd10;
91
        parameter                       BUSW = 32;
92
        parameter [0:0]                   SYNCHRONOUS=1;
93
        parameter                       HOLDOFFBITS = 20;
94
        parameter [(HOLDOFFBITS-1):0]    DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4);
95 2 dgisselq
        // The input signals that we wish to record
96
        input                           i_clk, i_ce, i_trigger;
97
        input           [(BUSW-1):0]     i_data;
98
        // The WISHBONE bus for reading and configuring this scope
99
        input                           i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we;
100
        input                           i_wb_addr; // One address line only
101
        input           [(BUSW-1):0]     i_wb_data;
102
        output  wire                    o_wb_ack, o_wb_stall;
103
        output  reg     [(BUSW-1):0]     o_wb_data;
104
        // And, finally, for a final flair --- offer to interrupt the CPU after
105
        // our trigger has gone off.  This line is equivalent to the scope 
106
        // being stopped.  It is not maskable here.
107
        output  wire                    o_interrupt;
108
 
109
        reg     [(LGMEM-1):0]    raddr;
110
        reg     [(BUSW-1):0]     mem[0:((1<<LGMEM)-1)];
111
 
112
        // Our status/config register
113
        wire            bw_reset_request, bw_manual_trigger,
114
                        bw_disable_trigger, bw_reset_complete;
115 51 dgisselq
        reg     [2:0]    br_config;
116
        reg     [(HOLDOFFBITS-1):0]      br_holdoff;
117
        initial br_config = 3'b0;
118
        initial br_holdoff = DEFAULT_HOLDOFF;
119 2 dgisselq
        always @(posedge i_wb_clk)
120 51 dgisselq
                if ((i_wb_stb)&&(!i_wb_addr))
121 2 dgisselq
                begin
122
                        if (i_wb_we)
123 51 dgisselq
                        begin
124 2 dgisselq
                                br_config <= { i_wb_data[31],
125 51 dgisselq
                                        i_wb_data[27],
126
                                        i_wb_data[26] };
127
                                br_holdoff = i_wb_data[(HOLDOFFBITS-1):0];
128
                        end
129 2 dgisselq
                end else if (bw_reset_complete)
130 51 dgisselq
                        br_config[2] <= 1'b1;
131
        assign  bw_reset_request   = (!br_config[2]);
132
        assign  bw_manual_trigger  = (br_config[1]);
133
        assign  bw_disable_trigger = (br_config[0]);
134 2 dgisselq
 
135
        wire    dw_reset, dw_manual_trigger, dw_disable_trigger;
136
        generate
137
        if (SYNCHRONOUS > 0)
138
        begin
139
                assign  dw_reset = bw_reset_request;
140
                assign  dw_manual_trigger = bw_manual_trigger;
141
                assign  dw_disable_trigger = bw_disable_trigger;
142
                assign  bw_reset_complete = bw_reset_request;
143
        end else begin
144
                reg             r_reset_complete;
145 46 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_iflags;
146
                reg     [2:0]    r_iflags;
147 2 dgisselq
 
148
                // Resets are synchronous to the bus clock, not the data clock
149
                // so do a clock transfer here
150
                initial q_iflags = 3'b000;
151
                initial r_reset_complete = 1'b0;
152
                always @(posedge i_clk)
153
                begin
154
                        q_iflags <= { bw_reset_request, bw_manual_trigger, bw_disable_trigger };
155
                        r_iflags <= q_iflags;
156
                        r_reset_complete <= (dw_reset);
157
                end
158
 
159
                assign  dw_reset = r_iflags[2];
160
                assign  dw_manual_trigger = r_iflags[1];
161
                assign  dw_disable_trigger = r_iflags[0];
162
 
163 46 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    q_reset_complete;
164
                reg     qq_reset_complete;
165 2 dgisselq
                // Pass an acknowledgement back from the data clock to the bus
166
                // clock that the reset has been accomplished
167
                initial q_reset_complete = 1'b0;
168
                initial qq_reset_complete = 1'b0;
169
                always @(posedge i_wb_clk)
170
                begin
171
                        q_reset_complete  <= r_reset_complete;
172
                        qq_reset_complete <= q_reset_complete;
173
                end
174
 
175
                assign bw_reset_complete = qq_reset_complete;
176
        end endgenerate
177
 
178
        //
179
        // Set up the trigger
180
        //
181
        //
182
        // Write with the i-clk, or input clock.  All outputs read with the
183
        // WISHBONE-clk, or i_wb_clk clock.
184
        reg     dr_triggered, dr_primed;
185
        wire    dw_trigger;
186
        assign  dw_trigger = (dr_primed)&&(
187 51 dgisselq
                                ((i_trigger)&&(!dw_disable_trigger))
188 2 dgisselq
                                ||(dw_manual_trigger));
189
        initial dr_triggered = 1'b0;
190
        always @(posedge i_clk)
191
                if (dw_reset)
192
                        dr_triggered <= 1'b0;
193
                else if ((i_ce)&&(dw_trigger))
194
                        dr_triggered <= 1'b1;
195
 
196
        //
197
        // Determine when memory is full and capture is complete
198
        //
199
        // Writes take place on the data clock
200 46 dgisselq
        // The counter is unsigned
201
        (* ASYNC_REG="TRUE" *) reg      [(HOLDOFFBITS-1):0]      counter;
202
 
203 2 dgisselq
        reg             dr_stopped;
204
        initial dr_stopped = 1'b0;
205 46 dgisselq
        initial counter = 0;
206 2 dgisselq
        always @(posedge i_clk)
207
                if (dw_reset)
208
                        counter <= 0;
209 51 dgisselq
                else if ((i_ce)&&(dr_triggered)&&(!dr_stopped))
210 2 dgisselq
                begin // MUST BE a < and not <=, so that we can keep this w/in
211
                        // 20 bits.  Else we'd need to add a bit to comparison 
212
                        // here.
213 46 dgisselq
                        counter <= counter + 1'b1;
214 2 dgisselq
                end
215 46 dgisselq
        always @(posedge i_clk)
216 51 dgisselq
                if ((!dr_triggered)||(dw_reset))
217 46 dgisselq
                        dr_stopped <= 1'b0;
218
                else
219 51 dgisselq
                        dr_stopped <= (counter >= br_holdoff);
220 2 dgisselq
 
221
        //
222
        //      Actually do our writes to memory.  Record, via 'primed' when
223
        //      the memory is full.
224
        //
225
        //      The 'waddr' address that we are using really crosses two clock
226
        //      domains.  While writing and changing, it's in the data clock
227
        //      domain.  Once stopped, it becomes part of the bus clock domain.
228
        //      The clock transfer on the stopped line handles the clock
229
        //      transfer for these signals.
230
        //
231
        reg     [(LGMEM-1):0]    waddr;
232
        initial waddr = {(LGMEM){1'b0}};
233
        initial dr_primed = 1'b0;
234
        always @(posedge i_clk)
235
                if (dw_reset) // For simulation purposes, supply a valid value
236
                begin
237
                        waddr <= 0; // upon reset.
238
                        dr_primed <= 1'b0;
239 46 dgisselq
                end else if ((i_ce)&&(!dr_stopped))
240 2 dgisselq
                begin
241
                        // mem[waddr] <= i_data;
242
                        waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1};
243
                        dr_primed <= (dr_primed)||(&waddr);
244
                end
245
        always @(posedge i_clk)
246 46 dgisselq
                if ((i_ce)&&(!dr_stopped))
247 2 dgisselq
                        mem[waddr] <= i_data;
248
 
249
        //
250
        // Clock transfer of the status signals
251
        //
252
        wire    bw_stopped, bw_triggered, bw_primed;
253
        generate
254
        if (SYNCHRONOUS > 0)
255
        begin
256
                assign  bw_stopped   = dr_stopped;
257
                assign  bw_triggered = dr_triggered;
258
                assign  bw_primed    = dr_primed;
259
        end else begin
260
                // These aren't a problem, since none of these are strobe
261
                // signals.  They goes from low to high, and then stays high
262
                // for many clocks.  Swapping is thus easy--two flip flops to
263
                // protect against meta-stability and we're done.
264
                //
265 46 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_oflags;
266
                reg     [2:0]    r_oflags;
267 2 dgisselq
                initial q_oflags = 3'h0;
268
                initial r_oflags = 3'h0;
269
                always @(posedge i_wb_clk)
270
                        if (bw_reset_request)
271
                        begin
272
                                q_oflags <= 3'h0;
273
                                r_oflags <= 3'h0;
274
                        end else begin
275
                                q_oflags <= { dr_stopped, dr_triggered, dr_primed };
276
                                r_oflags <= q_oflags;
277
                        end
278
 
279
                assign  bw_stopped   = r_oflags[2];
280
                assign  bw_triggered = r_oflags[1];
281
                assign  bw_primed    = r_oflags[0];
282
        end endgenerate
283
 
284
        // Reads use the bus clock
285
        reg     br_wb_ack;
286
        initial br_wb_ack = 1'b0;
287
        wire    bw_cyc_stb;
288 46 dgisselq
        assign  bw_cyc_stb = (i_wb_stb);
289 2 dgisselq
        always @(posedge i_wb_clk)
290
        begin
291
                if ((bw_reset_request)
292
                        ||((bw_cyc_stb)&&(i_wb_addr)&&(i_wb_we)))
293
                        raddr <= 0;
294 51 dgisselq
                else if ((bw_cyc_stb)&&(i_wb_addr)&&(!i_wb_we)&&(bw_stopped))
295 46 dgisselq
                        raddr <= raddr + 1'b1; // Data read, when stopped
296 2 dgisselq
 
297 51 dgisselq
                if ((bw_cyc_stb)&&(!i_wb_we))
298 2 dgisselq
                begin // Read from the bus
299
                        br_wb_ack <= 1'b1;
300
                end else if ((bw_cyc_stb)&&(i_wb_we))
301
                        // We did this write above
302
                        br_wb_ack <= 1'b1;
303
                else // Do nothing if either i_wb_cyc or i_wb_stb are low
304
                        br_wb_ack <= 1'b0;
305
        end
306
 
307
        reg     [31:0]   nxt_mem;
308
        always @(posedge i_wb_clk)
309
                nxt_mem <= mem[raddr+waddr+
310 51 dgisselq
                        (((bw_cyc_stb)&&(i_wb_addr)&&(!i_wb_we)) ?
311 2 dgisselq
                                {{(LGMEM-1){1'b0}},1'b1} : { (LGMEM){1'b0}} )];
312
 
313 46 dgisselq
        wire    [19:0]   full_holdoff;
314 51 dgisselq
        assign full_holdoff[(HOLDOFFBITS-1):0] = br_holdoff;
315 46 dgisselq
        generate if (HOLDOFFBITS < 20)
316
                assign full_holdoff[19:(HOLDOFFBITS)] = 0;
317
        endgenerate
318
 
319 2 dgisselq
        wire    [4:0]    bw_lgmem;
320
        assign          bw_lgmem = LGMEM;
321
        always @(posedge i_wb_clk)
322 51 dgisselq
                if (!i_wb_addr) // Control register read
323 2 dgisselq
                        o_wb_data <= { bw_reset_request,
324
                                        bw_stopped,
325
                                        bw_triggered,
326
                                        bw_primed,
327
                                        bw_manual_trigger,
328
                                        bw_disable_trigger,
329
                                        (raddr == {(LGMEM){1'b0}}),
330
                                        bw_lgmem,
331 46 dgisselq
                                        full_holdoff  };
332 51 dgisselq
                else if (!bw_stopped) // read, prior to stopping
333 2 dgisselq
                        o_wb_data <= i_data;
334
                else // if (i_wb_addr) // Read from FIFO memory
335
                        o_wb_data <= nxt_mem; // mem[raddr+waddr];
336
 
337
        assign  o_wb_stall = 1'b0;
338
        assign  o_wb_ack = (i_wb_cyc)&&(br_wb_ack);
339
 
340
        reg     br_level_interrupt;
341
        initial br_level_interrupt = 1'b0;
342 51 dgisselq
        assign  o_interrupt = (bw_stopped)&&(!bw_disable_trigger)
343
                                        &&(!br_level_interrupt);
344 2 dgisselq
        always @(posedge i_wb_clk)
345
                if ((bw_reset_complete)||(bw_reset_request))
346
                        br_level_interrupt<= 1'b0;
347
                else
348 51 dgisselq
                        br_level_interrupt<= (bw_stopped)&&(!bw_disable_trigger);
349 2 dgisselq
 
350
endmodule

powered by: WebSVN 2.1.0

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