Debian Codegraph @

Source/UnitTests/Core/MMIOTest.cpp

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