赞
踩
An adder-subtractor can be built from an adder by optionally negating one of the inputs, which is equivalent to inverting the input then adding 1. The net result is a circuit that can do two operations: (a + b + 0) and (a + ~b + 1). See Wikipedia if you want a more detailed explanation of how this circuit works.
Build the adder-subtractor below.
You are provided with a 16-bit adder module, which you need to instantiate twice:
Use a 32-bit wide XOR gate to invert the b input whenever sub is 1. (This can also be viewed as b[31:0] XORed with sub replicated 32 times. See replication operator.). Also connect the sub input to the carry-in of the adder.
该题目实现较为简单,题目中也给出了可以通过前面章节学习的复制将sub进行扩展,但该题目需要理解为什么通过异或与设置cin位为sub就能够将加法器改造为减法器。B站Up主视频讲解了计算机能够通过补码完成加减法运算。
因此,在进行减法运算时,需要将被减数b的补码变为-b的补码,视频中讲解了可以将每一位均取反再加+1。当sub为1,完成位扩展后变为32个1。此时,再与b进行异或操作(不相同为1,相同为0),即可完成取反,在结合sub的进位即可完成b的补码变为-b的补码。通过a+(-b)的补码完成取反操作。
同时,当sub为0是,b异或后不变,且进位cin为0,与原来的加法器一样。
module top_module( input [31:0] a, input [31:0] b, input sub, output [31:0] sum ); //进位信号 wire cout; wire [31:0] b_xor; assign b_xor = {32{sub}} ^ b; //低16位相加 add16 add16_init_0( .a(a[15:0]), .b(b_xor[15:0]), .cin(sub), .sum(sum[15:0]), .cout(cout) ); //高16位相加 add16 add16_init_2( .a(a[31:16]), .b(b_xor[31:16]), .cin(cout), .sum(sum[31:16]), .cout() ); endmodule
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。