From dd7f4c2a03a6be4db70c5653b7bdebfd3023dac3 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Mon, 16 Mar 2026 16:30:18 +0800 Subject: branch unit and test --- rv64cpu/rtl/core/branch_unit.sv | 52 ++++++++++++++++++ rv64cpu/rtl/core/include/rv64_pkg.sv | 13 +++++ rv64cpu/tb/CMakeLists.txt | 9 ++++ rv64cpu/tb/unit/tb_branch.cc | 100 +++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 rv64cpu/rtl/core/branch_unit.sv create mode 100644 rv64cpu/tb/unit/tb_branch.cc diff --git a/rv64cpu/rtl/core/branch_unit.sv b/rv64cpu/rtl/core/branch_unit.sv new file mode 100644 index 0000000..c9662ce --- /dev/null +++ b/rv64cpu/rtl/core/branch_unit.sv @@ -0,0 +1,52 @@ +// Richard CPU +// Copyright (C) 2026 Richard Qin + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// File path: rtl/core/branch_unit.sv +// Description: Branch Unit, responsible for determining whether conditional jump instructions are effective + +module branch_unit + import rv64_pkg::*; +( + input logic is_branch, // Indicates whether the current instruction is a branch instruction + input logic [2:0] branch_op, // Specific branch types func3 + input logic [XLEN-1:0] operand_a, // operand 1 + input logic [XLEN-1:0] operand_b, // operand 2 + output logic branch_taken // Branch taken signal +); + + always_comb begin + branch_taken = 1'b0; + + if (is_branch) begin + case (branch_op) + // 3'b000: BEQ (Branch if Equal) + BRANCH_EQ: branch_taken = (operand_a == operand_b); + // 3'b001: BNE (Branch if Not Equal) + BRANCH_NE: branch_taken = (operand_a != operand_b); + // 3'b100: BLT (Branch if Less Than, Signed) + BRANCH_LT: branch_taken = ($signed(operand_a) < $signed(operand_b)); + // 3'b101: BGE (Branch if Greater Than or Equal, Signed) + BRANCH_GE: branch_taken = ($signed(operand_a) >= $signed(operand_b)); + // 3'b110: BLTU (Branch if Less Than, Unsigned) + BRANCH_LTU: branch_taken = (operand_a < operand_b); + // 3'b111: BGEU (Branch if Greater Than or Equal, Unsigned) + BRANCH_GEU: branch_taken = (operand_a >= operand_b); + default: branch_taken = 1'b0; + endcase + end + end + +endmodule diff --git a/rv64cpu/rtl/core/include/rv64_pkg.sv b/rv64cpu/rtl/core/include/rv64_pkg.sv index 984a93b..372cc1b 100644 --- a/rv64cpu/rtl/core/include/rv64_pkg.sv +++ b/rv64cpu/rtl/core/include/rv64_pkg.sv @@ -92,4 +92,17 @@ package rv64_pkg; ALU_AND = 4'b0111 // And } alu_op_t; + + // Branch Operation Encoding + // Derived from funct3 of B-type instructions + typedef enum logic [2:0] { + BRANCH_EQ = 3'b000, // BEQ: Branch if Equal + BRANCH_NE = 3'b001, // BNE: Branch if Not Equal + BRANCH_LT = 3'b100, // BLT: Branch if Less Than (signed) + BRANCH_GE = 3'b101, // BGE: Branch if Greater Than or Equal (signed) + BRANCH_LTU = 3'b110, // BLTU: Branch if Less Than (unsigned) + BRANCH_GEU = 3'b111 // BGEU: Branch if Greater Than or Equal (unsigned) + } branch_op_t; + endpackage + diff --git a/rv64cpu/tb/CMakeLists.txt b/rv64cpu/tb/CMakeLists.txt index 42ed2c0..4bafc67 100644 --- a/rv64cpu/tb/CMakeLists.txt +++ b/rv64cpu/tb/CMakeLists.txt @@ -56,6 +56,15 @@ add_verilator_target( INCLUDE_DIRS ${RTL_COMMON_INCLUDES} TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_regfile.cc ) + +add_verilator_target( + NAME tb_branch + TOP_MODULE branch_unit + SOURCES ${RTL_CORE_DIR}/include/rv64_pkg.sv + ${RTL_CORE_DIR}/branch_unit.sv + INCLUDE_DIRS ${RTL_COMMON_INCLUDES} + TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_branch.cc +) # # add_verilator_target( # NAME tb_csr diff --git a/rv64cpu/tb/unit/tb_branch.cc b/rv64cpu/tb/unit/tb_branch.cc new file mode 100644 index 0000000..e47bf98 --- /dev/null +++ b/rv64cpu/tb/unit/tb_branch.cc @@ -0,0 +1,100 @@ +// Richard CPU +// Copyright (C) 2026 Richard Qin +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// File path: tb/unit/tb_branch.cc +// Description: Branch Unit test + +#include +#include +#include +#include +#include +#include "Vbranch_unit.h" + +// Enums must match rv64_pkg::branch_op_t +enum BranchOp { + BRANCH_EQ = 0b000, + BRANCH_NE = 0b001, + BRANCH_LT = 0b100, + BRANCH_GE = 0b101, + BRANCH_LTU = 0b110, + BRANCH_GEU = 0b111 +}; + +void test_branch(Vbranch_unit* dut, uint8_t is_branch, uint8_t op, uint64_t a, uint64_t b, uint8_t expected, const char* name) { + dut->is_branch = is_branch; + dut->branch_op = op; + dut->operand_a = a; + dut->operand_b = b; + dut->eval(); + + if (dut->branch_taken == expected) { + std::cout << "\033[32m[PASS] " << name << " | Expected: " << (int)expected << " Got: " << (int)dut->branch_taken << "\033[0m" << std::dec << std::endl; + } else { + std::cout << "\033[31m[FAIL] " << name << " | Expected: " << (int)expected << " Got: " << (int)dut->branch_taken << "\033[0m" << std::endl; + assert(false); + } +} + +int main(int argc, char** argv) { + Verilated::commandArgs(argc, argv); + Vbranch_unit* dut = new Vbranch_unit; + + std::cout << "----------------------------------" << std::endl; + std::cout << " Branch Unit Test" << std::endl; + std::cout << "----------------------------------" << std::endl; + + // --- Not a branch --- + test_branch(dut, 0, BRANCH_EQ, 10, 10, 0, "Not_A_Branch"); + + // --- BEQ / BNE --- + test_branch(dut, 1, BRANCH_EQ, 10, 10, 1, "BEQ_Equal"); + test_branch(dut, 1, BRANCH_EQ, 10, 20, 0, "BEQ_Not_Equal"); + test_branch(dut, 1, BRANCH_NE, 10, 20, 1, "BNE_Not_Equal"); + test_branch(dut, 1, BRANCH_NE, 10, 10, 0, "BNE_Equal"); + + uint64_t minus_one = (uint64_t)-1; // 0xFFFFFFFFFFFFFFFF + uint64_t one = 1; + + // --- BLT / BGE (Signed) --- + // -1 is less than 1 (Signed) + test_branch(dut, 1, BRANCH_LT, minus_one, one, 1, "BLT_Signed_Negative_vs_Positive"); + test_branch(dut, 1, BRANCH_GE, minus_one, one, 0, "BGE_Signed_Negative_vs_Positive"); + + // 1 is greater than -1 (Signed) + test_branch(dut, 1, BRANCH_LT, one, minus_one, 0, "BLT_Signed_Positive_vs_Negative"); + test_branch(dut, 1, BRANCH_GE, one, minus_one, 1, "BGE_Signed_Positive_vs_Negative"); + + // Equal signed + test_branch(dut, 1, BRANCH_LT, minus_one, minus_one, 0, "BLT_Signed_Equal"); + test_branch(dut, 1, BRANCH_GE, minus_one, minus_one, 1, "BGE_Signed_Equal"); + + // --- BLTU / BGEU (Unsigned) --- + // 0xFFFFFFFFFFFFFFFF is NOT less than 1 (Unsigned) + test_branch(dut, 1, BRANCH_LTU, minus_one, one, 0, "BLTU_Unsigned_Max_vs_1"); + // 0xFFFFFFFFFFFFFFFF is greater than or equal to 1 (Unsigned) + test_branch(dut, 1, BRANCH_GEU, minus_one, one, 1, "BGEU_Unsigned_Max_vs_1"); + + // Equal unsigned + test_branch(dut, 1, BRANCH_LTU, minus_one, minus_one, 0, "BLTU_Unsigned_Equal"); + test_branch(dut, 1, BRANCH_GEU, minus_one, minus_one, 1, "BGEU_Unsigned_Equal"); + + std::cout << "----------------------------------" << std::endl; + std::cout << "\033[32mAll tests passed!\033[0m" << std::endl; + std::cout << "----------------------------------" << std::endl; + + delete dut; + return 0; +} -- cgit v1.2.3