Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TEMA 2.2
SENTENCIAS Y MODELOS
TIPOS DE SENTENCIAS
SECUENCIALES
CONCURRENTES
WAIT
Asignacin de variable
Asignacin de seal simple(1)
IF
CASE
NULL
LOOP
NEXT
EXIT
RETURN
Llamada a Procedimientos (1)
ASSERT (1)
(1) Estas
CONCURRENTES (I)
Asignacin Simple, Selectiva y Condicional: Van fuera de un process
Asignacin de seal simple. Ejemplo:
A <= "1010";
B <= 3;
Asignacin de seal selectiva. Ejemplo: (multiplexor)
with s select salida <=
d0 when "00",
d1 when "01",
d2 when "10",
d3 when "11";
A partir de los valores de s se asigna valor a salida
Asignacin de seal condicional. Ejemplo:
b <= "1000" when a = "00" else
0100" when a = "01" else
0010" when a = "10" else
0001" when a = "11";
CONCURRENTES (II)
Ejemplos de asignacin concurrente :
CONCURRENTES (III)
Process:
Sintaxis:
[etiqueta :] process [(lista de sensibilidad)]
Declaraciones de variables, procedimientos o tipos, pero no seales
begin
sentencias_secuenciales;
end process [etiqueta];
Una arquitectura puede tener tantos procesos como se quiera, todos ejecutndose
en paralelo
Han de tener obligatoriamente un mecanismo de detencin/reactivacin (lista de
sensibilidad o sentencia WAIT)
Las instrucciones dentro del proceso se ejecutan secuencialmente, una detrs de
otra, pero sin dar lugar a que avance el tiempo durante su ejecucin
El tiempo slo avanza cuando se llega al final del proceso o ste se suspende
CONCURRENTES (IV)
Retardo delta:
Este mecanismo se introduce para permitir la simulacin de hardware (paralelo
por naturaleza) usando mquinas secuenciales
Un retardo delta no implica actualizar o cambiar el tiempo de simulacin, pero
s que implica ejecutar un nuevo ciclo de simulacin.
Modela el retardo entre el momento en que un proceso coloca un nuevo valor
en la cola de eventos de una seal (el proceso ejecuta la asignacin sobre la
seal) y el momento en que esta seal toma el valor programado en la cola de
eventos.
Ejemplo:
ENTITY timing IS
(PORT(a,b : IN BIT; z,zbar : OUT BIT);
END ENTITY;
ARCHITECTURE delta of timing IS
BEGIN
zbar <= NOT z;
z <= a AND b AFTER 10 NS;
END delta;
o z cambia 10 ns despus de a b
o zbar cambia 10ns+ despus de a b
o slo es vlido internamente en el simulador
o Externamente z y z bar cambian a la vez,
10 ns despus de a b
CONCURRENTES (V)
Ejemplo procesos:
entity ORGATE is
port (A, B: in BIT;Z: out BIT);
end ORGATE;
architecture BEHAVE of ORGATE is
Begin
OR_FUNC: process (A,B)
begin
if (A='1' or B='1') then
Z <= '1';
else
Z <= '0';
end if;
end process OR_FUNC;
end BEHAVE;
CONCURRENTES (VI)
Ejemplo procesos:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity cnt1 is
port ( clk,rst : in std_logic;
salida : out std_logic_vector(3 downto 0));
end cnt1;
architecture B2 of cnt1 is
begin
process (clk,rst)
variable cuenta:unsigned(3 downto 0);
begin
if (rst='1') then
cuenta:= (others=> '0');
elsif (clk'event and clk='1') then
cuenta:= cuenta+1;
end if;
salida<=std_logic_vector(cuenta);
end process;
end B2;
CONCURRENTES (VII)
Declaracin e Instancia de componentes: Permiten realizar descripciones
estructurales y diseos jerrquicos
La declaracin puede aparecer en una arquitectura o en un paquete
Sintaxis de declaracin de un componente:
component nombre_componente
[generic (lista_generic);]
[port (lista_puertos);]
end component [nombre_componente];
Instancia de componentes: Es un instruccin concurrente que especifica la
interconexin de las seales del componente dentro del diseo en el que est
siendo utilizado.
Sintaxis de instancia de un componente:
etiqueta_referencia : nombre_componente
[generic map (lista_asociacin);]
[port map (lista_asociacin);]
CONCURRENTES (VIII)
Ejemplo de Declaracin e Instancia de componentes:
entity GNAND2 is
port ( A: in BIT; B: in BIT; -- A,B: in BIT;
C: out BIT );
end GNAND2;
architecture PRIMERA of GNAND2 is
begin
C <= not (A and B); -- sent. asignacion a senial
end PRIMERA;
architecture DELAY of GNAND2 is
begin
C <= A nand B after 2 NS;
end;
entity GINVER is
port (A: in BIT; B: out BIT );
end GINVER;
architecture FUNCIONAL of GINVER is
begin
B <= not A;
end FUNCIONAL;
architecture DELAY of GINVER is
begin
B <= not A after 1 NS;
end DELAY ;
CONCURRENTES (IX)
entity LATCHD is
port (D, L: in BIT; Q, QN: out BIT );
end LATCHD;
architecture ESTRUCTURAL of LATCHD is
component NAND2
port ( A, B: in BIT; C: out BIT );
end component;
component INVER
port ( A: in BIT; B: out BIT );
end component;
for U0: INVER use entity WORK.GINVER(FUNCIONAL);
for all: NAND2 use entity WORK.GNAND2(PRIMERA);
signal DN, R, S, Qi, Qni: BIT;
begin
U0: INVER port map ( D, DN );
U1: NAND2 port map ( D, L, S );
U2: NAND2 port map ( DN, L, R );
U3: NAND2 port map ( S, QNi, Qi );
U4: NAND2 port map ( R, Qi, QNi );
Q <= Qi; QN <= QNi;
end ESTRUCTURAL;
CONCURRENTES (IX)
entity LATCHD is
port (D, L: in BIT; Q, QN: out BIT );
end LATCHD;
architecture ESTRUCTURAL of LATCHD is
component NAND2
port ( A, B: in BIT; C: out BIT );
end component;
component INVER
port ( A: in BIT; B: out BIT );
end component;
for U0: INVER use entity WORK.GINVER(FUNCIONAL);
for all: NAND2 use entity WORK.GNAND2(PRIMERA);
signal DN, R, S, Qi, Qni: BIT;
S
begin
D
U0: INVER port map ( D, DN );
U1: NAND2 port map ( D, L, S );
U2: NAND2 port map ( DN, L, R );
L
U3: NAND2 port map ( S, QNi, Qi );
U4: NAND2 port map ( R, Qi, QNi );
DN
Q <= Qi; QN <= QNi;
R
end ESTRUCTURAL;
Q
Q
QN
CONCURRENTES (X)
Llamada a subprogramas (procedimientos o funciones): Dicha llamada puede
hacerse de forma secuencial o concurrente en funcin de dnde se escriba la
sentencia
Sintaxis:
nombre_procedimiento_o_funcin [{parmetros, }];
Ejemplo:
PROCEDURE lee(longitud : IN integer := 200) IS
BEGIN
....
END PROCEDURE;
-- Posible llamada
lee(350);
CONCURRENTES (XI)
ASSERT: Sentencia que comprueba si se cumple una condicin y genera un mensaje
si no se cumple
Sintaxis:
ASSERT condicion REPORT string SEVERITY nivel;
La expresin que se pone tras REPORT debe de ser de tipo string y el el mensaje que
se presenta si no se cumple la condicin
Tipos de SEVERITY: NOTE, WARNING, ERROR (por defecto si no se especifica), y
FAILURE
CONCURRENTES (XII)
GENERATE: Permite crear estructuras repetitivas y regulares. Se suele emplear para
instanciar componentes idnticos bajo el control de un ndice o de una condicin .
Sintaxis:
label: for parameter in range generate
concurrent statements
end generate label;
Ejemplo:
entity FULL_ADD4 is
port(A, B : in BIT_vector(3 downto 0); CIN : in BIT;
SUM : out BIT_vector(3 downto 0); COUT : out BIT);
end FULL_ADD4;
architecture USO_GENERATE of FULL_ADD4 is
component FULL_ADDER
port(PA, PB, PCin : in BIT; PSUM, PCOUT: out BIT);
end component;
signal CAR : BIT_vector(4 downto 0);
begin
CAR(0) <= CIN;
GK : for K in 3 downto 0 generate
FA : FULL_ADDER port map(A(K),
B(K), CAR(K), SUM(K), CAR(K+1));
end generate GK;
COUT <= CAR(4);
end USO_GENERATE;
SECUENCIALES (I)
WAIT: Es una sentencia secuencial, por lo que slo puede aparecer en el cuerpo de
un PROCESO.
Es un mecanismo de control de la ejecucin de dicho proceso (suspende y reactiva
la ejecucin del mismo )
SINTAXIS:
wait [on LISTA] [until CONDICION] [for TIEMPO];
Sin opciones, no hay condicin de reactivacin, el proceso queda suspendido
indefinidamente:
wait;
Con opcin de reactivacin simple:
wait on A; -- Evento en la seal A
wait until B; - La condicin B pasa a ser TRUE
wait for X NS; -- Transcurrido X NS de tiempo simulado
SECUENCIALES (II)
Asignacin de Variables: Las variables slo pueden declararse y utilizarse dentro
de procesos o subprogramas.
La asignacin se realiza con :=
Se puede asignar un valor inicial a la variable al realizar la declaracin
Ejemplo:
Process ()
variable Estado: bit:=0; --definicin y asignacin inicial
begin
Estado:= 1;
SECUENCIALES (III)
Diferencia entre seales y variables en asignacin de valores:
SIG: process
VAR: process
ns delta
SUMA
NV
SUMAV
+0
10
+0
10
+1
20
+0
20
+1
30
+0
30
+1
40
+0
10
40
+1
10
50
+0
15
50
+1
10
15
60
+0
10
21
60
+1
15
21
SECUENCIALES (IV)
IF: Permite la ejecucin de sentencias en funcin del cumplimiento de ciertas
condiciones.
SINTAXIS:
if ... then
S1; S2; ...; SN;
end if;
if ... then
R1; R2; ...; RN;
else
S1; S2; ...; SN;
end if;
if ... then
R1; R2; ...; RN;
elsif ... then
S1; S2; ...; SN;
elsif ... then
T1; T2; ...; TN;
else
W1; W2; ...; WN;
end if;
Est permitido el uso de IFs anidados, pero es recomendable utilizar las clausulas
ELSIF pues tienen una interpretacin ms simple.
SECUENCIALES (V)
CASE: A partir del valor de una expresin permite seleccionar una y slo una de un
conjunto de opciones posibles
SINTAXIS:
case EXPRESION is
when VALOR => -- un valor simple
S1;S2;...;SN;
when VALOR1 | VALOR2 | VALOR3 => -- un conjunto de valores
S1;S2;...;SN;
when VALOR4 to VALOR5 => -- un rango de valores
S1;S2;...;SN;
when others => -- resto de valores no cubiertos
S1;S2;...;SN; -- en la otras opciones
end case;
Las distintas opciones que aparecen deben cubrir todos los posibles valores que
puede generar la evaluacin de la expresin.
SECUENCIALES (VI)
Ejemplo sentencia CASE:
entity BCD2LED is
port ( BCD: in BIT_VECTOR(3 downto 0); LED: out BIT_VECTOR(6 downto 0);
end BCD2LED;
architecture F of BCD2LED is
begin
process (BCD)
begin
case BCD is
when "0000" => LED <= "1111110"; -- abcdefg -when "0001" => LED <= "1100000";
when "0010" => LED <= "1011011";
when "0011" => LED <= "1110011";
when "0100" => LED <= "1100101";
when "0101" => LED <= "0110111";
when "0110" => LED <= "0111111";
when "0111" => LED <= "1100010";
when "1000" => LED <= "1111111";
--completar lo que falta
end case;
end process;
end;
SECUENCIALES (VII)
Ejemplo sentencia CASE:
entity BCD2LED is
port ( BCD: in BIT_VECTOR(3 downto 0); LED: out BIT_VECTOR(6 downto 0);
end BCD2LED;
architecture F of BCD2LED is
begin
process (BCD)
begin
case BCD is
when "0000" => LED <= "1111110"; -- abcdefg -when "0001" => LED <= "1100000";
when "0010" => LED <= "1011011";
when "0011" => LED <= "1110011";
when "0100" => LED <= "1100101";
when "0101" => LED <= "0110111";
when "0110" => LED <= "0111111";
when "0111" => LED <= "1100010";
when "1000" => LED <= "1111111";
when "1001" => LED <= "1110111";
when others => LED <= "0000000";
PENSAR EL CASO HEX2LED!
end case;
end process;
end;
SECUENCIALES (VIII)
NULL: No realiza funcin alguna. Pasa la ejecucin a la siguiente sentencia
secuencial.
Sintaxis:
[etiqueta:] [FOR identificador IN rango| WHILE condicion] LOOP
secuencias secuenciales
END LOOP [etiqueta];
SECUENCIALES (IX)
LOOP: Ejemplos:
process (A)
begin
Z <= "0000";
for I in 0 to 3 loop
if (A = I) then
Z(I) <= '1';
end if;
end loop;
end process;
process (A)
variable I : integer range 0 to 4;
begin
Z <= "0000";
I := 0;
while (I <= 3) loop
if (A = I) then
Z(I) <= '1';
end if;
I := I + 1;
end loop;
end process;
SECUENCIALES (X)
NEXT: Dentro de una sentencia Loop, detiene la ejecucin de la iteracin actual y
pasa a la siguiente iteracin
Sintaxis:
[etiqueta:] NEXT [identificador bucle] [WHEN condicin];
Ejemplo:
--Interrumpe el FOR y sigue por el WHILE
termina: WHILE a< 100 LOOP
--- sentencias
sigue: FOR n IN 0 TO 100
--- sentencias
NEXT termina WHEN n=a;
END LOOP sigue;
END LOOP termina;
SECUENCIALES (XI)
EXIT: Dentro de una sentencia Loop, detiene la ejecucin en ese instante y sale del
bucle
Sintaxis:
[etiqueta:] EXIT [identificador bucle] [WHEN condicin];
Ejemplo:
for I in 0 to 7 loop
if FINISH_LOOP_EARLY = '1' then
exit;
else
A_BUS <= TABLE(I);
wait for 5 ns;
end if;
end loop;
SECUENCIALES (XII)
RETURN: Slo puede aparecer en subprogramas, y fuerza la vuelta de los mismos
al programa que lo llama.
Sintaxis:
[etiqueta:] RETURN[expresion] ;
RETURN es opcional dentro de un procedimiento y no puede contener una
expresin. Fuerza la salida del procedimiento antes de llegar al final.
Una funcin tiene que contener obligatoriamente un RETURN y una expresin
Ejemplo:
FUNCTION es_uno(din : std_logic_vector(31 downto 0)) RETURN std_logic IS
VARIABLE val : std_logic;
BEGIN
IF din = X"00000001" THEN
val := '1';
ELSE
val := '0';
END IF;
RETURN val;
END es_uno;
SUBPROGRAMAS (I)
Un subprograma es una funcin o procedimiento que realiza una determinada
tarea, y que ayudan a estructurar y reutilizar el cdigo.
Puede declararse en un paquete, o localmente en la parte declarativa de una
arquitectura, de un bloque o proceso.
Una funcin devuelve un valor y un procedimiento devuelve los valores a travs de
los parmetros que le han sido pasados como argumentos.
A consecuencia de la anterior, en una funcin todos sus parmetros son de entrada,
por el contrario en un procedimiento los parmetros pueden ser de entrada, de
salida o de entrada y salida.
Las funciones debern contener la palabra reservada RETURN, mientras que los
subprogramas no tienen necesidad de disponer dicha sentencia.
Las funciones se usan en expresiones, sin embargo, los procedimientos se llaman
como una sentencia secuencial o concurrente.
Las funciones nunca pueden tener una sentencia WAIT, pero los procedimientos s.
SUBPROGRAMAS (II)
Sintaxis:
function nombre_funcion [ (lista_parametros)] return tipo is
{declaraciones}
begin
{sentencias secuenciales}
end nombre_funcion;
SUBPROGRAMAS (III)
Los procedimientos pueden tener en el encabezado parmetros de tipo in,
out o inout
Estos parmetros pueden ser seales, variables o constantes
El tipo por defecto (si no se especifica) para parmetros de tipo in es una
constante.
La llamada en los de tipo constante de entrada se puede hacer desde un
literal, una constante, una seal o una expresin
El tipo por defecto (si no se especifica) para parmetros de tipo out e inout
es una variable
Si el de salida es una variable, el que lo llama tambin ha de serlo.
Si el de salida es una seal, el que lo llama tambin ha de serlo.
SUBPROGRAMAS (IV)
Ejemplos:
FUNCTION es_uno(din : std_logic_vector(31 downto 0)) RETURN std_logic IS
VARIABLE val : std_logic;
BEGIN
IF din = X"00000001" THEN
val := '1';
ELSE
val := '0';
END IF;
RETURN val;
END es_uno;
PROCEDURE es_uno(din : in std_logic_vector(31 downto 0); dout : out std_logic) IS
BEGIN
IF din = X"00000001" THEN
dout := '1';
ELSE
dout := '0';
END IF;
END es_uno;
SUBPROGRAMAS (V)
Ejemplos de llamadas:
PROCEDURE limite(CONSTANT conj : IN std_logic_vector(3 DOWNTO 0);
VARIABLE min, max : INOUT integer) IS ...
Llamada implcita:
limite(cjt(31 DOWNTO 28), valmin, valmax);
Llamada explcita:
limite(min=>valmin, max=>valmax, conj=>cjt(31 DOWNTO 28));
PROCEDURE lee (longitud : IN integer := 200) IS BEGIN ....
END PROCEDURE;
-- Posibles llamadas
lee;
lee(350);
variable i: INTEGER;
begin
case S is
-- completar
end case;
end process;
end mux4a1p;
OUT
INP
EA LC
ES
LC
INP
LC
OUT
EA
ES
FF
LC
FF
MEALY
MOORE
S0
X=1/Z=0
S1
X=1/Z=0
S2
X=1/Z=0
X=1/Z=1
S3
architecture F2 of TEST is
type ESTADO is (S0,S1,S2,S3);
signal EA: ESTADO;
Begin
UNICO: process
begin
wait until CLK'EVENT and CLK='1';
if RESET=1 then EA<=S0; Z<='0';
else
if X='0' then Z<='0';
else
case EA is
when S0 => EA <= S1; Z <= '0';
when S1 => EA <= S2; Z <= '0';
when S2 => EA <= S3; Z <= '0';
when S3 => EA <= S0; Z <= '1';
end case;
end if;
end if;
end process;
end F2;
S0
X=1/Z=0
S1
X=1/Z=0
S2
X=1/Z=0
X=1/Z=1
S3
architecture F1 of TEST is
type ESTADO is (S0,S1,S2,S3);
signal ES, EA: ESTADO;
Begin
SYNCH: process
begin
wait until CLK'EVENT and CLK='1';
if RESET=1 then EA <= S0; else EA <= ES; end if;
end process;
COMB: process (EA, X)
begin
case EA is
when S0 => if X='1' then ES <= S1; else ES <= S0; end if; Z <= '0';
when S1 => if X='1' then ES <= S2; else ES <= S1; end if; Z <= '0';
when S2 => if X='1' then ES <= S3; else ES <= S2; end if; Z <= '0';
when S3 => if X='1' then ES <= S0; Z <= '1'; else ES <= S3; Z <= '0'; end if;
end case;
end process;
end F1;
elsif(QUARTER_IN) then
NEXT_STATE <= IDLE;
DISPENSE <= TRUE;
NICKEL_OUT <= TRUE;
end if;
when TWENTY =>
if(NICKEL_IN) then
NEXT_STATE <= TWENTY_FIVE;
elsif(DIME_IN) then
NEXT_STATE <= THIRTY;
elsif(QUARTER_IN) then
NEXT_STATE <= IDLE;
DISPENSE <= TRUE;
DIME_OUT <= TRUE;
end if;
if(NICKEL_IN) then
NEXT_STATE <= TWENTY;
elsif(DIME_IN) then
NEXT_STATE <= TWENTY_FIVE;
elsif(QUARTER_IN) then
NEXT_STATE <= IDLE;
DISPENSE <= TRUE;
NICKEL_OUT <= TRUE;
end if;
when TWENTY =>
if(NICKEL_IN) then
NEXT_STATE <= TWENTY_FIVE;
elsif(DIME_IN) then
NEXT_STATE <= THIRTY;
elsif(QUARTER_IN) then
NEXT_STATE <= IDLE;
DISPENSE <= TRUE;
DIME_OUT <= TRUE;
end if;