From a0ef6eda1917f9ba5f051049e6fd57a3a8dbd600 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sat, 13 Jan 2018 18:03:34 +0000 Subject: [PATCH] tests/A64: Move TestEnvironment to own header --- tests/A64/a64.cpp | 47 +----------------------- tests/A64/testenv.h | 87 ++++++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + 3 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 tests/A64/testenv.h diff --git a/tests/A64/a64.cpp b/tests/A64/a64.cpp index 861746fa..3d5c5d43 100644 --- a/tests/A64/a64.cpp +++ b/tests/A64/a64.cpp @@ -4,54 +4,9 @@ * General Public License version 2 or any later version. */ -#include -#include - #include -#include - -#include "common/assert.h" -#include "common/common_types.h" - -class TestEnv final : public Dynarmic::A64::UserCallbacks { -public: - u64 ticks_left = 0; - std::array code_mem{}; - - std::uint32_t MemoryReadCode(u64 vaddr) override { - if (vaddr < code_mem.size() * sizeof(u32)) { - size_t index = vaddr / sizeof(u32); - return code_mem[index]; - } - return 0x14000000; // B . - } - - std::uint8_t MemoryRead8(u64 vaddr) override { ASSERT_MSG(false, "MemoryRead8(%" PRIx64 ")", vaddr); } - std::uint16_t MemoryRead16(u64 vaddr) override { ASSERT_MSG(false, "MemoryRead16(%" PRIx64 ")", vaddr); } - std::uint32_t MemoryRead32(u64 vaddr) override { ASSERT_MSG(false, "MemoryRead32(%" PRIx64 ")", vaddr); } - std::uint64_t MemoryRead64(u64 vaddr) override { ASSERT_MSG(false, "MemoryRead64(%" PRIx64 ")", vaddr); } - - void MemoryWrite8(u64 vaddr, std::uint8_t value) override { ASSERT_MSG(false, "MemoryWrite8(%" PRIx64 ", %" PRIx8 ")", vaddr, value); } - void MemoryWrite16(u64 vaddr, std::uint16_t value) override { ASSERT_MSG(false, "MemoryWrite16(%" PRIx64 ", %" PRIx16 ")", vaddr, value); } - void MemoryWrite32(u64 vaddr, std::uint32_t value) override { ASSERT_MSG(false, "MemoryWrite32(%" PRIx64 ", %" PRIx32 ")", vaddr, value); } - void MemoryWrite64(u64 vaddr, std::uint64_t value) override { ASSERT_MSG(false, "MemoryWrite64(%" PRIx64 ", %" PRIx64 ")", vaddr, value); } - - void InterpreterFallback(u64 pc, size_t num_instructions) override { ASSERT_MSG(false, "InterpreterFallback(%" PRIx64 ", %zu)", pc, num_instructions); } - - void CallSVC(std::uint32_t swi) override { ASSERT_MSG(false, "CallSVC(%u)", swi); } - - void AddTicks(std::uint64_t ticks) override { - if (ticks > ticks_left) { - ticks_left = 0; - return; - } - ticks_left -= ticks; - } - std::uint64_t GetTicksRemaining() override { - return ticks_left; - } -}; +#include "testenv.h" TEST_CASE("A64: ADD", "[a64]") { TestEnv env; diff --git a/tests/A64/testenv.h b/tests/A64/testenv.h new file mode 100644 index 00000000..3ed19ddb --- /dev/null +++ b/tests/A64/testenv.h @@ -0,0 +1,87 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * This software may be used and distributed according to the terms of the GNU + * General Public License version 2 or any later version. + */ + +#pragma once + +#include +#include +#include + +#include + +#include "common/assert.h" +#include "common/common_types.h" + +class TestEnv final : public Dynarmic::A64::UserCallbacks { +public: + u64 ticks_left = 0; + bool code_mem_modified_by_guest = false; + std::array code_mem{}; + std::map modified_memory; + + std::uint32_t MemoryReadCode(u64 vaddr) override { + if (vaddr < code_mem.size() * sizeof(u32)) { + size_t index = vaddr / sizeof(u32); + return code_mem[index]; + } + return 0x14000000; // B . + } + + std::uint8_t MemoryRead8(u64 vaddr) override { + if (vaddr < 4 * code_mem.size()) { + return reinterpret_cast(code_mem.data())[vaddr]; + } + if (auto iter = modified_memory.find(vaddr); iter != modified_memory.end()) { + return iter->second; + } + return static_cast(vaddr); + } + std::uint16_t MemoryRead16(u64 vaddr) override { + return u16(MemoryRead8(vaddr)) | u16(MemoryRead8(vaddr + 1)) << 8; + } + std::uint32_t MemoryRead32(u64 vaddr) override { + return u32(MemoryRead16(vaddr)) | u32(MemoryRead16(vaddr + 2)) << 16; + } + std::uint64_t MemoryRead64(u64 vaddr) override { + return u64(MemoryRead32(vaddr)) | u64(MemoryRead32(vaddr + 4)) << 32; + } + + void MemoryWrite8(u64 vaddr, std::uint8_t value) override { + if (vaddr < code_mem.size() * sizeof(u32)) { + code_mem_modified_by_guest = true; + } + modified_memory[vaddr] = value; + } + void MemoryWrite16(u64 vaddr, std::uint16_t value) override { + MemoryWrite8(vaddr, static_cast(value)); + MemoryWrite8(vaddr + 1, static_cast(value >> 8)); + } + void MemoryWrite32(u64 vaddr, std::uint32_t value) override { + MemoryWrite16(vaddr, static_cast(value)); + MemoryWrite16(vaddr + 2, static_cast(value >> 16)); + } + void MemoryWrite64(u64 vaddr, std::uint64_t value) override { + MemoryWrite32(vaddr, static_cast(value)); + MemoryWrite32(vaddr + 4, static_cast(value >> 32)); + } + + void InterpreterFallback(u64 pc, size_t num_instructions) override { ASSERT_MSG(false, "InterpreterFallback(%llx, %zu)", pc, num_instructions); } + + void CallSVC(std::uint32_t swi) override { ASSERT_MSG(false, "CallSVC(%u)", swi); } + + void ExceptionRaised(u64 pc, Dynarmic::A64::Exception /*exception*/) override { ASSERT_MSG(false, "ExceptionRaised(%" PRIx64 ")", pc); } + + void AddTicks(std::uint64_t ticks) override { + if (ticks > ticks_left) { + ticks_left = 0; + return; + } + ticks_left -= ticks; + } + std::uint64_t GetTicksRemaining() override { + return ticks_left; + } +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3f47eed2..75023182 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(dynarmic_tests A32/test_arm_disassembler.cpp A32/test_thumb_instructions.cpp A64/a64.cpp + A64/testenv.h main.cpp rand_int.h )