Skip to content

Instantly share code, notes, and snippets.

@racerxdl
Created August 12, 2021 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save racerxdl/4c4cdd68a79f099ec241b180d95faa69 to your computer and use it in GitHub Desktop.
Save racerxdl/4c4cdd68a79f099ec241b180d95faa69 to your computer and use it in GitHub Desktop.
module FIFO
#(
parameter NUMSAMPLES = 16,
parameter NUMBITS = 16
) (
input wire rclk,
input wire wclk,
input wire reset,
input wire [NUMBITS-1:0] wdata,
input wire readEnable,
input wire writeEnable,
output [NUMBITS-1:0] rdata,
output wire isEmpty,
output wire isFull
);
localparam ABITS = $clog2(NUMSAMPLES); // Minimum required address bits
reg [NUMBITS-1:0] rdata;
reg [NUMBITS-1:0] fifo [0:NUMSAMPLES];
wire [ABITS:0] writePtr;
wire [ABITS:0] readPtr;
GrayCounter #(
.BITS(ABITS+1)
) writeCounter (
wclk,
reset,
writeEnable,
writePtr
);
wire fullOrEmpty = (writePtr[ABITS-1:0] == readPtr[ABITS-1:0]);
wire empty = (writePtr == readPtr);
wire [ABITS-1:0] wAddr = writePtr[ABITS-1:0];
wire [ABITS-1:0] rAddr = readPtr[ABITS-1:0];
GrayCounter #(
.BITS(ABITS+1)
) readCounter (
.clk(rclk),
.reset(reset),
.enable(readEnable && !empty),
.out(readPtr)
);
assign isEmpty = empty;
assign isFull = fullOrEmpty && !empty;
always @(posedge rclk)
begin
if (readEnable)
rdata <= reset ? 0 : fifo[rAddr];
else
rdata <= 0;
end
always @(posedge wclk)
begin
if (!reset && writeEnable) fifo[wAddr] <= wdata;
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment