用文件初始化FIFO

`timescale 1ps/1ps

module FILEFIFO(
	iCLK,
	iRST_N,
	iREAD,
	oDATA,
	iWRITE,
	iDATA
);

parameter DATA_WIDTH=8;		//数据位宽
parameter DATA_LENGTH=256;	//FIFO深度
parameter FILE_NAME="file.txt";

input iCLK;
input iRST_N;
input iREAD;
output [DATA_WIDTH-1:0] oDATA;
input iWRITE;
input [DATA_WIDTH-1:0] iDATA;

reg [DATA_WIDTH-1:0] oDATA;
reg [DATA_WIDTH-1:0] rMem[DATA_LENGTH-1:0];

integer fp;
integer i;
integer n;
initial begin
	fp=$fopen(FILE_NAME,"r");
	i=0;
	while(!($feof(fp)) && i<DATA_LENGTH) begin
		n=$fscanf(fp,"%x",rMem[i]);
		i=i+1;
	end
	$fclose(fp);
end

integer readAddr;
integer writeAddr;
initial begin
	readAddr=0;
	writeAddr=0;
end

always@(posedge iCLK or negedge iRST_N)begin
	if(!iRST_N) oDATA<=0;
	else if(iWRITE) begin
		rMem[writeAddr]<=iDATA;
		if(writeAddr==DATA_LENGTH-1) writeAddr<=0;
		else writeAddr<=writeAddr+1;
	end
	else if(iREAD) begin
		oDATA<=rMem[readAddr];
		if(readAddr==DATA_LENGTH-1) readAddr<=0;
		else readAddr<=readAddr+1;
	end
end

endmodule


你可能感兴趣的:(用文件初始化FIFO)