-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo_buffer.v
59 lines (52 loc) · 1.07 KB
/
fifo_buffer.v
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
module fifo_buffer
(
input clock, reset_n,
input write_enable, read_enable,
input [7:0] data_in,
input [5:0] full_thres,
output [7:0] data_out,
output empty, full
);
wire wr_en_mem, rd_en_mem;
reg [4:0] write_ptr, read_ptr;
reg [5:0] fifo_cnt;
reg [7:0] memory [0:31];
//CONTROL
assign wr_en_mem = write_enable & ~full;
assign rd_en_mem = read_enable & ~empty;
assign full = fifo_cnt == full_thres;
assign empty = fifo_cnt == 0;
always@(posedge clock) begin
//FIFO COUNTER
if(reset_n == 0)
fifo_cnt <= 0;
else begin
if((wr_en_mem == 0) & (rd_en_mem == 1))
fifo_cnt <= fifo_cnt - 1;
if((wr_en_mem == 1) & (rd_en_mem == 0))
fifo_cnt <= fifo_cnt + 1;
end
//WRITE_PTR
if(reset_n == 0)
write_ptr <= 0;
else
if(wr_en_mem == 1)
write_ptr <= write_ptr + 1;
else
write_ptr <= write_ptr;
//READ_PTR
if(reset_n == 0)
read_ptr <= 0;
else
if(rd_en_mem == 1)
read_ptr <= read_ptr + 1;
else
read_ptr <= read_ptr;
end
// RAM
always@(posedge clock) begin
if(wr_en_mem)
memory[write_ptr] <= data_in;
end
assign data_out = memory[read_ptr];
endmodule