重写异步fifo(.v文件加仿真)

之前写的一篇文章作为参考 

http://t.csdn.cn/k0sfX

然后再写一个位宽为8位,深度位256位的fifo。

模块整体的代码如下 

.v文件如下 

module asyn_fifo
	#(
		parameter data_width = 16,
		parameter data_depth = 8,
		parameter ram_depth = 256
	)
	(
		input							rst_n,
		
		input							wr_clk,
		input							wr_en,
		input		[data_width-1:0]	data_in,
		output							full,
		
		input							rd_clk,
		input							rd_en,
		output	reg	[data_width-1:0]	data_out,
		output							empty
	);
	
	wire	[data_depth-1:0]	wr_adr;
	wire	[data_depth-1:0]	rd_adr;
	
	reg		[data_depth:0]		wr_adr_ptr;
	reg		[data_depth:0]		rd_adr_ptr;
	
	wire 	[data_depth:0]		wr_adr_gray;
	reg		[data_depth:0]		wr_adr_gray1;
	reg		[data_depth:0]		wr_adr_gray2;
	
	wire 	[data_depth:0]		rd_adr_gray;
	reg		[data_depth:0]		rd_adr_gray1;
	reg		[data_depth:0]		rd_adr_gray2;
	
	//dual port ram - write and read
	assign wr_adr = wr_adr_ptr[data_depth-1:0];
	assign rd_adr = rd_adr_ptr[data_depth-1:0];
	
	integer i;
	reg [data_width-1:0] ram_fifo [ram_depth-1:0];
	
	always @(posedge wr_clk or negedge rst_n)
	begin
		if(!rst_n)
		begin
			for(i=0;i>1) ^ wr_adr_ptr;
	assign rd_adr_gray = (rd_adr_ptr>>1) ^ rd_adr_ptr;
	
	//gray cdc cpmpare
	always @(posedge wr_clk or negedge rst_n)
	begin
		if(!rst_n)
		begin
			rd_adr_gray1 <= 'd0;
			rd_adr_gray2 <= 'd0;
		end
		else 
		begin
			rd_adr_gray1 <= rd_adr_gray;
			rd_adr_gray2 <= rd_adr_gray1;
		end
	end
	
	always @(posedge rd_clk or negedge rst_n)
	begin
		if(!rst_n)
		begin
			wr_adr_gray1 <= 'd0;
			wr_adr_gray2 <= 'd0;
		end
		else 
		begin
			wr_adr_gray1 <= wr_adr_gray;
			wr_adr_gray2 <= wr_adr_gray1;
		end
	end
	
	assign empty = (rd_adr_gray == wr_adr_gray2)?1'b1:1'b0;
	assign full = (wr_adr_gray[data_depth:data_depth-1] != rd_adr_gray[data_depth:data_depth-1])&&(wr_adr_gray[data_depth-2:0] == rd_adr_gray[data_depth-2:0]);
	
endmodule	
	
	
	
	
	

tb仿真文件如下

`timescale 1ns / 1ps

module asyn_fifo_tb;
    
    reg 						      rst_n;
								
	reg 						      wr_clk;
	reg 						      wr_en;
	reg 	      [15:0]	          data_in;
	wire						      full;
				
	reg 						      rd_clk;
	reg 						      rd_en;
	wire	      [15:0]	          data_out;
	wire	 					      empty;

    asyn_fifo asyn_fifo_inst
	(
		  .rst_n      (rst_n),
								
		  .wr_clk     (wr_clk),
		  .wr_en      (wr_en),
		  .data_in    (data_in),
		  .full       (full),
				
		  .rd_clk     (rd_clk),
		  .rd_en      (rd_en),
		  .data_out   (data_out),
		  .empty      (empty)
);
    
    initial wr_clk = 0;
    always#10 wr_clk = ~wr_clk;
    
    initial rd_clk = 0;
    always#30 rd_clk = ~rd_clk;
    
    always@(posedge wr_clk or negedge rst_n)begin
        if(!rst_n)
            data_in <= 'd0;
        else if(wr_en)
            data_in <= data_in + 1'b1;
        else
            data_in <= data_in;
    end
    
    initial begin
        rst_n = 0;
        wr_en = 0;
        rd_en = 0;
        #200;
        rst_n = 1;
        wr_en = 1;
        #20000;
        wr_en = 0;
        rd_en = 1;
        #20000;
        rd_en = 0;
        $stop; 
    end
    
endmodule

以下是vivado运行的波形图

写满信号拉高

重写异步fifo(.v文件加仿真)_第1张图片 

读空信号拉高

重写异步fifo(.v文件加仿真)_第2张图片

下面是整体波形图,运行时间为1ms

tb仿真文件中,写信号时钟是读信号时钟的三倍,

 重写异步fifo(.v文件加仿真)_第3张图片

 

 

 

 

你可能感兴趣的:(fpga开发)