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 20

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 dinesha
/*********************************************************************
2
 
3
  This file is part of the sdram controller project
4
  http://www.opencores.org/cores/sdr_ctrl/
5
 
6
  Description: SYNC FIFO
7
  Parameters:
8
      W : Width (integer)
9
      D : Depth (integer, power of 2, 4 to 256)
10
 
11
  To Do:
12
    nothing
13
 
14
  Author(s):  Dinesh Annayya, dinesha@opencores.org
15
 
16
 Copyright (C) 2000 Authors and OPENCORES.ORG
17
 
18
 This source file may be used and distributed without
19
 restriction provided that this copyright statement is not
20
 removed from the file and that any derivative work contains
21
 the original copyright notice and the associated disclaimer.
22
 
23
 This source file is free software; you can redistribute it
24
 and/or modify it under the terms of the GNU Lesser General
25
 Public License as published by the Free Software Foundation;
26
 either version 2.1 of the License, or (at your option) any
27
later version.
28
 
29
 This source is distributed in the hope that it will be
30
 useful, but WITHOUT ANY WARRANTY; without even the implied
31
 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
32
 PURPOSE.  See the GNU Lesser General Public License for more
33
 details.
34
 
35
 You should have received a copy of the GNU Lesser General
36
 Public License along with this source; if not, download it
37
 from http://www.opencores.org/lgpl.shtml
38
 
39
*******************************************************************/
40 11 dinesha
 
41
 
42 19 dinesha
module sync_fifo (clk,
43
                  reset_n,
44
                  wr_en,
45
                  wr_data,
46
                  full,
47
                  empty,
48
                  rd_en,
49
                  rd_data);
50 11 dinesha
 
51 19 dinesha
   parameter W = 8;
52
   parameter D = 4;
53 11 dinesha
 
54 20 dinesha
   parameter AW = (D == 2)   ? 1 :
55
                  (D == 4)   ? 2 :
56 19 dinesha
                  (D == 8)   ? 3 :
57
                  (D == 16)  ? 4 :
58
                  (D == 32)  ? 5 :
59
                  (D == 64)  ? 6 :
60
                  (D == 128) ? 7 :
61
                  (D == 256) ? 8 : 0;
62
 
63
   output [W-1 : 0]  rd_data;
64
   input [W-1 : 0]   wr_data;
65
   input             clk, reset_n, wr_en, rd_en;
66
   output            full, empty;
67 11 dinesha
 
68 19 dinesha
   // synopsys translate_off
69 11 dinesha
 
70 19 dinesha
   initial begin
71
      if (AW == 0) begin
72
         $display ("%m : ERROR!!! Fifo depth %d not in range 4 to 256", D);
73
      end // if (AW == 0)
74
   end // initial begin
75 11 dinesha
 
76 19 dinesha
   // synopsys translate_on
77 11 dinesha
 
78
 
79 19 dinesha
   reg [W-1 : 0]    mem[D-1 : 0];
80
   reg [AW-1 : 0]   rd_ptr, wr_ptr;
81
   reg              full, empty;
82 11 dinesha
 
83 19 dinesha
   wire [W-1 : 0]   rd_data;
84
 
85
   always @ (posedge clk or negedge reset_n)
86
      if (reset_n == 1'b0) begin
87
         wr_ptr <= {AW{1'b0}} ;
88
      end
89
      else begin
90
         if (wr_en & !full) begin
91
            wr_ptr <= wr_ptr + 1'b1 ;
92
         end
93
      end
94 11 dinesha
 
95 19 dinesha
   always @ (posedge clk or negedge reset_n)
96
      if (reset_n == 1'b0) begin
97
         rd_ptr <= {AW{1'b0}} ;
98
      end
99
      else begin
100
         if (rd_en & !empty) begin
101
            rd_ptr <= rd_ptr + 1'b1 ;
102
         end
103
      end
104
 
105
 
106
   always @ (posedge clk or negedge reset_n)
107
      if (reset_n == 1'b0) begin
108
         empty <= 1'b1 ;
109
      end
110
      else begin
111
         empty <= (((wr_ptr - rd_ptr) == {{(AW-1){1'b0}}, 1'b1}) & rd_en & ~wr_en) ? 1'b1 :
112
                   ((wr_ptr == rd_ptr) & ~rd_en & wr_en) ? 1'b0 : empty ;
113
      end
114
 
115
   always @ (posedge clk or negedge reset_n)
116
      if (reset_n == 1'b0) begin
117
         full <= 1'b0 ;
118
      end
119
      else begin
120
         full <= (((wr_ptr - rd_ptr) == {{(AW-1){1'b1}}, 1'b0}) & ~rd_en & wr_en) ? 1'b1 :
121
                 (((wr_ptr - rd_ptr) == {AW{1'b1}}) & rd_en & ~wr_en) ? 1'b0 : full ;
122
      end
123
 
124
   always @ (posedge clk)
125
      if (wr_en)
126
         mem[wr_ptr] <= wr_data;
127
 
128
assign  rd_data = mem[rd_ptr];
129
 
130
 
131
// synopsys translate_off
132
   always @(posedge clk) begin
133
      if (wr_en && full) begin
134
         $display("%m : Error! sfifo overflow!");
135
      end
136
   end
137
 
138
   always @(posedge clk) begin
139
      if (rd_en && empty) begin
140
         $display("%m : error! sfifo underflow!");
141
      end
142
   end
143
 
144
// synopsys translate_on
145
//---------------------------------------
146
 
147 11 dinesha
endmodule
148 19 dinesha
 
149
 

powered by: WebSVN 2.1.0

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