2016-07-12 09:12:56 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
2020-04-23 15:25:11 +01:00
|
|
|
* SPDX-License-Identifier: 0BSD
|
2016-07-12 09:12:56 +01:00
|
|
|
*/
|
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
2016-07-12 13:25:33 +01:00
|
|
|
#include <functional>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2016-07-12 09:12:56 +01:00
|
|
|
|
2021-08-08 12:51:37 +01:00
|
|
|
#include <catch2/catch.hpp>
|
2021-05-19 17:28:35 +01:00
|
|
|
|
|
|
|
#include "../fuzz_util.h"
|
|
|
|
#include "../rand_int.h"
|
|
|
|
#include "../unicorn_emu/a32_unicorn.h"
|
|
|
|
#include "./testenv.h"
|
|
|
|
#include "dynarmic/common/common_types.h"
|
|
|
|
#include "dynarmic/common/fp/fpcr.h"
|
|
|
|
#include "dynarmic/common/fp/fpsr.h"
|
|
|
|
#include "dynarmic/common/llvm_disassemble.h"
|
|
|
|
#include "dynarmic/common/scope_exit.h"
|
|
|
|
#include "dynarmic/frontend/A32/ITState.h"
|
2021-12-17 02:34:49 +00:00
|
|
|
#include "dynarmic/frontend/A32/a32_location_descriptor.h"
|
|
|
|
#include "dynarmic/frontend/A32/a32_types.h"
|
|
|
|
#include "dynarmic/frontend/A32/translate/a32_translate.h"
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/interface/A32/a32.h"
|
|
|
|
#include "dynarmic/ir/basic_block.h"
|
|
|
|
#include "dynarmic/ir/location_descriptor.h"
|
|
|
|
#include "dynarmic/ir/opcodes.h"
|
2016-07-12 09:12:56 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
// Must be declared last for all necessary operator<< to be declared prior to this.
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <fmt/ostream.h>
|
2016-08-07 19:25:12 +01:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
namespace {
|
2019-04-26 12:06:10 +01:00
|
|
|
using namespace Dynarmic;
|
2016-07-12 09:12:56 +01:00
|
|
|
|
2021-02-07 20:33:48 +00:00
|
|
|
bool ShouldTestInst(u32 instruction, u32 pc, bool is_thumb, bool is_last_inst, A32::ITState it_state = {}) {
|
|
|
|
const A32::LocationDescriptor location = A32::LocationDescriptor{pc, {}, {}}.SetTFlag(is_thumb).SetIT(it_state);
|
2019-04-26 12:06:10 +01:00
|
|
|
IR::Block block{location};
|
|
|
|
const bool should_continue = A32::TranslateSingleInstruction(block, location, instruction);
|
2016-07-12 09:12:56 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
if (!should_continue && !is_last_inst) {
|
|
|
|
return false;
|
2016-07-12 09:12:56 +01:00
|
|
|
}
|
2016-08-05 01:56:35 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
if (auto terminal = block.GetTerminal(); boost::get<IR::Term::Interpret>(&terminal)) {
|
|
|
|
return false;
|
2016-07-12 09:12:56 +01:00
|
|
|
}
|
2016-08-05 00:35:46 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
for (const auto& ir_inst : block) {
|
|
|
|
switch (ir_inst.GetOpcode()) {
|
|
|
|
case IR::Opcode::A32ExceptionRaised:
|
|
|
|
case IR::Opcode::A32CallSupervisor:
|
|
|
|
case IR::Opcode::A32CoprocInternalOperation:
|
|
|
|
case IR::Opcode::A32CoprocSendOneWord:
|
|
|
|
case IR::Opcode::A32CoprocSendTwoWords:
|
|
|
|
case IR::Opcode::A32CoprocGetOneWord:
|
|
|
|
case IR::Opcode::A32CoprocGetTwoWords:
|
|
|
|
case IR::Opcode::A32CoprocLoadWords:
|
|
|
|
case IR::Opcode::A32CoprocStoreWords:
|
|
|
|
return false;
|
2020-06-20 15:17:08 +01:00
|
|
|
// Currently unimplemented in Unicorn
|
|
|
|
case IR::Opcode::FPVectorRecipEstimate16:
|
|
|
|
case IR::Opcode::FPVectorRSqrtEstimate16:
|
2020-06-21 10:00:24 +01:00
|
|
|
case IR::Opcode::VectorPolynomialMultiplyLong64:
|
2020-06-20 15:17:08 +01:00
|
|
|
return false;
|
2019-04-26 12:06:10 +01:00
|
|
|
default:
|
|
|
|
continue;
|
2016-07-12 09:12:56 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 21:48:45 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
return true;
|
2016-12-20 20:04:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
u32 GenRandomArmInst(u32 pc, bool is_last_inst) {
|
2019-04-26 12:06:10 +01:00
|
|
|
static const struct InstructionGeneratorInfo {
|
|
|
|
std::vector<InstructionGenerator> generators;
|
|
|
|
std::vector<InstructionGenerator> invalid;
|
2021-05-22 14:51:20 +01:00
|
|
|
} instructions = [] {
|
|
|
|
const std::vector<std::tuple<std::string, const char*>> list{
|
2019-04-26 12:06:10 +01:00
|
|
|
#define INST(fn, name, bitstring) {#fn, bitstring},
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/frontend/A32/decoder/arm.inc"
|
|
|
|
#include "dynarmic/frontend/A32/decoder/asimd.inc"
|
|
|
|
#include "dynarmic/frontend/A32/decoder/vfp.inc"
|
2019-04-26 12:06:10 +01:00
|
|
|
#undef INST
|
2017-04-02 12:36:32 +01:00
|
|
|
};
|
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
std::vector<InstructionGenerator> generators;
|
|
|
|
std::vector<InstructionGenerator> invalid;
|
|
|
|
|
|
|
|
// List of instructions not to test
|
2021-05-22 14:51:20 +01:00
|
|
|
static constexpr std::array do_not_test{
|
2019-04-26 12:06:10 +01:00
|
|
|
// Translating load/stores
|
|
|
|
"arm_LDRBT", "arm_LDRBT", "arm_LDRHT", "arm_LDRHT", "arm_LDRSBT", "arm_LDRSBT", "arm_LDRSHT", "arm_LDRSHT", "arm_LDRT", "arm_LDRT",
|
|
|
|
"arm_STRBT", "arm_STRBT", "arm_STRHT", "arm_STRHT", "arm_STRT", "arm_STRT",
|
|
|
|
// Exclusive load/stores
|
2020-05-15 21:07:07 +01:00
|
|
|
"arm_LDREXB", "arm_LDREXD", "arm_LDREXH", "arm_LDREX", "arm_LDAEXB", "arm_LDAEXD", "arm_LDAEXH", "arm_LDAEX",
|
|
|
|
"arm_STREXB", "arm_STREXD", "arm_STREXH", "arm_STREX", "arm_STLEXB", "arm_STLEXD", "arm_STLEXH", "arm_STLEX",
|
2019-04-26 12:06:10 +01:00
|
|
|
"arm_SWP", "arm_SWPB",
|
|
|
|
// Elevated load/store multiple instructions.
|
|
|
|
"arm_LDM_eret", "arm_LDM_usr",
|
|
|
|
"arm_STM_usr",
|
|
|
|
// Hint instructions
|
2019-05-03 01:03:33 +01:00
|
|
|
"arm_NOP", "arm_PLD_imm", "arm_PLD_reg", "arm_SEV",
|
|
|
|
"arm_WFE", "arm_WFI", "arm_YIELD",
|
2019-04-26 12:06:10 +01:00
|
|
|
// E, T, J
|
|
|
|
"arm_BLX_reg", "arm_BLX_imm", "arm_BXJ", "arm_SETEND",
|
|
|
|
// Coprocessor
|
|
|
|
"arm_CDP", "arm_LDC", "arm_MCR", "arm_MCRR", "arm_MRC", "arm_MRRC", "arm_STC",
|
|
|
|
// System
|
|
|
|
"arm_CPS", "arm_RFE", "arm_SRS",
|
|
|
|
// Undefined
|
|
|
|
"arm_UDF",
|
2020-05-10 14:47:21 +01:00
|
|
|
// FPSCR is inaccurate
|
|
|
|
"vfp_VMRS",
|
2020-06-20 14:45:29 +01:00
|
|
|
// Incorrect Unicorn implementations
|
2021-05-22 14:51:20 +01:00
|
|
|
"asimd_VRECPS", // Unicorn does not fuse the multiply and subtraction, resulting in being off by 1ULP.
|
|
|
|
"asimd_VRSQRTS", // Unicorn does not fuse the multiply and subtraction, resulting in being off by 1ULP.
|
|
|
|
"vfp_VCVT_from_fixed", // Unicorn does not do round-to-nearest-even for this instruction correctly.
|
2016-07-12 09:12:56 +01:00
|
|
|
};
|
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
for (const auto& [fn, bitstring] : list) {
|
|
|
|
if (std::find(do_not_test.begin(), do_not_test.end(), fn) != do_not_test.end()) {
|
|
|
|
invalid.emplace_back(InstructionGenerator{bitstring});
|
|
|
|
continue;
|
TranslateArm: Implement CLREX, LDREX, LDREXB, LDREXD, LDREXH, STREX, STREXB, STREXD, STREXH, SWP, SWPB
2016-08-09 22:48:20 +01:00
|
|
|
}
|
2019-04-26 12:06:10 +01:00
|
|
|
generators.emplace_back(InstructionGenerator{bitstring});
|
2016-08-07 21:55:38 +01:00
|
|
|
}
|
2019-04-26 12:06:10 +01:00
|
|
|
return InstructionGeneratorInfo{generators, invalid};
|
|
|
|
}();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const size_t index = RandInt<size_t>(0, instructions.generators.size() - 1);
|
|
|
|
const u32 inst = instructions.generators[index].Generate();
|
2019-05-05 18:43:09 +01:00
|
|
|
|
2020-05-10 13:57:39 +01:00
|
|
|
if ((instructions.generators[index].Mask() & 0xF0000000) == 0 && (inst & 0xF0000000) == 0xF0000000) {
|
2019-04-26 12:06:10 +01:00
|
|
|
continue;
|
2016-08-07 21:55:38 +01:00
|
|
|
}
|
2020-05-10 13:57:39 +01:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
if (ShouldTestInst(inst, pc, false, is_last_inst)) {
|
2019-04-26 12:06:10 +01:00
|
|
|
return inst;
|
2016-08-07 21:55:38 +01:00
|
|
|
}
|
2016-08-05 02:35:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 20:33:48 +00:00
|
|
|
std::vector<u16> GenRandomThumbInst(u32 pc, bool is_last_inst, A32::ITState it_state = {}) {
|
2021-02-06 21:25:08 +00:00
|
|
|
static const struct InstructionGeneratorInfo {
|
|
|
|
std::vector<InstructionGenerator> generators;
|
|
|
|
std::vector<InstructionGenerator> invalid;
|
2021-05-22 14:51:20 +01:00
|
|
|
} instructions = [] {
|
|
|
|
const std::vector<std::tuple<std::string, const char*>> list{
|
2021-02-06 21:25:08 +00:00
|
|
|
#define INST(fn, name, bitstring) {#fn, bitstring},
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/frontend/A32/decoder/thumb16.inc"
|
|
|
|
#include "dynarmic/frontend/A32/decoder/thumb32.inc"
|
2021-02-06 21:25:08 +00:00
|
|
|
#undef INST
|
|
|
|
};
|
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
const std::vector<std::tuple<std::string, const char*>> vfp_list{
|
2021-05-03 22:33:37 +01:00
|
|
|
#define INST(fn, name, bitstring) {#fn, bitstring},
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/frontend/A32/decoder/vfp.inc"
|
2021-05-03 22:33:37 +01:00
|
|
|
#undef INST
|
|
|
|
};
|
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
const std::vector<std::tuple<std::string, const char*>> asimd_list{
|
2021-05-03 23:30:59 +01:00
|
|
|
#define INST(fn, name, bitstring) {#fn, bitstring},
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/frontend/A32/decoder/asimd.inc"
|
2021-05-03 23:30:59 +01:00
|
|
|
#undef INST
|
|
|
|
};
|
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
std::vector<InstructionGenerator> generators;
|
|
|
|
std::vector<InstructionGenerator> invalid;
|
|
|
|
|
|
|
|
// List of instructions not to test
|
2021-05-22 14:51:20 +01:00
|
|
|
static constexpr std::array do_not_test{
|
2021-02-06 21:25:08 +00:00
|
|
|
"thumb16_BKPT",
|
2021-02-07 20:33:48 +00:00
|
|
|
"thumb16_IT",
|
|
|
|
"thumb16_SETEND",
|
2021-04-02 00:18:52 +01:00
|
|
|
|
|
|
|
// Exclusive load/stores
|
2021-04-19 14:26:51 +01:00
|
|
|
"thumb32_LDREX",
|
|
|
|
"thumb32_LDREXB",
|
|
|
|
"thumb32_LDREXD",
|
|
|
|
"thumb32_LDREXH",
|
2021-04-02 00:18:52 +01:00
|
|
|
"thumb32_STREX",
|
|
|
|
"thumb32_STREXB",
|
|
|
|
"thumb32_STREXD",
|
|
|
|
"thumb32_STREXH",
|
2021-05-03 22:33:37 +01:00
|
|
|
|
|
|
|
// FPSCR is inaccurate
|
|
|
|
"vfp_VMRS",
|
|
|
|
|
2021-05-04 12:35:28 +01:00
|
|
|
// Unicorn is incorrect?
|
|
|
|
"thumb32_MRS_reg",
|
|
|
|
|
2021-05-03 22:33:37 +01:00
|
|
|
// Unicorn has incorrect implementation (incorrect rounding and unsets CPSR.T??)
|
|
|
|
"vfp_VCVT_to_fixed",
|
|
|
|
"vfp_VCVT_from_fixed",
|
2021-05-22 14:51:20 +01:00
|
|
|
"asimd_VRECPS", // Unicorn does not fuse the multiply and subtraction, resulting in being off by 1ULP.
|
|
|
|
"asimd_VRSQRTS", // Unicorn does not fuse the multiply and subtraction, resulting in being off by 1ULP.
|
2021-05-13 18:02:38 +01:00
|
|
|
|
|
|
|
// Coprocessor
|
2021-05-22 14:51:20 +01:00
|
|
|
"thumb32_CDP",
|
|
|
|
"thumb32_LDC",
|
|
|
|
"thumb32_MCR",
|
|
|
|
"thumb32_MCRR",
|
|
|
|
"thumb32_MRC",
|
|
|
|
"thumb32_MRRC",
|
|
|
|
"thumb32_STC",
|
2021-02-06 21:25:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& [fn, bitstring] : list) {
|
|
|
|
if (std::find(do_not_test.begin(), do_not_test.end(), fn) != do_not_test.end()) {
|
|
|
|
invalid.emplace_back(InstructionGenerator{bitstring});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
generators.emplace_back(InstructionGenerator{bitstring});
|
|
|
|
}
|
2021-05-03 22:33:37 +01:00
|
|
|
for (const auto& [fn, bs] : vfp_list) {
|
|
|
|
std::string bitstring = bs;
|
|
|
|
if (bitstring.substr(0, 4) == "cccc" || bitstring.substr(0, 4) == "----") {
|
|
|
|
bitstring.replace(0, 4, "1110");
|
|
|
|
}
|
|
|
|
if (std::find(do_not_test.begin(), do_not_test.end(), fn) != do_not_test.end()) {
|
|
|
|
invalid.emplace_back(InstructionGenerator{bitstring.c_str()});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
generators.emplace_back(InstructionGenerator{bitstring.c_str()});
|
|
|
|
}
|
2021-05-03 23:30:59 +01:00
|
|
|
for (const auto& [fn, bs] : asimd_list) {
|
|
|
|
std::string bitstring = bs;
|
|
|
|
if (bitstring.substr(0, 7) == "1111001") {
|
|
|
|
const char U = bitstring[7];
|
|
|
|
bitstring.replace(0, 8, "111-1111");
|
|
|
|
bitstring[3] = U;
|
|
|
|
} else if (bitstring.substr(0, 8) == "11110100") {
|
|
|
|
bitstring.replace(0, 8, "11111001");
|
|
|
|
} else {
|
|
|
|
ASSERT_FALSE("Unhandled ASIMD instruction: {} {}", fn, bs);
|
|
|
|
}
|
|
|
|
if (std::find(do_not_test.begin(), do_not_test.end(), fn) != do_not_test.end()) {
|
|
|
|
invalid.emplace_back(InstructionGenerator{bitstring.c_str()});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
generators.emplace_back(InstructionGenerator{bitstring.c_str()});
|
|
|
|
}
|
2021-02-06 21:25:08 +00:00
|
|
|
return InstructionGeneratorInfo{generators, invalid};
|
|
|
|
}();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const size_t index = RandInt<size_t>(0, instructions.generators.size() - 1);
|
|
|
|
const u32 inst = instructions.generators[index].Generate();
|
|
|
|
const bool is_four_bytes = (inst >> 16) != 0;
|
|
|
|
|
2021-02-07 20:33:48 +00:00
|
|
|
if (ShouldTestInst(is_four_bytes ? Common::SwapHalves32(inst) : inst, pc, true, is_last_inst, it_state)) {
|
2021-02-06 21:25:08 +00:00
|
|
|
if (is_four_bytes)
|
2021-05-22 14:51:20 +01:00
|
|
|
return {static_cast<u16>(inst >> 16), static_cast<u16>(inst)};
|
|
|
|
return {static_cast<u16>(inst)};
|
2021-02-06 21:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
template<typename TestEnv>
|
2021-02-06 21:25:08 +00:00
|
|
|
Dynarmic::A32::UserConfig GetUserConfig(TestEnv& testenv) {
|
2019-04-26 12:06:10 +01:00
|
|
|
Dynarmic::A32::UserConfig user_config;
|
2020-06-28 21:39:26 +01:00
|
|
|
user_config.optimizations &= ~OptimizationFlag::FastDispatch;
|
2019-04-26 12:06:10 +01:00
|
|
|
user_config.callbacks = &testenv;
|
2019-07-27 19:54:57 +01:00
|
|
|
user_config.always_little_endian = true;
|
2019-04-26 12:06:10 +01:00
|
|
|
return user_config;
|
2016-08-05 02:03:23 +01:00
|
|
|
}
|
2016-08-12 16:53:16 +01:00
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
template<typename TestEnv>
|
2021-02-06 21:25:08 +00:00
|
|
|
static void RunTestInstance(Dynarmic::A32::Jit& jit,
|
|
|
|
A32Unicorn<TestEnv>& uni,
|
|
|
|
TestEnv& jit_env,
|
|
|
|
TestEnv& uni_env,
|
|
|
|
const typename A32Unicorn<TestEnv>::RegisterArray& regs,
|
|
|
|
const typename A32Unicorn<TestEnv>::ExtRegArray& vecs,
|
|
|
|
const std::vector<typename TestEnv::InstructionType>& instructions,
|
|
|
|
const u32 cpsr,
|
|
|
|
const u32 fpscr,
|
|
|
|
const size_t ticks_left) {
|
2019-04-26 12:06:10 +01:00
|
|
|
const u32 initial_pc = regs[15];
|
2021-02-06 21:25:08 +00:00
|
|
|
const u32 num_words = initial_pc / sizeof(typename TestEnv::InstructionType);
|
2019-05-05 18:43:09 +01:00
|
|
|
const u32 code_mem_size = num_words + static_cast<u32>(instructions.size());
|
2019-04-26 12:06:10 +01:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
jit_env.code_mem.resize(code_mem_size);
|
|
|
|
uni_env.code_mem.resize(code_mem_size);
|
2021-02-06 22:15:02 +00:00
|
|
|
std::fill(jit_env.code_mem.begin(), jit_env.code_mem.end(), TestEnv::infinite_loop);
|
|
|
|
std::fill(uni_env.code_mem.begin(), uni_env.code_mem.end(), TestEnv::infinite_loop);
|
2019-04-26 12:06:10 +01:00
|
|
|
|
|
|
|
std::copy(instructions.begin(), instructions.end(), jit_env.code_mem.begin() + num_words);
|
|
|
|
std::copy(instructions.begin(), instructions.end(), uni_env.code_mem.begin() + num_words);
|
2021-02-06 21:25:08 +00:00
|
|
|
jit_env.PadCodeMem();
|
|
|
|
uni_env.PadCodeMem();
|
2019-04-26 12:06:10 +01:00
|
|
|
jit_env.modified_memory.clear();
|
|
|
|
uni_env.modified_memory.clear();
|
|
|
|
jit_env.interrupts.clear();
|
|
|
|
uni_env.interrupts.clear();
|
|
|
|
|
|
|
|
jit.Regs() = regs;
|
|
|
|
jit.ExtRegs() = vecs;
|
|
|
|
jit.SetFpscr(fpscr);
|
|
|
|
jit.SetCpsr(cpsr);
|
|
|
|
jit.ClearCache();
|
|
|
|
uni.SetRegisters(regs);
|
|
|
|
uni.SetExtRegs(vecs);
|
|
|
|
uni.SetFpscr(fpscr);
|
|
|
|
uni.EnableFloatingPointAccess();
|
|
|
|
uni.SetCpsr(cpsr);
|
|
|
|
uni.ClearPageCache();
|
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
jit_env.ticks_left = ticks_left;
|
2019-04-26 12:06:10 +01:00
|
|
|
jit.Run();
|
2016-08-12 16:53:16 +01:00
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
uni_env.ticks_left = instructions.size(); // Unicorn counts thumb instructions weirdly.
|
2019-04-26 12:06:10 +01:00
|
|
|
uni.Run();
|
2016-11-26 17:24:57 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
SCOPE_FAIL {
|
|
|
|
fmt::print("Instruction Listing:\n");
|
2021-02-07 20:31:48 +00:00
|
|
|
fmt::print("{}\n", Common::DisassembleAArch32(std::is_same_v<TestEnv, ThumbTestEnv>, initial_pc, (const u8*)instructions.data(), instructions.size() * sizeof(instructions[0])));
|
2016-11-26 17:24:57 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
fmt::print("Initial register listing:\n");
|
|
|
|
for (size_t i = 0; i < regs.size(); ++i) {
|
|
|
|
fmt::print("{:3s}: {:08x}\n", static_cast<A32::Reg>(i), regs[i]);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < vecs.size(); ++i) {
|
|
|
|
fmt::print("{:3s}: {:08x}\n", static_cast<A32::ExtReg>(i), vecs[i]);
|
|
|
|
}
|
|
|
|
fmt::print("cpsr {:08x}\n", cpsr);
|
|
|
|
fmt::print("fpcr {:08x}\n", fpscr);
|
|
|
|
fmt::print("fpcr.AHP {}\n", FP::FPCR{fpscr}.AHP());
|
|
|
|
fmt::print("fpcr.DN {}\n", FP::FPCR{fpscr}.DN());
|
|
|
|
fmt::print("fpcr.FZ {}\n", FP::FPCR{fpscr}.FZ());
|
|
|
|
fmt::print("fpcr.RMode {}\n", static_cast<size_t>(FP::FPCR{fpscr}.RMode()));
|
|
|
|
fmt::print("fpcr.FZ16 {}\n", FP::FPCR{fpscr}.FZ16());
|
|
|
|
fmt::print("\n");
|
|
|
|
|
|
|
|
fmt::print("Final register listing:\n");
|
|
|
|
fmt::print(" unicorn dynarmic\n");
|
|
|
|
const auto uni_regs = uni.GetRegisters();
|
|
|
|
for (size_t i = 0; i < regs.size(); ++i) {
|
|
|
|
fmt::print("{:3s}: {:08x} {:08x} {}\n", static_cast<A32::Reg>(i), uni_regs[i], jit.Regs()[i], uni_regs[i] != jit.Regs()[i] ? "*" : "");
|
|
|
|
}
|
|
|
|
const auto uni_ext_regs = uni.GetExtRegs();
|
|
|
|
for (size_t i = 0; i < vecs.size(); ++i) {
|
|
|
|
fmt::print("s{:2d}: {:08x} {:08x} {}\n", static_cast<size_t>(i), uni_ext_regs[i], jit.ExtRegs()[i], uni_ext_regs[i] != jit.ExtRegs()[i] ? "*" : "");
|
|
|
|
}
|
|
|
|
fmt::print("cpsr {:08x} {:08x} {}\n", uni.GetCpsr(), jit.Cpsr(), uni.GetCpsr() != jit.Cpsr() ? "*" : "");
|
|
|
|
fmt::print("fpsr {:08x} {:08x} {}\n", uni.GetFpscr(), jit.Fpscr(), (uni.GetFpscr() & 0xF0000000) != (jit.Fpscr() & 0xF0000000) ? "*" : "");
|
|
|
|
fmt::print("\n");
|
|
|
|
|
|
|
|
fmt::print("Modified memory:\n");
|
|
|
|
fmt::print(" uni dyn\n");
|
|
|
|
auto uni_iter = uni_env.modified_memory.begin();
|
|
|
|
auto jit_iter = jit_env.modified_memory.begin();
|
|
|
|
while (uni_iter != uni_env.modified_memory.end() || jit_iter != jit_env.modified_memory.end()) {
|
|
|
|
if (uni_iter == uni_env.modified_memory.end() || (jit_iter != jit_env.modified_memory.end() && uni_iter->first > jit_iter->first)) {
|
|
|
|
fmt::print("{:08x}: {:02x} *\n", jit_iter->first, jit_iter->second);
|
|
|
|
jit_iter++;
|
|
|
|
} else if (jit_iter == jit_env.modified_memory.end() || jit_iter->first > uni_iter->first) {
|
|
|
|
fmt::print("{:08x}: {:02x} *\n", uni_iter->first, uni_iter->second);
|
|
|
|
uni_iter++;
|
|
|
|
} else if (uni_iter->first == jit_iter->first) {
|
|
|
|
fmt::print("{:08x}: {:02x} {:02x} {}\n", uni_iter->first, uni_iter->second, jit_iter->second, uni_iter->second != jit_iter->second ? "*" : "");
|
|
|
|
uni_iter++;
|
|
|
|
jit_iter++;
|
2019-04-20 10:00:12 +01:00
|
|
|
}
|
2016-08-13 06:51:55 +01:00
|
|
|
}
|
2019-04-26 12:06:10 +01:00
|
|
|
fmt::print("\n");
|
2016-08-13 06:51:55 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
fmt::print("x86_64:\n");
|
2021-08-06 15:28:17 +01:00
|
|
|
jit.DumpDisassembly();
|
2016-08-13 06:51:55 +01:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
fmt::print("Interrupts:\n");
|
|
|
|
for (const auto& i : uni_env.interrupts) {
|
|
|
|
std::puts(i.c_str());
|
2019-04-20 16:11:44 +01:00
|
|
|
}
|
|
|
|
};
|
2016-11-23 18:14:07 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
REQUIRE(uni_env.code_mem_modified_by_guest == jit_env.code_mem_modified_by_guest);
|
|
|
|
if (uni_env.code_mem_modified_by_guest) {
|
|
|
|
return;
|
2017-12-09 15:42:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
// Qemu doesn't do Thumb transitions??
|
|
|
|
{
|
|
|
|
const u32 uni_pc = uni.GetPC();
|
|
|
|
const bool is_thumb = (jit.Cpsr() & (1 << 5)) != 0;
|
|
|
|
const u32 new_uni_pc = uni_pc & (is_thumb ? 0xFFFFFFFE : 0xFFFFFFFC);
|
|
|
|
uni.SetPC(new_uni_pc);
|
2017-12-09 15:42:47 +00:00
|
|
|
}
|
2019-04-26 12:06:10 +01:00
|
|
|
|
|
|
|
REQUIRE(uni.GetRegisters() == jit.Regs());
|
|
|
|
REQUIRE(uni.GetExtRegs() == jit.ExtRegs());
|
2020-06-15 23:43:44 +01:00
|
|
|
REQUIRE((uni.GetCpsr() & 0xFFFFFDDF) == (jit.Cpsr() & 0xFFFFFDDF));
|
2019-04-26 12:06:10 +01:00
|
|
|
REQUIRE((uni.GetFpscr() & 0xF0000000) == (jit.Fpscr() & 0xF0000000));
|
|
|
|
REQUIRE(uni_env.modified_memory == jit_env.modified_memory);
|
|
|
|
REQUIRE(uni_env.interrupts.empty());
|
2017-12-09 15:42:47 +00:00
|
|
|
}
|
2021-05-22 14:51:20 +01:00
|
|
|
} // Anonymous namespace
|
2017-12-09 15:42:47 +00:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
TEST_CASE("A32: Single random arm instruction", "[arm]") {
|
2019-04-26 12:06:10 +01:00
|
|
|
ArmTestEnv jit_env{};
|
|
|
|
ArmTestEnv uni_env{};
|
2016-12-15 22:33:20 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ArmTestEnv> uni{uni_env};
|
2016-12-15 22:33:20 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
A32Unicorn<ArmTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ArmTestEnv>::ExtRegArray ext_reg;
|
|
|
|
std::vector<u32> instructions(1);
|
2016-12-15 22:33:20 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
for (size_t iteration = 0; iteration < 100000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
2016-12-21 14:15:46 +00:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
instructions[0] = GenRandomArmInst(0, true);
|
2016-12-21 14:15:46 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
const u32 start_address = 100;
|
2019-07-27 19:54:57 +01:00
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x10;
|
2019-04-26 12:06:10 +01:00
|
|
|
const u32 fpcr = RandomFpcr();
|
2016-11-23 21:45:18 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
INFO("Instruction: 0x" << std::hex << instructions[0]);
|
2016-11-23 21:45:18 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
regs[15] = start_address;
|
2021-02-06 21:25:08 +00:00
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, 1);
|
2016-11-23 21:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
TEST_CASE("A32: Small random arm block", "[arm]") {
|
2019-04-26 12:06:10 +01:00
|
|
|
ArmTestEnv jit_env{};
|
|
|
|
ArmTestEnv uni_env{};
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ArmTestEnv> uni{uni_env};
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
A32Unicorn<ArmTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ArmTestEnv>::ExtRegArray ext_reg;
|
|
|
|
std::vector<u32> instructions(5);
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
for (size_t iteration = 0; iteration < 100000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
instructions[0] = GenRandomArmInst(0, false);
|
|
|
|
instructions[1] = GenRandomArmInst(4, false);
|
|
|
|
instructions[2] = GenRandomArmInst(8, false);
|
|
|
|
instructions[3] = GenRandomArmInst(12, false);
|
|
|
|
instructions[4] = GenRandomArmInst(16, true);
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
const u32 start_address = 100;
|
2019-07-27 19:54:57 +01:00
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x10;
|
2019-04-26 12:06:10 +01:00
|
|
|
const u32 fpcr = RandomFpcr();
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
INFO("Instruction 1: 0x" << std::hex << instructions[0]);
|
|
|
|
INFO("Instruction 2: 0x" << std::hex << instructions[1]);
|
|
|
|
INFO("Instruction 3: 0x" << std::hex << instructions[2]);
|
|
|
|
INFO("Instruction 4: 0x" << std::hex << instructions[3]);
|
|
|
|
INFO("Instruction 5: 0x" << std::hex << instructions[4]);
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2019-04-26 12:06:10 +01:00
|
|
|
regs[15] = start_address;
|
2021-02-06 21:25:08 +00:00
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, 5);
|
2019-04-26 12:06:10 +01:00
|
|
|
}
|
2017-02-16 18:18:29 +00:00
|
|
|
}
|
2020-06-21 00:36:33 +01:00
|
|
|
|
2021-02-06 21:25:08 +00:00
|
|
|
TEST_CASE("A32: Large random arm block", "[arm]") {
|
2020-06-21 00:36:33 +01:00
|
|
|
ArmTestEnv jit_env{};
|
|
|
|
ArmTestEnv uni_env{};
|
|
|
|
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ArmTestEnv> uni{uni_env};
|
|
|
|
|
|
|
|
A32Unicorn<ArmTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ArmTestEnv>::ExtRegArray ext_reg;
|
|
|
|
|
|
|
|
constexpr size_t instruction_count = 100;
|
|
|
|
std::vector<u32> instructions(instruction_count);
|
|
|
|
|
|
|
|
for (size_t iteration = 0; iteration < 10000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
|
|
|
|
for (size_t j = 0; j < instruction_count; ++j) {
|
2021-02-06 21:25:08 +00:00
|
|
|
instructions[j] = GenRandomArmInst(j * 4, j == instruction_count - 1);
|
2020-06-21 00:36:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const u64 start_address = 100;
|
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x10;
|
|
|
|
const u32 fpcr = RandomFpcr();
|
|
|
|
|
|
|
|
regs[15] = start_address;
|
2021-02-06 21:25:08 +00:00
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("A32: Single random thumb instruction", "[thumb]") {
|
|
|
|
ThumbTestEnv jit_env{};
|
|
|
|
ThumbTestEnv uni_env{};
|
|
|
|
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ThumbTestEnv> uni{uni_env};
|
|
|
|
|
|
|
|
A32Unicorn<ThumbTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ThumbTestEnv>::ExtRegArray ext_reg;
|
|
|
|
std::vector<u16> instructions;
|
|
|
|
|
|
|
|
for (size_t iteration = 0; iteration < 100000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
|
|
|
|
instructions = GenRandomThumbInst(0, true);
|
|
|
|
|
|
|
|
const u32 start_address = 100;
|
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x1F0;
|
|
|
|
const u32 fpcr = RandomFpcr();
|
|
|
|
|
|
|
|
INFO("Instruction: 0x" << std::hex << instructions[0]);
|
|
|
|
|
|
|
|
regs[15] = start_address;
|
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("A32: Small random thumb block", "[thumb]") {
|
|
|
|
ThumbTestEnv jit_env{};
|
|
|
|
ThumbTestEnv uni_env{};
|
|
|
|
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ThumbTestEnv> uni{uni_env};
|
|
|
|
|
|
|
|
A32Unicorn<ThumbTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ThumbTestEnv>::ExtRegArray ext_reg;
|
|
|
|
std::vector<u16> instructions;
|
|
|
|
|
|
|
|
for (size_t iteration = 0; iteration < 100000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
|
|
|
|
instructions.clear();
|
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
|
|
const std::vector<u16> inst = GenRandomThumbInst(instructions.size() * 2, i == 4);
|
|
|
|
instructions.insert(instructions.end(), inst.begin(), inst.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 start_address = 100;
|
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x1F0;
|
|
|
|
const u32 fpcr = RandomFpcr();
|
|
|
|
|
|
|
|
regs[15] = start_address;
|
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, 5);
|
2020-06-21 00:36:33 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-07 20:33:48 +00:00
|
|
|
|
|
|
|
TEST_CASE("A32: Test thumb IT instruction", "[thumb]") {
|
|
|
|
ThumbTestEnv jit_env{};
|
|
|
|
ThumbTestEnv uni_env{};
|
|
|
|
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(jit_env)};
|
|
|
|
A32Unicorn<ThumbTestEnv> uni{uni_env};
|
|
|
|
|
|
|
|
A32Unicorn<ThumbTestEnv>::RegisterArray regs;
|
|
|
|
A32Unicorn<ThumbTestEnv>::ExtRegArray ext_reg;
|
|
|
|
std::vector<u16> instructions;
|
|
|
|
|
|
|
|
for (size_t iteration = 0; iteration < 100000; ++iteration) {
|
|
|
|
std::generate(regs.begin(), regs.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
std::generate(ext_reg.begin(), ext_reg.end(), [] { return RandInt<u32>(0, ~u32(0)); });
|
|
|
|
|
|
|
|
const size_t pre_instructions = RandInt<size_t>(0, 3);
|
|
|
|
const size_t post_instructions = RandInt<size_t>(5, 8);
|
|
|
|
|
|
|
|
instructions.clear();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pre_instructions; i++) {
|
|
|
|
const std::vector<u16> inst = GenRandomThumbInst(instructions.size() * 2, false);
|
|
|
|
instructions.insert(instructions.end(), inst.begin(), inst.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit IT instruction
|
2021-05-22 14:51:20 +01:00
|
|
|
A32::ITState it_state = [&] {
|
2021-02-07 20:33:48 +00:00
|
|
|
while (true) {
|
|
|
|
const u16 imm8 = RandInt<u16>(0, 0xFF);
|
|
|
|
if (Common::Bits<0, 3>(imm8) == 0b0000 || Common::Bits<4, 7>(imm8) == 0b1111 || (Common::Bits<4, 7>(imm8) == 0b1110 && Common::BitCount(Common::Bits<0, 3>(imm8)) != 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
instructions.push_back(0b1011111100000000 | imm8);
|
|
|
|
return A32::ITState{static_cast<u8>(imm8)};
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < post_instructions; i++) {
|
|
|
|
const std::vector<u16> inst = GenRandomThumbInst(instructions.size() * 2, i == post_instructions - 1, it_state);
|
|
|
|
instructions.insert(instructions.end(), inst.begin(), inst.end());
|
|
|
|
it_state = it_state.Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 start_address = 100;
|
|
|
|
const u32 cpsr = (RandInt<u32>(0, 0xF) << 28) | 0x1F0;
|
|
|
|
const u32 fpcr = RandomFpcr();
|
|
|
|
|
|
|
|
regs[15] = start_address;
|
|
|
|
RunTestInstance(jit, uni, jit_env, uni_env, regs, ext_reg, instructions, cpsr, fpcr, pre_instructions + 1 + post_instructions);
|
|
|
|
}
|
|
|
|
}
|