1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
module simple_fifo(
|
28 |
|
|
input clk,
|
29 |
|
|
input rst_n,
|
30 |
|
|
input sclr,
|
31 |
|
|
|
32 |
|
|
input rdreq,
|
33 |
|
|
input wrreq,
|
34 |
|
|
input [width-1:0] data,
|
35 |
|
|
|
36 |
|
|
output empty,
|
37 |
|
|
output reg full,
|
38 |
|
|
output [width-1:0] q,
|
39 |
|
|
output reg [widthu-1:0] usedw
|
40 |
|
|
);
|
41 |
|
|
|
42 |
|
|
parameter width = 1;
|
43 |
|
|
parameter widthu = 1;
|
44 |
|
|
|
45 |
|
|
reg [width-1:0] mem [(2**widthu)-1:0];
|
46 |
|
|
|
47 |
|
|
reg [widthu-1:0] rd_index = 0;
|
48 |
|
|
reg [widthu-1:0] wr_index = 0;
|
49 |
|
|
|
50 |
|
|
assign q = mem[rd_index];
|
51 |
|
|
assign empty= usedw == 0 && ~(full);
|
52 |
|
|
|
53 |
|
|
always @(posedge clk or negedge rst_n) begin
|
54 |
|
|
if(rst_n == 1'b0) rd_index <= 0;
|
55 |
|
|
else if(sclr) rd_index <= 0;
|
56 |
|
|
else if(rdreq && ~(empty)) rd_index <= rd_index + { {widthu-1{1'b0}}, 1'b1 };
|
57 |
|
|
end
|
58 |
|
|
|
59 |
|
|
always @(posedge clk or negedge rst_n) begin
|
60 |
|
|
if(rst_n == 1'b0) wr_index <= 0;
|
61 |
|
|
else if(sclr) wr_index <= 0;
|
62 |
|
|
else if(wrreq && (~(full) || rdreq)) wr_index <= wr_index + { {widthu-1{1'b0}}, 1'b1 };
|
63 |
|
|
end
|
64 |
|
|
|
65 |
|
|
always @(posedge clk) begin
|
66 |
|
|
if(wrreq && (~(full) || rdreq)) mem[wr_index] <= data;
|
67 |
|
|
end
|
68 |
|
|
|
69 |
|
|
always @(posedge clk or negedge rst_n) begin
|
70 |
|
|
if(rst_n == 1'b0) full <= 1'b0;
|
71 |
|
|
else if(sclr) full <= 1'b0;
|
72 |
|
|
else if(rdreq && ~(wrreq) && full) full <= 1'b0;
|
73 |
|
|
else if(~(rdreq) && wrreq && ~(full) && usedw == (2**widthu)-1) full <= 1'b1;
|
74 |
|
|
end
|
75 |
|
|
|
76 |
|
|
always @(posedge clk or negedge rst_n) begin
|
77 |
|
|
if(rst_n == 1'b0) usedw <= 0;
|
78 |
|
|
else if(sclr) usedw <= 0;
|
79 |
|
|
else if(rdreq && ~(wrreq) && ~(empty)) usedw <= usedw - { {widthu-1{1'b0}}, 1'b1 };
|
80 |
|
|
else if(~(rdreq) && wrreq && ~(full)) usedw <= usedw + { {widthu-1{1'b0}}, 1'b1 };
|
81 |
|
|
else if(rdreq && wrreq && empty) usedw <= { {widthu-1{1'b0}}, 1'b1 };
|
82 |
|
|
end
|
83 |
|
|
|
84 |
|
|
endmodule
|