verilog-ethernet/rtl/axis_fifo.v

156 lines
4.3 KiB
Coq
Raw Normal View History

2014-09-13 21:21:39 -07:00
/*
2016-01-05 00:24:20 -08:00
Copyright (c) 2013-2016 Alex Forencich
2014-09-13 21:21:39 -07:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Language: Verilog 2001
`timescale 1ns / 1ps
/*
* AXI4-Stream FIFO
*/
module axis_fifo #
(
parameter ADDR_WIDTH = 12,
parameter DATA_WIDTH = 8
)
(
input wire clk,
input wire rst,
/*
* AXI input
*/
input wire [DATA_WIDTH-1:0] input_axis_tdata,
input wire input_axis_tvalid,
output wire input_axis_tready,
input wire input_axis_tlast,
input wire input_axis_tuser,
/*
* AXI output
*/
output wire [DATA_WIDTH-1:0] output_axis_tdata,
output wire output_axis_tvalid,
input wire output_axis_tready,
output wire output_axis_tlast,
output wire output_axis_tuser
);
2015-11-07 01:15:11 -08:00
reg [ADDR_WIDTH:0] wr_ptr_reg = {ADDR_WIDTH+1{1'b0}}, wr_ptr_next;
reg [ADDR_WIDTH:0] wr_addr_reg = {ADDR_WIDTH+1{1'b0}};
2015-11-07 01:15:11 -08:00
reg [ADDR_WIDTH:0] rd_ptr_reg = {ADDR_WIDTH+1{1'b0}}, rd_ptr_next;
reg [ADDR_WIDTH:0] rd_addr_reg = {ADDR_WIDTH+1{1'b0}};
2014-09-13 21:21:39 -07:00
reg [DATA_WIDTH+2-1:0] mem[(2**ADDR_WIDTH)-1:0];
2016-06-27 12:10:36 -07:00
reg [DATA_WIDTH+2-1:0] mem_read_data_reg = {DATA_WIDTH+2{1'b0}};
wire [DATA_WIDTH+2-1:0] mem_write_data;
2014-09-13 21:21:39 -07:00
2015-11-07 01:15:11 -08:00
reg output_axis_tvalid_reg = 1'b0, output_axis_tvalid_next;
2014-09-13 21:21:39 -07:00
2014-11-08 12:45:36 -08:00
// full when first MSB different but rest same
2015-11-07 01:15:11 -08:00
wire full = ((wr_ptr_reg[ADDR_WIDTH] != rd_ptr_reg[ADDR_WIDTH]) &&
(wr_ptr_reg[ADDR_WIDTH-1:0] == rd_ptr_reg[ADDR_WIDTH-1:0]));
2014-11-08 12:45:36 -08:00
// empty when pointers match exactly
2015-11-07 01:15:11 -08:00
wire empty = wr_ptr_reg == rd_ptr_reg;
2014-09-13 21:21:39 -07:00
2015-11-07 01:15:11 -08:00
// control signals
reg write;
reg read;
2014-09-13 21:21:39 -07:00
assign input_axis_tready = ~full;
2015-11-07 01:15:11 -08:00
2014-09-13 21:21:39 -07:00
assign output_axis_tvalid = output_axis_tvalid_reg;
2016-06-27 12:10:36 -07:00
assign mem_write_data = {input_axis_tlast, input_axis_tuser, input_axis_tdata};
assign {output_axis_tlast, output_axis_tuser, output_axis_tdata} = mem_read_data_reg;
2015-11-07 01:15:11 -08:00
// Write logic
always @* begin
write = 1'b0;
wr_ptr_next = wr_ptr_reg;
if (input_axis_tvalid) begin
// input data valid
if (~full) begin
// not full, perform write
write = 1'b1;
wr_ptr_next = wr_ptr_reg + 1;
end
end
end
2014-09-13 21:21:39 -07:00
2015-10-08 11:26:32 -07:00
always @(posedge clk) begin
2014-09-13 21:21:39 -07:00
if (rst) begin
2015-11-07 01:15:11 -08:00
wr_ptr_reg <= {ADDR_WIDTH+1{1'b0}};
end else begin
wr_ptr_reg <= wr_ptr_next;
end
wr_addr_reg <= wr_ptr_next;
2015-11-07 01:15:11 -08:00
if (write) begin
mem[wr_addr_reg[ADDR_WIDTH-1:0]] <= mem_write_data;
2014-09-13 21:21:39 -07:00
end
end
2015-11-07 01:15:11 -08:00
// Read logic
always @* begin
read = 1'b0;
rd_ptr_next = rd_ptr_reg;
output_axis_tvalid_next = output_axis_tvalid_reg;
if (output_axis_tready | ~output_axis_tvalid) begin
// output data not valid OR currently being transferred
if (~empty) begin
// not empty, perform read
read = 1'b1;
output_axis_tvalid_next = 1'b1;
rd_ptr_next = rd_ptr_reg + 1;
end else begin
output_axis_tvalid_next = 1'b0;
end
2014-09-13 21:21:39 -07:00
end
end
2015-10-08 11:26:32 -07:00
always @(posedge clk) begin
2014-09-13 21:21:39 -07:00
if (rst) begin
2015-11-07 01:15:11 -08:00
rd_ptr_reg <= {ADDR_WIDTH+1{1'b0}};
2014-09-13 21:21:39 -07:00
output_axis_tvalid_reg <= 1'b0;
end else begin
2015-11-07 01:15:11 -08:00
rd_ptr_reg <= rd_ptr_next;
output_axis_tvalid_reg <= output_axis_tvalid_next;
end
rd_addr_reg <= rd_ptr_next;
2015-11-07 01:15:11 -08:00
if (read) begin
mem_read_data_reg <= mem[rd_addr_reg[ADDR_WIDTH-1:0]];
2014-09-13 21:21:39 -07:00
end
end
endmodule