fifo设计要点
fifo是一种先进先出的存储结构,需要3个计数器一个作为读计数器,一个作为写计数器,另一个作为深度计数器,当fifo未满时可进行写操作,当fifo不为空时进行读操作
//an highlighted block
module fifo#(parameter size=8,depth=16)(
input clock, //时钟信号
input aclr, //异步清零信号
input [size-1:0]data, //输入数据
input rdreq, //读使能
input wreq, //写使能
output reg full, //满指示信号
output reg empty, //空指示信号
output reg[size-1:0]q //输出数据
);
reg [size-1:0]data_get[0:depth-1]; //数据寄存
reg [depth-1:0]cnt; //写入计数器
reg [depth-1:0]cnt1; //读出计数器
reg [depth-1:0]count; //深度计数器
reg [3:0]i;
always@(posedge clock or posedge aclr)
begin
if(aclr)begin
count<=0;
end
else begin
if((rdreq)&&(wreq))begin
count<=count;
end
else begin
if((wreq)&&(count<depth))begin
count<=count+1;
end
else if((rdreq)&&(count>0))begin
count<=count-1;
end
end
end
end
always@(posedge clock or posedge aclr)
begin
if(aclr==1'b1)begin
data_get[0]<={8{1'b0}}; //清零信号到来时数据寄存器清零
for(i=1;i<depth;i=i+1)
begin
data_get[i]<={8{1'b0}};
end
end
else begin
if((wreq==1'b1)&&(count<depth))begin
data_get[cnt]<=data; //读使能信号到来时如果fifo没有写满数据输入
if((rdreq==1'b1)&&(count>0))begin
q<=data_get[cnt1]; //写使能信号到来时如果fifo不为空数据读出
end
end
end
always@(posedge clock or posedge aclr)
begin
if(aclr==1'b1)begin
full<=1'b0;
empty<=1'b1;
end
else begin
if(count==depth-1)begin
full<=1'b1;
end
else if(count==0)begin
empty<=1'b1;
end
else begin
empty<=1'b0;
full<=1'b0;
end
end
end
always@(posedge clock or posedge aclr)
begin
if(aclr==1'b1)begin //清零信号到来时读写计数器清零
cnt<=0;
cnt1<=0;
end
else begin
if((count<depth)&&(wreq==1'b1))begin //写使能信号到来,fifo未满时写计数器加1
if(cnt<depth)
cnt<=cnt+1;
else
cnt<=0;
end
else if((count>0)&&(rdreq==1'b1))begin //读使能信号到来,fifo不为空读计数器加1
if(cnt1>0)
cnt1<=cnt1-1;
else
cnt1<=depth-1;
end
end
end
endmodule