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

In this chapter, we will design a RISC-V processor and connect the software and hardware parts of our CS 61C story. In the below figure, this chapter designs the hardware that can actually execute RISC-V code.

"TODO"

Great Idea #1: Abstraction.

There are many such variations of RISC-V processor hardware; we will discuss one such processor this chapter, and a second one soon. There are many other designs of RISC-V chips on the market[1]. Ultimately, all RISC-V processors must correctly execute RISC-V instructions. Let’s see how!

"TODO"

“New-School” Machine Structures leverage parallelism in both software and hardware.

If we execute a single instruction on a single clock cycle, combinational logic blocks might be operating on garbage until they get the right values from state elements, and we will often need to wait until the end of the clock cycle before we can guarantee stable values for the next instruction. In the next chapter, we will discuss pipelining, which lets us execute multiple instructions in parallel.

2Datapath vs. Control

To build a processor, recall the basic computer layout below. The processor on the left of the figure is the active part of the computer that does all the work. It accesses memory to read/write data, it executes instructions, and it makes decisions on which instruction to execute next.

"TODO"

Basic computer layout from a previous chapter.

Inside the processor, there are two main components:

One analogy comes from the operation of the International Space Station (ISS). The space station is like the datapath that has all the hardware needed to perform operations in space. Mission control is on the Earth and, given some data from the space station, sends directions back to the ISS. Both the space station and mission control have computers (e.g., combinational logic) and state, and both are needed to ensure correct operation of the International Space Station as a system.

The RISC-V datapath is like a space station.

The RISC-V datapath is like a space station.

The RISC-V control logic is like mission control.

The RISC-V control logic is like mission control.

3Single-Cycle Processor

A CPU is a complex digital logic system that updates state using combinational logic. It has two types of elements (discussed in the next section):

Our goal for this chapter is to design a single-cycle processor that executes one instruction each cycle, starting from fetching the instruction all the way to updating the PC for the next instruction.

3.1The Processor as a State Machine?

Theoretically, to design our CPU we could implement something like Figure 1. We could make a “finite” state machine that considers every instruction as a separate state, then specifies combinational logic for transitioning to every other possible instruction.

"TODO"

Figure 1:A strawman, bulky approach to implementing our datapath. We discuss the state elements listed in the figure in the next section.

45 Steps to a RISC-V Instruction

Instead of the complicated FSM approach above, we will break up the process into five steps, then connect the steps to create the whole processor circuit. For each instruction, we will determine whether additional logic needs to be incorporated into each phase.

The benefit of this approach is two-fold:

  1. Smaller steps are easier to design

  2. Modularity means that we can optimize one step without touching the others.

Here are the five steps to executing a RISC-V instruction.

  1. Instruction Fetch (IF): Fetch the instruction from memory and increment the program counter.

  2. Instruction Decode (ID): Determine the operation from the bits of the instruction and read registers from the register file.

  3. Execute (EX): Use the Arithmetic Logic Unit (ALU) to perform the operation.

  4. Memory Access (MEM): Load data from memory, or store data[2] to memory.

  5. Write Back (WB): Write back[3] to the register file.

All phases of one RV32I instruction will execute within the same cycle[2][3]: Instruction Fetch (IF) starts on the first rising edge of a clock, and Write Back (WB) finishes[3] the final result on the next rising edge.

In the next section, we introduce the key elements of a RISC-V datapath. For now, we share Figure 2 and let you guess at the meaning of each hardware block.

Five steps of a single-cycle datapath. See this section for descriptions of each hardware block.

Figure 2:Five steps of a single-cycle datapath. See this section for descriptions of each hardware block.

We emphasize the layer of abstraction between datapath and control with Figure 3 below. The The control logic selects “needed” datapath lines based on the instruction. The control logic specifies if data needs to be written to memory or registers, which arithmetic or logical operation to execute, selectors for MUXes, etc.

As the datapath computes values, the control logic selects the necessary values needed to execute the instruction.

Figure 3:As the datapath computes values, the control logic selects the necessary values needed to execute the instruction.

Finally, it should be noted that this single-cycle approach is not practical, since the clock cycle must stretch to accommodate the longest instruction that takes all five steps. After designing the single-cycle datapath, we will look at faster implementations that leverage pipelining.

Footnotes
  1. See SiFive for some additional examples.

  2. It is more accurate to say that during MEM, we setup the data to store back to DMEM by ensuring that the input to DMEM is stable before the next rising edge of the clock. Then, on the rising edge, MEM stores the correct value to memory. See more: DMEM

  3. It is more accurate to say that during WB, we set up the value to write back to the Register File by ensuring that the input D of register rd is stable at setup time, before the next rising edge of the clock. Then, on the rising edge, the register rd takes this value, then after a clk-to-q delay, has the correct value on its output Q. See more: RegFile