-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathfifo.sv
67 lines (60 loc) · 1.34 KB
/
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
module fifo (
clk,
resetN,
write_en,
read_en,
data_in,
data_out,
empty,
full
);
parameter FIFO_WIDTH = 8;
parameter FIFO_DEPTH = 16;
input clk;
input resetN;
input write_en;
input read_en;
input [FIFO_WIDTH-1:0] data_in;
output [FIFO_WIDTH-1:0] data_out;
output empty;
output full;
wire clk;
wire write_en;
wire read_en;
wire [FIFO_WIDTH-1:0] data_in;
reg [FIFO_WIDTH-1:0] data_out;
wire empty;
wire full;
reg [FIFO_WIDTH-1:0] ram [0:FIFO_DEPTH];
reg tmp_empty;
reg tmp_full;
integer write_ptr;
integer read_ptr;
always@(negedge resetN) begin
data_out = 0;
tmp_empty = 1'b1;
tmp_full = 1'b0;
write_ptr = 0;
read_ptr = 0;
end
assign empty = tmp_empty;
assign full = tmp_full;
always @(posedge clk) begin
if ((write_en == 1'b1) && (tmp_full == 1'b0)) begin
ram[write_ptr] = data_in;
tmp_empty <= 1'b0;
write_ptr = (write_ptr + 1) % FIFO_DEPTH;
if ( read_ptr == write_ptr ) begin
tmp_full <= 1'b1;
end
end
if ((read_en == 1'b1) && (tmp_empty == 1'b0)) begin
data_out <= ram[read_ptr];
tmp_full <= 1'b0;
read_ptr = (read_ptr + 1) % FIFO_DEPTH;
if ( read_ptr == write_ptr ) begin
tmp_empty <= 1'b1;
end
end
end
endmodule //fifo