gpt-4-0314· openai
Suzhen/CodeChat
Asyc fifo verilog code
- User
Asyc fifo verilog code
- Assistant
An Asynchronous FIFO (First In First Out) refers to a design where a FIFO's read and write pointers are in different clock domains. This kind of design is used when two modules or blocks of a digital system need to transfer data, and they have different clock rates. Here is a basic Verilog code for an Async FIFO:
module async_fifo ( input wire clk_wr, input wire clk_rd, input wire rst_wr, input wire rst_rd, input wire [7:0] data_in, output reg [7:0] data_out, input wire wr_en, input wire rd_en, output reg empty, output reg full ); parameter DEPTH = 16; // FIFO depth reg [7:0] mem [DEPTH-1:0]; // Memory array to hold FIFO data reg [4:0] wr_ptr = 0; // Write pointer reg [4:0] rd_ptr = 0; // Read pointer // Gray code conversion for read and write pointers function [4:0] gray_encode; input [4:0] binary; gray_encode = binary ^ (binary >> 1); endfunction always @(posedge clk_wr or posedge rst_wr) begin if (rst_wr) begin wr_ptr <= 0; full <= 0; end else if (wr_en) begin mem[wr_ptr] <= data_in; wr_ptr <= wr_ptr + 1; full <= (gray_encode(wr_ptr[4:1]) == gray_encode(rd_ptr[4:1])) & (wr_ptr[0] != rd_ptr[0]); end end always @(posedge clk_rd or posedge rst_rd) begin if (rst_rd) begin rd_ptr <= 0; empty <= 1; data_out <= 0; end else if (rd_en) begin data_out <= mem[rd_ptr]; rd_ptr <= rd_ptr + 1; empty <= (gray_encode(wr_ptr[4:1]) == gray_encode(rd_ptr[4:1])) & (wr_ptr[0] == rd_ptr[0]); end end endmodule