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

Subversion Repositories openarty

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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