💎 Travaux Pratiques VHDL
6 TPs Complets - De la Logique Combinatoire aux Machines a Etats
1
Portes Logiques et Logique Combinatoire
Introduction a VHDL - Premiers circuits
Objectifs
Maitriser la structure de base VHDL (entity/architecture), implementer des portes logiques, comprendre les affectations concurrentes.
Partie 1 : Portes Logiques de Base
Exercice 1.1 : Porte AND 2 entrees
Creer une entity porte_and avec deux entrees A, B et une sortie Y.
library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity porte_and is port ( A, B : in std_logic; Y : out std_logic ); end entity; architecture dataflow of porte_and is begin Y <= A and B; end architecture;
📝 A faire
- Implementer les portes OR, NAND, NOR, XOR, XNOR
- Creer une porte AND a 4 entrees
- Ecrire un testbench pour verifier chaque porte
Partie 2 : Multiplexeur
Exercice 1.2 : Multiplexeur 2:1
entity mux2to1 is port ( A, B : in std_logic; sel : in std_logic; Y : out std_logic ); end entity; architecture behavioral of mux2to1 is begin Y <= A when sel = '0' else B; end architecture;
📝 A faire
- Implementer un MUX 4:1 avec
with...select - Implementer un MUX 8:1 parametrable (generic)
- Creer un demultiplexeur 1:4
Partie 3 : Decodeur 7 Segments
entity dec7seg is port ( digit : in std_logic_vector(3 downto 0); segments : out std_logic_vector(6 downto 0) -- abcdefg ); end entity; architecture behavioral of dec7seg is begin with digit select segments <= "1111110" when "0000", -- 0 "0110000" when "0001", -- 1 "1101101" when "0010", -- 2 "1111001" when "0011", -- 3 "0110011" when "0100", -- 4 "1011011" when "0101", -- 5 "1011111" when "0110", -- 6 "1110000" when "0111", -- 7 "1111111" when "1000", -- 8 "1111011" when "1001", -- 9 "0000000" when others; end architecture;
2
Arithmetique et Operations
Additionneurs, soustracteurs, comparateurs
Partie 1 : Additionneur Complet
entity full_adder is port ( A, B, Cin : in std_logic; Sum, Cout : out std_logic ); end entity; architecture behavioral of full_adder is begin Sum <= A xor B xor Cin; Cout <= (A and B) or (Cin and (A xor B)); end architecture;
📝 A faire
- Creer un additionneur 4 bits en cascadant des full_adder
- Implementer un additionneur 8 bits avec NUMERIC_STD
- Ajouter la detection d'overflow
Partie 2 : ALU Simple
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; entity alu_8bits is port ( A, B : in std_logic_vector(7 downto 0); op : in std_logic_vector(2 downto 0); result : out std_logic_vector(7 downto 0); zero : out std_logic ); end entity; architecture behavioral of alu_8bits is signal res : std_logic_vector(7 downto 0); begin with op select res <= std_logic_vector(unsigned(A) + unsigned(B)) when "000", -- ADD std_logic_vector(unsigned(A) - unsigned(B)) when "001", -- SUB A and B when "010", -- AND A or B when "011", -- OR A xor B when "100", -- XOR not A when "101", -- NOT (others => '0') when others; result <= res; zero <= '1' when res = x"00" else '0'; end architecture;
3
Logique Sequentielle
Bascules, registres et compteurs
Partie 1 : Bascule D
-- Bascule D avec reset asynchrone entity dff is port ( clk, reset, D : in std_logic; Q : out std_logic ); end entity; architecture behavioral of dff is begin process(clk, reset) begin if reset = '1' then Q <= '0'; elsif rising_edge(clk) then Q <= D; end if; end process; end architecture;
Partie 2 : Compteur Parametrable
entity counter is generic (N : integer := 8); port ( clk, reset, enable : in std_logic; count : out std_logic_vector(N-1 downto 0) ); end entity; architecture behavioral of counter is signal cnt : unsigned(N-1 downto 0); begin process(clk, reset) begin if reset = '1' then cnt <= (others => '0'); elsif rising_edge(clk) then if enable = '1' then cnt <= cnt + 1; end if; end if; end process; count <= std_logic_vector(cnt); end architecture;
📝 A faire
- Ajouter une entree de chargement parallele
- Implementer un compteur up/down
- Creer un compteur BCD (0-9)
- Implementer un diviseur de frequence
Partie 3 : Registre a Decalage
entity shift_reg is generic (N : integer := 8); port ( clk, reset : in std_logic; shift_en : in std_logic; serial_in : in std_logic; parallel_out : out std_logic_vector(N-1 downto 0) ); end entity; architecture behavioral of shift_reg is signal reg : std_logic_vector(N-1 downto 0); begin process(clk, reset) begin if reset = '1' then reg <= (others => '0'); elsif rising_edge(clk) then if shift_en = '1' then reg <= reg(N-2 downto 0) & serial_in; end if; end if; end process; parallel_out <= reg; end architecture;
4
Machines a Etats Finis (FSM)
Conception de controleurs
Exemple : Controleur de Feux Tricolores
entity traffic_light is port ( clk, reset : in std_logic; red, yellow, green : out std_logic ); end entity; architecture behavioral of traffic_light is type state_type is (S_RED, S_RED_YELLOW, S_GREEN, S_YELLOW); signal state : state_type := S_RED; signal counter : integer range 0 to 100 := 0; begin process(clk, reset) begin if reset = '1' then state <= S_RED; counter <= 0; elsif rising_edge(clk) then case state is when S_RED => if counter = 50 then state <= S_RED_YELLOW; counter <= 0; else counter <= counter + 1; end if; when S_RED_YELLOW => if counter = 10 then state <= S_GREEN; counter <= 0; else counter <= counter + 1; end if; when S_GREEN => if counter = 50 then state <= S_YELLOW; counter <= 0; else counter <= counter + 1; end if; when S_YELLOW => if counter = 10 then state <= S_RED; counter <= 0; else counter <= counter + 1; end if; end case; end if; end process; -- Sorties (Moore) red <= '1' when state = S_RED or state = S_RED_YELLOW else '0'; yellow <= '1' when state = S_YELLOW or state = S_RED_YELLOW else '0'; green <= '1' when state = S_GREEN else '0'; end architecture;
📝 A faire
- Ajouter un mode nuit (clignotement jaune)
- Implementer un detecteur de sequence (1011)
- Creer un controleur de distributeur automatique
5
Communication Serie (UART)
Transmission et reception RS-232
Transmetteur UART
Rappel UART
Format: 1 bit start (0) + 8 bits data + 1 bit stop (1). Baudrate typique: 9600, 115200.
📝 A faire
- Implementer un generateur de baud rate (diviseur d'horloge)
- Creer le transmetteur UART avec FSM
- Creer le recepteur UART
- Tester en loopback (TX connecte a RX)
6
Projet Final : Horloge Numerique
Integration des concepts
Specifications
- Affichage HH:MM:SS sur 6 afficheurs 7 segments
- Boutons pour reglage heures/minutes
- Mode 12h/24h
- Alarme programmable
Architecture suggeree
┌─────────────────────────────────────────────────────┐ │ TOP LEVEL │ ├──────────┬──────────┬──────────┬───────────────────┤ │ Clock │ Counter │ Counter │ Counter │ │ Divider │ Seconds │ Minutes │ Hours │ │ 1Hz │ 0-59 │ 0-59 │ 0-23 │ ├──────────┴──────────┴──────────┴───────────────────┤ │ Display MUX │ ├─────────────────────────────────────────────────────┤ │ 7-Segment Decoder │ └─────────────────────────────────────────────────────┘