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

Subversion Repositories mesi_isc

[/] [mesi_isc/] [trunk/] [src/] [rtl/] [mesi_isc_basic_fifo.v] - Blame information for rev 4

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

Line No. Rev Author Line
1 2 yaira
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
4
////                                                              ////
5
//// This source file may be used and distributed without         ////
6
//// restriction provided that this copyright statement is not    ////
7
//// removed from the file and that any derivative work contains  ////
8
//// the original copyright notice and the associated disclaimer. ////
9
////                                                              ////
10
//// This source file is free software; you can redistribute it   ////
11
//// and/or modify it under the terms of the GNU Lesser General   ////
12
//// Public License as published by the Free Software Foundation; ////
13
//// either version 2.1 of the License, or (at your option) any   ////
14
//// later version.                                               ////
15
////                                                              ////
16
//// This source is distributed in the hope that it will be       ////
17
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
18
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
19
//// PURPOSE.  See the GNU Lesser General Public License for more ////
20
//// details.                                                     ////
21
////                                                              ////
22
//// You should have received a copy of the GNU Lesser General    ////
23
//// Public License along with this source; if not, download it   ////
24
//// from http://www.opencores.org/lgpl.shtml                     ////
25
////                                                              ////
26
//////////////////////////////////////////////////////////////////////
27
 
28
//////////////////////////////////////////////////////////////////////
29
////                                                              ////
30
////  MESI_ISC Project                                            ////
31
////                                                              ////
32
////  Author(s):                                                  ////
33
////      - Yair Amitay       yair.amitay@yahoo.com               ////
34
////                          www.linkedin.com/in/yairamitay      ////
35
////                                                              ////
36
////  Description                                                 ////
37
////  mesi_isc_basic_fifo                                         ////
38
////  -------------------                                         ////
39
////  The basic fifo is a fifo for instantiation in the different ////
40
////  parts of the block                                          ////
41
////                                                              ////
42
////  To Do:                                                      ////
43
////   -                                                          ////
44
////                                                              ////
45
//////////////////////////////////////////////////////////////////////
46
 
47
`include "mesi_isc_define.v"
48
 
49
module mesi_isc_basic_fifo
50
    (
51
     // Inputs
52
     clk,
53
     rst,
54
     wr_i,
55
     rd_i,
56
     data_i,
57
     // Outputs
58
     data_o,
59
     status_empty_o,
60
     status_full_o
61
     );
62
parameter
63
  DATA_WIDTH        = 32,
64
  FIFO_SIZE         = 4,
65
  FIFO_SIZE_LOG2    = 2;
66
 
67
 
68
// Inputs
69
//================================
70
// System
71
input                   clk;          // System clock
72
input                   rst;          // Active high system reset
73
 
74
input                   wr_i;         // Write data to the fifo (store the data)
75
input                   rd_i;         // Read data from the fifo. Data is erased
76
                                      // afterward.
77
input [DATA_WIDTH-1:0]  data_i;       // Data data in to be stored
78
 
79
// Outputs
80
//================================
81
output [DATA_WIDTH-1:0] data_o;       // Data out to be rad 
82
// Status outputs
83
output                  status_empty_o; // There are no valid entries in the
84
                                       // fifo
85
output                  status_full_o; // There are no free entries in the fifo
86
                                       //  all the entries are valid
87
 
88
// Regs
89
//================================
90
reg  [DATA_WIDTH-1:0]   data_o;       // Data out to be rad 
91
reg  [DATA_WIDTH-1:0]   entry [FIFO_SIZE-1:0];  // The fifo entries
92
reg  [FIFO_SIZE_LOG2-1:0] ptr_wr;      // Fifo write pointer
93
reg  [FIFO_SIZE_LOG2-1:0] ptr_rd;      // Fifo read pointer
94
wire [FIFO_SIZE_LOG2-1:0] ptr_rd_plus_1;
95
reg                     status_empty;
96
reg                     status_full;
97
wire [FIFO_SIZE_LOG2-1:0] fifo_depth;  // Number of used entries
98
wire                    fifo_depth_increase;
99
wire                    fifo_depth_decrease;
100
 
101
integer                 i;             // For loop
102
 
103
 
104
`ifdef mesi_isc_debug
105
reg dbg_fifo_overflow;                 // Sticky bit for fifo overflow
106
reg dbg_fifo_underflow;                // Sticky bit for fifo underflow
107
`endif
108
 
109
// Write to the fifo
110
//================================
111
// ptr_wr
112
// entry array
113
always @(posedge clk or posedge rst)
114
  if (rst)
115
  begin
116
     for(i=0; i < FIFO_SIZE; i = i + 1 )
117
       entry[i]    <= 0;
118
     ptr_wr        <= 0;
119
  end
120
  else if (wr_i)
121
  begin
122
     entry[ptr_wr] <= data_i;        // Store the data_i to entry ptr_wr
123
     ptr_wr[FIFO_SIZE_LOG2-1:0] <= ptr_wr[FIFO_SIZE_LOG2-1:0] + 1; // Increase
124
                                     // the write pointer
125
  end
126
 
127
 
128
// Read from the fifo
129
//================================
130
// data_o
131
// The fifo output data_o is sampled. It always contains the data of 
132
// the entry[ptr_rd]; 
133
always @(posedge clk or posedge rst)
134
  if (rst)
135
    data_o[DATA_WIDTH-1:0] <= 0;
136
  else if (status_empty)
137
    data_o[DATA_WIDTH-1:0] <= data_i[DATA_WIDTH-1:0]; // When the fifo is empty
138
                                       // the write data
139
                                       // (if exists) is sampled to the fifo and
140
                                       // to the fifo output. In a case that in
141
                                       // the current cycle there is a write and
142
                                       // in the next cycle there is a read, the
143
                                       // data is ready in the output
144
  else if (rd_i)
145
    data_o[DATA_WIDTH-1:0] <= entry[ptr_rd_plus_1]; // Output the next data if this
146
                                       //  is a read cycle.
147
  else
148
    data_o[DATA_WIDTH-1:0] <= entry[ptr_rd]; // The first data is sampled and
149
                                       //  ready for a read
150
// ptr_rd
151
always @(posedge clk or posedge rst)
152
  if (rst)
153
    ptr_rd[FIFO_SIZE_LOG2-1:0] <= 0;
154
  else if (rd_i)
155
    ptr_rd[FIFO_SIZE_LOG2-1:0] <= ptr_rd[FIFO_SIZE_LOG2-1:0] + 1; // Increase the
156
                                       //  read pointer
157
 
158
assign ptr_rd_plus_1 = ptr_rd + 1;
159
 
160
// Status
161
//================================
162
assign  status_empty_o        = status_empty;
163
assign  status_full_o         = status_full;
164
 
165
// status_empty
166
// status_empty is set when there are no any valid entries
167
always @(posedge clk or posedge rst)
168
  // On reset the fifo is empty
169
  if (rst)
170
                                                    status_empty <= 1;
171
  // There is one valid entry which is read (without write another entry)
172
  else if (fifo_depth == 1 & fifo_depth_decrease)
173
                                                    status_empty <= 1;
174
  // The fifo is empty and it is in a write cycle (without read)
175
  // The fifo_depth == 0 when the fifo is empty and when it is full
176
  else if (fifo_depth == 0   &
177
           status_empty      &
178
           fifo_depth_increase)
179
                                                    status_empty <= 0;
180
 
181
always @(posedge clk or posedge rst)
182
  // On reset the fifo not full
183
  if (rst)
184
                                                    status_full  <= 0;
185
  // There is free entry which is written (without read other entry)
186
  else if (fifo_depth == FIFO_SIZE-1 & fifo_depth_increase)
187
                                                    status_full  <= 1;
188
  // The fifo is full and it is in a read cycle (without write)
189
  // The fifo_depth == 0 when the fifo is empty and when it is full
190
  else if (fifo_depth == 0 &
191
           status_full     &
192
           fifo_depth_decrease)
193
                                                    status_full  <= 0;
194
 
195
 
196
// The depth of the used fifo's entries is increased when there is a write
197
// and there is no a read
198
assign fifo_depth_increase     = wr_i & !rd_i;
199
 
200
// The depth of the used fifo's entries is decreased when there is a write
201
// and there is no a read
202
assign fifo_depth_decrease     = !wr_i & rd_i;
203
// In other cases (ptr_wr & ptr_rd) or (!ptr_wr & !ptr_rd) the number of the
204
// valid entries remains the same
205
 
206
// Because the buffer is cyclic the depth is always correct
207
assign fifo_depth[FIFO_SIZE_LOG2-1:0] = ptr_wr[FIFO_SIZE_LOG2-1:0] -
208
                                        ptr_rd[FIFO_SIZE_LOG2-1:0];
209
 
210
`ifdef mesi_isc_debug
211
// Debug
212
//================================
213
// dbg_fifo_overflow is a sticky bit which is set when writing (without reading)
214
// to a full fifo
215
// dbg_fifo_underflow is a sticky bit which is set when reading from an empty
216
// fifo
217
always @(posedge clk or posedge rst)
218
  if (rst)
219
    begin
220
     dbg_fifo_overflow   <= 0;
221
     dbg_fifo_underflow  <= 0;
222
    end
223
  else
224
  begin
225
     dbg_fifo_overflow   <= dbg_fifo_overflow |
226
                            (status_full & fifo_depth_increase);
227
     dbg_fifo_underflow  <= dbg_fifo_underflow  |
228
                            (status_empty & fifo_depth_decrease);
229
  end
230
`endif
231
 
232
endmodule
233
 

powered by: WebSVN 2.1.0

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