Skip to content

Commit 8312f48

Browse files
committed
Update following review
- Improve unit tests - Add a mask to handle complex pointer like PSX
1 parent 2fc324b commit 8312f48

File tree

2 files changed

+82
-30
lines changed

2 files changed

+82
-30
lines changed

src/ui/viewmodels/MemoryViewerViewModel.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,22 @@ void MemoryViewerViewModel::OnShiftClick(int nX, int nY)
903903

904904
OnClick(nX, nY);
905905

906-
ra::ByteAddress nAddress = pEmulatorContext.ReadMemory(GetAddress(), GetSize());
906+
// We need to create a mask based on the last address of the current console to be able to point to more complexe pointer format
907+
// like on Playstation on which pointers are represented as a 24-bit pointer prefixed by 80.
908+
std::wstring sMaxAddress = ra::data::MemSizeFormat(pConsoleContext.MaxAddress(), MemSize::ThirtyTwoBit, MemFormat::Hex);
909+
std::wstring sMask;
910+
911+
for (wchar_t ch : sMaxAddress)
912+
if (ch != L'0')
913+
sMask.push_back(L'F');
914+
915+
auto nMask = std::stoi(sMask, 0, 16);
916+
ra::ByteAddress nAddress = (pEmulatorContext.ReadMemory(GetAddress(), GetSize())) & nMask;
907917
const auto nConvertedAddress = pConsoleContext.ByteAddressFromRealAddress(nAddress);
908918
if (nConvertedAddress != 0xFFFFFFFF)
909-
nAddress = nConvertedAddress;
910-
911-
SetAddress(nAddress);
919+
{
920+
SetAddress(nConvertedAddress);
921+
}
912922
}
913923

914924
void MemoryViewerViewModel::OnResized(int nWidth, int nHeight)

tests/ui/viewmodels/MemoryViewerViewModel_Tests.cpp

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,19 +1925,17 @@ TEST_CLASS(MemoryViewerViewModel_Tests)
19251925

19261926
TEST_METHOD(TestOnShiftClickEightBit)
19271927
{
1928-
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"Playstation");
1928+
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"PlayStation");
19291929

19301930
MemoryViewerViewModelHarness viewer;
1931-
viewer.InitializeMemory(128); // 8 rows of 16 bytes
1932-
viewer.mockEmulatorContext.WriteMemoryByte(0U, 0x20);
1933-
viewer.mockEmulatorContext.WriteMemoryByte(1U, 0xff);
1934-
viewer.mockEmulatorContext.WriteMemoryByte(2U, 0x0);
1931+
viewer.InitializeMemory(0xFFFF);
19351932

19361933
Assert::AreEqual(MemSize::EightBit, viewer.GetSize());
19371934
Assert::AreEqual({ 0U }, viewer.GetAddress());
19381935
Assert::AreEqual({ 0U }, viewer.GetSelectedNibble());
19391936

19401937
// If fixed address, ignore
1938+
viewer.mockEmulatorContext.WriteMemoryByte(0U, 0x20);
19411939
viewer.SetAddressFixed(true);
19421940
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
19431941
Assert::AreEqual({0x0U}, viewer.GetAddress());
@@ -1947,26 +1945,20 @@ TEST_CLASS(MemoryViewerViewModel_Tests)
19471945
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
19481946
Assert::AreEqual({ 0x20U }, viewer.GetAddress());
19491947

1950-
// Shift click on the second byte containing 0xFF should lead to address 0x7F as 0xFF is bigger than the last
1951-
// address in memory
1952-
viewer.OnShiftClick(13 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1953-
Assert::AreEqual({0x7FU}, viewer.GetAddress());
1954-
1955-
// Shift click on the third byte containing 0x0 should lead back to address 0x0
1956-
viewer.OnShiftClick(16 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1957-
Assert::AreEqual({0x0U}, viewer.GetAddress());
1948+
// Shift click on the second byte containing 0x0 should do nothing
1949+
viewer.SetAddress(0);
1950+
viewer.mockEmulatorContext.WriteMemoryByte(1U, 0x0);
1951+
viewer.OnShiftClick(15 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1952+
Assert::AreEqual({0x1U}, viewer.GetAddress());
19581953
}
19591954

1960-
TEST_METHOD(TestOnShiftClickSixTeenBit)
1955+
TEST_METHOD(TestOnShiftClickSixteenBit)
19611956
{
1962-
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"Playstation");
1957+
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"PlayStation");
19631958

19641959
MemoryViewerViewModelHarness viewer;
1965-
viewer.InitializeMemory(512); // 32 rows of 16 bytes
1960+
viewer.InitializeMemory(0xFFFF);
19661961
viewer.SetSize(MemSize::SixteenBit);
1967-
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::SixteenBit, 0x20);
1968-
viewer.mockEmulatorContext.WriteMemory(2U, MemSize::SixteenBit, 0xff);
1969-
viewer.mockEmulatorContext.WriteMemory(4U, MemSize::SixteenBit, 0xffff);
19701962

19711963
Assert::AreEqual(MemSize::SixteenBit, viewer.GetSize());
19721964
Assert::AreEqual({0U}, viewer.GetAddress());
@@ -1978,18 +1970,68 @@ TEST_CLASS(MemoryViewerViewModel_Tests)
19781970
Assert::AreEqual({0x0U}, viewer.GetAddress());
19791971

19801972
// If not fixed address and Shift click on the first word containing 0x20 should lead to address 0x20
1973+
viewer.SetAddress(0);
19811974
viewer.SetAddressFixed(false);
1975+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::SixteenBit, 0x0020);
19821976
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
19831977
Assert::AreEqual({0x20U}, viewer.GetAddress());
19841978

1985-
// Shift click on the second word containing 0x40 should lead to address 0xFF
1986-
viewer.OnShiftClick(15 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1987-
Assert::AreEqual({0xFFU}, viewer.GetAddress());
1979+
// Shift click on the first word containing 0x0140 should lead to address 0x0140
1980+
viewer.SetAddress(0);
1981+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::SixteenBit, 0x0140);
1982+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1983+
Assert::AreEqual({0x0140U}, viewer.GetAddress());
1984+
1985+
// Shift click on the second word containing 0x0 should do nothing
1986+
viewer.SetAddress(0x4);
1987+
viewer.mockEmulatorContext.WriteMemory(4U, MemSize::SixteenBit, 0x0);
1988+
viewer.OnShiftClick(22 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1989+
Assert::AreEqual({0x4U}, viewer.GetAddress());
1990+
}
1991+
1992+
TEST_METHOD(TestOnShiftClickThirtyTwoBit)
1993+
{
1994+
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"Playstation");
1995+
1996+
MemoryViewerViewModelHarness viewer;
1997+
viewer.InitializeMemory(0x1FFFFF); // PSX full memory
1998+
viewer.SetSize(MemSize::ThirtyTwoBit);
1999+
2000+
Assert::AreEqual(MemSize::ThirtyTwoBit, viewer.GetSize());
2001+
Assert::AreEqual({0U}, viewer.GetAddress());
2002+
Assert::AreEqual({0U}, viewer.GetSelectedNibble());
19882003

1989-
// Shift click on the third word containing 0xFFFF should lead to address 0x1FF as 0xFFFF is bigger than the
1990-
// last address in memory
1991-
viewer.OnShiftClick(19 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1992-
Assert::AreEqual({0x1FFU}, viewer.GetAddress());
2004+
// If fixed address, ignore
2005+
viewer.SetAddressFixed(true);
2006+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
2007+
Assert::AreEqual({0x0U}, viewer.GetAddress());
2008+
2009+
// If not fixed address and Shift click on the first dword containing 0x1234 should lead to address 0x1234
2010+
viewer.SetAddress(0);
2011+
viewer.SetAddressFixed(false);
2012+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::ThirtyTwoBit, 0x1234);
2013+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
2014+
Assert::AreEqual({0x1234U}, viewer.GetAddress());
2015+
2016+
// Shift click on the first dword containing 0x80012345 should lead to address 0x00012345 as the conversion keeps
2017+
// only the same number of nibbles as the Max address from console context (PSX : 1FFFFF)
2018+
viewer.SetAddress(0);
2019+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::ThirtyTwoBit, 0x00012345);
2020+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
2021+
Assert::AreEqual({0x00012345}, viewer.GetAddress());
2022+
2023+
// Shift click on the second dword containing 0x0 should do nothing
2024+
viewer.SetAddress(0x8);
2025+
viewer.mockEmulatorContext.WriteMemory(8U, MemSize::ThirtyTwoBit, 0x0);
2026+
viewer.OnShiftClick(28 * CHAR_WIDTH, CHAR_HEIGHT + 4);
2027+
Assert::AreEqual({0x8U}, viewer.GetAddress());
2028+
2029+
// Shift click on the first dword containing 0x005FFFFF should do nothing as 0x005FFFFF exceed the
2030+
// last address in the current console context (PSX : 0x1FFFFF) and can't be converted to real address
2031+
viewer.SetAddress(0);
2032+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::ThirtyTwoBit, 0x005FFFFF);
2033+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
2034+
Assert::AreEqual({0x0}, viewer.GetAddress());
19932035
}
19942036
};
19952037

0 commit comments

Comments
 (0)