From 6124dfd8977f5e0d1e73ccbd818554e393e8d624 Mon Sep 17 00:00:00 2001 From: Maxim Poliakovski Date: Sat, 14 Nov 2020 03:05:06 +0100 Subject: [PATCH] Add virtual CPU benchmarking code. --- CMakeLists.txt | 10 +++++ benchmark/bench1.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 benchmark/bench1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d9dd0e9..c7b6bb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,16 @@ else() target_link_libraries(testppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) endif() +file(GLOB BENCH_SOURCES "${PROJECT_SOURCE_DIR}/benchmark/*.cpp") +add_executable(bench1 ${BENCH_SOURCES} $ + $ + $ + $ + $ + $) + +target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) + add_custom_command( TARGET testppc POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy diff --git a/benchmark/bench1.cpp b/benchmark/bench1.cpp new file mode 100644 index 0000000..b994b32 --- /dev/null +++ b/benchmark/bench1.cpp @@ -0,0 +1,91 @@ +#include +#include +#include "cpu/ppc/ppcemu.h" +#include "cpu/ppc/ppcmmu.h" +#include "devices/mpc106.h" +#include + +uint32_t cs_code[] = { + 0x3863FFFC, 0x7C861671, 0x41820090, 0x70600002, 0x41E2001C, 0xA0030004, + 0x3884FFFE, 0x38630002, 0x5486F0BF, 0x7CA50114, 0x41820070, 0x70C60003, + 0x41820014, 0x7CC903A6, 0x84030004, 0x7CA50114, 0x4200FFF8, 0x5486E13F, + 0x41820050, 0x80030004, 0x7CC903A6, 0x80C30008, 0x7CA50114, 0x80E3000C, + 0x7CA53114, 0x85030010, 0x7CA53914, 0x42400028, 0x80030004, 0x7CA54114, + 0x80C30008, 0x7CA50114, 0x80E3000C, 0x7CA53114, 0x85030010, 0x7CA53914, + 0x4200FFE0, 0x7CA54114, 0x70800002, 0x41E20010, 0xA0030004, 0x38630002, + 0x7CA50114, 0x70800001, 0x41E20010, 0x88030004, 0x5400402E, 0x7CA50114, + 0x7C650194, 0x4E800020 +}; + + +int main(int argc, char** argv) { + int i; + + /* initialize logging */ + loguru::g_preamble_date = false; + loguru::g_preamble_time = false; + loguru::g_preamble_thread = false; + + loguru::g_stderr_verbosity = 0; + loguru::init(argc, argv); + + MPC106* grackle_obj = new MPC106; + + /* we need some RAM */ + if (!grackle_obj->add_ram_region(0, 0x9000)) { + LOG_F(ERROR, "Could not create RAM region"); + delete(grackle_obj); + return -1; + } + + ppc_cpu_init(grackle_obj, PPC_VER::MPC750); + + /* load executable code into RAM at address 0 */ + for (i = 0; i < sizeof(cs_code); i++) { + mem_write_dword(i*4, cs_code[i]); + } + + srand(0xCAFEBABE); + + for (i = 0; i < 32768; i++) { + mem_write_byte(0x1000+i, rand() % 256); + } + + /* prepare benchmark code execution */ + ppc_state.pc = 0; + ppc_state.gpr[3] = 0x1000; // buf + ppc_state.gpr[4] = 0x8000; // len + ppc_state.gpr[5] = 0; // sum + + ppc_exec_until(0xC4); + + LOG_F(INFO, "Checksum: 0x%08X", ppc_state.gpr[3]); + + // run the clock once for cache fill etc. + auto start_time = std::chrono::steady_clock::now(); + auto end_time = std::chrono::steady_clock::now(); + auto time_elapsed = std::chrono::duration_cast(end_time - start_time); + LOG_F(INFO, "Time elapsed (dry run): %lld ns", time_elapsed.count()); + + for (i = 0; i < 5; i++) { + ppc_state.pc = 0; + ppc_state.gpr[3] = 0x1000; // buf + ppc_state.gpr[4] = 0x8000; // len + ppc_state.gpr[5] = 0; // sum + + auto start_time = std::chrono::steady_clock::now(); + + ppc_exec_until(0xC4); + + auto end_time = std::chrono::steady_clock::now(); + + LOG_F(INFO, "Checksum: 0x%08X", ppc_state.gpr[3]); + + auto time_elapsed = std::chrono::duration_cast(end_time - start_time); + LOG_F(INFO, "Time elapsed (run #%d): %lld ns", i, time_elapsed.count()); + } + + delete(grackle_obj); + + return 0; +}