Different methods of implementation of RISC-V ( Reduced Instruction Set Computing ) Integer Edition with all the instructions along with the Control Status Register with different methods of modelling and pipeline
The Arithmetic Logic Unit (ALU) is a crucial component of a processor, responsible for executing arithmetic, logical, and shift operations. This ALU is designed to support R-Type Instructions in RISC-V, handling 10 different operations.
The ALU performs three categories of operations:
| Instruction | Operation | Description |
|---|---|---|
| ADD | A + B |
Addition |
| SUB | A - B |
Subtraction |
| SLT | (A < B) ? 1 : 0 |
Set Less Than (Signed) |
| SLTU | (unsigned(A) < unsigned(B)) ? 1 : 0 |
Set Less Than Unsigned |
| Instruction | Operation | Description |
|---|---|---|
| XOR | A ^ B |
Bitwise XOR |
| OR | `A | B` |
| AND | A & B |
Bitwise AND |
| Instruction | Operation | Description |
|---|---|---|
| SLL | A << B |
Shift Left Logical |
| SRL | A >> B |
Shift Right Logical |
| SRA | A >>> B |
Shift Right Arithmetic |
Each R-Type instruction is uniquely identified by the funct3 field in the instruction format.
| Instruction | Funct3 |
|---|---|
| ADD | 000 |
| SUB | 000 |
| SLL | 001 |
| SLT | 010 |
| SLTU | 011 |
| XOR | 100 |
| SRL | 101 |
| SRA | 101 |
| OR | 110 |
| AND | 111 |
Some instructions share the same funct3 encoding. The difference is determined by bit 5 of funct7.
| Instruction Pair | Funct3 | funct7[5] | Operation |
|---|---|---|---|
| ADD vs SUB | 000 | 0 → ADD | 1 → SUB |
| SRL vs SRA | 101 | 0 → SRL | 1 → SRA |
- If funct7[5] = 0, the instruction is ADD or SRL.
- If funct7[5] = 1, the instruction is SUB or SRA.
- SUB does not exist in I-Type instructions.
- In I-Type instructions, the
funct7[5]bit is part of the immediate field, notfunct7. - This means
funct7[5] = 1does not indicate a subtraction.
To implement these operations, the ALU should have:
- Arithmetic Unit: Handles ADD, SUB, SLT, SLTU operations.
- Logical Unit: Performs XOR, OR, AND.
- Shifter Unit: Executes SLL, SRL, SRA.
- Multiplexers: Select between different results based on
funct3andfunct7[5].
The ALU control logic is designed based on funct3 and funct7[5].
| Funct3 | Funct7[5] | ALU Operation |
|---|---|---|
| 000 | 0 | ADD |
| 000 | 1 | SUB |
| 001 | X | SLL |
| 010 | X | SLT |
| 011 | X | SLTU |
| 100 | X | XOR |
| 101 | 0 | SRL |
| 101 | 1 | SRA |
| 110 | X | OR |
| 111 | X | AND |
"X"means the value offunct7[5]does not affect the instruction.
- ADD/SUB:
Result = A + (funct7[5] ? ~B + 1 : B); - SLT/SLTU:
Result = (signed(A) < signed(B)) ? 1 : 0; - XOR, OR, AND: Direct bitwise operations.
- SLL/SRL/SRA: Use the shift unit with different logic.
The ALU design consists of:
- Multiplexers to choose between operations.
- Arithmetic Logic Circuits for addition, subtraction, and comparisons.
- Logical Circuits for bitwise operations.
- Shift Circuits for shifting operations.
Each operation is implemented using basic logic gates:
- Addition/Subtraction: Full adder circuits.
- Bitwise Logic Operations: AND, OR, XOR gates.
- Shift Operations: Barrel shifters for SLL, SRL, and SRA.
A sample Verilog implementation of the ALU based on the above logic would involve:
- Case statements to select operations.
- Bitwise operations for logical instructions.
- Shift operations using shift operators.
- Multiplexers for ADD/SUB and SRL/SRA differentiation.
The ALU should be tested with:
- All possible funct3 values.
- Different values of funct7[5] to differentiate ADD/SUB and SRL/SRA.
- Signed and unsigned comparisons for SLT and SLTU.
- Shifting edge cases (e.g., shifting by 0 or 31 bits).
- Logical operations with all 1s and 0s to check correctness.
A Verilog testbench can be created to verify each instruction using assertions and test vectors.
- This ALU efficiently supports all R-Type Instructions in RISC-V.
- Uses funct3 and funct7[5] to differentiate between similar instructions.
- Implements arithmetic, logical, and shift operations using minimal hardware.
- Can be integrated into a RISC-V processor pipeline to execute ALU operations efficiently.
