diff --git a/CMakeLists.txt b/CMakeLists.txt index a39803b..657124c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.20) project(simulator) - +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) +endif() file(GLOB_RECURSE sources "src/*.cpp") set(CMAKE_CXX_STANDARD 20) diff --git a/include/alu.h b/include/alu.h index a628292..f2af75f 100644 --- a/include/alu.h +++ b/include/alu.h @@ -23,7 +23,7 @@ struct ALU : public dark::Module { // Constructor } void work() { - std::cerr << "ALU: cur request_full_id=" << std::hex << std::setw(8) << std::setfill('0') << std::uppercase + DEBUG_CERR << "ALU: cur request_full_id=" << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << static_cast(request_full_id) << " request_ROB_index=" << std::dec << static_cast(request_ROB_index) << std::endl; switch (static_cast(request_full_id)) { @@ -42,7 +42,7 @@ struct ALU : public dark::Module { alu_status <= 0b10; result_ROB_index <= request_ROB_index; result <= imm; - std::cerr << "lui: imm=" << std::hex << static_cast(imm) << std::endl; + DEBUG_CERR << "lui: imm=" << std::hex << static_cast(imm) << std::endl; completed_alu_resulting_PC <= static_cast(request_PC) + 4; return; } @@ -67,8 +67,8 @@ struct ALU : public dark::Module { alu_status <= 0b10; result_ROB_index <= request_ROB_index; result <= static_cast(request_PC) + 4; - std::cerr << "alu: jalr: imm=" << std::hex << static_cast(imm) << std::endl; - std::cerr << "alu: jalr: operand1=" << std::hex << static_cast(operand1) << std::endl; + DEBUG_CERR << "alu: jalr: imm=" << std::hex << static_cast(imm) << std::endl; + DEBUG_CERR << "alu: jalr: operand1=" << std::hex << static_cast(operand1) << std::endl; completed_alu_resulting_PC <= ((static_cast(operand1) + static_cast(imm)) & 0xfffffffe); return; } @@ -145,7 +145,7 @@ struct ALU : public dark::Module { alu_status <= 0b10; result_ROB_index <= request_ROB_index; result <= static_cast(operand1) + imm; - std::cerr << "\taddi: operand1=" << std::hex << static_cast(operand1) << " imm=" << std::hex + DEBUG_CERR << "\taddi: operand1=" << std::hex << static_cast(operand1) << " imm=" << std::hex << static_cast(imm) << " result=" << std::hex << result.peek() << std::endl; completed_alu_resulting_PC <= static_cast(request_PC) + 4; return; diff --git a/include/cpu.h b/include/cpu.h index 3acd9e1..5b9b604 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -65,12 +65,12 @@ public: auto func = shuffle ? &CPU::run_once_shuffle : &CPU::run_once; reset_signal=true; while (max_cycles == 0 || cycles < max_cycles) { - std::cerr<<"\nclock: "<*func)(); reset_signal=false; halt_signal.sync(); uint32_t halt_signal_value = static_cast(halt_signal); - std::cerr<<"simulator received halt_signal_value="<(record.instruction) << std::endl; is_committing <= 1; has_committed = true; commit_has_resulting_register <= record.has_resulting_register; commit_reg_index <= record.resulting_register_idx; commit_reg_value <= record.resulting_register_value; - std::cerr << "commit_reg_index=" << std::dec << commit_reg_index.peek() << " commit_reg_value=" << std::hex + DEBUG_CERR << "commit_reg_index=" << std::dec << commit_reg_index.peek() << " commit_reg_value=" << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << commit_reg_value.peek() << std::endl; commit_ins_ROB_index <= i; actual_PC <= static_cast(record.resulting_PC); if (static_cast(record.PC_mismatch_mark) == 1) { force_clear_announcer <= 1; - std::cerr << "[warning] csu is announcing rolling back due to PC mismatch" << std::endl; + DEBUG_CERR << "[warning] csu is announcing rolling back due to PC mismatch" << std::endl; } ROB_next_remain_space++; if (record.instruction == 0x0ff00513) { halt_signal <= (0b100000000 | static_cast(a0)); - std::cerr << "halting with code " << std::dec << int(halt_signal.peek()) << std::endl; + DEBUG_CERR << "halting with code " << std::dec << int(halt_signal.peek()) << std::endl; } if (record.instruction == 0x1B07A503) { - std::cerr << "judgeResult loaded from memory is " << std::dec + DEBUG_CERR << "judgeResult loaded from memory is " << std::dec << static_cast(record.resulting_register_value) << std::endl; } } @@ -297,7 +297,7 @@ struct CentralScheduleUnit if ((static_cast(record.instruction) & 0x7F) == 0b1100111) { has_predicted_PC <= 1; predicted_PC <= res_PC; - std::cerr << "The jalr instruction is committed, now predicted_PC is " << std::hex << std::setw(8) + DEBUG_CERR << "The jalr instruction is committed, now predicted_PC is " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << predicted_PC.peek() << std::endl; } } @@ -305,12 +305,12 @@ struct CentralScheduleUnit } } }; - std::cerr << "csu is listening data from memory" << std::endl; + DEBUG_CERR << "csu is listening data from memory" << std::endl; if (static_cast(mem_status_receiver) == 0b10) { process_data(static_cast(completed_memins_ROB_index), static_cast(completed_memins_read_data), 0); } - std::cerr << "csu is listening data from alu" << std::endl; + DEBUG_CERR << "csu is listening data from alu" << std::endl; if (static_cast(alu_status_receiver) == 0b10) { process_data(static_cast(completed_aluins_ROB_index), static_cast(completed_aluins_result), @@ -335,7 +335,7 @@ struct CentralScheduleUnit static_cast(has_instruction_issued_last_cycle); if (ROB_next_remain_space > 0 && actual_remain_space > 0) { // can issue - std::cerr << "csu is issuing mem instruct " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase + DEBUG_CERR << "csu is issuing mem instruct " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << instruction << " full_ins_id= " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << full_ins_id << " with ROB_index=" << std::dec << static_cast(ROB_tail) << std::endl; @@ -375,7 +375,7 @@ struct CentralScheduleUnit static_cast(has_instruction_issued_last_cycle); if (ROB_next_remain_space > 0 && actual_remain_space > 0) { // can issue - std::cerr << "csu is issuing alu instruct " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase + DEBUG_CERR << "csu is issuing alu instruct " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << instruction << " full_ins_id= " << std::hex << std::setw(8) << std::setfill('0') << std::uppercase << full_ins_id << " with ROB_index=" << std::dec << static_cast(ROB_tail) << std::endl; @@ -398,7 +398,7 @@ struct CentralScheduleUnit break; case 0b1100111: // jalr - std::cerr << "encounter jalr" << std::endl; + DEBUG_CERR << "encounter jalr" << std::endl; ROB_records[tail].resulting_PC_ready <= 0; has_predicted_PC <= 0; break; @@ -442,7 +442,7 @@ struct CentralScheduleUnit } // provide the potentially missing data for instruction issued last cycle if (bool(has_instruction_issued_last_cycle)) { - std::cerr << "CSU is processing potentially missing data for instruction issued last cycle" << std::endl; + DEBUG_CERR << "CSU is processing potentially missing data for instruction issued last cycle" << std::endl; uint8_t rs1 = static_cast(this->decoded_rs1); uint8_t found_rs1 = 0; uint32_t rs1_v; @@ -457,7 +457,7 @@ struct CentralScheduleUnit static_cast(ROB_records[ptr].resulting_register_idx) == rs1) { rs1_v = ROB_records[ptr].resulting_register_value.peek(); found_rs1 = 1; - std::cerr << "matching rs1=" << std::dec << int(rs1) << " ptr=" << std::dec << ptr << " rs1_v=" << std::hex + DEBUG_CERR << "matching rs1=" << std::dec << int(rs1) << " ptr=" << std::dec << ptr << " rs1_v=" << std::hex << std::setw(8) << std::setfill('0') << rs1_v << std::endl; } if (bool(ROB_records[ptr].has_resulting_register) && @@ -469,7 +469,7 @@ struct CentralScheduleUnit if (bool(ROB_records[ptr].has_resulting_register) && static_cast(ROB_records[ptr].resulting_register_idx) == rs1) { found_rs1 = 0; - std::cerr << "dematching rs1=" << std::dec << int(rs1) << " ptr=" << std::dec << ptr << std::endl; + DEBUG_CERR << "dematching rs1=" << std::dec << int(rs1) << " ptr=" << std::dec << ptr << std::endl; } if (bool(ROB_records[ptr].has_resulting_register) && static_cast(ROB_records[ptr].resulting_register_idx) == rs2) { diff --git a/include/loadstorequeue.h b/include/loadstorequeue.h index 3d60bd5..5e26dba 100644 --- a/include/loadstorequeue.h +++ b/include/loadstorequeue.h @@ -125,17 +125,17 @@ struct LoadStoreQueue : public dark::Module(full_ins_id) << std::endl; - std::cerr << "\tins_ROB_index: " << std::dec << static_cast(issue_ROB_index) << std::endl; - std::cerr << "\tins_self_PC: " << std::hex << std::setw(8) << std::setfill('0') + DEBUG_CERR << "LoadStoreQueue is accepting instruction" << std::endl; + DEBUG_CERR << "\tfull_ins_id: " << std::hex << static_cast(full_ins_id) << std::endl; + DEBUG_CERR << "\tins_ROB_index: " << std::dec << static_cast(issue_ROB_index) << std::endl; + DEBUG_CERR << "\tins_self_PC: " << std::hex << std::setw(8) << std::setfill('0') << static_cast(issuing_PC) << std::endl; - std::cerr << "\tins_imm: " << std::hex << static_cast(decoded_imm) << std::endl; - std::cerr << "\thas_decoded_rs1: " << std::hex << std::setw(8) << std::setfill('0') + DEBUG_CERR << "\tins_imm: " << std::hex << static_cast(decoded_imm) << std::endl; + DEBUG_CERR << "\thas_decoded_rs1: " << std::hex << std::setw(8) << std::setfill('0') << static_cast(has_decoded_rs1) << std::endl; - std::cerr << "\thas_decoded_rs2: " << std::hex << std::setw(8) << std::setfill('0') + DEBUG_CERR << "\thas_decoded_rs2: " << std::hex << std::setw(8) << std::setfill('0') << static_cast(has_decoded_rs2) << std::endl; - std::cerr << "\tstored in positon " << std::dec << static_cast(cur_queue_tail) << " of LSQ" + DEBUG_CERR << "\tstored in positon " << std::dec << static_cast(cur_queue_tail) << " of LSQ" << std::endl; // LSQ_queue[cur_queue_tail].Q1 <= decoded_rs1; // temporarily, no use // LSQ_queue[cur_queue_tail].Q2 <= decoded_rs2; // temporarily, no use @@ -147,36 +147,36 @@ struct LoadStoreQueue : public dark::Module void { - std::cerr << "res_ROB_index=" << std::dec << res_ROB_index << std::endl; - std::cerr << "res_value=" << std::hex << std::setw(8) << std::setfill('0') << res_value << std::endl; - std::cerr << "rs1_deps=" << std::dec << static_cast(rs1_deps) << std::endl; - std::cerr << "rs2_deps=" << std::dec << static_cast(rs2_deps) << std::endl; + DEBUG_CERR << "res_ROB_index=" << std::dec << res_ROB_index << std::endl; + DEBUG_CERR << "res_value=" << std::hex << std::setw(8) << std::setfill('0') << res_value << std::endl; + DEBUG_CERR << "rs1_deps=" << std::dec << static_cast(rs1_deps) << std::endl; + DEBUG_CERR << "rs2_deps=" << std::dec << static_cast(rs2_deps) << std::endl; uint32_t ptr = static_cast(LSQ_head); while (ptr != static_cast(LSQ_tail)) { - std::cerr << "\tptr=" << std::dec << ptr << std::endl; + DEBUG_CERR << "\tptr=" << std::dec << ptr << std::endl; if ((!bool(has_accepted_ins_last_cycle)) || ptr != last_idx) { - std::cerr << "\tnormal" << std::endl; + DEBUG_CERR << "\tnormal" << std::endl; dark::debug::assert(LSQ_queue[ptr].state == 2, "LSQ_queue[ptr].state != 2"); if ((!bool(LSQ_queue[ptr].D1)) && static_cast(LSQ_queue[ptr].Q1) == res_ROB_index) { LSQ_queue[ptr].V1 <= res_value; @@ -203,17 +203,17 @@ struct LoadStoreQueue : public dark::Module(rs2_deps) == res_ROB_index) { - std::cerr << "load rs2" << std::endl; + DEBUG_CERR << "load rs2" << std::endl; LSQ_queue[last_idx].V2 <= res_value; LSQ_queue[last_idx].D2 <= 1; should_monitor_V2 = false; @@ -222,15 +222,15 @@ struct LoadStoreQueue : public dark::Module(alu_status_receiver) == 0b10) { - std::cerr << "potentially have sth from alu" << std::endl; + DEBUG_CERR << "potentially have sth from alu" << std::endl; process_listend_data(static_cast(completed_aluins_ROB_index), static_cast(completed_aluins_result)); } - std::cerr << "Load Store Queue is listening data from memory" << std::endl; + DEBUG_CERR << "Load Store Queue is listening data from memory" << std::endl; if (static_cast(mem_data_sign) == 0b10) { - std::cerr << "potentially have sth from memory" << std::endl; + DEBUG_CERR << "potentially have sth from memory" << std::endl; process_listend_data(static_cast(completed_memins_ROB_index), static_cast(completed_memins_read_data)); } @@ -256,7 +256,7 @@ struct LoadStoreQueue : public dark::Module(LSQ_queue[head].ins_ROB_index); request_address_output <= (static_cast(LSQ_queue[head].V1) + static_cast(LSQ_queue[head].ins_imm)); - std::cerr << "\trequest_address_output=" << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "\trequest_address_output=" << std::hex << std::setfill('0') << std::setw(8) << request_address_output.peek() << std::endl; - std::cerr << "\toperand1=" << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "\toperand1=" << std::hex << std::setfill('0') << std::setw(8) << static_cast(LSQ_queue[head].V1) << std::endl; - std::cerr << "\timm=" << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "\timm=" << std::hex << std::setfill('0') << std::setw(8) << static_cast(LSQ_queue[head].ins_imm) << std::endl; - std::cerr << "\tROB_index=" << std::dec << static_cast(LSQ_queue[head].ins_ROB_index) + DEBUG_CERR << "\tROB_index=" << std::dec << static_cast(LSQ_queue[head].ins_ROB_index) << std::endl; request_data_output <= static_cast(LSQ_queue[head].V2); } else { @@ -338,7 +338,7 @@ struct LoadStoreQueue : public dark::Module(LSQ_queue[16].V1) << std::endl; } }; diff --git a/include/memory.h b/include/memory.h index 85543e8..6f059b8 100644 --- a/include/memory.h +++ b/include/memory.h @@ -116,7 +116,7 @@ struct Memory : dark::Module { } } completed_memins_read_data <= tmp; - std::cerr << "memory read: " << std::hex << std::setfill('0') << std::setw(2) << tmp << " from " << std::hex + DEBUG_CERR << "memory read: " << std::hex << std::setfill('0') << std::setw(2) << tmp << " from " << std::hex << static_cast(cur_opt_addr) << std::endl; break; } @@ -129,13 +129,13 @@ struct Memory : dark::Module { } } completed_memins_read_data <= tmp; - std::cerr << "memory read: " << std::hex << std::setfill('0') << std::setw(4) << tmp << " from " << std::hex + DEBUG_CERR << "memory read: " << std::hex << std::setfill('0') << std::setw(4) << tmp << " from " << std::hex << static_cast(cur_opt_addr) << std::endl; break; } case 4: completed_memins_read_data <= *reinterpret_cast(&memory_data[max_size_t(cur_opt_addr)]); - std::cerr << "memory read: " << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "memory read: " << std::hex << std::setfill('0') << std::setw(8) << *reinterpret_cast(&memory_data[max_size_t(cur_opt_addr)]) << " from " << std::hex << static_cast(cur_opt_addr) << std::endl; break; @@ -188,11 +188,11 @@ struct Memory : dark::Module { playback[cur_opt_ROB_index].changes[3].addr <= cur_opt_addr + 3; playback[cur_opt_ROB_index].changes[3].before <= memory_data[max_size_t(cur_opt_addr) + 3]; *reinterpret_cast(&memory_data[max_size_t(cur_opt_addr)]) = max_size_t(cur_opt_data); - std::cerr << "Memory executing sw, ROB_index=" << std::dec + DEBUG_CERR << "Memory executing sw, ROB_index=" << std::dec << static_cast(completed_memins_ROB_index) << std::endl; - std::cerr << "\taddr=" << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "\taddr=" << std::hex << std::setfill('0') << std::setw(8) << static_cast(cur_opt_addr) << std::endl; - std::cerr << "\tdata=" << std::hex << std::setfill('0') << std::setw(8) + DEBUG_CERR << "\tdata=" << std::hex << std::setfill('0') << std::setw(8) << static_cast(cur_opt_data) << std::endl; break; default: @@ -214,7 +214,7 @@ struct Memory : dark::Module { cur_opt_data <= data_input; cur_opt_type <= rw_type; cur_opt_bytes <= opt_bytes; - std::cerr << "Memory is accepting a request" << std::endl; + DEBUG_CERR << "Memory is accepting a request" << std::endl; } max_size_t FetchInstruction(max_size_t addr) { // assume we have a super nb instruction fetch method that can fetch // an instruction immediately diff --git a/include/register.h b/include/register.h index ab07c10..4262590 100644 --- a/include/register.h +++ b/include/register.h @@ -40,8 +40,9 @@ public: this->_M_new = static_cast(value); } auto peek() const -> max_size_t { // this function should only be used for convinience within the same module - if(this->_M_assigned) return this->_M_new; - return this->_M_old; + // if(this->_M_assigned) return this->_M_new; + // return this->_M_old; + return this->_M_new; } explicit operator max_size_t() const { return this->_M_old; } diff --git a/include/registerfile.h b/include/registerfile.h index 57cbb43..dfb432d 100644 --- a/include/registerfile.h +++ b/include/registerfile.h @@ -45,8 +45,8 @@ struct RegisterFile : public dark::Module(decoded_rd) == static_cast(commit_reg_index)))) register_nodep[static_cast(commit_reg_index)] <= 1; @@ -87,7 +87,7 @@ struct RegisterFile : public dark::Module(decoded_rs1) == 0) { rs1_deps <= 0; @@ -102,9 +102,9 @@ struct RegisterFile : public dark::Module void { - std::cerr << "\tres_ROB_index=" << std::dec << res_ROB_index << std::endl; + DEBUG_CERR << "\tres_ROB_index=" << std::dec << res_ROB_index << std::endl; for (uint32_t ptr = 0; ptr < 32; ptr++) { if (RS_records[ptr].state == 0) continue; if ((!bool(has_accepted_ins_last_cycle)) || ptr != last_idx) { @@ -200,12 +200,12 @@ struct ReserveStation : public dark::Module(alu_status_receiver) == 0b10) { process_listend_data(static_cast(completed_aluins_ROB_index), static_cast(completed_aluins_result)); } - std::cerr << "Reservestation is listening data from Memory" << std::endl; + DEBUG_CERR << "Reservestation is listening data from Memory" << std::endl; if (static_cast(mem_status_receiver) == 0b10) { process_listend_data(static_cast(completed_memins_ROB_index), static_cast(completed_memins_read_data)); @@ -216,12 +216,12 @@ struct ReserveStation : public dark::Module(RS_remaining_space) << std::endl; if (tot != static_cast(RS_remaining_space)) { diff --git a/src/main.cpp b/src/main.cpp index 24e2f8f..8cfcf94 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -170,6 +170,6 @@ int main(int argc, char **argv) { RWConnect(rf.rs2_deps, rs.rs2_deps); RWConnect(rf.rs2_value, rs.rs2_value); // now start running - std::cout << uint32_t(cpu.run(1000, false)) << std::endl; + std::cout << uint32_t(cpu.run(0, false)) << std::endl; return 0; } \ No newline at end of file