2017-02-10 59 views
-1

我一直在研究Verilog程序,该程序应该在每个时钟增量上向16位输出添加一个带符号的8位输入,并在接收到复位信号时复位。添加部分工作正常,甚至添加负1值的作品,但我得到奇怪的结果在模拟数字少于那个。自从我上次使用Verilog已经过去了大约一年之后,尽管尝试了所有我能想到的事情,但我不确定问题出在哪里。这是我有尽可能代码:Verilog签名加法减法错误

module varcount (clk, reset, in, out); 
input clk, reset; 
input [7:0] in; 
output reg [15:0] out; 
reg [15:0] temp; 
reg [15:0]count; 
parameter X=1000000; 
always @ (posedge clk) 
    begin 
    if (in[7] == 1) 
    begin 
    temp = 16'b00000000000000001 + !in; 
    count = count - temp; 
    if (reset) 
    begin 
     count = 0; 
     out = 0; 
    end 
    out = count; 
end 
    else 
    begin 
     count = count + in; 
     if (reset) 
     begin 
      count = 0; 
      out = 0; 
     end 
     out = count; 
    end 
end 
endmodule 

这里是我的模拟输入:

enter image description here

这里就是我得到的输出。

enter image description here

好像在我的节目一个直接的错误,但我不能确定它。

+2

欢迎StackOverflow上。由于您没有正确缩进,因此很难阅读您的代码。你也不会显示你期望的结果与实际结果的正确结果。 –

回答

0

问题是你正在做一个逻辑否定in与比特否定。

您的代码可以通过

  • reset条件分支使用signed数据类型
  • 用Verilog-2001的风格端口声明

对于移动到顶级

  • 可以大大简化例如:

    module varcount (input clk, reset, 
          wire signed [7:0] in, 
        output reg signed [15:0] out); 
    always @ (posedge clk) 
          if (reset) 
           out = 0; 
          else 
           out = out + in; 
    endmodule 
    
  • +0

    有没有办法从无符号数组中获取签名数组,反之亦然? – StandardIssue

    +0

    作为一个新问题,你应该问一个新问题。 –

    0

    我看到这个代码:

    temp = 16'b00000000000000001 + !in; 
    count = count - temp; 
    

    温度看起来像你试图计算二进制补码。 !是一个逻辑NOT。你会想~一个位反转。

    所以你可以有:

    sum <= a + (~b +1'b1); 
    

    或者

    sum <= a - b; 
    

    -b == (~b +1'b1)