topics of the lecture packages use clause aliases data alias non-data alias resolved signals

96
Topics of the Lecture Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Post on 20-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Topics of the LectureTopics of the Lecture

• Packages

• USE clause

• Aliases

• Data alias

• Non-data alias

• Resolved signals

Page 2: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages

Page 3: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages• Method for Grouping Related

Declarations Which Serve a Common Purpose– Set of subprograms to operate on

particular data type

– Set of declarations for particular model

– Separate interface from implementation

– Reusable

Page 4: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages– Unclutter rest of model

– Allows declaration of “global” signals, e.g., clocks.• Not a generally good since behavior can

change through means other than signals declared in entity interface

Page 5: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages• Design Unit Similar to Entity Declarations and

Architecture Bodies– Can be put in library and made accessible to other units– Access to items declared in the package is through using

its Selected Namelibrary_name.package_name.item_name

– Aliases can be used to allow shorter names for accessing declared items

Page 6: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages• Two Components to Packages

–Package declaration

–Package body

Page 7: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package DeclarationPackage Declaration• Subprograms Using Header, Implementation

Is Hidden– “information hiding”

• Constants, Do Not Need to Be Initialized in Declaration– “information hiding”

Page 8: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package DeclarationPackage Declaration• Types, Must Be Completely Specified

– Can have variable size arrays

• Signals Must Be Completely Specified

Page 9: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Declaration SyntaxPackage Declaration Syntax

package identifier is

{ package_declarative_item }

end [ package ] [ identifier ] ;

Page 10: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Declaration Package Declaration Example*Example*

package dp32_types is

constant unit_delay : Time := 1 ns ;

type bool_to_bit_table is

array ( boolean ) of bit ;

. . .

*Ashenden VHDL cookbook

Page 11: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Declaration Package Declaration Example*Example*

function bits_to_int

( bits : in bit_vector ) return integer ;

function bits_to_natural

( bits : in bit_vector ) return natural ;

procedure int_to_bits

( int : in integer ;

bits : out bit_vector ) ;

end dp32_types ;

*Ashenden VHDL cookbook

Page 12: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package BodyPackage Body• Not Necessary If Package Declaration Does

Not Declare Subprograms

• May Contain Additional Declarations Which Are Local to the Package Body– Cannot declare signals in body

Page 13: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package BodyPackage Body• Declared Subprograms Must Include the Full

Declaration As Used in Package Declaration– Numeric literals can be written differently if same

value

– Simple name may be replaced by a selected name provided it refers to same item

Page 14: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Body SyntaxPackage Body Syntax

package body identifier is

{ package_body_declarative_item }

end [ package body ] [ identifier ] ;

Page 15: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Body Example*Package Body Example*package body dp32_types is

constant bool_to_bit : bool_to_bit_table := ( false => '0' , true => '1' ) ;function resolve_bit_32 ( driver : in bit_32_array ) return bit_32 is

constant float_value : bit_32 := X"0000_0000" ; variable result : bit_32 := float_value ;

*Ashenden VHDL cookbook

Page 16: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Body Example*Package Body Example*

begin for i in driver'range loop result := result or driver ( i ) ; end loop ; return result ;end resolve_bit_32 ;

*Ashenden VHDL cookbook

Page 17: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Library ClauseLibrary Clause• Makes Items in a Library Available to a

VHDL Model

• To Access Items in a Library Need to Use Their selected_name

library identifier { , . . . } ;

Page 18: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Use ClauseUse Clause• Tedious to Always Use an Item’s Selected

Name

• All Items Declared in a Package or Library Can Be Made “Visible” Through a Use Clause

Page 19: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Use ClauseUse Clause• Can Be Used in Any Declarative Section

• Keyword “All” Imports All Identifiers

Page 20: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Use Clause SyntaxUse Clause Syntax

use selected_name { , . . . }

selected_name <=

name . ( identifier

| character_literal

| operator_symbol

| all )

Page 21: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Use Clause Example*Use Clause Example*

use work.dp32_types.all ;entity dp32 is generic ( Tpd : Time := unit_delay ) ; port ( d_bus : inout bus_bit_32 bus ; a_bus : out bit_32 ; read, write, fetch : out bit ; ready, phi1, phi2, reset : in bit ) ;end dp32 ;

*Ashenden VHDL cookbook

Page 22: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

A35asesA35asesAliasesAliases

Page 23: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

AliasesAliases• Alternative Identifier for an Item

• Improves Readability

• Allows One to Differentiate Among Identically Named Items in Different Packages

• Can Refer to a Single Element or Part of a Composite Data Type, e.g.,

alias interrupt_level is PSW(30 downto 26);

Page 24: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

AliasesAliases• Operations on Aliases Operate on

Actual Items Except for the Following Attributes–x’simple_name–x’path_name–x’instance_name

Page 25: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

AliasesAliases• Cannot Declare Aliases for

–Labels

–Loop parameters

–Generate parameters (replicates items)

Page 26: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Data Alias SyntaxData Alias Syntax

alias identifier

[ : subtype_indication ] is name ;

Page 27: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Data AliasData Alias• Subtype_indication Allows for the Type to Be

Changed – If scalar original

• Direction cannot change• Bounds cannot change• Unconstrained type allowed

Page 28: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Data AliasData Alias• Subtype_indication Allows for the Type to Be

Changed – If array or array slice

• Direction can differ• Bounds can differ• Base type must remain unchanged

Page 29: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Non-Data Alias SyntaxNon-Data Alias Syntax

alias ( identifier

| character_literal

| operator_symbol )

is name [ signature ] ;

Page 30: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Non-Data AliasNon-Data Alias• Alias for Enumeration Type Does Not Require

Definition of Aliases for Enumeration Literals of Original

• Alias for Physical Type Retains Units Without Redefinition

Page 31: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Non-Data Alias SyntaxNon-Data Alias Syntax• Optional Signature

– Only for subprograms and enumeration literals

– Overloading of identifiers may require means of differentiating among alternatives• return type does this

– Outer [ ] are required

Page 32: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Non-Data Alias SyntaxNon-Data Alias Syntax

signature <=

[ [ type_mark { , . . . } ] [ return type_mark ] ]– e.g.,

alias high is std.standard.’1’ [ return bit ]

Page 33: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Resolved SignalsSignals

• VHDL Requires a Function to Specify the Values Which Result From Tying Multiple Outputs Together

• Resolved Signal Includes Resolution Function– Inclusion of function indicates it is a resolved signal

Page 34: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved SignalsResolved Signals• Resolution Function Must Be Written for an

Indeterminate Number of Signals Since It Is Not Known When Declared How Many Signals Will Be Connected to It.

• The Value of a Signal at a Transaction Is Determined by the Resolution Function Operating on the Multiply Connected Signals.

Page 35: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal SyntaxResolved Signal Syntax

subtype_indication <=

[ resolution_function_name ]

type_mark [ range

( range_attribute_name

| simple_expression ( to | downto )

simple_expression)

| ( discrete_range { , . . . } ) ] ;

Page 36: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

package MVL4 is

type MVL4_ulogic is ( ‘X’, ‘0’, ‘1’, ‘Z’ );

type MVL4_ulogic_vector is array

( natural range <> ) of MVL4_ulogic ;

function resolve_MVL4

( contribution : MVL4_ulogic_vector )

return MVL4_ulogic ;

*Ashenden

Page 37: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

subtype MVL4_logic is

resolve_MVL4 MVL4_ulogic ;

end package MVL4 ;

*Ashenden

Page 38: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

package body MVL4 istype table is array

( MVL4_ulogic , MVL4_ulogic ) of MVL4_ulogic ;

Page 39: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

constant resolution_table : table := -- ‘X’ ‘0’ ‘1’ ‘Z’ -- -------------------------( ( ‘X’ ‘X’ ‘X’ ‘X’ ), -- ‘X’ ( ‘X’ ‘0’ ‘X’ ‘0’ ), -- ‘0’ ( ‘X’ ‘X’ ‘1’ ‘1’ ), -- ‘1’ ( ‘X’ ‘0’ ‘1’ ‘Z’ ) ) ;-- ‘Z’

Page 40: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

function resolve_MVL4 ( contribution : MVL4_ulogic_vector ) return MVL4_ulogic is variable result : MVL4_ulogic := ‘Z’;

Page 41: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resolved Signal Example*Resolved Signal Example*

begin for index in contribution’range loop result := resolution_table ( result, contribution ( index ) ) ; end loop ; return result ; end function resolve_MVL4 ;end package body MVL4 ;

Page 42: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

• Advanced Modeling in VHDL

• Standard Logic System

• Package Body Example

• Use of IEEE 1164 Package

Page 43: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Advanced Advanced Modelling Modelling

Techniques in Techniques in VHDLVHDL

• VHDL defines only basic features to model digital devices, such as, simple BIT type, two types of a delay, etc.

• more complex data types for signals must be definedmust be defined for realistic simulation.

Page 44: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Advanced Modelling Advanced Modelling Techniques in VHDLTechniques in VHDL

• more complex data types for signals must be defined for realistic simulation.

• Recall our old Example of Nand_2:

Page 45: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Why we need more complex Why we need more complex logic definition than BIT?logic definition than BIT?

– we want to get more accurate simulation results,– we use different technology and we want to simulate the

same design in different technologies,– real world is more complex than '0' and '1',– more complex delays than standard VHDL models

• inputs, output delays,

• rising edge, falling edge delays,

• min, max and typical delays

Page 46: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

A Case ExampleA Case Example• Std_logic_1164 defined by IEEE as a portable constructs• Requirements:

– support for more realistic signal modeling,– technology independence, allowing extensions for the future and providing support for all current

technologies,– provide consistent features which facilitate logic level modeling,– hide the complexity of the package from the designer as much as possible, make models readable,– provide for the timing accuracy continuum, giving the modeler flexibility to choose the appropriate level

of model accuracy.

• Intended use of the packages:

-- access standard logic facilities

use ieee.Std_logic_1164.all;

-- VHDL entity declaration

--

--

--

Page 47: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Standard Logic SystemStandard Logic System• modeling signals with '0' and '1' simplifies the real circuit, because it

only consider signal voltage without signals current,– many simulators introduce, so called, signal strength related to the signal's

current,

– in std_logic_1164 we will introduce the following strengths of signals:• unknown,

• forced,

• weak,

• high impedance,

• don’t care.

Page 48: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Standard Standard Logic Logic

SystemSystem

Page 49: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Standard Standard Logic Logic

SystemSystem

Page 50: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Rising and Falling EdgesRising and Falling Edges

Page 51: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Package BodyBody

Page 52: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Package Body Body

(cont’d)(cont’d)

Page 53: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Package Body Body

(cont’d)(cont’d)

Page 54: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Package Body (cont’d)Package Body (cont’d)

Page 55: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Use of IEEE 1164 PackageUse of IEEE 1164 Package

• An Example:

Page 56: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

VHDL Standard OperatorsVHDL Standard Operators

• Boolean Operators– not, and, nand, or, nor, xor, xnor

• Comparison Operators– =, /=, >=, >, <=, <

• Shifting– sll, srl, sla, sra, rol, ror

• Arithmetic– +, -, + (unary), - (unary), *, /,

mod, rem, **, abs

• Concatenation– &

for bit and bit array types

most types, but not “expected” behavior for non numeric types

for bit array types

for numeric types

for array types

Page 57: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Operator PrecedenceOperator Precedence• The following is the order of

operator precedence1. Miscellaneous: **, abs, not

2. Multiplying: *, /, mod, rem

3. Sign: +, -

4. Adding: +, -, &

5. Shifting: sll, srl, sla, sra, rol, ror

6. Relational: =, /=, <, <=, >, >=

7. Logical: and, or, nand, nor, xor, xnor

expression ::= relation {and relation} | relation {or relation} | relation {xor relation} | relation {xnor relation} | relation [nand relation] | relation [nor relation] relation ::= shift_expression [(= | /= | < | <= | > | >=) shift_expression ]

shift_expression ::= [(sll | srl | sla | sra | rol | ror) simple_expression] simple_expression ::= [+ | -] term {(+ | - | &) term}

term ::= factor {(* | / | mod | rem) factor}

factor ::= primary [** primary] | abs primary | not primary

primary ::= (expression) | literal | function_call | ...

Operator Precedence in Extended Backus Naur Form (EBNF)

Page 58: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Operator Precedence (2)Operator Precedence (2)

--ILLEGAL!, can't mix logical operatorsresult <= a and b or c; --!

--should be...result <= (a and b) or c;

--ILLEGAL!, can't chain nand or nor--(nand and nor are not associative)result <= a nand b nand c; --!result <= a nor b nor c; --!

--mayberesult <= (a nand b) nand c;

--orresult <= a nand (b nand c);

expression ::= relation {and relation} | relation {or relation} | relation {xor relation} | relation {xnor relation} | relation [nand relation] | relation [nor relation]

•Logical operators are the lowest precedence. Logical operators are evaluated after the rest of the expression (the relations).

•Logical operators have the same precedence. This is unusual in that some languages equate and with the * operator and or with the + operator.

•Since logical operators have the same precedence, it is impossible to mix logical operators. Because logical operators cannot be mixed, parentheses must used a lot.

•nand and nor cannot be chained (note the use of the [] rather than {} brackets in the EBNF syntax). This is because nand and nor are not associative.

Page 59: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Operator Precedence (3)Operator Precedence (3)--ILLEGAL! for similar reason as logicalif a < b = false then --!--should beif (a < b) = false then

--this is fineif a = '1' and b = '0' then--and is the same asif (a = '1') and (b = '0') then

--ILLEGAL! cannot chain shift operatorsresult <= a sll 2 srl 2; --!

--this is okayif a sll 2 > 5 then--is equivalent toif (a sll 2) > 5 then

--ILLEGAL, sign can’t appear in second termresult <= a + -b; --!--but this is legalresult <= -a + b;

--this...result <= -a mod b;--is the same asresult <= -(a mod b);

--ILLEGAL, not allowedresult <= a ** b ** c; --!result <= a ** (b ** c);

--ILLEGALresult <= a ** -b --!--but, strangely enough, not illegalresult <= a ** abs b;

expression ::= relation {and relation} | relation {or relation} | relation {xor relation} | relation {xnor relation} | relation [nand relation] | relation [nor relation] relation ::= shift_expression [(= | /= | < | <= | > | >=) shift_expression ]

shift_expression ::= [(sll | srl | sla | sra | rol | ror) simple_expression] simple_expression ::= [+op | -op] term {(+ | - | &) term}

term ::= factor {(* | / | mod | rem) factor}

factor ::= primary [** primary] | abs primary | not primary

primary ::= (expression) | literal | function_call | ...

Page 60: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Standard, but not VHDL std typesStandard, but not VHDL std types

• Given VHDL doesn’t even contain the types that we most frequently use, how do we determine what operators are available for :– User defined types– Types from standardized packages (e.g.

std_logic_vector..etc.)

• This issue is made more complicated by the lack of a dominant standard synthesis package– VHDL loses some benefit if code is not completely

portable among tools.

Page 61: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

PackagesPackages• Packages are used to collect together

a set of closely related sub-programs, parts, or types.

• For example, std_logic_1164 is a package that defines four types (std_ulogic, std_logic, std_ulogic_vector, std_logic_vector) as well as the operations that can be performed on them.

• A package is comprised of a package header and a package body

package test_parts is constant cycleW : integer := 3; constant addrW : integer := 22; constant dataW : integer := 16;

procedure generate_clock (Tperiod, Tpulse, Tphase : in time; signal clk : out std_logic);end package test_parts;

package body test_parts is

procedure generate_clock ( Tperiod, Tpulse, Tphase : in time; signal clk : out std_logic ) is begin wait for Tphase; loop clk <= '0', '1' after Tpulse; wait for Tperiod; end loop; end procedure generate_clock;

end package body test_parts;

library ieee;use ieee.std_logic_1164.all;

use work.test_parts.all;

. . .

use work.my_components.all;

architecture behavior of simplecircuit is signal andout1, andout2 : std_logic;begin U6: and2 port map (a1,b1,andout1); U7: and2 port map (a2,b2,andout2); sigout <= andout1 or andout2;end behavior;

Page 62: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Packages and Operator Packages and Operator OverloadingOverloading

• It is generally the case that the operations that you want to perform on your newly defined types (say std_logic_vector) are the same as the VHDL std operators.

• VHDL supports “operator overloading”

– multiple versions of the operators can be available, with the same name

– which version is “called” depends on the parameters and their types

------------------------------------------------------------------- -- overloaded logical operators -------------------------------------------------------------------

FUNCTION "and" ( l : std_ulogic; r : std_ulogic ) RETURN UX01; FUNCTION "nand" ( l : std_ulogic; r : std_ulogic ) RETURN UX01; FUNCTION "or" ( l : std_ulogic; r : std_ulogic ) RETURN UX01; FUNCTION "nor" ( l : std_ulogic; r : std_ulogic ) RETURN UX01; FUNCTION "xor" ( l : std_ulogic; r : std_ulogic ) RETURN UX01;-- function "xnor" ( l : std_ulogic; r : std_ulogic ) return ux01; function xnor ( l : std_ulogic; r : std_ulogic ) return ux01; FUNCTION "not" ( l : std_ulogic ) RETURN UX01; ------------------------------------------------------------------- -- vectorized overloaded logical operators ------------------------------------------------------------------- FUNCTION "and" ( l, r : std_logic_vector ) RETURN std_logic_vector; FUNCTION "and" ( l, r : std_ulogic_vector ) RETURN std_ulogic_vector;

FUNCTION "nand" ( l, r : std_logic_vector ) RETURN std_logic_vector; FUNCTION "nand" ( l, r : std_ulogic_vector ) RETURN std_ulogic_vector;

FUNCTION "or" ( l, r : std_logic_vector ) RETURN std_logic_vector; FUNCTION "or" ( l, r : std_ulogic_vector ) RETURN std_ulogic_vector;

FUNCTION "nor" ( l, r : std_logic_vector ) RETURN std_logic_vector; FUNCTION "nor" ( l, r : std_ulogic_vector ) RETURN std_ulogic_vector;

FUNCTION "xor" ( l, r : std_logic_vector ) RETURN std_logic_vector; FUNCTION "xor" ( l, r : std_ulogic_vector ) RETURN std_ulogic_vector;

from std_logic_1164 package header

bitwise boolean operators are overloaded for std_logic, so their behavior is now governed by the package body

Page 63: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Implementation of AND in 1164Implementation of AND in 1164• If you want to know how a function

works, check the source in the package body.

• AND on two std_logic_vectors works as we would expect, bitwise and’ing of the two vectors. Metalogical and hi-Z values are handled according to the table.

• This is the simulator model, understanding this lets you understand the behavior, not necessarily the synthesis implementation. Synthesis Implementation is typically the minimal combinatorial representation and is guaranteed by the tool vendor to match the simulation model.

CONSTANT and_table : stdlogic_table := ( -- ---------------------------------------------------- -- | U X 0 1 Z W L H - | | -- ---------------------------------------------------- ( 'U', 'U', '0', 'U', 'U', 'U', '0', 'U', 'U' ), -- | U | ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | X | ( '0', '0', '0', '0', '0', '0', '0', '0', '0' ), -- | 0 | ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', 'X' ), -- | 1 | ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | Z | ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | W | ( '0', '0', '0', '0', '0', '0', '0', '0', '0' ), -- | L | ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', 'X' ), -- | H | ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ) -- | - | );

FUNCTION "and" ( l,r : std_logic_vector ) RETURN std_logic_vector IS -- pragma built_in SYN_AND

-- pragma subpgm_id 198 --synopsys synthesis_off ALIAS lv : std_logic_vector ( 1 TO l'LENGTH ) IS l; ALIAS rv : std_logic_vector ( 1 TO r'LENGTH ) IS r; VARIABLE result : std_logic_vector ( 1 TO l'LENGTH ); --synopsys synthesis_on BEGIN --synopsys synthesis_off IF ( l'LENGTH /= r'LENGTH ) THEN ASSERT FALSE REPORT "arguments of overloaded 'and' operator are not of

the same length" SEVERITY FAILURE; ELSE FOR i IN result'RANGE LOOP result(i) := and_table (lv(i), rv(i)); END LOOP; END IF; RETURN result; --synopsys synthesis_on END "and";

from std_logic_1164 package body

Page 64: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Comparison OperatorsComparison Operators for array types for array types

• Arrays are compared from left to right, regardless of their ranges or sizes! If a bit is not found that satisfies the condition, the result is determined by the lengths.

=

<

=

<

=

<

A(2) A(1) A(0)

B(2) B(1) B(0)B(3)

'1'

Less Than

Example circuit for a < b where a’range is 2 downto 0 and b’range is 3 downto 0.

Operator a'length <a'length =a'length >b'length b'length b'length

< 1 0 0<= 1 1 0> 0 0 1

>= 0 1 1

Since std_logic_1164 only overloads logical operators, we are left with the VHDL built-in comparisons, arithmetic..etc. These don’t always behave like you expect, because now std_logic_vector is just an array of enumerated types.

Page 65: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Comparison Operators for Comparison Operators for array typesarray types

• Arrays are compared from left to right, regardless of their ranges or sizes! If a bit is not found that satisfies the condition, the result is determined by the lengths.

Operator a'length <a'length =a'length >b'length b'length b'length

< 1 0 0<= 1 1 0> 0 0 1

>= 0 1 1

Examples :

library ieee;use ieee.std_logic_1164.all;

entity test is port (a : in std_logic_vector (7 downto 0); z : in std_logic_vector (3 downto 0); altz, agtz : out std_logic);end entity test;

architecture example of test isbegin

altz <= '1' when a < z else '0';agtz <= '1' when a > z else '0';

end architecture example;

a = “00000000” z = “1111” : altz = ‘1’, agtz = ‘0’a = “1111- - - -” z = “1111” : altz = ‘0’, agtz = ‘1’a = “10111111” z = “1111” : altz = ‘1’, agtz = ‘0’a = “00011111” z = “1111” : altz = ‘1’, agtz = ‘0’

Page 66: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Numerical PackagesNumerical Packages• Problems

– comparison operators have strange behavior on our array types– no arithmetic operators available for arrays of std_logic (which we definitely need)– shifting operators not supported (87) for std_logic_vector, and these also have some non-

conventional behavior

• Solutions– Packages that define new types and overload appropriate operatorstype UNSIGNED is array (NATURAL range <>) of STD_LOGIC;type SIGNED is array (NATURAL range <>) of STD_LOGIC;– This gives us a type that represents the hardware we are building like a std_logic_vector,

while still giving us the mathematical capabilities of a integer

• There is a cost to using these packages, and that is simulation time– When using integers, the simulator can before the operation as written, when using

std_logic_arith the operation is much more complicated– For example, an integer multiplication working on 32-bit operands is a single operation,

whereas the same multiplication on 32 bit arrays takes more than a thousand operations– This is only a concern when it affects us, therefore it is rarely a concern!

std_logic_arith & numeric_std

Page 67: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

std_logic_arith & numeric_stdstd_logic_arith & numeric_std

• Since the arrays of std_logic now represent numeric values– Operators can be defined to support the arithmetic operations

(which the integer type provided us)…– The comparison operations can be overloaded to be numerical

comparisons– PLUS, we can relatively easily use the bitwise logical

operations on our numbers (unlike integers)…– …as well as slicing and concatenation (which are also not

offered by integers). These are VHDL standard array operators, and come for free.

• Note that the shift operators are not supported <check this>

Page 68: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

std_logic_arith & numeric_stdstd_logic_arith & numeric_std

Convention Note : All of the functions in the standard packages are defined such that a vector is always interpreted where the left-most bit is the MSB (regardless of array range at definition)

Reminder: although signed,unsigned,and std_logic_vector look the same, VHDL is strongly typed, so they cannot be interchanged without explicit conversion.

Page 69: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

What package to use?What package to use?• Approved IEEE package : numeric_std

• Old Defacto std : std_logic_arith– written by Synposys before there was a standard.

– still widely in use mostly due to inertia

• Since numeric_std is indeed a standard, it is guaranteed to be portable among synthesizers and simulators.

• Though std_logic_arith is fairly uniform, there are various implementations

• Recommendation : use approved IEEE package

Page 70: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Operators defined in Operators defined in numeric_stdnumeric_std

• Many operators are defined in the numeric_std package for signed and unsigned:– Comparison: =, /=, <, <=, >, >=

• return Boolean, and work exactly as you would expect, and based on the numeric value. Operators are overloaded for comparing each type with itself and with integers only.

• function ">" (L, R: UNSIGNED) return BOOLEAN;• function ">" (L, R: SIGNED) return BOOLEAN;• function ">" (L: NATURAL; R: UNSIGNED) return BOOLEAN;• function ">" (L: INTEGER; R: SIGNED) return BOOLEAN;• function ">" (L: UNSIGNED; R: NATURAL) return BOOLEAN;• function ">" (L: SIGNED; R: INTEGER) return BOOLEAN;

– Arithmetic: sign -, sign +, abs, +, -, * (sign – and abs are only defined for signed)• Again, these are overloaded for the same combinations. Note the lack of overloading for a

combination of signed and unsigned operands.

– Boolean Operators: not, and, or, nand, nor, xor• These are only supported for types signed and unsigned with themselves. (std_logic_1164

contains these for std_logic_vector)

– Limited Arithmetic Operators : /, mod, rem• not universally synthesizeable, can be synthesized in XST (Xilinx Synthesis Technology) if left

operand is a constant power of 2

Page 71: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Comparison Operators in Comparison Operators in numeric_stdnumeric_std

• Numerical values are compared regardless of size

Example :

library ieee;use ieee.numeric_std_all;use ieee.std_logic_1164.all;

entity test is port (a : in unsigned (7 downto 0); z : in unsigned (3 downto 0); altz, agtz : out std_logic);end entity test;

architecture example of test isbegin altz <= '1' when a < z else '0'; agtz <= '1' when a > z else '0';end architecture example;

a = “00000000” z = “1111” : altz = ‘1’, agtz = ‘0’a = “1111- - - -” z = “1111” : altz = ‘0’, agtz = ‘1’a = “10111111” z = “1111” : altz = ‘0’, agtz = ‘1’a = “00011111” z = “1111” : altz = ‘0’, agtz = ‘1’

Page 72: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Resize Functions (Numeric_Std)Resize Functions (Numeric_Std)

library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;

entity resizes is port (sa: in signed(7 downto 0); sb: in signed(15 downto 0); sa_large : out signed (15 downto 0); sb_small : out signed (7 downto 0); ua: in unsigned(7 downto 0); ub: in unsigned(15 downto 0); ua_large : out unsigned (15 downto 0); ub_small : out unsigned (7 downto 0) );end entity resizes;

architecture example of resizes isbegin --signed length conversions sa_large <= resize(sa, sa_large'length); sb_small <= resize(sb, sb_small'length);

--unsigned length conversions ua_large <= resize(ua, ua_large'length); ub_small <= resize(ub, ub'length);end architecture example;

-- conventional resizing could be done using slice-- sb_small <= sb(sb_small’range);-- sb_small <= sb(7 downto 0);

sign

lsbmsb

lsbmsb

function RESIZE (ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED;function RESIZE (ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED;

Page 73: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

numeric_std Arithmetic Operators (+/-)numeric_std Arithmetic Operators (+/-)

• The “+” and “-” preserve the length of the data path– This means that addition and

subtraction wrap around on overflow

– This can be avoided by resizing prior to the operation

• The “+” and “-” operators have been defined so that they can be used on arguments which are different sizes.– In the case when the arguments

are different sizes, the result is the size of the largest argument

– For example, if an 8-bit number is added to a 16-bit number, then the result is a 16-bit number

library ieee;use ieee.std_logic_arith.all;use ieee.std_logic_1164.all;

entity addition is port (a,b : in signed (7 downto 0); z : out signed (7 downto 0));end entity addition;

architecture example of addition isbegin --a + b could possibly wrap z <= a + b;end architecture addition;

library ieee;use ieee.std_logic_arith.all;use ieee.std_logic_1164.all;

entity addition is port (a,b : in signed (7 downto 0); z : out signed (8 downto 0));end entity addition;

architecture example of addition isbegin --a + b cannot wrap, but the length has --not been preserved z <= resize(a,z’length) + resize(b, z’length);end architecture addition;

Page 74: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Exa : “+” in numeric_stdExa : “+” in numeric_std• “+” resizes input operands (and takes

the strength out if it exists)• calls local function ADD_UNSIGNED,

shown below• ADD_UNSIGNED operates just like

you’d expect addition to operate, LSB to MSB, keeping track of carry between bits.

function "+" (L, R: UNSIGNED) return UNSIGNED is constant SIZE: natural := MAX(L'LENGTH, R'LENGTH); variable L01 : UNSIGNED(SIZE-1 downto 0); variable R01 : UNSIGNED(SIZE-1 downto 0); begin if ((L'LENGTH < 1) or (R'LENGTH < 1)) then return NAU; end if; L01 := TO_01(RESIZE(L, SIZE), 'X'); if (L01(L01'LEFT)='X') then return L01; end if; R01 := TO_01(RESIZE(R, SIZE), 'X'); if (R01(R01'LEFT)='X') then return R01; end if; return ADD_UNSIGNED(L01, R01, '0');

end "+";

function ADD_UNSIGNED (L, R: UNSIGNED; C: std_logic) return UNSIGNED is constant L_LEFT: integer := L'LENGTH-1; alias XL: UNSIGNED(L_LEFT downto 0) is L; alias XR: UNSIGNED(L_LEFT downto 0) is R; variable RESULT: UNSIGNED(L_LEFT downto 0); variable CBIT: std_logic := C; begin for I in 0 to L_LEFT loop RESULT(I) := CBIT xor XL(I) xor XR(I); CBIT := (CBIT and XL(I)) or (CBIT and XR(I)) or (XL(I) and XR(I)); end loop; return RESULT; end ADD_UNSIGNED;

Page 75: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Arithmetic OpsArithmetic Ops• Seeing this model emphasizes the simulation time penalty paid by using

ennumerrated array types for arithmetic.• Synthesis for this operation would simply be a standard adder

implemented optimally (hopefully) for that particular technology• “Wrapping” code not required in a counter, since as you can see, “111” +

“001” = “000”, just like a hardware adder

function ADD_UNSIGNED (L, R: UNSIGNED; C: std_logic) return UNSIGNED is constant L_LEFT: integer := L'LENGTH-1; alias XL: UNSIGNED(L_LEFT downto 0) is L; alias XR: UNSIGNED(L_LEFT downto 0) is R; variable RESULT: UNSIGNED(L_LEFT downto 0); variable CBIT: std_logic := C; begin for I in 0 to L_LEFT loop RESULT(I) := CBIT xor XL(I) xor XR(I); CBIT := (CBIT and XL(I)) or (CBIT and XR(I)) or (XL(I) and XR(I)); end loop; return RESULT; end ADD_UNSIGNED;

Page 76: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

numeric_std Arithmetic Operators (sign +/-, abs)numeric_std Arithmetic Operators (sign +/-, abs)

• As noted, the sign +, sign -, abs, +, -, and * operators are available for signed vectors, while the +, -, and * operators are available for unsigned vectors.

• The operators have been defined so that the numbers wrap on overflow

– Therefore, adding 1 to the most positive signed number returns the most negative signed number (“01..1” + ‘1’ => “11...1”)…

– …while adding 1 to the most positive unsigned number returns 0 (“11..1” + ‘1’ -> “00..0”).

• Note that taking the sign – or abs of the most negative signed number returns the most negative signed number

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity negation is port (a : in signed (7 downto 0); aneg : out signed (7 downto 0); aabs : out signed (7 downto 0));end entity negation;

architecture example of negation isbegin --if a is max neg number, then will return --the same thing! aneg <= -a; aabs <= abs a;end architecture example;

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity negation is port (a : in signed (7 downto 0); aneg : out signed (7 downto 0); aabs : out signed (8 downto 0));end entity negation;

architecture example of negation isbegin --this is a fix, but probably --not neccesary aneg <= -resize(a, 9); aabs <= abs resize(a, 9);end architecture example;

Decimal Number

Two's Complement

15 0,111114 0,111013 0,110112 0,110011 0,101110 0,10109 0,10018 0,10007 0,01116 0,01105 0,01014 0,01003 0,00112 0,00101 0,00010 0,0000-1 1,1111-2 1,1110-3 1,1101-4 1,1100-5 1,1011-6 1,1010-7 1,1001-8 1,1000-9 1,0111-10 1,0110-11 1,0101-12 1,0100-13 1,0011-14 1,0010-15 1,0001-16 1,0000

Page 77: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

numeric_std Arithmetic Operators numeric_std Arithmetic Operators (*)(*)

• The “*” operator is an exception, in that it does not preserve the data bus size

• The result size is calculated by adding the sizes of the arguments

• Therefore, multiplication can never overflow, so resizing is not necessary before performing a multiplication operation

• However, it often means that the result after the multiplication will need to be truncated by slicing or by resizing.

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity mult is port (a,b : in signed (7 downto 0); z : out signed (7 downto 0));end entity mult;

architecture example of mult is signal product : signed(15 downto 0);begin --multiply, and then resize product <= a*b; z <= resize(product, z’length);

--shorter method z <= resize(a*b, 8);end architecture example;

Page 78: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

numeric_std Arithmetic Operators (*)numeric_std Arithmetic Operators (*)library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity mult is port (a,b : in signed (7 downto 0); z : out signed (7 downto 0));end entity mult;

architecture example of mult is signal product : signed(15 downto 0);begin --multiply, and then resize product <= a*b; z <= resize(product, z’length);

--shorter method z <= resize(a*b, 8);end architecture example;

Cell Usage :# BELS : 1# GND : 1# IO Buffers : 24# IBUF : 16# OBUF : 8# Others : 1# MULT18X18 : 1

# BELS : 99# GND : 1# LUT2 : 16# LUT4 : 16# MULT_AND : 16# MUXCY : 25# XORCY : 25# IO Buffers : 24# IBUF : 16# OBUF : 8========================================

Spartan II Virtex II

Page 79: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Type Conversion Functions Type Conversion Functions (Numeric_Std)(Numeric_Std)

library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;

entity to_integer_demo is port (a : in unsigned(7 downto 0); z : out natural range 0 to 255);end entity to_integer_demo;

architecture example of to_integer_demo isbegin

z <= to_integer(a);

end architecture example;

function TO_INTEGER (ARG: SIGNED) return INTEGER;function TO_INTEGER (ARG: UNSIGNED) return NATURAL;function TO_UNSIGNED (ARG, SIZE: NATURAL) return UNSIGNED;function TO_SIGNED (ARG: INTEGER; SIZE: NATURAL) return SIGNED;

library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;

entity to_unsigned_demo is port (value : in natural range 0 to 255; result : out unsigned (7 downto 0));end entity to_unsigned_demo;

architecture behavior of to_unsigned_demo isbegin

result <= to_unsigned(value,8);

end architecture example;

-- good practice :-- result <= to_unsigned(value, result’length);

Page 80: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Type Conversion Functions Type Conversion Functions (Numeric_Std)(Numeric_Std)

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity type_demo is port (a : in std_logic_vector(7 downto 0); z : out unsigned(7 downto 0));end entity type_demo;

architecture example of type_demo isbegin

z <= unsigned(a);

end architecture example;

Remember : we automatically have closely related type conversion functions:

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity type_demo2 is port (a : in unsigned(6 downto 0); z : out signed(7 downto 0));end entity type_demo2;

architecture example of type_demo2 isbegin

z <= ‘0’ & signed(a);

end architecture example;

-- we add an extra bit to preserve numeric -- representation-- NOTHING special is done here, we are simply-- telling the compiler to interpret the bits-- in the array as a different type

Page 81: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

ConstantsConstantsconstant values of signed and unsigned (since they are array’s of std_logic) canbe represented by string values :

Assignments :

z <= “00000000”; -- MSB on left… must be exactly right lengthz <= (others => ‘0’); -- avoid this using aggregate

Bit String Literals : VHDL 87 – Available only to arrays of type bit.VHDL 93 – All character array types that contain ‘0’ and ‘1’

(e.g. std_logic, signed, unsigned…)

x <= B”0000_0000_1111”; -- B = Binary, underscores optionalx <= O”00_17”; -- O = Octalx <= X”00F”;

Page 82: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Arithmetic with ConstantsArithmetic with Constants

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity increment is port (a : in signed(7 downto 0); z : out signed(7 downto 0));end entity increment;

architecture example of increment isbegin

z <= a + “1”;

end architecture example;

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity increment is port (a : in signed(7 downto 0); z : out signed(7 downto 0));end entity increment;

architecture example of increment isbegin

z <= a + 1;

end architecture example;

mixing types by expressing the constantas an integer is often more clear

Page 83: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Example : Comparison OperatorsExample : Comparison Operators

Note the use of the to_signed function when copying the integer to signal c. Although the functions are overloaded, signal assignments still are strongly typed.

library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;

entity max is port (a : in signed (15 downto 0); b : in natural; c : out signed (15 downto 0));end entity max;

architecture example of max isbegin c <= a when (a > b) else to_signed(b, c'length);end architecture example;

library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;

entity compare is port (a : in signed (15 downto 0); negative : out std_logic);end entity compare;

architecture example of compare isbegin negative <= ‘1’ when a<0 else ‘0’;end architecture example;

Page 84: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

VHDL’93 built-in Shift VHDL’93 built-in Shift FunctionsFunctions

• a(4 downto 0) sll 2 == a(2 downto 0) & “00”

• a(4 downto 0) srl 2 == “00” & a(4 downto 2)

• a(4 downto 0) sla 2 == ?

• a(4 downto 0) sra 2 == ?

• a(4 downto 0) rol 2 == a(2 downto 0) & a(4 downto 3)

• a(4 downto 0) ror 2 == a(1 downto 0) & a(4 downto 2)

for bit vectors…

numeric_std overloads sll, srl, rol, ror for signed and unsigned typesalso available (especially in ’87 synthesizers) as shift_left(), shift_right(), rotate_left(), and rotate_right()

Page 85: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Shift Functions for numeric_stdShift Functions for numeric_std

• Two shift operations are available for std_logic_arith, sll (shift_left) and srl (shift_right).

• The sll and srl functions each take two arguments, the first begin the value, signal, or variable to shift, and the second being an unsigned value specifying how far to shift the value– Note that for synthesis the shift value must

be a constant value because the synthesis interpretation of a shift is a hardwired rearrangement of the bits of the bus

– z <= shift_left(a,4);– z <= a sll 4;

not anymore!

sll (signed and unsigned)

srl (unsigned)

srl (signed)

Page 86: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Barrel ShifterBarrel Shifterlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.numeric_std.all;

entity test is Port ( norm : in unsigned(7 downto 0); sh : out unsigned(7 downto 0); num : in natural range 0 to 5);end test;

architecture Behavioral of test is

begin

sh <= norm sll num;

end Behavioral;

Synthesizing Unit <test>. Related source file is C:\… Found 8-bit shifter logical left for signal <sh>. Summary:

inferred 1 Combinational logic shifter(s).Unit <test> synthesized.

=====================================HDL Synthesis Report

Macro Statistics# Logic shifters : 1 8-bit shifter logical left : 1

=====================================

Page 87: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Boolean OperatorsBoolean Operators• Boolean operators (not, and nand, or, nor, xor, xnor) for std_logic_vectors:

– The boolean operation is performed bit-by-bit (positionally) on the two vectors

– Each vector must have the same ‘length (but not neccesarily the same ‘range)

• In Numeric_Std supported for signed and unsigned– SHOULD be the same length

a : signed(7 downto 0);

b : signed(6 downto 0);

c : signed(15 downto 0));

c <= a and b;

10011110

1111111

1111111110011110

XST synthesis results

Simulation produces “Run-Time Error”

Page 88: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

STD_LOGIC_ARITHSTD_LOGIC_ARITH

std_logic_unsigned

std_logic_signed

Page 89: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Boolean OperatorsBoolean Operators• Boolean operators (not, and nand, or, nor, xor, xnor) for std_logic_vectors:

– The boolean operation is performed bit-by-bit (positionally) on the two vectors– Each vector must have the same ‘length (but not neccesarily the same ‘range)

• Surprisingly, the boolean operators are not supported for std_logic_arith– This can be circumvented by type converting signed or unsigned vectors to

std_logic_vectors, and then using the boolean operators available (remember that type conversion are available for closely related types.

– As an example, consider performing a masking operation on an signed value:• z <= a when signed(std_logic_vector(c) and “00001111”) /= “0” else b;

– Note that the string “00001111” is implicity assumed to be a std_logic_vector, and that the “0” does not need to be the same length as the resultant signed value because the signed comparisons are defined for different length vectors.

Page 90: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Function NameArgument

TypeSecond

Argument? Result Typeconv_integer integer no integerconv_integer signed no integerconv_integer unsigned no integerconv_integer std_ulogic no integerconv_unsigned integer yes unsignedconv_unsigned signed yes unsignedconv_unsigned unsigned yes unsignedconv_unsigned std_ulogic yes unsignedconv_signed integer yes signedconv_signed signed yes signedconv_signed unsigned yes signedconv_signed std_ulogic yes signedconv_std_logic_vector integer yes std_logic_vectorconv_std_logic_vector signed yes std_logic_vectorconv_std_logic_vector unsigned yes std_logic_vectorconv_std_logic_vector std_ulogic yes std_logic_vector

Type conversionsType conversions

Gives the size of the array to be created, must be constant for synthesis

Functions that take and return the same type are used as resize functions

Note that there are not conversion functions from unsigned./signed to std_logic_vector

No second argument because the size of the integer type is fixed

All functions in the form “conv_type” where type is the format to convert to.

Page 91: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

std_logic_arith: Resize Functionsstd_logic_arith: Resize Functionslibrary ieee;use ieee.std_logic_arith.all;use ieee.std_logic_1164.all;

entity resizes is port (sa: in signed(7 downto 0); sb: in signed(15 downto 0); sa_large : out signed (15 downto 0); sb_small : out signed (7 downto 0); ua: in unsigned(7 downto 0); ub: in unsigned(15 downto 0); ua_large : out unsigned (15 downto 0); ub_small : out unsigned (7 downto 0) );end entity resizes;

architecture example of resizes isbegin --signed length conversions sa_large <= conv_signed(sa, sa_large'length); sb_small <= conv_signed(sb, sb_small'length);

--unsigned length conversions ua_large <= conv_unsigned(ua, ua_large'length); ub_small <= conv_unsigned(ub, ub'length);

--would be the same as ua_large <= (ua’range => '0') & ua; ub_small <= ub(ub_small'range);end architecture example;

sign

lsbmsb

lsbmsb

For example:

“1000000000000001” (-32767) => “00000001” (+1)

Page 92: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Constant Values and Type Constant Values and Type AmbiguityAmbiguity

• Constant values of signed and unsigned types can be represented by string values

– Z <= “00000000”;

– Z <= X”00”;

– Z <= (others => ‘0’);

– Z <= conv_unsigned(‘0’, 8);

• When adding a signal to a constant value, there is no need to match the size of the arguments, since the functions can take operators of different lengths

• Due to the different permutations of additions possible for signed and unsigned vectors (because of the extensive overloading), type ambiguity can occur

• This is resolved by type qualification– When there is ambiguity in the model (that is there are

several different values that the analyzer could choose), type qualification informs the analyzer which to choose

– A type qualifier consists of the name of the expected type, a tick (i.e. a single quote mark), and the expression being resolved enclosed in parentheses.

library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;

entity increment is port (a : unsigned (7 downto 0); z : unsigned (7 downto 0));end;

architecture behavior of increment is --ERROR, type abiguity, the compiler --is not able to resolve this z <= a + "1"; --we can perform a type qualification --to solve the problem z <= a + unsigned'("1"); --or we can add a '1', this is --intepreted as a std_ulogic '1', --which the “+” for which the unsigned --and signed types are overloaded z <= a + '1';end architecture increment;

Page 93: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

std_logic_signed and std_logic_unsignedstd_logic_signed and std_logic_unsignedlibrary IEEE;use IEEE.std_logic_1164.all;use IEEE.std_logic_arith.all;

package STD_LOGIC_UNSIGNED is

function "+"(L: STD_LOGIC_VECTOR; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR; function "+"(L: STD_LOGIC_VECTOR; R: INTEGER) return STD_LOGIC_VECTOR; function "+"(L: INTEGER; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR; function "+"(L: STD_LOGIC_VECTOR; R: STD_LOGIC) return STD_LOGIC_VECTOR; function "+"(L: STD_LOGIC; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR;

function "-"(L: STD_LOGIC_VECTOR; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR; function "-"(L: STD_LOGIC_VECTOR; R: INTEGER) return STD_LOGIC_VECTOR; function "-"(L: INTEGER; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR; function "-"(L: STD_LOGIC_VECTOR; R: STD_LOGIC) return STD_LOGIC_VECTOR; function "-"(L: STD_LOGIC; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR; . . . function "<"(L: STD_LOGIC_VECTOR; R: STD_LOGIC_VECTOR) return BOOLEAN; function "<"(L: STD_LOGIC_VECTOR; R: INTEGER) return BOOLEAN; function "<"(L: INTEGER; R: STD_LOGIC_VECTOR) return BOOLEAN; . . . function CONV_INTEGER(ARG: STD_LOGIC_VECTOR) return INTEGER; . . .end STD_LOGIC_UNSIGNED;

package body STD_LOGIC_UNSIGNED is

function "+"(L: STD_LOGIC_VECTOR; R: STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR is -- pragma label_applies_to plus constant length: INTEGER := maximum(L'length, R'length); variable result : STD_LOGIC_VECTOR (length-1 downto 0); begin result := UNSIGNED(L) + UNSIGNED(R);-- pragma label plus return std_logic_vector(result); end; . . .

These functions define the unsigned operations for std_logic_vectors

All functions in std_logic_arith are overloaded such that they can be used on std_logic_vectors, treated as unsigned of course

The conversion functions available for closely related types are used to convert to unsigned, then the unsigned operations are used.

Note how the +, -, <, operators can be overloaded.

Page 94: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

Overview / SuggestionsOverview / Suggestions• Use Numeric_Std

• Force yourself to use some of the nice features when you are dealing with numbers, code will be much more readable and compact– mixing types– mixing sizes– using shifting operators and type conversions– represent constants in a natural fashion (I.e. decimal

integers or bitstring literals)

Page 95: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

ExamplesExamplessignal clk : std_logic;signal data_in : unsigned(15 downto 0);signal shiftreg : unsigned(15 downto 0);signal load : std_logic;

…process (clk)begin if rising_edge(clk) then if load = ‘1’ then shiftreg <= data_in; else shiftreg <= shiftreg sll 1; end if;end process;

library ieee;use ieee.numeric_std.all;use ieee.std_logic_1164.all;

entity test is port (a : in std_logic_vector (3 downto 0); dec : out std_logic_vector (15 downto 0));end entity test;

architecture example of test is

begin

process(a)begin dec <= (others => '0'); dec(to_integer(unsigned(a))) <= '1';end process;

end architecture example;

Page 96: Topics of the Lecture Packages USE clause Aliases Data alias Non-data alias Resolved signals

SourcesSources• Krzysztof Kuchcinski

• K. J. Hintz