From 216fcb228c5ae45ce2cdde774f32e2d8c60f1d22 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 27 Aug 2022 15:43:44 -0700 Subject: [PATCH] riscv: Add a simple unit test. Since I haven't tried running these yet, at least best to validate... --- CMakeLists.txt | 1 + android/jni/Android.mk | 2 + unittest/TestRiscVEmitter.cpp | 69 ++++++++++++++++++++++++++++++ unittest/UnitTest.cpp | 4 ++ unittest/UnitTests.vcxproj | 1 + unittest/UnitTests.vcxproj.filters | 1 + 6 files changed, 78 insertions(+) create mode 100644 unittest/TestRiscVEmitter.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d34c0fb91d..376e2c5ad8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2348,6 +2348,7 @@ if(UNITTEST) unittest/TestIRPassSimplify.cpp unittest/TestX64Emitter.cpp unittest/TestVertexJit.cpp + unittest/TestRiscVEmitter.cpp unittest/TestSoftwareGPUJit.cpp unittest/TestThreadManager.cpp unittest/JitHarness.cpp diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 7f3afa7849..67f87fa589 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -721,11 +721,13 @@ ifeq ($(UNITTEST),1) TESTARMEMITTER_FILE = \ $(SRC)/Common/ArmEmitter.cpp \ $(SRC)/Common/Arm64Emitter.cpp \ + $(SRC)/Common/RiscVEmitter.cpp \ $(SRC)/Core/MIPS/ARM/ArmRegCacheFPU.cpp \ $(SRC)/Core/Util/DisArm64.cpp \ $(SRC)/ext/disarm.cpp \ $(SRC)/unittest/TestArmEmitter.cpp \ $(SRC)/unittest/TestArm64Emitter.cpp \ + $(SRC)/unittest/TestRiscVEmitter.cpp \ $(SRC)/unittest/TestX64Emitter.cpp endif diff --git a/unittest/TestRiscVEmitter.cpp b/unittest/TestRiscVEmitter.cpp new file mode 100644 index 0000000000..35daa4a675 --- /dev/null +++ b/unittest/TestRiscVEmitter.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2022- PPSSPP Project. + +// 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, version 2.0 or later versions. + +// 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 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include +#include "Common/CPUDetect.h" +#include "Common/RiscVEmitter.h" +#include "UnitTest.h" + +bool TestRiscVEmitter() { + using namespace RiscVGen; + + // TODO: Set cpu_info flags. + + u32 code[1024]; + RiscVEmitter emitter((u8 *)code, (u8 *)code); + + emitter.SetAutoCompress(false); + emitter.LUI(X1, 0xFFFFF000); + emitter.AUIPC(X2, 0x70051000); + emitter.JAL(X3, code); + emitter.JALR(X1, X2, -12); + FixupBranch b1 = emitter.JAL(X4); + emitter.SetJumpTarget(b1); + emitter.BEQ(X5, X6, code); + emitter.LB(X7, X8, 42); + emitter.SB(X9, X10, 1337); + emitter.ADDI(X11, X12, 42); + emitter.SLLI(X13, X14, 3); + emitter.ADD(X15, X16, X17); + emitter.FENCE(Fence::RW, Fence::RW); + + static constexpr uint32_t expected[] = { + 0xfffff0b7, + 0x70051117, + 0xff9ff1ef, + 0xff4100e7, + 0x0040026f, + 0xfe6286e3, + 0x02a40383, + 0x52950ca3, + 0x02a60593, + 0x00371693, + 0x011807b3, + 0x0330000f, + }; + + ptrdiff_t len = (u32 *)emitter.GetWritableCodePtr() - code; + EXPECT_EQ_INT(len, ARRAY_SIZE(expected)); + + for (ptrdiff_t i = 0; i < len; ++i) { + EXPECT_EQ_HEX(code[i], expected[i]); + } + + return true; +} diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 6bb000418e..78a08515d6 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -754,6 +754,7 @@ struct TestItem { bool TestArmEmitter(); bool TestArm64Emitter(); bool TestX64Emitter(); +bool TestRiscVEmitter(); bool TestShaderGenerators(); bool TestSoftwareGPUJit(); bool TestIRPassSimplify(); @@ -768,6 +769,9 @@ TestItem availableTests[] = { #endif #if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86) TEST_ITEM(X64Emitter), +#endif +#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86) || PPSSPP_ARCH(RISCV64) + TEST_ITEM(RiscVEmitter), #endif TEST_ITEM(VertexJit), TEST_ITEM(Asin), diff --git a/unittest/UnitTests.vcxproj b/unittest/UnitTests.vcxproj index dc50dacf92..057c5fe52b 100644 --- a/unittest/UnitTests.vcxproj +++ b/unittest/UnitTests.vcxproj @@ -383,6 +383,7 @@ true + diff --git a/unittest/UnitTests.vcxproj.filters b/unittest/UnitTests.vcxproj.filters index d0e70e6021..14344efb6a 100644 --- a/unittest/UnitTests.vcxproj.filters +++ b/unittest/UnitTests.vcxproj.filters @@ -15,6 +15,7 @@ +