只需遵循@Morgan 的回答,并且因为我的系统中已经有一个执行此操作的模块,这是我的贡献:
module Mod(
input signed [11:0] i,
output signed [11:0] o
);
assign o = i[11] ? -i : i; // This does all the magic
endmodule
这是一个测试平台:
module tb;
reg signed [11:0] i;
wire signed [11:0] o;
Mod M(i,o);
integer t;
initial begin
for (t = -10; t < 10; t = t + 1) begin
#1
i <= t;
$display("i = %d, o = %d", i, o);
end
end
endmodule
输出是:
i = x, o = x
i = -10, o = 10
i = -9, o = 9
i = -8, o = 8
i = -7, o = 7
i = -6, o = 6
i = -5, o = 5
i = -4, o = 4
i = -3, o = 3
i = -2, o = 2
i = -1, o = 1
i = 0, o = 0
i = 1, o = 1
i = 2, o = 2
i = 3, o = 3
i = 4, o = 4
i = 5, o = 5
i = 6, o = 6
i = 7, o = 7
i = 8, o = 8