DDCA Ch4 VHDL
DDCA Ch4 VHDL
DDCA Ch4 VHDL
Chapter 4 <1>
Chapter 4 :: Topics
Introduction
Combinational Logic
Structural Modeling
Sequential Logic
More Combinational Logic
Finite State Machines
Parameterized Modules
Testbenches
Chapter 4 <2>
Introduction
Hardware description language (HDL):
specifies logic function only
Computer-aided design (CAD) tool produces or
synthesizes the optimized gates
VHDL 2008
Developed in 1981 by the Department of Defense
IEEE standard (1076) in 1987
Updated in 2008 (IEEE STD 1076-2008)
Chapter 4 <3>
HDL to Gates
Simulation
Inputs applied to circuit
Outputs checked for correctness
Millions of dollars saved by debugging in simulation
instead of hardware
Synthesis
Transforms HDL code into a netlist describing the
hardware (i.e., a list of gates and the wires connecting
them)
IMPORTANT:
When using an HDL, think of the hardware the HDL
should produce
Chapter 4 <4>
VHDL Modules
a
b
c
VHDL
Module
Chapter 4 <5>
VHDL: example
VHDL:
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity sillyfunction is
port(a, b, c: in STD_LOGIC;
y:
out STD_LOGIC);
end;
architecture synth of sillyfunction is
begin
y <= (not a and not b and not c) or
(a and not b and not c) or
(a and not b and c);
end;
Chapter 4 <6>
HDL Simulation
VHDL:
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity sillyfunction is
port(a, b, c: in STD_LOGIC;
y:
out STD_LOGIC);
end;
architecture synth of sillyfunction is
begin
y <= (not a and not b and not c) or (a and not b and not
c) or (a and not b and c);
end;
Chapter 4 <7>
HDL Synthesis
VHDL:
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity sillyfunction is
port(a, b, c: in STD_LOGIC;
y:
out STD_LOGIC);
end;
architecture synth of sillyfunction is
begin
y <= (not a and not b and not c) or (a and not b and not
c) or (a and not b and c);
b
end;
c
un5_y
Synthesis:
un8_y
Chapter 4 <8>
VHDL Syntax
Case sensitive
Example: reset and Reset are not the same signal.
Whitespace ignored
Comments:
-- single line comment
/* multiline
comment */
Chapter 4 <9>
Bitwise Operators
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity inv is
port(a: in STD_LOGIC_VECTOR(3 downto 0);
y: out STD_LOGIC_VECTOR(3 downto 0));
end;
architecture synth of inv is
begin
y <= not a;
end;
Chapter 4 <10>
Bitwise Operators
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity gates is
port(a, b: in STD_LOGIC_VECTOR(3 downto 0);
y1, y2, y3, y4,
y5: out STD_LOGIC_VECTOR(3 downto 0));
end;
architecture synth of gates is
begin
y1 <= a and b;
y2 <= a or b;
y3 <= a xor b;
y4 <= a nand b;
y5 <= a nor b;
end;
Chapter 4 <11>
Reduction Operators
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity and8 is
port(a: in STD_LOGIC_VECTOR(7 downto 0);
y: out STD_LOGIC);
end;
architecture synth of and8 is
begin
y <= and a;
and a is much easier to write than
y <= a(7) and a(6) and a(5) and a(4) and
Chapter 4 <12>
Conditional Assignment
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity mux2 is
port(d0, d1: in STD_LOGIC_VECTOR(3 downto 0);
s:
in STD_LOGIC;
y:
out STD_LOGIC_VECTOR(3 downto 0));
end;
architecture synth of mux2 is
begin
y <= d1 when s else d0;
end;
Chapter 4 <13>
Internal Variables
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity fulladder is
port(a, b, cin: in STD_LOGIC;
s, cout:
out STD_LOGIC);
end;
architecture synth of fulladder is
signal p, g: STD_LOGIC;
begin
p <= a xor b;
g <= a and b;
s <= p xor cin;
cout <= g or (p and cin);
end;
cin
cout
a
b
un1_cout
Chapter 4 <14>
cout
Precedence
Order of operations
Highest
Lowest
Chapter 4 <15>
Numbers
Format: NBvalue
N = number of bits, B = base (B binary, D decimal, X hexadecimal)
NB is optional but recommended (default is binary)
Chapter 4 <16>
Z: Floating Output
VHDL:
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity tristate is
port(a: in STD_LOGIC_VECTOR(3 downto 0);
en: in STD_LOGIC;
y: out STD_LOGIC_VECTOR(3 downto 0));
end;
architecture synth of tristate is
begin
y <= a when en else ZZZZ;
end;
en
a[3:0]
[3:0]
[3:0]
[3:0]
[3:0]
y[3:0]
y_1[3:0]
Chapter 4 <17>
Delays
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity example is
port(a, b, c: in STD_LOGIC;
y:
out STD_LOGIC);
end;
architecture synth of example is
signal ab, bb, cb, n1, n2, n3: STD_LOGIC;
begin
ab <= not a after 1 ns;
bb <= not b after 1 ns;
cb <= not c after 1 ns;
n1 <= ab and bb and cb after 2 ns;
n2 <= a and bb and cb after 2 ns;
n3 <= a and bb and c after 2 ns;
y <= n1 or n2 or n3 after 4 ns;
end;
Sequential Logic
VHDL uses Idioms to describe latches,
flip-flops and FSMs
Other coding styles may simulate correctly
but produce incorrect hardware
Chapter 4 <19>
Behaviour Description
Behaviour description is done with the aid of:
Processes (sensitivity list)
Control structures
Functions
Process
Process Statement
General Structure:
process(sensitivity list) begin
statement;
end process;
Whenever the event in sensitivity list occurs,
statement is executed
Chapter 4 <22>
D Flip-Flop
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity flop is
port(clk: in STD_LOGIC;
d: in STD_LOGIC_VECTOR(3 downto 0);
q: out STD_LOGIC_VECTOR(3 downto 0));
end;
Chapter 4 <23>
Resettable D Flip-Flop
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity flopr is
port(clk, reset: in STD_LOGIC;
d:
in STD_LOGIC_VECTOR(3 downto 0);
q:
out STD_LOGIC_VECTOR(3 downto 0));
end;
clk
d[3:0]
reset
[3:0]
[3:0]
D[3:0]
R
Q[3:0]
[3:0]
[3:0]
q[3:0]
Chapter 4 <24>
q[3:0]
Resettable D Flip-Flop
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity flopr is
port(clk, reset: in STD_LOGIC;
d:
in STD_LOGIC_VECTOR(3 downto 0);
q:
out STD_LOGIC_VECTOR(3 downto 0));
end;
architecture asynchronous of flopr is
begin
process(clk, reset) begin
if reset then
q <= "0000";
elsif rising_edge(clk) then
q <= d;
end if;
end process;
end;
clk
d[3:0]
[3:0]
[3:0]
D[3:0]
Q[3:0]
R
reset
q[3:0]
Chapter 4 <25>
[3:0]
[3:0]
q[3:0]
Chapter 4 <26>
Multiple D Flip-Flop
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity sync is
port(clk: in STD_LOGIC;
d: in STD_LOGIC;
-- WHAT ELSE?
q: out STD_LOGIC);
end;
architecture good of sync is
signal n1: STD_LOGIC;
begin
process(clk) begin
if rising_edge(clk) then
n1 <= d;
q <= n1;
end if;
end process;
end;
Chapter 4 <27>
Latch
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity latch is
port (clk: in STD_LOGIC;
d: in STD_LOGIC_VECTOR (3 downto 0);
q: out STD_LOGIC_VECTOR (3 downto 0));
end;
architecture synth of latch is
begin
process (clk, d) begin
if clk == 1 then q <= d;
end if;
end process;
d[3:0]
clk
end;
[3:0]
[3: 0]
lat
D[3:0]
C
Q[ 3:0]
[3:0] [ 3: 0]
q[3:0]
q[3:0]
The sensitivity list contains both clk and d, so the process
evaluates anytime clk or d changes. If clk is HIGH, d flows
through to q.
Chapter 4 <28>
Chapter 4 <29>
case data is
when X"0" => segments <= "1111110";
when X"1" => segments <= "0110000";
when X"2" => segments <= "1101101";
when X"3" => segments <= "1111001";
when X"4" => segments <= "0110011";
when X"5" => segments <= "1011011";
when X"6" => segments <= "1011111";
when X"7" => segments <= "1110000";
when X"8" => segments <= "1111111";
when X"9" => segments <= "1110011";
when others => segments <= 0000000;
end case;
end process;
end;
The case statement checks the value of data. When data is 0, the statement performs the action after the
=>, setting segments to 1111110. The case statement similarly checks other data values up to 9 (note
the use of X for hexadecimal numbers). The others clause is a convenient way to define the output for
all cases not explicitly listed, guaranteeing combinational logic.
Chapter 4 <31>
Chapter 4 <32>
inputs
next
state
logic
CLK
next
k state
k
state
output
logic
Chapter 4 <33>
outputs
FSM in VHDL
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity divideby3FSM is
port(clk, reset: in STD_LOGIC;
y:
out STD_LOGIC);
end;
architecture synth of divideby3FSM is
type statetype is (S0, S1, S2);
signal state, nextstate: statetype;
begin
state register
process(clk, reset) begin
if reset then state <= S0;
elsif rising_edge(clk) then
state <= nextstate;
end if;
end process;
next state logic
nextstate <= S1 when state = S0 else
S2 when state = S1 else
S0;
output logic
y <= 1 when state = S0 else 0;
end;