What is a FIFO? What are overflow and underflow? Write the Verilog
What is a FIFO? What are overflow and underflow? Write the Verilog
Model answer
FIFO = First-In-First-Out buffer: the first word written is the first word read. - **Overflow:** writing when the FIFO is **full**. Data is lost. Prevented by the full flag. - **Underflow:** reading when the FIFO is **empty**. Returns invalid data. Prevented by the empty flag. Synchronous FIFO using an extra pointer bit to tell *full* from *empty*: verilog module sync_fifo #(parameter DATA_W = 8, parameter DEPTH = 8) ( input clk, rst_n, input wr_en, rd_en, input [DATA_W-1:0] din, output reg [DATA_W-1:0] dout, output full, empty ); localparam AW = $clog2(DEPTH); verilog reg [DATA_W-1:0] mem [0:DEPTH-1]; reg [AW:0] wptr, rptr; verilog assign empty = (wptr == rptr); assign full = (wptr[AW-1:0] == rptr[AW-1:0]) && (wptr[AW] != rptr[AW]); verilog always @(posedge clk or negedge rst_n) if (!rst_n) wptr <= 0; else if (wr_en && !full) wptr <= wptr + 1'b1; verilog always @(posedge clk or negedge rst_n) if (!rst_n) rptr <= 0; else if (rd_en && !empty) rptr <= rptr + 1'b1; verilog always @(posedge clk) begin if (wr_en && !full) mem[wptr[AW-1:0]] <= din; if (rd_en && !empty) dout <= mem[rptr[AW-1:0]]; end endmodule Block diagram (TikZ):