⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 afifo_beh.v

📁 异步FIFO 已上板试过 并附测试文件
💻 V
字号:
/*********************************************************/
// MODULE:		Synchronizing FIFO
//
// FILE NAME:	afifo_beh.v
// VERSION:		1.0
// DATE:		January 1, 1999
// AUTHOR:		Bob Zeidman, Zeidman Consulting
// 
// CODE TYPE:	Behavioral Level
//
// DESCRIPTION:	This module defines a Synchronizing FIFO.
// The FIFO memory is implemented as a ring buffer. The read
// pointer points to the beginning of the buffer, while the
// write pointer points to the end of the buffer.
//
/*********************************************************/

// DEFINES
`define DEL	1			// Clock-to-output delay. Zero
						// time delays can be confusing
						// and sometimes cause problems.

`define FIFO_DEPTH 15	// Depth of FIFO (number of bytes)
`define FIFO_HALF 8		// Half depth of FIFO
						// (this avoids rounding errors)
`define FIFO_BITS 4		// Number of bits required to
						// represent the FIFO size
`define FIFO_WIDTH 8	// Width of FIFO data

// TOP MODULE
module Afifo(
		reset_n,
		data_in,
		read_n,
		write_n,
		data_out,
		full,
		empty,
		half);

// INPUTS
input				  	reset_n;	// Active low reset
input [`FIFO_WIDTH-1:0]	data_in; 	// Data input to FIFO
input					read_n;	 	// Read FIFO (active low)
input					write_n;  	// Write FIFO (active low)

// OUTPUTS
output [`FIFO_WIDTH-1:0]	data_out;	// FIFO data output
output						full;		// FIFO is full
output						empty;		// FIFO is empty
output						half;		// FIFO is half full
										// or more

// INOUTS

// SIGNAL DECLARATIONS
wire					reset_n;
wire [`FIFO_WIDTH-1:0]	data_in;
wire					read_n;
wire					write_n;
reg  [`FIFO_WIDTH-1:0] 	data_out;
wire					full;
wire					empty;
wire					half;

							// The FIFO memory.
reg	 [`FIFO_WIDTH-1:0]	fifo_mem[0:`FIFO_DEPTH-1];
							// How many locations in the FIFO
							// are occupied?
reg	 [`FIFO_BITS-1:0]	counter;
							// FIFO read pointer points to
							// the location in the FIFO to
							// read from next
reg	 [`FIFO_BITS-1:0]	rd_pointer;
							// FIFO write pointer points to
							// the location in the FIFO to
							// write to next
reg	 [`FIFO_BITS-1:0]	wr_pointer;

// PARAMETERS

// ASSIGN STATEMENTS
assign #`DEL full = (counter == `FIFO_DEPTH) ? 1'b1 : 1'b0;
assign #`DEL empty = (counter == 0) ? 1'b1 : 1'b0;
assign #`DEL half = (counter >= `FIFO_HALF) ? 1'b1 : 1'b0;

// MAIN CODE

// Look at the edges of reset_n
always @(reset_n) begin
	if (~reset_n) begin
		// Reset the FIFO pointer
		#`DEL;
		assign rd_pointer = `FIFO_BITS'b0;
		assign wr_pointer = `FIFO_BITS'b0;
		assign counter = `FIFO_BITS'b0;
	end
	else begin
		#`DEL;
		deassign rd_pointer;
		deassign wr_pointer;
		deassign counter;
	end
end

// Look at the falling edge of the read signal
always @(negedge read_n) begin
	// Check for FIFO underflow
	if (counter == 0) begin
		$display("\nERROR at time %0t:", $time);
		$display("FIFO Underflow\n");
				
		// Use $stop for debugging
		$stop;
	end

	// Decrement the FIFO counter
	counter = counter - 1;

	// Output the data
	data_out <= #`DEL fifo_mem[rd_pointer];

	// Increment the read pointer
	// Check if the read pointer has gone beyond the
	// depth of the FIFO. If so, set it back to the
	// beginning of the FIFO
	if (rd_pointer == `FIFO_DEPTH-1)
		rd_pointer <= #`DEL `FIFO_BITS'b0;
	else
		rd_pointer <= #`DEL rd_pointer + 1;
end

// Look at the falling edge of the write signal
always @(negedge write_n) begin
	// Check for FIFO overflow
	if (counter >= `FIFO_DEPTH) begin
		$display("\nERROR at time %0t:", $time);
		$display("FIFO Overflow\n");
				
		// Use $stop for debugging
		$stop;
	end

	// Increment the FIFO counter
	counter = counter + 1;

	// Store the data
	fifo_mem[wr_pointer] <= #`DEL data_in;

	// Increment the write pointer
	// Check if the write pointer has gone beyond the
	// depth of the FIFO. If so, set it back to the
	// beginning of the FIFO
	if (wr_pointer == `FIFO_DEPTH-1)
		wr_pointer <= #`DEL `FIFO_BITS'b0;
	else
		wr_pointer <= #`DEL wr_pointer + 1;
end
endmodule		// Afifo

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -