fifo的简单读写,使用begin_case

本文深入探讨了一个FIFO模块的设计案例,详细介绍了其参数配置、状态转换逻辑及读写操作实现。通过对不同读出写法的仿真讨论,展示了FIFO在实际应用中的行为表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//////////////////////////////////////////////////////////////////////////////////

module fifo_case #(

parameter DATA_WIDTH=4

) (

input clk,

input wr_en,

input rd_en,

input rst,

input [DATA_WIDTH-1:0]wr,

output reg [DATA_WIDTH-1:0]rd,

output reg fifo_empty,

output reg fifo_full

);

parameter ADDER_WIDTH=4;

parameter MAX_COUNT=15;

reg [ADDER_WIDTH-1:0] count;

always @(posedge clk or posedge rst)begin

if(rst) count<=0;

else begin

case({wr_en,rd_en})

2'b00:count<=count;

2'b01:

if(count!==0) count<=count-1;

else count<=0;

2'b10:

if(count!==MAX_COUNT) count<=count+1;

else count<=MAX_COUNT;

2'b11:count<=count;

endcase

end

end

 

//1.自己添加的读出写法

always @(posedge clk or posedge rst)begin

if(rst) begin

rd <= 4'd0;

fifo[wr_adder] <= 4'd0;

end

else begin

case({wr_en,rd_en})

2'b00:begin

rd <= 4'd0;

fifo[wr_adder] <= 4'd0;

end

2'b01:begin

rd <= fifo[rd_adder];

//wr <= wr;

end

2'b10:begin

rd <= rd;

fifo[wr_adder] <= wr;

end

2'b11:begin

rd <= rd;

//wr <= wr;

end

endcase

end

end

always @(posedge clk) begin

if(count==0)fifo_empty <= 1;

else fifo_empty <= 0;

end

always @(posedge clk)begin

if(count==MAX_COUNT) fifo_full <= 1;

else fifo_full <= 0;

end

reg [ADDER_WIDTH-1:0] wr_adder;

reg [ADDER_WIDTH-1:0] rd_adder;

 

always @(posedge clk)begin

if(rst)wr_adder<=0;

else if(fifo_full==0&&wr_en==1) wr_adder<=wr_adder+1;

else if(fifo_full==0&&wr_en==0) wr_adder<=wr_adder;

else wr_adder<=0;

end

 

always @(posedge clk)begin

if(rst) rd_adder<=0;

else if(fifo_empty==0&&rd_en==1) rd_adder<=rd_adder+1;

else if(fifo_empty==0&&rd_en==0) rd_adder<=rd_adder;

else rd_adder<=0;

end

parameter WIDTH=16;

reg [WIDTH-1:0]fifo[0:WIDTH-1];

 

//下面是对rd读出的不同写法进行了仿真讨论

//2.会有一些不确定输出的问题

//always @(posedge clk )begin

// if(rd_en) rd<=fifo[rd_adder];

// else rd<= rd;

//end

//

//always @(posedge clk )begin

// if(wr_en) fifo[wr_adder]<=wr;

// else fifo[wr_adder] <= fifo[wr_adder];

//end

 

//3.类似上面begin_case写法的应用

//always @(*)begin

// if(rd_en) rd = fifo[rd_adder];

// else rd<=0;

//end

//

//always @(*)begin

// if(wr_en) fifo[wr_adder] = wr;

//end

 

endmodule

module rd_fifo_to_we_ddr_ctrl( input I_pl_clk , input I_SSC_CLK , input I_fifo_start , ///we ddr input [31:0] I_ddr_start_addr , input [31:0] I_ddr_add_addr , output [31:0] O_wr_ddr_addr , output O_ddr_wr_start , output [31:0] O_ddr_wr_data , input I_ddr_wr_en , input I_ddr_wr_done , output O_wr_bit_done , ////smartnt3 fifo2 output O_fifo2_rd , input [15:0] I_fifo2_rdata , input I_smartnt_req , input I_smartnt_empty , output O_smartnt_ack ); reg S_fifo_start_buf1 = 1'b0 ; reg S_fifo_start_buf2 = 1'b0 ; reg S_fifo_start_buf3 = 1'b0 ; reg [2:0] S_ctrl_cnt = 3'h7 ; // fifo ctrl reg S_fifo_rst = 1'b1 ; reg S_fifo_rst_buf = 1'b0 ; reg [3:0] S_fifo_rst_cnt = 4'd0 ; reg [7:0] S_fifo_reset_delay = 8'd0 ; ////fifo we wire [15:0] S_fifo_wr_data_count ; wire [15:0] S_fifo_we_data ; wire S_fifo_we_en ; reg S_fifo_rd_ready = 1'b0 ; wire [31:0] S_fifo_out ; wire S_fifo_rd ; reg S_ddr_wr_done_buf = 1'b0 ; always@(posedge I_SSC_CLK) begin S_fifo_start_buf1 <= I_fifo_start ; S_fifo_start_buf2 <= S_fifo_start_buf1; S_fifo_start_buf3 <= S_fifo_start_buf2; S_ddr_wr_done_buf <= I_ddr_wr_done; end always@(posedge I_SSC_CLK) begin if(!S_fifo_start_buf3) begin S_ctrl_cnt <= 3'd0; end else if(&S_ctrl_cnt) begin S_ctrl_cnt <= S_ctrl_cnt; end else if (S_ctrl_cnt == 3'd1) begin if(S_fifo_rst == 1'b0) begin S_ctrl_cnt <= S_ctrl_cnt + 1'b1 ; end else begin S_ctrl_cnt <= S_ctrl_cnt ; end end else if(S_ctrl_cnt == 3'd2) begin if(S_fifo_reset_delay >= 8'd100) begin S_ctrl_cnt <= S_ctrl_cnt + 1'b1 ; end else begin S_ctrl_cnt <= S_ctrl_cnt ; end end else if(S_ctrl_cnt == 3'd3) begin if(I_ddr_wr_done && !S_ddr_wr_done_buf) begin S_ctrl_cnt <= S_ctrl_cnt + 1'b1 ; end else begin S_ctrl_cnt <= S_ctrl_cnt ; end end else begin S_ctrl_cnt <= S_ctrl_cnt + 1'b1 ; end end //////////////fifo ctrl/////////////////////////////////////////////////////// always@(posedge I_SSC_CLK) begin S_fifo_rst_buf <= S_fifo_rst ; end always@(posedge I_SSC_CLK) begin if(S_fifo_rst && !S_fifo_rst_buf) begin S_fifo_rst_cnt <= 4'b0 ; end else if(&S_fifo_rst_cnt) begin S_fifo_rst_cnt <= S_fifo_rst_cnt ; end else begin S_fifo_rst_cnt <= S_fifo_rst_cnt + 1'b1 ; end end always@(posedge I_SSC_CLK) begin if(S_fifo_start_buf2 && !S_fifo_start_buf3) begin S_fifo_rst <= 1'b1 ; end else if(S_fifo_rst_cnt == 4'd10) begin S_fifo_rst <= 1'b0 ; end else begin S_fifo_rst <= S_fifo_rst ; end end always@(posedge I_SSC_CLK) begin if(S_ctrl_cnt == 4'd2) begin S_fifo_reset_delay <= S_fifo_reset_delay + 1'b1 ; end else begin S_fifo_reset_delay <= 4'd0; end end fifo_rd_interface fifo_rd_interface( .I_SSC_CLK (I_SSC_CLK ), // .O_fifo2_rd (O_fifo2_rd ), // .I_fifo2_rdata (I_fifo2_rdata ), //[15:0] .I_smartnt_req (I_smartnt_req ), // .I_smartnt_empty (I_smartnt_empty ), // .O_smartnt_ack (O_smartnt_ack ), // .I_fifo_we_count (S_fifo_wr_data_count), //[15:0] .O_fifo_we_en (S_fifo_we_en ), // .O_fifo_we_data (S_fifo_we_data ) //[15:0] ); fifo_16to32_65535 fifo_16to32_65535 ( .rst (S_fifo_rst ), .wr_clk (I_SSC_CLK ), .rd_clk (I_pl_clk ), .din (S_fifo_we_data ), .wr_en (S_fifo_we_en ), .rd_en (S_fifo_rd ), .dout (S_fifo_out ), .full ( ), //S_fifo_full .empty ( ), //S_fifo_empty .wr_data_count (S_fifo_wr_data_count ) ); ///////璇籪ifo鍚庡皢鏁版嵁鍐欏叆DDR///////////////////////////////////////////////////// always@(posedge I_SSC_CLK) begin if(S_fifo_start_buf2 && !S_fifo_start_buf3) begin S_fifo_rd_ready <= 1'B0; end else if(S_ctrl_cnt == 3'd3) begin if(S_fifo_wr_data_count == 16'd8192) begin S_fifo_rd_ready <= 1'b1 ; end else if(I_ddr_wr_done && !S_ddr_wr_done_buf) begin S_fifo_rd_ready <= 1'b0 ; end else begin S_fifo_rd_ready <= S_fifo_rd_ready; end end else if(S_ctrl_cnt >= 3'd4) begin if(O_wr_bit_done) if(S_fifo_wr_data_count >= 16'd8192) begin S_fifo_rd_ready <= 1'b1 ; end else begin S_fifo_rd_ready <= S_fifo_rd_ready; end else begin S_fifo_rd_ready <= 1'b0; end end else begin S_fifo_rd_ready <= S_fifo_rd_ready; end end ps_ddr_wr ps_ddr_wr( .I_clk (I_pl_clk ), .I_readbit_start (I_fifo_start ), .I_fifo_rd_ready (S_fifo_rd_ready ), .I_fifo_out (S_fifo_out ), .O_fifo_rd (S_fifo_rd ), .I_ddr_start_addr (I_ddr_start_addr ), .I_ddr_add_addr (I_ddr_add_addr ), .O_wr_ddr_addr (O_wr_ddr_addr ), .O_ddr_wr_start (O_ddr_wr_start ), .O_ddr_wr_data (O_ddr_wr_data ), .I_ddr_wr_en (I_ddr_wr_en ), .I_ddr_wr_done (I_ddr_wr_done ), .O_wr_bit_done (O_wr_bit_done ), .O_we_cnt () ); (* MARK_DEBUG="true" *)reg [2:0] dbg_S_ctrl_cnt ; (* MARK_DEBUG="true" *)reg dbg_S_rd_start_buf3 ; (* MARK_DEBUG="true" *)reg [15:0] dbg_S_fifo_wr_data_count ; (* MARK_DEBUG="true" *)reg [15:0] dbg_S_fifo_we_data ; (* MARK_DEBUG="true" *)reg dbg_S_fifo_we_en ; (* MARK_DEBUG="true" *)reg dbg_S_fifo_rd_ready ; (* MARK_DEBUG="true" *)reg dbg_S_fifo_rd ; (* MARK_DEBUG="true" *)reg [31:0] dbg_S_fifo_out ; (* MARK_DEBUG="true" *)reg [15:0] dbg_S_fifo_wr_data_count1 ; (* MARK_DEBUG="true" *)reg dbg_S_fifo_rd_ready1 ; always@(posedge I_SSC_CLK) begin dbg_S_ctrl_cnt <= S_ctrl_cnt ; dbg_S_rd_start_buf3 <= S_fifo_start_buf3 ; dbg_S_fifo_wr_data_count <= S_fifo_wr_data_count ; dbg_S_fifo_we_data <= S_fifo_we_data ; dbg_S_fifo_we_en <= S_fifo_we_en ; dbg_S_fifo_rd_ready <= S_fifo_rd_ready ; end always@(posedge I_pl_clk) begin dbg_S_fifo_rd <= S_fifo_rd ; dbg_S_fifo_out <= S_fifo_out ; dbg_S_fifo_wr_data_count1 <= S_fifo_wr_data_count ; dbg_S_fifo_rd_ready1 <= S_fifo_rd_ready ; end endmodule给我写一个1553b的rt模式的verilog程序,并且将数据存在上述的smart ncfifo中,并与这个代码进行交互
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值