verilog,FSM,有限状态机,错误

时间:2015-07-24 08:52:02

标签: verilog fsm state-machine

当我在Modelsim中模拟下面的模块时,我没有看到cikis的任何输出波,请让我知道这个FSM和测试平台出了什么问题。

我有3个输入和1个输出模块。

    module sayici_fsm( clock, reset,cikis, out);

    input  wire clock;
    input  wire reset;

    output reg  [2:0] cikis;
    output wire       out;

    localparam durum0 = 3'd0,
               durum1 = 3'd1,
               durum2 = 3'd2,
               durum3 = 3'd3,
               durum4 = 3'd4,
               durum5 = 3'd5,
               durum6 = 3'd6,
               durum7 = 3'd7;

    reg [2:0] currentstate;
    reg [2:0] nextstate;

    assign out = (durum1& durum3);

    always @(*) begin
        cikis = 3'b000;
        case (currentstate)
            durum0: 
            begin
                 cikis = 3'b000;
            end
            durum1: 
            begin
                cikis = 3'b000;
            end
            durum2: 
            begin
                cikis = 3'b010;
            end
            durum3: 
            begin
                cikis = 3'b100;
            end
            durum4: 
            begin
                cikis = 3'b110;
            end
        endcase
    end

  always @(posedge clock)  begin
    if(reset)  currentstate <= durum0 ;
    else       currentstate <= nextstate;
    end

  always @(*) begin
    nextstate =currentstate;
      case (currentstate)
        durum0: 
          begin
            nextstate = durum1;
          end
        durum1: 
          begin
            nextstate = durum2;
          end
        durum2: 
          begin
            nextstate = durum3;
          end
        durum3: 
          begin
            nextstate = durum4;
          end
         durum4: 
           begin
             nextstate = durum1;
           end
         durum5: 
           begin
             nextstate = durum0;
           end
         durum6: 
           begin
             nextstate = durum0;
           end
          durum7: 
            begin
              nextstate = durum0;
            end
        endcase
    end    
    endmodule

module sayici_fsm_tb();

wire [2:0] cikis;
wire       out;

reg clock;    
reg reset;    

sayici_fsm u1(.out   (out  ),
              .clock (clock),
              .cikis (cikis),
              .reset (reset)
             );

initial 
begin
    $display( $time,  "Similasyon Baslasin");
    $display( " Time  clock reset  enable giris cikis");
    $monitor ("time =%d, clock =%b, reset=%b, cikis=%b  out =%b", $time, clock, reset, cikis,out);

    reset = 1'b1;
    clock = 1'b0;
#5  reset = 1'b0;
    clock = 1'b1;
#100 $finish;  
end

always #7  clock =!clock;

endmodule

1 个答案:

答案 0 :(得分:2)

错误在于在测试平台中生成重置,因为未正确初始化cikis会转到默认值&#39; x&#39;

     reset = 1'b1;
     clock = 1'b0;
  #5 reset = 1'b0;  // give more time  so that at posedge of clk reset is asserted say #8
     clock = 1'b1;  // remove from tb
  #100 $finish;  
end
always #7  clock =!clock;

从上面的片段开始,复位在5 ns时变为低电平,但是时钟在第一个时间点为7 ns,在时钟发生时,当前状态被编码为durum0的值,如下所述

always @(posedge clock)  begin
if(reset)  currentstate <= durum0 ;
else       currentstate <= nextstate;
end

要解决错误,请等待大量时钟并重置总线。