Note that the memory is word-addressable, meaning given an address, it will return 4-byte data.
Your processor will have a 2-stage pipeline:
- Instruction Fetch: An instruction is fetched from the instruction memory. (Note: while you can, please do not calculate jump address
in this stage. Instead, you should try to deal with the jump control hazard.)
- Execute: The instruction is decoded, executed, and committed (written back). This is a combination of the remaining stages of a normal
five-stage RISC-V pipeline.
First, make sure you understand what hazards you will have to deal with.
The instruction immediately after a branch or jump is not executed if a branch is taken. This makes your task a bit more complex. By the time you have figured out that a branch or jump is
in the execute stage, you have already accessed the instruction memory and pulled out (possibly) the wrong instruction. You will therefore need
to "kill" instruction that is being fetched if the instruction under execution is a jump or a taken branch. Instruction
kills for this project MUST be accomplished by MUXing a nop
into the instruction stream and sending the nop
into the Execute
stage instead of using the fetched instruction. Notice that 0x00000013, or addi x0, x0, 0
is a nop
instruction; you should only kill if a branch is taken (do not kill otherwise). Do kill on every type of jump.
Do not solve this issue by calculating branch offsets in the IF stage. Because we test your output against the reference every cycle,
and the reference returns a nop, while it may be a conceptually correct solution, this will cause you to fail our tests.
Because all of the control and execution is handled in the Execute stage, your processor should be more or less indistinguishable from a
single-cycle implementation, barring the one-cycle startup latency and the branch/jump delays. However, we will be enforcing the two-stage
pipeline design. Some things to consider:
- Will the IF and EX stages have the same or different PC values?
- Do you need to store the PC between the pipelining stages?
- To MUX a nop into the instruction stream, do you place it before or after the instruction register?
- What address should be requested next while the EX stage executes a nop? Is this different than normal?
You might also notice a bootstrapping problem here: during the first cycle, the instruction register sitting between the pipeline stages won't
contain an instruction loaded from memory. How do we deal with this? It happens that Logisim automatically sets registers to zero on reset; the
instruction register will then contain a nop. We will allow you to depend on this behavior of Logisim. Remember to go to
Simulate --> Reset Simulation (Ctrl+R) to reset your processor.
Getting Started: A Guide
We know that trying to build a CPU with a blank slate might be intimidating, so we want to guide you through how to think about this project
by implementing a simple I-type instruction, addi.
Recall the five stages of the CPU pipeline:
- Instruction Fetch
- Instruction Decode
- Write Back
This guide will help you work through each of these stages, as it pertains to the add instruction. Each section will contain
questions for you to think through and pointers to important details, but it won't tell you exactly how to implement the instruction.
You may need to read and understand each question before going to the next one, and you can see the answers by clicking
on the question. During your implementation, feel free to place things in subcircuits as you see fit.
Stage 1: Instruction Fetch
The main thing we are concerned about in this stage is: how do we get the current instruction? From lecture, we know that instructions are
stored in the instruction memory, and each of these instructions can be accessed through an address.
Which file in the project holds your instruction memory? How does it connect to your cpu.circ file?
The instruction memory is the ROM module in run.circ. It provides an input into your CPU named "Instruction" and takes an
output from your CPU named "fetch_addr".
In your CPU, how would changing the address you output to fetch_addr affect the instruction input?
The instruction that run.circ outputs to your CPU should be the instruction at address fetch_addr in instruction memory.
How do you know what the fetch_addr should be? (Hint: it is also known as PC)
fetch_addr is the address of the current instruction being executed, so it is saved in the PC register. For this project,
it's fine for the PC to start at 0, and that is the default value for registers.
For this project, does your PC hold an address of a byte or a word?
If you look in run.circ, you will see that the address coming from your CPU goes straight into the memory module; the bits that
cut off by the splitter are the upper 18 bits of the address. So fetch_addr is a word address. Whether PC is a word or a byte address
is up to you as the hardware designer, but keep in mind that instructions that write PC + 4 back to the RegFile are treating PC like a byte address.
For basic programs without any jumps or branches, how will the PC change from line to line?
The PC must increment by 1 instruction in order to go to the next instruction, as the address held by the PC register represents what
instruction to execute.
We have provided the PC register in the cpu.circ file. Please implement the PC's behavior for simple programs - ignoring jumps
and branches. You will have to add in the latter two in the project, but for now we are only concerned with being able to run strings
of addi instructions. Where should the output of the PC register go? Remember to connect the clock! Remember that we're implementing
a 2-stage pipelined processor, so the IF stage is separate from the remaining stages. What circuitry separates the different stages of a pipeline? Do
you need to add anything?
Stage 2: Instruction Decode
Now that we have our instruction coming from the instruction input, we have break it down in the Instruction Decode step,
according to the RISC-V instruction formats you have learned.
What type of instruction is addi? What are the different bit fields and which bits are needed for each?
I type. The fields are:
- imm [31-20]
- rs1 [19-15]
- funct3 [14-12]
- rd [11-7]
- opcode [6-0]
In Logisim, what tool would you use to split out different groups of bits?
Implement the instruction field decode stage using the instruction input. You should use tunnels to label and group the bits.
Now we need to get the data from the corresponding registers, using the register file. Which instruction fields should be connected
to the register file? Which inputs of the register file should it connect to?
Instruction field rs1 will need to connect to read register 1.
Implement reading from the register file. You will have to bring in your RegFile from Project 3-1. Remember to connect
What does the Immediate Generator need to do?
For addi, the immediate generator takes in 12 bits and produces a signed 32-bit immediate. We highly suggest you create an Immediate Generator subcircuit!
Stage 3: Execute
The Execute stage, also known as the ALU stage, is where the computation of most instructions is performed. This is also where we will introduce
the idea of using a Control Module.
For the add instruction, what should be your inputs in to the ALU?
Read Data 1 and the immediate produced by the Immediate Generator.
In the ALU, what is the purpose of ALU_Sel?
It chooses which operation the ALU should perform.
Although it is possible for now to just put a constant as the ALUSel, why would this be infeasible as you need to implement more instructions?
With more instructions, the input to the ALU might need to change, so you would need to have some sort of circuit that changes ALUSel
depending on the instruction being executed.
Create a new subcircuit for the Control Module. This module will need to take in as inputs the opcode, funct3, and funct7, and (for now) use these to output a
value for ALUSel, depending on what the current instruction is. Read over the "Control" section above for some suggestions about different
approaches for doing this. As you implement more instructions, this circuit will have to expand and become more complex.
Bring in your ALU and connect the ALU inputs correctly. Do you need to connect the clock? Why or why not?
Stage 4: Memory
The memory stage is where the memory can be written to using store instructions and read from using load instructions. Because the addi
instruction does not use memory, we will not spend too much time here.
Bring in the MEM module that we provided. At this point, we cannot connect most of the inputs, as we don't know where they should come
from. However, you can still connect the clock.
Stage 5: Write back
The write back stage is where the results of the operation is saved back to the registers. Although not all instructions will write back to
the register file (can you think of some which do not?), the addi instruction does.
Looking at the entire ISA, what are some of the instructions that will write back to a register? Where in the datapath would it get
addi is an example that will take the output from the ALU and write it back. lhu will take the output from MEM and write
it to a register.
Let's create the write back phase so that it is able to write both ALU and MEM outputs to the Register File. Later, when you implement
branching/jumping, you may need to add more to this mux. However, at the moment, we need to choose between the ALU and MEM outputs, as only
one wire can end up being an input to the register file. Bring a wire from both the ALU and MEM, and connect it to a MUX.
What should you use as the Select input to the MUX? What does the input depend on?
This input should be able to choose between the two MUX inputs, ALU and MEM, which means that its value depends on which instruction is
executing. This suggests that the input should originate from the Control Module, as the Control Module is responsible for figuring out
which instruction is executing. We'll need another control signal here, commonly called WBSel. WBSel determines
which value to write back to the RegFile.
Now that we have the inputs to the MUX sorted out, we need to wire the output. Where should the output connect to?
Because the output is the data that you want to write into the Register File, it should connect to the Write Data input on the Register File.
There are two more inputs on the Register File which are important for writing data: RegWEn and rd. One of these will come from the Instruction Decode stage
and the other one will be a new control signal that you need to design. Please finish off the Writeback stage by these inputs on the RegFile correctly.
If you have done all of the following steps correctly, you should have a processor that hatorks for addi instructions. You can run ./cpu-sanity.sh and
see if it's working correctly! You should pass the first test: CPU-addi. For the rest of the project, you will be implementing more instructions in much the same way--connecting outputs to
inputs, adding MUXes and other Logisim components, and defining new control signals. Hopefully, this will be an easier task now that you have a
basic skeleton to work off of. Good luck!
We've included 6 sanity tests for you with the starter code. You can run them using ./cpu-sanity.sh.
Understanding how the tests work
Each test is a copy of the run.circ file included with the starter code that has instructions loaded into its IMEM. When you run logisim-evolution from the command line,
the clock ticks, the program counter is incremented, and the values in each of the outputs is printed to stdout.
Let's take as an example one of the 4 sanity tests included with the starter code, CPU_addi.circ. This is a very simple test that has 3 addi instructions
(addi t0, x0, 5, addi t1, t0, 7, addi s0, t0, 9), meant for a first sanity check after you go through the "Getting Started" guidelines above.
If you open CPU_addi.circ in Logisim Evolution, it'll look like this:
Let's take a closer look at the various parts of the test file. At the top, you'll see the place where your CPU is connected to the test outputs. With the skeleton file,
you'll see all xxxx's, as you do below; when your CPU is working, this should not be the case. Your CPU takes in one input (instruction), and along with the values in each of the registers,
it has one additional output: fetch_addr, or the address of the instruction to be fetched from IMEM to be executed the next clock cycle.
Be careful that you don't move any of the inputs/outputs of your CPU around, or add any additional inputs/outputs. This will change the shape of the CPU subcircuit, and as a result
the connections in the test files may no longer work properly.
Below the CPU, you'll see instruction memory. The hex for the 3 addi instructions (0x00500293, 0x00728313, 0x00928413) has been loaded into instruction memory. Instruction memory takes in one
input (called fetch_addr) and outputs the instruction at that address. fetch_addr is a 32-bit value, but because Logisim Evolution caps the size of ROM units at 2^16B, we have to
use a splitter to get only the bottom 14 bits of fetch_addr. Notice that fetch_addr is a word address, not a byte address.
So what happens when the clock ticks? Each tick of the clock increments an input in the test file called Time_Step. The clock will continue to tick until Time_Step is equal to the halting
constant for that test file (for this particular test file, the halting constant is 5). At that point, the Logisim Evolution command line will print the values in each of your outputs to stdout. Our tests will compare
this output to the expected; if your output is different, you will fail the test.
Six sanity tests have been included for you with the starter code: CPU-addi.circ, CPU-add_lui_sll.circ, CPU-mem.circ, CPU-branch.circ, CPU-br_jalr.circ, and CPU-jump.circ. You can see the .s files and hex corresponding to each of these tests in the tests/input directory. Run them using the following command from your main Project 3-2 directory:
Like in Project 3-1, we've included a Python script to make it a bit easier to interpret your test output. It's called binary_to_hex.py. It is in the tests/circ_files directory. You can run it on the reference output for CPU-addi.circ with the following command:
$ python binary_to_hex.py reference_output/CPU-addi.out
or, on your CPU's output with the following command:
$ python binary_to_hex.py output/CPU-addi.out
Writing your own tests
The autograder tests fall into 3 main categories: unit tests, integration tests, and edge case tests. We won't be revealing to you what these tests
are specificaly, but you should be able to re-create a very close approximation of them on your own in order to test your datapath.
What is a unit test? A unit test exercises your datapath with a single instruction, to make sure that each individual instruction has been implemented and is working
as expected. You should write a different unit test for every single instruction that you need to implement, and make sure that you test the spectrum of possibilities for that instruction thoroughly.
For example, a unit test slt should contain a case both where rs1 < rs2 and where rs1 !< rs2.
What is an integration test? After you've passed your unit tests, move onto tests that use multiple functions in combination. Try out various simple RISC-V programs that run a single function; your CPU should
be able to handle them, if working properly. Feel free to use riscv-gcc to compile C programs to RISC-V, but be aware of the limited instruction set we're working with (you don't have any ecall instructions, for example).
Finally, edge cases! What edge cases should you look for? A hint from us: our 2 main classes of edge cases come from memory operations and branch/jump operations (we call them something along the lines of
"mem-full" and "br-jump-edge"). Think about all the different ways these operations could go wrong.
Creating your tests
For this project, we will not be releasing all the tests. However, we have included some scripts to make test creation a little bit easier on you. Included in the starter code is a file called create_test.py. It expects 1 argument: .s files containing the RISC-V code you wish to test. The script will then generate copies of run.circ with your new tests loaded in for you, along with some other files. For everything to work properly:
- Write your tests and name them whatever you would like, but be sure to save them as .s files
- Don't move the tests anywhere, keep them (as well as create_test.py) in the root directory of the project
$ python create_test.py <test 1 name here>.s <test 2 name here>.s ...
Your file hierarchy should now contain some new things:
-- <test name here>.s # Your test
-- <alu, cpu, mem, regfile>.circ # All your circuits
-- CPU-<test name here>.circ # The new circuit containing your test
-- <test name here>.hex # The hex dissasembly of your test
-- <test name here>.s # A copy of your test
Now that everything is created, to test your own tests, run:
If you wish to delve into your the circuit running your test, you can simulate it by opening up the CPU-<test name here>.circ file. If you don't remember how to simulate your circuit, please refer back to the Logism labs. We highly encourage you to poke your circuit while the test is running to observe how your circuit reacts to various inputs (perhaps this can give you ideas for new tests to write).
If you wish to simulate your code only for a certain number of cycles, you can do that by running the following:
$ python create_test.py <test name here>.s -n <number of cycles>
If you would like to decode your output, use the provided binary_to_hex.py on the appropriate .out file in the output folder shown above. Be aware that because you're implementing a 2-stage pipelined processor and the first instruction writes on the rising edge of the second clock cycle, the effects of your instructions will have a 2 instruction delay. For example, let's say I have written a test with one instruction:
$ addi t0, x0, 1
The output will actually come out to be:
Note how t0 doesn't get changed until line 3. NOTE: This testing harness assumes you have the 2-stage pipeline implemented. You should first concern yourself with getting the single-cycle working, then the 2-stage, then the sanity tests, and then your own tests. The sanity tests do NOT test every instruction mentioned in the spec, so make sure you are writing extra tests!
Our testing harness is using some features of Venus that are still in beta. If you have find any bugs, please let us know on Piazza. Thank you!