XST VHDL Ram
XST VHDL Ram
XST VHDL Ram
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
200 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
RAM-Supported Features
RAM-supported features include:
• RAM Inferencing Capabilities
• Parity Bits
Parity Bits
XST does not support parity bits.
• Parity bits are available on certain block RAM primitives.
• XST can use parity bits as regular data bits in order to accommodate the described
data widths.
• XST cannot:
– Automatically generate parity control logic.
– Use those parity bit positions for their intended purpose.
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 201
Chapter 7: HDL Coding Techniques
RAM Modeling
RAM is usually modeled with an array of array object.
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
202 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 203
Chapter 7: HDL Coding Techniques
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
204 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 205
Chapter 7: HDL Coding Techniques
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
206 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 207
Chapter 7: HDL Coding Techniques
entity bytewrite_ram_1b is
generic (
SIZE : integer := 1024;
ADDR_WIDTH : integer := 10;
COL_WIDTH : integer := 8;
NB_COL : integer := 2);
port (
clk : in std_logic;
we : in std_logic_vector(NB_COL-1 downto 0);
addr : in std_logic_vector(ADDR_WIDTH-1 downto 0);
di : in std_logic_vector(NB_COL*COL_WIDTH-1 downto 0);
do : out std_logic_vector(NB_COL*COL_WIDTH-1 downto 0));
end bytewrite_ram_1b;
begin
process (clk)
begin
if rising_edge(clk) then
do <= RAM(conv_integer(addr));
for i in 0 to NB_COL-1 loop
if we(i) = ’1’ then
RAM(conv_integer(addr))((i+1)*COL_WIDTH-1 downto i*COL_WIDTH)
<= di((i+1)*COL_WIDTH-1 downto i*COL_WIDTH);
end if;
end loop;
end if;
end process;
end behavioral;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
208 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input [NB_COL-1:0] we;
input [ADDR_WIDTH-1:0] addr;
input [NB_COL*COL_WIDTH-1:0] di;
output reg [NB_COL*COL_WIDTH-1:0] do;
generate
genvar i;
for (i = 0; i < NB_COL; i = i+1)
begin
always @(posedge clk)
begin
if (we[i])
RAM[addr][(i+1)*COL_WIDTH-1:i*COL_WIDTH] <= di[(i+1)*COL_WIDTH-1:i*COL_WIDTH];
end
end
endgenerate
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 209
Chapter 7: HDL Coding Techniques
entity bytewrite_nochange is
generic (
SIZE : integer := 1024;
ADDR_WIDTH : integer := 10;
COL_WIDTH : integer := 8;
NB_COL : integer := 2);
port (
clk : in std_logic;
we : in std_logic_vector(NB_COL-1 downto 0);
addr : in std_logic_vector(ADDR_WIDTH-1 downto 0);
di : in std_logic_vector(NB_COL*COL_WIDTH-1 downto 0);
do : out std_logic_vector(NB_COL*COL_WIDTH-1 downto 0));
end bytewrite_nochange;
process (clk)
begin
if rising_edge(clk) then
if (we = (we’range => ’0’)) then
do <= RAM(conv_integer(addr));
end if;
for i in 0 to NB_COL-1 loop
if we(i) = ’1’ then
RAM(conv_integer(addr))((i+1)*COL_WIDTH-1 downto i*COL_WIDTH)
<= di((i+1)*COL_WIDTH-1 downto i*COL_WIDTH);
end if;
end loop;
end if;
end process;
end behavioral;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
210 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input [NB_COL-1:0] we;
input [ADDR_WIDTH-1:0] addr;
input [NB_COL*COL_WIDTH-1:0] di;
output reg [NB_COL*COL_WIDTH-1:0] do;
generate
genvar i;
for (i = 0; i < NB_COL; i = i+1)
begin
always @(posedge clk)
begin
if (we[i])
RAM[addr][(i+1)*COL_WIDTH-1:i*COL_WIDTH]
<= di[(i+1)*COL_WIDTH-1:i*COL_WIDTH];
end
end
endgenerate
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 211
Chapter 7: HDL Coding Techniques
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
212 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
entity rams_24 is
generic (
SIZE : integer := 512;
ADDR_WIDTH : integer := 9;
COL_WIDTH : integer := 16;
NB_COL : integer := 2);
port (
clk : in std_logic;
we : in std_logic_vector(NB_COL-1 downto 0);
addr : in std_logic_vector(ADDR_WIDTH-1 downto 0);
di : in std_logic_vector(NB_COL*COL_WIDTH-1 downto 0);
do : out std_logic_vector(NB_COL*COL_WIDTH-1 downto 0));
end rams_24;
process(we, di)
begin
if we(1) = ’1’ then
di1 <= di(2*COL_WIDTH-1 downto 1*COL_WIDTH);
else
di1 <= RAM(conv_integer(addr))(2*COL_WIDTH-1 downto 1*COL_WIDTH);
end if;
process(clk)
begin
if (clk’event and clk = ’1’) then
do <= RAM(conv_integer(addr));
RAM(conv_integer(addr)) <= di1 & di0;
end if;
end process;
end syn;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 213
Chapter 7: HDL Coding Techniques
input clk;
input [1:0] we;
input [ADDR_WIDTH-1:0] addr;
input [2*DI_WIDTH-1:0] di;
output [2*DI_WIDTH-1:0] do;
reg [2*DI_WIDTH-1:0] RAM [SIZE-1:0];
reg [2*DI_WIDTH-1:0] do;
if (we[0])
di0 = di[DI_WIDTH-1:0];
else
di0 = RAM[addr][DI_WIDTH-1:0];
end
endmodule
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
214 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 215
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_1a is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
weA : in std_logic;
enA : in std_logic;
enB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_1a;
begin
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
216 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
process (clkA)
begin
if rising_edge(clkA) then
if enA = ’1’ then
if weA = ’1’ then
ram(conv_integer(addrA)) <= diA;
end if;
end if;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if enB = ’1’ then
readB(minWIDTH-1 downto 0)
<= ram(conv_integer(addrB&conv_std_logic_vector(0,2)));
readB(2*minWIDTH-1 downto minWIDTH)
<= ram(conv_integer(addrB&conv_std_logic_vector(1,2)));
readB(3*minWIDTH-1 downto 2*minWIDTH)
<= ram(conv_integer(addrB&conv_std_logic_vector(2,2)));
readB(4*minWIDTH-1 downto 3*minWIDTH)
<= ram(conv_integer(addrB&conv_std_logic_vector(3,2)));
end if;
regB <= readB;
end if;
end process;
end behavioral;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 217
Chapter 7: HDL Coding Techniques
parameter WIDTHA = 8;
parameter SIZEA = 256;
parameter ADDRWIDTHA = 8;
parameter WIDTHB = 32;
parameter SIZEB = 64;
parameter ADDRWIDTHB = 6;
input clkA;
input clkB;
input weA;
input reB;
input [ADDRWIDTHA-1:0] addrA;
input [ADDRWIDTHB-1:0] addrB;
input [WIDTHA-1:0] diA;
output reg [WIDTHB-1:0] doB;
endmodule
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
218 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_1b is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
weA : in std_logic;
enA : in std_logic;
enB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_1b;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 219
Chapter 7: HDL Coding Techniques
begin
for i in 30 downto 0 loop
if (val >= (2**i)) then
res := i;
exit;
end if;
end loop;
return res;
end function log2;
begin
process (clkA)
begin
if rising_edge(clkA) then
if enA = ’1’ then
if weA = ’1’ then
ram(conv_integer(addrA)) <= diA;
end if;
end if;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if enB = ’1’ then
for i in 0 to RATIO-1 loop
readB((i+1)*minWIDTH-1 downto i*minWIDTH)
<= ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))));
end loop;
end if;
regB <= readB;
end if;
end process;
end behavioral;
parameter WIDTHA = 8;
parameter SIZEA = 256;
parameter ADDRWIDTHA = 8;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
220 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clkA;
input clkB;
input weA;
input reB;
input [ADDRWIDTHA-1:0] addrA;
input [ADDRWIDTHB-1:0] addrB;
input [WIDTHA-1:0] diA;
output reg [WIDTHB-1:0] doB;
genvar i;
endmodule
Note These coding examples use min, max, and log2 functions to make the code as
generic and clean as possible. Those functions can be defined anywhere in the design,
typically in a package.
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 221
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_4 is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
reA : in std_logic;
weB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diB : in std_logic_vector(WIDTHB-1 downto 0);
doA : out std_logic_vector(WIDTHA-1 downto 0)
);
end asymmetric_ram_4;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
222 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
begin
process (clkA)
begin
if rising_edge(clkA) then
if reA = ’1’ then
readA <= ram(conv_integer(addrA));
end if;
regA <= readA;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if weB = ’1’ then
for i in 0 to RATIO-1 loop
ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))))
:= diB((i+1)*minWIDTH-1 downto i*minWIDTH);
end loop;
end if;
end if;
end process;
end behavioral;
Caution! Shared variables are an extension of variables, from which they inherit all
basic characteristics, allowing inter-process communication. Use them with great
caution.
• The order in which items in a sequential process are described can condition the
functionality being modeled.
• Two or more processes making assignments to a shared variable in the same
simulation cycle can lead to unpredictable results.
Read-Write Synchronization
• Read-Write synchronization is controlled in a similar manner, whether describing a
symmetric or asymmetric RAM.
• The following coding examples describe a RAM with two asymmetric read-write
ports, and illustrate how to respectively model write-first, read-first, and no-change
synchronization.
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 223
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_2b is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
enA : in std_logic;
enB : in std_logic;
weA : in std_logic;
weB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
diB : in std_logic_vector(WIDTHB-1 downto 0);
doA : out std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_2b;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
224 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
end loop;
return res;
end function log2;
begin
process (clkA)
begin
if rising_edge(clkA) then
if enA = ’1’ then
if weA = ’1’ then
ram(conv_integer(addrA)) := diA;
end if;
readA <= ram(conv_integer(addrA));
end if;
regA <= readA;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if enB = ’1’ then
if weB = ’1’ then
for i in 0 to RATIO-1 loop
ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))))
:= diB((i+1)*minWIDTH-1 downto i*minWIDTH);
end loop;
end if;
for i in 0 to RATIO-1 loop
readB((i+1)*minWIDTH-1 downto i*minWIDTH)
<= ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))));
end loop;
end if;
regB <= readB;
end if;
end process;
end behavioral;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 225
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_2c is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
enA : in std_logic;
enB : in std_logic;
weA : in std_logic;
weB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
diB : in std_logic_vector(WIDTHB-1 downto 0);
doA : out std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_2c;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
226 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
end loop;
return res;
end function log2;
begin
process (clkA)
begin
if rising_edge(clkA) then
if enA = ’1’ then
readA <= ram(conv_integer(addrA));
if weA = ’1’ then
ram(conv_integer(addrA)) := diA;
end if;
end if;
regA <= readA;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if enB = ’1’ then
for i in 0 to RATIO-1 loop
readB((i+1)*minWIDTH-1 downto i*minWIDTH)
<= ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))));
end loop;
if weB = ’1’ then
for i in 0 to RATIO-1 loop
ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))))
:= diB((i+1)*minWIDTH-1 downto i*minWIDTH);
end loop;
end if;
end if;
regB <= readB;
end if;
end process;
end behavioral;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 227
Chapter 7: HDL Coding Techniques
entity asymmetric_ram_2d is
generic (
WIDTHA : integer := 8;
SIZEA : integer := 256;
ADDRWIDTHA : integer := 8;
WIDTHB : integer := 32;
SIZEB : integer := 64;
ADDRWIDTHB : integer := 6
);
port (
clkA : in std_logic;
clkB : in std_logic;
enA : in std_logic;
enB : in std_logic;
weA : in std_logic;
weB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
diB : in std_logic_vector(WIDTHB-1 downto 0);
doA : out std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_2d;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
228 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
end loop;
return res;
end function log2;
begin
process (clkA)
begin
if rising_edge(clkA) then
if enA = ’1’ then
if weA = ’1’ then
ram(conv_integer(addrA)) := diA;
else
readA <= ram(conv_integer(addrA));
end if;
end if;
regA <= readA;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
if enB = ’1’ then
for i in 0 to RATIO-1 loop
if weB = ’1’ then
ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))))
:= diB((i+1)*minWIDTH-1 downto i*minWIDTH);
else
readB((i+1)*minWIDTH-1 downto i*minWIDTH)
<= ram(conv_integer(addrB & conv_std_logic_vector(i,log2(RATIO))));
end if;
end loop;
end if;
regB <= readB;
end if;
end process;
end behavioral;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 229
Chapter 7: HDL Coding Techniques
Parity Bits
For asymmetric port RAMs, XST can take advantage of the available block RAM parity
bits to implement extra data bits for word sizes of 9, 18 and 36 bits.
entity asymmetric_ram_3 is
generic (
WIDTHA : integer := 18;
SIZEA : integer := 2048;
ADDRWIDTHA : integer := 11;
WIDTHB : integer := 9;
SIZEB : integer := 4096;
ADDRWIDTHB : integer := 12
);
port (
clkA : in std_logic;
clkB : in std_logic;
weA : in std_logic;
reB : in std_logic;
addrA : in std_logic_vector(ADDRWIDTHA-1 downto 0);
addrB : in std_logic_vector(ADDRWIDTHB-1 downto 0);
diA : in std_logic_vector(WIDTHA-1 downto 0);
doB : out std_logic_vector(WIDTHB-1 downto 0)
);
end asymmetric_ram_3;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
230 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
end if;
end loop;
return res;
end function log2;
begin
process (clkA)
begin
if rising_edge(clkA) then
if weA = ’1’ then
for i in 0 to RATIO-1 loop
ram(conv_integer(addrA & conv_std_logic_vector(i,log2(RATIO))))
:= diA((i+1)*minWIDTH-1 downto i*minWIDTH);
end loop;
end if;
end if;
end process;
process (clkB)
begin
if rising_edge(clkB) then
regB <= readB;
if reB = ’1’ then
readB <= ram(conv_integer(addrB));
end if;
end if;
end process;
end behavioral;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 231
Chapter 7: HDL Coding Techniques
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
232 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
=========================================================================
HDL Synthesis Report
Macro Statistics
# RAMs : 1
256x8:64x32-bit dual-port RAM : 1
# Registers : 2
32-bit register : 2
=========================================================================
=========================================================================
* Advanced HDL Synthesis *
=========================================================================
=========================================================================
Advanced HDL Synthesis Report
Macro Statistics
# RAMs : 1
256x8:64x32-bit dual-port block RAM : 1
=========================================================================
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 233
Chapter 7: HDL Coding Techniques
initial begin
ram[31] = 20’h0200A; ram[30] = 20’h00300; ram[39] = 20’h08101;
(...)
ram[2] = 20’h02341; ram[1] = 20’h08201; ram[0] = 20’h0400D;
end
integer i;
initial for (i=0; i<DEPTH; i=i+1) ram[i] = 0;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
234 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
integer index;
initial begin
for (index = 0 ; index <= 97 ; index = index + 1)
ram[index] = 16’h8282;
ram[98] <= 16’h1111;
ram[99] <= 16’h7778;
for (index = 100 ; index <= 255 ; index = index + 1)
ram[index] = 16’hB8B8;
end
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 235
Chapter 7: HDL Coding Techniques
initial begin
$readmemb("rams_20c.data", ram, 0, 63);
end
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
236 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 237
Chapter 7: HDL Coding Techniques
Optimization Trade-Offs
The RAM Style constraint makes two optimization trade-offs available:
• block_power1
• block_power2
block_power1
• Achieves some degree of power reduction.
• May minimally impact power depending on memory characteristics.
• Minimally impacts performance.
• Uses the default block RAM decomposition method. This method:
– Is performance-oriented.
– Adds block RAM enable logic.
block_power2
• Provides more significant power reduction.
• May leave some performance capability unused.
• May induce additional slice logic.
• Uses a different block RAM decomposition method from block_power1.
– Attempts to reduce the number of block RAM primitives required to implement
an inferred memory. This method:
– Inserts block RAM enable logic in order to minimize the number of active block
RAM components.
– Creates multiplexing logic to read the data from active block RAM components.
Use block_power2 if:
• Your primary concern is power reduction, and
• You are willing to give up some degree of speed and area optimization.
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
238 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 239
Chapter 7: HDL Coding Techniques
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
240 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
RAM Reporting
• XST provides detailed information on inferred RAM, including:
– Size
– Synchronization
– Control signals
• RAM recognition consists of two steps:
1. HDL Synthesis
XST recognizes the presence of the memory structure in the HDL source code.
2. Advanced HDL Synthesis
After acquiring a more accurate picture of each RAM component, XST
implements them on distributed or block RAM resources, depending on
resource availability.
• An inferred block RAM is generally reported as shown in the following example.
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 241
Chapter 7: HDL Coding Techniques
=========================================================================
HDL Synthesis Report
Macro Statistics
# RAMs : 1
128x16-bit dual-port RAM : 1
# Registers : 1
16-bit register : 1
=========================================================================
=========================================================================
* Advanced HDL Synthesis *
=========================================================================
=========================================================================
Advanced HDL Synthesis Report
Macro Statistics
# RAMs : 1
128x16-bit dual-port block RAM : 1
=========================================================================
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
242 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
entity rams_04 is
port (clk : in std_logic;
we : in std_logic;
a : in std_logic_vector(5 downto 0);
di : in std_logic_vector(15 downto 0);
do : out std_logic_vector(15 downto 0));
end rams_04;
process (clk)
begin
if (clk’event and clk = ’1’) then
if (we = ’1’) then
RAM(conv_integer(a)) <= di;
end if;
end if;
end process;
do <= RAM(conv_integer(a));
end syn;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 243
Chapter 7: HDL Coding Techniques
input clk;
input we;
input [5:0] a;
input [5:0] dpra;
input [15:0] di;
output [15:0] spo;
output [15:0] dpo;
reg [15:0] ram [63:0];
endmodule
entity rams_01 is
port (clk : in std_logic;
we : in std_logic;
en : in std_logic;
addr : in std_logic_vector(5 downto 0);
di : in std_logic_vector(15 downto 0);
do : out std_logic_vector(15 downto 0));
end rams_01;
process (clk)
begin
if clk’event and clk = ’1’ then
if en = ’1’ then
if we = ’1’ then
RAM(conv_integer(addr)) <= di;
end if;
do <= RAM(conv_integer(addr)) ;
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
244 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input we;
input en;
input [5:0] addr;
input [15:0] di;
output [15:0] do;
reg [15:0] RAM [63:0];
reg [15:0] do;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 245
Chapter 7: HDL Coding Techniques
entity rams_02a is
port (clk : in std_logic;
we : in std_logic;
en : in std_logic;
addr : in std_logic_vector(5 downto 0);
di : in std_logic_vector(15 downto 0);
do : out std_logic_vector(15 downto 0));
end rams_02a;
process (clk)
begin
if clk’event and clk = ’1’ then
if en = ’1’ then
if we = ’1’ then
RAM(conv_integer(addr)) <= di;
do <= di;
else
do <= RAM( conv_integer(addr));
end if;
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
246 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input we;
input en;
input [5:0] addr;
input [15:0] di;
output [15:0] do;
reg [15:0] RAM [63:0];
reg [15:0] do;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 247
Chapter 7: HDL Coding Techniques
entity rams_03 is
port (clk : in std_logic;
we : in std_logic;
en : in std_logic;
addr : in std_logic_vector(5 downto 0);
di : in std_logic_vector(15 downto 0);
do : out std_logic_vector(15 downto 0));
end rams_03;
process (clk)
begin
if clk’event and clk = ’1’ then
if en = ’1’ then
if we = ’1’ then
RAM(conv_integer(addr)) <= di;
else
do <= RAM( conv_integer(addr));
end if;
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
248 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input we;
input en;
input [5:0] addr;
input [15:0] di;
output [15:0] do;
reg [15:0] RAM [63:0];
reg [15:0] do;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 249
Chapter 7: HDL Coding Techniques
Dual-Port Block RAM with Two Write Ports VHDL Coding Example
--
-- Dual-Port Block RAM with Two Write Ports
-- Correct Modelization with a Shared Variable
--
-- Download: http://www.xilinx.com/txpatches/pub/documentation/misc/xstug_examples.zip
-- File: HDL_Coding_Techniques/rams/rams_16b.vhd
--
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
entity rams_16b is
port(clka : in std_logic;
clkb : in std_logic;
ena : in std_logic;
enb : in std_logic;
wea : in std_logic;
web : in std_logic;
addra : in std_logic_vector(6 downto 0);
addrb : in std_logic_vector(6 downto 0);
dia : in std_logic_vector(15 downto 0);
dib : in std_logic_vector(15 downto 0);
doa : out std_logic_vector(15 downto 0);
dob : out std_logic_vector(15 downto 0));
end rams_16b;
process (CLKA)
begin
if CLKA’event and CLKA = ’1’ then
if ENA = ’1’ then
DOA <= RAM(conv_integer(ADDRA));
if WEA = ’1’ then
RAM(conv_integer(ADDRA)) := DIA;
end if;
end if;
end if;
end process;
process (CLKB)
begin
if CLKB’event and CLKB = ’1’ then
if ENB = ’1’ then
DOB <= RAM(conv_integer(ADDRB));
if WEB = ’1’ then
RAM(conv_integer(ADDRB)) := DIB;
end if;
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
250 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
Dual-Port Block RAM with Two Write Ports Verilog Coding Example
//
// Dual-Port Block RAM with Two Write Ports
//
// Download: http://www.xilinx.com/txpatches/pub/documentation/misc/xstug_examples.zip
// File: HDL_Coding_Techniques/rams/rams_16.v
//
module v_rams_16 (clka,clkb,ena,enb,wea,web,addra,addrb,dia,dib,doa,dob);
input clka,clkb,ena,enb,wea,web;
input [5:0] addra,addrb;
input [15:0] dia,dib;
output [15:0] doa,dob;
reg [15:0] ram [63:0];
reg [15:0] doa,dob;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 251
Chapter 7: HDL Coding Techniques
entity rams_18 is
port (clk : in std_logic;
en : in std_logic;
we : in std_logic;
rst : in std_logic;
addr : in std_logic_vector(6 downto 0);
di : in std_logic_vector(15 downto 0);
do : out std_logic_vector(15 downto 0));
end rams_18;
process (clk)
begin
if clk’event and clk = ’1’ then
if en = ’1’ then -- optional enable
if we = ’1’ then -- write enable
ram(conv_integer(addr)) <= di;
end if;
if rst = ’1’ then -- optional reset
do <= (others => ’0’);
else
do <= ram(conv_integer(addr));
end if;
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
252 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input en;
input we;
input rst;
input [6:0] addr;
input [15:0] di;
output [15:0] do;
reg [15:0] ram [127:0];
reg [15:0] do;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 253
Chapter 7: HDL Coding Techniques
entity rams_19 is
port (clk1, clk2 : in std_logic;
we, en1, en2 : in std_logic;
addr1 : in std_logic_vector(5 downto 0);
addr2 : in std_logic_vector(5 downto 0);
di : in std_logic_vector(15 downto 0);
res1 : out std_logic_vector(15 downto 0);
res2 : out std_logic_vector(15 downto 0));
end rams_19;
process (clk1)
begin
if rising_edge(clk1) then
if we = ’1’ then
ram(conv_integer(addr1)) <= di;
end if;
do1 <= ram(conv_integer(addr1));
end if;
end process;
process (clk2)
begin
if rising_edge(clk2) then
do2 <= ram(conv_integer(addr2));
end if;
end process;
process (clk1)
begin
if rising_edge(clk1) then
if en1 = ’1’ then
res1 <= do1;
end if;
end if;
end process;
process (clk2)
begin
if rising_edge(clk2) then
if en2 = ’1’ then
res2 <= do2;
end if;
end if;
end process;
end beh;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
254 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk1;
input clk2;
input we, en1, en2;
input [6:0] addr1;
input [6:0] addr2;
input [15:0] di;
output [15:0] res1;
output [15:0] res2;
reg [15:0] res1;
reg [15:0] res2;
reg [15:0] RAM [127:0];
reg [15:0] do1;
reg [15:0] do2;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 255
Chapter 7: HDL Coding Techniques
entity rams_20a is
port (clk : in std_logic;
we : in std_logic;
addr : in std_logic_vector(5 downto 0);
di : in std_logic_vector(19 downto 0);
do : out std_logic_vector(19 downto 0));
end rams_20a;
begin
process (clk)
begin
if rising_edge(clk) then
if we = ’1’ then
RAM(conv_integer(addr)) <= di;
end if;
do <= RAM(conv_integer(addr));
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
256 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
initial begin
ram[63] = 20’h0200A; ram[62] = 20’h00300; ram[61] = 20’h08101;
ram[60] = 20’h04000; ram[59] = 20’h08601; ram[58] = 20’h0233A;
ram[57] = 20’h00300; ram[56] = 20’h08602; ram[55] = 20’h02310;
ram[54] = 20’h0203B; ram[53] = 20’h08300; ram[52] = 20’h04002;
ram[51] = 20’h08201; ram[50] = 20’h00500; ram[49] = 20’h04001;
ram[48] = 20’h02500; ram[47] = 20’h00340; ram[46] = 20’h00241;
ram[45] = 20’h04002; ram[44] = 20’h08300; ram[43] = 20’h08201;
ram[42] = 20’h00500; ram[41] = 20’h08101; ram[40] = 20’h00602;
ram[39] = 20’h04003; ram[38] = 20’h0241E; ram[37] = 20’h00301;
ram[36] = 20’h00102; ram[35] = 20’h02122; ram[34] = 20’h02021;
ram[33] = 20’h00301; ram[32] = 20’h00102; ram[31] = 20’h02222;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 257
Chapter 7: HDL Coding Techniques
Initializing Block RAM From an External Data File VHDL Coding Example
--
-- Initializing Block RAM from external data file
--
-- Download: http://www.xilinx.com/txpatches/pub/documentation/misc/xstug_examples.zip
-- File: HDL_Coding_Techniques/rams/rams_20c.vhd
--
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all;
entity rams_20c is
port(clk : in std_logic;
we : in std_logic;
addr : in std_logic_vector(5 downto 0);
din : in std_logic_vector(31 downto 0);
dout : out std_logic_vector(31 downto 0));
end rams_20c;
begin
process (clk)
begin
if clk’event and clk = ’1’ then
if we = ’1’ then
RAM(conv_integer(addr)) <= to_bitvector(din);
end if;
dout <= to_stdlogicvector(RAM(conv_integer(addr)));
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
258 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
Initializing Block RAM From an External Data File Verilog Coding Example
//
// Initializing Block RAM from external data file
// Binary data
//
// Download: http://www.xilinx.com/txpatches/pub/documentation/misc/xstug_examples.zip
// File: HDL_Coding_Techniques/rams/rams_20c.v
//
module v_rams_20c (clk, we, addr, din, dout);
input clk;
input we;
input [5:0] addr;
input [31:0] din;
output [31:0] dout;
initial
begin
// $readmemb("rams_20c.data",ram, 0, 63);
$readmemb("rams_20c.data",ram);
end
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 259
Chapter 7: HDL Coding Techniques
entity rams_22 is
port (clk : in std_logic;
we : in std_logic;
addr : in std_logic_vector(8 downto 0);
di : in std_logic_vector(3 downto 0);
do : out std_logic_vector(3 downto 0));
end rams_22;
process (clk)
begin
if clk’event and clk = ’1’ then
if we = ’1’ then
RAM(conv_integer(addr)) <= di;
else
pipe_reg <= RAM( conv_integer(addr));
end if;
do <= pipe_reg;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
260 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input we;
input [8:0] addr;
input [3:0] di;
output [3:0] do;
(*ram_style="pipe_distributed"*)
reg [3:0] RAM [511:0];
reg [3:0] do;
reg [3:0] pipe_reg;
do <= pipe_reg;
end
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 261
Chapter 7: HDL Coding Techniques
ROM Description
ROM Description includes:
• ROM Modeling
• Describing Read Access
ROM Modeling
ROM Modeling includes:
• Loading ROM From an External Data File
• ROM Modeling in VHDL
• ROM Modeling in Verilog
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
262 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
initial begin
rom[0] = 16’b0011111100000010;
rom[1] = 16’b0000000100001001;
rom[2] = 16’b0001000000111000;
rom[3] = 16’b0000000000000000;
rom[4] = 16’b1100001010011000;
rom[5] = 16’b0000000000000000;
rom[6] = 16’b0000000110000000;
rom[7] = 16’b0111111111110000;
rom[8] = 16’b0010000010001001;
rom[9] = 16’b0101010101011000;
rom[10] = 16’b1111111010101010;
rom[11] = 16’b0000000000000000;
rom[12] = 16’b1110000000001000;
rom[13] = 16’b0000000110001010;
rom[14] = 16’b0110011100010000;
rom[15] = 16’b0000100010000000;
end
Describing ROM With a Case Statement Verilog Coding Example
You can also describe the ROM with a case statement (or equivalent if-elseif construct).
input [3:0] addr
output reg [15:0] data;
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 263
Chapter 7: HDL Coding Techniques
ROM Implementation
• When XST detects that a properly synchronized ROM can be implemented on block
RAM resources, it applies the principles outlined in Block RAM Optimization
Strategies.
• To override any default XST decision criteria, use ROM Style instead of RAM Style.
• For more information about ROM Style, see Chapter 9, Design Constraints.
• For more information about ROM implementation, see Chapter 8, FPGA
Optimization.
ROM Reporting
The following report shows how the Read-Only Memory (ROM) is identified during
HDL Synthesis. Based on the availability of proper synchronization, the decision to
implement a ROM on block RAM resources is made during Advanced HDL Synthesis.
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
264 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
=========================================================================
HDL Synthesis Report
Macro Statistics
# ROMs : 1
128x20-bit ROM : 1
# Registers : 1
20-bit register : 1
=========================================================================
=========================================================================
* Advanced HDL Synthesis *
=========================================================================
=========================================================================
Advanced HDL Synthesis Report
Macro Statistics
# RAMs : 1
128x20-bit single-port block RAM : 1
=========================================================================
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 265
Chapter 7: HDL Coding Techniques
entity roms_constant is
port (clk : in std_logic;
en : in std_logic;
addr : in std_logic_vector(6 downto 0);
data : out std_logic_vector(19 downto 0));
end roms_constant;
begin
process (clk)
begin
if (clk’event and clk = ’1’) then
if (en = ’1’) then
data <= ROM(conv_integer(addr));
end if;
end if;
end process;
end syn;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
266 www.xilinx.com UG687 (v 14.5) March 20, 2013
Chapter 7: HDL Coding Techniques
input clk;
input en;
input [5:0] addr;
output reg [19:0] data;
endmodule
XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices Send Feedback
UG687 (v 14.5) March 20, 2013 www.xilinx.com 267
Chapter 7: HDL Coding Techniques
entity roms_dualport is
port (clk : in std_logic;
ena, enb : in std_logic;
addra, addrb : in std_logic_vector(5 downto 0);
dataa, datab : out std_logic_vector(19 downto 0));
end roms_dualport;
begin
process (clk)
begin
if rising_edge(clk) then
if (ena = ’1’) then
dataa <= ROM(conv_integer(addra));
end if;
end if;
end process;
process (clk)
begin
if rising_edge(clk) then
if (enb = ’1’) then
datab <= ROM(conv_integer(addrb));
end if;
end if;
end process;
end behavioral;
Send Feedback XST User Guide for Virtex-6, Spartan-6, and 7 Series Devices
268 www.xilinx.com UG687 (v 14.5) March 20, 2013