5 VHDL (Ii)
5 VHDL (Ii)
VHDL (II)
- What is VHDL?
• An acronym for Very High Speed Integrated Circuit Hardware Description Language.
• A program language used to describe a logic circuit by function, data flow behavior, or
structure.
- What is GHDL?
• Like Java and C, VHDL programs need to be compiled before running.
• GHDL is a compiler for VHDL.
• GHDL is short for G Hardware Design Language. Currently, G has no meaning.
- Now, let’s see how to use VHDL to describe the full adder we talked in last class.
- Now, we need a test program for the circuit so that we can tell whether the circuit works
correctly.
-- Ying Li
-- A testbench for the full adder
library ieee;
use ieee.std_logic_1164.all;
begin
-- component instantiation
-- connect the inputs and outputs of the entity to the local signals
adder1 : adder port map (A=>I0, B=>I1, Ci=>I2, Co=>O0, S=>O1);
I0 <= '0', '1' after 4 ns; -- send signals to port a, the signal is 0 in the first 4 ns
and 1 afterward
I1 <= '0', '1' after 2 ns, '0' after 4 ns, '1' after 6 ns;
I2 <= '0', '1' after 1 ns, '0' after 2 ns, '1' after 3 ns, '0' after 4 ns, '1' after 5
ns, '0' after 6 ns, '1' after 7 ns, '0' after 8 ns;
end behavior;
2
CS232 Lecture Notes 5. VHDL Fall 2021
- Open the adder.vcd file using gktwave, you will get a window like this
- Select the all the signals and click “Insert”, then click the “Zoom Fit” button. You will get a
window like this. The green signals can help us to validate the full adder.
3
CS232 Lecture Notes 5. VHDL Fall 2021
Two-Bit Adder
- Design a circuit that takes two 2-bit inputs as unsigned binary sequences, add them, and
generate a 3-bit outputs.
- We can design the circuit using gates like what we did for the full adder, but we can also
leverage the numeric_std library to simplify the process.
-- Ying Li
-- A circuit that takes two 2-bit inputs and generate a 3-bit output
library ieee;
use ieee.std_logic_1164.all;
functions
entity twoBitAdder is
-- F is an array of outputs
port (
);
end twoBitAdder;
begin
-- extend both 2-bit inputs to 3-bit inputs and then do the addition
end behavior;
4
CS232 Lecture Notes 5. VHDL Fall 2021
-- Ying Li
-- A testbench for the 2bitAdder
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
begin
-- component instantiation
-- connect the inputs and outputs of the entity to the local signals
twoBitAdder1 : twoBitAdder port map (A=>I0, B=>I1, F=>O);
process begin
for i in 1 to num_iterations loop
I1(1) <= '0';
wait for 2 ns;
I1(1) <= '1';
wait for 2 ns;
end loop;
wait;
end process;
process begin
for i in 1 to num_iterations loop
I1(0) <= '0';
wait for 1 ns;
I1(0) <= '1';
wait for 1 ns;
I1(0) <= '0';
wait for 1 ns;
I1(0) <= '1';
wait for 1 ns;
end loop;
wait;
end process;
end behavior;
5
CS232 Lecture Notes 5. VHDL Fall 2021
- We can get a gtkwave file like this to demonstrate the correctness of the circuit.
6
CS232 Lecture Notes 5. VHDL Fall 2021
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter is
port
(
clk: in std_logic;
reset: in std_logic;
enable: in std_logic;
q: out std_logic_vector (1 downto 0) -- array/vector of std_logic
);
end entity;
begin
process (clk)
begin
if reset = '1' then
-- Reset the counter to 0
cnt <= "00";
elsif (rising_edge(clk)) then
if enable = '1' then
-- Increment the counter if counting is enabled
cnt <= cnt + 1;
end if;
end if;
end process;
q <= std_logic_vector(cnt);
end behavior;
7
CS232 Lecture Notes 5. VHDL Fall 2021
-- Ying Li
-- A circuit that takes two 2-bit inputs and generate a 3-bit output
- In the twoBitAdderTest.vhd, we want to use counter. So, there are two components
in this test file: one for twoBitAdder, and the other for counter.vhd.
- We want each input of the twoBitAdder get signals from a counter, so we instantiate
two counter instances (counter1 and counter2).
- We then map the ports of the two counters to the local signals, and assign the two
counter outputs to the two inputs of the twoBitAdder.
• Please note that if the local signals have the same names as the component ports,
we can simplify the port map as counter1: counter port map (clk, reset, enable,
unsigned(q)=>I0); No => needed here.
• We use a type casting here. q is a std_logic_vector, and I0 is unsigned array,
although they have the same size. So, we type cast q to an unsigned array here to
make the assignment happen.
- Finally, we set the reset, enable, and clk signals appropriately, so that the counters
can work.
8
CS232 Lecture Notes 5. VHDL Fall 2021
-- Ying Li
-- A testbench for the 2bitAdder
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
-- counter is used to generate signals for the two inputs of the twoBitAdder
component counter
port (
clk: in std_logic;
reset: in std_logic;
enable: in std_logic;
q: out std_logic_vector (1 downto 0)
);
end component;
begin
-- component instantiation
-- connect the inputs and outputs of the entity to the local signals
twoBitAdder1 : twoBitAdder port map (A=>I0, B=>I1, F=>O);
counter1: counter port map (clk, reset, enable, unsigned(q)=>I0);
counter2: counter port map (clk, reset, enable, unsigned(q)=>I1);
-- create a clock
process begin
for i in 1 to num_cycles loop
clk <= '0';
wait for 1 ns;
clk <= '1';
wait for 1 ns;
end loop;
wait;
end process;
end behavior;
9
CS232 Lecture Notes 5. VHDL Fall 2021
10