Implementing a 4-bit ripple carry adder/subtractor using structural VHDL
Once you have a Ripple-Carry Adder (as you had in your Question), you can create a Ripple-Carry Adder-Subtractor with a little extra logic.


  Note: The Answer here is given without a concrete "this is the VHDL code" solution. Homework Questions on EE.SE (provided a reasonable effort is put into the Question) are met with Socratic questions and general tips to guide the student to the solution.


Your adder implements the following operation (in this revision of your adder.):

$$ Sum = A + B $$

You're tasked with adding a control line (Add/Sub) to make your adder perform both addition and subtraction. When commanded to subtract, the operation should be:

$$ Difference = A - B = A + (-B)$$

Now that you've transformed your subtraction operation back into an addition operation, we just need to find \$-B\$. From Two's Complement we know:

$$ -B = \bar{B} + 1 $$

Let's establish a convention. When our input add_sub is 1, we'll do subtraction. When add_sub is 0, we'll do addition. So when add_sub is 1, we need to:  


Invert B
Add 1


Thus we have:

B &lt;= B_in XOR add_sub;


Finally, adding 1. Hey, isn't that what would happen if Cin is 1? So make Cin = 1 when add_sub = 1...