设计一个VHDL状态机,实现一个简单的交通信号灯控制器。

VHDL交通信号灯控制器状态机设计

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity traffic_light_controller is
    port(
        clk     : in std_logic;
        reset   : in std_logic;
        red     : out std_logic;
        yellow  : out std_logic;
        green   : out std_logic
    );
end entity traffic_light_controller;

architecture traffic_light of traffic_light_controller is
    type state_type is (STOP, READY, GO);
    signal current_state, next_state: state_type;
    signal timer: integer range 0 to 7 := 0;

    constant red_time: integer := 4;
    constant yellow_time: integer := 1;
    constant green_time: integer := 2;

begin

    process(clk, reset)
    begin
        if reset = '1' then
            current_state <= READY;
            timer <= 0;
        elsif clk'event and clk = '1' then
            current_state <= next_state;
        end if;
    end process;

    process(current_state, timer)
    begin
        case current_state is
            when STOP =>
                red <= '1';
                yellow <= '0';
                green <= '0';
                if timer >= red_time then
                    next_state <= READY;
                    timer <= 0;
                else
                    timer <= timer + 1;
                end if;
            when READY =>
                red <= '1';
                yellow <= '1';
                green <= '0';
                if timer >= yellow_time then
                    next_state <= GO;
                    timer <= 0;
                else
                    timer <= timer + 1;
                end if;
            when GO =>
                red <= '0';
                yellow <= '0';
                green <= '1';
                if timer >= green_time then
                    next_state <= STOP;
                    timer <= 0;
                else
                    timer <= timer + 1;
                end if;
        end case;
    end process;

end architecture traffic_light;