题解 | #数据累加输出#
数据累加输出
https://www.nowcoder.com/practice/956fa4fa03e4441d85262dc1ec46a3bd
感觉可以记录一下这题在实际运用场景所需要的valid/ready产生逻辑。
作为valid方:产生数据后置起valid,等到对方ready拉起后释放。
作为ready方:数据还在累加期间一直保持高电平,一次累加周期结束后,需要看下游方是否接收到数据。
`timescale 1ns/1ns module valid_ready( input clk , input rst_n , input [7:0] data_in , input valid_a , input ready_b , output ready_a , output reg valid_b , output reg [9:0] data_out ); reg[1:0] cnt; always @(posedge clk or negedge rst_n) begin if(~rst_n) begin cnt <= 2'd0; end else if(valid_a & ready_a) begin cnt <= (cnt >= 2'd3) ? 0 : (cnt + 1); end end always @(posedge clk or negedge rst_n) begin if(~rst_n) begin data_out <= 10'd0; end else if((cnt == 0) && ready_a && valid_a) begin data_out <= data_in; end else if(valid_a && ready_a) begin data_out <= data_in + data_out; end end always @(posedge clk or negedge rst_n) begin if(~rst_n) begin valid_b <= 1'b0; end else if((cnt == 2'd3) && ready_a && valid_a) begin valid_b <= 1'b1; end else if(ready_b) begin valid_b <= 1'b0; end end assign ready_a = (cnt == 0) ? ready_b : 1'b1; endmodule