module signed_op(
input clk_i,
input rst_n,
input signed cin,
input signed [3:0] dataa_i,
input signed [3:0] datab_i,
output signed [4:0] sum_o
);
assign sum_o = dataa_i + datab_i + cin;
endmodule
module signed_op(
input clk_i,
input rst_n,
input cin,
input signed [3:0] dataa_i,
input signed [3:0] datab_i,
output signed [4:0] sum_o
);
assign sum_o = dataa_i + datab_i + cin;
endmodule
module signed_op(
input clk_i,
input rst_n,
input cin,
input signed [3:0] dataa_i,
input signed [3:0] datab_i,
output signed [4:0] sum_o
);
assign sum_o = {dataa_i[3],dataa_i} + {datab_i[3],datab_i} + cin;
endmodule
assign sum_o = dataa_i + datab_i + $signed(cin);
module signed_op(
input clk_i,
input rst_n,
input cin,
input signed [7:0] dataa_i,
input signed [5:0] datab_i,
output signed [8:0] sum_o
);
assign sum_o = dataa_i + datab_i + cin;
endmodule
最后3行都是unsigned格式显示。再向上三行是Decimal。
由于cin是无符号的,所以整个运算都按照无符号来进行了。
如何进行不等位宽的混合运算呢?首先明确:verilog在进行计算的时候是按照补码来进行的。-6D原码:10_0110B,补码11_1010B。这种情况同样需要进行拓位,拓位是以多个符号位进行补全。比如拓展成8bit的-6D,就是1111_1010B。易错的是按照上面的思维定势,只拿1bit符号位放到最高,其余补0,这是不对的。
module signed_op(
input clk_i,
input rst_n,
input cin,
input signed [7:0] dataa_i,
input signed [5:0] datab_i,
output signed [8:0] sum_o
);
wire signed [8:0] datab_w = {{3{datab_i[5]}},datab_i};
wire signed [8:0] dataa_w = {dataa_i[7],dataa_i};
assign sum_o = dataa_w + datab_w + cin;
endmodule
module signed_op(
input clk_i,
input rst_n,
input cin,
input [7:0] dataa_i,
input signed [5:0] datab_i,
output signed [8:0] sum_o
);
wire signed [8:0] datab_w = {{3{datab_i[5]}},datab_i};
assign sum_o = dataa_i + datab_w + cin;
endmodule