FPGA-片内ROM FIFO RAM连用

我做了个利用rom进行同步fifo的读写并把读出的数据输出到ram里并读出数据检验数据的正确性

直接贴代码吧,没什么难度:都是IP核的应用熟悉下流程

`timescale 1ns / 1ps
module rom_fifo_controller(clk,rst_n,fifo_full,fifo_empty,ram_rddb
    );
	input clk;
	input rst_n;
//--------------------------------------------------
	output fifo_full;
	output fifo_empty;
	output [7:0]ram_rddb;
//--------------------------------------------------
	wire [7:0]rom_out;
	wire [7:0]fifo_rddb;
//--------------------------------------------------
	reg [8:0]cnt;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			cnt<=1'b0;
		end
		else begin
			cnt<=cnt+1;
		end
	end
	
	rom uut_rom(
	.clka(clk), // input clka
	.addra(cnt), // input [3 : 0] addra
	.douta(rom_out) // output [7 : 0] douta
);
	reg wr_en;
	reg [7:0]wr_data;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			wr_en<=1'b0;
			wr_data<=1'b0;
		end
		else if(cnt>9'd0&&cnt<9'd11)begin
			wr_en<=1'b1;
			wr_data<=rom_out;
		end
		else begin
			wr_en<=1'b0;
			wr_data<=1'b0;
		end
	end
	
	reg rd_en;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			rd_en<=1'b0;
		end
		else if(cnt>9'd20&&cnt<9'd31)begin
			rd_en<=1'b1;
		end
		else begin
			rd_en<=1'b0;
		end
	end
	reg rd_rdrdy;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			rd_rdrdy<=1'b0;
		end
		else begin
			rd_rdrdy<=rd_en;
		end
	end
	reg en;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			en<=1'b0;
		end
		else begin
			en<=rd_rdrdy;
		end
	end
	fifo uut_fifo(
	.clk(clk), // input clk
	.rst(~rst_n), // input rst
	.din(wr_data), // input [7 : 0] din
	.wr_en(wr_en), // input wr_en
	.rd_en(rd_en), // input rd_en
	.dout(fifo_rddb), // output [7 : 0] dout
	.full(fifo_full), // output full
	.empty(fifo_empty) // output empty
);
	reg[3:0]ram_add;
	reg[7:0]ram_data;
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			ram_add<=1'b0;
		end
		else if(rd_rdrdy==1'b1)begin
			ram_add<=ram_add+1'b1;
		end
		else if(cnt>9'd40&&cnt<9'd57)begin
			ram_add<=ram_add+1'b1;
		end
		else begin
			ram_add<=1'b0;
		end
	end
	always@(posedge clk or negedge rst_n)begin
		if(rst_n==1'b0)begin
			ram_data=1'b0;
		end
		else if(rd_rdrdy==1'b1)begin
			ram_data=fifo_rddb;
		end
		else if(cnt>9'd40&&cnt<9'd57)begin
			ram_data=1'b0;
		end
		else begin
			ram_data=1'b0;
		end
	end
	ram uut_ram(
	.clka(clk), // input clka
	.wea(en), // input [0 : 0] wea
	.addra(ram_add), // input [3 : 0] addra
	.dina(ram_data), // input [7 : 0] dina
	.douta(ram_rddb) // output [7 : 0] douta
	);
endmodule

 

你可能感兴趣的:(FPGA专栏)