-
Notifications
You must be signed in to change notification settings - Fork 3
/
syn_fifo.sv
103 lines (89 loc) · 2.77 KB
/
syn_fifo.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
`timescale 1ns/100ps
// ============================================================================= //
// Designer: Jose Iuri B. de Brito - [email protected] //
// Rubbens Fernandes Roux - [email protected] //
// //
// Design Name: Sybnchronous FIFO //
// Module Name: syn_fifo //
// //
// ============================================================================= //
//
// This file contains the FIFO for comunnication between UART modules and AXI4Lite
// modules
module syn_fifo (
clk , // Clock input
rst , // Active low reset
data_in , // Data input
rd_en , // Read enable
rd_valid , // Read Valid
wr_en , // Write Enable
wr_ready , // Write ready
data_out , // Data Output
empty , // FIFO empty
full // FIFO full
);
// FIFO constants
parameter DATA_WIDTH = 8;
parameter ADDR_WIDTH = 8;
parameter RAM_DEPTH = (1 << ADDR_WIDTH);
// Port Declarations
input logic clk ;
input logic rst ;
input logic rd_en ;
input logic wr_en ;
output logic rd_valid;
output logic wr_ready;
input logic [DATA_WIDTH-1:0] data_in ;
output logic full ;
output logic empty ;
output logic [DATA_WIDTH-1:0] data_out ;
logic [DATA_WIDTH-1:0] fila [RAM_DEPTH];
//-----------Internal variables-------------------
logic [ADDR_WIDTH-1:0] wr_pointer;
logic [ADDR_WIDTH-1:0] rd_pointer;
logic [ADDR_WIDTH :0] status_cnt;
//-----------Variable assignments---------------
assign full = (status_cnt == (RAM_DEPTH-1));
assign empty = (status_cnt == 0);
assign wr_ready = 1'b1;
//-----------Code Start---------------------------
always_ff @(posedge clk or negedge rst)
begin : WRITE_POINTER
if (!rst) begin
wr_pointer <= 0;
end else if (wr_en) begin
wr_pointer <= wr_pointer + 1;
fila[wr_pointer] <= data_in;
end
end
always_ff @(posedge clk or negedge rst)
begin : READ_POINTER
if (!rst) begin
rd_pointer <= 0;
rd_valid <= 0;
end else if (rd_en) begin
rd_valid <= 1;
rd_pointer <= rd_pointer + 1;
endmodule
end
always_ff @(posedge clk or negedge rst)
begin : READ_DATA
if (!rst) begin
data_out <= 0;
end else if (rd_en) begin
data_out <= fila[rd_pointer];
end
end
always_ff @(posedge clk or negedge rst)
begin : STATUS_COUNTER
if (!rst) begin
status_cnt <= 0;
// Read but no write.
end else if ((rd_en) && !(wr_en) && (status_cnt != 0)) begin
status_cnt <= status_cnt - 1;
// Write but no read.
end else if ((wr_en) && !(rd_en) && (status_cnt != RAM_DEPTH)) begin
status_cnt <= status_cnt + 1;
end
end
endmodule