MMIOTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2014 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <gtest/gtest.h>
  4. #include <memory>
  5. #include <unordered_set>
  6. #include "Common/CommonTypes.h"
  7. #include "Core/HW/GPFifo.h"
  8. #include "Core/HW/MMIO.h"
  9. #include "Core/System.h"
  10. // Tests that the UniqueID function returns a "unique enough" identifier
  11. // number: that is, it is unique in the address ranges we care about.
  12. TEST(UniqueID, UniqueEnough)
  13. {
  14. std::unordered_set<u32> ids;
  15. for (u32 i = 0x0C000000; i < 0x0C010000; ++i)
  16. {
  17. u32 unique_id = MMIO::UniqueID(i);
  18. EXPECT_FALSE(ids.contains(unique_id));
  19. ids.insert(unique_id);
  20. }
  21. for (u32 i = 0x0D000000; i < 0x0D010000; ++i)
  22. {
  23. u32 unique_id = MMIO::UniqueID(i);
  24. EXPECT_FALSE(ids.contains(unique_id));
  25. ids.insert(unique_id);
  26. }
  27. }
  28. TEST(IsMMIOAddress, SpecialAddresses)
  29. {
  30. constexpr bool is_wii = true;
  31. // WG Pipe address, should not be handled by MMIO.
  32. EXPECT_FALSE(MMIO::IsMMIOAddress(GPFifo::GATHER_PIPE_PHYSICAL_ADDRESS, is_wii));
  33. // Locked L1 cache allocation.
  34. EXPECT_FALSE(MMIO::IsMMIOAddress(0xE0000000, is_wii));
  35. // Uncached mirror of MEM1, shouldn't be handled by MMIO
  36. EXPECT_FALSE(MMIO::IsMMIOAddress(0xC0000000, is_wii));
  37. // Effective address of an MMIO register; MMIO only deals with physical
  38. // addresses.
  39. EXPECT_FALSE(MMIO::IsMMIOAddress(0xCC0000E0, is_wii));
  40. // And let's check some valid addresses too
  41. EXPECT_TRUE(MMIO::IsMMIOAddress(0x0C0000E0, is_wii)); // GameCube MMIOs
  42. EXPECT_TRUE(MMIO::IsMMIOAddress(0x0D00008C, is_wii)); // Wii MMIOs
  43. EXPECT_TRUE(MMIO::IsMMIOAddress(0x0D800F10, is_wii)); // Mirror of Wii MMIOs
  44. }
  45. class MappingTest : public testing::Test
  46. {
  47. protected:
  48. virtual void SetUp() override
  49. {
  50. m_system = &Core::System::GetInstance();
  51. m_mapping = std::make_unique<MMIO::Mapping>();
  52. }
  53. virtual void TearDown() override
  54. {
  55. m_system = nullptr;
  56. m_mapping.reset();
  57. }
  58. Core::System* m_system = nullptr;
  59. std::unique_ptr<MMIO::Mapping> m_mapping;
  60. };
  61. TEST_F(MappingTest, ReadConstant)
  62. {
  63. m_mapping->Register(0x0C001234, MMIO::Constant<u8>(0x42), MMIO::Nop<u8>());
  64. m_mapping->Register(0x0C001234, MMIO::Constant<u16>(0x1234), MMIO::Nop<u16>());
  65. m_mapping->Register(0x0C001234, MMIO::Constant<u32>(0xdeadbeef), MMIO::Nop<u32>());
  66. u8 val8 = m_mapping->Read<u8>(*m_system, 0x0C001234);
  67. u16 val16 = m_mapping->Read<u16>(*m_system, 0x0C001234);
  68. u32 val32 = m_mapping->Read<u32>(*m_system, 0x0C001234);
  69. EXPECT_EQ(0x42, val8);
  70. EXPECT_EQ(0x1234, val16);
  71. EXPECT_EQ(0xdeadbeef, val32);
  72. }
  73. TEST_F(MappingTest, ReadWriteDirect)
  74. {
  75. u8 target_8 = 0;
  76. u16 target_16 = 0;
  77. u32 target_32 = 0;
  78. m_mapping->Register(0x0C001234, MMIO::DirectRead<u8>(&target_8),
  79. MMIO::DirectWrite<u8>(&target_8));
  80. m_mapping->Register(0x0C001234, MMIO::DirectRead<u16>(&target_16),
  81. MMIO::DirectWrite<u16>(&target_16));
  82. m_mapping->Register(0x0C001234, MMIO::DirectRead<u32>(&target_32),
  83. MMIO::DirectWrite<u32>(&target_32));
  84. for (u32 i = 0; i < 100; ++i)
  85. {
  86. u8 val8 = m_mapping->Read<u8>(*m_system, 0x0C001234);
  87. EXPECT_EQ(i, val8);
  88. u16 val16 = m_mapping->Read<u16>(*m_system, 0x0C001234);
  89. EXPECT_EQ(i, val16);
  90. u32 val32 = m_mapping->Read<u32>(*m_system, 0x0C001234);
  91. EXPECT_EQ(i, val32);
  92. val8 += 1;
  93. m_mapping->Write(*m_system, 0x0C001234, val8);
  94. val16 += 1;
  95. m_mapping->Write(*m_system, 0x0C001234, val16);
  96. val32 += 1;
  97. m_mapping->Write(*m_system, 0x0C001234, val32);
  98. }
  99. }
  100. TEST_F(MappingTest, ReadWriteComplex)
  101. {
  102. bool read_called = false, write_called = false;
  103. m_mapping->Register(0x0C001234, MMIO::ComplexRead<u8>([&read_called](Core::System&, u32 addr) {
  104. EXPECT_EQ(0x0C001234u, addr);
  105. read_called = true;
  106. return 0x12;
  107. }),
  108. MMIO::ComplexWrite<u8>([&write_called](Core::System&, u32 addr, u8 val) {
  109. EXPECT_EQ(0x0C001234u, addr);
  110. EXPECT_EQ(0x34, val);
  111. write_called = true;
  112. }));
  113. u8 val = m_mapping->Read<u8>(*m_system, 0x0C001234);
  114. EXPECT_EQ(0x12, val);
  115. m_mapping->Write(*m_system, 0x0C001234, (u8)0x34);
  116. EXPECT_TRUE(read_called);
  117. EXPECT_TRUE(write_called);
  118. }