Skip to content

Commit

Permalink
wip labels
Browse files Browse the repository at this point in the history
  • Loading branch information
telecomadm1145 committed Aug 18, 2024
1 parent 75bb942 commit d1ce065
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 17 deletions.
1 change: 1 addition & 0 deletions CasioEmuMsvc/CasioEmuMsvc.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
<ClInclude Include="Chipset\MMURegion.hpp" />
<ClInclude Include="Config.hpp" />
<ClInclude Include="Containers\ConcurrentObject.h" />
<ClInclude Include="Ext\LabelFile.h" />
<ClInclude Include="Gui\CallAnalysis.h" />
<ClInclude Include="Gui\CasioData.h" />
<ClInclude Include="Gui\CwiiHelp.h" />
Expand Down
3 changes: 3 additions & 0 deletions CasioEmuMsvc/CasioEmuMsvc.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@
<ClInclude Include="Ext\Memory.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Ext\LabelFile.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
4 changes: 2 additions & 2 deletions CasioEmuMsvc/Chipset/CPU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace casioemu {
bool GetMasterInterruptEnable();
std::string GetBacktrace() const;

private:

#ifdef DBG
struct StackFrame {
bool lr_pushed;
Expand All @@ -141,7 +141,7 @@ namespace casioemu {
};
ConcurrentObject<std::vector<StackFrame>> stack;
#endif

private:
uint16_t Fetch();

enum OpcodeHint {
Expand Down
4 changes: 3 additions & 1 deletion CasioEmuMsvc/Chipset/CPUControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ namespace casioemu {
sf.new_pc = reg_csr << 16 | reg_pc;
if (!stack->empty() && !stack->back().lr_pushed) {
std::cout << "[CPU][Warn] Control flow get override!(likely stack corruption or a bootloader)\n";
stack->clear();
stack->back().lr_push_address = 0x0721;
stack->back().lr_pushed = true;
// stack->clear();
}
stack->push_back(sf);
if (on_call_function)
Expand Down
100 changes: 100 additions & 0 deletions CasioEmuMsvc/Ext/LabelFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#pragma once
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stringhelper.h>
#include <vector>
struct Label {
uint32_t address;
std::string name;
};
// Function to parse the file
inline std::vector<Label> parseFile(const std::string& filename) {
std::vector<Label> labels;
std::ifstream file(filename);
if (!file.is_open()) {
return labels;
}

std::string line;
bool inBlockComment = false;

while (std::getline(file, line)) {
// Trim the line
line = trim(line);

// Skip empty lines
if (line.empty())
continue;

// Handle block comments
if (inBlockComment) {
if (line.find("*/") != std::string::npos) {
inBlockComment = false;
}
continue;
}
else if (line.find("/*") != std::string::npos) {
if (line.find("*/") == std::string::npos) {
inBlockComment = true;
}
continue;
}

// Handle line comments
size_t commentPos = line.find('#');
if (commentPos != std::string::npos) {
line = line.substr(0, commentPos);
line = trim(line);
if (line.empty())
continue;
}

// Parse the line
std::string address;
std::string functionName;

size_t commaPos = line.find(',');
if (commaPos != std::string::npos) {
// Format: 0x01234,function or 0x01234,"function with space"
address = line.substr(0, commaPos);
functionName = line.substr(commaPos + 1);
}
else {
// Format: 01234 function
std::istringstream iss(line);
iss >> address;
std::getline(iss, functionName);
}

// Trim extracted strings
address = trim(address);
functionName = trim(functionName);

// Remove quotes from function name if present
if (!functionName.empty() && functionName.front() == '"') {
functionName = functionName.substr(1, functionName.size() - 2);
}

// Print the result
if (!address.empty() && !functionName.empty()) {
auto addr_p = (uint32_t)0;
if (address.length() > 2) {
if (address.starts_with("0x")) {
addr_p = std::strtoul(address.c_str() + 2, 0, 16);
}
else {
addr_p = std::strtoul(address.c_str(), 0, 16);
}
}
else {
addr_p = std::strtoul(address.c_str(), 0, 16);
}
labels.push_back({addr_p, functionName});
}
}

file.close();
return labels;
}
13 changes: 9 additions & 4 deletions CasioEmuMsvc/Gui/Ui.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "ui.hpp"
#include "Chipset/Chipset.hpp"
#include "Chipset/MMU.hpp"
#include "CallAnalysis.h"
#include "CasioData.h"
#include "Chipset/Chipset.hpp"
#include "Chipset/MMU.hpp"
#include "CodeViewer.hpp"
#include "Editors.h"
#include "HwController.h"
#include "Injector.hpp"
#include "LabelFile.h"
#include "LabelViewer.h"
#include "MemBreakpoint.hpp"
#include "VariableWindow.h"
Expand All @@ -24,6 +25,8 @@ static SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | S
static SDL_Window* window = 0;
SDL_Renderer* renderer = 0;

std::vector<Label> g_labels;

CodeViewer* code_viewer = 0;
Injector* injector = 0;
MemBreakPoint* membp = 0;
Expand Down Expand Up @@ -128,9 +131,11 @@ int test_gui(bool* guiCreated) {
ImGui_ImplSDLRenderer2_Init(renderer);
if (guiCreated)
*guiCreated = true;
while (!m_emu || !me_mmu)
while (!me_mmu)
std::this_thread::sleep_for(std::chrono::microseconds(1));
std::this_thread::sleep_for(std::chrono::microseconds(100));

g_labels = parseFile(m_emu->GetModelFilePath("labels"));

for (auto item : std::initializer_list<UIWindow*>{
new VariableWindow(),
new HwController(),
Expand Down
44 changes: 41 additions & 3 deletions CasioEmuMsvc/Gui/WatchWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "Config.hpp"
#include "Models.h"
#include <iomanip>

void WatchWindow::PrepareRX() {
for (int i = 0; i < 16; i++) {
Expand Down Expand Up @@ -60,7 +61,7 @@ void WatchWindow::ShowRX() {
ImGui::SameLine();
show_sfr(reg_psw, "PSW: ", 5, 2);
ImGui::SameLine();
show_sfr(reg_dsr, "DSR: ", 5, 2);
show_sfr(reg_dsr, "DSR: ", 6, 2);
}
void WatchWindow::ModRX() {
char id[10];
Expand Down Expand Up @@ -99,7 +100,7 @@ void WatchWindow::ModRX() {
ImGui::SameLine();
show_sfr(reg_psw, "PSW: ", 5, 2);
ImGui::SameLine();
show_sfr(reg_dsr, "DSR: ", 5, 2);
show_sfr(reg_dsr, "DSR: ", 6, 2);
}

void WatchWindow::UpdateRX() {
Expand All @@ -112,7 +113,44 @@ void WatchWindow::UpdateRX() {
m_emu->chipset.cpu.reg_sp = (uint16_t)strtol((char*)reg_sp, nullptr, 16);
m_emu->chipset.cpu.reg_psw = (uint16_t)strtol((char*)reg_psw, nullptr, 16);
}
struct SymbolAddr {
public:
uint32_t addr;
};
inline static std::ostream& operator<<(std::ostream& os, SymbolAddr ad) {
auto iter = std::find_if(g_labels.begin(), g_labels.end(), [&](Label& dat) { return dat.address == ad.addr; });
if (iter == g_labels.end())
{
return os << ad.addr;
}
else {
return os << iter->name;
}
}
inline std::string GetBacktrace() {
std::stringstream output;
output << std::hex << std::setfill('0') << std::uppercase;
auto stack = m_emu->chipset.cpu.stack.get_const();
for (casioemu::CPU::StackFrame frame : *stack) {
output << " function "
<< std::setw(6) << SymbolAddr{frame.new_pc}
<< " returns to " << std::setw(6);
if (frame.lr_pushed) {
output << SymbolAddr{frame.lr};

output << " - lr pushed at "
<< std::setw(4) << frame.lr_push_address;
}
else {
output << (((size_t)m_emu->chipset.cpu.reg_lcsr) << 16 | m_emu->chipset.cpu.reg_lr);
}
if (frame.is_jump) {
output << " (called by pop pc)";
}
output << '\n';
}
return output.str();
}
void WatchWindow::RenderCore() {
char_width = ImGui::CalcTextSize("F").x;
casioemu::Chipset& chipset = m_emu->chipset;
Expand All @@ -136,7 +174,7 @@ void WatchWindow::RenderCore() {
ImGui::Separator();
static int range = 64;
ImGui::BeginChild("##stack_trace", ImVec2(0, ImGui::GetWindowHeight() / 2));
std::string s = chipset.cpu.GetBacktrace();
std::string s = GetBacktrace();
ImGui::InputTextMultiline("##as", (char*)s.c_str(), s.size(), ImVec2(ImGui::GetWindowWidth(), -1), ImGuiInputTextFlags_ReadOnly);
ImGui::EndChild();
ImGui::BeginChild("##stack_view");
Expand Down
4 changes: 3 additions & 1 deletion CasioEmuMsvc/Gui/ui.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once
#include "Chipset/MMU.hpp"
#include "Emulator.hpp"
#include "LabelFile.h"
#include "imgui/imgui.h"
int test_gui(bool* guiCreated);
void gui_cleanup();
void gui_loop();
extern char* n_ram_buffer;
extern casioemu::MMU* me_mmu;
extern casioemu::Emulator* m_emu;
extern std::vector<Label> g_labels;
class UIWindow {
public:
UIWindow(const char* name) : name(name) {}
Expand All @@ -26,4 +28,4 @@ class UIWindow {
}
virtual void RenderCore() = 0;
};
constexpr ImGuiTableFlags pretty_table = ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Reorderable ;
constexpr ImGuiTableFlags pretty_table = ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Reorderable;
10 changes: 4 additions & 6 deletions CasioEmuMsvc/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ namespace casioemu {
inline constexpr size_t GetRamBaseAddr(HardwareId hid) {
if (hid == HW_TI)
return 0xB000;
return hid == HW_ES_PLUS ? 0x8000 : hid == HW_CLASSWIZ ? 0xD000
: hid == HW_FX_5800P ? 0x8000
: 0x9000;
return (hid == HW_FX_5800P || hid == HW_ES_PLUS) ? 0x8000 : hid == HW_CLASSWIZ ? 0xD000
: 0x9000;
}
inline constexpr size_t GetRamSize(HardwareId hid) {
if (hid == HW_TI)
return 0xF000 - 0xB000;
return hid == HW_ES_PLUS ? 0x0E00 : hid == HW_CLASSWIZ ? 0x2000
: hid == HW_FX_5800P ? 0xf000 - 0x8000
: 0x6000;
return (hid == HW_FX_5800P || hid == HW_ES_PLUS) ? 0x0E00 : hid == HW_CLASSWIZ ? 0x2000
: 0x6000;
}
inline std::vector<MemoryEditor::MarkedSpan> GetCommonMemLabels(HardwareId hid) {
int i = 0;
Expand Down
6 changes: 6 additions & 0 deletions CasioEmuMsvc/stringhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ inline void rtrim(std::string& s) {
return !std::isspace(ch);
}).base(),
s.end());
}
inline std::string trim(const std::string& str) {
std::string fin = str;
ltrim(fin);
rtrim(fin);
return fin;
}

0 comments on commit d1ce065

Please sign in to comment.