vhdl - diegm.uniud.it · vhdl vhdl linguaggio di descrizione dell'hardware – vhsic hardware...

14
VHDL VHDL Linguaggio di descrizione dell'hardware VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits Processi Attivati da qualche segnale Assegnazioni concorrenti A <= B sono eseguite contemporaneamente Variabili Solo all'interno di processi C := D Strutture di controllo (if, case,...) Case insensitive Quick reference: VHDL cookbook VHDL – schema file library <library_name>; use <library_name>.<pkg_name>.<pkg_section>; entity <component_name> is port (<port_specs>); end <component_name>; architecture <arch_ident> of <component_name> is type <type_defs> signal <signal_decl> <func_defs> <component_decls> begin <arch_specification> end <arch_ident>; definizione dell'interfaccia librerie da usare struttura o comportamento del componente VHDL - librerie library ieee; use ieee.std_logic_1164.all; library IEEE; use IEEE.std_logic_1164.all; use IEEE.numeric_std.all; library STD; use STD.textio.all; std_logic std_logic_vector librerie da usare 0, 1, Z, X, U, L, H, W, - array di std_logic (specificare il range)

Upload: vuongdung

Post on 22-Jul-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL

VHDL

� Linguaggio di descrizione dell'hardware� VHSIC Hardware Description Language

� VHSIC: Very High Speed Integrated Circuits

� Processi� Attivati da qualche segnale

� Assegnazioni concorrenti� A <= B� sono eseguite contemporaneamente

� Variabili� Solo all'interno di processi� C := D

� Strutture di controllo (if, case,...)

� Case insensitive

� Quick reference: VHDL cookbook

VHDL – schema file

library <library_name>;use <library_name>.<pkg_name>.<pkg_section>;

entity <component_name> is port (<port_specs>);end <component_name>;

architecture <arch_ident> of <component_name> is type <type_defs> signal <signal_decl> <func_defs> <component_decls>begin

<arch_specification>end <arch_ident>;

definizione dell'interfaccia

librerie da usare

struttura o comportamento

del componente

VHDL - librerie

library ieee;use ieee.std_logic_1164.all;

library IEEE;use IEEE.std_logic_1164.all;use IEEE.numeric_std.all;library STD;use STD.textio.all;

std_logic

std_logic_vector

librerie da usare

0, 1, Z, X, U, L, H, W, -

array di std_logic

(specificare il range)

Page 2: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL – definizione interfaccia

entity Dff is port (<port_specs>);end Dff;

entity counter is port (<port_specs>);end counter;

stesso identificatore

VHDL – definizione interfaccia

<port_specs>: <port_spec>; <port_spec>; ...

<port_spec>: <NAME>, <NAME>, ... : [in|out] <type>

MB : in std_logic;DA : in std_logic_vector(2 downto 0);V,C,N,Z : out std_logicDO : out std_logic_vector(31 downto 0);

entity Dff is port (CLK, RST, D : in std_logic; Q : out std_logic);end Dff;

VHDL – definizione tipi

type <name> is <typespec><typespec>: range <num1> [to|downto] <num2><typespec>: (elem1, elem2, ...)<typespec>: array (<range>) of <type>;

type signed_byte is range -128 to 127;type state_type is (A, B, C, D);type bit_word is array (15 downto 0) of bit;type bit_vector is array (natural range <>) of bit;

VHDL – definizione segnali

signal <name>, <name>, ... : <type>;

signal bus : std_logic_vector(31 downto 0);signal Z : std_logic;signal next_state : state_type;

Page 3: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL – definizione componenti

component <component_name> is port (<port_specs>);end component;

component mux32x2 port (

I0, I1 : in std_logic_vector(31 downto 0); S : in std_logic; Y : out std_logic_vector(31 downto 0));end component;

VHDL

� Descrizione comportamentale

� cosa succede quando i segnali cambiano

� process

� conditional signal assignment

� Descrizione strutturale

� quali sono i blocchi componenti

� come sono collegati tra loro

� conditional signal assignment

VHDL – process

<process_name> : process (<sensitivity_list>) <var_decls>begin

instructionsend process;

entity reg32 is port (CLK, RST, load : in std_logic; D : in std_logic_vector(31 downto 0); Q : out std_logic_vector(31 downto 0));end reg32;

architecture behav of reg32 is

process (CLK, RST)begin

if (RST = '1') then Q <= (others => '0'); elsif (CLK'event and CLK='1' and load='1') then Q <= D; end if;end process;

end;

segnali a cui il processo

è sensibile

assegnazioni concorrenti

se all'attivazione di un processo

si ha Q='1', dopo le istruzioni:

Q <= '0';

A <= Q;

si avra':

Q='0' e A='1'

VHDL –

conditional signal assignment

<signal> <= <data> when <condition> else <data> when <condition> else ... <data>

entity and2 is port (I0, I1 : in std_logic; Y0 : out std_logic);end and2;

architecture s of and2 isbegin

Y0 <= I0 when I1='1' else '0';end;

Page 4: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL –

conditional signal assignment

<signal> <= <data> when <condition> else <data> when <condition> else ... <data>

entity and2 is port (I0, I1 : in std_logic; Y0 : out std_logic);end and2;

architecture s of and2 isbegin

Y0 <= I0 when I1='1' else '0';end;

process (I0,I1)begin

if (I1='1') then Y0 <= I0; else Y0 <= '0';end process;

VHDL –

conditional signal assignment

<signal> <= <data> when <condition> else <data> when <condition> else ... <data>

entity and2 is port (I0, I1 : in std_logic; Y0 : out std_logic);end and2;

architecture s of and2 isbegin

Y0 <= I0 and I1 after 1 ns;end;

process (I0,I1)begin

Y0 <= I0 and I1 after 1 ns;end process;

VHDL

� if

if <condizione> then

sequenza di istruzioni

{ elsif <condizione> then

sequenza di istruzioni }

else

sequenza di istruzioni

end if;

VHDL

� case

case <espressione> is

{ when <scelta> =>

sequenza di istruzioni }

{ when others =>

sequenza di istruzioni }

end case;

Page 5: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL

Le istruzioni condizionali possono causare l'uso di elementi di memoria da parte dei tool di

sintesi

� condizione non gestita

� i segnali devono mantenere il valore precedente, è necessario un registro

esempio:

if A='0' thenB <= '1';

end if;

serve un registro per mantenere il valore di B nel caso A!='0'

if A='0' thenB <= '1';

elseB <= '0';

end if;

non serve un registro, si può usare una rete combinatoria

VHDL – Esempio 1

� Flip-flop D sensibile al fronte di salita del clock e con reset asincrono

library ieee;use ieee.std_logic_1164.all;

entity Dff is port (CLK, RESET, D : in std_logic; Q : out std_logic);end Dff;

architecture behav of Dff isbegin

process (CLK, RESET) begin if (RESET = '1') then Q <= '0'; elsif (CLK'event and CLK='1') then Q <= D; end if; end process;end behav;

altrimenti, nel caso di un fronte di

salita di CLK, assegnare all'uscita

il valore dell'ingresso

se RESET è alto, azzerare l'uscita

sensitivity list del processo

definizione dell'interfaccia

VHDL – Esempio 2

� Contatore a 4 bit con indicazione di raggiungimento del limite (indicazione abilitata solo

se l'ingresso EN è alto)

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

entity count4 is port (CLK, RESET, EN : in std_logic; Q : out std_logic_vector(3 downto 0); C0 : out std_logic);end count4;

architecture behav of count4 issignal count : std_logic_vector(3 downto 0);begin

process (CLK, RESET) begin if (RESET = '1') then count <= “0000”; elsif (CLK'event and CLK='1') then count <= std_logic_vector(unsigned(count) + 1); end if; end process;Q <= count;C0 <= '1' when count=”1111” and EN='1' else '0';end behav;

assegnazione uscite

sarà sintetizzata con logica

combinatoria

incremento contatore

reset del contatore

definizione interfaccia

package per gestire i numeri

VHDL – Esempio 3

Sequence recognizer

� FSM che riconosce una sequenza di bit in un

flusso seriale

� Sequenza da riconoscere: 1101

� Esempio:

� Input: 0110101011010101101101

� Output: 0000100000010000001001

Page 6: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Sequence recognizer

Diagramma degli stati

A B C D

0/0

1/0

0/0

1/0 0/0

1/0

1/1

0/0

0 1

A/0 B/0A

A/0 C/0B

D/0 C/0C

A/0 B/1D

Tabella delle transizioni

Sequence Recognizer

library ieee;use ieee.std_logic_1164.all;

entity seq_rec is port (CLK, RESET, I : in std_logic; SEQUENCE : out std_logic);end seq_rec;

definizione dell'interfaccia

Sequence Recognizer

architecture processes3 of seq_rec is

type state_type is (A, B, C, D); signal state, next_state : state_type

begin

update_state : process (CLK, RESET) begin if (RESET = '1') then state <= A; elsif (CLK'event and CLK='1') then state <= next_state; end if; end process;

processo che aggiorna lo stato ad

ogni fronte di salita del clock

dichiarazione di stato e prossimo

stato

Sequence Recognizer

compute_state : process (I, state) begin case state is when A => if I = '1' then next_state <= B; else next_state <= A; end if; when B => if I = '1' then next_state <= C; else next_state <= A; end if; when C => if I = '1' then next_state <= C; else next_state <= D; end if;

calcolo del prossimo stato, a

partire dallo stato corrente e

dall'ingresso

sarà sintetizzato con una rete

combinatoria

Page 7: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Sequence Recognizer

when D => if I = '1' then next_state <= B; else next_state <= A; end if; end case; end process;

Sequence Recognizer

compute_output : process (I, state) begin case state is when A => SEQUENCE <= '0'; when B => SEQUENCE <= '0'; when C => SEQUENCE <= '0'; when D => if I = '1' then SEQUENCE <= '1'; else SEQUENCE <= '0'; end if; end case; end process;

end processes3;

calcolo dell'uscita, a partire dallo

stato corrente e dall'ingresso

sarà sintetizzato con una rete

combinatoria

variante I

Sequence Recognizer

compute_output : process (I, state) begin if state=D and I = '1' then SEQUENCE <= '1'; else SEQUENCE <= '0'; end if; end process;

end processes3;

calcolo dell'uscita, a partire dallo

stato corrente e dall'ingresso

sarà sintetizzato con una rete

combinatoria

variante II

Sequence Recognizer

SEQUENCE <= '1' when (state=D and I='1') else '0';

end processes3; calcolo dell'uscita, a partire dallo

stato corrente e dall'ingresso

sarà sintetizzato con una rete

combinatoria

variante III

Page 8: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

VHDL – Esempio 3

Complemento a 2

� Data una stringa di ingresso seriale, che parte dal

bit meno significativo, generare la stringa seriale

che ne rappresenta il complemento a 2

� Esiste un ingresso FINE che indica la fine della

stringa di ingresso

� Esempi

� Input: 011101 Input: 1001100� Output: 011110 Output: 0110011

� Input: 11100 Input: 111001� Output: 00100 Output: 000111

Complemento a 2

Diagramma degli stati

A B

0/0

1/1

1/0

0/1

Senza il segnale FINE

Complemento a 2

Diagramma degli stati

A B

00/0

10/1

10/0

00/1

00 01

A/0 A/0A

B/1 A/1B

Tabella delle transizioni

Input:

primo bit: I

secondo bit: FINE

01/0

11/1

01/1

11/0

11 10

A/1 B/1

A/0 B/0

Complemento a 2

library ieee;use ieee.std_logic_1164.all;

entity compl2 is port (CLK, RESET, I, FINE : in std_logic; RES : out std_logic);end compl2;

definizione dell'interfaccia

Page 9: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Complemento a 2

architecture processes of seq_rec is

signal state, next_state : std_logic

-- codifica stati:-- stato A = 0-- stato B = 1

begin

update_state : process (CLK, RESET) begin if (RESET = '1') then state <= '0'; elsif (CLK'event and CLK='1') then state <= next_state; end if; end process;

dichiarazione di stato e prossimo

stato

codifica stati esplicita

processo che aggiorna lo stato

Complemento a 2

compute_state : process (I, FINE, state) begin case state is when '0' => if I = '1' and FINE = '0' then next_state <= '1'; else next_state <= '0'; end if;

when '1' => if FINE = '1' then next_state <= '0'; else next_state <= '1'; end if; end case; end process;

calcolo del prossimo stato

sarà sintetizzato con una rete

combinatoria

Complemento a 2

RES <= '1' when (state='0' and I='1') or (state='1' and I='0') else '0';

end processes;calcolo dell'uscita

sarà sintetizzato con una rete

combinatoria

VHDL – descrizione strutturale

component <component_name> is port (<port_specs>);end component;

architecture <identifier> of <device_name> is component <component_name> is port (<port_specs>); end component; ...

signal <signals>;

begin

<name> : <component_name> port map (<port_name> => <signal>, ...); ...end;

n0: NOT1 port map (in1=>SIGNAL_0, out1=>X0); SIGNAL_0X0

in1out1

Page 10: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Esempio 4 - Decoder

S0

S1

Decoder

X0

X1 Y

0

Y1

Y2

Y3

Esempio 4 - Decoder

� Descrizione comportamentale

library ieee;use ieee.std_logic_1164.all;

entity DECODER is port (S0, S1 : in std_logic; Y0, Y1, Y2, Y3 : out std_logic);end DECODER;

architecture behav of DECODER isbegin

process (S0, S1) begin if (S0 = '0' and S1 = '0') then Y0 <= '1'; Y1<='0'; Y2<='0'; Y3<='0'; elsif (S0 = '0' and S1 = '1') then Y0 <= '0'; Y1<='1'; Y2<='0'; Y3<='0'; elsif (S0 = '1' and S1 = '0') then Y0 <= '0'; Y1<='0'; Y2<='1'; Y3<='0'; else Y0 <= '0'; Y1<='0'; Y2<='0'; Y3<='1'; end if; end process;end;

Esempio 4 - Decoder

library ieee;use ieee.std_logic_1164.all;

entity DECODER is port (S0, S1 : in std_logic; Y0, Y1, Y2, Y3 : out std_logic);end DECODER;

architecture s of DECODER isbegin

Y0 <= '1' when S0='0' and S1='0' else '0'; Y1 <= '1' when S0='0' and S1='1' else '0'; Y2 <= '1' when S0='1' and S1='0' else '0'; Y3 <= '1' when S0='1' and S1='1' else '0';end;

Esempio 4 - Decoder

� Descrizione strutturale

library ieee, techlib;use ieee.std_logic_1164.all, techlib.all;

entity DECODER is port (S0, S1 : in std_logic; Y0, Y1, Y2, Y3 : out std_logic);end DECODER;

architecture struct of DECODER is component NOT1 port (in1: in std_logic; out1: out std_logic); end component;

component AND2 port (in1, in2: in std_logic; out1: out std_logic); end component;

Interfaccia dei componenti

utilizzati

Libreria che fornisce i

componenti utilizzati:

NOT1: porta not

AND2: and a 2 ingressi

Page 11: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Esempio 4 - Decoder

� Descrizione strutturale

signal X0, X1: std_logic;

begin

n0: NOT1 port map (in1=>S0, out1=>X0); n1: NOT1 port map (in1=>S1, out1=>X1); a1: AND2 port map (in1=>X0, in2=>X1, out1=>Y0); a2: AND2 port map (in1=>X0, in2=>S1, out1=>Y1); a3: AND2 port map (in1=>S0, in2=>X1, out1=>Y2); a4: AND2 port map (in1=>S0, in2=>S1, out1=>Y3);end;

Segnali interni

Descrizione componenti e

collegamenti

S0

S1

X0

X1

Y0

Y1

Y2

Y3

Esempio 4 - Decoder

� Descrizione strutturale

signal X0, X1: std_logic;

begin

n0: NOT1 port map (S0, X0); n1: NOT1 port map (S1, X1); a1: AND2 port map (X0, X1, Y0); a2: AND2 port map (X0, S1, Y1); a3: AND2 port map (S0, X1, Y2); a4: AND2 port map (S0, S1, Y3);end;

Segnali interni

Descrizione componenti e

collegamenti

i nomi delle porte si

possono omettere

(se i segnali sono indicati

nel giusto ordine)

Esempio 5 - Multiplexer

I0

I1

I2

I3

S0

S1

Decoder

AND-OR

Y

X0

X1

X3

X4

X5

X6

X7

X8

X9

X10

Esempio 5 - Multiplexer

� Descrizione comportamentale

library ieee;use ieee.std_logic_1164.all;

entity MUX is port (I0, I1, I2, I3, S0, S1 : in std_logic; Y : out std_logic);end MUX;

architecture behav of MUX isbegin

process (I0, I1, I2, I3, S0, S1) begin if (S0 = '0' and S1 = '0') then Y <= I0; elsif (S0 = '0' and S1 = '1') then Y <= I1; elsif (S0 = '1' and S1 = '0') then Y <= I2; else Y <= I3; end if; end process;end;

Page 12: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Esempio 5 - Multiplexer

� Descrizione comportamentale (2)

library ieee;use ieee.std_logic_1164.all;

entity MUX is port (I0, I1, I2, I3, S0, S1 : in std_logic; Y : out std_logic);end MUX;

architecture s of MUX isbegin

Y <= I0 when S0='0' and S1='0' else I1 when S0='0' and S1='1' else I2 when S0='1' and S1='0' else I3;end;

Esempio 5 - Multiplexer

� Descrizione strutturale

library ieee, techlib;use ieee.std_logic_1164.all, techlib.all;

entity MUX is port (I0, I1, I2, I3, S0, S1 : in std_logic; Y : out std_logic);end MUX;

architecture struct of MUX is component NOT1 port (in1: in std_logic; out1: out std_logic); end component;

component OR4 port (in1, in2, in3, in4: in std_logic; out1: out std_logic); end component;

component AND2 port (in1, in2: in std_logic; out1: out std_logic); end component;

Interfaccia dei componenti

utilizzati

� Descrizione strutturale

Libreria che fornisce i

componenti utilizzati

NOT1: porta not

AND2: and a 2 ingressi

OR 4: or a 4 ingressi

Esempio 5 - Multiplexer

� Descrizione strutturale

signal X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10: std_logic;

begin

n0: NOT1 port map (in1=>S0, out1=>X0); n1: NOT1 port map (in1=>S1, out1=>X1); a1: AND2 port map (in1=>X0, in2=>X1, out1=>X3); a2: AND2 port map (in1=>X0, in2=>S1, out1=>X4); a3: AND2 port map (in1=>S0, in2=>X1, out1=>X5); a4: AND2 port map (in1=>S0, in2=>S1, out1=>X6); a5: AND2 port map (in1=>X3, in2=>I0, out1=>X7); a6: AND2 port map (in1=>X4, in2=>I1, out1=>X8); a7: AND2 port map (in1=>X5, in2=>I2, out1=>X9); a8: AND2 port map (in1=>X6, in2=>I3, out1=>X10);

Piano AND della parte di

uscita

Decoder

Segnali interni

Esempio 5 - Multiplexer

� Descrizione strutturale

01: OR4 port map (in1=>X7, in2=>X8, in3=>X9, in4=>X10, out1=>Y);end;

OR finale

I0

I1

I2

I3

Y

X3

X4

X5

X6

X7

X8

X9

X10

Page 13: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Esempio 5 - Multiplexer

� Descrizione strutturale

01: OR4 port map (X7, X8, X9, X10, Y);end;

i nomi delle porte si

possono omettere

(se i segnali sono indicati

nel giusto ordine)

Inferenza

� Registro REG <= (others=>'0') when RESET='1' else REG_in when CLK'event and CLK='1' and load='1';

� Latch LATCH <= LATCH_in when load='1';

� Multiplexer MUX2 <= MUX2_in1 when MUX2_sel='0' else MUX2_in2;

MUX4 <= MUX4_in1 when MUX4_sel=”00” else MUX4_in2 when MUX4_sel=”01” else MUX4_in3 when MUX4_sel=”10” else MUX4_in4;

� Logica combinatoria F <= '1' when <condition> else '0';

Adder - Shifter

� Sommatore signal OPERAND1, OPERAND2 : std_logic_vector(31 downto 0); signal Cin, Cout : std_logic; signal RES_int : std_logic_vector(OPERAND1'left+2 downto 0); signal RES : std_logic_vector(OPERAND1'left downto 0); signal Cin, Cout : std_logic;

RES_int <= std_logic_vector( unsigned('0' & OPERAND1 & '1') + unsigned('0' & OPERAND2 & Cin) ); RES <= RES_int(RES_int'left-1 downto 1); Cout <= RES_int(RES_int'left);

� Shifter SHR <= '0' & SHR_in(SHR_in'left downto 1);

SHL <= SHL_in(SHL_in'left-1 downto 0) & '0';

Testbench

Testbench

Device

Under

Test

RESET

CLK

INPUTS

OUTPUTS

Page 14: VHDL - diegm.uniud.it · VHDL VHDL Linguaggio di descrizione dell'hardware – VHSIC Hardware Description Language VHSIC: Very High Speed Integrated Circuits – Processi Attivati

Testbench

Testbench

Device

Under

Test

RESET

CLK

INPUTS

OUTPUTS

Generatore

Ingressi

Generatore

Reset

Generatore

Clock

Testbench

Testbench

Device

Under

Test

RESET

CLK

INPUTS

OUTPUTS

Read file

File dati

Generatore

Reset

Generatore

Clock