arunragavan
Advanced Member level 1
Well guys.. i need a synthesisable VHDL code for a FIFO.. asynchronous FIFO. Can anyone help me with the code and as well explain the basic workin for a asyn FIFO unit.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
arunragavan said:Well guys.. i need a synthesisable VHDL code for a FIFO.. asynchronous FIFO. Can anyone help me with the code and as well explain the basic workin for a asyn FIFO unit.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity dp_ram is
generic(
WIDTH:natural:=8; --data width
ADDR_WIDTH:natural:=8 --address width
);
port(
rst :in std_logic; --reset
rd_clk :in std_logic; --read clock
wr_clk :in std_logic; --write clock
data_in :in std_logic_vector(WIDTH-1 downto 0); --input data
data_out :out std_logic_vector(WIDTH-1 downto 0); --output data
rd_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0); --read address
wr_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0); --write address
rd_en :in std_logic; --read enable
wr_en :in std_logic --write enable
);
end entity dp_ram;
architecture mem of dp_ram is
type register_array is array(natural range <>) of std_logic_vector(WIDTH-1 downto 0);
signal mem:register_array(0 to (2**ADDR_WIDTH-1));
begin
--citanje iz memorije
read:process(rst,rd_clk,rd_en,rd_addr) is
begin
if (rst='1') then
data_out<=(others => '0');
else --if rising_edge(rd_clk) then
-- if (rd_en='1') then
data_out<=mem(conv_integer(rd_addr));
-- else
-- data_out<=mem(conv_integer(rd_addr));
-- data_out<=(others=>'Z');
-- end if;
end if;
end process read;
write:process(rst,wr_clk) is
begin
if (rst='1') then
for i in 0 to (2**ADDR_WIDTH-1) loop
mem(i)<=(others=>'0');
end loop;
elsif rising_edge(wr_clk) then
if (wr_en='1') then
mem(conv_integer(wr_addr))<=data_in;
end if;
end if;
end process write;
end architecture mem;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity fifo is
generic(
WIDTH:natural:=8;
ADDR_WIDTH:natural:=2
);
port(
rst :in std_logic; --reset
clk :in std_logic; --clock
data_in :in std_logic_vector(WIDTH-1 downto 0);
data_out :out std_logic_vector(WIDTH-1 downto 0);
rd_en :in std_logic; --read enable
wr_en :in std_logic; --write enable
empty :out std_logic; --queue is empty
full :out std_logic --queue is full
);
end entity fifo;
architecture RTL of fifo is
signal rd_ptr:std_logic_vector(ADDR_WIDTH-1 downto 0):=(others=>'0'); --read pointer
signal wr_ptr:std_logic_vector(ADDR_WIDTH-1 downto 0):=(others=>'0'); --write pointer
signal cnt:std_logic_vector(ADDR_WIDTH downto 0):=(others=>'0'); --count
signal valid_rd:std_logic:='0';
signal valid_wr:std_logic:='0';
signal is_empty:std_logic:='1';
signal is_full:std_logic:='0';
constant MAX:std_logic_vector(ADDR_WIDTH downto 0):=('1',others=>'0');
constant MIN:std_logic_vector(ADDR_WIDTH downto 0):=(others=>'0');
component dp_ram
generic(
WIDTH:natural:=8;
ADDR_WIDTH:natural:=8
);
port(
rst :in std_logic;
rd_clk :in std_logic;
wr_clk :in std_logic;
data_in :in std_logic_vector(WIDTH-1 downto 0);
data_out :out std_logic_vector(WIDTH-1 downto 0);
rd_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0);
wr_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0);
rd_en :in std_logic;
wr_en :in std_logic
);
end component dp_ram;
begin
memory:dp_ram
generic map(
WIDTH=>WIDTH,
ADDR_WIDTH=>ADDR_WIDTH
)
port map(
rst=>rst,
rd_clk=>clk,
wr_clk=>clk,
data_in=>data_in,
data_out=>data_out,
rd_addr=>rd_ptr(ADDR_WIDTH-1 downto 0),
wr_addr=>wr_ptr(ADDR_WIDTH-1 downto 0),
rd_en=>valid_rd,
wr_en=>valid_wr
);
valid_rd<='1' when (rd_en='1' and is_empty='0') else '0';
valid_wr<='1' when (wr_en='1' and is_full='0') else '0';
is_empty<='1' when cnt=MIN else '0';
is_full<='1' when cnt=MAX else'0';
main:process(rst,clk) is
begin
if (rst='1') then
rd_ptr<=(others=>'0');
wr_ptr<=(others=>'0');
cnt<=(others=>'0');
elsif rising_edge(clk) then
if (valid_rd='1') then
rd_ptr<=rd_ptr+1;
if (valid_wr='1') then
cnt<=cnt;
else
cnt<=cnt-1;
end if;
end if;
if (valid_wr='1') then
wr_ptr<=wr_ptr+1;
if (valid_rd='1') then
cnt<=cnt;
else
cnt<=cnt+1;
end if;
end if;
end if;
end process main;
empty<=is_empty;
full<=is_full;
end architecture RTL;
configuration dp_fifo of fifo is
for RTL
for memory:dp_ram
use entity work.dp_ram(mem);
end for;
end for;
end configuration dp_fifo;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity dp_ram is
generic(
WIDTH:natural:=8; --data width
ADDR_WIDTH:natural:=8 --address width
);
port(
rst :in std_logic; --reset
rd_clk :in std_logic; --read clock
wr_clk :in std_logic; --write clock
data_in :in std_logic_vector(WIDTH-1 downto 0); --input data
data_out :out std_logic_vector(WIDTH-1 downto 0); --output data
rd_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0); --read address
wr_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0); --write address
rd_en :in std_logic; --read enable
wr_en :in std_logic --write enable
);
end entity dp_ram;
architecture mem of dp_ram is
type register_array is array(natural range <>) of std_logic_vector(WIDTH-1 downto 0);
signal mem:register_array(0 to (2**ADDR_WIDTH-1));
begin
--citanje iz memorije
read:process(rst,rd_clk,rd_en,rd_addr) is
begin
if (rst='1') then
data_out<=(others => '0');
else --if rising_edge(rd_clk) then
-- if (rd_en='1') then
data_out<=mem(conv_integer(rd_addr));
-- else
-- data_out<=mem(conv_integer(rd_addr));
-- data_out<=(others=>'Z');
-- end if;
end if;
end process read;
write:process(rst,wr_clk) is
begin
if (rst='1') then
for i in 0 to (2**ADDR_WIDTH-1) loop
mem(i)<=(others=>'0');
end loop;
elsif rising_edge(wr_clk) then
if (wr_en='1') then
mem(conv_integer(wr_addr))<=data_in;
end if;
end if;
end process write;
end architecture mem;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity fifo is
generic(
WIDTH:natural:=8;
ADDR_WIDTH:natural:=2
);
port(
rst :in std_logic; --reset
clk :in std_logic; --clock
data_in :in std_logic_vector(WIDTH-1 downto 0);
data_out :out std_logic_vector(WIDTH-1 downto 0);
rd_en :in std_logic; --read enable
wr_en :in std_logic; --write enable
empty :out std_logic; --queue is empty
full :out std_logic --queue is full
);
end entity fifo;
architecture RTL of fifo is
signal rd_ptr:std_logic_vector(ADDR_WIDTH-1 downto 0):=(others=>'0'); --read pointer
signal wr_ptr:std_logic_vector(ADDR_WIDTH-1 downto 0):=(others=>'0'); --write pointer
signal cnt:std_logic_vector(ADDR_WIDTH downto 0):=(others=>'0'); --count
signal valid_rd:std_logic:='0';
signal valid_wr:std_logic:='0';
signal is_empty:std_logic:='1';
signal is_full:std_logic:='0';
constant MAX:std_logic_vector(ADDR_WIDTH downto 0):=('1',others=>'0');
constant MIN:std_logic_vector(ADDR_WIDTH downto 0):=(others=>'0');
component dp_ram
generic(
WIDTH:natural:=8;
ADDR_WIDTH:natural:=8
);
port(
rst :in std_logic;
rd_clk :in std_logic;
wr_clk :in std_logic;
data_in :in std_logic_vector(WIDTH-1 downto 0);
data_out :out std_logic_vector(WIDTH-1 downto 0);
rd_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0);
wr_addr :in std_logic_vector(ADDR_WIDTH-1 downto 0);
rd_en :in std_logic;
wr_en :in std_logic
);
end component dp_ram;
begin
memory:dp_ram
generic map(
WIDTH=>WIDTH,
ADDR_WIDTH=>ADDR_WIDTH
)
port map(
rst=>rst,
rd_clk=>clk,
wr_clk=>clk,
data_in=>data_in,
data_out=>data_out,
rd_addr=>rd_ptr(ADDR_WIDTH-1 downto 0),
wr_addr=>wr_ptr(ADDR_WIDTH-1 downto 0),
rd_en=>valid_rd,
wr_en=>valid_wr
);
valid_rd<='1' when (rd_en='1' and is_empty='0') else '0';
valid_wr<='1' when (wr_en='1' and is_full='0') else '0';
is_empty<='1' when cnt=MIN else '0';
is_full<='1' when cnt=MAX else'0';
main:process(rst,clk) is
begin
if (rst='1') then
rd_ptr<=(others=>'0');
wr_ptr<=(others=>'0');
cnt<=(others=>'0');
elsif rising_edge(clk) then
if (valid_rd='1') then
rd_ptr<=rd_ptr+1;
if (valid_wr='1') then
cnt<=cnt;
else
cnt<=cnt-1;
end if;
end if;
if (valid_wr='1') then
wr_ptr<=wr_ptr+1;
if (valid_rd='1') then
cnt<=cnt;
else
cnt<=cnt+1;
end if;
end if;
end if;
end process main;
empty<=is_empty;
full<=is_full;
end architecture RTL;
configuration dp_fifo of fifo is
for RTL
for memory:dp_ram
use entity work.dp_ram(mem);
end for;
end for;
end configuration dp_fifo;