Stage 9: Full implemntation + passing tests
This commit is contained in:
@@ -6,8 +6,8 @@ import chisel3.util.is
|
||||
|
||||
object AluOpCode extends ChiselEnum {
|
||||
val Add, And, Or, Xor, ShiftLeft, ShiftRight, ShiftArithmeticRight,
|
||||
LessThanSigned, LessThanUnsigned, GreaterEqualSigned, GreaterEqualUnsigned,
|
||||
Equal = Value
|
||||
LessThanSigned, LessThanUnsigned, GreaterEqualSigned,
|
||||
GreaterEqualUnsigned, Equal = Value
|
||||
}
|
||||
|
||||
class Alu extends Module {
|
||||
@@ -22,7 +22,7 @@ class Alu extends Module {
|
||||
})
|
||||
|
||||
io.out := 0.U;
|
||||
io.flags := false.B;
|
||||
io.comp_result := false.B;
|
||||
switch(io.opcode) {
|
||||
is(Add) {
|
||||
io.out := io.a + io.b;
|
||||
@@ -60,25 +60,25 @@ class Alu extends Module {
|
||||
}
|
||||
|
||||
is(LessThanSigned) {
|
||||
val less_than := io.a.asSInt < io.b.asSInt
|
||||
val less_than = io.a.asSInt < io.b.asSInt
|
||||
io.out := less_than
|
||||
io.comp_result := less_than;
|
||||
}
|
||||
|
||||
|
||||
is(GreaterEqualUnsigned) {
|
||||
val geq_than = io.a >== io.b;
|
||||
val geq_than = io.a >= io.b;
|
||||
io.out := geq_than
|
||||
io.comp_result := geq_than;
|
||||
}
|
||||
|
||||
is(GreaterEqualSigned) {
|
||||
val geq_than := io.a.asSInt >== io.b.asSInt
|
||||
val geq_than = io.a.asSInt >= io.b.asSInt
|
||||
io.out := geq_than
|
||||
io.comp_result := geq_than;
|
||||
}
|
||||
|
||||
is(Equal) {
|
||||
io.comp_result := io.& === io.b;
|
||||
io.comp_result := io.a === io.b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@ class CUInterface extends Bundle {
|
||||
val instruction = Input(UInt(32.W))
|
||||
val reg_file_we = Output(Bool())
|
||||
val alu_opcode = Output(AluOpCode())
|
||||
// Alu comparison result
|
||||
val alu_comp_result = Input(Bool())
|
||||
|
||||
val optype = Output(OpType())
|
||||
|
||||
// True if the decoded instruction is a jump
|
||||
@@ -40,7 +43,7 @@ class CUInterface extends Bundle {
|
||||
}
|
||||
|
||||
object OpType extends ChiselEnum {
|
||||
val U, I, IR, J = Value
|
||||
val U, I, IR, J, B = Value
|
||||
}
|
||||
|
||||
object OpCode extends ChiselEnum {
|
||||
@@ -72,6 +75,9 @@ object OpCode extends ChiselEnum {
|
||||
|
||||
// Type I
|
||||
val JALR = "b1100111".U
|
||||
|
||||
// Type B
|
||||
val OpBranch = "b1100011".U
|
||||
}
|
||||
|
||||
class ControlUnit() extends Module {
|
||||
@@ -153,6 +159,11 @@ class ControlUnit() extends Module {
|
||||
io.mux_pcadder_writeback := false.B; // Set pc to writeback dest
|
||||
io.optype := OpType.I
|
||||
}
|
||||
|
||||
is(OpCode.OpBranch) {
|
||||
io.optype := OpType.B;
|
||||
opBranch(funct3, io);
|
||||
}
|
||||
}
|
||||
|
||||
// Implements functions for all instruction of the opImm kind
|
||||
@@ -304,4 +315,78 @@ class ControlUnit() extends Module {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implements functions for all instruction of the branch kind
|
||||
def opBranch(funct3: UInt, io: CUInterface) = {
|
||||
// Meaning of Funct3 in the context of an OpBranch instruction
|
||||
object Funct3 extends ChiselEnum {
|
||||
val BEQ = "b000".U
|
||||
val BNE = "b001".U
|
||||
val BLT = "b100".U
|
||||
val BGE = "b101".U
|
||||
val BLTU = "b110".U
|
||||
val BGEU = "b111".U
|
||||
}
|
||||
|
||||
val branch = WireInit(false.B);
|
||||
switch(funct3) {
|
||||
is(Funct3.BEQ) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.Equal
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := io.alu_comp_result
|
||||
}
|
||||
|
||||
is(Funct3.BNE) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.Equal
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := ~io.alu_comp_result
|
||||
}
|
||||
|
||||
// Signed comparison
|
||||
is(Funct3.BLT) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.LessThanSigned
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := io.alu_comp_result
|
||||
}
|
||||
|
||||
is(Funct3.BGE) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.LessThanSigned
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := ~io.alu_comp_result
|
||||
}
|
||||
|
||||
// Signed comparison
|
||||
is(Funct3.BLTU) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.LessThanUnsigned
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := io.alu_comp_result
|
||||
}
|
||||
|
||||
is(Funct3.BGEU) {
|
||||
// Send operands from registers to alu
|
||||
io.alu_opcode := AluOpCode.LessThanUnsigned
|
||||
io.mux_rega_pc := true.B;
|
||||
io.mux_regb_imm := true.B;
|
||||
branch := ~io.alu_comp_result
|
||||
}
|
||||
}
|
||||
|
||||
// Branch if comparison is successful
|
||||
when(branch) {
|
||||
io.mux_pcadder_writeback := true.B;
|
||||
io.mux_incrpc4_imm := false.B;
|
||||
io.mux_regpc_executepc := false.B;
|
||||
io.is_jump := true.B;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,14 @@ class ImmediateDecoder extends Module {
|
||||
io.instruction(30, 21),
|
||||
0.U
|
||||
);
|
||||
val imm_b = Cat(
|
||||
Fill(19, io.instruction(31)),
|
||||
io.instruction(31),
|
||||
io.instruction(7),
|
||||
io.instruction(30, 25),
|
||||
io.instruction(11, 8),
|
||||
0.U
|
||||
)
|
||||
|
||||
switch(io.op_type) {
|
||||
is(OpType.U) {
|
||||
@@ -42,5 +50,8 @@ class ImmediateDecoder extends Module {
|
||||
is(OpType.J) {
|
||||
io.immediate := imm_j
|
||||
}
|
||||
is(OpType.B) {
|
||||
io.immediate := imm_b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ class Rv32i(sim: Boolean = true) extends Module {
|
||||
val immediate_decoder = Module(new ImmediateDecoder());
|
||||
val control_unit = Module(new ControlUnit());
|
||||
|
||||
control_unit.io.alu_comp_result := alu.io.comp_result
|
||||
|
||||
// PC delayed to execute stage for auipc op
|
||||
val execute_pc = Delay.Delay(reg_pc, 1, 0.U);
|
||||
// True if the instruction in the execute stage is a jump
|
||||
|
||||
Reference in New Issue
Block a user