在verilog中行为模型的不正确结果

时间:2014-03-09 17:42:56

标签: verilog

即使有输入错误,我也主要担心case构造执行默认语句。

我将always @(in1,in2)更改为always @*,并且还观察到算术移位操作无法正常工作。我更改了以下内容: 现在我得到了正确的结果。

`timescale 1ns / 1ps

module simpleBmul(res,in1,in2);
output [15:0] res;
input [7:0] in1,in2;
reg [15:0] res;

reg FF;

initial
begin
    FF=1'b0;
    res[15:0]=16'h00;
end

always @*
begin
    res[15:8]=8'h00;
    res[7:0]=in1;
    FF=1'b0;
    repeat(8)
    begin
        case({res[0],FF})
        2'b00,
        2'b11: ;
        2'b10:{res[15:8]}=res[15:8]-in2;
        2'b01:{res[15:8]}=res[15:8]+in2;
        default:$display("\t\t\terror");
        endcase
        {res,FF}=$signed({res,FF})>>>1'b1;
    end
end
endmodule

--------------------------------------------------------------
ouput:
at time 0   in1=00000000, in2=00000000, res=0000000000000000
at time 21  in1=01011000, in2=00110111, res=0001001011101000
at time 31  in1=00110101, in2=01011001, res=0001001001101101

抱歉中断并感谢您的回复

1 个答案:

答案 0 :(得分:1)

我不确定这是否是故意的,但你有:

  else 
    $display("\t\t\terror");    
    {res,FF}={res,FF}>>>1'b1;
  end

作为您的一部分,如果您没有使用开始结束语句,则始终会执行第{res,FF}={res,FF}>>>1'b1;行。在学习verilog时,我会鼓励自由使用begin end语句,因为我发现这是最常见的bug类型。

你实际上是这样写的:

repeat(8) begin 
  if(({res[0],FF}==2'b00) | ({res[0],FF}==2'b11)) begin
    ; //Missing assignment
  end
  else if({res[0],FF}==2'b10) begin
    res[15:8]=res[15:8]-in2;
  end
  else if({res[0],FF}==2'b00) begin
    res[15:8]=res[15:8]+in2;
  end
  else begin 
    $display("\t\t\terror");    
  end
  {res,FF}={res,FF}>>>1'b1; //Always execute
end

注意:我会避免使用像always @(in1,in2)这样的手动敏感度列表 替换为always @*