From a2b0b2a7092ae1c3c582679173d21aa4a186ebfa Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Sat, 19 Nov 2022 23:41:15 -0700 Subject: [PATCH] add skidbuffer. Untested --- tests/common/skidbuffer.sv | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/common/skidbuffer.sv diff --git a/tests/common/skidbuffer.sv b/tests/common/skidbuffer.sv new file mode 100644 index 0000000..f1cf0ac --- /dev/null +++ b/tests/common/skidbuffer.sv @@ -0,0 +1,55 @@ +module skidbuffer #( + parameter WIDTH = 1 +)( + input logic clk, + input logic reset, + + input logic [WIDTH-1:0] in, + input logic in_valid, + output logic in_ready, + + output logic [WIDTH-1:0] out, + output logic out_valid, + input logic out_ready +); + +logic buffer_filled = 0; +logic [WIDTH-1:0] buffer_val; + +always_ff @(posedge clk) begin + if (reset) begin + buffer_filled <= 0; + end else begin + if (in_valid && in_ready) begin + // input always gets stored whether it needs to be or not + buffer_val <= in; + end + + if (buffer_filled) begin + if (out_ready && !(in_valid && in_ready)) begin + // out_valid = 1 since buffer is full + buffer_filled <= 0; + end + end else begin + if (in_valid && !(out_valid && out_ready)) begin + // in_ready = 1 since buffer is empty + buffer_val = in; + buffer_filled <= 1; + end + end + end +end + +always_comb begin + if (buffer_filled) begin + in_ready = out_ready; + out_valid = 1; + out = buffer_val; + end else begin + in_ready = 1; + out_valid = in_valid; + out = in; + end +end + +endmodule \ No newline at end of file