練習寫了一個Sequence
Detector
網路上的範例大多都是四個bit
我練習寫了一個8
bit
對於每個state的轉換,其實不是百分之百的確定
只是寫了一些變化的testbench來測
如果遇到其他多元化的情形,FSM一定是需要再修改的
Sequence 10101100
VHDL
library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity seq_det is port( clk : in std_logic; reset : in std_logic; enable : in std_logic; seq_in : in std_logic; seq_detected : out std_logic); end seq_det; ---- Pattern 10101100--------- architecture Behavioral of seq_det is type state_type is (IDLE,S1,S2,S3,S4,S5,S6,S7,S8,S9); signal cur_state, nxt_state : state_type; signal seq_det,seq_det_w : std_logic; begin seq_detected <= seq_det; process(clk) begin if( reset = '1' ) then cur_state <= IDLE; seq_det <= '0'; elsif (clk'event and clk = '1') then cur_state <= nxt_state; seq_det <= seq_det_w; end if; end process; Process(cur_state,seq_in,enable) begin case cur_state is when IDLE => seq_det_w <= '0'; if( enable = '1') then if ( seq_in = '1' ) then nxt_state <= S1; else nxt_state <= IDLE; end if; else nxt_state <= IDLE; end if; when S1 => -- 1 seq_det_w <= seq_det; if ( seq_in = '0' ) then nxt_state <= S2; else nxt_state <= S1; end if; when S2 => -- 0 seq_det_w <= seq_det; if ( seq_in = '1' ) then nxt_state <= S3; else nxt_state <= IDLE; end if; - when S3 => -- 1 seq_det_w <= seq_det; if (seq_in = '0' ) then nxt_state <= S4; else nxt_state <= S1; end if; when S4 => -- 0 seq_det_w <= seq_det; if ( seq_in = '1' ) then nxt_state <= S5; else nxt_state <= IDLE; end if; when S5 => -- 1 seq_det_w <= seq_det; if ( seq_in = '1' ) then nxt_state <= S6; else nxt_state <= S4; end if; when S6 => -- 1 seq_det_w <= seq_det; if ( seq_in = '0' ) then nxt_state <= S7; else nxt_state <= S1; end if; when S7 => -- 0 seq_det_w <= seq_det; if ( seq_in = '0' ) then nxt_state <= S8; else nxt_state <= S3; end if; when S8 => -- 0 seq_det_w <= '1'; if ( seq_in = '1' ) then nxt_state <= S1; else nxt_state <= IDLE; end if; when others => NULL; end case; end process; end Behavioral; |
沒有留言:
張貼留言