tastynoob
Articles58
Tags18
Categories7
FPGA-可配置的FIFO模块

FPGA-可配置的FIFO模块

FPGA-可配置的FIFO模块

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
module FIFO #(
parameter unitwid = 1,
parameter unitdpth = 2
)(
input wire i_clk,
input wire i_rstn,

input wire i_wen,
input wire[unitwid-1:0] i_unitdata,
output wire o_full,

input wire i_ren,
output reg[unitwid-1:0] o_unitdata,
output wire o_empty
);


function integer clogb2 (input integer bit_depth);
begin
for(clogb2=0; bit_depth>0; clogb2=clogb2+1)
bit_depth = bit_depth>>1;
end
endfunction


reg[unitwid-1:0] fifo_units[unitdpth-1:0];
reg[clogb2(unitdpth)-1:0] hptr,eptr,count;

assign o_full = (count == unitdpth);
assign o_empty = ~(|count);

wire[clogb2(unitdpth)-1:0] hptr_1 = ((hptr == unitdpth) ? 0 : (hptr + 1));
wire[clogb2(unitdpth)-1:0] eptr_1 = ((eptr == unitdpth) ? 0 : (eptr + 1));


initial begin
hptr<=0;
eptr<=0;
count<=0;
end


always @(posedge i_clk) begin
if(~i_rstn)begin
hptr<=0;
eptr<=0;
count<=0;
end
else begin
if(i_wen && i_ren)begin//如果同时发生读写
hptr<=hptr_1;
eptr<=eptr_1;
end
else if(i_wen & (~o_full))begin//只有写

fifo_units[hptr] <= i_unitdata;
count<=count+1;
hptr<=hptr_1;

end
else if(i_ren & (~o_empty))begin//只有读
count<=count-1;
eptr<=eptr_1;
o_unitdata <=fifo_units[eptr];
end
end
end



endmodule
Author:tastynoob
Link:https://tastynoob.github.io/1970/01/01/FPGA_/FPGA-FIFO/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×