使用Verilog shows X对FSM进行建模

使用Verilog shows X对FSM进行建模,verilog,state-machine,Verilog,State Machine,鉴于以下FSM: 我正在尝试使用Verilog对其进行建模,下面是我的试验: module fsm (u,d,clr,clk,inc,dec,c); input u,d, clr,clk; output reg inc,dec,c ; // the state variables reg y,Y; // y is the present state and Y is the next state reg S0,S1; // next state and output spec

鉴于以下FSM:

我正在尝试使用Verilog对其进行建模,下面是我的试验:

module fsm (u,d,clr,clk,inc,dec,c);
  
input  u,d, clr,clk;
output reg inc,dec,c ;
// the state variables
reg y,Y; // y is the present state and Y is the next state
reg S0,S1;  

  // next state and output specifications 
 
  always @ (y,u,d) begin 
    
    case (y)
      S0 : if (u==1) begin
        Y= S1;
        inc=1;
        dec=0;
        c=1;
      end
      else if (u==0) begin
        c=0;
      end
      S1 :  if (d==1) begin
         Y= S0;
         inc=0;
         dec=1;
         c=0;
      end
      else if (d==0) begin
            c=1;
      end
   endcase 
  end 
  // state update
    always @(posedge clk,clr) begin
      if (clr) 
        y=S0;
      else 
        y=Y;
    end
    endmodule
请让我知道我的代码是否正确描述了此FSM,如果代码中有任何增强,请让我知道

以下是初始测试台代码:

module tb();
 reg u,d, clr,clk;
reg inc,dec,c,y ;
  
  fsm ttb (.u(u),.d(d),.clk(clk),.clr(clr),.inc(inc),.dec(dec),.c(c));
  
     initial begin 
    $dumpfile("dump.vcd");
    $dumpvars;
  end 
  
  initial begin //{
   clk=0 ; clr=0; #10
    clk=1; u=0 ; clr=0 ; d=0; #10
    clk = 0;#10
    u=1; clk=1; #10
   
    
    
    
    
    $finish;
    
    
    
    
  end //}
  
endmodule
testbench代码显示出了一些错误。当前状态变量
y
显示未定义的值“X”。我不知道为什么。输出变量
inc
dec
也是如此


有几个原因导致
X

在设计中,您将
S0
S1
声明为
reg
,但从未为它们赋值。Verilog
reg
被初始化为
X
。FSM通常使用常量
参数
值进行编码。例如,更改:

reg S0,S1;  
致:


在所有情况下,您的下一状态逻辑都需要为
Y
赋值(请参阅
当您尝试编译和模拟代码时会发生什么情况?我会删除敏感度列表中的clr,除非您希望它是异步的。@Laburtz是的,我需要它是异步的。我是否保持它的原样?我的代码中的这一写行与“@(pos clk或clr)”有什么区别?
parameter S0=0, S1=1;  
  always @ (y,u,d) begin 
    case (y)
      S0 : if (u==1) begin
        Y= S1;
        inc=1;
        dec=0;
        c=1;
      end
      else if (u==0) begin
        c=0;
        Y= S0; // <-----
      end
      S1 :  if (d==1) begin
         Y= S0;
         inc=0;
         dec=1;
         c=0;
      end
      else if (d==0) begin
            c=1;
            Y= S1; // <-----
      end
   endcase 
  end 
  initial begin
    u=0; d=0;
    clk=0 ; clr=1; #10
    clk=1; u=0 ; d=0; #10
    clk = 0;#10
    u=1; clk=1; clr=0; #10
    $finish; 
  end
always @(posedge clk,clr) begin
always @(posedge clk, posegde clr) begin