Introduce memory editor and fix crash on dumping RDRAM

This commit is contained in:
CocoSimone
2022-10-12 01:46:22 +02:00
parent eafebc4c28
commit fc6642554c
27 changed files with 1058 additions and 214 deletions

View File

@@ -0,0 +1,742 @@
// Mini memory editor for Dear ImGui (to embed in your game/tools)
// Get latest version at http://www.github.com/ocornut/imgui_club
//
// Right-click anywhere to access the Options menu!
// You can adjust the keyboard repeat delay/rate in ImGuiIO.
// The code assume a mono-space font for simplicity!
// If you don't use the default font, use ImGui::PushFont()/PopFont() to switch to a mono-space font before calling this.
//
// Usage:
// // Create a window and draw memory editor inside it:
// static MemoryEditor mem_edit_1;
// static char data[0x10000];
// size_t data_size = 0x10000;
// mem_edit_1.DrawWindow("Memory Editor", data, data_size);
//
// Usage:
// // If you already have a window, use DrawContents() instead:
// static MemoryEditor mem_edit_2;
// ImGui::Begin("MyWindow")
// mem_edit_2.DrawContents(this, sizeof(*this), (size_t)this);
// ImGui::End();
//
// Changelog:
// - v0.10: initial version
// - v0.23 (2017/08/17): added to github. fixed right-arrow triggering a byte write.
// - v0.24 (2018/06/02): changed DragInt("Rows" to use a %d data format (which is desirable since imgui 1.61).
// - v0.25 (2018/07/11): fixed wording: all occurrences of "Rows" renamed to "Columns".
// - v0.26 (2018/08/02): fixed clicking on hex region
// - v0.30 (2018/08/02): added data preview for common data types
// - v0.31 (2018/10/10): added OptUpperCaseHex option to select lower/upper casing display [@samhocevar]
// - v0.32 (2018/10/10): changed signatures to use void* instead of unsigned char*
// - v0.33 (2018/10/10): added OptShowOptions option to hide all the interactive option setting.
// - v0.34 (2019/05/07): binary preview now applies endianness setting [@nicolasnoble]
// - v0.35 (2020/01/29): using ImGuiDataType available since Dear ImGui 1.69.
// - v0.36 (2020/05/05): minor tweaks, minor refactor.
// - v0.40 (2020/10/04): fix misuse of ImGuiListClipper API, broke with Dear ImGui 1.79. made cursor position appears on left-side of edit box. option popup appears on mouse release. fix MSVC warnings where _CRT_SECURE_NO_WARNINGS wasn't working in recent versions.
// - v0.41 (2020/10/05): fix when using with keyboard/gamepad navigation enabled.
// - v0.42 (2020/10/14): fix for . character in ASCII view always being greyed out.
// - v0.43 (2021/03/12): added OptFooterExtraHeight to allow for custom drawing at the bottom of the editor [@leiradel]
// - v0.44 (2021/03/12): use ImGuiInputTextFlags_AlwaysOverwrite in 1.82 + fix hardcoded width.
// - v0.50 (2021/11/12): various fixes for recent dear imgui versions (fixed misuse of clipper, relying on SetKeyboardFocusHere() handling scrolling from 1.85). added default size.
//
// Todo/Bugs:
// - This is generally old/crappy code, it should work but isn't very good.. to be rewritten some day.
// - PageUp/PageDown are supported because we use _NoNav. This is a good test scenario for working out idioms of how to mix natural nav and our own...
// - Arrows are being sent to the InputText() about to disappear which for LeftArrow makes the text cursor appear at position 1 for one frame.
// - Using InputText() is awkward and maybe overkill here, consider implementing something custom.
#pragma once
#include <stdio.h> // sprintf, scanf
#include <stdint.h> // uint8_t, etc.
#ifdef _MSC_VER
#define _PRISizeT "I"
#define ImSnprintf _snprintf
#else
#define _PRISizeT "z"
#define ImSnprintf snprintf
#endif
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4996) // warning C4996: 'sprintf': This function or variable may be unsafe.
#endif
struct MemoryEditor
{
enum DataFormat
{
DataFormat_Bin = 0,
DataFormat_Dec = 1,
DataFormat_Hex = 2,
DataFormat_COUNT
};
// Settings
bool Open; // = true // set to false when DrawWindow() was closed. ignore if not using DrawWindow().
bool ReadOnly; // = false // disable any editing.
int Cols; // = 16 // number of columns to display.
bool OptShowOptions; // = true // display options button/context menu. when disabled, options will be locked unless you provide your own UI for them.
bool OptShowDataPreview; // = false // display a footer previewing the decimal/binary/hex/float representation of the currently selected bytes.
bool OptShowHexII; // = false // display values in HexII representation instead of regular hexadecimal: hide null/zero bytes, ascii values as ".X".
bool OptShowAscii; // = true // display ASCII representation on the right side.
bool OptGreyOutZeroes; // = true // display null/zero bytes using the TextDisabled color.
bool OptUpperCaseHex; // = true // display hexadecimal values as "FF" instead of "ff".
int OptMidColsCount; // = 8 // set to 0 to disable extra spacing between every mid-cols.
int OptAddrDigitsCount; // = 0 // number of addr digits to display (default calculated based on maximum displayed addr).
float OptFooterExtraHeight; // = 0 // space to reserve at the bottom of the widget to add custom widgets
ImU32 HighlightColor; // // background color of highlighted bytes.
ImU8 (*ReadFn)(const ImU8* data, size_t off); // = 0 // optional handler to read bytes.
void (*WriteFn)(ImU8* data, size_t off, ImU8 d); // = 0 // optional handler to write bytes.
bool (*HighlightFn)(const ImU8* data, size_t off);//= 0 // optional handler to return Highlight property (to support non-contiguous highlighting).
// [Internal State]
bool ContentsWidthChanged;
size_t DataPreviewAddr;
size_t DataEditingAddr;
bool DataEditingTakeFocus;
char DataInputBuf[32];
char AddrInputBuf[32];
size_t GotoAddr;
size_t HighlightMin, HighlightMax;
int PreviewEndianess;
ImGuiDataType PreviewDataType;
MemoryEditor()
{
// Settings
Open = true;
ReadOnly = false;
Cols = 16;
OptShowOptions = true;
OptShowDataPreview = false;
OptShowHexII = false;
OptShowAscii = true;
OptGreyOutZeroes = true;
OptUpperCaseHex = true;
OptMidColsCount = 8;
OptAddrDigitsCount = 0;
OptFooterExtraHeight = 0.0f;
HighlightColor = IM_COL32(255, 255, 255, 50);
ReadFn = NULL;
WriteFn = NULL;
HighlightFn = NULL;
// State/Internals
ContentsWidthChanged = false;
DataPreviewAddr = DataEditingAddr = (size_t)-1;
DataEditingTakeFocus = false;
memset(DataInputBuf, 0, sizeof(DataInputBuf));
memset(AddrInputBuf, 0, sizeof(AddrInputBuf));
GotoAddr = (size_t)-1;
HighlightMin = HighlightMax = (size_t)-1;
PreviewEndianess = 0;
PreviewDataType = ImGuiDataType_S32;
}
void GotoAddrAndHighlight(size_t addr_min, size_t addr_max)
{
GotoAddr = addr_min;
HighlightMin = addr_min;
HighlightMax = addr_max;
}
struct Sizes
{
int AddrDigitsCount;
float LineHeight;
float GlyphWidth;
float HexCellWidth;
float SpacingBetweenMidCols;
float PosHexStart;
float PosHexEnd;
float PosAsciiStart;
float PosAsciiEnd;
float WindowWidth;
Sizes() { memset(this, 0, sizeof(*this)); }
};
void CalcSizes(Sizes& s, size_t mem_size, size_t base_display_addr)
{
ImGuiStyle& style = ImGui::GetStyle();
s.AddrDigitsCount = OptAddrDigitsCount;
if (s.AddrDigitsCount == 0)
for (size_t n = base_display_addr + mem_size - 1; n > 0; n >>= 4)
s.AddrDigitsCount++;
s.LineHeight = ImGui::GetTextLineHeight();
s.GlyphWidth = ImGui::CalcTextSize("F").x + 1; // We assume the font is mono-space
s.HexCellWidth = (float)(int)(s.GlyphWidth * 2.5f); // "FF " we include trailing space in the width to easily catch clicks everywhere
s.SpacingBetweenMidCols = (float)(int)(s.HexCellWidth * 0.25f); // Every OptMidColsCount columns we add a bit of extra spacing
s.PosHexStart = (s.AddrDigitsCount + 2) * s.GlyphWidth;
s.PosHexEnd = s.PosHexStart + (s.HexCellWidth * Cols);
s.PosAsciiStart = s.PosAsciiEnd = s.PosHexEnd;
if (OptShowAscii)
{
s.PosAsciiStart = s.PosHexEnd + s.GlyphWidth * 1;
if (OptMidColsCount > 0)
s.PosAsciiStart += (float)((Cols + OptMidColsCount - 1) / OptMidColsCount) * s.SpacingBetweenMidCols;
s.PosAsciiEnd = s.PosAsciiStart + Cols * s.GlyphWidth;
}
s.WindowWidth = s.PosAsciiEnd + style.ScrollbarSize + style.WindowPadding.x * 2 + s.GlyphWidth;
}
// Standalone Memory Editor window
void DrawWindow(const char* title, void* mem_data, size_t mem_size, size_t base_display_addr = 0x0000)
{
Sizes s;
CalcSizes(s, mem_size, base_display_addr);
ImGui::SetNextWindowSize(ImVec2(s.WindowWidth, s.WindowWidth * 0.60f), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, 0.0f), ImVec2(s.WindowWidth, FLT_MAX));
Open = true;
if (ImGui::Begin(title, &Open, ImGuiWindowFlags_NoScrollbar))
{
if (ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows) && ImGui::IsMouseReleased(ImGuiMouseButton_Right))
ImGui::OpenPopup("context");
DrawContents(mem_data, mem_size, base_display_addr);
if (ContentsWidthChanged)
{
CalcSizes(s, mem_size, base_display_addr);
ImGui::SetWindowSize(ImVec2(s.WindowWidth, ImGui::GetWindowSize().y));
}
}
ImGui::End();
}
// Memory Editor contents only
void DrawContents(void* mem_data_void, size_t mem_size, size_t base_display_addr = 0x0000)
{
if (Cols < 1)
Cols = 1;
ImU8* mem_data = (ImU8*)mem_data_void;
Sizes s;
CalcSizes(s, mem_size, base_display_addr);
ImGuiStyle& style = ImGui::GetStyle();
// We begin into our scrolling region with the 'ImGuiWindowFlags_NoMove' in order to prevent click from moving the window.
// This is used as a facility since our main click detection code doesn't assign an ActiveId so the click would normally be caught as a window-move.
const float height_separator = style.ItemSpacing.y;
float footer_height = OptFooterExtraHeight;
if (OptShowOptions)
footer_height += height_separator + ImGui::GetFrameHeightWithSpacing() * 1;
if (OptShowDataPreview)
footer_height += height_separator + ImGui::GetFrameHeightWithSpacing() * 1 + ImGui::GetTextLineHeightWithSpacing() * 3;
ImGui::BeginChild("##scrolling", ImVec2(0, -footer_height), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
// We are not really using the clipper API correctly here, because we rely on visible_start_addr/visible_end_addr for our scrolling function.
const int line_total_count = (int)((mem_size + Cols - 1) / Cols);
ImGuiListClipper clipper;
clipper.Begin(line_total_count, s.LineHeight);
bool data_next = false;
if (ReadOnly || DataEditingAddr >= mem_size)
DataEditingAddr = (size_t)-1;
if (DataPreviewAddr >= mem_size)
DataPreviewAddr = (size_t)-1;
size_t preview_data_type_size = OptShowDataPreview ? DataTypeGetSize(PreviewDataType) : 0;
size_t data_editing_addr_next = (size_t)-1;
if (DataEditingAddr != (size_t)-1)
{
// Move cursor but only apply on next frame so scrolling with be synchronized (because currently we can't change the scrolling while the window is being rendered)
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow)) && (ptrdiff_t)DataEditingAddr >= (ptrdiff_t)Cols) { data_editing_addr_next = DataEditingAddr - Cols; }
else if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_DownArrow)) && (ptrdiff_t)DataEditingAddr < (ptrdiff_t)mem_size - Cols) { data_editing_addr_next = DataEditingAddr + Cols; }
else if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_LeftArrow)) && (ptrdiff_t)DataEditingAddr > (ptrdiff_t)0) { data_editing_addr_next = DataEditingAddr - 1; }
else if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_RightArrow)) && (ptrdiff_t)DataEditingAddr < (ptrdiff_t)mem_size - 1) { data_editing_addr_next = DataEditingAddr + 1; }
}
// Draw vertical separator
ImVec2 window_pos = ImGui::GetWindowPos();
if (OptShowAscii)
draw_list->AddLine(ImVec2(window_pos.x + s.PosAsciiStart - s.GlyphWidth, window_pos.y), ImVec2(window_pos.x + s.PosAsciiStart - s.GlyphWidth, window_pos.y + 9999), ImGui::GetColorU32(ImGuiCol_Border));
const ImU32 color_text = ImGui::GetColorU32(ImGuiCol_Text);
const ImU32 color_disabled = OptGreyOutZeroes ? ImGui::GetColorU32(ImGuiCol_TextDisabled) : color_text;
const char* format_address = OptUpperCaseHex ? "%0*" _PRISizeT "X: " : "%0*" _PRISizeT "x: ";
const char* format_data = OptUpperCaseHex ? "%0*" _PRISizeT "X" : "%0*" _PRISizeT "x";
const char* format_byte = OptUpperCaseHex ? "%02X" : "%02x";
const char* format_byte_space = OptUpperCaseHex ? "%02X " : "%02x ";
while (clipper.Step())
for (int line_i = clipper.DisplayStart; line_i < clipper.DisplayEnd; line_i++) // display only visible lines
{
size_t addr = (size_t)(line_i * Cols);
ImGui::Text(format_address, s.AddrDigitsCount, base_display_addr + addr);
// Draw Hexadecimal
for (int n = 0; n < Cols && addr < mem_size; n++, addr++)
{
float byte_pos_x = s.PosHexStart + s.HexCellWidth * n;
if (OptMidColsCount > 0)
byte_pos_x += (float)(n / OptMidColsCount) * s.SpacingBetweenMidCols;
ImGui::SameLine(byte_pos_x);
// Draw highlight
bool is_highlight_from_user_range = (addr >= HighlightMin && addr < HighlightMax);
bool is_highlight_from_user_func = (HighlightFn && HighlightFn(mem_data, addr));
bool is_highlight_from_preview = (addr >= DataPreviewAddr && addr < DataPreviewAddr + preview_data_type_size);
if (is_highlight_from_user_range || is_highlight_from_user_func || is_highlight_from_preview)
{
ImVec2 pos = ImGui::GetCursorScreenPos();
float highlight_width = s.GlyphWidth * 2;
bool is_next_byte_highlighted = (addr + 1 < mem_size) && ((HighlightMax != (size_t)-1 && addr + 1 < HighlightMax) || (HighlightFn && HighlightFn(mem_data, addr + 1)));
if (is_next_byte_highlighted || (n + 1 == Cols))
{
highlight_width = s.HexCellWidth;
if (OptMidColsCount > 0 && n > 0 && (n + 1) < Cols && ((n + 1) % OptMidColsCount) == 0)
highlight_width += s.SpacingBetweenMidCols;
}
draw_list->AddRectFilled(pos, ImVec2(pos.x + highlight_width, pos.y + s.LineHeight), HighlightColor);
}
if (DataEditingAddr == addr)
{
// Display text input on current byte
bool data_write = false;
ImGui::PushID((void*)addr);
if (DataEditingTakeFocus)
{
ImGui::SetKeyboardFocusHere(0);
sprintf(AddrInputBuf, format_data, s.AddrDigitsCount, base_display_addr + addr);
sprintf(DataInputBuf, format_byte, ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]);
}
struct UserData
{
// FIXME: We should have a way to retrieve the text edit cursor position more easily in the API, this is rather tedious. This is such a ugly mess we may be better off not using InputText() at all here.
static int Callback(ImGuiInputTextCallbackData* data)
{
UserData* user_data = (UserData*)data->UserData;
if (!data->HasSelection())
user_data->CursorPos = data->CursorPos;
if (data->SelectionStart == 0 && data->SelectionEnd == data->BufTextLen)
{
// When not editing a byte, always refresh its InputText content pulled from underlying memory data
// (this is a bit tricky, since InputText technically "owns" the master copy of the buffer we edit it in there)
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, user_data->CurrentBufOverwrite);
data->SelectionStart = 0;
data->SelectionEnd = 2;
data->CursorPos = 0;
}
return 0;
}
char CurrentBufOverwrite[3]; // Input
int CursorPos; // Output
};
UserData user_data;
user_data.CursorPos = -1;
sprintf(user_data.CurrentBufOverwrite, format_byte, ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]);
ImGuiInputTextFlags flags = ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoHorizontalScroll | ImGuiInputTextFlags_CallbackAlways;
#if IMGUI_VERSION_NUM >= 18104
flags |= ImGuiInputTextFlags_AlwaysOverwrite;
#else
flags |= ImGuiInputTextFlags_AlwaysInsertMode;
#endif
ImGui::SetNextItemWidth(s.GlyphWidth * 2);
if (ImGui::InputText("##data", DataInputBuf, IM_ARRAYSIZE(DataInputBuf), flags, UserData::Callback, &user_data))
data_write = data_next = true;
else if (!DataEditingTakeFocus && !ImGui::IsItemActive())
DataEditingAddr = data_editing_addr_next = (size_t)-1;
DataEditingTakeFocus = false;
if (user_data.CursorPos >= 2)
data_write = data_next = true;
if (data_editing_addr_next != (size_t)-1)
data_write = data_next = false;
unsigned int data_input_value = 0;
if (data_write && sscanf(DataInputBuf, "%X", &data_input_value) == 1)
{
if (WriteFn)
WriteFn(mem_data, addr, (ImU8)data_input_value);
else
mem_data[addr] = (ImU8)data_input_value;
}
ImGui::PopID();
}
else
{
// NB: The trailing space is not visible but ensure there's no gap that the mouse cannot click on.
ImU8 b = ReadFn ? ReadFn(mem_data, addr) : mem_data[addr];
if (OptShowHexII)
{
if ((b >= 32 && b < 128))
ImGui::Text(".%c ", b);
else if (b == 0xFF && OptGreyOutZeroes)
ImGui::TextDisabled("## ");
else if (b == 0x00)
ImGui::Text(" ");
else
ImGui::Text(format_byte_space, b);
}
else
{
if (b == 0 && OptGreyOutZeroes)
ImGui::TextDisabled("00 ");
else
ImGui::Text(format_byte_space, b);
}
if (!ReadOnly && ImGui::IsItemHovered() && ImGui::IsMouseClicked(0))
{
DataEditingTakeFocus = true;
data_editing_addr_next = addr;
}
}
}
if (OptShowAscii)
{
// Draw ASCII values
ImGui::SameLine(s.PosAsciiStart);
ImVec2 pos = ImGui::GetCursorScreenPos();
addr = line_i * Cols;
ImGui::PushID(line_i);
if (ImGui::InvisibleButton("ascii", ImVec2(s.PosAsciiEnd - s.PosAsciiStart, s.LineHeight)))
{
DataEditingAddr = DataPreviewAddr = addr + (size_t)((ImGui::GetIO().MousePos.x - pos.x) / s.GlyphWidth);
DataEditingTakeFocus = true;
}
ImGui::PopID();
for (int n = 0; n < Cols && addr < mem_size; n++, addr++)
{
if (addr == DataEditingAddr)
{
draw_list->AddRectFilled(pos, ImVec2(pos.x + s.GlyphWidth, pos.y + s.LineHeight), ImGui::GetColorU32(ImGuiCol_FrameBg));
draw_list->AddRectFilled(pos, ImVec2(pos.x + s.GlyphWidth, pos.y + s.LineHeight), ImGui::GetColorU32(ImGuiCol_TextSelectedBg));
}
unsigned char c = ReadFn ? ReadFn(mem_data, addr) : mem_data[addr];
char display_c = (c < 32 || c >= 128) ? '.' : c;
draw_list->AddText(pos, (display_c == c) ? color_text : color_disabled, &display_c, &display_c + 1);
pos.x += s.GlyphWidth;
}
}
}
ImGui::PopStyleVar(2);
ImGui::EndChild();
// Notify the main window of our ideal child content size (FIXME: we are missing an API to get the contents size from the child)
ImGui::SetCursorPosX(s.WindowWidth);
if (data_next && DataEditingAddr + 1 < mem_size)
{
DataEditingAddr = DataPreviewAddr = DataEditingAddr + 1;
DataEditingTakeFocus = true;
}
else if (data_editing_addr_next != (size_t)-1)
{
DataEditingAddr = DataPreviewAddr = data_editing_addr_next;
DataEditingTakeFocus = true;
}
const bool lock_show_data_preview = OptShowDataPreview;
if (OptShowOptions)
{
ImGui::Separator();
DrawOptionsLine(s, mem_data, mem_size, base_display_addr);
}
if (lock_show_data_preview)
{
ImGui::Separator();
DrawPreviewLine(s, mem_data, mem_size, base_display_addr);
}
}
void DrawOptionsLine(const Sizes& s, void* mem_data, size_t mem_size, size_t base_display_addr)
{
IM_UNUSED(mem_data);
ImGuiStyle& style = ImGui::GetStyle();
const char* format_range = OptUpperCaseHex ? "Range %0*" _PRISizeT "X..%0*" _PRISizeT "X" : "Range %0*" _PRISizeT "x..%0*" _PRISizeT "x";
// Options menu
if (ImGui::Button("Options"))
ImGui::OpenPopup("context");
if (ImGui::BeginPopup("context"))
{
ImGui::SetNextItemWidth(s.GlyphWidth * 7 + style.FramePadding.x * 2.0f);
if (ImGui::DragInt("##cols", &Cols, 0.2f, 4, 32, "%d cols")) { ContentsWidthChanged = true; if (Cols < 1) Cols = 1; }
ImGui::Checkbox("Show Data Preview", &OptShowDataPreview);
ImGui::Checkbox("Show HexII", &OptShowHexII);
if (ImGui::Checkbox("Show Ascii", &OptShowAscii)) { ContentsWidthChanged = true; }
ImGui::Checkbox("Grey out zeroes", &OptGreyOutZeroes);
ImGui::Checkbox("Uppercase Hex", &OptUpperCaseHex);
ImGui::EndPopup();
}
ImGui::SameLine();
ImGui::Text(format_range, s.AddrDigitsCount, base_display_addr, s.AddrDigitsCount, base_display_addr + mem_size - 1);
ImGui::SameLine();
ImGui::SetNextItemWidth((s.AddrDigitsCount + 1) * s.GlyphWidth + style.FramePadding.x * 2.0f);
if (ImGui::InputText("##addr", AddrInputBuf, IM_ARRAYSIZE(AddrInputBuf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_EnterReturnsTrue))
{
size_t goto_addr;
if (sscanf(AddrInputBuf, "%" _PRISizeT "X", &goto_addr) == 1)
{
GotoAddr = goto_addr - base_display_addr;
HighlightMin = HighlightMax = (size_t)-1;
}
}
if (GotoAddr != (size_t)-1)
{
if (GotoAddr < mem_size)
{
ImGui::BeginChild("##scrolling");
ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + (GotoAddr / Cols) * ImGui::GetTextLineHeight());
ImGui::EndChild();
DataEditingAddr = DataPreviewAddr = GotoAddr;
DataEditingTakeFocus = true;
}
GotoAddr = (size_t)-1;
}
}
void DrawPreviewLine(const Sizes& s, void* mem_data_void, size_t mem_size, size_t base_display_addr)
{
IM_UNUSED(base_display_addr);
ImU8* mem_data = (ImU8*)mem_data_void;
ImGuiStyle& style = ImGui::GetStyle();
ImGui::AlignTextToFramePadding();
ImGui::Text("Preview as:");
ImGui::SameLine();
ImGui::SetNextItemWidth((s.GlyphWidth * 10.0f) + style.FramePadding.x * 2.0f + style.ItemInnerSpacing.x);
if (ImGui::BeginCombo("##combo_type", DataTypeGetDesc(PreviewDataType), ImGuiComboFlags_HeightLargest))
{
for (int n = 0; n < ImGuiDataType_COUNT; n++)
if (ImGui::Selectable(DataTypeGetDesc((ImGuiDataType)n), PreviewDataType == n))
PreviewDataType = (ImGuiDataType)n;
ImGui::EndCombo();
}
ImGui::SameLine();
ImGui::SetNextItemWidth((s.GlyphWidth * 6.0f) + style.FramePadding.x * 2.0f + style.ItemInnerSpacing.x);
ImGui::Combo("##combo_endianess", &PreviewEndianess, "LE\0BE\0\0");
char buf[128] = "";
float x = s.GlyphWidth * 6.0f;
bool has_value = DataPreviewAddr != (size_t)-1;
if (has_value)
DrawPreviewData(DataPreviewAddr, mem_data, mem_size, PreviewDataType, DataFormat_Dec, buf, (size_t)IM_ARRAYSIZE(buf));
ImGui::Text("Dec"); ImGui::SameLine(x); ImGui::TextUnformatted(has_value ? buf : "N/A");
if (has_value)
DrawPreviewData(DataPreviewAddr, mem_data, mem_size, PreviewDataType, DataFormat_Hex, buf, (size_t)IM_ARRAYSIZE(buf));
ImGui::Text("Hex"); ImGui::SameLine(x); ImGui::TextUnformatted(has_value ? buf : "N/A");
if (has_value)
DrawPreviewData(DataPreviewAddr, mem_data, mem_size, PreviewDataType, DataFormat_Bin, buf, (size_t)IM_ARRAYSIZE(buf));
buf[IM_ARRAYSIZE(buf) - 1] = 0;
ImGui::Text("Bin"); ImGui::SameLine(x); ImGui::TextUnformatted(has_value ? buf : "N/A");
}
// Utilities for Data Preview
const char* DataTypeGetDesc(ImGuiDataType data_type) const
{
const char* descs[] = { "Int8", "Uint8", "Int16", "Uint16", "Int32", "Uint32", "Int64", "Uint64", "Float", "Double" };
IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT);
return descs[data_type];
}
size_t DataTypeGetSize(ImGuiDataType data_type) const
{
const size_t sizes[] = { 1, 1, 2, 2, 4, 4, 8, 8, sizeof(float), sizeof(double) };
IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT);
return sizes[data_type];
}
const char* DataFormatGetDesc(DataFormat data_format) const
{
const char* descs[] = { "Bin", "Dec", "Hex" };
IM_ASSERT(data_format >= 0 && data_format < DataFormat_COUNT);
return descs[data_format];
}
bool IsBigEndian() const
{
uint16_t x = 1;
char c[2];
memcpy(c, &x, 2);
return c[0] != 0;
}
static void* EndianessCopyBigEndian(void* _dst, void* _src, size_t s, int is_little_endian)
{
if (is_little_endian)
{
uint8_t* dst = (uint8_t*)_dst;
uint8_t* src = (uint8_t*)_src + s - 1;
for (int i = 0, n = (int)s; i < n; ++i)
memcpy(dst++, src--, 1);
return _dst;
}
else
{
return memcpy(_dst, _src, s);
}
}
static void* EndianessCopyLittleEndian(void* _dst, void* _src, size_t s, int is_little_endian)
{
if (is_little_endian)
{
return memcpy(_dst, _src, s);
}
else
{
uint8_t* dst = (uint8_t*)_dst;
uint8_t* src = (uint8_t*)_src + s - 1;
for (int i = 0, n = (int)s; i < n; ++i)
memcpy(dst++, src--, 1);
return _dst;
}
}
void* EndianessCopy(void* dst, void* src, size_t size) const
{
static void* (*fp)(void*, void*, size_t, int) = NULL;
if (fp == NULL)
fp = IsBigEndian() ? EndianessCopyBigEndian : EndianessCopyLittleEndian;
return fp(dst, src, size, PreviewEndianess);
}
const char* FormatBinary(const uint8_t* buf, int width) const
{
IM_ASSERT(width <= 64);
size_t out_n = 0;
static char out_buf[64 + 8 + 1];
int n = width / 8;
for (int j = n - 1; j >= 0; --j)
{
for (int i = 0; i < 8; ++i)
out_buf[out_n++] = (buf[j] & (1 << (7 - i))) ? '1' : '0';
out_buf[out_n++] = ' ';
}
IM_ASSERT(out_n < IM_ARRAYSIZE(out_buf));
out_buf[out_n] = 0;
return out_buf;
}
// [Internal]
void DrawPreviewData(size_t addr, const ImU8* mem_data, size_t mem_size, ImGuiDataType data_type, DataFormat data_format, char* out_buf, size_t out_buf_size) const
{
uint8_t buf[8];
size_t elem_size = DataTypeGetSize(data_type);
size_t size = addr + elem_size > mem_size ? mem_size - addr : elem_size;
if (ReadFn)
for (int i = 0, n = (int)size; i < n; ++i)
buf[i] = ReadFn(mem_data, addr + i);
else
memcpy(buf, mem_data + addr, size);
if (data_format == DataFormat_Bin)
{
uint8_t binbuf[8];
EndianessCopy(binbuf, buf, size);
ImSnprintf(out_buf, out_buf_size, "%s", FormatBinary(binbuf, (int)size * 8));
return;
}
out_buf[0] = 0;
switch (data_type)
{
case ImGuiDataType_S8:
{
int8_t int8 = 0;
EndianessCopy(&int8, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%hhd", int8); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%02x", int8 & 0xFF); return; }
break;
}
case ImGuiDataType_U8:
{
uint8_t uint8 = 0;
EndianessCopy(&uint8, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%hhu", uint8); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%02x", uint8 & 0XFF); return; }
break;
}
case ImGuiDataType_S16:
{
int16_t int16 = 0;
EndianessCopy(&int16, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%hd", int16); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%04x", int16 & 0xFFFF); return; }
break;
}
case ImGuiDataType_U16:
{
uint16_t uint16 = 0;
EndianessCopy(&uint16, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%hu", uint16); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%04x", uint16 & 0xFFFF); return; }
break;
}
case ImGuiDataType_S32:
{
int32_t int32 = 0;
EndianessCopy(&int32, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%d", int32); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%08x", int32); return; }
break;
}
case ImGuiDataType_U32:
{
uint32_t uint32 = 0;
EndianessCopy(&uint32, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%u", uint32); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%08x", uint32); return; }
break;
}
case ImGuiDataType_S64:
{
int64_t int64 = 0;
EndianessCopy(&int64, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%lld", (long long)int64); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%016llx", (long long)int64); return; }
break;
}
case ImGuiDataType_U64:
{
uint64_t uint64 = 0;
EndianessCopy(&uint64, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%llu", (long long)uint64); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "0x%016llx", (long long)uint64); return; }
break;
}
case ImGuiDataType_Float:
{
float float32 = 0.0f;
EndianessCopy(&float32, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%f", float32); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "%a", float32); return; }
break;
}
case ImGuiDataType_Double:
{
double float64 = 0.0;
EndianessCopy(&float64, buf, size);
if (data_format == DataFormat_Dec) { ImSnprintf(out_buf, out_buf_size, "%f", float64); return; }
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "%a", float64); return; }
break;
}
case ImGuiDataType_COUNT:
break;
} // Switch
IM_ASSERT(0); // Shouldn't reach
}
};
#undef _PRISizeT
#undef ImSnprintf
#ifdef _MSC_VER
#pragma warning (pop)
#endif

View File

@@ -139,7 +139,7 @@ WSI* LoadWSIPlatform(Vulkan::WSIPlatform* wsi_platform, std::unique_ptr<Parallel
return wsi; return wsi;
} }
void LoadParallelRDP(u8* rdram) { void LoadParallelRDP(const u8* rdram, SDL_Window* window) {
ResourceLayout vertLayout; ResourceLayout vertLayout;
ResourceLayout fragLayout; ResourceLayout fragLayout;
@@ -181,10 +181,10 @@ void LoadParallelRDP(u8* rdram) {
} }
} }
void InitParallelRDP(u8* rdram, SDL_Window* window) { void InitParallelRDP(const u8* rdram, SDL_Window* window) {
g_Window = window; g_Window = window;
LoadWSIPlatform(new SDLWSIPlatform(), std::make_unique<SDLParallelRdpWindowInfo>()); LoadWSIPlatform(new SDLWSIPlatform(), std::make_unique<SDLParallelRdpWindowInfo>());
LoadParallelRDP(rdram); LoadParallelRDP(rdram, window);
} }
void DrawFullscreenTexturedQuad(Util::IntrusivePtr<Image> image, Util::IntrusivePtr<CommandBuffer> cmd) { void DrawFullscreenTexturedQuad(Util::IntrusivePtr<Image> image, Util::IntrusivePtr<CommandBuffer> cmd) {

View File

@@ -33,7 +33,7 @@ uint32_t GetVkGraphicsQueueFamily();
VkFormat GetVkFormat(); VkFormat GetVkFormat();
VkCommandBuffer GetVkCommandBuffer(); VkCommandBuffer GetVkCommandBuffer();
void SubmitRequestedVkCommandBuffer(); void SubmitRequestedVkCommandBuffer();
void InitParallelRDP(u8* rdram, SDL_Window*); void InitParallelRDP(const u8* rdram, SDL_Window* window);
void UpdateScreenParallelRdp(n64::Core& core, Window& imguiWindow, n64::VI& vi); void UpdateScreenParallelRdp(n64::Core& core, Window& imguiWindow, n64::VI& vi);
void ParallelRdpEnqueueCommand(int command_length, u32* buffer); void ParallelRdpEnqueueCommand(int command_length, u32* buffer);
void ParallelRdpOnFullSync(); void ParallelRdpOnFullSync();

View File

@@ -14,7 +14,7 @@ using json = nlohmann::json;
Window::Window(n64::Core& core) { Window::Window(n64::Core& core) {
InitSDL(); InitSDL();
InitParallelRDP(core.mem.GetRDRAM(), window); InitParallelRDP(core.mem.GetRDRAM(), window);
InitImgui(); InitImgui(core);
NFD::Init(); NFD::Init();
} }
@@ -27,6 +27,7 @@ void Window::InitSDL() {
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
n64::InitAudio(); n64::InitAudio();
windowTitle = "natsukashii";
window = SDL_CreateWindow( window = SDL_CreateWindow(
"natsukashii", "natsukashii",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
@@ -48,7 +49,19 @@ static void check_vk_result(VkResult err) {
} }
} }
void Window::InitImgui() { inline ImU8 readHandler(const ImU8* core_, size_t offset) {
auto* core = reinterpret_cast<n64::Core*>(const_cast<ImU8*>(core_));
auto& mem = core->mem;
return mem.Read8<false, false>(core->cpu.regs, offset, core->cpu.regs.oldPC);
}
inline void writeHandler(ImU8* core_, size_t offset, ImU8 value) {
auto* core = reinterpret_cast<n64::Core*>(const_cast<ImU8*>(core_));
auto& mem = core->mem;
mem.Write8<false, false>(core->cpu.regs, offset, value, core->cpu.regs.oldPC);
}
void Window::InitImgui(const n64::Core& core) {
VkResult err; VkResult err;
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
@@ -122,6 +135,9 @@ void Window::InitImgui() {
ImGui_ImplVulkan_CreateFontsTexture(commandBuffer); ImGui_ImplVulkan_CreateFontsTexture(commandBuffer);
SubmitRequestedVkCommandBuffer(); SubmitRequestedVkCommandBuffer();
} }
memoryEditor.ReadFn = readHandler;
memoryEditor.WriteFn = writeHandler;
} }
Window::~Window() { Window::~Window() {
@@ -160,6 +176,7 @@ void Window::LoadROM(n64::Core& core, const std::string &path) {
} }
windowTitle = "natsukashii - " + name; windowTitle = "natsukashii - " + name;
shadowWindowTitle = windowTitle;
SDL_SetWindowTitle(window, windowTitle.c_str()); SDL_SetWindowTitle(window, windowTitle.c_str());
} }
@@ -167,9 +184,18 @@ void Window::LoadROM(n64::Core& core, const std::string &path) {
void Window::Render(n64::Core& core) { void Window::Render(n64::Core& core) {
ImGui::PushFont(uiFont); ImGui::PushFont(uiFont);
u32 ticks = SDL_GetTicks();
if(!core.pause && lastFrame < ticks - 1000) {
lastFrame = ticks;
windowTitle += fmt::format(" | {:02d} In-Game FPS", core.mem.mmio.vi.swaps);
core.mem.mmio.vi.swaps = 0;
SDL_SetWindowTitle(window, windowTitle.c_str());
windowTitle = shadowWindowTitle;
}
static bool showSettings = false; static bool showSettings = false;
static std::string windowTitleCache = windowTitle;
bool showMainMenuBar = windowID == SDL_GetWindowID(SDL_GetMouseFocus()); bool showMainMenuBar = windowID == SDL_GetWindowID(SDL_GetMouseFocus());
memoryEditor.DrawWindow("Memory viewer", &core, 0x80000000);
if(showMainMenuBar) { if(showMainMenuBar) {
ImGui::BeginMainMenuBar(); ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File")) { if (ImGui::BeginMenu("File")) {
@@ -183,18 +209,10 @@ void Window::Render(n64::Core& core) {
} }
} }
if (ImGui::MenuItem("Dump RDRAM")) { if (ImGui::MenuItem("Dump RDRAM")) {
FILE *fp = fopen("rdram.dump", "wb"); core.mem.DumpRDRAM();
u8 *temp = core.mem.GetRDRAM();
util::SwapBuffer32(RDRAM_SIZE, temp);
fwrite(temp, 1, RDRAM_SIZE, fp);
fclose(fp);
} }
if (ImGui::MenuItem("Dump IMEM")) { if (ImGui::MenuItem("Dump IMEM")) {
FILE *fp = fopen("imem.dump", "wb"); core.mem.DumpIMEM();
u8 *temp = core.mem.mmio.rsp.imem;
util::SwapBuffer32(IMEM_SIZE, temp);
fwrite(temp, 1, IMEM_SIZE, fp);
fclose(fp);
} }
if (ImGui::MenuItem("Exit")) { if (ImGui::MenuItem("Exit")) {
core.done = true; core.done = true;
@@ -212,12 +230,11 @@ void Window::Render(n64::Core& core) {
} }
if (ImGui::MenuItem(core.pause ? "Resume" : "Pause", nullptr, false, core.romLoaded)) { if (ImGui::MenuItem(core.pause ? "Resume" : "Pause", nullptr, false, core.romLoaded)) {
core.TogglePause(); core.TogglePause();
std::string paused = " | Paused";
if(core.pause) { if(core.pause) {
windowTitleCache = windowTitle; shadowWindowTitle = windowTitle;
windowTitle += paused; windowTitle += " | Paused";
} else { } else {
windowTitle = windowTitleCache; windowTitle = shadowWindowTitle;
} }
SDL_SetWindowTitle(window, windowTitle.c_str()); SDL_SetWindowTitle(window, windowTitle.c_str());
} }
@@ -237,10 +254,10 @@ void Window::Render(n64::Core& core) {
if (!lockVolume) { if (!lockVolume) {
ImGui::SliderFloat("Volume R", &volumeR, 0, 1, "%.2f", ImGuiSliderFlags_NoInput); ImGui::SliderFloat("Volume R", &volumeR, 0, 1, "%.2f", ImGuiSliderFlags_NoInput);
} else { } else {
volumeR = volumeL;
ImGui::BeginDisabled(); ImGui::BeginDisabled();
ImGui::SliderFloat("Volume R", &volumeR, 0, 1, "%.2f", ImGuiSliderFlags_NoInput); ImGui::SliderFloat("Volume R", &volumeR, 0, 1, "%.2f", ImGuiSliderFlags_NoInput);
ImGui::EndDisabled(); ImGui::EndDisabled();
volumeR = volumeL;
} }
ImGui::EndPopup(); ImGui::EndPopup();
} }

View File

@@ -3,6 +3,7 @@
#include <imgui.h> #include <imgui.h>
#include <imgui_impl_sdl.h> #include <imgui_impl_sdl.h>
#include <imgui_impl_vulkan.h> #include <imgui_impl_vulkan.h>
#include <imgui_memory_editor.h>
#include <SDL.h> #include <SDL.h>
#include <Core.hpp> #include <Core.hpp>
#include <vector> #include <vector>
@@ -19,11 +20,12 @@ struct Window {
void LoadROM(n64::Core& core, const std::string& path); void LoadROM(n64::Core& core, const std::string& path);
private: private:
bool lockVolume = true; bool lockVolume = true;
bool showSettings = false;
SDL_Window* window; SDL_Window* window;
std::string windowTitle; std::string windowTitle;
std::string shadowWindowTitle;
u32 lastFrame = 0;
void InitSDL(); void InitSDL();
void InitImgui(); void InitImgui(const n64::Core& core);
void Render(n64::Core& core); void Render(n64::Core& core);
VkPhysicalDevice physicalDevice{}; VkPhysicalDevice physicalDevice{};
@@ -34,6 +36,8 @@ private:
VkDescriptorPool descriptorPool{}; VkDescriptorPool descriptorPool{};
VkAllocationCallbacks* allocator{}; VkAllocationCallbacks* allocator{};
MemoryEditor memoryEditor;
u32 minImageCount = 2; u32 minImageCount = 2;
bool rebuildSwapchain = false; bool rebuildSwapchain = false;
}; };

View File

@@ -19,31 +19,46 @@ void MMIO::Reset() {
si.Reset(); si.Reset();
} }
template <bool crashOnUnimplemented>
u32 MMIO::Read(u32 addr) { u32 MMIO::Read(u32 addr) {
switch (addr) { switch (addr) {
case 0x04040000 ... 0x040FFFFF: return rsp.Read(addr); case 0x04040000 ... 0x040FFFFF: return rsp.Read<crashOnUnimplemented>(addr);
case 0x04100000 ... 0x041FFFFF: return rdp.Read(addr); case 0x04100000 ... 0x041FFFFF: return rdp.Read<crashOnUnimplemented>(addr);
case 0x04300000 ... 0x043FFFFF: return mi.Read(addr); case 0x04300000 ... 0x043FFFFF: return mi.Read<crashOnUnimplemented>(addr);
case 0x04400000 ... 0x044FFFFF: return vi.Read(addr); case 0x04400000 ... 0x044FFFFF: return vi.Read<crashOnUnimplemented>(addr);
case 0x04500000 ... 0x045FFFFF: return ai.Read(addr); case 0x04500000 ... 0x045FFFFF: return ai.Read(addr);
case 0x04600000 ... 0x046FFFFF: return pi.Read(mi, addr); case 0x04600000 ... 0x046FFFFF: return pi.Read<crashOnUnimplemented>(mi, addr);
case 0x04700000 ... 0x047FFFFF: return ri.Read(addr); case 0x04700000 ... 0x047FFFFF: return ri.Read<crashOnUnimplemented>(addr);
case 0x04800000 ... 0x048FFFFF: return si.Read(mi, addr); case 0x04800000 ... 0x048FFFFF: return si.Read<crashOnUnimplemented>(mi, addr);
default: util::panic("Unhandled mmio read at addr {:08X}\n", addr); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled mmio read at addr {:08X}\n", addr);
}
return 0;
} }
} }
template u32 MMIO::Read<true>(u32);
template u32 MMIO::Read<false>(u32);
template <bool crashOnUnimplemented>
void MMIO::Write(Mem& mem, Registers& regs, u32 addr, u32 val) { void MMIO::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
switch (addr) { switch (addr) {
case 0x04040000 ... 0x040FFFFF: rsp.Write(mem, regs, addr, val); break; case 0x04040000 ... 0x040FFFFF: rsp.Write<crashOnUnimplemented>(mem, regs, addr, val); break;
case 0x04100000 ... 0x041FFFFF: rdp.Write(mi, regs, rsp, addr, val); break; case 0x04100000 ... 0x041FFFFF: rdp.Write<crashOnUnimplemented>(mi, regs, rsp, addr, val); break;
case 0x04300000 ... 0x043FFFFF: mi.Write(regs, addr, val); break; case 0x04300000 ... 0x043FFFFF: mi.Write<crashOnUnimplemented>(regs, addr, val); break;
case 0x04400000 ... 0x044FFFFF: vi.Write(mi, regs, addr, val); break; case 0x04400000 ... 0x044FFFFF: vi.Write<crashOnUnimplemented>(mi, regs, addr, val); break;
case 0x04500000 ... 0x045FFFFF: ai.Write(mem, regs, addr, val); break; case 0x04500000 ... 0x045FFFFF: ai.Write<crashOnUnimplemented>(mem, regs, addr, val); break;
case 0x04600000 ... 0x046FFFFF: pi.Write(mem, regs, addr, val); break; case 0x04600000 ... 0x046FFFFF: pi.Write<crashOnUnimplemented>(mem, regs, addr, val); break;
case 0x04700000 ... 0x047FFFFF: ri.Write(addr, val); break; case 0x04700000 ... 0x047FFFFF: ri.Write<crashOnUnimplemented>(addr, val); break;
case 0x04800000 ... 0x048FFFFF: si.Write(mem, regs, addr, val); break; case 0x04800000 ... 0x048FFFFF: si.Write<crashOnUnimplemented>(mem, regs, addr, val); break;
default: util::panic("Unhandled mmio write at addr {:08X} with val {:08X}\n", addr, val); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled mmio write at addr {:08X} with val {:08X}\n", addr, val);
} }
} }
} }
template void MMIO::Write<true>(Mem&, Registers&, u32, u32);
template void MMIO::Write<false>(Mem&, Registers&, u32, u32);
}

View File

@@ -24,7 +24,9 @@ struct MMIO {
RSP rsp; RSP rsp;
RDP rdp; RDP rdp;
template <bool crashOnUnimplemented = true>
u32 Read(u32); u32 Read(u32);
void Write(Mem&, Registers& regs, u32, u32); template <bool crashOnUnimplemented = true>
void Write(Mem&, Registers&, u32, u32);
}; };
} }

View File

@@ -14,6 +14,8 @@ void Mem::Reset() {
std::fill(sram.begin(), sram.end(), 0); std::fill(sram.begin(), sram.end(), 0);
romMask = 0; romMask = 0;
mmio.Reset(); mmio.Reset();
cart.resize(0xFC00000);
std::fill(cart.begin(), cart.end(), 0);
} }
CartInfo Mem::LoadROM(const std::string& filename) { CartInfo Mem::LoadROM(const std::string& filename) {
@@ -68,7 +70,7 @@ bool MapVAddr(Registers& regs, TLBAccessType accessType, u64 vaddr, u32& paddr)
template bool MapVAddr<true>(Registers& regs, TLBAccessType accessType, u64 vaddr, u32& paddr); template bool MapVAddr<true>(Registers& regs, TLBAccessType accessType, u64 vaddr, u32& paddr);
template bool MapVAddr<false>(Registers& regs, TLBAccessType accessType, u64 vaddr, u32& paddr); template bool MapVAddr<false>(Registers& regs, TLBAccessType accessType, u64 vaddr, u32& paddr);
template <bool tlb> template <bool tlb, bool crashOnUnimplemented>
u8 Mem::Read8(n64::Registers &regs, u64 vaddr, s64 pc) { u8 Mem::Read8(n64::Registers &regs, u64 vaddr, s64 pc) {
u32 paddr = vaddr; u32 paddr = vaddr;
if(!MapVAddr<tlb>(regs, LOAD, vaddr, paddr)) { if(!MapVAddr<tlb>(regs, LOAD, vaddr, paddr)) {
@@ -86,7 +88,7 @@ u8 Mem::Read8(n64::Registers &regs, u64 vaddr, s64 pc) {
return mmio.rsp.dmem[BYTE_ADDRESS(paddr) & DMEM_DSIZE]; return mmio.rsp.dmem[BYTE_ADDRESS(paddr) & DMEM_DSIZE];
case 0x04040000 ... 0x040FFFFF: case 0x04100000 ... 0x041FFFFF: case 0x04040000 ... 0x040FFFFF: case 0x04100000 ... 0x041FFFFF:
case 0x04300000 ... 0x044FFFFF: case 0x04500000 ... 0x048FFFFF: case 0x04300000 ... 0x044FFFFF: case 0x04500000 ... 0x048FFFFF:
return mmio.Read(paddr); return mmio.Read<crashOnUnimplemented>(paddr);
case 0x10000000 ... 0x1FBFFFFF: case 0x10000000 ... 0x1FBFFFFF:
paddr = (paddr + 2) & ~2; paddr = (paddr + 2) & ~2;
return cart[BYTE_ADDRESS(paddr) & romMask]; return cart[BYTE_ADDRESS(paddr) & romMask];
@@ -95,9 +97,12 @@ u8 Mem::Read8(n64::Registers &regs, u64 vaddr, s64 pc) {
case 0x1FC007C0 ... 0x1FC007FF: case 0x1FC007C0 ... 0x1FC007FF:
return pifRam[paddr & PIF_RAM_DSIZE]; return pifRam[paddr & PIF_RAM_DSIZE];
case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x07FFFFFF: case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x0FFFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: return 0;
case 0x80000000 ... 0xFFFFFFFF: case 0x1FC00800 ... 0x7FFFFFFF: return 0; default:
default: util::panic("Unimplemented 8-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc); if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented 8-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc);
}
return 0;
} }
} }
@@ -128,8 +133,7 @@ u16 Mem::Read16(n64::Registers &regs, u64 vaddr, s64 pc) {
case 0x1FC007C0 ... 0x1FC007FF: case 0x1FC007C0 ... 0x1FC007FF:
return be16toh(util::ReadAccess<u16>(pifRam, paddr & PIF_RAM_DSIZE)); return be16toh(util::ReadAccess<u16>(pifRam, paddr & PIF_RAM_DSIZE));
case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x07FFFFFF: case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x0FFFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: return 0;
case 0x80000000 ... 0xFFFFFFFF: case 0x1FC00800 ... 0x7FFFFFFF: return 0;
default: util::panic("Unimplemented 16-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc); default: util::panic("Unimplemented 16-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc);
} }
} }
@@ -160,9 +164,9 @@ u32 Mem::Read32(n64::Registers &regs, u64 vaddr, s64 pc) {
case 0x1FC007C0 ... 0x1FC007FF: case 0x1FC007C0 ... 0x1FC007FF:
return be32toh(util::ReadAccess<u32>(pifRam, paddr & PIF_RAM_DSIZE)); return be32toh(util::ReadAccess<u32>(pifRam, paddr & PIF_RAM_DSIZE));
case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x07FFFFFF: case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x0FFFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: return 0;
case 0x80000000 ... 0xFFFFFFFF: case 0x1FC00800 ... 0x7FFFFFFF: return 0; default:
default: util::panic("Unimplemented 32-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc); util::panic("Unimplemented 32-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64) regs.pc);
} }
} }
@@ -192,14 +196,15 @@ u64 Mem::Read64(n64::Registers &regs, u64 vaddr, s64 pc) {
case 0x1FC007C0 ... 0x1FC007FF: case 0x1FC007C0 ... 0x1FC007FF:
return be64toh(util::ReadAccess<u64>(pifRam, paddr & PIF_RAM_DSIZE)); return be64toh(util::ReadAccess<u64>(pifRam, paddr & PIF_RAM_DSIZE));
case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x07FFFFFF: case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x0FFFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: return 0;
case 0x80000000 ... 0xFFFFFFFF: case 0x1FC00800 ... 0x7FFFFFFF: return 0;
default: util::panic("Unimplemented 32-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc); default: util::panic("Unimplemented 32-bit read at address {:08X} (PC = {:016X})\n", paddr, (u64)regs.pc);
} }
} }
template u8 Mem::Read8<false>(n64::Registers &regs, u64 vaddr, s64 pc); template u8 Mem::Read8<false, false>(n64::Registers &regs, u64 vaddr, s64 pc);
template u8 Mem::Read8<true>(n64::Registers &regs, u64 vaddr, s64 pc); template u8 Mem::Read8<false, true>(n64::Registers &regs, u64 vaddr, s64 pc);
template u8 Mem::Read8<true, false>(n64::Registers &regs, u64 vaddr, s64 pc);
template u8 Mem::Read8<true, true>(n64::Registers &regs, u64 vaddr, s64 pc);
template u16 Mem::Read16<false>(n64::Registers &regs, u64 vaddr, s64 pc); template u16 Mem::Read16<false>(n64::Registers &regs, u64 vaddr, s64 pc);
template u16 Mem::Read16<true>(n64::Registers &regs, u64 vaddr, s64 pc); template u16 Mem::Read16<true>(n64::Registers &regs, u64 vaddr, s64 pc);
template u32 Mem::Read32<false>(n64::Registers &regs, u64 vaddr, s64 pc); template u32 Mem::Read32<false>(n64::Registers &regs, u64 vaddr, s64 pc);
@@ -207,7 +212,7 @@ template u32 Mem::Read32<true>(n64::Registers &regs, u64 vaddr, s64 pc);
template u64 Mem::Read64<false>(n64::Registers &regs, u64 vaddr, s64 pc); template u64 Mem::Read64<false>(n64::Registers &regs, u64 vaddr, s64 pc);
template u64 Mem::Read64<true>(n64::Registers &regs, u64 vaddr, s64 pc); template u64 Mem::Read64<true>(n64::Registers &regs, u64 vaddr, s64 pc);
template <bool tlb> template <bool tlb, bool crashOnUnimplemented>
void Mem::Write8(Registers& regs, u64 vaddr, u32 val, s64 pc) { void Mem::Write8(Registers& regs, u64 vaddr, u32 val, s64 pc) {
u32 paddr = vaddr; u32 paddr = vaddr;
if(!MapVAddr<tlb>(regs, STORE, vaddr, paddr)) { if(!MapVAddr<tlb>(regs, STORE, vaddr, paddr)) {
@@ -228,7 +233,7 @@ void Mem::Write8(Registers& regs, u64 vaddr, u32 val, s64 pc) {
util::WriteAccess<u32>(mmio.rsp.dmem, paddr & DMEM_DSIZE, val); util::WriteAccess<u32>(mmio.rsp.dmem, paddr & DMEM_DSIZE, val);
break; break;
case 0x04040000 ... 0x040FFFFF: case 0x04100000 ... 0x041FFFFF: case 0x04040000 ... 0x040FFFFF: case 0x04100000 ... 0x041FFFFF:
case 0x04300000 ... 0x044FFFFF: case 0x04500000 ... 0x048FFFFF: mmio.Write(*this, regs, paddr, val); break; case 0x04300000 ... 0x044FFFFF: case 0x04500000 ... 0x048FFFFF: mmio.Write<crashOnUnimplemented>(*this, regs, paddr, val); break;
case 0x10000000 ... 0x13FFFFFF: break; case 0x10000000 ... 0x13FFFFFF: break;
case 0x1FC007C0 ... 0x1FC007FF: case 0x1FC007C0 ... 0x1FC007FF:
val = val << (8 * (3 - (paddr & 3))); val = val << (8 * (3 - (paddr & 3)));
@@ -239,7 +244,10 @@ void Mem::Write8(Registers& regs, u64 vaddr, u32 val, s64 pc) {
case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF:
case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x07FFFFFF: case 0x08000000 ... 0x0FFFFFFF: case 0x04900000 ... 0x07FFFFFF:
case 0x1FC00800 ... 0x7FFFFFFF: case 0x80000000 ... 0xFFFFFFFF: break; case 0x1FC00800 ... 0x7FFFFFFF: case 0x80000000 ... 0xFFFFFFFF: break;
default: util::panic("Unimplemented 8-bit write at address {:08X} with value {:0X} (PC = {:016X})\n", paddr, val, (u64)regs.pc); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented 8-bit write at address {:08X} with value {:0X} (PC = {:016X})\n", paddr, val, (u64)regs.pc);
}
} }
} }
@@ -355,8 +363,10 @@ void Mem::Write64(Registers& regs, u64 vaddr, u64 val, s64 pc) {
} }
} }
template void Mem::Write8<false>(Registers& regs, u64 vaddr, u32 val, s64 pc); template void Mem::Write8<false, false>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write8<true>(Registers& regs, u64 vaddr, u32 val, s64 pc); template void Mem::Write8<false, true>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write8<true, false>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write8<true, true>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write16<false>(Registers& regs, u64 vaddr, u32 val, s64 pc); template void Mem::Write16<false>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write16<true>(Registers& regs, u64 vaddr, u32 val, s64 pc); template void Mem::Write16<true>(Registers& regs, u64 vaddr, u32 val, s64 pc);
template void Mem::Write32<false>(Registers& regs, u64 vaddr, u32 val, s64 pc); template void Mem::Write32<false>(Registers& regs, u64 vaddr, u32 val, s64 pc);

View File

@@ -23,7 +23,7 @@ struct Mem {
return mmio.rdp.dram.data(); return mmio.rdp.dram.data();
} }
template <bool tlb = true> template <bool tlb = true, bool crashOnUnimplemented = true>
u8 Read8(Registers&, u64, s64); u8 Read8(Registers&, u64, s64);
template <bool tlb = true> template <bool tlb = true>
u16 Read16(Registers&, u64, s64); u16 Read16(Registers&, u64, s64);
@@ -31,7 +31,7 @@ struct Mem {
u32 Read32(Registers&, u64, s64); u32 Read32(Registers&, u64, s64);
template <bool tlb = true> template <bool tlb = true>
u64 Read64(Registers&, u64, s64); u64 Read64(Registers&, u64, s64);
template <bool tlb = true> template <bool tlb = true, bool crashOnUnimplemented = true>
void Write8(Registers&, u64, u32, s64); void Write8(Registers&, u64, u32, s64);
template <bool tlb = true> template <bool tlb = true>
void Write16(Registers&, u64, u32, s64); void Write16(Registers&, u64, u32, s64);
@@ -42,6 +42,26 @@ struct Mem {
MMIO mmio; MMIO mmio;
u8 pifRam[PIF_RAM_SIZE]{}; u8 pifRam[PIF_RAM_SIZE]{};
inline void DumpRDRAM() const {
FILE *fp = fopen("rdram.dump", "wb");
u8 *temp = (u8*)calloc(RDRAM_SIZE, 1);
memcpy(temp, mmio.rdp.dram.data(), RDRAM_SIZE);
util::SwapBuffer32(RDRAM_SIZE, temp);
fwrite(temp, 1, RDRAM_SIZE, fp);
free(temp);
fclose(fp);
}
inline void DumpIMEM() const {
FILE *fp = fopen("imem.dump", "wb");
u8 *temp = (u8*)calloc(IMEM_SIZE, 1);
memcpy(temp, mmio.rsp.imem, IMEM_SIZE);
util::SwapBuffer32(IMEM_SIZE, temp);
fwrite(temp, 1, IMEM_SIZE, fp);
free(temp);
fclose(fp);
}
private: private:
friend struct SI; friend struct SI;
friend struct PI; friend struct PI;

View File

@@ -23,6 +23,7 @@ static const int cmd_lens[64] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
}; };
template <bool crashOnUnimplemented>
auto RDP::Read(u32 addr) const -> u32{ auto RDP::Read(u32 addr) const -> u32{
switch(addr) { switch(addr) {
case 0x04100000: return dpc.start; case 0x04100000: return dpc.start;
@@ -33,19 +34,32 @@ auto RDP::Read(u32 addr) const -> u32{
case 0x04100014: return dpc.status.cmdBusy; case 0x04100014: return dpc.status.cmdBusy;
case 0x04100018: return dpc.status.pipeBusy; case 0x04100018: return dpc.status.pipeBusy;
case 0x0410001C: return dpc.tmem; case 0x0410001C: return dpc.tmem;
default: util::panic("Unhandled DP Command Registers read (addr: {:08X})\n", addr); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled DP Command Registers read (addr: {:08X})\n", addr);
} }
return 0; return 0;
} }
}
template auto RDP::Read<true>(u32 addr) const -> u32;
template auto RDP::Read<false>(u32 addr) const -> u32;
template <bool crashOnUnimplemented>
void RDP::Write(MI& mi, Registers& regs, RSP& rsp, u32 addr, u32 val) { void RDP::Write(MI& mi, Registers& regs, RSP& rsp, u32 addr, u32 val) {
switch(addr) { switch(addr) {
case 0x04100000: WriteStart(val); break; case 0x04100000: WriteStart(val); break;
case 0x04100004: WriteEnd(mi, regs, rsp, val); break; case 0x04100004: WriteEnd(mi, regs, rsp, val); break;
case 0x0410000C: WriteStatus(mi, regs, rsp, val); break; case 0x0410000C: WriteStatus(mi, regs, rsp, val); break;
default: util::panic("Unhandled DP Command Registers write (addr: {:08X}, val: {:08X})\n", addr, val); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled DP Command Registers write (addr: {:08X}, val: {:08X})\n", addr, val);
} }
} }
}
template void RDP::Write<true>(MI&, Registers&, RSP&, u32, u32);
template void RDP::Write<false>(MI&, Registers&, RSP&, u32, u32);
void RDP::WriteStatus(MI& mi, Registers& regs, RSP& rsp, u32 val) { void RDP::WriteStatus(MI& mi, Registers& regs, RSP& rsp, u32 val) {
bool rdpUnfrozen = false; bool rdpUnfrozen = false;

View File

@@ -58,7 +58,9 @@ struct RDP {
void Reset(); void Reset();
std::vector<u8> dram; std::vector<u8> dram;
template <bool crashOnUnimplemented = true>
[[nodiscard]] auto Read(u32 addr) const -> u32; [[nodiscard]] auto Read(u32 addr) const -> u32;
template <bool crashOnUnimplemented = true>
void Write(MI& mi, Registers& regs, RSP& rsp, u32 addr, u32 val); void Write(MI& mi, Registers& regs, RSP& rsp, u32 addr, u32 val);
void WriteStatus(MI& mi, Registers& regs, RSP& rsp, u32 val); void WriteStatus(MI& mi, Registers& regs, RSP& rsp, u32 val);
void RunCommand(MI& mi, Registers& regs, RSP& rsp); void RunCommand(MI& mi, Registers& regs, RSP& rsp);

View File

@@ -1,7 +1,6 @@
#include <n64/core/RSP.hpp> #include <n64/core/RSP.hpp>
#include <util.hpp> #include <util.hpp>
#include <n64/core/Mem.hpp> #include <n64/core/Mem.hpp>
#include <n64/core/mmio/Interrupt.hpp>
namespace n64 { namespace n64 {
RSP::RSP() { RSP::RSP() {
@@ -31,47 +30,54 @@ void RSP::Reset() {
divInLoaded = false; divInLoaded = false;
} }
inline void logRSP(const RSP& rsp, const u32 instr) {
util::print("{:04X} {:08X} ", rsp.oldPC, instr);
for (int i = 0; i < 32; i++) {
util::print("{:08X} ", (u32)rsp.gpr[i]);
}
for (int i = 0; i < 32; i++) {
for (int e = 0; e < 8; e++) {
util::print("{:04X}", rsp.vpr[i].element[e]);
}
util::print(" ");
}
for (int e = 0; e < 8; e++) {
util::print("{:04X}", rsp.acc.h.element[e]);
}
util::print(" ");
for (int e = 0; e < 8; e++) {
util::print("{:04X}", rsp.acc.m.element[e]);
}
util::print(" ");
for (int e = 0; e < 8; e++) {
util::print("{:04X}", rsp.acc.l.element[e]);
}
util::print(" ");
util::print("{:04X} {:04X} {:02X}", rsp.GetVCC(), rsp.GetVCO(), rsp.GetVCE());
util::print("\n");
}
void RSP::Step(Registers& regs, Mem& mem) { void RSP::Step(Registers& regs, Mem& mem) {
gpr[0] = 0; gpr[0] = 0;
u32 instr = util::ReadAccess<u32>(imem, pc & IMEM_DSIZE); u32 instr = util::ReadAccess<u32>(imem, pc & IMEM_DSIZE);
oldPC = pc & 0xFFC; oldPC = pc & 0xFFC;
pc = nextPC & 0xFFC; pc = nextPC & 0xFFC;
nextPC += 4; nextPC += 4;
if(oldPC == 0x00E4 && gpr[1] == 0x18) {
printf("\n");
}
Exec(regs, mem, instr); Exec(regs, mem, instr);
/*
util::print("{:04X} {:08X} ", oldPC, instr); // logRSP(*this, instr);
for (int i = 0; i < 32; i++) {
util::print("{:08X} ", (u32)gpr[i]);
}
for (int i = 0; i < 32; i++) {
for (int e = 0; e < 8; e++) {
util::print("{:04X}", vpr[i].element[e]);
}
util::print(" ");
}
for (int e = 0; e < 8; e++) {
util::print("{:04X}", acc.h.element[e]);
}
util::print(" ");
for (int e = 0; e < 8; e++) {
util::print("{:04X}", acc.m.element[e]);
}
util::print(" ");
for (int e = 0; e < 8; e++) {
util::print("{:04X}", acc.l.element[e]);
}
util::print(" ");
util::print("{:04X} {:04X} {:02X}", GetVCC(), GetVCO(), GetVCE());
util::print("\n");
*/
} }
template <bool crashOnUnimplemented>
auto RSP::Read(u32 addr) -> u32{ auto RSP::Read(u32 addr) -> u32{
switch (addr) { switch (addr) {
case 0x04040000: return lastSuccessfulSPAddr.raw & 0x1FF8; case 0x04040000: return lastSuccessfulSPAddr.raw & 0x1FF8;
@@ -83,10 +89,18 @@ auto RSP::Read(u32 addr) -> u32{
case 0x04040018: return 0; case 0x04040018: return 0;
case 0x0404001C: return AcquireSemaphore(); case 0x0404001C: return AcquireSemaphore();
case 0x04080000: return pc & 0xFFC; case 0x04080000: return pc & 0xFFC;
default: util::panic("Unimplemented SP register read {:08X}\n", addr); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented SP register read {:08X}\n", addr);
}
return 0;
} }
} }
template auto RSP::Read<true>(u32 addr) -> u32;
template auto RSP::Read<false>(u32 addr) -> u32;
template <bool crashOnUnimplemented>
void RSP::Write(Mem& mem, Registers& regs, u32 addr, u32 value) { void RSP::Write(Mem& mem, Registers& regs, u32 addr, u32 value) {
MI& mi = mem.mmio.mi; MI& mi = mem.mmio.mi;
switch (addr) { switch (addr) {
@@ -107,8 +121,12 @@ void RSP::Write(Mem& mem, Registers& regs, u32 addr, u32 value) {
SetPC(value); SetPC(value);
} break; } break;
default: default:
if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented SP register write {:08X}, val: {:08X}\n", addr, value); util::panic("Unimplemented SP register write {:08X}, val: {:08X}\n", addr, value);
} }
} }
}
template void RSP::Write<true>(n64::Mem &mem, n64::Registers &regs, u32 addr, u32 value);
template void RSP::Write<false>(n64::Mem &mem, n64::Registers &regs, u32 addr, u32 value);
} }

View File

@@ -9,8 +9,6 @@
#define SET_RSP_HALF(addr, buf, value) do { RSP_BYTE(addr, buf) = ((value) >> 8) & 0xFF; RSP_BYTE((addr) + 1, buf) = (value) & 0xFF;} while(0) #define SET_RSP_HALF(addr, buf, value) do { RSP_BYTE(addr, buf) = ((value) >> 8) & 0xFF; RSP_BYTE((addr) + 1, buf) = (value) & 0xFF;} while(0)
#define GET_RSP_WORD(addr, buf) ((GET_RSP_HALF(addr, buf) << 16) | GET_RSP_HALF((addr) + 2, buf)) #define GET_RSP_WORD(addr, buf) ((GET_RSP_HALF(addr, buf) << 16) | GET_RSP_HALF((addr) + 2, buf))
#define SET_RSP_WORD(addr, buf, value) do { SET_RSP_HALF(addr, buf, ((value) >> 16) & 0xFFFF); SET_RSP_HALF((addr) + 2, buf, (value) & 0xFFFF);} while(0) #define SET_RSP_WORD(addr, buf, value) do { SET_RSP_HALF(addr, buf, ((value) >> 16) & 0xFFFF); SET_RSP_HALF((addr) + 2, buf, (value) & 0xFFFF);} while(0)
#define GET_RSP_DWORD(addr, buf) (((u64)GET_RSP_WORD(addr, buf) << 32) | (u64)GET_RSP_WORD((addr) + 4, buf))
#define SET_RSP_DWORD(addr, buf, value) do { SET_RSP_WORD(addr, buf, ((value) >> 32) & 0xFFFFFFFF); SET_RSP_WORD((addr) + 4, buf, (value) & 0xFFFFFFFF);} while(0)
namespace n64 { namespace n64 {
union SPStatus { union SPStatus {
@@ -98,7 +96,9 @@ union VPR {
u16 element[8]; u16 element[8];
u8 byte[16]; u8 byte[16];
u32 word[4]; u32 word[4];
}; } __attribute__((packed));
static_assert(sizeof(VPR) == 16);
struct Mem; struct Mem;
struct Registers; struct Registers;
@@ -113,7 +113,9 @@ struct RSP {
RSP(); RSP();
void Reset(); void Reset();
void Step(Registers& regs, Mem& mem); void Step(Registers& regs, Mem& mem);
template <bool crashOnUnimplemented = true>
auto Read(u32 addr) -> u32; auto Read(u32 addr) -> u32;
template <bool crashOnUnimplemented = true>
void Write(Mem& mem, Registers& regs, u32 addr, u32 value); void Write(Mem& mem, Registers& regs, u32 addr, u32 value);
void Exec(Registers& regs, Mem& mem, u32 instr); void Exec(Registers& regs, Mem& mem, u32 instr);
SPStatus spStatus; SPStatus spStatus;
@@ -147,7 +149,7 @@ struct RSP {
nextPC = pc + 4; nextPC = pc + 4;
} }
inline s64 GetACC(int e) { inline s64 GetACC(int e) const {
s64 val = s64(acc.h.element[e]) << 32; s64 val = s64(acc.h.element[e]) << 32;
val |= s64(acc.m.element[e]) << 16; val |= s64(acc.m.element[e]) << 16;
val |= s64(acc.l.element[e]) << 00; val |= s64(acc.l.element[e]) << 00;
@@ -163,7 +165,7 @@ struct RSP {
acc.l.element[e] = val; acc.l.element[e] = val;
} }
inline u16 GetVCO() { inline u16 GetVCO() const {
u16 value = 0; u16 value = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
bool h = vco.h.element[7 - i] != 0; bool h = vco.h.element[7 - i] != 0;
@@ -174,7 +176,7 @@ struct RSP {
return value; return value;
} }
inline u16 GetVCC() { inline u16 GetVCC() const {
u16 value = 0; u16 value = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
bool h = vcc.h.element[7 - i] != 0; bool h = vcc.h.element[7 - i] != 0;
@@ -185,7 +187,7 @@ struct RSP {
return value; return value;
} }
inline u8 GetVCE() { inline u8 GetVCE() const {
u8 value = 0; u8 value = 0;
for(int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
bool l = vce.element[ELEMENT_INDEX(i)] != 0; bool l = vce.element[ELEMENT_INDEX(i)] != 0;
@@ -194,7 +196,6 @@ struct RSP {
return value; return value;
} }
inline void WriteStatus(MI& mi, Registers& regs, u32 value) { inline void WriteStatus(MI& mi, Registers& regs, u32 value) {
auto write = SPStatusWrite{.raw = value}; auto write = SPStatusWrite{.raw = value};
if(write.clearHalt && !write.setHalt) { if(write.clearHalt && !write.setHalt) {
@@ -220,88 +221,6 @@ struct RSP {
CLEAR_SET(spStatus.signal7, write.clearSignal7, write.setSignal7); CLEAR_SET(spStatus.signal7, write.clearSignal7, write.setSignal7);
} }
inline u64 ReadDword(u32 addr, bool i) {
addr &= 0xfff;
if (i) {
return GET_RSP_DWORD(addr, imem);
} else {
return GET_RSP_DWORD(addr, dmem);
}
}
inline void WriteDword(u32 addr, u64 val, bool i) {
addr &= 0xfff;
if (i) {
SET_RSP_DWORD(addr, imem, val);
} else {
SET_RSP_DWORD(addr, dmem, val);
}
}
inline u32 ReadWord(u32 addr, bool i) {
addr &= 0xfff;
if (i) {
return GET_RSP_WORD(addr, imem);
} else {
return GET_RSP_WORD(addr, dmem);
}
}
inline void WriteWord(u32 addr, u32 val, bool i) {
addr &= 0xfff;
if (i) {
SET_RSP_WORD(addr, imem, val);
} else {
SET_RSP_WORD(addr, dmem, val);
}
}
inline u16 ReadHalf(u32 addr, bool i) {
addr &= 0xfff;
if (i) {
return GET_RSP_HALF(addr, imem);
} else {
return GET_RSP_HALF(addr, dmem);
}
}
inline void WriteHalf(u32 addr, u16 val, bool i) {
addr &= 0xfff;
if (i) {
SET_RSP_HALF(addr, imem, val);
} else {
SET_RSP_HALF(addr, dmem, val);
}
}
inline u8 ReadByte(u32 addr, bool i) {
addr &= 0xfff;
if (i) {
return RSP_BYTE(addr, imem);
} else {
return RSP_BYTE(addr, dmem);
}
}
inline void WriteByte(u32 addr, u8 val, bool i) {
addr &= 0xfff;
if (i) {
RSP_BYTE(addr, imem) = val;
} else {
RSP_BYTE(addr, dmem) = val;
}
}
inline u64 ReadDword(u32 addr) {
addr &= 0xfff;
return GET_RSP_DWORD(addr, dmem);
}
inline void WriteDword(u32 addr, u64 val) {
addr &= 0xfff;
SET_RSP_DWORD(addr, dmem, val);
}
inline u32 ReadWord(u32 addr) { inline u32 ReadWord(u32 addr) {
addr &= 0xfff; addr &= 0xfff;
return GET_RSP_WORD(addr, dmem); return GET_RSP_WORD(addr, dmem);

View File

@@ -34,6 +34,7 @@ auto AI::Read(u32 addr) const -> u32 {
#define max(x, y) ((x) > (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y))
template <bool crashOnUnimplemented>
void AI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) { void AI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
switch(addr) { switch(addr) {
case 0x04500000: case 0x04500000:
@@ -68,9 +69,15 @@ void AI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
bitrate = val & 0xF; bitrate = val & 0xF;
dac.precision = bitrate + 1; dac.precision = bitrate + 1;
break; break;
default: util::panic("Unhandled AI write at addr {:08X} with val {:08X}\n", addr, val); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled AI write at addr {:08X} with val {:08X}\n", addr, val);
} }
} }
}
template void AI::Write<true>(Mem&, Registers&, u32, u32);
template void AI::Write<false>(Mem&, Registers&, u32, u32);
void AI::Step(Mem& mem, Registers& regs, int cpuCycles, float volumeL, float volumeR) { void AI::Step(Mem& mem, Registers& regs, int cpuCycles, float volumeL, float volumeR) {
cycles += cpuCycles; cycles += cpuCycles;

View File

@@ -10,6 +10,7 @@ struct AI {
AI() = default; AI() = default;
void Reset(); void Reset();
auto Read(u32) const -> u32; auto Read(u32) const -> u32;
template <bool crashOnUnimplemented = true>
void Write(Mem&, Registers&, u32, u32); void Write(Mem&, Registers&, u32, u32);
void Step(Mem&, Registers&, int, float, float); void Step(Mem&, Registers&, int, float, float);
bool dmaEnable{}; bool dmaEnable{};

View File

@@ -16,17 +16,25 @@ void MI::Reset() {
miMode = 0; miMode = 0;
} }
template <bool crashOnUnimplemented>
auto MI::Read(u32 paddr) const -> u32 { auto MI::Read(u32 paddr) const -> u32 {
switch(paddr & 0xF) { switch(paddr & 0xF) {
case 0x0: return miMode & 0x3FF; case 0x0: return miMode & 0x3FF;
case 0x4: return MI_VERSION_REG; case 0x4: return MI_VERSION_REG;
case 0x8: return miIntr.raw & 0x3F; case 0x8: return miIntr.raw & 0x3F;
case 0xC: return miIntrMask.raw & 0x3F; case 0xC: return miIntrMask.raw & 0x3F;
default: util::panic("Unhandled MI[{:08X}] read\n", paddr); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled MI[{:08X}] read\n", paddr);
} }
return 0; return 0;
} }
}
template auto MI::Read<true>(u32 paddr) const -> u32;
template auto MI::Read<false>(u32 paddr) const -> u32;
template <bool crashOnUnimplemented>
void MI::Write(Registers& regs, u32 paddr, u32 val) { void MI::Write(Registers& regs, u32 paddr, u32 val) {
switch(paddr & 0xF) { switch(paddr & 0xF) {
case 0x0: case 0x0:
@@ -78,7 +86,12 @@ void MI::Write(Registers& regs, u32 paddr, u32 val) {
UpdateInterrupt(*this, regs); UpdateInterrupt(*this, regs);
break; break;
default: default:
if(crashOnUnimplemented) {
util::panic("Unhandled MI[{:08X}] write ({:08X})\n", val, paddr); util::panic("Unhandled MI[{:08X}] write ({:08X})\n", val, paddr);
} }
} }
} }
template void MI::Write<true>(Registers&, u32 paddr, u32 val);
template void MI::Write<false>(Registers&, u32 paddr, u32 val);
}

View File

@@ -21,7 +21,9 @@ struct Registers;
struct MI { struct MI {
MI(); MI();
void Reset(); void Reset();
template <bool crashOnUnimplemented = true>
[[nodiscard]] auto Read(u32) const -> u32; [[nodiscard]] auto Read(u32) const -> u32;
template <bool crashOnUnimplemented = true>
void Write(Registers& regs, u32, u32); void Write(Registers& regs, u32, u32);
u32 miMode; u32 miMode;

View File

@@ -17,6 +17,7 @@ void PI::Reset() {
memset(stub, 0, 8); memset(stub, 0, 8);
} }
template <bool crashOnUnimplemented>
auto PI::Read(MI& mi, u32 addr) const -> u32 { auto PI::Read(MI& mi, u32 addr) const -> u32 {
switch(addr) { switch(addr) {
case 0x04600000: return dramAddr; case 0x04600000: return dramAddr;
@@ -35,10 +36,17 @@ auto PI::Read(MI& mi, u32 addr) const -> u32 {
case 0x04600024: case 0x04600028: case 0x0460002C: case 0x04600030: case 0x04600024: case 0x04600028: case 0x0460002C: case 0x04600030:
return stub[(addr & 0xff) - 5]; return stub[(addr & 0xff) - 5];
default: default:
util::panic("Unhandled PI[{:08X}] read\n", addr); return 0; if constexpr (crashOnUnimplemented) {
util::panic("Unhandled PI[{:08X}] read\n", addr);
}
return 0;
} }
} }
template auto PI::Read<true>(MI&, u32) const -> u32;
template auto PI::Read<false>(MI&, u32) const -> u32;
template <bool crashOnUnimplemented>
void PI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) { void PI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
MI& mi = mem.mmio.mi; MI& mi = mem.mmio.mi;
switch(addr) { switch(addr) {
@@ -86,8 +94,12 @@ void PI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
stub[(addr & 0xff) - 5] = val & 0xff; stub[(addr & 0xff) - 5] = val & 0xff;
break; break;
default: default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled PI[{:08X}] write ({:08X})\n", val, addr); util::panic("Unhandled PI[{:08X}] write ({:08X})\n", val, addr);
} }
} }
}
template void PI::Write<true>(n64::Mem &mem, n64::Registers &regs, u32 addr, u32 val);
template void PI::Write<false>(n64::Mem &mem, n64::Registers &regs, u32 addr, u32 val);
} }

View File

@@ -10,7 +10,9 @@ struct Registers;
struct PI { struct PI {
PI(); PI();
void Reset(); void Reset();
template <bool crashOnUnimplemented = true>
auto Read(MI&, u32) const -> u32; auto Read(MI&, u32) const -> u32;
template <bool crashOnUnimplemented = true>
void Write(Mem&, Registers&, u32, u32); void Write(Mem&, Registers&, u32, u32);
u32 dramAddr{}, cartAddr{}; u32 dramAddr{}, cartAddr{};
u32 rdLen{}, wrLen{}; u32 rdLen{}, wrLen{};

View File

@@ -13,24 +13,38 @@ void RI::Reset() {
refresh = 0x63634; refresh = 0x63634;
} }
template <bool crashOnUnimplemented>
auto RI::Read(u32 addr) const -> u32 { auto RI::Read(u32 addr) const -> u32 {
switch(addr) { switch(addr) {
case 0x04700000: return mode; case 0x04700000: return mode;
case 0x04700004: return config; case 0x04700004: return config;
case 0x0470000C: return select; case 0x0470000C: return select;
case 0x04700010: return refresh; case 0x04700010: return refresh;
default: util::panic("Unhandled RI[{:08X}] read\n", addr); return 0; default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled RI[{:08X}] read\n", addr);
}
return 0;
} }
} }
template auto RI::Read<true>(u32 addr) const -> u32;
template auto RI::Read<false>(u32 addr) const -> u32;
template <bool crashOnUnimplemented>
void RI::Write(u32 addr, u32 val) { void RI::Write(u32 addr, u32 val) {
switch(addr) { switch(addr) {
case 0x04700000: mode = val; break; case 0x04700000: mode = val; break;
case 0x04700004: config = val; break; case 0x04700004: config = val; break;
case 0x0470000C: select = val; break; case 0x0470000C: select = val; break;
case 0x04700010: refresh = val; break; case 0x04700010: refresh = val; break;
default: util::panic("Unhandled RI[{:08X}] read\n", addr); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled RI[{:08X}] write with val {:08X}\n", addr, val);
}
} }
} }
template void RI::Write<true>(u32 addr, u32 val);
template void RI::Write<false>(u32 addr, u32 val);
} }

View File

@@ -6,9 +6,11 @@ namespace n64 {
struct RI { struct RI {
RI(); RI();
void Reset(); void Reset();
u32 mode{0xE}, config{0x40}, select{0x14}, refresh{0x63634}; template <bool crashOnUnimplemented = true>
auto Read(u32) const -> u32; auto Read(u32) const -> u32;
template <bool crashOnUnimplemented = true>
void Write(u32, u32); void Write(u32, u32);
u32 mode{0xE}, config{0x40}, select{0x14}, refresh{0x63634};
}; };
} }

View File

@@ -12,6 +12,7 @@ void SI::Reset() {
controller.raw = 0; controller.raw = 0;
} }
template<bool crashOnUnimplemented>
auto SI::Read(MI& mi, u32 addr) const -> u32 { auto SI::Read(MI& mi, u32 addr) const -> u32 {
switch(addr) { switch(addr) {
case 0x04800000: return dramAddr; case 0x04800000: return dramAddr;
@@ -24,10 +25,18 @@ auto SI::Read(MI& mi, u32 addr) const -> u32 {
val |= (status.intr << 12); val |= (status.intr << 12);
return val; return val;
} }
default: return 0xFFFFFFFF; default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled SI[{:08X}] read\n", addr);
}
return 0;
} }
} }
template auto SI::Read<true>(MI &mi, u32 addr) const -> u32;
template auto SI::Read<false>(MI &mi, u32 addr) const -> u32;
template<bool crashOnUnimplemented>
void SI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) { void SI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
switch(addr) { switch(addr) {
case 0x04800000: case 0x04800000:
@@ -56,8 +65,13 @@ void SI::Write(Mem& mem, Registers& regs, u32 addr, u32 val) {
InterruptLower(mem.mmio.mi, regs, Interrupt::SI); InterruptLower(mem.mmio.mi, regs, Interrupt::SI);
status.intr = 0; status.intr = 0;
break; break;
default: util::panic("Unhandled SI[%08X] write (%08X)\n", addr, val); default:
if constexpr (crashOnUnimplemented) {
util::panic("Unhandled SI[%08X] write (%08X)\n", addr, val);
}
} }
} }
template void SI::Write<true>(Mem &mem, Registers &regs, u32 addr, u32 val);
template void SI::Write<false>(Mem &mem, Registers &regs, u32 addr, u32 val);
} }

View File

@@ -27,7 +27,9 @@ struct SI {
u32 dramAddr{}; u32 dramAddr{};
Controller controller{}; Controller controller{};
template<bool crashOnUnimplemented = true>
auto Read(MI&, u32) const -> u32; auto Read(MI&, u32) const -> u32;
template<bool crashOnUnimplemented = true>
void Write(Mem&, Registers&, u32, u32); void Write(Mem&, Registers&, u32, u32);
}; };
} }

View File

@@ -22,6 +22,7 @@ void VI::Reset() {
cyclesPerHalfline = 1000; cyclesPerHalfline = 1000;
} }
template <bool crashOnUnimplemented>
u32 VI::Read(u32 paddr) const { u32 VI::Read(u32 paddr) const {
switch(paddr) { switch(paddr) {
case 0x04400000: return status.raw; case 0x04400000: return status.raw;
@@ -39,10 +40,17 @@ u32 VI::Read(u32 paddr) const {
case 0x04400030: return xscale.raw; case 0x04400030: return xscale.raw;
case 0x04400034: return yscale.raw; case 0x04400034: return yscale.raw;
default: default:
if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented VI[%08X] read\n", paddr); util::panic("Unimplemented VI[%08X] read\n", paddr);
} }
return 0;
}
} }
template u32 VI::Read<true>(u32 paddr) const;
template u32 VI::Read<false>(u32 paddr) const;
template <bool crashOnUnimplemented>
void VI::Write(MI& mi, Registers& regs, u32 paddr, u32 val) { void VI::Write(MI& mi, Registers& regs, u32 paddr, u32 val) {
switch(paddr) { switch(paddr) {
case 0x04400000: case 0x04400000:
@@ -81,7 +89,12 @@ void VI::Write(MI& mi, Registers& regs, u32 paddr, u32 val) {
case 0x04400030: xscale.raw = val; break; case 0x04400030: xscale.raw = val; break;
case 0x04400034: yscale.raw = val; break; case 0x04400034: yscale.raw = val; break;
default: default:
if constexpr (crashOnUnimplemented) {
util::panic("Unimplemented VI[%08X] write (%08X)\n", paddr, val); util::panic("Unimplemented VI[%08X] write (%08X)\n", paddr, val);
} }
} }
} }
template void VI::Write<true>(n64::MI &mi, n64::Registers &regs, u32 paddr, u32 val);
template void VI::Write<false>(n64::MI &mi, n64::Registers &regs, u32 paddr, u32 val);
}

View File

@@ -88,7 +88,9 @@ struct Registers;
struct VI { struct VI {
VI(); VI();
void Reset(); void Reset();
template <bool crashOnUnimplemented = true>
[[nodiscard]] u32 Read(u32) const; [[nodiscard]] u32 Read(u32) const;
template <bool crashOnUnimplemented = true>
void Write(MI&, Registers&, u32, u32); void Write(MI&, Registers&, u32, u32);
AxisScale xscale{}, yscale{}; AxisScale xscale{}, yscale{};
VIHsyncLeap hsyncLeap{}; VIHsyncLeap hsyncLeap{};

View File

@@ -148,9 +148,7 @@ inline void cop2(RSP& rsp, u32 instr) {
case 0x30: rsp.vrcp(instr); break; case 0x30: rsp.vrcp(instr); break;
case 0x33: rsp.vmov(instr); break; case 0x33: rsp.vmov(instr); break;
case 0x34: rsp.vrsq(instr); break; case 0x34: rsp.vrsq(instr); break;
case 0x37: case 0x3F: case 0x37: case 0x3F: break;
printf("RSP VNULL or VNOP\n");
break;
default: util::panic("Unhandled RSP COP2 ({:06b})\n", mask); default: util::panic("Unhandled RSP COP2 ({:06b})\n", mask);
} }
} }

View File

@@ -98,8 +98,8 @@ inline VPR GetVTE(const VPR& vt, u8 e) {
break; break;
case 8 ... 15: { case 8 ... 15: {
int index = ELEMENT_INDEX(e - 8); int index = ELEMENT_INDEX(e - 8);
for (u16& vteE : vte.element) { for (int i = 0; i < 8; i++) {
vteE = vt.element[index]; vte.element[i] = vt.element[index];
} }
} break; } break;
} }
@@ -581,7 +581,7 @@ void RSP::vadd(u32 instr) {
for(int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
s32 result = vs.selement[i] + vte.selement[i] + (vco.l.selement[i] != 0); s32 result = vs.selement[i] + vte.selement[i] + (vco.l.selement[i] != 0);
acc.l.element[i] = result; acc.l.element[i] = result;
vd.element[i] = signedClamp(result); vd.element[i] = (u16)signedClamp(result);
vco.l.element[i] = 0; vco.l.element[i] = 0;
vco.h.element[i] = 0; vco.h.element[i] = 0;
} }
@@ -611,22 +611,21 @@ void RSP::vch(u32 instr) {
s16 vsElem = vs.selement[i]; s16 vsElem = vs.selement[i];
s16 vteElem = vte.selement[i]; s16 vteElem = vte.selement[i];
if((vsElem ^ vteElem) < 0) { vco.l.element[i] = ((vsElem ^ vteElem) < 0) ? 0xffff : 0;
if(vco.l.element[i]) {
s16 result = vsElem + vteElem; s16 result = vsElem + vteElem;
acc.l.selement[i] = (result <= 0) ? -vteElem : vsElem; acc.l.selement[i] = (result <= 0) ? -vteElem : vsElem;
vcc.l.element[i] = result <= 0 ? 0xffff : 0; vcc.l.element[i] = result <= 0 ? 0xffff : 0;
vcc.h.element[i] = vteElem < 0 ? 0xffff : 0; vcc.h.element[i] = vteElem < 0 ? 0xffff : 0;
vco.l.element[i] = 0xffff; vco.h.element[i] = (result != 0 && (vteElem != ~vsElem)) ? 0xffff : 0;
vco.h.element[i] = (result != 0 && (u16)vsElem != ((u16)vteElem ^ 0xffff)) ? 0xffff : 0;
vce.element[i] = result == -1 ? 0xffff : 0; vce.element[i] = result == -1 ? 0xffff : 0;
} else { } else {
s16 result = vsElem - vteElem; s16 result = vsElem - vteElem;
acc.l.element[i] = (result >= 0) ? vteElem : vsElem; acc.l.element[i] = (result >= 0) ? vteElem : vsElem;
vcc.l.element[i] = vteElem < 0 ? 0xffff : 0; vcc.l.element[i] = vteElem < 0 ? 0xffff : 0;
vcc.h.element[i] = result >= 0 ? 0xffff : 0; vcc.h.element[i] = result >= 0 ? 0xffff : 0;
vco.l.element[i] = 0; vco.h.element[i] = result != 0 ? 0xffff : 0;
vco.h.element[i] = (result != 0 && (u16)vsElem != ((u16)vteElem ^ 0xffff)) ? 0xffff : 0;
vce.element[i] = 0; vce.element[i] = 0;
} }