1 |
30 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
3 |
dgisselq |
//
|
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 |
30 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
62 |
3 |
dgisselq |
//
|
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 |
30 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
//
|
86 |
|
|
//
|
87 |
3 |
dgisselq |
module wbscope(i_clk, i_ce, i_trigger, i_data,
|
88 |
|
|
i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
|
89 |
|
|
o_wb_ack, o_wb_stall, o_wb_data,
|
90 |
|
|
o_interrupt);
|
91 |
30 |
dgisselq |
parameter LGMEM = 5'd10, BUSW = 32, SYNCHRONOUS=1,
|
92 |
|
|
DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4);
|
93 |
3 |
dgisselq |
// The input signals that we wish to record
|
94 |
|
|
input i_clk, i_ce, i_trigger;
|
95 |
|
|
input [(BUSW-1):0] i_data;
|
96 |
|
|
// The WISHBONE bus for reading and configuring this scope
|
97 |
|
|
input i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we;
|
98 |
|
|
input i_wb_addr; // One address line only
|
99 |
|
|
input [(BUSW-1):0] i_wb_data;
|
100 |
|
|
output wire o_wb_ack, o_wb_stall;
|
101 |
|
|
output reg [(BUSW-1):0] o_wb_data;
|
102 |
|
|
// And, finally, for a final flair --- offer to interrupt the CPU after
|
103 |
|
|
// our trigger has gone off. This line is equivalent to the scope
|
104 |
|
|
// being stopped. It is not maskable here.
|
105 |
|
|
output wire o_interrupt;
|
106 |
|
|
|
107 |
|
|
reg [(LGMEM-1):0] raddr;
|
108 |
|
|
reg [(BUSW-1):0] mem[0:((1<<LGMEM)-1)];
|
109 |
|
|
|
110 |
|
|
// Our status/config register
|
111 |
|
|
wire bw_reset_request, bw_manual_trigger,
|
112 |
|
|
bw_disable_trigger, bw_reset_complete;
|
113 |
|
|
reg [22:0] br_config;
|
114 |
|
|
wire [19:0] bw_holdoff;
|
115 |
30 |
dgisselq |
initial br_config = DEFAULT_HOLDOFF;
|
116 |
3 |
dgisselq |
always @(posedge i_wb_clk)
|
117 |
25 |
dgisselq |
if ((i_wb_cyc)&&(i_wb_stb)&&(~i_wb_addr))
|
118 |
3 |
dgisselq |
begin
|
119 |
25 |
dgisselq |
if (i_wb_we)
|
120 |
|
|
br_config <= { i_wb_data[31],
|
121 |
|
|
(i_wb_data[27]),
|
122 |
|
|
i_wb_data[26],
|
123 |
|
|
i_wb_data[19:0] };
|
124 |
3 |
dgisselq |
end else if (bw_reset_complete)
|
125 |
|
|
br_config[22] <= 1'b1;
|
126 |
|
|
assign bw_reset_request = (~br_config[22]);
|
127 |
|
|
assign bw_manual_trigger = (br_config[21]);
|
128 |
|
|
assign bw_disable_trigger = (br_config[20]);
|
129 |
|
|
assign bw_holdoff = br_config[19:0];
|
130 |
|
|
|
131 |
|
|
wire dw_reset, dw_manual_trigger, dw_disable_trigger;
|
132 |
|
|
generate
|
133 |
|
|
if (SYNCHRONOUS > 0)
|
134 |
|
|
begin
|
135 |
|
|
assign dw_reset = bw_reset_request;
|
136 |
|
|
assign dw_manual_trigger = bw_manual_trigger;
|
137 |
|
|
assign dw_disable_trigger = bw_disable_trigger;
|
138 |
|
|
assign bw_reset_complete = bw_reset_request;
|
139 |
|
|
end else begin
|
140 |
|
|
reg r_reset_complete;
|
141 |
30 |
dgisselq |
(* ASYNC_REG = "TRUE" *) reg [2:0] q_iflags;
|
142 |
|
|
reg [2:0] r_iflags;
|
143 |
3 |
dgisselq |
|
144 |
|
|
// Resets are synchronous to the bus clock, not the data clock
|
145 |
|
|
// so do a clock transfer here
|
146 |
|
|
initial q_iflags = 3'b000;
|
147 |
|
|
initial r_reset_complete = 1'b0;
|
148 |
|
|
always @(posedge i_clk)
|
149 |
|
|
begin
|
150 |
|
|
q_iflags <= { bw_reset_request, bw_manual_trigger, bw_disable_trigger };
|
151 |
|
|
r_iflags <= q_iflags;
|
152 |
|
|
r_reset_complete <= (dw_reset);
|
153 |
|
|
end
|
154 |
|
|
|
155 |
|
|
assign dw_reset = r_iflags[2];
|
156 |
|
|
assign dw_manual_trigger = r_iflags[1];
|
157 |
|
|
assign dw_disable_trigger = r_iflags[0];
|
158 |
|
|
|
159 |
30 |
dgisselq |
(* ASYNC_REG = "TRUE" *) reg q_reset_complete;
|
160 |
|
|
reg qq_reset_complete;
|
161 |
3 |
dgisselq |
// Pass an acknowledgement back from the data clock to the bus
|
162 |
|
|
// clock that the reset has been accomplished
|
163 |
|
|
initial q_reset_complete = 1'b0;
|
164 |
|
|
initial qq_reset_complete = 1'b0;
|
165 |
|
|
always @(posedge i_wb_clk)
|
166 |
|
|
begin
|
167 |
|
|
q_reset_complete <= r_reset_complete;
|
168 |
|
|
qq_reset_complete <= q_reset_complete;
|
169 |
|
|
end
|
170 |
|
|
|
171 |
|
|
assign bw_reset_complete = qq_reset_complete;
|
172 |
|
|
end endgenerate
|
173 |
|
|
|
174 |
|
|
//
|
175 |
|
|
// Set up the trigger
|
176 |
|
|
//
|
177 |
|
|
//
|
178 |
|
|
// Write with the i-clk, or input clock. All outputs read with the
|
179 |
|
|
// WISHBONE-clk, or i_wb_clk clock.
|
180 |
|
|
reg dr_triggered, dr_primed;
|
181 |
|
|
wire dw_trigger;
|
182 |
|
|
assign dw_trigger = (dr_primed)&&(
|
183 |
|
|
((i_trigger)&&(~dw_disable_trigger))
|
184 |
|
|
||(dr_triggered)
|
185 |
|
|
||(dw_manual_trigger));
|
186 |
|
|
initial dr_triggered = 1'b0;
|
187 |
|
|
always @(posedge i_clk)
|
188 |
|
|
if (dw_reset)
|
189 |
|
|
dr_triggered <= 1'b0;
|
190 |
|
|
else if ((i_ce)&&(dw_trigger))
|
191 |
|
|
dr_triggered <= 1'b1;
|
192 |
|
|
|
193 |
|
|
//
|
194 |
|
|
// Determine when memory is full and capture is complete
|
195 |
|
|
//
|
196 |
|
|
// Writes take place on the data clock
|
197 |
25 |
dgisselq |
reg dr_stopped;
|
198 |
30 |
dgisselq |
(* ASYNC_REG="TRUE" *) reg [19:0] counter;// This is unsigned
|
199 |
3 |
dgisselq |
initial dr_stopped = 1'b0;
|
200 |
|
|
initial counter = 20'h0000;
|
201 |
|
|
always @(posedge i_clk)
|
202 |
|
|
if (dw_reset)
|
203 |
|
|
begin
|
204 |
|
|
counter <= 0;
|
205 |
|
|
dr_stopped <= 1'b0;
|
206 |
|
|
end else if ((i_ce)&&(dr_triggered))
|
207 |
|
|
begin // MUST BE a < and not <=, so that we can keep this w/in
|
208 |
|
|
// 20 bits. Else we'd need to add a bit to comparison
|
209 |
|
|
// here.
|
210 |
25 |
dgisselq |
if (counter < bw_holdoff)
|
211 |
3 |
dgisselq |
counter <= counter + 20'h01;
|
212 |
25 |
dgisselq |
else
|
213 |
|
|
dr_stopped <= 1'b1;
|
214 |
3 |
dgisselq |
end
|
215 |
|
|
|
216 |
|
|
//
|
217 |
|
|
// Actually do our writes to memory. Record, via 'primed' when
|
218 |
|
|
// the memory is full.
|
219 |
|
|
//
|
220 |
|
|
// The 'waddr' address that we are using really crosses two clock
|
221 |
|
|
// domains. While writing and changing, it's in the data clock
|
222 |
|
|
// domain. Once stopped, it becomes part of the bus clock domain.
|
223 |
|
|
// The clock transfer on the stopped line handles the clock
|
224 |
|
|
// transfer for these signals.
|
225 |
|
|
//
|
226 |
|
|
reg [(LGMEM-1):0] waddr;
|
227 |
|
|
initial waddr = {(LGMEM){1'b0}};
|
228 |
|
|
initial dr_primed = 1'b0;
|
229 |
|
|
always @(posedge i_clk)
|
230 |
|
|
if (dw_reset) // For simulation purposes, supply a valid value
|
231 |
|
|
begin
|
232 |
|
|
waddr <= 0; // upon reset.
|
233 |
|
|
dr_primed <= 1'b0;
|
234 |
25 |
dgisselq |
end else if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff)))
|
235 |
3 |
dgisselq |
begin
|
236 |
|
|
// mem[waddr] <= i_data;
|
237 |
|
|
waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1};
|
238 |
|
|
dr_primed <= (dr_primed)||(&waddr);
|
239 |
|
|
end
|
240 |
|
|
always @(posedge i_clk)
|
241 |
25 |
dgisselq |
if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff)))
|
242 |
3 |
dgisselq |
mem[waddr] <= i_data;
|
243 |
|
|
|
244 |
|
|
//
|
245 |
|
|
// Clock transfer of the status signals
|
246 |
|
|
//
|
247 |
|
|
wire bw_stopped, bw_triggered, bw_primed;
|
248 |
|
|
generate
|
249 |
|
|
if (SYNCHRONOUS > 0)
|
250 |
|
|
begin
|
251 |
|
|
assign bw_stopped = dr_stopped;
|
252 |
|
|
assign bw_triggered = dr_triggered;
|
253 |
|
|
assign bw_primed = dr_primed;
|
254 |
|
|
end else begin
|
255 |
|
|
// These aren't a problem, since none of these are strobe
|
256 |
|
|
// signals. They goes from low to high, and then stays high
|
257 |
|
|
// for many clocks. Swapping is thus easy--two flip flops to
|
258 |
|
|
// protect against meta-stability and we're done.
|
259 |
|
|
//
|
260 |
30 |
dgisselq |
(* ASYNC_REG = "TRUE" *) reg [2:0] q_oflags;
|
261 |
|
|
reg [2:0] r_oflags;
|
262 |
3 |
dgisselq |
initial q_oflags = 3'h0;
|
263 |
|
|
initial r_oflags = 3'h0;
|
264 |
|
|
always @(posedge i_wb_clk)
|
265 |
|
|
if (bw_reset_request)
|
266 |
|
|
begin
|
267 |
|
|
q_oflags <= 3'h0;
|
268 |
|
|
r_oflags <= 3'h0;
|
269 |
|
|
end else begin
|
270 |
|
|
q_oflags <= { dr_stopped, dr_triggered, dr_primed };
|
271 |
|
|
r_oflags <= q_oflags;
|
272 |
|
|
end
|
273 |
|
|
|
274 |
|
|
assign bw_stopped = r_oflags[2];
|
275 |
|
|
assign bw_triggered = r_oflags[1];
|
276 |
|
|
assign bw_primed = r_oflags[0];
|
277 |
|
|
end endgenerate
|
278 |
|
|
|
279 |
|
|
// Reads use the bus clock
|
280 |
25 |
dgisselq |
reg br_wb_ack;
|
281 |
3 |
dgisselq |
initial br_wb_ack = 1'b0;
|
282 |
25 |
dgisselq |
wire bw_cyc_stb;
|
283 |
|
|
assign bw_cyc_stb = ((i_wb_cyc)&&(i_wb_stb));
|
284 |
3 |
dgisselq |
always @(posedge i_wb_clk)
|
285 |
25 |
dgisselq |
begin
|
286 |
|
|
if ((bw_reset_request)
|
287 |
|
|
||((bw_cyc_stb)&&(i_wb_addr)&&(i_wb_we)))
|
288 |
3 |
dgisselq |
raddr <= 0;
|
289 |
25 |
dgisselq |
else if ((bw_cyc_stb)&&(i_wb_addr)&&(~i_wb_we)&&(bw_stopped))
|
290 |
3 |
dgisselq |
raddr <= raddr + {{(LGMEM-1){1'b0}},1'b1}; // Data read, when stopped
|
291 |
|
|
|
292 |
25 |
dgisselq |
if ((bw_cyc_stb)&&(~i_wb_we))
|
293 |
|
|
begin // Read from the bus
|
294 |
|
|
br_wb_ack <= 1'b1;
|
295 |
|
|
end else if ((bw_cyc_stb)&&(i_wb_we))
|
296 |
|
|
// We did this write above
|
297 |
|
|
br_wb_ack <= 1'b1;
|
298 |
|
|
else // Do nothing if either i_wb_cyc or i_wb_stb are low
|
299 |
|
|
br_wb_ack <= 1'b0;
|
300 |
3 |
dgisselq |
end
|
301 |
|
|
|
302 |
|
|
reg [31:0] nxt_mem;
|
303 |
|
|
always @(posedge i_wb_clk)
|
304 |
25 |
dgisselq |
nxt_mem <= mem[raddr+waddr+
|
305 |
|
|
(((bw_cyc_stb)&&(i_wb_addr)&&(~i_wb_we)) ?
|
306 |
|
|
{{(LGMEM-1){1'b0}},1'b1} : { (LGMEM){1'b0}} )];
|
307 |
3 |
dgisselq |
|
308 |
|
|
wire [4:0] bw_lgmem;
|
309 |
|
|
assign bw_lgmem = LGMEM;
|
310 |
|
|
always @(posedge i_wb_clk)
|
311 |
25 |
dgisselq |
if (~i_wb_addr) // Control register read
|
312 |
3 |
dgisselq |
o_wb_data <= { bw_reset_request,
|
313 |
|
|
bw_stopped,
|
314 |
|
|
bw_triggered,
|
315 |
|
|
bw_primed,
|
316 |
|
|
bw_manual_trigger,
|
317 |
|
|
bw_disable_trigger,
|
318 |
|
|
(raddr == {(LGMEM){1'b0}}),
|
319 |
|
|
bw_lgmem,
|
320 |
|
|
bw_holdoff };
|
321 |
|
|
else if (~bw_stopped) // read, prior to stopping
|
322 |
|
|
o_wb_data <= i_data;
|
323 |
|
|
else // if (i_wb_addr) // Read from FIFO memory
|
324 |
|
|
o_wb_data <= nxt_mem; // mem[raddr+waddr];
|
325 |
|
|
|
326 |
|
|
assign o_wb_stall = 1'b0;
|
327 |
25 |
dgisselq |
assign o_wb_ack = (i_wb_cyc)&&(br_wb_ack);
|
328 |
3 |
dgisselq |
|
329 |
|
|
reg br_level_interrupt;
|
330 |
|
|
initial br_level_interrupt = 1'b0;
|
331 |
|
|
assign o_interrupt = (bw_stopped)&&(~bw_disable_trigger)
|
332 |
|
|
&&(~br_level_interrupt);
|
333 |
|
|
always @(posedge i_wb_clk)
|
334 |
|
|
if ((bw_reset_complete)||(bw_reset_request))
|
335 |
|
|
br_level_interrupt<= 1'b0;
|
336 |
|
|
else
|
337 |
|
|
br_level_interrupt<= (bw_stopped)&&(~bw_disable_trigger);
|
338 |
|
|
|
339 |
|
|
endmodule
|