//rtl code for verilog code for Mealy 101 detector
`timescale 1ns/1ns
module mealy_101(clk,rst,x,z);
input clk,rst,x;
output z;
reg z;
parameter [1:0] s0 = 2'b00;
parameter [1:0] s1 = 2'b01;
parameter [1:0] s2 = 2'b10;
reg [1:0] present_state;
reg [1:0] next_state;
always @(posedge clk or negedge rst)
begin
  if(!rst)
 present_state <= s0;
  else
   present_state <= next_state;
end
always @(x or present_state)
begin
case (present_state)
s0: if (x == 1'b1)
 next_state = s1;
     else
 next_state = s0;
s1: if (x == 1'b1)
 next_state = s1;
     else
 next_state = s2;
s2: if (x == 1'b1)
 next_state = s1;
     else
 next_state = s0;
default: next_state = s0;
endcase
end
always@(present_state or x)
begin
case (present_state)
s0: z = 1'b0;
s1: z = 1'b0;
s2: if (x == 1'b1)
 z= 1'b1;
     else
 z = 1'b0; 
endcase
end
endmodule
//testbench 
`timescale 1ns/1ns
module test_mealy_101;
 reg clk, rst, x1;
 wire z1;
 mealy_101 m1(.clk(clk),
       .rst(rst),
       .x(x1),
       .z(z1)
      );
initial 
begin
clk=1'b1;
forever #10 clk=~clk;
end
initial
begin
 rst=1'b0;
#10 rst = 1'b1;
end
initial
begin
 #10 x1 = 1'b0;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b0;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b1;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b0;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b1;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b1;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b1;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b0;
 #10 $display("output=%b",z1);
 #5 x1 = 1'b1;
 #10 $display("output=%b",z1);
#40 $finish;
end
initial
begin
 $recordfile ("mealy_101.trn");
 $recordvars ("depth = 0");
end
endmodule
No comments:
Post a Comment