关于fifo的设计

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值