在verilog(符号扩展)中编辑算术

making Arithmetic in verilog (sign extension) edited

(已编辑)我正在研究 verilog 算术项目,但我被困在符号扩展部分(假设这是问题所在)。我有 4 位输入 A、B,应该有 8 位输出。对于某些过程(总和,子...),我需要使用符号扩展来生成 8 位输出。所以对于算术主体,我有这段代码。这是代码的一半。我没有包括一半因为它太长了..

module arithmetic(A, B, AN0, DP, sum, sub, mult, div, comp, shiftLeft, 
shiftRight, signExtend);

   input signed [3:0] A, B;

   output [7:0] sum, sub, mult, div, comp, shiftLeft, shiftRight, 
   signExtend;

   output AN0, DP;

   //sum
   reg [4:0] qsum;   
   always@ (A, B)
      qsum = A+B;

   assign sum = {{3{qsum[4]}},qsum};

   //sub
   reg [4:0] qsub;

   always@ (A, B)
      qsub = A-B;

   assign sub = {{3{qsub[4]}},qsub};

   //mult
   reg [7:0] qmult;
   always@ (A, B)
      qmult = A * B;

   assign mult = qmult;

当我检查我的模拟时,它没有任何值,只有 Z 和 Xs。它甚至不显示任何输入值。为什么会这样??谢谢

(edited) 这是我的测试台代码。共有8种运算(求和、减、乘、除、比较、左移、右移、符号扩展)

module lap3_top_tb();
reg signed [3:0] A, B;
reg [2:0] Operation;
wire [7:0] Result;
wire DP, AN0;

lab3_top ulap3_top(
  .A(A),
  .B(B),
  .Operation(Operation),
  .Result(Result),
  .DP(DP),
  .AN0(AN0)
);

initial begin
   A = 6; B = 7; Operation = 0;
   #20;
   A = -6; B = -7; Operation = 0;
   #20;      
   A = 6; B = 7; Operation = 1;
   #20;
   A = -6; B = -7; Operation = 1;
   #20;
   A = 6; B = 7; Operation = 2;
   #20;
   A = -6; B = 7; Operation = 2;
   #20;
   A = 7; B = 4; Operation = 3;
   #20;
   A = 7; B = 0; Operation = 3;
   #20;
   A = 6; B = 7; Operation = 4;
   #20;
   A = -6; B = -7; Operation = 4;
   #20;
   A = 1; B = 6; Operation = 5;
   #20;
   A = 1; B = -6; Operation = 5;
   #20;
   A = 1; B = 6; Operation = 6;
   #20;
   A = 1; B = -6; Operation = 6;
   #20;
   A = 6; B = 0; Operation = 7;
   #20;
   A = -5; B = 0; Operation = 7;
   #20;
end

endmodule

lap3_top 文件在这里。 (mux_8_1 将选择输出并通过结果输出。如果您需要代码,请告诉我!但我认为 mux 工作正常)

module lap3_top(A, B, Operation, Result, AN0, DP);
   input signed [3:0] A, B;
   input [2:0] Operation;
   output AN0, DP;

   output [7:0] Result;
   wire a, b, c, d, e, f, g, h;

   arithmetic uarithmetic(
   .A(A), 
   .B(B), 
   .AN0(AN0), 
   .DP(DP), 
   .sum(a), 
   .sub(b), 
   .mult(c), 
   .div(d), 
   .comp(e), 
   .shiftLeft(f), 
   .shiftRight(g), 
   .signExtend(h)
   );

   mux_8_1 umux8_1(
   .A(a), 
   .B(b), 
   .C(c), 
   .D(d), 
   .E(e), 
   .F(f), 
   .G(g), 
   .H(h), 
   .Operation(Operation), 
   .Result(Result)
   );

endmodule

非常感谢你们!

我尝试模拟你的代码并在你的代码中发现了以下错误:当你在 testbench 模块中实例化 top 模块时你使用 lab3_top ulap3_top(...); lab3_top 模块名称,但是你想要的模块有另一个名称 module lap3_top(...); lap3_top.

我更改了名称,一切正常(在波形上你可以看到 ZZ 状态, 因为我在代码中没有mux_8_1模块,而且很少有操作没有描述)

P.S。顺便说一下,我假设您在添加此标签时使用的是 Vivado。如果是这样,会有提示如何检查这样的错误(模块和实例化中的名称不同,或者当模块中有一些错误无法在库中编译时)。如果您展开层次结构中的所有模块,您会发现 ? 登录错误所在的模块。