假设我有一个模块,它接受两个数字和一个信号,指示它是有符号乘法还是无符号乘法。模块必须执行乘法。
module signed_or_unsigned_mul
# (
parameter n = 8
)
(
input [ n - 1:0] a, b,
input signed_mul,
output [2 * n - 1:0] res
);
我尝试用三元运算符实现它,但它输出不正确的结果,例如 -8 * -7 = 72
assign res = signed_mul ? ($signed(a) * $signed(b)) : (a * b) ;
下面的代码使用 always_comb 和 if 产生正确的结果:
logic [2 * n - 1:0] res_stub;
assign res = res_stub;
always_comb
if (signed_mul)
res_stub = $signed(a) * $signed(b);
else
res_stub = a * b;
我不明白这两种实现有什么区别,也不明白为什么它们会产生不同的输出。你能帮我指出来吗?额外的逻辑变量会不会有这种影响?