ecse-323 digital system design - simonfoucher.comsimonfoucher.com/mcgill/ecse323 dsd/lecture...

37
1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323 Digital System Design VHDL Lecture #2

Upload: lenhu

Post on 25-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

1

McGill University ECSE-323 Digital System Design / Prof. J. Clark

ECSE-323Digital System Design

VHDL Lecture #2

Page 2: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

2

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Simple Concurrent Assignment Statements

signal <= expression;

Examples:

A_Out <= not ( A_In or B_in ) and Enable;

Data <= X nor D2 xor (flag_A and flag_B);

•The “not” operator has the highest precedence.•Operators in parentheses are evaluated first.•All binary operators have equal precedence.•Operators with the same precedence are evaluated left-to-right.

Page 3: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

3

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Other Types of Assignment Statements

The descriptions of complex circuits can be tedious to write using just simple assignment statements

So, VHDL gives additional types of signal assignment statements.

These other types of statements can also better direct synthesis programs in generating hardware

Page 4: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

4

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Forms of signal assignment statements:

• Simple Concurrent Assignment• Selected Assignment• Conditional Assignment• Component Instantiation• Generate Statements• Process Blocks

Page 5: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

5

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Selected Signal Assignment Statements

A selected signal assignment is a means of conveniently describing multiplexer structures

with Dsel select

Y <=

A when "00”,

B when "01”,

C when "10”,

D when others;

Order is notImportant!

Page 6: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

6

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Note: All conditions must be defined in a selected signal assignment, otherwise your code will not conform to the VHDL standard and you will probably get an error from whatever software happens to be reading your code.

Coverage of all conditions can be tricky to ensure – which is why the “when others” clause is useful

Page 7: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

7

McGill University ECSE-323 Digital System Design / Prof. J. Clark

An example of possible problems -with Dsel select

Y <=

A when “00”,

B when “01”,

C when “10”,

D when “11”;

This assignment statement will give an error if Dsel is of type STD_LOGIC_VECTOR (but not if Dsel is of type BIT_VECTOR).Why?Because the type of signal Dsel has more possible values than just 0 and 1 (e.g. a high-impedance value – Z) so that not all possibilities are covered.

Page 8: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

8

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Conditional Assignment Statements

Conditional signal assignment is similar to selected signal assignment. It is often used for circuits which implement some sort of priority.

Y <=

A when DSel = "00" else

B when DSel = "01" else

C when DSel = "10" else

D;

Order isImportant!

Page 9: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

9

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Conditional Assignment Statements

Unlike in selected assignment it is not absolutely required that all conditions be covered.

Y <=

A when DSel = "00" else

B when DSel = "01" else

C when DSel = "10";

Page 10: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

10

McGill University ECSE-323 Digital System Design / Prof. J. Clark

If all conditions are not accounted for in a conditional assignment statement, a storage element (e.g. a flipflop) will be created, a phenomenon known as implied memory.

Page 11: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

11

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Implied Memory

Consider the following conditional statement:Y <= A when D = ‘1’;

This statement does not say what happens whenD is 0. Therefore no events can be created on Ywhen D is 0, no matter what events occur on other signals. Thus the value of Y is held (memory) when D is 0.

Page 12: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

12

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Implied Memory

To remove the implied memory we can write:Y <= A when D = ‘1’ else

‘0’;

This describes a purely combinational circuit, with no storage elements.

Page 13: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

13

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Conditional vs. Selected AssignmentWhat is the difference between a conditional assignment and a selected assignment? From a functional point of view, not much – they both can implement the same function.From a synthesis point of view, quite a bit! – they both generate different forms of hardware.

Selected assignment ⇒ two-level, fast, high gate count and longer VHDL descriptions.Conditional assignment ⇒ multi-level, slow (worst-case), low gate count and shorter VHDL descriptions.

Page 14: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

14

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Use conditional assignments if you want to implement some sort of priority scheme.For example, priority encoders are often used to handle multipleinterrupt lines.

INT_VEC <=

VEC1 when Int1 = ‘1’ else

VEC2 when Int2 = ‘1’ else

VEC3 when Int3 = ‘1’ else

VEC4 when Int4 = ‘1’ else

‘0’;

Page 15: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

15

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Priority Interrupt Encoder - as describedwith conditional signal assignment.

INT1INT2INT3

VEC1

VEC2

VEC30

1

0

1

0

1

0

1

0

VEC4

INT_VEC

INT4

Maximum delay is 4 MUX gate delays

Page 16: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

16

McGill University ECSE-323 Digital System Design / Prof. J. Clark

The equivalent circuit description using Selected Signal assignment is longer:

with IntSel select

IntVec <=

Vec1 when “1000”, Vec1 when “1001”,

Vec1 when “1010”, Vec1 when “1011”,

Vec1 when “1100”, Vec1 when “1101”,

Vec1 when “1110”, Vec1 when “1111”,

Vec2 when “0100”, Vec2 when “0101”,

Vec2 when “0110”, Vec2 when “0111”,

Vec3 when “0010”, Vec3 when “0011”,

Vec4 when “0001”, ‘0’ when others;

Page 17: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

17

McGill University ECSE-323 Digital System Design / Prof. J. Clark

To make the selected assignment statement as short as possible, always group the output value that has the most cases (for this example it is Vec1) under the "when others" clause:

with IntSel select

IntVec <=

Vec2 when “0100”, Vec2 when “0101”,

Vec2 when “0110”, Vec2 when “0111”,

Vec3 when “0010”, Vec3 when “0011”,

Vec4 when “0001”, '0' when “0000”,

Vec1 when others;

Page 18: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

18

McGill University ECSE-323 Digital System Design / Prof. J. Clark

INT1INT2

INT3

VEC1

VEC2

INT4

Maximum delay is 1 MUX gate delays

56789101112131415

16-1 MUX

Priority Interrupt Encoder - as describedwith selected signal assignment.

0 0VEC4 1

2VEC3 3

4

INT_VEC

Page 19: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

19

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Usually, selected signal assignment resultsin faster, but larger, circuits than usingconditional signal assignment statements.

This is true only for the worst-case speed.The priority tree structures generated by conditional signal assignment have differing delays for different signal paths.

Some paths can have very short delays!

Page 20: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

20

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Conditional assignment statements are often used to reduce delay for critical path signals.

Consider the following VHDL description that uses selected signal assignment:

sel <= crit or sorta_crit or non_crit;

with sel select

Y <=in1 when ‘0’, in2 when ‘1’, in2 when others;

crit

non_crit

in2

in1

sel

0

1

Critical path has a delay of 2 gate delays

sorta_crit

Synthesis

Y

Page 21: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

21

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Re-design with conditional assignment statements:

Y <= in2 when crit = ‘1’ else in2 when sorta_crit = ‘1’ else in2 when non_crit = ‘1’ else in1;

critsorta_crit

0

1Critical path has a delay of 1 gate delay

0

1

0

1

Synthesis

A priority tree structure is synthesized

in1

in2Ynon_crit

Page 22: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

22

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Design Rule:

Put time-critical signals at the top levels of conditional assignment trees.

After circuit synthesis these signals will usually have the shortest path to the output.

Page 23: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

23

McGill University ECSE-323 Digital System Design / Prof. J. Clark

ComponentsComponents are used to connect multiple VHDL design units (entity/architecture pairs) together to form a larger, hierarchical design.

Hierarchy can dramatically simplify your design description and can make it much easier to re-use portions of the design in other projects.

Page 24: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

24

McGill University ECSE-323 Digital System Design / Prof. J. Clark

XY_Gate

QZ_Gate

XY_Gate

D1

D2

X

SET

WIDGET

I1

I2

x

y

s1

s2 x

y

s1

s2Z2

ST

Q

Z1

Gate1

Gate2

Gate3

STATUS

Q1

Q2

Example of Hierarchical Design – The design entity “WIDGET” contains instantiations of other design entities.

Page 25: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

25

McGill University ECSE-323 Digital System Design / Prof. J. Clark

A component must be declared, in the declarations section of the architecture body, before it is instantiated in the concurrent statements area.

component component_nameport list (…);

end component;

Page 26: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

26

McGill University ECSE-323 Digital System Design / Prof. J. Clark

XY_Gate

QZ_Gate

XY_Gate

D1

D2

X

SET

WIDGET

I1

I2

x

y

s1

s2 x

y

s1

s2Z2

ST

Q

Z1

Gate1

Gate2

Gate3

STATUS

Q1

Q2

There are 3 component instances, but only 2 different types of components. Thus we need 2 component declarations.

Page 27: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

27

McGill University ECSE-323 Digital System Design / Prof. J. Clark

The component declarations for our example:

component XY_gateport(X, Y: in std_logic; s1, s2: out std_logic);

end component;

component QZ_gateport(Z1, Z2 : in std_logic; ST: out std_logic;

Q : in std_logic_vector(7 downto 0));end component;

Note: The port list should match the port list of the component’s entity declaration.

Page 28: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

28

McGill University ECSE-323 Digital System Design / Prof. J. Clark

To actually use a component it must be instantiated, in the concurrent statements section of the architecture body.

The basic template for component instantiation is:

instance_label: component_nameport map (

component_port1 => signal1,component_port2 => signal2,

component_portn => signaln);

Page 29: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

29

McGill University ECSE-323 Digital System Design / Prof. J. Clark

XY_Gate

QZ_Gate

XY_Gate

D1

D2

X

SET

WIDGET

I1

I2

x

y

s1

s2 x

y

s1

s2Z2

ST

Q

Z1

Gate1

Gate2

Gate3

STATUS

Q1

Q2

There are 3 components in total of all kinds, so we need 3 instantiation statements.

Page 30: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

30

McGill University ECSE-323 Digital System Design / Prof. J. Clark

The component instantiations for our example:

Gate1: XY_Gate port map (

x=>D1, y=>D2, s2=>I1, s1=>STATUS);Gate2: XY_Gate port map (

x=>I1, s2=>Q2, y=>I2, s1=>Q1);Gate3: QZ_Gate port map (

Q=>X, Z2=>I1, Z1=>SET, ST=>I2);

Now, let’s put it all together…

Page 31: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

31

McGill University ECSE-323 Digital System Design / Prof. J. Clark

entity widget is

port ( D1, D2, SET : in std_logic; X : in std_logic_vector(7 downto 0);

Q1, Q2, STATUS : out std_logic); end widget;

architecture A of widget is signal I1, I2 : std_logic;

component XY_gate port(X, Y: in std_logic; s1, s2: out std_logic);

end component;

component QZ_gate port(Z1, Z2 : in std_logic; ST: out std_logic;Q : in std_logic_vector(7 downto 0));

end component;

beginGate1: XY_Gate port map (x=>D1, y=>D2, s2=>I1, s1=>STATUS);Gate2: XY_Gate port map (x=>I1, s2=>Q2, y=>I2, s1=>Q1);Gate3: QZ_Gate port map (Q=>X, Z2=>I1, Z1=>SET, ST=>I2);end A;

Page 32: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

32

McGill University ECSE-323 Digital System Design / Prof. J. Clark

VHDL Libraries

Libraries are collections of signal definitions, and design entity definitions (for use as components).

Always declare all used libraries before the design entity

library IEEE;use std_logic_1164.all;library lpm;use lpm.lpm.components.all;

entity my_design is. . . . . . . . . . .

Page 33: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

33

McGill University ECSE-323 Digital System Design / Prof. J. Clark

IEEE STD_LOGIC Library

This is a set of standard packages defining commonly used datatypes and operations

Std_Logic_1164 Multivalue Logic System

Defines types and operations to deal with strong, weak andhigh-impedance strengths, and unknown values

– Std_logic– Std_logic_vector

Always include this library in your VHDL designs

Page 34: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

34

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Example of std_logic package usage:

library ieee;use ieee.std_logic_1164.all;

entity nandgate isport (A, B, OE: in std_logic; Y: out

std_logic);end nandgate;

architecture arch1 of nandgate issignal n: std_logic;begin

n <= not (A and B);Y <= n when OE = ‘1' else 'Z';

end arch1;

Page 35: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

35

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Altera LPM Library

This is a set of design entities which implement various logic blocks, ranging from simple (and gates, nor gates, etc) to complex (adders, multipliers, counters).

LPM stands for Library of Parametrized Modules.

Various parameters of these modules (such as data_bus widths) can be adjusted (see the online MaxPlusII documentation for information on these parameters) using GENERICs.

You can use these as components in your VHDL designs.

Page 36: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

36

McGill University ECSE-323 Digital System Design / Prof. J. Clark

Example of lpm package usage:library ieee;

use ieee.std_logic_1164.all;

library lpm;

use lpm.lpm_components.all;

architecture arch1 of widget is

component lpm_inv generic (LPM_WIDTH: POSITIVE;

PORT (data : IN std_logic_vector(LPM_WIDTH-1 downto 0); result : OUT std_logic_vector(LPM_WIDTH-1 downto 0));

signal int1, int2 : std _logic_vector(3 downto 0));

begin

inv1 : lpm_inv generic map (LPM_WIDTH => 4)port map( result => int1, data => int2);

end arch1;

Page 37: ECSE-323 Digital System Design - simonfoucher.comsimonfoucher.com/McGill/ECSE323 DSD/Lecture Notes/4... · 1 McGill University ECSE-323 Digital System Design / Prof. J. Clark ECSE-323

37

McGill University ECSE-323 Digital System Design / Prof. J. Clark

OK, that’s all the VHDL for now.More to come next month…

In the meantime, just remember…

H is for Hardware!