CS 211: Assignment 4
Building a Simple Processor in CircuitVerse
Introduction
This project guides you through the step-by-step construction of a simplified processor ar- chitecture inspired by the MIPS instruction set. Unlike the complex and variable-length instructions of the x86 architecture (which we study in class), this processor follows a clean, RISC-style (Reduced Instruction Set Computer) design.
Key Features of This Design:
• Fixed-length 16-bit instructions for predictable decoding.
• Three instruction formats: R-type (register), I-type (immediate), and J-type (jump).
• 4-bit general-purpose registers for simplicity.
• Stepwise development over three phases: starting with basic arithmetic and grad- ually adding control flow and branching.
• Modular structure: built using subcircuits for ALU, Control Unit, Register File, and Program Counter.
This processor is intentionally simplified to reinforce fundamental processor concepts: in- struction decoding, ALU operation, register file management, branching, and memory ac- cess. It mirrors the design philosophy of MIPS — a classic teaching architecture — but uses a smaller data width (4-bit registers and immediates) and a compact instruction memory.
Comparison with x86:
• x86: CISC (Complex Instruction Set Computing) architecture, variable-length in- structions, many addressing modes.
• This Project: RISC-style, fixed-length instructions, clean control flow, ideal for learn- ing core concepts.
Please use the pdf about MIPS single cycle implementation for reference.
Phase 1: Building an R-Type Instruction Processor
Objective
In this phase, we will design and implement a processor capable of executing basic R-type in- structions using a custom 16-bit instruction format. The processor will follow the Fetch–De- code–Execute model and be built in CircuitVerse.
Instruction Format
R-type instructions are 16 bits wide and operate entirely on register operands. The instruc- tion is broken down as follows:
Bits
|
Field
|
Description
|
Width
|
15–12
|
Opcode
|
Operation type (ADD, SUB, etc.)
|
4 bits
|
11–8
|
Rd
|
Destination register
|
4 bits
|
7–4
|
Rs1
|
Source register 1
|
4 bits
|
3–0
|
Rs2
|
Source register 2
|
4 bits
|
Example: ADD R0, R1, R2 → Rd = R0, Rs1 = R1, Rs2 = R2 Supported Instructions (Phase 1)
Opcode (bin)
|
Mnemonic
|
Operation
|
0000
|
ADD
|
Rd ← Rs1 + Rs2
|
0001
|
SUB
|
Rd ← Rs1 - Rs2
|
0010
|
AND
|
Rd ← Rs1 AND Rs2
|
0011
|
OR
|
Rd ← Rs1 OR Rs2
|
0100
|
SLT
|
Rd ← (Rs1 < Rs2) ? 1 : 0
|
Processor Design: Fetch—Decode—Execute Cycle
1. Fetch:
• The Program Counter (PC) outputs a 4-bit address.
• This address is used to retrieve the 16-bit instruction from Instruction Memory (RAM).
2. Decode:
• A 16-bit splitter breaks the instruction into Opcode, Rd, Rs1, and Rs2 fields.
• These fields are routed to the Control Unit and Register File.
3. Execute:
• Values from Rs1 and Rs2 are fetched from the register file.
• The ALU performs the operation as specified by the Opcode.
• The result is written back to Rd.
• The PC is incremented to fetch the next instruction.
Implementation Steps in CircuitVerse
Step 1. Instruction Memory (ROM)
• Use a 16x16-bit RAM or ROM block.
• 4-bit address input comes from the PC.
• Outputs a 16-bit instruction.
Step 2. Program Counter (PC)
• 4-bit register to store the current instruction address.
• Connect to a 4-bit adder that increments the PC by 1 each clock cycle.
• Add a clock and reset input.
Step 3. Instruction Decoder
• Use a 16-bit splitter.
• Output 4-bit signals for Opcode, Rd, Rs1, and Rs2.
Step 4. Register File (4 registers: R0—R3)
• Create 4 registers, each 4 bits wide.
• Use two 2:4 decoders to select Rs1 and Rs2 (read ports).
• Use 4:1 multiplexers to read from Rs1 and Rs2.
• Another 2:4 decoder selects Rd (write port) for writing the result.
• Add write-enable logic from the Control Unit.
Step 5. Arithmetic Logic Unit (ALU)
• Inputs: Rs1 and Rs2 values.
• Output: Result sent to Rd.
• Operation is selected using the 4-bit Opcode.
• Support ADD, SUB, AND, OR, and SLT.
Step 6. Control Unit (Combinational Logic)
• Input: 4-bit Opcode.
• Outputs:
— ALU control signals.
— Register file write-enable signal.
• Implement using logic gates or multiplexers.
Step 7. Main Processor Circuit
• Integrate PC, RAM, Decoder, Register File, ALU, and Control Unit.
• Ensure all components are correctly synchronized using the system clock.
• Use output pins or LEDs to visualize register contents and ALU output.
Test Program (RAM Contents)
Instruction
|
Binary Description
|
LOADI R1, 3
|
0110 0001 0000 0011 (Preset value in register)
|
LOADI R2, 5
|
0110 0010 0000 0101 (Preset value in register)
|
ADD R0, R1, R2
|
0000 0000 0001 0010 R0 = R1 + R2
|
SUB R3, R2, R1
|
0001 0011 0010 0001 R3 = R2 - R1
|
AND R0, R1, R2
|
0010 0000 0001 0010 R0 = R1 AND R2
|
SLT R1, R1, R2
|
0100 0001 0001 0010 R1 = (R1 < R2) ? 1 : 0
|
Expected Behavior
• The PC should increment automatically every cycle.
• ALU should perform the correct operation according to the Opcode.
• The result of each instruction should be written to the Rd register.
• All register updates should reflect the expected instruction behavior.
Checklist Before Proceeding to Phase 2
R-type instructions load and execute correctly from RAM.
ALU performs correct operations for ADD, SUB, AND, OR, SLT.
PC increments properly each clock cycle.
Write-back to Rd is functional.
Test outputs match expected behavior.
Phase 2: Adding I-Type Instruction Support
Overview
Phase 2 extends the processor developed in Phase 1 to support I-type instructions. Unlike R-type instructions, which use two register operands, I-type instructions use a constant 4-bit immediate value in place of the second operand. These instructions enable:
• Loading constants into registers.
• Performing arithmetic using an immediate value.
• Implementing simple conditional branching (control flow).
I-Type Instruction Format
Bits
|
Field
|
Description
|
Width
|
15–12
11–8
7–4
3–0
|
Opcode
Rd or Rs
Rs or –
Imm4
|
Operation code (e.g., LOADI)
Register used for destination (LOADI, ADDI) or condition (BEQZ, BNEZ)
Optional source register (used in ADDI only)
4-bit unsigned immediate constant
|
4 bits
4 bits
4 bits
4 bits
|
Note: Rs is only relevant for ADDI, BEQZ, and BNEZ. It is ignored for LOADI.
Supported I-Type Instructions
Opcode (Binary)
|
Mnemonic
|
Operation
|
0110
|
LOADI
|
Rd ← Imm4
|
0111
|
ADDI
|
Rd ← Rs + Imm4
|
1000
|
BEQZ
|
If (Rs == 0) then PC ← PC + Imm4
|
1001
|
BNEZ
|
If (Rs 0) then PC ← PC + Imm4
|
Processor Modifications for I-Type Support
To implement I-type instruction functionality, the following hardware changes and enhance- ments are required:
Step 1. Immediate Extractor
• Extend the instruction decoder to extract bits 3–0 (Imm4).
• Route this 4-bit immediate to a new input line on the ALU-side MUX.
• Label clearly to avoid confusion with Rs2.
Step 2. ALU Input B Multiplexer (ALUSrc)
• Insert a 2:1 multiplexer before ALU input B.
• Input 0: Rs2 value (for R-type instructions).
• Input 1: Imm4 value (for I-type instructions).
• Controlled by a new signal: ALUSrc.
• ALUSrc = 0 → R-type; ALUSrc = 1 → I-type.
Step 3. Update Control Unit
• Recognize new opcodes: LOADI (0110), ADDI (0111), BEQZ (1000), BNEZ (1001).
• For each I-type opcode, generate appropriate control signals:
— ALUSrc = 1 for LOADI and ADDI.
— RegWrite = 1 for LOADI and ADDI.
— RegWrite = 0 for BEQZ and BNEZ.
— Branch = 1 only for BEQZ and BNEZ.