2、用状态机实现10010码的探测,如x=1001001000 z=0000100100(输出)
module check(rst_i,clk_i,data_i,data_o);
input rst_i,clk_i;
input data_i;
output data_o;
reg[3:0] current_state,next_state;
parameter[3:0]
idle="0000",
state1="0001",
state2="0010",
state3="0100",
state4="1000";
always@(posedge clk_i or negedge rst_i)
if (!rst_i)
current_state<=idle;
else
current_state<=next_state;
always@(current_state,data_i)
case(current_state)
idle : if (data_i==1)next_state=state1;
else next_state=idle;
state1: if (data_i==0)next_state=state2;
else next_state=idle;
state2: if (data_i==0)next_state=state3;
else next_state=idle;
state3: if (data_i==1)next_state=state4;
else next_state=idle;
state4: if (data_i==0)next_state=idle;
else next_state=idle;
endcase
assign data_o= (current_state==state4);
endmodule
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity check is
port(
rst_i :in std_logic;
clk_i :in std_logic;
data_i:in std_logic;
data_out std_logic);
end entity;
--}} End of automatically maintained section
architecture behave of check is
type state is (state_a,state_b,state_c,state_d,state_e);
signal current_state:state;
signal next_state:state;
begin
--type state is (state_a,state_b,state_c,state_d);
process(rst_i,clk_i)
begin
if rst_i='1' then
data_o<='0';
current_state<=state_a;
elsif rising_edge(clk_i) then
current_state<=next_state;
end if;
end process;
process(current_state,data_i)
begin
case current_state is
when state_a => if data_i='1' then
next_state<=state_b;
else
next_state<=state_a;
end if;
when state_b => if data_i='0' then
next_state<=state_c;
else
next_state<=state_a;
end if;
when state_c => if data_i='0' then
next_state<=state_d;
else
next_state<=state_a;
end if;
when state_d => if data_i='1' then
next_state<=state_e;
else
next_state<=state_a;
end if;
when state_e => if data_i='0' then
next_state<=state_a;
else
next_state<=state_a;
end if;
when others => null;
end case;
end;
data_o <='1' when current_state = state_e else '0';
end; |