一、实验一:在同步fifo里写一个读一个(写入是8个位宽,读出是16个位宽)
程序:
`timescale 1ns / 1ps
//要求写一个读一个
//读写时钟一致,写是8个位宽,读是16个位宽
module sync_fifo_test(input sys_clk ,input rst_n );wire [15 : 0] dout ; wire full ;wire wr_ack ;wire empty ;wire valid ;wire [4 : 0] rd_data_count ;wire [5 : 0] wr_data_count ;reg [7 : 0] din ;reg wr_en ;reg rd_en ;always@(posedge sys_clk )if(!rst_n)din <= 0 ;elsedin <= din +1 ;always@(posedge sys_clk )if(!rst_n)wr_en <= 0 ;else if (full)wr_en <= 0 ;elsewr_en <= 1 ;//----------- Begin Cut here for INSTANTIATION Template ---// INST_TAG
sync_fifo_generator your_instance_name (.clk(sys_clk), // input wire clk.srst(~rst_n), // input wire srst.din(din), // input wire [7 : 0] din.wr_en(wr_en), // input wire wr_en.rd_en(wr_ack), // input wire rd_en.dout(dout), // output wire [15 : 0] dout.full(full), // output wire full.wr_ack(wr_ack), // output wire wr_ack.empty(empty), // output wire empty.valid(valid), // output wire valid.rd_data_count(rd_data_count), // output wire [4 : 0] rd_data_count.wr_data_count(wr_data_count) // output wire [5 : 0] wr_data_count
);
// INST_TAG_END ------ End INSTANTIATION Template ---------endmodule
仿真程序:
`timescale 1ns / 1ps
module test_sync_fifo( );reg sys_clk ;reg rst_n ;initialbeginsys_clk = 0 ;rst_n = 0 ;#10rst_n = 1 ;endalways #1 sys_clk = ~sys_clk ; sync_fifo_test sync_fifo_test_1(. sys_clk ( sys_clk) ,. rst_n ( rst_n ) );endmodule
实验结果:
TIPS:
二、实验二:在实验一的基础上完成“写完再读”
程序:
`timescale 1ns / 1ps
module sync_fifo_2(input sys_clk ,input rst_n );reg [7 : 0] din ;reg wr_en ;reg rd_en ;wire [15 : 0] dout ;wire full ;wire wr_ack ;wire empty ;wire valid ;wire [4 : 0] rd_data_count ;wire [5 : 0] wr_data_count ;always@(posedge sys_clk )if(!rst_n)wr_en <= 0 ;else if (full)wr_en <= 0 ;else if (rd_en)wr_en <= 0 ;elsewr_en <= 1 ;always@(posedge sys_clk )if(!rst_n)din <= 0 ;elsedin <= din +1 ;always@(posedge sys_clk )if(!rst_n)rd_en <= 0 ;else if (full) //写满了 rd_en <= 1 ;else if (empty) // 读空了rd_en <= 0 ;elserd_en <= rd_en ;//----------- Begin Cut here for INSTANTIATION Template ---// INST_TAG
sync_fifo_generator your_instance_name (.clk(sys_clk ), // input wire clk.srst(~rst_n), // input wire srst.din(din), // input wire [7 : 0] din.wr_en(wr_en), // input wire wr_en.rd_en(rd_en), // input wire rd_en.dout(dout), // output wire [15 : 0] dout.full(full), // output wire full.wr_ack(wr_ack), // output wire wr_ack.empty(empty), // output wire empty.valid(valid), // output wire valid.rd_data_count(rd_data_count), // output wire [4 : 0] rd_data_count.wr_data_count(wr_data_count) // output wire [5 : 0] wr_data_count
);
// INST_TAG_END ------ End INSTANTIATION Template ---------endmodule
仿真程序:
`timescale 1ns / 1ps
module test_sync_fifo_2( );reg sys_clk ;reg rst_n ;initialbeginsys_clk = 0 ;rst_n = 0 ;#10rst_n = 1 ;endalways #1 sys_clk = ~sys_clk ;sync_fifo_2 sync_fifo_2_1(. sys_clk (sys_clk) ,. rst_n (rst_n ) ); endmodule
仿真结果: