首页 > 解决方案 > Verilog switch 案例中的“非法引用内存”

问题描述

我正在尝试为作业编写有限状态机。但是,我不明白我的模块代码有什么问题。我得到错误:

错误 VCP5221 “非法引用内存:st.” “设计.sv” 77 5

当我尝试合成时。我想我要么误解了在 Verilog 中如何使用 switch-case,要么我试图在 switch-case 内进行非法分配,但我不知道如何找到它。

//Code here

module MooreFSM(
  input clk,
  input in,
  output out
);
  
  reg state[2:0];
  reg sel[2:0];
  reg o;
  assign out = o;
  
  initial begin
    state <= 3'b000;
    o <= 1'b0;
  end
  
  always@(negedge clk) begin
    state <= sel;
  end
  
  always@(posedge clk) begin
    case(state)
      3'b000: begin
        if(in == 1'b0) begin
          sel <= 3'b010;
          o <= 1'b0;
        end
        else if(in == 1'b1) begin
          sel <= 3'b001;
          o <= 1'b0;
        end
      end
      3'b001: begin
        if(in == 1'b0) begin
          sel <= 3'b000;
          o <= 1'b0;
        end
        else if(in == 1'b1) begin
          state <= 3'b011;
          o <= 1'b0;
        end
      end
      3'b010: begin
        if(in == 1'b0) begin
          sel <= 3'b100;
          o <= 1'b0;
        end
        else if(in == 1'b1) begin
          sel <= 3'b000;
          o <= 1'b0;
        end
      end
      3'b011: begin
        if(in == 1'b0) begin
          sel <= 3'b000;
          o <= 1'b1;
        end
        else if(in == 1'b1) begin
          sel <= 3'b011;
          o <= 1'b1;
        end
      end
      3'b100: begin
        if(in == 1'b0) begin
          sel <= 3'b100;
          o <= 1'b1;
        end
        else if(in == 1'b1) begin
          sel <= 3'b000;
          o <= 1'b1;
        end
      end
      default: begin
        sel <= 3'b000;
        o <= 1'b0;
      end
    endcase
  end
endmodule

请帮助我确定问题,或者如果我需要使用不同的方法来合成多路复用器。

标签: switch-statementstate-machinesynthesis

解决方案


我想当你放

else if(in == 1'b1) begin
  state <= 3'b011;
  o <= 1'b0;
end

你可能打算写

else if(in == 1'b1) begin
  sel <= 3'b011;
  o <= 1'b0;
end

通过state在此 switch 语句的一种情况下分配一些东西,你隐含地要求它在所有其他情况下都被锁定,这与你明确地使它成为上面的失败相冲突。


推荐阅读