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

Subversion Repositories usb2uart

[/] [usb2uart/] [trunk/] [rtl/] [lib/] [sync_fifo.v] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 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
 
41
 
42
module sync_fifo (clk,
43
                  reset_n,
44
                  clr,
45
                  wr_en,
46
                  wr_data,
47
                  full,
48
                  empty,
49
                  rd_en,
50
                  rd_data);
51
 
52
   parameter W = 8;
53
   parameter D = 4;
54
 
55
   parameter AW = (D == 4)   ? 2 :
56
                  (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, clr,wr_en, rd_en;
66
   output            full, empty;
67
 
68
   // synopsys translate_off
69
 
70
   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
 
76
   // synopsys translate_on
77
 
78
 
79
   reg [W-1 : 0]    mem[D-1 : 0];
80
   reg [AW-1 : 0]   rd_ptr, wr_ptr;
81
   reg              full, empty;
82
 
83
 
84
   wire [W-1 : 0]   rd_data;
85
 
86
   always @ (posedge clk or negedge reset_n)
87
      if (reset_n == 1'b0) begin
88
         wr_ptr <= {AW{1'b0}} ;
89
      end
90
      else begin
91
         if(clr)   wr_ptr <= {AW{1'b0}} ;
92
         else begin
93
            if (wr_en & !full) begin
94
               wr_ptr <= wr_ptr + 1'b1 ;
95
            end
96
         end
97
      end
98
 
99
   always @ (posedge clk or negedge reset_n)
100
      if (reset_n == 1'b0) begin
101
         rd_ptr <= {AW{1'b0}} ;
102
      end
103
      else begin
104
         if(clr)   rd_ptr <= {AW{1'b0}} ;
105
         else begin
106
            if (rd_en & !empty) begin
107
               rd_ptr <= rd_ptr + 1'b1 ;
108
            end
109
         end
110
      end
111
 
112
 
113
   always @ (posedge clk or negedge reset_n)
114
      if (reset_n == 1'b0) begin
115
         empty <= 1'b1 ;
116
      end
117
      else begin
118
         empty <= (((wr_ptr - rd_ptr) == {{(AW-1){1'b0}}, 1'b1}) & rd_en & ~wr_en) ? 1'b1 :
119
                   ((wr_ptr == rd_ptr) & ~rd_en & wr_en) ? 1'b0 : empty ;
120
      end
121
 
122
   always @ (posedge clk or negedge reset_n)
123
      if (reset_n == 1'b0) begin
124
         full <= 1'b0 ;
125
      end
126
      else begin
127
         full <= (((wr_ptr - rd_ptr) == {{(AW-1){1'b1}}, 1'b0}) & ~rd_en & wr_en) ? 1'b1 :
128
                 (((wr_ptr - rd_ptr) == {AW{1'b1}}) & rd_en & ~wr_en) ? 1'b0 : full ;
129
      end
130
 
131
   always @ (posedge clk)
132
      if (wr_en)
133
         mem[wr_ptr] <= wr_data;
134
 
135
assign  rd_data = mem[rd_ptr];
136
 
137
 
138
// synopsys translate_off
139
   always @(posedge clk) begin
140
      if (wr_en && full) begin
141
         $display("%m : Error! sfifo overflow!");
142
      end
143
   end
144
 
145
   always @(posedge clk) begin
146
      if (rd_en && empty) begin
147
         $display("%m : error! sfifo underflow!");
148
      end
149
   end
150
 
151
// synopsys translate_on
152
//---------------------------------------
153
 
154
endmodule
155
 
156
 

powered by: WebSVN 2.1.0

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