Skip to content

Commit

Permalink
QMemoryView octal/hex modes
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Jan 4, 2024
1 parent 29ef984 commit 3213f1f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions emulator/QtUkncBtl.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<file>images/iconCartridgeSlot.svg</file>
<file>images/iconHdd.svg</file>
<file>images/iconHddSlot.svg</file>
<file>images/iconHex.svg</file>
<file>images/iconSound.svg</file>
<file>images/iconSoundOff.svg</file>
<file>images/iconStepInto.svg</file>
Expand Down
10 changes: 10 additions & 0 deletions emulator/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,15 @@ bool Settings_GetDebugMemoryByte()
return value.toBool();
}

void Settings_SetDebugMemoryNumeral(quint16 mode)
{
Global_getSettings()->setValue("DebugMemoryNumeral", mode);
}
quint16 Settings_GetDebugMemoryNumeral()
{
QVariant value = Global_getSettings()->value("DebugMemoryNumeral", 0);
return (quint16)value.toUInt();
}


//////////////////////////////////////////////////////////////////////
1 change: 1 addition & 0 deletions emulator/images/iconHex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions emulator/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void Settings_SetDebugMemoryAddress(quint16 address);
quint16 Settings_GetDebugMemoryAddress();
bool Settings_GetDebugMemoryByte();
void Settings_SetDebugMemoryByte(bool flag);
void Settings_SetDebugMemoryNumeral(quint16 mode);
quint16 Settings_GetDebugMemoryNumeral();


//////////////////////////////////////////////////////////////////////
Expand Down
89 changes: 69 additions & 20 deletions emulator/qmemoryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ static const char * MemoryView_ModeNames[] =
"RAM0", "RAM1", "RAM2", "ROM", "CPU", "PPU"
};

enum MemoryViewNumeralMode
{
MEMMODENUM_OCT = 0,
MEMMODENUM_HEX = 1,
};


QMemoryView::QMemoryView()
{
m_Mode = Settings_GetDebugMemoryMode();
if (m_Mode > MEMMODE_LAST) m_Mode = MEMMODE_LAST;
m_ByteMode = Settings_GetDebugMemoryByte();
m_NumeralMode = Settings_GetDebugMemoryNumeral();
m_wBaseAddress = Settings_GetDebugMemoryAddress();
m_cyLineMemory = 0;
m_nPageSize = 0;
Expand Down Expand Up @@ -66,9 +73,11 @@ QMemoryView::QMemoryView()
QAction* actionGotoAddr = m_toolbar->addAction(QIcon(":/images/iconEditAddress.svg"), "");
m_toolbar->addSeparator();
QAction* actionWordByte = m_toolbar->addAction(QIcon(":/images/iconWordByte.svg"), "");
QAction* actionOctalHex = m_toolbar->addAction(QIcon(":/images/iconHex.svg"), "");

QObject::connect(actionGotoAddr, SIGNAL(triggered()), this, SLOT(gotoAddress()));
QObject::connect(actionWordByte, SIGNAL(triggered()), this, SLOT(changeWordByteMode()));
QObject::connect(actionOctalHex, SIGNAL(triggered()), this, SLOT(changeNumeralMode()));

setFocusPolicy(Qt::ClickFocus);
}
Expand Down Expand Up @@ -112,7 +121,7 @@ void QMemoryView::focusOutEvent(QFocusEvent *)
void QMemoryView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(tr("Go to Address..."), this, SLOT(gotoAddress()));
menu.addAction(QIcon(":/images/iconEditAddress.svg"), tr("Go to Address..."), this, SLOT(gotoAddress()));
menu.addSeparator();

for (int mode = 0; mode <= MEMMODE_LAST; mode++)
Expand All @@ -126,7 +135,8 @@ void QMemoryView::contextMenuEvent(QContextMenuEvent *event)
}

menu.addSeparator();
menu.addAction(tr("Words / Bytes"), this, SLOT(changeWordByteMode()));
menu.addAction(QIcon(":/images/iconWordByte.svg"), tr("Words / Bytes"), this, SLOT(changeWordByteMode()));
menu.addAction(QIcon(":/images/iconHex.svg"), tr("Octal / Hex"), this, SLOT(changeNumeralMode()));

menu.exec(event->globalPos());
}
Expand All @@ -153,6 +163,15 @@ void QMemoryView::changeWordByteMode()
repaint();
}

void QMemoryView::changeNumeralMode()
{
int newMode = m_NumeralMode ^ 1;
m_NumeralMode = newMode;
Settings_SetDebugMemoryNumeral(newMode);

repaint();
}

void QMemoryView::scrollBy(qint16 delta)
{
if (delta == 0) return;
Expand Down Expand Up @@ -260,14 +279,26 @@ void QMemoryView::paintEvent(QPaintEvent * /*event*/)

m_cyLineMemory = cyLine;

if (m_NumeralMode == MEMMODENUM_OCT)
m_PostionIncrement = cxChar * 7;
else
m_PostionIncrement = cxChar * 5;
if (m_ByteMode)
m_PostionIncrement += cxChar;

char buffer[7];
const char * ADDRESS_LINE = " addr";
painter.drawText(30, cyLine, ADDRESS_LINE);
for (int j = 0; j < 8; j++)
{
_snprintf(buffer, 7, "%o", j * 2);
painter.drawText(38 + (9 + j * 7) * cxChar, cyLine, buffer);
}
const char* ADDRESS_LINE_OCT_WORDS = " addr 0 2 4 6 10 12 14 16";
const char* ADDRESS_LINE_OCT_BYTES = " addr 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17";
const char* ADDRESS_LINE_HEX_WORDS = " addr 0 2 4 6 8 a c e";
const char* ADDRESS_LINE_HEX_BYTES = " addr 0 1 2 3 4 5 6 7 8 9 a b c d e f";
if (m_NumeralMode == MEMMODENUM_OCT && !m_ByteMode)
painter.drawText(38, cyLine, ADDRESS_LINE_OCT_WORDS);
else if (m_NumeralMode == MEMMODENUM_OCT && m_ByteMode)
painter.drawText(38, cyLine, ADDRESS_LINE_OCT_BYTES);
else if (m_ByteMode)
painter.drawText(38, cyLine, ADDRESS_LINE_HEX_BYTES);
else
painter.drawText(38, cyLine, ADDRESS_LINE_HEX_WORDS);

// Calculate m_nPageSize
m_nPageSize = this->height() / cyLine - 1;
Expand All @@ -276,11 +307,13 @@ void QMemoryView::paintEvent(QPaintEvent * /*event*/)
int y = 2 * cyLine;
for (;;) // Draw lines
{
DrawOctalValue(painter, 38 + 1 * cxChar, y, address);
if (m_NumeralMode == MEMMODENUM_OCT)
DrawOctalValue(painter, 38 + 1 * cxChar, y, address);
else
DrawHexValue(painter, 38 + 3 * cxChar, y, address);

int x = 38 + 9 * cxChar;
ushort wchars[16];

for (int j = 0; j < 8; j++) // Draw words as octal value
{
bool okValid = false;
Expand All @@ -294,27 +327,37 @@ void QMemoryView::paintEvent(QPaintEvent * /*event*/)
painter.setPen(colorMemoryRom);
else
painter.setPen(wChanged != 0 ? colorChanged : colorText);
if (m_ByteMode)

if (m_NumeralMode == MEMMODENUM_OCT && !m_ByteMode)
DrawOctalValue(painter, x, y, word);
else if (m_NumeralMode == MEMMODENUM_OCT && m_ByteMode)
{
PrintOctalValue(buffer, (word & 0xff));
painter.drawText(x, y, buffer + 3);
PrintOctalValue(buffer, (word >> 8));
painter.drawText(x + 3 * cxChar + cxChar / 2, y, buffer + 3);
painter.drawText(x + 4 * cxChar, y, buffer + 3);
}
else if (m_NumeralMode == MEMMODENUM_HEX && !m_ByteMode)
DrawHexValue(painter, x, y, word);
else if (m_NumeralMode == MEMMODENUM_HEX && m_ByteMode)
{
PrintHexValue(buffer, word);
painter.drawText(x, y, buffer + 2);
buffer[2] = 0;
painter.drawText(x + 3 * cxChar, y, buffer);
}
else
DrawOctalValue(painter, x, y, word);
}
else
else // No value
{
if (addrtype == ADDRTYPE_IO)
{
painter.setPen(colorMemoryIO);
painter.drawText(x, y, " IO ");
painter.drawText(x + (m_PostionIncrement - cxChar) / 2 - cxChar, y, "IO");
}
else
{
painter.setPen(colorMemoryNA);
painter.drawText(x, y, " NA ");
painter.drawText(x + (m_PostionIncrement - cxChar) / 2 - cxChar, y, "NA");
}
}

Expand All @@ -329,7 +372,7 @@ void QMemoryView::paintEvent(QPaintEvent * /*event*/)
wchars[j * 2 + 1] = wch2;

address += 2;
x += 7 * cxChar;
x += m_PostionIncrement;
}
painter.setPen(colorText);

Expand All @@ -349,7 +392,7 @@ void QMemoryView::paintEvent(QPaintEvent * /*event*/)
option.initFrom(this);
option.state |= QStyle::State_KeyboardFocusChange;
option.backgroundColor = QColor(Qt::gray);
option.rect = QRect(38, cyLine + fontmetrics.descent(), 83 * cxChar, cyLine * m_nPageSize);
option.rect = QRect(38, cyLine + fontmetrics.descent(), 28 * cxChar + 8 * m_PostionIncrement, cyLine * m_nPageSize);
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this);
}
}
Expand All @@ -375,9 +418,15 @@ void QMemoryView::keyPressEvent(QKeyEvent *event)
gotoAddress();
break;
case Qt::Key_B:
case Qt::Key_W:
event->accept();
changeWordByteMode();
break;
case Qt::Key_H:
case Qt::Key_O:
event->accept();
changeNumeralMode();
break;

case Qt::Key_Up:
event->accept();
Expand Down
5 changes: 4 additions & 1 deletion emulator/qmemoryview.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class QMemoryView : public QWidget
public slots:
void changeMemoryMode();
void changeWordByteMode();
void changeNumeralMode();
void gotoAddress();
void scrollBy(qint16 delta);

Expand All @@ -40,10 +41,12 @@ protected slots:
private:
int m_Mode;
bool m_ByteMode; // false - word mode, true - byte mode
unsigned short m_wBaseAddress;
quint16 m_NumeralMode = 0;
quint16 m_wBaseAddress;
int m_cyLineMemory; // Line height in pixels
int m_nPageSize; // Page size in lines
int m_cyLine;
int m_PostionIncrement = 100; // Increment by X to the next word
QScrollBar *m_scrollbar;
QToolBar* m_toolbar;
};
Expand Down

0 comments on commit 3213f1f

Please sign in to comment.