مثالهایی از انواع ماشین حالت (FSM) در VHDL
در این بخش، چند مثال از ماشین حالت Moore و Mealy ارائه میکنیم تا تفاوت آنها را بهتر درک کنید.
1️⃣ مثال: ماشین حالت Moore - تشخیص الگوی "101"
در این ماشین حالت، خروجی تنها به حالت فعلی بستگی دارد.
🔹 تعریف حالات
type state_type is (S0, S1, S2, S3);signal current_state, next_state : state_type;
🔹 فرآیند تغییر حالت
process (clk, rst)beginif rst = '1' thencurrent_state <= S0;elsif rising_edge(clk) thencurrent_state <= next_state;end if;end process;
🔹 فرآیند انتقال بین حالتها
process (current_state, input_signal) begincase current_state iswhen S0 =>if input_signal = '1' thennext_state <= S1;else next_state <= S0;end if;when S1 =>if input_signal = '0' thennext_state <= S2;elsenext_state <= S1;end if;when S2 =>if input_signal = '1'then next_state <= S3;else next_state <= S0;end if;when S3 =>next_state <= S0;when others =>next_state <= S0;end case;end process;
🔹 تولید خروجی
process (current_state) begincase current_state iswhen S3 =>output_signal <= '1';when others =>output_signal <= '0';end case;end process;
2️⃣ مثال: ماشین حالت Mealy - تشخیص الگوی "101"
در این ماشین حالت، خروجی علاوه بر حالت فعلی، به ورودی نیز وابسته است.
🔹 فرآیند تغییر حالت
process (clk, rst)beginif rst = '1' thencurrent_state <= S0;elsif rising_edge(clk) thencurrent_state <= next_state;end if;end process;
🔹 فرآیند انتقال بین حالتها
process (current_state, input_signal) begincase current_state iswhen S0 =>if input_signal = '1' thennext_state <= S1;else next_state <= S0;end if;when S1 =>if input_signal = '0' thennext_state <= S2;else next_state <= S1;end if;when S2 =>if input_signal = '1' thennext_state <= S1;else next_state <= S0;end if;when others =>next_state <= S0;end case;end process;
🔹 تولید خروجی
process (current_state, input_signal) begincase current_state iswhen S2 =>if input_signal = '1' thenoutput_signal <= '1';else output_signal <= '0';end if;when others =>output_signal <= '0';end case;end process;
3️⃣ مثال: کنترل چراغ راهنمایی
یک ماشین حالت ساده برای کنترل چراغهای راهنمایی.
🔹 تعریف حالات
type state_type is (GREEN, YELLOW, RED);signal current_state, next_state : state_type;
🔹 فرآیند تغییر حالت
process (clk, rst)begin if rst = '1' thencurrent_state <= RED;elsif rising_edge(clk) thencurrent_state <= next_state;end if;end process;
🔹 فرآیند انتقال بین حالتها
process (current_state) begincase current_state iswhen GREEN =>next_state <= YELLOW;when YELLOW =>next_state <= RED;when RED =>next_state <= GREEN;when others =>next_state <= RED;end case;end process;
🔹 تولید خروجی
process (current_state)begincase current_state iswhen GREEN =>green_light <= '1';yellow_light <= '0';red_light <= '0';when YELLOW =>green_light <= '0';yellow_light <= '1';red_light <= '0';when RED =>green_light <= '0';yellow_light <= '0';red_light <= '1';when others =>green_light <= '0';yellow_light <= '0';red_light <= '0';end case;end process;
🔹 جمعبندی
در این مطلب، انواع ماشینهای حالت Moore و Mealy را بررسی کرده و با چند مثال عملی نحوه پیادهسازی آنها را در VHDL نشان دادیم.