ENCS 533 Advanced Digital Design: Introduction To VHDL
ENCS 533 Advanced Digital Design: Introduction To VHDL
ENCS 533 Advanced Digital Design: Introduction To VHDL
Design
Lecture 2
Introduction to VHDL
Introduction
b nandgate
c
FOR i IN ( 1 TO N ) LOOP
BEGIN
a(i) = i;
b(i) = a(i) * a(i);
END LOOP;
Semicolons
; indicates end of statement.
Statements that "open up" a block don't take
semicolons:
C:
for (i=1; i<=n; i++); /* WRONG semicolon */
{; /* ALSO WRONG semicolon */
a[i]=i;
b[i]=a[i]*a[i];
}
Semicolons
; indicates end of statement.
Statements that "open up" a block don't take
semicolons:
VHDL:
FOR i IN ( 1 TO N ) LOOP; --WRONG semicolon
BEGIN; --ALSO WRONG semicolon
a(i) = i;
b(i) = a(i) * a(i);
END LOOP;
ENTITY doesnt need a BEGIN
ENTITY nandgate IS
PORT ( a, b: IN STD_LOGIC; c: OUT STD_LOGIC );
END;
entity NANDGATE is
port ( A, B: in std_logic; C: out std_logic);
end;
entity nandgate is
port ( a, b: in std_logic; c: out std_logic);
end;
Spaces, indents and line breaks
Have no effect on code meaning
Used to enhance clarity
These are the same:
ENTITY nandgate IS
PORT ( a, b: IN STD_LOGIC; c: OUT STD_LOGIC);
END;
ENTITY nandgate IS
PORT ( a, b: IN STD_LOGIC;
c: OUT STD_LOGIC);
END;
Annotating END statements
ENTITY nandgate IS
PORT ( a, b: IN STD_LOGIC; c: OUT STD_LOGIC);
END;
ARCHITECTURE simple OF nandgate IS
BEGIN
c <= a NAND b;
END;
-- This is a comment
Libraries
void main ()
{
printf(My first C program);
}
#include <stdio.h>
void main ()
{
printf(My first C program);
}
Works OK
The IEEE library
ENTITY nandgate IS a
PORT ( a, b: IN STD_LOGIC; b nandgate
c
c: OUT STD_LOGIC);
END ENTITY nandgate;
ARCHITECTURE simple OF nandgate IS
BEGIN
c <= a NAND b;
END ARCHITECTURE simple;
LIBRARY IEEE;
USE IEEE.XXXX.YYYY
LIBRARY IEEE;
USE IEEE.XXXX.ALL
ENTITY nandgate IS
a
PORT ( a, b: IN STD_LOGIC;
b nandgate
c: OUT STD_LOGIC ); c
END ENTITY nandgate;
ENTITY question1 IS
PORT ( a: IN STD_LOGIC; b: OUT STD_LOGIC;
c: IN STD_LOGIC; d: OUT STD_LOGIC );
END ENTITY question1;
Questions
b equals
c
Conditionals
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY equals IS a
PORT ( a, b: IN STD_LOGIC;
c: OUT STD_LOGIC); b equals
c
END ENTITY equals;
Conditionals
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY equals IS a
PORT ( a, b: IN STD_LOGIC;
c: OUT STD_LOGIC); b equals
c
END ENTITY equals;
4 b1 c1
a 4 a
1
4 c
b c2
b 2
a
2
b3 c3
a
3
STD_LOGIC_VECTOR(0 TO 3)
Four members a(0), a(1), a(2) and a(3).
Each is of type STD_LOGIC.
Example of 4-bit device
b
LIBRARY ieee;
0 c0
a USE ieee.std_logic_1164.ALL;
0
b1 c1
a
1
ENTITY orgate IS
b c2 PORT
2
a
2
( a, b: IN STD_LOGIC_VECTOR(0 TO 3);
b3 c3 c: OUT STD_LOGIC_VECTOR(0 TO 3));
a
3 END ENTITY orgate;
Example of 4-bit device
b
ARCHITECTURE number1 OF orgate IS
0 c0
a
0
BEGIN
b1 c1 C(0) <= a(0) OR b(0);
a
1 C(1) <= a(1) OR b(1);
b
2
c2 C(2) <= a(2) OR b(2);
a
2
C(3) <= a(3) OR b(3);
b3 c3
a END ARCHITECTURE number1;
3
Example of 4-bit device
b
ARCHITECTURE number2 OF orgate IS
0 c0
a
0
BEGIN
b1 c1 c <= a OR b;
a
1 END ARCHITECTURE number2;
b c2
2
a
2
b3 c3
a
3
b
ARCHITECTURE number3 OF orgate IS
0 c0
a
0
BEGIN
b1 c1 C(0 TO 3) <= a(0 TO 3) OR b(0 TO 3);
a
1 END ARCHITECTURE number3;
b c2
2
a
2
b3 c3
a
3
a <= 1110;
Element 0 a: STD_LOGIC_VECTOR(3 DOWNTO 0);
Element 1
Element 2 a <= 1110;
Element 3
Element 3
Element 2
Element 1
Element 0
a <= 1110;
Element 0 a: STD_LOGIC_VECTOR(3 DOWNTO 0);
Element 1
Element 2 a <= 1110;
Element 3
Element 3
Element 2
Element 1
Element 0
Arithmetic on STD_LOGIC_VECTORs
Comparator
4
a
4 g
b
ENTITY alu IS
PORT ( a, b: IN STD_LOGIC_VECTOR(15 DOWNTO 0);
opcode: IN STD_LOGIC_VECTOR(1 DOWNTO 0);
c: OUT STD_LOGIC_VECTOR(15 DOWNTO 0) );
END ENTITY alu;
ALU example
ARCHITECTURE simple OF alu IS
BEGIN
c <= a + b WHEN opcode=00
ELSE a - b WHEN opcode=01
ELSE a OR b WHEN opcode=10
ELSE a AND b WHEN opcode=11;
END ARCHITECTURE simple;
Simulate the ALU
a = 0000000001110111, 0077H.
b = 0000000000000001, 0001H.
As opcode changes 00,01,10,11, (0,1,2,3)
output gets a+b, then a-b, then a OR b then a AND c.
Synthesise the example
It works as expected
Synthesise to gate level description
Much quicker and easier than doing it the old
fashioned way
More flexible too
Summary
Intro to VHDL
Basic examples