Skip to content

Commit

Permalink
wip property rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gulrak committed Sep 26, 2024
1 parent 52b1716 commit ed59780
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 87 deletions.
102 changes: 44 additions & 58 deletions src/cadmium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,10 @@ class Cadmium : public emu::EmuHostEx
using namespace gui;
auto oldProps = _properties;
bool forceUpdate = false;
static emu::Properties propsMemento;
auto& props = _properties;
if(props != propsMemento)
propsMemento = props;
BeginColumns();
SetNextWidth(0.42f);
BeginGroupBox("CHIP-8 variant / Core");
Expand Down Expand Up @@ -1855,54 +1859,51 @@ class Cadmium : public emu::EmuHostEx
}
//else if(!_chipEmu->isGenericEmulation()) {
#endif
if(true) {
BeginGroupBox("System Configuration");
auto startY = GetCurrentPos().y;
auto colWidth1 = GetContentAvailable().width / 2 - 1;
auto colWidth2 = GetContentAvailable().width - colWidth1 - 1;
auto colHeight = GetContentAvailable().height;
auto rowCount = 0;
Space(5);
BeginColumns();
SetSpacing(2);
SetNextWidth(colWidth1);
Begin();
SetSpacing(2);
static emu::Properties propsMemento;
auto& props = _properties;
if(props != propsMemento)
propsMemento = props;
for(size_t i = 0; i < props.numProperties(); ++i) {
auto& prop = props[i];
if(prop.getName().empty()) {
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
Begin();
SetSpacing(2);
BeginGroupBox("System Configuration");
auto startY = GetCurrentPos().y;
auto colWidth1 = GetContentAvailable().width / 2 - 1;
auto colWidth2 = GetContentAvailable().width - colWidth1 - 1;
auto colHeight = GetContentAvailable().height;
auto rowCount = 0;
Space(5);
BeginColumns();
SetSpacing(2);
SetNextWidth(colWidth1);
Begin();
SetSpacing(2);
for(size_t i = 0; i < props.numProperties(); ++i) {
auto& prop = props[i];
if(prop.getName().empty()) {
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
Begin();
SetSpacing(2);
}
else if(prop.access() != emu::eInvisible && !fuzzyAnyOf(prop.getName(), {"TraceLog", "InstructionsPerFrame", "FrameRate"})) {
if(props.numProperties() > 20 && std::holds_alternative<bool>(prop.getValue())) {
editProperty(prop, forceUpdate, PA_LEFT);
}
else if(prop.access() != emu::eInvisible && !fuzzyAnyOf(prop.getName(), {"TraceLog", "InstructionsPerFrame", "FrameRate"})) {
if(props.numProperties() > 20 && std::holds_alternative<bool>(prop.getValue())) {
editProperty(prop, forceUpdate, PA_LEFT);
}
else {
editProperty(prop, forceUpdate);
}
++rowCount;
else {
editProperty(prop, forceUpdate);
}
++rowCount;
}
auto* changedProp = props.changedProperty(propsMemento);
if(changedProp) {
// on change...
}
auto* changedProp = props.changedProperty(propsMemento);
if(changedProp) {
// on change...
if(_chipEmu->updateProperties(props, *changedProp)) {
// core asks for a cold start...
updateEmulatorOptions(props);
//rcb->updateProperties(*changedProp);
}
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
EndColumns();
EndGroupBox();
}
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
EndColumns();
EndGroupBox();

Space(14);
{
StyleManager::Scope guard;
Expand Down Expand Up @@ -1930,7 +1931,6 @@ class Cadmium : public emu::EmuHostEx
prevPalette.assign(_colorPalette.begin(), _colorPalette.end());
}
static int sel = 5;

if(DropdownBox("Cadmium;Silicon-8;Pico-8;Octo Classic;LCD;Custom", &sel, true)) {
switch(sel) {
case 0:
Expand Down Expand Up @@ -2011,11 +2011,6 @@ class Cadmium : public emu::EmuHostEx
EndColumns();
}
Space(8);
// TODO: Fix this
/*if(forceUpdate || oldOptions != _options) {
updateEmulatorOptions(_options);
saveConfig();
}*/
BeginColumns();
Space(100);
SetNextWidth(0.21f);
Expand Down Expand Up @@ -2170,24 +2165,15 @@ class Cadmium : public emu::EmuHostEx
SetNextWidth(80);
if(!selectedInfo.analyzed) GuiDisable();
if(Button("Load") && selectedInfo.analyzed) {
//if(selectedInfo.variant != _options.behaviorBase && selectedInfo.variant != emu::Chip8EmulatorOptions::eCHIP8) {
// updateEmulatorOptions(emu::Chip8EmulatorOptions::optionsOfPreset(selectedInfo.variant));
//}
auto mainView = _mainView;
loadRom(_librarian.fullPath(selectedInfo.filePath).c_str(), LoadOptions::None);
if(_mainView == mainView)
_mainView = _lastView;
}
SetNextWidth(110);
if(Button("Load w/o Config") && selectedInfo.analyzed) {
//if(selectedInfo.variant != _options.behaviorBase && selectedInfo.variant != emu::Chip8EmulatorOptions::eCHIP8) {
// updateEmulatorOptions(emu::Chip8EmulatorOptions::optionsOfPreset(selectedInfo.variant));
//}
// TODO: Fix this
// auto options = _options;
_chipEmu->reset();
loadRom(_librarian.fullPath(selectedInfo.filePath).c_str(), LoadOptions::DontChangeOptions);
// TODO: Fix this
// updateEmulatorOptions(options);
_mainView = _lastView;
}
GuiEnable();
Expand Down
77 changes: 64 additions & 13 deletions src/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "icons.h"
#include <rlguipp/rlguipp4.hpp>
#include <ghc/bit.hpp>
#include <stylemanager.hpp>
#include "debugger.hpp"

Expand Down Expand Up @@ -56,14 +57,12 @@ void Debugger::captureStates()
auto unit = _core->executionUnit(0);
_memBackup.resize(_core->memSize());
std::memcpy(_memBackup.data(), _core->memory(), _core->memSize());
if(chip8Core()) {
_chip8StackBackup.resize(chip8Core()->stackSize());
// TODO: Fix this
//std::memcpy(_chip8StackBackup.data(), chip8Core()->stackElements(), sizeof(uint16_t) * chip8Core()->stackSize());
}
_cpuStatesBackup.resize(_core->numberOfExecutionUnits());
_stackBackup.resize(_core->numberOfExecutionUnits());
for(size_t i = 0; i < _core->numberOfExecutionUnits(); ++i) {
_core->executionUnit(i)->fetchAllRegisters(_cpuStatesBackup[i]);
auto stack = _core->executionUnit(i)->stack();
_stackBackup[i].assign(stack.content.begin(), stack.content.end());
}
}

Expand All @@ -72,6 +71,62 @@ emu::IChip8Emulator* Debugger::chip8Core()
return dynamic_cast<emu::IChip8Emulator*>(_core->executionUnit(0));
}

template<typename T>
T readStackEntry(const uint8_t* address, emu::GenericCpu::Endianness endianness)
{
switch(endianness) {
case emu::GenericCpu::eNATIVE:
return *reinterpret_cast<const T*>(address);
case emu::GenericCpu::eBIG: {
T result = 0;
for (int i = sizeof(T) - 1; i >= 0; --i) {
result |= static_cast<T>(address[i]) << (8 * i);
}
return result;
}
case emu::GenericCpu::eLITTLE: {
T result = 0;
for (int i = 0; i < sizeof(T); ++i) {
result |= static_cast<T>(address[i]) << (8 * i);
}
return result;
}
}
return 0;
}

static std::pair<const char*, bool> formatStackElement(const emu::GenericCpu::StackContent& stack, size_t index, const uint8_t* backup)
{
static char buffer[64];
if(stack.content.empty() || !stack.entrySize || index * stack.entrySize > stack.content.size()) {
buffer[0] = 0;
return {buffer, false};
}
auto offset = stack.stackDirection == emu::GenericCpu::eUPWARDS ? index * stack.entrySize : stack.content.size() - (index + 1) * stack.entrySize;
const uint8_t* entry = &stack.content[offset];
backup += offset;
bool changed = false;
switch(stack.entrySize) {
case 1: {
auto val = readStackEntry<uint8_t>(entry, stack.endianness);
changed = val != readStackEntry<uint8_t>(backup, stack.endianness);
fmt::format_to_n(buffer, 64, "{:02X}\0", val); break;
}
case 2: {
auto val = readStackEntry<uint16_t>(entry, stack.endianness);
changed = val != readStackEntry<uint16_t>(backup, stack.endianness);
fmt::format_to_n(buffer, 64, "{:04X}\0", val); break;
}
case 4: {
auto val = readStackEntry<uint32_t>(entry, stack.endianness);
changed = val != readStackEntry<uint32_t>(backup, stack.endianness);
fmt::format_to_n(buffer, 64, "{:06X}\0", val); break;
}
default: buffer[0] = 0;
}
return {buffer, changed};
}

void Debugger::render(Font& font, std::function<void(Rectangle,int)> drawScreen)
{
using namespace gui;
Expand Down Expand Up @@ -152,15 +207,11 @@ void Debugger::render(Font& font, std::function<void(Rectangle,int)> drawScreen)
auto area = GetContentAvailable();
pos.x += 0;
Space(area.height);
// TODO: Fix this
/*
auto stackSize = _core->focussedExecutionUnit()->stackSize();
const auto* stack = _core->focussedExecutionUnit()->stackElements();
bool fourDigitStack = !_core->isGenericEmulation() || _core->memSize() > 4096;
for (int i = 0; i < stackSize; ++i) {
DrawTextEx(font, fourDigitStack ? TextFormat("%X:%04X", i, stack[i]) : TextFormat("%X: %03X", i, stack[i]), {pos.x, pos.y + i * lineSpacing}, 8, 0, stack[i] == _chip8StackBackup[i] ? lightgrayCol : yellowCol);
auto stack = _core->focussedExecutionUnit()->stack();
for (int i = 0; i < _core->focussedExecutionUnit()->stackSize(); ++i) {
auto element = formatStackElement(stack, i, _stackBackup[0].data());
DrawTextEx(font, TextFormat("%X:%s", i & 0xF, element.first), {pos.x, pos.y + i * lineSpacing}, 8, 0, element.second ? yellowCol : lightgrayCol);
}
*/
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Debugger
bool _memViewFollow{true};
std::vector<RegPack> _cpuStates;
std::vector<RegPack> _cpuStatesBackup;
std::vector<uint16_t> _chip8StackBackup;
std::vector<std::vector<uint8_t>> _stackBackup;
std::vector<uint8_t> _memBackup;
};

4 changes: 3 additions & 1 deletion src/emuhostex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ void EmuHostEx::updateEmulatorOptions(const Properties& properties)
{
// TODO: Fix this
if(_previousProperties != properties || !_chipEmu) {

if(&properties != &_properties) {
_previousProperties = _properties = properties;
_properties = properties;
}
_previousProperties = properties;
_chipEmu = create(_properties, _chipEmu.get());
if(_chipEmu->getScreen())
(void)_chipEmu->getScreen();
Expand Down
Loading

0 comments on commit ed59780

Please sign in to comment.