module streamdecoder(flage1,flage2,data,clk,reset);
reg [2:0]count=3'b000;
parameter a1=8'hfa,a2=8'hfb;
//count=3'b000;
output reg flage1,flage2;
reg[2:0]out1;
input clk,reset;
input data;


always @(posedge clk)
begin
if(reset==1'b1)
begin

if((data==a1)&&(count==3'b000))
begin
count=count+3'b001;
flage1=1'b0;
end
else
if((data==a1)&&(count==3'b001))
begin
count=count+3'b001;
flage1=1'b0;
end
else
if((data==a1)&&(count==3'b010))
begin
count=count+3'b001;
flage1=1'b0;
end

else
if((data==a1)&&(count==3'b010))
begin
count=count+3'b000;
flage1=1'b0;
end
else
if((data==a2)&&(count==3'b011))
begin
count=count+3'b001;
flage1=1'b0;
end
else
if((data==a1)&&(count==3'b011))
begin
count=3'b001;
flage1=1'b0;
end

else
if((data==a2)&&(count==3'b100))
begin
count=count+3'b001;
flage1=1'b1;
end
else
begin
count=3'b001;
flage1=1'b0;
end

end

else
count=3'b000;


end //end of first always

//secand aloways
always @(flage1)
begin
if(flage1==1'b1)
out1=out1+1'b1;
else
out1=out1+1'b0;

if(out1==3'b110)
flage2=1'b1;
end//end of secand always


endmodule
//*
//*
//*
//*
//*
//test module forstreamdecoder for create a1a1a1a2a2
module test;
reg clk,reset;
reg [7:0]data;
wire flage1,flage2;
integer i,j;
parameter a1=8'hfa,a2=8'hfb;
streamdecoder s1(flage1,flage2,data,clk,reset);
initial
begin
reset=1'b0;
#20 reset=1'b1;
#180 reset=1'b0;
end
initial
clk=1'b0;
initial
forever #10 clk=~clk;
initial
begin
for(i=0;i<128;i=i+1)
for(j=0;j<1024;j=j+1)
begin
if((i>=2)&&(i<=20)&&(j<4))
@(posedge clk) data=a1;
else
if((i>=2)&&(i<=20)&&(j<6))
@(posedge clk) data=a2;
else
@(posedge clk) data=8'b0;
end
end

integer hand1;
integer hand2;


initial
begin
hand1=$fopen("out.txt");
// hand2=hand1|32'b1;
end
initial
$fmonitor(hand1,"i=%d\tj=%d\tdata=%h\tflag1=%b\tfl ag2=%b\tReset=%b\n",i,j,data,flage1,flage2,reset);
//initial
//$fdisplay(hand1,"i=%d\tj=%d\tdata=%h\tflag1=%b\tre set=%b\n",i,j,data,flage1,reset);
initial
// begin
#1000 $fclose(hand1);
initial
#10000 $finish;
endmodule