Digital Design With Synthesizable VHDL
Digital Design With Synthesizable VHDL
Digital Design With Synthesizable VHDL
Synthesizable VHDL
Prof. Stephen A. Edwards
sedwards@cs.columbia.edu
Columbia University
Spring 2010
entity ALU is
port( A: in unsigned(1 downto 0);
B: in unsigned(1 downto 0);
Sel: in unsigned(1 downto 0);
Res: out unsigned(1 downto 0));
end ALU;
architecture behv of ALU is begin
process (A,B,Sel) begin
case Sel is
when "00" => Res <= A + B;
when "01" => Res <= A + (not B) + 1;
when "10" => Res <= A and B;
when "11" => Res <= A or B;
when others => Res <= "XX";
end case;
end process;
end behv;
Identifiers: [a-zA-Z](_?[a-zA-Z0-9])*
Examples: X X_or_Y ADDR addr
Illegal: 14M CLK__4 FOO_
Binary 1 0
package std_logic_1164 is
type std_ulogic is
( ’U’, -- Uninitialized
’X’, -- Forcing Unknown
’0’, -- Forcing 0
’1’, -- Forcing 1
’Z’, -- High Impedance
’W’, -- Weak Unknown
’L’, -- Weak 0
’H’, -- Weak 1
’-’ -- Don’t care
);
-- Precedence
not a or b and c = (not a) or (b and c)
-- Basic relationships
not not a = a
a and ’1’ = a
a and ’0’ = ’0’
a or ’1’ = ’1’
a or ’0’ = a
a and a = a
a and not a = ’0’
a or a = a
a or not a = ’1’
a nand b = not (a and b)
a nor b = not (a or b)
a xor ’0’ = a
a xor ’1’ = not a
a xor b = (not a and b) or (a and not b) Digital Design with Synthesizable VHDL – p. 1
Rules of Boolean Algebra (2)
-- Commutativity
a and b = b and a
a or b = b or a
-- Associativity
a and (b and c) = (a and b) and c
a or (b or c) = (a or b) or c
-- Distributivity
a and (b or c) = a and b or a and c
a or (b and c) = (a or b) and (a or c)
-- De Morgan’s Law
not (a and b) = not a or not b
not (a or b) = not a and not b
sum <= (not a and not b and c) or (not a and b and not c) or
(a and not b and not c) or (a and b and c);
Process
Ports
process (clk)
in begin
Component
if rising_edge(clk) then
in count <= count + 1;
end if;
end process;
out Component
out Signal
inout X <= ’1’ when Y = ’1’ and X = "110" else ’0’
Dataflow Expression
carry~ 3
b
c
carry~ 0 carry~ 4
a
carry
carry~ 1
sum~ 1
sum
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity vectors is
port(vect : in std_logic_vector(1 downto 0);
unsi : in unsigned(7 downto 0);
sign : out unsigned(15 downto 0));
end entity; Digital Design with Synthesizable VHDL – p. 1
Endianness
f b
e c
entity multiplexer_4_1 is
port(in0, in1, in2, in3 : in unsigned(15 downto 0);
s : in unsigned(1 downto 0);
z : out unsigned(15 downto 0));
end multiplexer_4_1;
entity multiplexer_4_1 is
port(in0, in1, in2, in3 : in unsigned(15 downto 0);
s0, s1 : in std_logic;
z : out unsigned(15 downto 0));
end multiplexer_4_1;
end comb;
Three-to-eight Decoder
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity dec1_8 is
port (
sel : in unsigned(2 downto 0);
res : out unsigned(7 downto 0));
end dec1_8;
entity priority is
port (
sel : in std_logic_vector(7 downto 0);
code : out unsigned(2 downto 0));
end priority;
end imp;
Integer Arithmetic
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity adder is
port (
A, B : in unsigned(7 downto 0);
CI : in std_logic;
SUM : out unsigned(7 downto 0);
CO : out std_logic);
end adder;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity alu is
port (
A, B : in unsigned(7 downto 0);
ADD : in std_logic;
RES : out unsigned(7 downto 0));
end alu;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity comparator is
port (
A, B : in unsigned(7 downto 0);
GE : out std_logic);
end comparator;
entity tri_demo is
port(addr : out unsigned(15 downto 0); -- output only
data : inout unsigned(7 downto 0)); -- bidirectional
end tri_demo;
bit1 : full_adder port map (a => A(1), b => B(1), c => carry,
sum => C(1), carry => C(2));
end imp; Digital Design with Synthesizable VHDL – p. 3
Direct Instantiation (no component)
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity add2 is
port (A, B : in unsigned(1 downto 0);
C : out unsigned(2 downto 0));
end add2;
entity dumb_inv is
port( a: in std_logic; y : out std_logic );
end dumb_inv;
end comb;
A 4-to-1 mux using case
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity cmultiplexer_4_1 is
port(in0, in1, in2, in3 : in unsigned(15 downto 0);
s : in unsigned(1 downto 0);
z : out unsigned(15 downto 0));
end cmultiplexer_4_1;
architecture comb of cmultiplexer_4_1 is
begin
process (in0, in1, in2, in3, s)
begin
case s is
when "00" => z <= in0;
when "01" => z <= in1;
when "10" => z <= in2;
when "11" => z <= in3;
when others => z <= (others => ’X’);
end case;
end process; Digital Design with Synthesizable VHDL – p. 4
end comb;
An Address Decoder
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity adecoder is
port(a : in unsigned(15 downto 0);
ram, rom, video, io : out std_logic);
end adecoder;
null
signal <= expr ;
variable := expr ;
if expr then stmts
(elsif expr then stmts)∗
(else stmts)?
end if;
case expr is
(when choices => stmts)∗
end case;
entity flipflop is
port (Clk, D : in std_logic;
Q : out std_logic);
end flipflop;
D Q
end imp;
Digital Design with Synthesizable VHDL – p. 4
Flip-Flop with Latch Enable
library ieee;
use ieee.std_logic_1164.all;
entity flipflop_enable is
port (Clk, Reset, D, EN : in std_logic;
Q : out std_logic);
end flipflop_enable;
entity shifter is
port ( Clk, SI : in std_logic;
SO : out std_logic);
end shifter;
end process;
A small ROM
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity rom_32_4 is
port (Clk, en : in std_logic;
addr : in unsigned(3 downto 0);
data : out unsigned(3 downto 0));
end rom_32_4;
architecture imp of rom_32_4 is
type rom_type is array (0 to 15) of unsigned(3 downto 0);
constant ROM : rom_type :=
(X"1", X"2", X"3", X"4", X"5", X"6", X"7", X"8",
X"9", X"A", X"B", X"C", X"D", X"E", X"F", X"1");
begin
process (Clk)
begin
if rising_edge(Clk) then
if en = ’1’ then data <= ROM(TO_INTEGER(addr)); end if;
end if;
end process;
Digital Design with Synthesizable VHDL – p. 5
end imp;
Variables and Signals
library ieee; use ieee.std_logic_1164.all;
entity twoshiftreg is
port(clk, si1, si2 : in std_logic; so1, so2 : out std_logic);
end twoshiftreg;
architecture imp of twoshiftreg is
signal sr1 : std_logic_vector(1 downto 0); -- visible globally
begin
process (clk)
variable sr2 : std_logic_vector(1 downto 0); -- process-only
begin
if rising_edge(clk) then
sr1(1) <= si1; -- Effect seen only after next clk
sr1(0) <= sr1(1); -- Any order works
so1 <= sr1(0);
end rtl;
Rocket Science: FSMs
Inputs Outputs
Combinational
Logic
State
Clock
Inputs
Combinational
Logic
State
Clock
Outputs
cars
This controls a traffic light at
the intersection of a busy highway
cars
and a farm road. Normally,
the highway light is green but if
a sensor detects a car on the farm
road, the highway light turns yellow then red. The
farm road light then turns green until there are no
cars or after a long timeout. Then, the farm road light
turns yellow then red, and the highway light returns to
green. The inputs to the machine are the car sensor,
a short timeout signal, and a long timeout signal. The
outputs are a timer start signal and the colors of the
highway and farm road lights.
Source: Mead and Conway, Introduction to VLSI Systems, 1980, p. 85.
Digital Design with Synthesizable VHDL – p. 6
FSM for the Traffic Light Controller
C+L S
CL/T
HG HY
C: Car sensor
S: Short timeout
S/T S/T L: Long timeout
T: Start timer
FY FG St Hwy Farm
C + L/T HG G R
HY Y R
S CL
FG R G
FY R Y
Digital Design with Synthesizable VHDL – p. 6
Traffic Light Controller in VHDL
library ieee;
use ieee.std_logic_1164.all;
entity tlc is
port (clk, reset : in std_logic;
cars, short, long : in std_logic;
highway_yellow, highway_red : out std_logic;
farm_yellow, farm_red : out std_logic;
start_timer : out std_logic);
end tlc;
end imp;
Digital Design with Synthesizable VHDL – p. 6
Summary of the Three
Modeling Styles
-- When...else selector
b <= ’1’ when x = y else
’0’;
Dout Char.
VSYNC
Din RAM Controller
HSYNC
Addr 2.5K
BLANK
Load/Shift
Font
RAM
1.5K
Video
Shift Register
Clk
CharAddr VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
i 1
− i i+1
-- OK -- Better
process (current)
begin
case current is
when START => ...
when RUN => ...
when IDLE => ...
end case;
end process;
if a = ’1’ then -- OK
foo <= ’1’; Digital Design with Synthesizable VHDL – p. 8
end if;
Pitfalls: Inferring a Latch
process (x, y)
begin
y <= ’1’; -- OK: y is always assigned
if x = ’1’ then
y <= ’0’;
end if;
end process
Digital Design with Synthesizable VHDL – p. 8
Pitfalls: Reading Output Port
library ieee;
use ieee.std_logic_1164.all;
entity dont_read_output is
port ( a : in std_logic;
x, y : out std_logic );
end dont_read_output;
architecture OK of dont_read_output is
signal x_sig : std_logic;
begin
x_sig <= not a;
x <= x_sig; -- x_sig just another name for x
y <= not x_sig; -- OK
end OK; Digital Design with Synthesizable VHDL – p. 8
Pitfalls: Complex Port Map Args
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity bad_port_map is end bad_port_map;
architecture OK of bad_port_map is
component bar port (x : in unsigned(5 downto 0) ); end component;
signal a : unsigned(3 downto 0);
signal aa : unsigned(5 downto 0);
begin
aa <= "000" & a;
mybar : bar port map ( x => aa ); -- OK
end OK; Digital Design with Synthesizable VHDL – p. 9
Pitfalls: Combinational Loops
process
begin
reset <= ’0’;
wait for 10 ns; -- Explicit delay
reset <= ’1’;
wait for a = ’1’; -- Delay for an event
assert b = ’1’ report "b did not rise" severity failure;
assert c = ’1’ report "c=0" severity warning; -- or error or note
wait for 50 ns; -- Delay for some time
wait; -- Halt this process
end process;
architecture tb of tlc_tb is
signal clk : std_logic := ’0’; -- Must initialize!