Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

Figure 1 shows the add/subtract block used in our basic ALU:

"TODO"

Figure 1:Adder/Subtractor block.

This single circuit is capable of addition and subtraction:

When SUB=1, the circuit performs the subtraction A - B. When SUB=0 the circuit performs the addition A + B.

The “special” output, labeled overflow signals for integer overflow. Upon performing an addition or subtraction, this output will be a 1 if the result is too large to fit in 32 bits. We return to this fact later.

To design our adder/subtractor circuit, we will start out by studying the design of an adder circuit, then later augment it to also perform subtraction.

2Cascading Intuition

One strawman[1] method for arriving at the detailed gate-level circuit for the adder would be to follow the procedure we learned in the previous chapter[2]. Unfortunately, that technique is only effective for very narrow adders (e.g., 1-bit or 2-bit wide), because the size of the truth table is too large for wider adders.

Therefore, we turn to modular design: Find a way to break the design up into smaller more manageable pieces. We will design the smaller pieces individually, then wire them together to create the entire wide adder.

Consider adding two 4-bit numbers A= a3a2a1a0 and B= b3b2b1b0:

c3c2c1a3a2a1a0+b3b2b1b0y3y2y1y0\begin{array}{c c c c c} & \texttt{c}_{\texttt{3}} & \texttt{c}_{\texttt{2}} & \texttt{c}_{\texttt{1}} & \\ & \texttt{a}_{\texttt{3}} & \texttt{a}_{\texttt{2}} & \texttt{a}_{\texttt{1}} & \texttt{a}_{\texttt{0}} \\ + & \texttt{b}_{\texttt{3}} & \texttt{b}_{\texttt{2}} & \texttt{b}_{\texttt{1}} & \texttt{b}_{\texttt{0}} \\ \hline & \texttt{y}_{\texttt{3}} & \texttt{y}_{\texttt{2}} & \texttt{y}_{\texttt{1}} & \texttt{y}_{\texttt{0}} \end{array}

Example, where A is 1011 and B is 0001:

0111011+00011100\begin{array}{c c c c c} & \texttt{0} & \texttt{1} & \texttt{1} & \\ & \texttt{1} & \texttt{0} & \texttt{1} & \texttt{1} \\ + & \texttt{0} & \texttt{0} & \texttt{0} & \texttt{1} \\ \hline & \texttt{1} & \texttt{1} & \texttt{0} & \texttt{0} \end{array}

31-bit Adder

Addition in the ith spot produces two outputs:

3.1Zero-th spot

We build our intuition for implementing a 1-bit adder by considering the adder in the zero-th spot (least significant stage). This computes the least significant result bit, y0\texttt{y}_{\texttt{0}}, and the carry-out bit of the least significant stage, c1\texttt{c}_{\texttt{1}}.

"TODO"

Figure 2:1-bit adder in zero-th spot, without carry-in bit.

By inspection of the truth table in Table 1, we can directly write the logic equations for this stage[3].

Table 1:Truth table for 1-bit adder in zero-th spot.

a0\texttt{a}_{\texttt{0}}b0\texttt{b}_{\texttt{0}}y0\texttt{y}_{\texttt{0}}c1\texttt{c}_{\texttt{1}}
0000
0110
1010
1101
s0=a0  ˆ b0c1=a0 & b0\begin{aligned} \\ \\ \\ \texttt{s}_{\texttt{0}} &= \texttt{a}_{\texttt{0}} \texttt{ \^ \ } \texttt{b}_{\texttt{0}} \\ \texttt{c}_{\texttt{1}} &= \texttt{a}_{\texttt{0}} \texttt{ \& } \texttt{b}_{\texttt{0}} \end{aligned}

3.2First spot

Next, consider the logic for the next significant stage (and all subsequent stages):

"TODO"

Figure 3:1-bit adder in first spot, with carry-in bit.

Even though the truth table is larger, the logic equations are still straightforward to write—at least, if we remember what we learned from previous sections.

Table 2:Truth table for 1-bit adder in first spot.

a1\texttt{a}_{\texttt{1}}b1\texttt{b}_{\texttt{1}}c1\texttt{c}_{\texttt{1}}y1\texttt{y}_{\texttt{1}}c2\texttt{c}_{\texttt{2}}
00000
00110
01010
01101
10010
10101
11001
11111
y1=XOR(a1,b1,c1)c2=MAJ(a1,b1,c1)=a1b1+a1c1+b1c1\begin{aligned} \\ \\ \\ \\ \\ \begin{aligned} \texttt{y}_{\texttt{1}} &= \texttt{XOR}(\texttt{a}_{\texttt{1}}, \texttt{b}_{\texttt{1}}, \texttt{c}_{\texttt{1}}) \\ \texttt{c}_{\texttt{2}} &= \texttt{MAJ}(\texttt{a}_{\texttt{1}}, \texttt{b}_{\texttt{1}}, \texttt{c}_{\texttt{1}}) \\ &= \texttt{a}_{\texttt{1}}\texttt{b}_{\texttt{1}} + \texttt{a}_{\texttt{1}}\texttt{c}_{\texttt{1}} + \texttt{b}_{\texttt{1}}\texttt{c}_{\texttt{1}} \end{aligned} \end{aligned}

3.3General case

Let us finally consider the logic for the ith spot, where i=0,...,n1i = 0, ..., n-1 for an nn-bit wide addition. We will encapsulate the operations of one stage, or column, of the add operation into a small block, shown in Figure 4:

"TODO"

Figure 4:1-bit adder in ith spot, with carry-in bit.

You can think of this block as a 1-bit adder. It’s official name is a full-adder cell, but most people confuse this with an n-bit adder, which it certainly is not.

The 1-bit adder’s gate-level circuit diagrams for its internals are in Figure 5. The logic equation can be pattern-matched from our earlier discussion and is Equation (2).

"TODO"

Figure 5:1-bit-adder circuit.

yi=XOR(ai,bi,ci)ci+1=MAJ(ai,bi,ci)=aibi+aici+bici\begin{aligned} \\ \\ \\ \begin{aligned} \texttt{y}_{\texttt{i}} &= \texttt{XOR}(\texttt{a}_{\texttt{i}}, \texttt{b}_{\texttt{i}}, \texttt{c}_{\texttt{i}}) \\ \texttt{c}_{\texttt{i+1}} &= \texttt{MAJ}(\texttt{a}_{\texttt{i}}, \texttt{b}_{\texttt{i}}, \texttt{c}_{\texttt{i}}) \\ &= \texttt{a}_{\texttt{i}}\texttt{b}_{\texttt{i}} + \texttt{a}_{\texttt{i}}\texttt{c}_{\texttt{i}} + \texttt{b}_{\texttt{i}}\texttt{c}_{\texttt{i}} \end{aligned} \\ \\ \\ \end{aligned}

4N-bit Adder

The next step in the design of our adder circuit is to wire together a collection of our 1-bit adders to create an n-bit adder. All we need to do is to wire the carry-out output of one stage into the carry-in input of the next, from least significant to most, as shown in Figure 6:

"TODO"

Figure 6:(Incomplete) Cascading n-bit adder. The carry-in bit c0 is set to GND, but the carry-out bit cn is currently unused.

Inputs are applied at the top and after some delay, associated with the delay through the logic gates for the blocks, and the delay of the carry from stage to stage, the final result appears at the bottom.

What to do with carry-in bit c0? For the adder to produce the correct result, convince yourself that c0 must be connected to a source of logic 0 (GND in the circuit). But we will find something more interesting to do for c0 when we discuss the subtractor below.

What to do with carry-out bit cn? The carry-out from the most significant stage, cn, can be used as the (n+1)th bit of the result (remember, adding two n-bit numbers could result in a (n+1)-bit result). However, in most uses for an adder, we must generate an n bit result. For instance, RV32I registers can only store 32 bits, so the result of the add instruction must be a 32-bit number. In this case, we must signal overflow, which we discuss next.

4.1Overflow

As discussed in our initial adder/subtractor design, an overflow output must be generated to indicate when the result cannot fit into n bits. Recall from an earlier section that overflow can occur when adding a pair of negative numbers or when adding a pair of positive numbers.

We can make the following observations about adds in this circuit. Remember, the most significant stage is the stage associated with the sign bit, i.e., stage n-1.

We demonstrate this intuition in Figure 7 for a 2-bit adder.

"TODO"

Figure 7:2-bit adder overflow table diagram. Overflow occurs when adding 1+(2)=11 + (-2) = -1, 1+11 + 1, and (1)+(2)(-1) + (-2). Notably, the first is a valid addition, where the latter two produce incorrect results.

Based on these observations, we can design a simple circuit that generates the overflow output signal by comparing the carry-in and carry-out of stage n-1. A simple circuit that indicates when two signals are different in value is the XOR gate.

overflow=cnXORcn-1\texttt{overflow} = \texttt{c}_{\texttt{n}} XOR \texttt{c}_{\texttt{n-1}}

Figure 9 shows our full n-bit adder:

"TODO"

Figure 9:Cascading n-bit adder with overflow.

5Subtractor

We mentioned earlier that addition and subtraction are closely related, and therefore we would expect that their respective circuits are similar and could serve dual both purposes.

As discussed in our initial adder/subtractor design, a control bit SUB signals if the add/subtract block should perform subtraction. We note the following holds, due to two’s complement:

This last observation gives us a very clean way to augment our adder circuit to be a combined adder and subtractor:

  1. Add 1 to the result.

  2. Invert the bits of B.

The augmented adder design is shown below. When the input SUB is 1 the block performs subtraction, when SUB=0 the block performs addition.

"TODO"

Figure 10:N-bit adder/substractor design circuit diagram.

Footnotes
  1. Wikipedia: Straw Man

  2. As discussed in the previous chapter: Start with a truth table, write the canonical Boolean equation, then simplify.

  3. ^: XOR, &: AND