用文件初始化FIFO2

`timescale 1ps/1ps

module FILEFIFO2(
	iCLK,
	iRST_N,
	iREAD,
	oDATA,
	iWRITE,
	iDATA,
	oCOUNT
);

parameter DATA_WIDTH=8;		//数据位宽
parameter DATA_LENGTH=256;	//FIFO深度
parameter COUNT_LENGTH=8;
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;
output [COUNT_LENGTH-1:0] oCOUNT;

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

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
	oCOUNT=i;
	$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;
		oCOUNT<=oCOUNT+1;
		if(writeAddr==DATA_LENGTH-1) writeAddr<=0;
		else writeAddr<=writeAddr+1;
	end
	else if(iREAD) begin
		oDATA<=rMem[readAddr];
		oCOUNT<=oCOUNT-1;
		if(readAddr==DATA_LENGTH-1) readAddr<=0;
		else readAddr<=readAddr+1;
	end
end

endmodule

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