2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-24 04:32:32 +00:00
|
|
|
|
2014-03-02 03:44:56 +00:00
|
|
|
#include <gtest/gtest.h>
|
2017-06-18 14:21:17 +00:00
|
|
|
#include <string>
|
2014-03-02 03:44:56 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2017-07-09 23:17:36 +00:00
|
|
|
#include "Common/Config/Config.h"
|
2017-06-18 14:21:17 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2022-07-21 00:44:49 +00:00
|
|
|
#include "Core/HW/GPFifo.h"
|
2014-03-02 03:44:56 +00:00
|
|
|
#include "Core/HW/MMIO.h"
|
2017-06-05 02:56:28 +00:00
|
|
|
#include "UICommon/UICommon.h"
|
2014-03-02 03:44:56 +00:00
|
|
|
|
|
|
|
// Tests that the UniqueID function returns a "unique enough" identifier
|
|
|
|
// number: that is, it is unique in the address ranges we care about.
|
|
|
|
TEST(UniqueID, UniqueEnough)
|
|
|
|
{
|
|
|
|
std::unordered_set<u32> ids;
|
2015-02-12 02:01:47 +00:00
|
|
|
for (u32 i = 0x0C000000; i < 0x0C010000; ++i)
|
2014-03-02 03:44:56 +00:00
|
|
|
{
|
|
|
|
u32 unique_id = MMIO::UniqueID(i);
|
|
|
|
EXPECT_EQ(ids.end(), ids.find(unique_id));
|
|
|
|
ids.insert(unique_id);
|
|
|
|
}
|
2015-02-12 02:01:47 +00:00
|
|
|
for (u32 i = 0x0D000000; i < 0x0D010000; ++i)
|
2014-03-02 03:44:56 +00:00
|
|
|
{
|
|
|
|
u32 unique_id = MMIO::UniqueID(i);
|
|
|
|
EXPECT_EQ(ids.end(), ids.find(unique_id));
|
|
|
|
ids.insert(unique_id);
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 03:58:29 +00:00
|
|
|
|
2014-03-09 13:27:04 +00:00
|
|
|
TEST(IsMMIOAddress, SpecialAddresses)
|
|
|
|
{
|
2017-06-18 14:21:17 +00:00
|
|
|
const std::string profile_path = File::CreateTempDir();
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_FALSE(profile_path.empty());
|
2017-06-18 14:21:17 +00:00
|
|
|
UICommon::SetUserDirectory(profile_path);
|
2017-05-13 21:29:55 +00:00
|
|
|
Config::Init();
|
2014-09-11 09:02:45 +00:00
|
|
|
SConfig::Init();
|
2015-06-12 11:56:53 +00:00
|
|
|
SConfig::GetInstance().bWii = true;
|
2014-09-11 09:02:45 +00:00
|
|
|
|
2014-03-09 13:27:04 +00:00
|
|
|
// WG Pipe address, should not be handled by MMIO.
|
2022-07-21 00:44:49 +00:00
|
|
|
EXPECT_FALSE(MMIO::IsMMIOAddress(GPFifo::GATHER_PIPE_PHYSICAL_ADDRESS));
|
2014-03-09 13:27:04 +00:00
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
// Locked L1 cache allocation.
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_FALSE(MMIO::IsMMIOAddress(0xE0000000));
|
2014-09-11 09:02:45 +00:00
|
|
|
|
|
|
|
// Uncached mirror of MEM1, shouldn't be handled by MMIO
|
|
|
|
EXPECT_FALSE(MMIO::IsMMIOAddress(0xC0000000));
|
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
// Effective address of an MMIO register; MMIO only deals with physical
|
|
|
|
// addresses.
|
|
|
|
EXPECT_FALSE(MMIO::IsMMIOAddress(0xCC0000E0));
|
|
|
|
|
2022-07-21 00:44:49 +00:00
|
|
|
// And let's check some valid addresses too
|
2021-11-24 16:04:30 +00:00
|
|
|
EXPECT_TRUE(MMIO::IsMMIOAddress(0x0C0000E0)); // GameCube MMIOs
|
2015-02-12 02:01:47 +00:00
|
|
|
EXPECT_TRUE(MMIO::IsMMIOAddress(0x0D00008C)); // Wii MMIOs
|
|
|
|
EXPECT_TRUE(MMIO::IsMMIOAddress(0x0D800F10)); // Mirror of Wii MMIOs
|
2014-09-11 09:02:45 +00:00
|
|
|
|
|
|
|
SConfig::Shutdown();
|
2017-05-13 21:29:55 +00:00
|
|
|
Config::Shutdown();
|
2017-06-18 14:21:17 +00:00
|
|
|
File::DeleteDirRecursively(profile_path);
|
2014-03-09 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 03:58:29 +00:00
|
|
|
class MappingTest : public testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() override { m_mapping = new MMIO::Mapping(); }
|
2014-03-08 00:54:44 +00:00
|
|
|
virtual void TearDown() override { delete m_mapping; }
|
2021-09-04 04:43:19 +00:00
|
|
|
MMIO::Mapping* m_mapping = nullptr;
|
2014-03-02 03:58:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MappingTest, ReadConstant)
|
|
|
|
{
|
2015-02-12 02:01:47 +00:00
|
|
|
m_mapping->Register(0x0C001234, MMIO::Constant<u8>(0x42), MMIO::Nop<u8>());
|
|
|
|
m_mapping->Register(0x0C001234, MMIO::Constant<u16>(0x1234), MMIO::Nop<u16>());
|
|
|
|
m_mapping->Register(0x0C001234, MMIO::Constant<u32>(0xdeadbeef), MMIO::Nop<u32>());
|
2014-03-02 03:58:29 +00:00
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
u8 val8 = m_mapping->Read<u8>(0x0C001234);
|
|
|
|
u16 val16 = m_mapping->Read<u16>(0x0C001234);
|
|
|
|
u32 val32 = m_mapping->Read<u32>(0x0C001234);
|
2014-03-02 03:58:29 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(0x42, val8);
|
|
|
|
EXPECT_EQ(0x1234, val16);
|
|
|
|
EXPECT_EQ(0xdeadbeef, val32);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MappingTest, ReadWriteDirect)
|
|
|
|
{
|
|
|
|
u8 target_8 = 0;
|
|
|
|
u16 target_16 = 0;
|
|
|
|
u32 target_32 = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
m_mapping->Register(0x0C001234, MMIO::DirectRead<u8>(&target_8),
|
|
|
|
MMIO::DirectWrite<u8>(&target_8));
|
|
|
|
m_mapping->Register(0x0C001234, MMIO::DirectRead<u16>(&target_16),
|
|
|
|
MMIO::DirectWrite<u16>(&target_16));
|
|
|
|
m_mapping->Register(0x0C001234, MMIO::DirectRead<u32>(&target_32),
|
|
|
|
MMIO::DirectWrite<u32>(&target_32));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-08 06:36:44 +00:00
|
|
|
for (u32 i = 0; i < 100; ++i)
|
2014-03-02 03:58:29 +00:00
|
|
|
{
|
2015-02-12 02:01:47 +00:00
|
|
|
u8 val8 = m_mapping->Read<u8>(0x0C001234);
|
|
|
|
EXPECT_EQ(i, val8);
|
|
|
|
u16 val16 = m_mapping->Read<u16>(0x0C001234);
|
|
|
|
EXPECT_EQ(i, val16);
|
|
|
|
u32 val32 = m_mapping->Read<u32>(0x0C001234);
|
|
|
|
EXPECT_EQ(i, val32);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
val8 += 1;
|
|
|
|
m_mapping->Write(0x0C001234, val8);
|
|
|
|
val16 += 1;
|
|
|
|
m_mapping->Write(0x0C001234, val16);
|
|
|
|
val32 += 1;
|
|
|
|
m_mapping->Write(0x0C001234, val32);
|
2014-03-02 03:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-09 13:27:04 +00:00
|
|
|
|
|
|
|
TEST_F(MappingTest, ReadWriteComplex)
|
|
|
|
{
|
|
|
|
bool read_called = false, write_called = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-09 13:27:04 +00:00
|
|
|
m_mapping->Register(0x0C001234, MMIO::ComplexRead<u8>([&read_called](u32 addr) {
|
2015-03-16 09:56:16 +00:00
|
|
|
EXPECT_EQ(0x0C001234u, addr);
|
2014-03-09 13:27:04 +00:00
|
|
|
read_called = true;
|
|
|
|
return 0x12;
|
|
|
|
}),
|
|
|
|
MMIO::ComplexWrite<u8>([&write_called](u32 addr, u8 val) {
|
2015-03-16 09:56:16 +00:00
|
|
|
EXPECT_EQ(0x0C001234u, addr);
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_EQ(0x34, val);
|
|
|
|
write_called = true;
|
|
|
|
}));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-12 02:01:47 +00:00
|
|
|
u8 val = m_mapping->Read<u8>(0x0C001234);
|
|
|
|
EXPECT_EQ(0x12, val);
|
|
|
|
m_mapping->Write(0x0C001234, (u8)0x34);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_TRUE(read_called);
|
|
|
|
EXPECT_TRUE(write_called);
|
|
|
|
}
|