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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [rtl/] [Fifo.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see .
17
 
18
module Fifo(input logic clk,
19
            input logic reset,
20
            // Write port
21
            input logic wr_en,
22
            input logic [data_width-1:0] wr_data,
23
            // Read port
24
            input logic rd_en,
25
            output logic [data_width-1:0] rd_data,
26
            output logic empty,
27
            output logic nearly_full,
28
            output logic full);
29
 
30
parameter data_width = 32;
31
parameter depth = 6;
32
parameter full_threshold = 2; // Number of entries free to be not-full
33
 
34
localparam ptr_bits = $clog2(depth);
35
 
36
reg [data_width-1:0] mem[depth-1:0];
37
reg [ptr_bits-1:0] rd_ptr;
38
reg [ptr_bits-1:0] wr_ptr;
39
reg [ptr_bits:0] count;
40
 
41
assign empty = count == 0;
42
assign full = count == depth;
43
assign nearly_full = count >= depth - full_threshold;
44
 
45
assign rd_data = mem[rd_ptr];
46
 
47
always_ff @(posedge clk or posedge reset) begin
48
    if (reset) begin
49
        wr_ptr <= {ptr_bits{1'b0}};
50
    end else if (wr_en && !full) begin
51
        mem[wr_ptr] <= wr_data;
52
        wr_ptr <= wr_ptr + 1'b1;
53
        if (wr_ptr == depth[ptr_bits-1:0] - 1'b1)
54
            wr_ptr <= {ptr_bits{1'b0}};
55
    end
56
end
57
 
58
always_ff @(posedge clk or posedge reset) begin
59
    if (reset) begin
60
        rd_ptr <= {ptr_bits{1'b0}};
61
    end else if (rd_en && !empty) begin
62
        rd_ptr <= rd_ptr + 1'b1;
63
        if (rd_ptr == depth[ptr_bits-1:0] - 1'b1)
64
            rd_ptr <= {ptr_bits{1'b0}};
65
    end
66
end
67
 
68
always_ff @(posedge clk or posedge reset) begin
69
    if (reset)
70
        count <= {ptr_bits + 1{1'b0}};
71
    else if (wr_en && !full && !rd_en)
72
        count <= count + 1'b1;
73
    else if (rd_en && !empty && !wr_en)
74
        count <= count - 1'b1;
75
end
76
 
77
endmodule

powered by: WebSVN 2.1.0

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