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

Subversion Repositories yifive

[/] [yifive/] [trunk/] [caravel_yifive/] [verilog/] [rtl/] [lib/] [sync_fifo.sv] - Blame information for rev 11

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

Line No. Rev Author Line
1 11 dinesha
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  YiFive cores common library Module                          ////
4
////                                                              ////
5
////  This file is part of the YIFive cores project               ////
6
////  http://www.opencores.org/cores/yifive/                      ////
7
////                                                              ////
8
////  Description                                                 ////
9
////     Sync Fifo with full and empty                            ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////    nothing                                                   ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Dinesh Annayya, dinesha@opencores.org                 ////
16
////                                                              ////
17
////  Revision : June 7, 2021                                     ////
18
////                                                              ////
19
//////////////////////////////////////////////////////////////////////
20
////                                                              ////
21
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
22
////                                                              ////
23
//// This source file may be used and distributed without         ////
24
//// restriction provided that this copyright statement is not    ////
25
//// removed from the file and that any derivative work contains  ////
26
//// the original copyright notice and the associated disclaimer. ////
27
////                                                              ////
28
//// This source file is free software; you can redistribute it   ////
29
//// and/or modify it under the terms of the GNU Lesser General   ////
30
//// Public License as published by the Free Software Foundation; ////
31
//// either version 2.1 of the License, or (at your option) any   ////
32
//// later version.                                               ////
33
////                                                              ////
34
//// This source is distributed in the hope that it will be       ////
35
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
36
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
37
//// PURPOSE.  See the GNU Lesser General Public License for more ////
38
//// details.                                                     ////
39
////                                                              ////
40
//// You should have received a copy of the GNU Lesser General    ////
41
//// Public License along with this source; if not, download it   ////
42
//// from http://www.opencores.org/lgpl.shtml                     ////
43
////                                                              ////
44
//////////////////////////////////////////////////////////////////////
45
 
46
module sync_fifo #(
47
      parameter  DATA_WIDTH  = 32, // Data Width
48
      parameter  ADDR_WIDTH   = 1,  // Address Width
49
      parameter  FIFO_DEPTH   = 2 // FIFO DEPTH
50
 
51
)(
52
       output [DATA_WIDTH-1:0] dout,
53
       input                    rstn,
54
       input                    clk,
55
       input                    wr_en, // Write
56
       input                    rd_en, // Read
57
       input [DATA_WIDTH-1:0]  din,
58
       output                  full,
59
       output                  empty
60
);
61
 
62
 
63
reg [DATA_WIDTH-1:0] ram [FIFO_DEPTH-1:0];
64
reg [ADDR_WIDTH-1:0]  wptr; // write ptr
65
reg [ADDR_WIDTH-1:0]  rptr; // write ptr
66
reg [ADDR_WIDTH:0]    status_cnt; // status counter
67
 
68
 //-----------Variable assignments---------------
69
 assign full  = (status_cnt == FIFO_DEPTH);
70
 assign empty = (status_cnt == 0);
71
 
72
 //-----------Code Start---------------------------
73
 always @ (negedge rstn or posedge clk)
74
 begin : WRITE_POINTER
75
   if (rstn==1'b0) begin
76
     wptr <= 0;
77
   end else if (wr_en ) begin
78
     wptr <= wptr + 1;
79
   end
80
 end
81
 
82
always @ (negedge rstn or posedge clk)
83
begin : READ_POINTER
84
  if (rstn==1'b0) begin
85
    rptr <= 0;
86
  end else if (rd_en) begin
87
    rptr <= rptr + 1;
88
  end
89
end
90
 
91
always @ (negedge rstn or posedge clk)
92
begin : STATUS_COUNTER
93
  if (rstn==1'b0) begin
94
       status_cnt <= 0;
95
  // Read but no write.
96
  end else if (rd_en &&   (!wr_en) && (status_cnt  != 0)) begin
97
    status_cnt <= status_cnt - 1;
98
  // Write but no read.
99
  end else if (wr_en &&  (!rd_en) && (status_cnt  != FIFO_DEPTH)) begin
100
    status_cnt <= status_cnt + 1;
101
  end
102
end
103
 
104
 
105
assign dout = ram[rptr];
106
 
107
always @ (posedge clk)
108
begin
109
  if (wr_en) ram[wptr] <= din;
110
end
111
 
112
 
113
endmodule

powered by: WebSVN 2.1.0

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