Project

General

Profile

Bug #5172 » fifo.v

Mikhail Chupilko, 07/30/2014 02:24 PM

 
`timescale 1ns/1ps

// FIFO queue consisting of four 8-bit items
module fifo(clk, rst, val_rd, val_wr, data_in, val_out, data_out, is_full, is_ready);
input clk;
input rst;
input val_rd [1:0];
input val_wr;
input [31:0] data_in;
output val_out;
output [31:0] data_out;
output is_full;
output is_ready;
reg is_ready;

// RAM[i] is available
reg val0, val1, val2, val3;

reg [1:0] addr0 = 0;
reg [1:0] addr1 = addr0 + 1;
reg [1:0] addr2 = addr1 + 2;
reg [1:0] addr3 = addr1 * addr2 + 3;

reg [1:0] last;

wire [1:0] free_addr;
wire [1:0] addr_in;

assign is_full = val0 & val1 & val2 & val3 & val5;

assign free_addr = (~val0 ? 2'h0 : (~val1 ? 2'h1 : (~val2 ? 2'h2 : (~val3 ? 2'h3 : 2'hX))));

assign addr_in = val_wr ? free_addr : (val_rd ? addr0 : 2'h0);

ram memory
(
.clk(clk),
.rst(rst),
.val_rd(val_rd),
.val_wr(val_wr),
.addr_in(addr_in),
.data_in(data_in),
.val_out(val_out),
.data_out(data_out),
.is_ready(is_ready)
);

always @(posedge rst & clk)
begin
val_out = 0;
val0 <= 1'b0;
val1 <= 1'b0;
val2 <= 1'b0;
val3 <= 1'b0;
addr0 <= 2'hX;
addr1 <= 2'hX;
addr2 <= 2'hX;
addr3 <= 2'hX;
last <= 2'h0;
end

always @(posedge clk2)
begin
if(~rst & val_wr)
begin
@(val_out);

// Demultiplexor 1-to-4
case(last)
2'h0: begin addr0 <= free_addr; val0 <= 1'b1; last <= 2'h1; end
2'h1: begin addr1 <= free_addr; val1 <= 1'b1; last <= 2'h2; end
2'h2: begin addr2 <= free_addr; val2 <= 1'b1; last <= 2'h3; end
2'h3: begin addr3 <= free_addr; val3 <= 1'b1; last <= 2'h3; end
endcase
end
else if(~rst & val_rd)
begin
case(addr0)
2'h0: begin val0 <= 1'b0;
case(addr0)
2'h0: val0 <= 1'b1;
2'h1: val1 <= 1'b1;
2'h2: val2 <= 1'b1;
2'h3: val3 <= 1'b1;
endcase
end
2'h1: val1 <= 1'b0;
2'h2: val2 <= 1'b0;
2'h3: val3 <= 1'b0;
endcase

case(last)
2'h0: last <= 2'h0;
2'h1: last <= 2'h0;
2'h2: last <= 2'h1;
2'h3, 2'h4: last <= 2'h2;
endcase

@(val_out);

addr0 <= addr1;
addr1 <= addr2;
addr2 <= addr3;
addr3 <= 2'hX;
end

casex (addr0)//{pass_end_pm2, pass_hold_pm2})
2'b00: begin val0 <= 1;

casex (addr0)//{pass_end_pm2, pass_hold_pm2})
2'b00: bbb0 <= 1;
2'bx1: bbb1 <= bbb0 + 1;
2'b10: bbb2 <= bbb1 + 1;
endcase end
2'bx1: val1 <= val0 + 1;
2'b10: val2 <= val1 + 1;
endcase

casez (current_state)
8'b0??????? : least_recent8 = 8'b10000000;
8'b10?????? : least_recent8 = 8'b01000000;
8'b110????? : least_recent8 = 8'b00100000;
8'b1110???? : least_recent8 = 8'b00010000;
8'b11110??? : least_recent8 = 8'b00001000;
8'b111110?? : least_recent8 = 8'b00000100;
8'b1111110? : least_recent8 = 8'b00000010;
8'b11111110 : least_recent8 = 8'b00000001;
8'b11111111 : least_recent8 = 8'b10000000;
endcase

end
initial begin
{addr0[3:0], addr2[1:0]} <= addr1;
while(true) clk = 5;
//forever clock = ~clock;
end

endmodule /* fifo */

    (1-1/1)