Skip to content

Commit

Permalink
fix typing in 32-bit BE mode (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Jul 26, 2023
1 parent dd94f5c commit c080e54
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/ui/viewmodels/MemoryViewerViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ int MemoryViewerViewModel::NibblesPerWord() const
return NibblesForSize(GetSize());
}

int MemoryViewerViewModel::GetSelectedNibbleOffset() const
{
const auto nSize = GetSize();
switch (nSize)
{
case MemSize::SixteenBit:
case MemSize::ThirtyTwoBit:
return (NibblesForSize(nSize) - m_nSelectedNibble - 1);

default:
return m_nSelectedNibble ^ 1;
}
}

static MemoryViewerViewModel::TextColor GetColor(ra::ByteAddress nAddress,
const ra::ui::viewmodels::MemoryBookmarksViewModel& pBookmarksViewModel,
const ra::data::context::GameContext& pGameContext, bool bCheckNotes = true)
Expand Down Expand Up @@ -318,11 +332,10 @@ void MemoryViewerViewModel::UpdateSelectedNibble(int nNewNibble)
{
const auto nAddress = GetAddress();
const auto nFirstAddress = GetFirstAddress();
const auto nNibblesPerWord = NibblesPerWord();
m_pColor[nAddress - nFirstAddress + (nNibblesPerWord - m_nSelectedNibble - 1) / 2] |= STALE_COLOR;
m_pColor[nAddress - nFirstAddress + GetSelectedNibbleOffset() / 2] |= STALE_COLOR;
m_nSelectedNibble = nNewNibble;

m_pColor[nAddress - nFirstAddress + (nNibblesPerWord - m_nSelectedNibble - 1) / 2] |= STALE_COLOR;
m_pColor[nAddress - nFirstAddress + GetSelectedNibbleOffset() / 2] |= STALE_COLOR;
m_nNeedsRedraw |= REDRAW_MEMORY;
}
}
Expand Down Expand Up @@ -919,8 +932,7 @@ bool MemoryViewerViewModel::OnChar(char c)
}

// adjust for 16-bit and 32-bit views
const auto nNibblesPerWord = NibblesPerWord();
auto nSelectedNibble = (nNibblesPerWord - m_nSelectedNibble - 1);
auto nSelectedNibble = GetSelectedNibbleOffset();
while (nSelectedNibble > 1)
{
++nAddress;
Expand Down
1 change: 1 addition & 0 deletions src/ui/viewmodels/MemoryViewerViewModel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ private:
void UpdateHighlight(ra::ByteAddress nAddress, int nNewLength, int nOldLength);

int NibblesPerWord() const;
int GetSelectedNibbleOffset() const;
void UpdateSelectedNibble(int nNewNibble);

std::unique_ptr<uint8_t[]> m_pBuffer;
Expand Down
69 changes: 69 additions & 0 deletions tests/ui/viewmodels/MemoryViewerViewModel_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,75 @@ TEST_CLASS(MemoryViewerViewModel_Tests)
Assert::AreEqual({ 0x27U }, viewer.GetByte(7U));
}

TEST_METHOD(TestOnCharThirtyTwoBitBigEndian)
{
MemoryViewerViewModelHarness viewer;
viewer.InitializeMemory(256);
viewer.SetSize(MemSize::ThirtyTwoBitBigEndian);
viewer.MockRender();

Assert::AreEqual({0U}, viewer.GetAddress());
Assert::AreEqual({0U}, viewer.GetSelectedNibble());
Assert::AreEqual({0U}, viewer.GetByte(0U));
Assert::IsFalse(viewer.NeedsRedraw());

// ignore if readonly
Assert::IsTrue(viewer.IsReadOnly());
Assert::IsFalse(viewer.OnChar('6'));

viewer.SetReadOnly(false);
Assert::IsFalse(viewer.IsReadOnly());

// '6' should be set as upper nibble of selected byte
Assert::IsTrue(viewer.OnChar('6'));
Assert::AreEqual({0U}, viewer.GetAddress());
Assert::AreEqual({1U}, viewer.GetSelectedNibble());
Assert::AreEqual({0x60U}, viewer.GetByte(0U));
Assert::AreEqual({COLOR_RED | COLOR_REDRAW}, viewer.GetColor(0U));
Assert::IsTrue(viewer.NeedsRedraw());
viewer.MockRender();

// ignore invalid character
Assert::IsFalse(viewer.OnChar('G'));
Assert::AreEqual({0U}, viewer.GetAddress());
Assert::AreEqual({1U}, viewer.GetSelectedNibble());
Assert::AreEqual({0x60U}, viewer.GetByte(0U));
Assert::AreEqual({COLOR_RED}, viewer.GetColor(0U));
Assert::IsFalse(viewer.NeedsRedraw());

// 'B' should be set as lower nibble of selected byte
viewer.OnChar('B');
Assert::AreEqual({0U}, viewer.GetAddress());
Assert::AreEqual({2U}, viewer.GetSelectedNibble());
Assert::AreEqual({0x6BU}, viewer.GetByte(0U));
Assert::AreEqual({COLOR_RED | COLOR_REDRAW}, viewer.GetColor(0U));
Assert::IsTrue(viewer.NeedsRedraw());
viewer.MockRender();

// 'F' should be set as upper nibble of next byte
viewer.OnChar('F');
Assert::AreEqual({0U}, viewer.GetAddress());
Assert::AreEqual({3U}, viewer.GetSelectedNibble());
Assert::AreEqual({0xF1U}, viewer.GetByte(1U));

viewer.OnChar('3');
viewer.OnChar('D');
viewer.OnChar('E');
viewer.OnChar('6');
viewer.OnChar('8');

Assert::AreEqual({4U}, viewer.GetAddress());
Assert::AreEqual({0U}, viewer.GetSelectedNibble());
Assert::AreEqual({0xF3U}, viewer.GetByte(1U));
Assert::AreEqual({0xDEU}, viewer.GetByte(2U));
Assert::AreEqual({0x68U}, viewer.GetByte(3U));

viewer.OnChar('2');
Assert::AreEqual({4U}, viewer.GetAddress());
Assert::AreEqual({1U}, viewer.GetSelectedNibble());
Assert::AreEqual({0x24U}, viewer.GetByte(4U));
}

TEST_METHOD(TestTotalMemorySizeChanged)
{
MemoryViewerViewModelHarness viewer;
Expand Down

0 comments on commit c080e54

Please sign in to comment.