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

Subversion Repositories wbscope

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

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

Line No. Rev Author Line
1 12 dgisselq
`timescale 1 ns / 1 ps
2
////////////////////////////////////////////////////////////////////////////////
3
//
4
// Filename:    axi4lscope.v
5
//
6
// Project:     FPGA Library of Routines
7
//
8
// Purpose:     This is a generic/library routine for providing a bus accessed
9
//      'scope' or (perhaps more appropriately) a bus accessed logic analyzer.
10
//      The general operation is such that this 'scope' can record and report
11
//      on any 32 bit value transiting through the FPGA.  Once started and
12
//      reset, the scope records a copy of the input data every time the clock
13
//      ticks with the circuit enabled.  That is, it records these values up
14
//      until the trigger.  Once the trigger goes high, the scope will record
15
//      for bw_holdoff more counts before stopping.  Values may then be read
16
//      from the 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
//      And, of course, since AXI wants to be particular about their port
59
//      naming conventions, anything beginning with
60
//
61
//      S_AXI_
62
//
63
//      is a signal associated with this function as an AXI slave.
64
//      
65
//
66
// Creator:     Dan Gisselquist, Ph.D.
67
//              Gisselquist Technology, LLC
68
//
69
////////////////////////////////////////////////////////////////////////////////
70
//
71
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
72
//
73
// This program is free software (firmware): you can redistribute it and/or
74
// modify it under the terms of  the GNU General Public License as published
75
// by the Free Software Foundation, either version 3 of the License, or (at
76
// your option) any later version.
77
//
78
// This program is distributed in the hope that it will be useful, but WITHOUT
79
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
80
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
81
// for more details.
82
//
83
// You should have received a copy of the GNU General Public License along
84
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
85
// target there if the PDF file isn't present.)  If not, see
86
// <http://www.gnu.org/licenses/> for a copy.
87
//
88
// License:     GPL, v3, as defined and found on www.gnu.org,
89
//              http://www.gnu.org/licenses/gpl.html
90
//
91
//
92
////////////////////////////////////////////////////////////////////////////////
93
//
94
//
95
module axi4lscope
96
        #(
97
                // Users to add parameters here
98
                parameter       LGMEM = 5'd10,
99
                parameter       BUSW = 32,
100
                parameter       SYNCHRONOUS=1,
101
                parameter       DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4),
102
                // User parameters ends
103
                // DO NOT EDIT BELOW THIS LINE ---------------------
104
                // Do not modify the parameters beyond this line
105
                // Width of S_AXI data bus
106
                parameter integer C_S_AXI_DATA_WIDTH    = 32,
107
                // Width of S_AXI address bus
108
                parameter integer C_S_AXI_ADDR_WIDTH    = 4
109
        )
110
        (
111
                // Users to add ports here
112
                input wire      i_clk,  // The data clock, can be set to ACLK
113
                input wire      i_ce,   // = '1' when recordable data is present
114
                input wire      i_trigger,// = '1' when interesting event hapns
115
                input wire      [31:0]   i_data,
116
                output  wire    o_interrupt,    // ='1' when scope has stopped
117
                // User ports ends
118
                // DO NOT EDIT BELOW THIS LINE ---------------------
119
                // Do not modify the ports beyond this line
120
                // Global Clock Signal
121
                input wire  S_AXI_ACLK,
122
                // Global Reset Signal. This Signal is Active LOW
123
                input wire  S_AXI_ARESETN,
124
                // Write address (issued by master, acceped by Slave)
125
                input wire [C_S_AXI_ADDR_WIDTH-1 : 0] S_AXI_AWADDR,
126
                // Write channel Protection type. This signal indicates the
127
                // privilege and security level of the transaction, and whether
128
                // the transaction is a data access or an instruction access.
129
                input wire [2 : 0] S_AXI_AWPROT,
130
                // Write address valid. This signal indicates that the master
131
                // signaling valid write address and control information.
132
                input wire  S_AXI_AWVALID,
133
                // Write address ready. This signal indicates that the slave
134
                // is ready to accept an address and associated control signals.
135
                output wire  S_AXI_AWREADY,
136
                // Write data (issued by master, acceped by Slave) 
137
                input wire [C_S_AXI_DATA_WIDTH-1 : 0] S_AXI_WDATA,
138
                // Write strobes. This signal indicates which byte lanes hold
139
                // valid data. There is one write strobe bit for each eight
140
                // bits of the write data bus.    
141
                input wire [(C_S_AXI_DATA_WIDTH/8)-1 : 0] S_AXI_WSTRB,
142
                // Write valid. This signal indicates that valid write
143
                // data and strobes are available.
144
                input wire  S_AXI_WVALID,
145
                // Write ready. This signal indicates that the slave
146
                // can accept the write data.
147
                output wire  S_AXI_WREADY,
148
                // Write response. This signal indicates the status
149
                // of the write transaction.
150
                output wire [1 : 0] S_AXI_BRESP,
151
                // Write response valid. This signal indicates that the channel
152
                // is signaling a valid write response.
153
                output wire  S_AXI_BVALID,
154
                // Response ready. This signal indicates that the master
155
                // can accept a write response.
156
                input wire  S_AXI_BREADY,
157
                // Read address (issued by master, acceped by Slave)
158
                input wire [C_S_AXI_ADDR_WIDTH-1 : 0] S_AXI_ARADDR,
159
                // Protection type. This signal indicates the privilege
160
                // and security level of the transaction, and whether the
161
                // transaction is a data access or an instruction access.
162
                input wire [2 : 0] S_AXI_ARPROT,
163
                // Read address valid. This signal indicates that the channel
164
                // is signaling valid read address and control information.
165
                input wire  S_AXI_ARVALID,
166
                // Read address ready. This signal indicates that the slave is
167
                // ready to accept an address and associated control signals.
168
                output wire  S_AXI_ARREADY,
169
                // Read data (issued by slave)
170
                output wire [C_S_AXI_DATA_WIDTH-1 : 0] S_AXI_RDATA,
171
                // Read response. This signal indicates the status of the
172
                // read transfer.
173
                output wire [1 : 0] S_AXI_RRESP,
174
                // Read valid. This signal indicates that the channel is
175
                // signaling the required read data.
176
                output wire  S_AXI_RVALID,
177
                // Read ready. This signal indicates that the master can
178
                // accept the read data and response information.
179
                input wire  S_AXI_RREADY
180
                // DO NOT EDIT ABOVE THIS LINE ---------------------
181
        );
182
 
183
        // AXI4LITE signals
184
        reg [C_S_AXI_ADDR_WIDTH-1 : 0]   axi_awaddr;
185
        reg                             axi_awready;
186
        reg                             axi_wready;
187
        // reg          [1 : 0]         axi_bresp;
188
        reg                             axi_bvalid;
189
        reg [C_S_AXI_ADDR_WIDTH-1 : 0]   axi_araddr;
190
        reg                             axi_arready;
191
        reg [C_S_AXI_DATA_WIDTH-1 : 0]   axi_rdata;
192
        // reg           [1 : 0]        axi_rresp;
193
        reg                             axi_rvalid;
194
 
195
 
196
        ///////////////////////////////////////////////////
197
        //
198
        // Decode and handle the AXI/Bus signaling
199
        //
200
        ///////////////////////////////////////////////////
201
        //
202
        // Sadly, the AXI bus is *way* more complicated to
203
        // deal with than it needs to be.  Still, we offer
204
        // the following as a simple means of dealing with
205
        // it.  The majority of the code in this section 
206
        // comes directly from a Xilinx/Vivado generated
207
        // file.
208
        //
209
        // Gisselquist Technology, LLC, claims no copyright
210
        // or ownership of this section of the code.
211
        //
212
        wire    i_reset;
213
        assign  i_reset = !S_AXI_ARESETN;
214
 
215
        always @(posedge S_AXI_ACLK)
216
                if (i_reset)
217
                        axi_awready <= 1'b0;
218
                else if ((!axi_awready)&&(S_AXI_AWVALID)&&(S_AXI_WVALID))
219
                        axi_awready <= 1'b1;
220
                else
221
                        axi_awready <= 1'b0;
222
        assign  S_AXI_AWREADY = axi_awready;
223
 
224
        always @(posedge S_AXI_ACLK)
225
                if ((!axi_awready)&&(S_AXI_AWVALID)&&(S_AXI_WVALID))
226
                        axi_awaddr <= S_AXI_AWADDR;
227
 
228
        always @(posedge S_AXI_ACLK)
229
                if (i_reset)
230
                        axi_wready <= 1'b0;
231
                else if ((!axi_wready)&&(S_AXI_WVALID)&&(S_AXI_AWVALID))
232
                        axi_wready <= 1'b1;
233
                else
234
                        axi_wready <= 1'b0;
235
        assign  S_AXI_WREADY = axi_wready;
236
 
237
        wire    write_stb;
238
 
239
        always @(posedge S_AXI_ACLK)
240
                if (i_reset)
241
                begin
242
                        axi_bvalid <= 0;
243
                        // axi_bresp <= 2'b00;
244
                end else if ((~axi_bvalid)&&(write_stb))
245
                begin
246
                        axi_bvalid <= 1'b1;
247
                        // axi_bresp <= 2'b00; // 'Okay' response
248
                end else if ((S_AXI_BREADY)&&(axi_bvalid))
249
                        axi_bvalid <= 1'b0;
250
        assign  S_AXI_BRESP = 2'b00;    // An 'OKAY' response
251
        assign  S_AXI_BVALID= axi_bvalid;
252
 
253
 
254
 
255
        always @(posedge S_AXI_ACLK)
256
                if (i_reset)
257
                begin
258
                        axi_arready <= 1'b0;
259
                        axi_araddr <= 0;
260
                end else if ((!axi_arready)&&(S_AXI_ARVALID))
261
                begin
262
                        axi_arready <= 1'b1;
263
                        axi_araddr <= S_AXI_ARADDR;
264
                end else
265
                        axi_arready <= 1'b0;
266
        assign  S_AXI_ARREADY = axi_arready;
267
 
268
        always @(posedge S_AXI_ACLK)
269
                if (i_reset)
270
                begin
271
                        axi_rvalid <= 0;
272
                        // axi_rresp  <= 0;
273
                end else if ((axi_arready)&&(S_AXI_ARVALID)&&(!axi_rvalid))
274
                begin
275
                        axi_rvalid <= 1'b0;
276
                        // axi_rresp <= 2'b00;
277
                end else if ((axi_rvalid)&&(S_AXI_RREADY))
278
                        axi_rvalid <= 1'b0;
279
        assign  S_AXI_RVALID = axi_rvalid;
280
        assign  S_AXI_RRESP  = 2'b00;
281
 
282
 
283
 
284
 
285
        ///////////////////////////////////////////////////
286
        //
287
        // Final simplification of the AXI code
288
        //
289
        ///////////////////////////////////////////////////
290
        //
291
        // Now that we've provided all of the bus signaling
292
        // above, can we make any sense of it?
293
        //
294
        // The following wires are here to provide some
295
        // simplification of the complex bus protocol.  In
296
        // particular, are we reading or writing during this
297
        // clock?  The two *should* be mutually exclusive
298
        // (i.e., you *shouldn't* be able to both read and
299
        // write on the same clock) ... but Xilinx's default
300
        // implementation does nothing to ensure that this
301
        // would be the case.
302
        //
303
        // From here on down, Gisselquist Technology, LLC,
304
        // claims a copyright on the code.
305
        //
306
        wire    read_from_data;
307
        assign  read_from_data = (S_AXI_ARVALID)&&(S_AXI_ARREADY)
308
                                        &&(axi_araddr[0]);
309
 
310
        assign  write_stb = ((axi_awready)&&(S_AXI_AWVALID)
311
                                &&(axi_wready)&&(S_AXI_WVALID));
312
        wire    write_to_control;
313
        assign  write_to_control = (write_stb)&&(!axi_awaddr[0]);
314
 
315
 
316
        wire    [31:0]   i_wb_data;
317
        assign  i_wb_data = S_AXI_WDATA;
318
 
319
 
320
        ///////////////////////////////////////////////////
321
        //
322
        // The actual SCOPE
323
        //
324
        ///////////////////////////////////////////////////
325
        //
326
        // Now that we've finished reading/writing from the
327
        // bus, ... or at least acknowledging reads and 
328
        // writes from and to the bus--even if they haven't
329
        // happened yet, now we implement our actual scope.
330
        // This includes implementing the actual reads/writes
331
        // from/to the bus.
332
        //
333
        // From here on down, is the heart of the scope itself.
334
        //
335
        reg     [(LGMEM-1):0]    raddr;
336
        reg     [(BUSW-1):0]     mem[0:((1<<LGMEM)-1)];
337
 
338
        // Our status/config register
339
        wire            bw_reset_request, bw_manual_trigger,
340
                        bw_disable_trigger, bw_reset_complete;
341
        reg     [22:0]   br_config;
342
        wire    [19:0]   bw_holdoff;
343
        initial br_config = DEFAULT_HOLDOFF;
344
        always @(posedge S_AXI_ACLK)
345
                if (write_to_control)
346
                begin
347
                        br_config <= { i_wb_data[31],
348
                                (i_wb_data[27]),
349
                                i_wb_data[26],
350
                                i_wb_data[19:0] };
351
                end else if (bw_reset_complete)
352
                        br_config[22] <= 1'b1;
353
        assign  bw_reset_request   = (~br_config[22]);
354
        assign  bw_manual_trigger  = (br_config[21]);
355
        assign  bw_disable_trigger = (br_config[20]);
356
        assign  bw_holdoff         = br_config[19:0];
357
 
358
        wire    dw_reset, dw_manual_trigger, dw_disable_trigger;
359
        generate
360
        if (SYNCHRONOUS > 0)
361
        begin
362
                assign  dw_reset = bw_reset_request;
363
                assign  dw_manual_trigger = bw_manual_trigger;
364
                assign  dw_disable_trigger = bw_disable_trigger;
365
                assign  bw_reset_complete = bw_reset_request;
366
        end else begin
367
                reg             r_reset_complete;
368
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_iflags;
369
                reg     [2:0]    r_iflags;
370
 
371
                // Resets are synchronous to the bus clock, not the data clock
372
                // so do a clock transfer here
373
                initial q_iflags = 3'b000;
374
                initial r_reset_complete = 1'b0;
375
                always @(posedge i_clk)
376
                begin
377
                        q_iflags <= { bw_reset_request, bw_manual_trigger, bw_disable_trigger };
378
                        r_iflags <= q_iflags;
379
                        r_reset_complete <= (dw_reset);
380
                end
381
 
382
                assign  dw_reset = r_iflags[2];
383
                assign  dw_manual_trigger = r_iflags[1];
384
                assign  dw_disable_trigger = r_iflags[0];
385
 
386
                (* ASYNC_REG = "TRUE" *) reg    q_reset_complete;
387
                reg     qq_reset_complete;
388
                // Pass an acknowledgement back from the data clock to the bus
389
                // clock that the reset has been accomplished
390
                initial q_reset_complete = 1'b0;
391
                initial qq_reset_complete = 1'b0;
392
                always @(posedge S_AXI_ACLK)
393
                begin
394
                        q_reset_complete  <= r_reset_complete;
395
                        qq_reset_complete <= q_reset_complete;
396
                end
397
 
398
                assign bw_reset_complete = qq_reset_complete;
399
        end endgenerate
400
 
401
        //
402
        // Set up the trigger
403
        //
404
        //
405
        // Write with the i-clk, or input clock.  All outputs read with the
406
        // WISHBONE-clk, or S_AXI_ACLK clock.
407
        reg     dr_triggered, dr_primed;
408
        wire    dw_trigger;
409
        assign  dw_trigger = (dr_primed)&&(
410
                                ((i_trigger)&&(~dw_disable_trigger))
411
                                ||(dr_triggered)
412
                                ||(dw_manual_trigger));
413
        initial dr_triggered = 1'b0;
414
        always @(posedge i_clk)
415
                if (dw_reset)
416
                        dr_triggered <= 1'b0;
417
                else if ((i_ce)&&(dw_trigger))
418
                        dr_triggered <= 1'b1;
419
 
420
        //
421
        // Determine when memory is full and capture is complete
422
        //
423
        // Writes take place on the data clock
424
        reg             dr_stopped;
425
        reg     [19:0]   counter;        // This is unsigned
426
        initial dr_stopped = 1'b0;
427
        initial counter = 20'h0000;
428
        always @(posedge i_clk)
429
                if (dw_reset)
430
                begin
431
                        counter <= 0;
432
                        dr_stopped <= 1'b0;
433
                end else if ((i_ce)&&(dr_triggered))
434
                begin // MUST BE a < and not <=, so that we can keep this w/in
435
                        // 20 bits.  Else we'd need to add a bit to comparison 
436
                        // here.
437
                        if (counter < bw_holdoff)
438
                                counter <= counter + 20'h01;
439
                        else
440
                                dr_stopped <= 1'b1;
441
                end
442
 
443
        //
444
        //      Actually do our writes to memory.  Record, via 'primed' when
445
        //      the memory is full.
446
        //
447
        //      The 'waddr' address that we are using really crosses two clock
448
        //      domains.  While writing and changing, it's in the data clock
449
        //      domain.  Once stopped, it becomes part of the bus clock domain.
450
        //      The clock transfer on the stopped line handles the clock
451
        //      transfer for these signals.
452
        //
453
        reg     [(LGMEM-1):0]    waddr;
454
        initial waddr = {(LGMEM){1'b0}};
455
        initial dr_primed = 1'b0;
456
        always @(posedge i_clk)
457
                if (dw_reset) // For simulation purposes, supply a valid value
458
                begin
459
                        waddr <= 0; // upon reset.
460
                        dr_primed <= 1'b0;
461
                end else if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff)))
462
                begin
463
                        // mem[waddr] <= i_data;
464
                        waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1};
465
                        dr_primed <= (dr_primed)||(&waddr);
466
                end
467
        always @(posedge i_clk)
468
                if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff)))
469
                        mem[waddr] <= i_data;
470
 
471
        //
472
        // Clock transfer of the status signals
473
        //
474
        wire    bw_stopped, bw_triggered, bw_primed;
475
        generate
476
        if (SYNCHRONOUS > 0)
477
        begin
478
                assign  bw_stopped   = dr_stopped;
479
                assign  bw_triggered = dr_triggered;
480
                assign  bw_primed    = dr_primed;
481
        end else begin
482
                // These aren't a problem, since none of these are strobe
483
                // signals.  They goes from low to high, and then stays high
484
                // for many clocks.  Swapping is thus easy--two flip flops to
485
                // protect against meta-stability and we're done.
486
                //
487
                (* ASYNC_REG = "TRUE" *) reg    [2:0]    q_oflags;
488
                reg     [2:0]    r_oflags;
489
                initial q_oflags = 3'h0;
490
                initial r_oflags = 3'h0;
491
                always @(posedge S_AXI_ACLK)
492
                        if (bw_reset_request)
493
                        begin
494
                                q_oflags <= 3'h0;
495
                                r_oflags <= 3'h0;
496
                        end else begin
497
                                q_oflags <= { dr_stopped, dr_triggered, dr_primed };
498
                                r_oflags <= q_oflags;
499
                        end
500
 
501
                assign  bw_stopped   = r_oflags[2];
502
                assign  bw_triggered = r_oflags[1];
503
                assign  bw_primed    = r_oflags[0];
504
        end endgenerate
505
 
506
        // Reads use the bus clock
507
        reg     br_wb_ack;
508
        initial br_wb_ack = 1'b0;
509
        always @(posedge S_AXI_ACLK)
510
        begin
511
                if ((bw_reset_request)||((write_stb)&&(axi_awaddr[0])))
512
                        raddr <= 0;
513
                else if ((read_from_data)&&(bw_stopped))
514
                        // Data read ... only takes place when stopped
515
                        raddr <= raddr + {{(LGMEM-1){1'b0}},1'b1};
516
        end
517
 
518
 
519
        reg     [31:0]   nxt_mem;
520
        always @(posedge S_AXI_ACLK)
521
                nxt_mem <= mem[raddr+waddr+ ((read_from_data) ?
522
                                {{(LGMEM-1){1'b0}},1'b1} : { (LGMEM){1'b0}} )];
523
 
524
 
525
 
526
 
527
 
528
 
529
 
530
        wire    [4:0]    bw_lgmem;
531
        assign          bw_lgmem = LGMEM;
532
        always @(posedge S_AXI_ACLK)
533
                if (~axi_araddr[0]) // Control register read
534
                        axi_rdata <= { bw_reset_request,
535
                                        bw_stopped,
536
                                        bw_triggered,
537
                                        bw_primed,
538
                                        bw_manual_trigger,
539
                                        bw_disable_trigger,
540
                                        (raddr == {(LGMEM){1'b0}}),
541
                                        bw_lgmem,
542
                                        bw_holdoff  };
543
                else if (~bw_stopped) // read, prior to stopping
544
                        axi_rdata <= i_data;
545
                else // if (i_wb_addr) // Read from FIFO memory
546
                        axi_rdata <= nxt_mem; // mem[raddr+waddr];
547
        assign  S_AXI_RDATA = axi_rdata;
548
 
549
 
550
        reg     br_level_interrupt;
551
        initial br_level_interrupt = 1'b0;
552
        assign  o_interrupt = (bw_stopped)&&(~bw_disable_trigger)
553
                                        &&(~br_level_interrupt);
554
        always @(posedge S_AXI_ACLK)
555
                if ((bw_reset_complete)||(bw_reset_request))
556
                        br_level_interrupt<= 1'b0;
557
                else
558
                        br_level_interrupt<= (bw_stopped)&&(~bw_disable_trigger);
559
 
560
endmodule
561
 
562
 

powered by: WebSVN 2.1.0

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