Sintaxis en VHDL - Ejemplos
Sintaxis en VHDL - Ejemplos
Sintaxis en VHDL - Ejemplos
Multiplexor
1 ---------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ---------------------------------------
5 ENTITY mux IS
6 PORT ( a, b, c, d, s0, s1: IN STD_LOGIC;
7 y: OUT STD_LOGIC);
8 END mux;
9 ---------------------------------------
10 ARCHITECTURE pure_logic OF mux IS
11 BEGIN
12 y <= (a AND NOT s1 AND NOT s0) OR
13 (b AND NOT s1 AND s0) OR
14 (c AND s1 AND NOT s0) OR
15 (d AND s1 AND s0);
16 END pure_logic;
17 ---------------------------------------
Buffer Tri-estado
1 LIBRARY ieee;
2 USE ieee.std_logic_1164.all;
3 ----------------------------------------------
4 ENTITY tri_state IS
5 PORT ( ena: IN STD_LOGIC;
6 input: IN STD_LOGIC_VECTOR (7 DOWNTO 0);
7 output: OUT STD_LOGIC_VECTOR (7 DOWNTO 0));
8 END tri_state;
9 ----------------------------------------------
10 ARCHITECTURE tri_state OF tri_state IS
11 BEGIN
12 output <= input WHEN (ena='0') ELSE
13 (OTHERS => 'Z');
14 END tri_state;
15 ----------------------------------------------
2. Usando WHEN/ELSE y WITH/SELECT/WHEN:
Sintaxis:
o Con WHEN/ELSE
Asignación WHEN condición ELSE
Asignación WHEN condición ELSE
……… ;
o Con WITH/SELECT/WHEN
WITH identificador SELECT
Asignación WHEN valor,
Asignación WHEN valor,
………;
Codificador
ALU
1 ----------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 USE ieee.std_logic_unsigned.all;
5 ----------------------------------------------
6 ENTITY ALU IS
7 PORT (a, b: IN STD_LOGIC_VECTOR (7 DOWNTO 0);
8 sel: IN STD_LOGIC_VECTOR (3 DOWNTO 0);
9 cin: IN STD_LOGIC;
10 y: OUT STD_LOGIC_VECTOR (7 DOWNTO 0));
11 END ALU;
12 ----------------------------------------------
13 ARCHITECTURE dataflow OF ALU IS
14 SIGNAL arith, logic: STD_LOGIC_VECTOR (7 DOWNTO 0);
15 BEGIN
16 ----- Unidad Aritmética: ------
17 WITH sel(2 DOWNTO 0) SELECT
18 arith <= a WHEN "000",
19 a+1 WHEN "001",
20 a-1 WHEN "010",
21 b WHEN "011",
22 b+1 WHEN "100",
23 b-1 WHEN "101",
24 a+b WHEN "110",
25 a+b+cin WHEN OTHERS;
26 ----- Unidad Lógica: -----------
27 WITH sel(2 DOWNTO 0) SELECT
28 logic <= NOT a WHEN "000",
29 NOT b WHEN "001",
30 a AND b WHEN "010",
31 a OR b WHEN "011",
32 a NAND b WHEN "100",
33 a NOR b WHEN "101",
34 a XOR b WHEN "110",
35 NOT (a XOR b) WHEN OTHERS;
36 -------- Mux: ---------------
37 WITH sel(3) SELECT
38 y <= arith WHEN '0',
39 logic WHEN OTHERS;
40 END dataflow;
41 ----------------------------------------------
3. Usando GENERATE
Se usa generalmente con la forma:
FOR /GENERATE
Sintaxis:
Etiqueta: FOR identificador IN rango GENERATE
(Asignaciones concurrentes)
END GENERATE;
Vector de desplazamiento
1 ------------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ------------------------------------------------
5 ENTITY shifter IS
6 PORT (inp: IN STD_LOGIC_VECTOR (3 DOWNTO 0);
7 sel: IN INTEGER RANGE 0 TO 4;
8 outp: OUT STD_LOGIC_VECTOR (7 DOWNTO 0));
9 END shifter;
10 ------------------------------------------------
11 ARCHITECTURE shifter OF shifter IS
12 SUBTYPE vector IS STD_LOGIC_VECTOR (7 DOWNTO 0);
13 TYPE matrix IS ARRAY (4 DOWNTO 0) OF vector;
14 SIGNAL row: matrix;
15 BEGIN
16 row(0) <= "0000" & inp;
17 G1: FOR i IN 1 TO 4 GENERATE
18 row(i) <= row(i-1)(6 DOWNTO 0) & '0';
19 END GENERATE;
20 outp <= row(sel);
21 END shifter;
22 ------------------------------------------------
4. Usando BLOCK:
Ejemplo:
b1: BLOCK
SIGNAL a: STD_LOGIC;
BEGIN
a <= input_sig WHEN ena='1' ELSE 'Z';
END BLOCK b1;
Ejemplo: LATCH
1 -------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 -------------------------------
5 ENTITY latch IS
6 PORT (d, clk: IN STD_LOGIC;
7 q: OUT STD_LOGIC);
8 END latch;
9 -------------------------------
10 ARCHITECTURE latch OF latch IS
11 BEGIN
12 b1: BLOCK (clk='1')
13 BEGIN
14 q <= GUARDED d;
15 END BLOCK b1;
16 END latch;
17 -------------------------------
Ejemplo: FFD
1 -------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 -------------------------------
5 ENTITY dff IS
6 PORT (d, clk, rst: IN STD_LOGIC;
7 q: OUT STD_LOGIC);
8 END dff;
9 -------------------------------
10 ARCHITECTURE dff OF dff IS
11 BEGIN
12 b1: BLOCK (clk'EVENT AND clk='1')
13 BEGIN
14 q <= GUARDED '0' WHEN rst='1' ELSE d;
15 END BLOCK b1;
16 END dff;
17 ------------------------------
PROBLEMAS PROPUESTOS
PROCESSES
FUNCTIONS IF, WAIT, CASE, y LOOP.
PROCEDURES. VARIABLE
Sintaxis
[Etiqueta:] PROCESS (lista sensible)
[VARIABLE nombre: tipo [rango] [:= valor inicial;]]
BEGIN
(Código secuencial)
END PROCESS [etiqueta];
Ejemplo: FFD con reset asincrónico
1 --------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 --------------------------------------
5 ENTITY dff IS
6 PORT (d, clk, rst: IN STD_LOGIC;
7 q: OUT STD_LOGIC);
8 END dff;
9 --------------------------------------
10 ARCHITECTURE comportamiento OF dff IS
11 BEGIN
12 PROCESS (clk, rst)
13 BEGIN
14 IF (rst='1') THEN
15 q <= '0';
16 ELSIF (clk'EVENT AND clk='1') THEN
17 q <= d;
18 END IF;
19 END PROCESS;
20 END comportamiento;
21 --------------------------------------
Código con IF
Sintaxis:
Ejemplo:
IF (x<y) THEN temp:="11111111";
ELSIF (x=y AND w='0') THEN temp:="11110000";
ELSE temp:=(OTHERS =>'0');
Ejemplo contador de 0 a 9:
1 ---------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ---------------------------------------------
5 ENTITY contador IS
6 PORT (clk: IN STD_LOGIC;
7 digit: OUT INTEGER RANGE 0 TO 9);
8 END contador;
9 ---------------------------------------------
10 ARCHITECTURE contador OF contador IS
11 BEGIN
12 cont: PROCESS(clk)
13 VARIABLE temp: INTEGER RANGE 0 TO 10;
14 BEGIN
15 IF (clk'EVENT AND clk='1') THEN
16 temp:= temp + 1;
17 IF (temp=10) THEN temp:= 0;
18 END IF;
19 END IF;
20 digit <= temp;
21 END PROCESS cont;
22 END contador;
23 ---------------------------------------------
1 --------------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 --------------------------------------------------
5 ENTITY shiftreg IS
6 GENERIC (n: INTEGER:= 4); -- # de etapas
7 PORT (d, clk, rst: IN STD_LOGIC;
8 q: OUT STD_LOGIC);
9 END shiftreg;
10 --------------------------------------------------
11 ARCHITECTURE behavior OF shiftreg IS
12 SIGNAL internal: STD_LOGIC_VECTOR (n-1 DOWNTO 0);
13 BEGIN
14 PROCESS (clk, rst)
15 BEGIN
16 IF (rst='1') THEN
17 internal <= (OTHERS => '0');
18 ELSIF (clk'EVENT AND clk='1') THEN
19 internal <= d & internal (internal'LEFT DOWNTO 1);
20 END IF;
21 END PROCESS;
22 q <= internal(0);
23 END behavior;
24 --------------------------------------------------
Sintaxis:
Ejemplo
1 ---------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ---------------------------------------------
5 ENTITY counter IS
6 PORT (clk : IN STD_LOGIC;
7 digit : OUT INTEGER RANGE 0 TO 9);
8 END counter;
9 ---------------------------------------------
10 ARCHITECTURE counter OF counter IS
11 BEGIN
12 PROCESS -- no sensitivity list
13 VARIABLE temp : INTEGER RANGE 0 TO 10;
14 BEGIN
15 WAIT UNTIL (clk'EVENT AND clk='1');
16 temp := temp + 1;
17 IF (temp=10) THEN temp := 0;
18 END IF;
19 digit <= temp;
20 END PROCESS;
21 END counter;
22 ---------------------------------------------
Ejemplo
PROCESS
BEGIN
WAIT ON clk, rst;
IF (rst='1') THEN
Output <= "00000000";
ELSIF (clk'EVENT AND clk='1') THEN
Output <= input;
END IF;
END PROCESS;
1 --------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 --------------------------------------
5 ENTITY dff IS
6 PORT (d, clk, rst: IN STD_LOGIC;
7 q: OUT STD_LOGIC);
8 END dff;
9 --------------------------------------
10 ARCHITECTURE dff OF dff IS
11 BEGIN
12 PROCESS
13 BEGIN
14 WAIT ON rst, clk;
15 IF (rst='1') THEN
16 q <= '0';
17 ELSIF (clk'EVENT AND clk='1') THEN
18 q <= d;
19 END IF;
20 END PROCESS;
21 END dff;
22 --------------------------------------
Ejemplo:
CASE control IS
WHEN "00" => x<=a; y<=b;
WHEN "01" => x<=b; y<=c;
WHEN OTHERS => x<="0000"; y<="ZZZZ";
END CASE;
1 ----------------------------------------------
2 LIBRARY ieee; -- Declaración innecesaria,
3 -- por que
4 USE ieee.std_logic_1164.all; -- BIT se usa dentro de
5 -- STD_LOGIC
6 ----------------------------------------------
7 ENTITY dff IS
8 PORT (d, clk, rst: IN BIT;
9 q: OUT BIT);
10 END dff;
11 ----------------------------------------------
12 ARCHITECTURE dff3 OF dff IS
13 BEGIN
14 PROCESS (clk, rst)
15 BEGIN
16 CASE rst IS
17 WHEN '1' => q<='0';
18 WHEN '0' =>
19 IF (clk'EVENT AND clk='1') THEN
20 q <= d;
21 END IF;
22 WHEN OTHERS => NULL; -- innecesario, rst es de tipo
23 -- BIT
24 END CASE;
25 END PROCESS;
26 END dff3;
27 ----------------------------------------------
Ejemplo
FOR i IN 0 TO 5 LOOP
x(i) <= enable AND w(i+2);
y(0, i) <= w(i);
END LOOP;
WHILE / LOOP:
[Etiqueta:] WHILE condición LOOP
(Instrucciones secuenciales)
END LOOP [etiqueta];
Ejemplo
Ejemplo
Ejemplo
FOR i IN 0 TO 15 LOOP
NEXT WHEN i=skip; -- salta a la siguiente iteración
(...)
END LOOP;
1 ---------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ---------------------------------------------
5 ENTITY barrel IS
6 GENERIC (n: INTEGER:= 8);
7 PORT (inp: IN STD_LOGIC_VECTOR (n-1 DOWNTO 0);
8 shift: IN INTEGER RANGE 0 TO 1;
9 outp: OUT STD_LOGIC_VECTOR (n-1 DOWNTO 0));
10 END barrel;
11 ---------------------------------------------
12 ARCHITECTURE RTL OF barrel IS
13 BEGIN
14 PROCESS (inp, shift)
15 BEGIN
16 IF (shift=0) THEN
17 outp <= inp;
18 ELSE
19 outp(0) <= '0';
20 FOR i IN 1 TO inp'HIGH LOOP
21 outp(i) <= inp(i-1);
22 END LOOP;
23 END IF;
24 END PROCESS;
25 END RTL;
26 ---------------------------------------------
1 --------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 --------------------------------------------
5 ENTITY LeadingZeros IS
6 PORT ( data: IN STD_LOGIC_VECTOR (7 DOWNTO 0);
7 zeros: OUT INTEGER RANGE 0 TO 8);
8 END LeadingZeros;
9 --------------------------------------------
10 ARCHITECTURE behavior OF LeadingZeros IS
11 BEGIN
12 PROCESS (data)
13 VARIABLE count: INTEGER RANGE 0 TO 8;
14 BEGIN
15 count := 0;
16 FOR i IN data'RANGE LOOP
17 CASE data(i) IS
18 WHEN '0' => count := count + 1;
19 WHEN OTHERS => EXIT;
20 END CASE;
21 END LOOP;
22 zeros <= count;
23 END PROCESS;
24 END behavior;
1 ---------------------------------------------------
2 LIBRARY ieee;
3 USE ieee.std_logic_1164.all;
4 ---------------------------------------------------
5 ENTITY ram IS
6 GENERIC ( bits: INTEGER := 8; -- # of bits per word
7 words: INTEGER := 16); -- # of words in the memory
8 PORT ( wr_ena, clk: IN STD_LOGIC;
9 addr: IN INTEGER RANGE 0 TO words-1;
10 data_in: IN STD_LOGIC_VECTOR (bits-1 DOWNTO 0);
11 data_out: OUT STD_LOGIC_VECTOR (bits-1 DOWNTO 0));
12 END ram;
13 ---------------------------------------------------
14 ARCHITECTURE ram OF ram IS
15 TYPE vector_array IS ARRAY (0 TO words-1) OF
16 STD_LOGIC_VECTOR (bits-1 DOWNTO 0);
17 SIGNAL memory: vector_array;
18 BEGIN
19 PROCESS (clk, wr_ena)
20 BEGIN
21 IF (wr_ena='1') THEN
22 IF (clk'EVENT AND clk='1') THEN
23 memory(addr) <= data_in;
24 END IF;
25 END IF;
26 END PROCESS;
27 data_out <= memory(addr);
28 END ram;
29 ---------------------------------------------------
PROBLEMAS PROPUESTOS