包含加法器的verilog代码

时间:2015-02-26 05:47:40

标签: verilog fpga system-verilog

我写的verilog代码只包含加法器。在这个g中,h是10位,r5(主输出)是11位。当我把r5作为11位然后我没有得到正确的输出,但当我把r5作为10位然后我得到正确。但是(r5 = g + h)所以它的位应该比g,h的位多一个。 我把输入作为clk = 1;

    s189 = 10'd200;
    s375 = 10'd75;
    s050 = 10'd300;
    s218 = 10'd54;

输出应为r5 = -16,但输出为(01111110000)而不是(11111110000)

module out(clk,s189,s375,s050,s218,r5,g,h);
input clk;
input [9:0] s189,s375,s050,s218;
output reg[10:0] r5;
output reg [9:0] g,h;
reg [3:0] countr=4'b0000;
always@(posedge clk)
begin
if (countr==4'b1000)
begin
 g<= s218-s189;
 h<= s375+s050;
 r5<=g+h;
 end
end

always@(posedge clk)
begin
 if (countr==4'b1001)
 countr<=4'b0000;
 else 
 countr<= countr+1;
end

endmodule

1 个答案:

答案 0 :(得分:2)

您正在执行无符号算术,如前所述,MSB为0而不是1(负)。您需要声明用作签名的输入,输出和变量,以进行自动符号扩展。

module out(
  input clk, 
  input signed [9:0] s189,
  input signed [9:0] s375,
  input signed [9:0] s050,
  input signed [9:0] s218,
  output reg signed [10:0] r5,
  output reg signed [9:0] g,
  output reg signed [9:0] h
);