1.废话:蓝皮书学习过程中,补充了自己对于加法器的认知不足,加法进位无需通过在“加”的过程中用扩展最高位体现,直接扩展输出结果的位数即可。
2. 正题:
① 在此之前自己为防止加法过程中进位的丢失采取如下做法:
wire [3:0] a,b ;
wire [4:0] c ;
assign c = {1'b0,a} + {1'b0,b} ;
如果是有符号数,就是补齐最高位符号位:
wire signed [3:0] a,b ;
wire signed [4:0] c ;
assign c = {a[3],a} + {b[3],b} ;
② 那么今天(2022.6.28)从蓝皮书中获得方法如下:直接扩展输出的位宽即可。
module add (X,Y,sum);
input wire [3:0] X,Y;
output wire [4:0] sum;
assign sum = X+Y ;
endmodule
其行为级仿真结果如下: 结果正确。
③ 若保持输出位宽与输入一致,为体现进位,可如下编写:
module add (X,Y,sum,C);
input wire [3:0] X,Y;
output wire [3:0] sum;
output wire C;
assign {C,sum} = X+Y ;
endmodule
如上所示,仿真结果符合预期。