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

Subversion Repositories wbscope

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    wbscope.v
4
//
5 12 dgisselq
// Project:     WBScope, a wishbone hosted scope
6 2 dgisselq
//
7
// Purpose:     This is a generic/library routine for providing a bus accessed
8 12 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
//      for br_holdoff more counts before stopping.  Values may then be read
15
//      from the buffer, oldest to most recent.  After reading, the scope may
16
//      then be reset for another run.
17 2 dgisselq
//
18 12 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 13 dgisselq
//              8. -- one value per i_rd&i_data_clk
31 2 dgisselq
//              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 14 dgisselq
//      since the width is tied to the width of the data bus, as is the
36 2 dgisselq
//      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 11 dgisselq
//              Gisselquist Technology, LLC
59 2 dgisselq
//
60 12 dgisselq
////////////////////////////////////////////////////////////////////////////////
61 2 dgisselq
//
62 12 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 12 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 12 dgisselq
////////////////////////////////////////////////////////////////////////////////
84
//
85
//
86 13 dgisselq
`default_nettype        none
87
//
88
module wbscope(i_data_clk, i_ce, i_trigger, i_data,
89 2 dgisselq
        i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
90
        o_wb_ack, o_wb_stall, o_wb_data,
91
        o_interrupt);
92 12 dgisselq
        parameter [4:0]                  LGMEM = 5'd10;
93
        parameter                       BUSW = 32;
94
        parameter [0:0]                   SYNCHRONOUS=1;
95
        parameter                       HOLDOFFBITS = 20;
96
        parameter [(HOLDOFFBITS-1):0]    DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4);
97 2 dgisselq
        // The input signals that we wish to record
98 13 dgisselq
        input   wire                    i_data_clk, i_ce, i_trigger;
99
        input   wire    [(BUSW-1):0]     i_data;
100 2 dgisselq
        // The WISHBONE bus for reading and configuring this scope
101 13 dgisselq
        input   wire                    i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we;
102
        input   wire                    i_wb_addr; // One address line only
103
        input   wire    [(BUSW-1):0]     i_wb_data;
104 2 dgisselq
        output  wire                    o_wb_ack, o_wb_stall;
105 13 dgisselq
        output  wire    [(BUSW-1):0]     o_wb_data;
106 2 dgisselq
        // And, finally, for a final flair --- offer to interrupt the CPU after
107 14 dgisselq
        // our trigger has gone off.  This line is equivalent to the scope
108 2 dgisselq
        // being stopped.  It is not maskable here.
109
        output  wire                    o_interrupt;
110
 
111 13 dgisselq
        wire    bus_clock;
112
        assign  bus_clock = i_wb_clk;
113
 
114
        ///////////////////////////////////////////////////
115
        //
116
        // Decode and handle the WB bus signaling in a
117
        // (somewhat) portable manner
118
        //
119
        ///////////////////////////////////////////////////
120
        //
121
        //
122
        assign  o_wb_stall = 1'b0;
123
 
124
        wire    read_from_data;
125
        assign  read_from_data = (i_wb_stb)&&(!i_wb_we)&&(i_wb_addr);
126
 
127
        wire    write_stb;
128
        assign  write_stb = (i_wb_stb)&&(i_wb_we);
129
 
130
        wire    write_to_control;
131
        assign  write_to_control = (write_stb)&&(!i_wb_addr);
132
 
133
        reg     read_address;
134
        always @(posedge bus_clock)
135
                read_address <= i_wb_addr;
136
 
137 2 dgisselq
        reg     [(LGMEM-1):0]    raddr;
138
        reg     [(BUSW-1):0]     mem[0:((1<<LGMEM)-1)];
139
 
140
        // Our status/config register
141
        wire            bw_reset_request, bw_manual_trigger,
142
                        bw_disable_trigger, bw_reset_complete;
143 12 dgisselq
        reg     [2:0]    br_config;
144
        reg     [(HOLDOFFBITS-1):0]      br_holdoff;
145
        initial br_config = 3'b0;
146
        initial br_holdoff = DEFAULT_HOLDOFF;
147 13 dgisselq
        always @(posedge bus_clock)
148
                if (write_to_control)
149 2 dgisselq
                begin
150 13 dgisselq
                        br_config <= { i_wb_data[31],
151
                                i_wb_data[27],
152
                                i_wb_data[26] };
153
                        br_holdoff <= i_wb_data[(HOLDOFFBITS-1):0];
154 2 dgisselq
                end else if (bw_reset_complete)
155 12 dgisselq
                        br_config[2] <= 1'b1;
156
        assign  bw_reset_request   = (!br_config[2]);
157
        assign  bw_manual_trigger  = (br_config[1]);
158
        assign  bw_disable_trigger = (br_config[0]);
159 2 dgisselq
 
160
        wire    dw_reset, dw_manual_trigger, dw_disable_trigger;
161
        generate
162
        if (SYNCHRONOUS > 0)
163
        begin
164
                assign  dw_reset = bw_reset_request;
165
                assign  dw_manual_trigger = bw_manual_trigger;
166
                assign  dw_disable_trigger = bw_disable_trigger;
167
                assign  bw_reset_complete = bw_reset_request;
168
        end else begin
169
                reg             r_reset_complete;
170 12 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_iflags;
171
                reg     [2:0]    r_iflags;
172 2 dgisselq
 
173
                // Resets are synchronous to the bus clock, not the data clock
174
                // so do a clock transfer here
175
                initial q_iflags = 3'b000;
176
                initial r_reset_complete = 1'b0;
177 13 dgisselq
                always @(posedge i_data_clk)
178 2 dgisselq
                begin
179
                        q_iflags <= { bw_reset_request, bw_manual_trigger, bw_disable_trigger };
180
                        r_iflags <= q_iflags;
181
                        r_reset_complete <= (dw_reset);
182
                end
183
 
184
                assign  dw_reset = r_iflags[2];
185
                assign  dw_manual_trigger = r_iflags[1];
186
                assign  dw_disable_trigger = r_iflags[0];
187
 
188 12 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    q_reset_complete;
189
                reg     qq_reset_complete;
190 2 dgisselq
                // Pass an acknowledgement back from the data clock to the bus
191
                // clock that the reset has been accomplished
192
                initial q_reset_complete = 1'b0;
193
                initial qq_reset_complete = 1'b0;
194 13 dgisselq
                always @(posedge bus_clock)
195 2 dgisselq
                begin
196
                        q_reset_complete  <= r_reset_complete;
197
                        qq_reset_complete <= q_reset_complete;
198
                end
199
 
200
                assign bw_reset_complete = qq_reset_complete;
201
        end endgenerate
202
 
203
        //
204
        // Set up the trigger
205
        //
206
        //
207
        // Write with the i-clk, or input clock.  All outputs read with the
208 13 dgisselq
        // bus clock, or bus_clock  as we've called it here.
209 2 dgisselq
        reg     dr_triggered, dr_primed;
210
        wire    dw_trigger;
211
        assign  dw_trigger = (dr_primed)&&(
212 12 dgisselq
                                ((i_trigger)&&(!dw_disable_trigger))
213 2 dgisselq
                                ||(dw_manual_trigger));
214
        initial dr_triggered = 1'b0;
215 13 dgisselq
        always @(posedge i_data_clk)
216 2 dgisselq
                if (dw_reset)
217
                        dr_triggered <= 1'b0;
218
                else if ((i_ce)&&(dw_trigger))
219
                        dr_triggered <= 1'b1;
220
 
221
        //
222
        // Determine when memory is full and capture is complete
223
        //
224
        // Writes take place on the data clock
225 12 dgisselq
        // The counter is unsigned
226
        (* ASYNC_REG="TRUE" *) reg      [(HOLDOFFBITS-1):0]      counter;
227
 
228 2 dgisselq
        reg             dr_stopped;
229
        initial dr_stopped = 1'b0;
230 12 dgisselq
        initial counter = 0;
231 13 dgisselq
        always @(posedge i_data_clk)
232 2 dgisselq
                if (dw_reset)
233
                        counter <= 0;
234 12 dgisselq
                else if ((i_ce)&&(dr_triggered)&&(!dr_stopped))
235 13 dgisselq
                begin
236 12 dgisselq
                        counter <= counter + 1'b1;
237 2 dgisselq
                end
238 13 dgisselq
        always @(posedge i_data_clk)
239 12 dgisselq
                if ((!dr_triggered)||(dw_reset))
240
                        dr_stopped <= 1'b0;
241 13 dgisselq
                else if (HOLDOFFBITS > 1) // if (i_ce)
242 12 dgisselq
                        dr_stopped <= (counter >= br_holdoff);
243 13 dgisselq
                else if (HOLDOFFBITS <= 1)
244
                        dr_stopped <= ((i_ce)&&(dw_trigger));
245 2 dgisselq
 
246
        //
247
        //      Actually do our writes to memory.  Record, via 'primed' when
248
        //      the memory is full.
249
        //
250
        //      The 'waddr' address that we are using really crosses two clock
251
        //      domains.  While writing and changing, it's in the data clock
252
        //      domain.  Once stopped, it becomes part of the bus clock domain.
253
        //      The clock transfer on the stopped line handles the clock
254
        //      transfer for these signals.
255
        //
256
        reg     [(LGMEM-1):0]    waddr;
257
        initial waddr = {(LGMEM){1'b0}};
258
        initial dr_primed = 1'b0;
259 13 dgisselq
        always @(posedge i_data_clk)
260 2 dgisselq
                if (dw_reset) // For simulation purposes, supply a valid value
261
                begin
262
                        waddr <= 0; // upon reset.
263
                        dr_primed <= 1'b0;
264 12 dgisselq
                end else if ((i_ce)&&(!dr_stopped))
265 2 dgisselq
                begin
266 9 dgisselq
                        // mem[waddr] <= i_data;
267
                        waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1};
268 13 dgisselq
                        if (!dr_primed)
269 14 dgisselq
                                dr_primed <= (&waddr);
270 2 dgisselq
                end
271 13 dgisselq
 
272
        // Delay the incoming data so that we can get our trigger
273
        // logic to line up with the data.  The goal is to have a
274
        // hold off of zero place the trigger in the last memory
275
        // address.
276
        localparam      STOPDELAY = 1;
277
        wire    [(BUSW-1):0]             wr_piped_data;
278
        generate
279
        if (STOPDELAY == 0)
280
                // No delay ... just assign the wires to our input lines
281
                assign  wr_piped_data = i_data;
282
        else if (STOPDELAY == 1)
283
        begin
284
                //
285
                // Delay by one means just register this once
286
                reg     [(BUSW-1):0]     data_pipe;
287
                always @(posedge i_data_clk)
288
                        if (i_ce)
289
                                data_pipe <= i_data;
290
                assign  wr_piped_data = data_pipe;
291
        end else begin
292
                // Arbitrary delay ... use a longer pipe
293
                reg     [(STOPDELAY*BUSW-1):0]   data_pipe;
294
 
295
                always @(posedge i_data_clk)
296
                        if (i_ce)
297
                                data_pipe <= { data_pipe[((STOPDELAY-1)*BUSW-1):0], i_data };
298
                assign  wr_piped_data = { data_pipe[(STOPDELAY*BUSW-1):((STOPDELAY-1)*BUSW)] };
299
        end endgenerate
300
 
301
        always @(posedge i_data_clk)
302 12 dgisselq
                if ((i_ce)&&(!dr_stopped))
303 13 dgisselq
                        mem[waddr] <= wr_piped_data;
304 2 dgisselq
 
305
        //
306
        // Clock transfer of the status signals
307
        //
308
        wire    bw_stopped, bw_triggered, bw_primed;
309
        generate
310
        if (SYNCHRONOUS > 0)
311
        begin
312
                assign  bw_stopped   = dr_stopped;
313
                assign  bw_triggered = dr_triggered;
314
                assign  bw_primed    = dr_primed;
315
        end else begin
316
                // These aren't a problem, since none of these are strobe
317
                // signals.  They goes from low to high, and then stays high
318
                // for many clocks.  Swapping is thus easy--two flip flops to
319
                // protect against meta-stability and we're done.
320
                //
321 12 dgisselq
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_oflags;
322
                reg     [2:0]    r_oflags;
323 2 dgisselq
                initial q_oflags = 3'h0;
324
                initial r_oflags = 3'h0;
325 13 dgisselq
                always @(posedge bus_clock)
326 2 dgisselq
                        if (bw_reset_request)
327
                        begin
328
                                q_oflags <= 3'h0;
329
                                r_oflags <= 3'h0;
330
                        end else begin
331
                                q_oflags <= { dr_stopped, dr_triggered, dr_primed };
332
                                r_oflags <= q_oflags;
333
                        end
334
 
335
                assign  bw_stopped   = r_oflags[2];
336
                assign  bw_triggered = r_oflags[1];
337
                assign  bw_primed    = r_oflags[0];
338
        end endgenerate
339
 
340
        // Reads use the bus clock
341 13 dgisselq
        reg     br_wb_ack, br_pre_wb_ack;
342 2 dgisselq
        initial br_wb_ack = 1'b0;
343
        wire    bw_cyc_stb;
344 12 dgisselq
        assign  bw_cyc_stb = (i_wb_stb);
345 13 dgisselq
        initial br_pre_wb_ack = 1'b0;
346
        initial br_wb_ack = 1'b0;
347
        always @(posedge bus_clock)
348 2 dgisselq
        begin
349 13 dgisselq
                if ((bw_reset_request)||(write_to_control))
350 2 dgisselq
                        raddr <= 0;
351 13 dgisselq
                else if ((read_from_data)&&(bw_stopped))
352 12 dgisselq
                        raddr <= raddr + 1'b1; // Data read, when stopped
353 2 dgisselq
 
354 13 dgisselq
                br_pre_wb_ack <= bw_cyc_stb;
355
                br_wb_ack <= (br_pre_wb_ack)&&(i_wb_cyc);
356 2 dgisselq
        end
357 13 dgisselq
        assign  o_wb_ack = (i_wb_cyc)&&(br_wb_ack);
358 2 dgisselq
 
359 13 dgisselq
        reg     [(LGMEM-1):0]    this_addr;
360
        always @(posedge bus_clock)
361
                if (read_from_data)
362
                        this_addr <= raddr + waddr + 1'b1;
363
                else
364
                        this_addr <= raddr + waddr;
365
 
366 9 dgisselq
        reg     [31:0]   nxt_mem;
367 13 dgisselq
        always @(posedge bus_clock)
368
                nxt_mem <= mem[this_addr];
369 9 dgisselq
 
370 12 dgisselq
        wire    [19:0]   full_holdoff;
371
        assign full_holdoff[(HOLDOFFBITS-1):0] = br_holdoff;
372
        generate if (HOLDOFFBITS < 20)
373
                assign full_holdoff[19:(HOLDOFFBITS)] = 0;
374
        endgenerate
375
 
376 13 dgisselq
        reg     [31:0]   o_bus_data;
377 2 dgisselq
        wire    [4:0]    bw_lgmem;
378
        assign          bw_lgmem = LGMEM;
379 13 dgisselq
        always @(posedge bus_clock)
380
                if (!read_address) // Control register read
381
                        o_bus_data <= { bw_reset_request,
382 2 dgisselq
                                        bw_stopped,
383
                                        bw_triggered,
384
                                        bw_primed,
385
                                        bw_manual_trigger,
386
                                        bw_disable_trigger,
387
                                        (raddr == {(LGMEM){1'b0}}),
388
                                        bw_lgmem,
389 12 dgisselq
                                        full_holdoff  };
390
                else if (!bw_stopped) // read, prior to stopping
391 13 dgisselq
                        o_bus_data <= i_data;
392 2 dgisselq
                else // if (i_wb_addr) // Read from FIFO memory
393 13 dgisselq
                        o_bus_data <= nxt_mem; // mem[raddr+waddr];
394 2 dgisselq
 
395 13 dgisselq
        assign  o_wb_data = o_bus_data;
396 2 dgisselq
 
397
        reg     br_level_interrupt;
398
        initial br_level_interrupt = 1'b0;
399 12 dgisselq
        assign  o_interrupt = (bw_stopped)&&(!bw_disable_trigger)
400
                                        &&(!br_level_interrupt);
401 13 dgisselq
        always @(posedge bus_clock)
402 2 dgisselq
                if ((bw_reset_complete)||(bw_reset_request))
403
                        br_level_interrupt<= 1'b0;
404
                else
405 12 dgisselq
                        br_level_interrupt<= (bw_stopped)&&(!bw_disable_trigger);
406 2 dgisselq
 
407 13 dgisselq
        // verilator lint_off UNUSED
408
        // Make verilator happy
409
        wire    [28:0]   unused;
410
        assign unused = { i_wb_data[30:28], i_wb_data[25:0] };
411
        // verilator lint_on UNUSED
412 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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