目录 ← 首页
CS61C

Arithmetic Logic Unit

Learning Outcomes

  • Design a basic Arithmetic Logic Unit (ALU).
  • Use mux circuits in the ALU.

Most processor implementations include a special combinational logic block called an arithmetic logic unit (ALU). In RISC-V, the ALU is used to compute the result in the R-type instructions, such as, add, sub, and, or addi, ori, etc.

ALU Block

We are going to consider the design of a simpler version of the ALU than the one in our RISC-V processor. Ours will include only four basic functions:

  • ADD
  • SUB
  • (bitwise) AND
  • (bitwise) OR

This ALU is implemented as a combinational logic block in #fig-alu:

  • two 32-bit wide data inputs, A and B;
  • a 32-bit wide data output, R; and
  • a 2-bit wide control input, S

Basic ALU block symbol with 32-bit inputs A and B, 2-bit control S, and 32-bit result R. The black-box ALU symbol can consist of operations such as add, subtract, and, or.

Basic ALU: ADD, SUB, AND, and OR

In our basic ALU, S is used to select R as one of the four operations:

R={A + Bwhen S=00A - Bwhen S=01A & Bwhen S=10A | Bwhen S=11 \texttt{R} = \begin{cases} \texttt{A + B} & \text{when } \texttt{S} = 00 \\ \texttt{A - B} & \text{when } \texttt{S} = 01 \\ \texttt{A \& B} & \text{when } \texttt{S} = 10 \\ \texttt{A | B} & \text{when } \texttt{S} = 11 \\ \end{cases}

ALU Circuit

The internal design of our simple ALU is shown in #fig-alu-circuit:

Internal ALU datapath showing AND, OR, and add-subtract blocks in parallel. A 4-to-1 mux selects the final 32-bit result from the outputs of the parallel logic blocks.

Basic ALU circuit with three blocks (AND, OR, add/subtract) and a 4-to-1 mux.

For our simple ALU we will need an add/subtract block, an AND block, and an OR block. Each of these blocks will take two 32-bit inputs and produce a 32-bit output. Read more about implementing these blocks below.

Implementing the Internal Blocks

The logical operations as defined by the RISC-V ISA are bitwise operations.

  • AND: rir_i, is aia_i & bib_i, for the ii-th bits of the output R, A, and B, respectively. Perform this bitwise operation as a collection of 32 AND gates, where each AND gate is responsible for one of the 32 resultant bits.
  • OR: Similarly, the OR block is a collection of 32 OR gates.
  • The add/subtract block is a significantly more complex block than the AND or OR blocks; its design is the subject of the next section. For now, we note that a subtractor circuit is very similar to an adder, hence why we provide a single circuit that is capable of either operation.

case of the AND, the resultant bit ri is generated as ai AND bi. The circuit to perform this operation is simply a collection of 32 AND gates. Each AND gate is responsible for one of the 32 resultant bits. Similarly, the OR block is a collection of 32 OR gates.