From fd0c169c1a7a080389c593fd50a816303f795d38 Mon Sep 17 00:00:00 2001 From: uvok Date: Mon, 29 Dec 2025 12:51:39 +0100 Subject: fifo: Count bytes --- fifo.v | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'fifo.v') diff --git a/fifo.v b/fifo.v index d900155..ebc1eee 100644 --- a/fifo.v +++ b/fifo.v @@ -5,8 +5,8 @@ module fifo #( input rst_i, input clk_i, - input data_write_i, - input data_read_i, + input write_i, + input read_i, output empty_o, output full_o, @@ -18,10 +18,26 @@ module fifo #( ); +reg [$clog2(DATA_DEPTH):0] r_count; + +initial begin + r_count = 0; +end + always @(posedge clk_i or negedge rst_i) begin if (!rst_i) begin - + r_count <= 0; + end else if (write_i && read_i) begin + // nothing to do + end else if (write_i && !full_o) begin + r_count <= r_count + 1; + end else if (read_i && !empty_o) begin + r_count <= r_count - 1; end end +assign empty_o = r_count == 0; +assign full_o = r_count >= DATA_DEPTH - 1; + + endmodule -- cgit v1.2.3