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

2Tracing the add Datapath

3Arithmetic Logic Unit (ALU)

In the previous chapter we implemented a basic four-operation ALU. In the full RISC-V implementation, our ALU (Figure 1) must support all operations for R-Type instructions:

ALU Block.

Figure 1:ALU Block.

Table 1:Signals for ALU Block

NameDirectionBit WidthDescription
AInput32Data to use for Input A in the ALU operation
BInput32Data to use for Input B in the ALU operation
ALUSelInput4Selects which operation the ALU should perform (see table-ops)
ALUResultOutput32Result of the ALU operation

3.1Course Project Details

Below, we detail the ALU operations that must be implemented for the course project’s datapath. We encourage revisiting this section after reading a few more example datapath traces.

Table 2:Operations for ALU Block for the course project

ALUSel Value
(for Project)
OperationALU Function
0addALUResult = A + B
1sllALUResult = A << B[4:0]
2sltALUResult = (A < B (signed)) ? 1 : 0
3Unused-
4xorALUResult = A ^ B
5srlALUResult = (unsigned) A >> B[4:0]
6orALUResult = A | B
7andALUResult = A & B
8mulALUResult = (signed) (A * B)[31:0]
9mulhALUResult = (signed) (A * B)[63:32]
10Unused-
11mulhuALUResult = (A * B)[63:32]
12subALUResult = A - B
13sraALUResult = (signed) A >> B[4:0]
14Unused-
15bselALUResult = B

Observations/reminders:

  • When performing shifts, only the lower 5 bits of B are needed, because only shifts of up to 32 are supported.

  • The comparator component might be useful for implementing instructions that involve comparing inputs. See the branch implementation later in this chapter.

  • A multiplexer (MUX) might be useful when deciding between operation outputs (recall our basic 4-operation ALU). Consider first processing the input for all operations first, and then outputting the one of your choice.

(sec-general-multiplication)

3.2General Multiplication

An ALU that implements the mul, mulh, and mulhu instructions can support parts of the RISC-V “M” extension.

InstructionNameDescriptionTypeOpcodeFunct3Funct7
mul rd rs1 rs2MULtiplyR[rd] = (R[rs1] * R[rs2])[31:0]R011 0011000000 0001
mulh rd rs1 rs2MULtiply Higher BitsR[rd] = (R[rs1] * R[rs2])[63:32] (Signed)R011 00110001000 0001
mulhu rd rs1 rs2MULtiply Higher Bits (Unsigned)R[rd] = (R[rs1] * R[rs2])[63:32] (Unigned)R011 0011011000 0001

The result of multiplying 2 32-bit numbers can be up to 64 bits of information, but we’re limited to 32-bit data lines, so mulh and mulhu are used to get the upper 32 bits of the product. The Multiplier component has a Carry Out output (with the description “the upper bits of the product”) which might be particularly useful for certain multiply operations.