Skip to content

Commit

Permalink
wip property rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gulrak committed Nov 10, 2024
1 parent c81aaed commit 2a101a9
Show file tree
Hide file tree
Showing 19 changed files with 1,240 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ add_subdirectory(resources)
add_subdirectory(src)
add_subdirectory(test)

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/experimental")
# add_subdirectory(experimental)
endif()

set(CPACK_PACKAGE_VENDOR "gulrak.net")
set(CPACK_PACKAGE_CONTACT "[email protected]")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/gulrak/cadmium")
Expand Down
1 change: 1 addition & 0 deletions cmake/BuildSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(EMSCRIPTEN)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3 -gsource-map")
#set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -g3 -gsource-map")
add_compile_options(-fexceptions)
add_compile_definitions(WEB_WITH_FETCHING)
add_link_options(-fexceptions)
else()
set(PLATFORM "Desktop")
Expand Down
60 changes: 32 additions & 28 deletions src/cadmium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ void main()

updateResolution();

_librarian.update(_properties); // allows librarian to complete background tasks
_librarian.update(*_properties); // allows librarian to complete background tasks

if (IsFileDropped()) {
auto files = LoadDroppedFiles();
Expand Down Expand Up @@ -1984,8 +1984,8 @@ void main()

void editPropertyCheckBox(std::string_view key, bool forceUpdate)
{
if(_properties.containsFuzzy(key)) {
editProperty(_properties.at(key), forceUpdate);
if(_properties->containsFuzzy(key)) {
editProperty(_properties->at(key), forceUpdate);
}
else {
static bool dummyBool = false;
Expand All @@ -1997,8 +1997,8 @@ void main()

int editPropertySpinner(std::string_view key, bool forceUpdate, int defaultValue = 0)
{
if(_properties.containsFuzzy(key)) {
return editProperty(_properties.at(key), forceUpdate, PA_LEFT);
if(_properties->containsFuzzy(key)) {
return editProperty(_properties->at(key), forceUpdate, PA_LEFT);
}
static int dummyInt = defaultValue;
GuiDisable();
Expand All @@ -2010,12 +2010,9 @@ void main()
void renderEmulationSettings()
{
using namespace gui;
auto oldProps = _properties;
auto oldProps = *_properties;
bool forceUpdate = false;
static emu::Properties propsMemento;
auto& props = _properties;
if(props != propsMemento)
propsMemento = props;
auto& props = *_properties;
BeginColumns();
SetNextWidth(0.42f);
BeginGroupBox("CHIP-8 variant / Core");
Expand All @@ -2026,14 +2023,16 @@ void main()
auto preset = _cores[_behaviorSel].variantProperties(0);
_frameBoost = 1;
updateEmulatorOptions(preset);
props = *_properties;
}
if(DropdownBox(_cores[_behaviorSel].variantsCombo.c_str(), &_subBehaviorSel)) {
auto preset = _cores[_behaviorSel].variantProperties(_subBehaviorSel);
_frameBoost = 1;
updateEmulatorOptions(preset);
props = *_properties;
}
if(_properties.containsFuzzy("Trace-log")) {
editProperty(_properties.at("Trace-log"), forceUpdate, PA_LEFT);
if(_properties->containsFuzzy("Trace-log")) {
editProperty(_properties->at("Trace-log"), forceUpdate, PA_LEFT);
}
else {
static bool dummyTrace = false;
Expand Down Expand Up @@ -2165,7 +2164,7 @@ void main()
++rowCount;
}
}
auto* changedProp = props.changedProperty(propsMemento);
auto* changedProp = props.changedProperty(_propsMemento);
if(changedProp) {
// on change...
if(_chipEmu->updateProperties(props, *changedProp)) {
Expand Down Expand Up @@ -2290,11 +2289,11 @@ void main()
Space(100);
SetNextWidth(0.21f);
bool romRemembered = _cfg.romConfigs.contains(_romSha1);
if((romRemembered && _properties == _cfg.romConfigs[_romSha1]) || (_romIsWellKnown && _properties == _romWellKnownProperties)) {
if((romRemembered && *_properties == _cfg.romConfigs[_romSha1]) || (_romIsWellKnown && *_properties == _romWellKnownProperties)) {
GuiDisable();
}
if (Button(!romRemembered ? "Remember for ROM" : "Update for ROM")) {
_cfg.romConfigs[_romSha1] = _properties;
_cfg.romConfigs[_romSha1] = *_properties;
saveConfig();
}
GuiEnable();
Expand Down Expand Up @@ -2583,7 +2582,7 @@ void main()
pal[i] = fmt::format("#{:06x}", _defaultPalette[i] >> 8);
}
// opt.advanced["palette"] = pal;
_cfg.emuProperties = _properties;
_cfg.emuProperties = *_properties;
_cfg.workingDirectory = _currentDirectory;
_cfg.databaseDirectory = _databaseDirectory;
if(!_cfg.save(_cfgPath)) {
Expand All @@ -2595,15 +2594,16 @@ void main()

void updateBehaviorSelects()
{
if (auto idx = _cores.classIndex(_properties); idx >= 0) {
if (auto idx = _cores.classIndex(*_properties); idx >= 0) {
_behaviorSel = idx;
_subBehaviorSel = static_cast<int>(emu::CoreRegistry::variantIndex(_properties).index);
_subBehaviorSel = static_cast<int>(emu::CoreRegistry::variantIndex(*_properties).index);
}
}

void whenEmuChanged(emu::IEmulationCore& emu) override
{
_debugger.updateCore(&emu);
_propsMemento = *_properties;
// TODO: Fix this
// _editor.updateCompilerOptions(_options.startAddress);
reloadRom();
Expand Down Expand Up @@ -2709,6 +2709,7 @@ void main()
uint32_t _previousColor{};
Rectangle _screenOverlay{};
int _screenScale{1};
emu::Properties _propsMemento{};

//std::string _romName;
//std::vector<uint8_t> _romImage;
Expand Down Expand Up @@ -3189,8 +3190,6 @@ void convertKnownRomList()
int main(int argc, char* argv[])
{
static emu::Properties coreProperties;
auto preset = emu::Chip8EmulatorOptions::eXOCHIP;
#ifndef PLATFORM_WEB
ghc::CLI cli(argc, argv);
int64_t traceLines = -1;
bool compareRun = false;
Expand All @@ -3212,10 +3211,10 @@ int main(int argc, char* argv[])
std::string presetName;
int64_t testSuiteMenuVal = 0;
cli.category("General Options");
#ifndef PLATFORM_WEB
cli.option({"-h", "--help"}, showHelp, "Show this help text");
cli.option({"-t", "--trace"}, traceLines, "Run headless and dump given number of trace lines");
cli.option({"-c", "--compare"}, compareRun, "Run and compare with reference engine, trace until diff");
cli.option({"-r", "--run"}, startRom, "if a ROM is given (positional) start it");
cli.option({"-b", "--benchmark"}, benchmark, "Run given number of cycles as benchmark");
cli.option({"--screen-dump"}, screenDump, "When in trace mode, dump the final screen content to the console");
cli.option({"--draw-dump"}, drawDump, "Dump screen after every draw when in trace mode.");
Expand All @@ -3226,6 +3225,13 @@ int main(int argc, char* argv[])
cli.option({"--dump-library-nickel"}, dumpLibNickel, "Dump library table for Nickel");
cli.option({"--convert-rom-list"}, convertRomList, "Convert list of known roms (just temporary available)");
#endif
#else
#ifdef WEB_WITH_FETCHING
std::string urlLoad;
cli.option({"-u", "--url"}, urlLoad, "An url that will be tried to load a rom or source from");
#endif
#endif
cli.option({"-r", "--run"}, startRom, "if a ROM is given (positional) start it");
emu::CoreRegistry reg{};
std::string coresAvailable;
std::string presetsDescription;
Expand Down Expand Up @@ -3312,11 +3318,12 @@ int main(int argc, char* argv[])
cli.positional(romFile, fmt::format("ROM file or source to load ({})", extensions));

CadmiumConfiguration config;
#ifndef PLATFORM_WEB
auto cfgPath = (fs::path(dataPath())/"config.json").string();
if(config.load(cfgPath)) {
coreProperties = config.emuProperties;
}

#endif
try {
cli.parse();
}
Expand All @@ -3328,6 +3335,7 @@ int main(int argc, char* argv[])
cli.usage();
exit(0);
}
#ifndef PLATFORM_WEB
if(convertRomList) {
convertKnownRomList();
exit(0);
Expand Down Expand Up @@ -3362,6 +3370,7 @@ int main(int argc, char* argv[])
}
*/
}
#endif
if(romFile.size() > 1) {
std::cerr << "ERROR: only one ROM/source file supported" << std::endl;
exit(1);
Expand All @@ -3374,22 +3383,17 @@ int main(int argc, char* argv[])
std::cerr << "ERROR: random generator must be 'rand-lgc' or 'counting' and trace must be used." << std::endl;
exit(1);
}
#ifndef PLATFORM_WEB
//if(execSpeed >= 0) {
// options.instructionsPerFrame = execSpeed;
//}
if(traceLines < 0 && !compareRun && !benchmark) {
#else
ghc::CLI cli(argc, argv);
std::string presetName = "schipc";
#ifdef WEB_WITH_FETCHING
std::string urlLoad;
#endif
int64_t execSpeed = -1;
cli.option({"-p", "--preset"}, presetName, "Select CHIP-8 preset to use: chip-8, chip-10, chip-48, schip1.0, schip1.1, megachip8, xo-chip of vip-chip-8");
cli.option({"-s", "--exec-speed"}, execSpeed, "Set execution speed in instructions per frame (0-500000, 0: unlimited)");
#ifdef WEB_WITH_FETCHING
cli.option({"-u", "--url"}, urlLoad, "An url that will be tried to load a rom or source from");
#endif
try {
cli.parse();
}
Expand Down
17 changes: 12 additions & 5 deletions src/emuhostex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,18 @@ void EmuHostEx::updateEmulatorOptions(const Properties& properties)
// TODO: Fix this
if(_previousProperties != properties || !_chipEmu) {

if(&properties != &_properties) {
_properties = properties;
if(&properties != _properties) {
if(!_properties || _properties->propertyClass().empty() || _properties->propertyClass() != properties.propertyClass()) {
auto pIter = _propertiesByClass.find(properties.propertyClass());
if(pIter == _propertiesByClass.end()) {
pIter = _propertiesByClass.emplace(properties.propertyClass(), properties).first;
}
_properties = &pIter->second;
}
*_properties = properties;
}
_previousProperties = properties;
_chipEmu = create(_properties, _chipEmu.get());
_chipEmu = create(*_properties, _chipEmu.get());
if(_chipEmu->getScreen())
(void)_chipEmu->getScreen();
whenEmuChanged(*_chipEmu);
Expand Down Expand Up @@ -272,7 +279,7 @@ bool EmuHostEx::loadBinary(std::string_view filename, ghc::span<const uint8_t> b
source.assign((const char*)fileData.data(), fileData.size());
if(c8c->compile(filename).resultType == emu::CompileResult::eOK)
{
auto startAddress = _properties.get<Property::Integer>("startAddress");
auto startAddress = _properties->get<Property::Integer>("startAddress");
auto loadAddress = startAddress ? startAddress->intValue : 0;
if(c8c->codeSize() < _chipEmu->memSize() - loadAddress) {
romImage.assign(c8c->code(), c8c->code() + c8c->codeSize());
Expand Down Expand Up @@ -333,7 +340,7 @@ bool EmuHostEx::loadBinary(std::string_view filename, ghc::span<const uint8_t> b
//TraceLog(LOG_INFO, "Setting variant.");
//decomp.setVariant(Chip8Variant::CHIP_8, true);
//TraceLog(LOG_INFO, "About to decompile...");
auto startAddress = _properties.get<Property::Integer>("startAddress");
auto startAddress = _properties->get<Property::Integer>("startAddress");
auto loadAddress = startAddress ? startAddress->intValue : 0;
decomp.decompile(filename, _romImage.data(), loadAddress, _romImage.size(), loadAddress, &os, false, true);
source = os.str();
Expand Down
5 changes: 3 additions & 2 deletions src/emuhostex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class EmuHostEx : public EmulatorHost
bool _customPalette{false};
std::array<uint32_t, 256> _colorPalette{};
std::array<uint32_t, 256> _defaultPalette{};
emu::Properties _properties;
emu::Properties* _properties{nullptr};
std::map<std::string, Properties> _propertiesByClass;
std::string _variantName;
emu::Properties _romWellKnownProperties;
emu::Properties _previousProperties;
Expand All @@ -110,7 +111,7 @@ class HeadlessHost : public EmuHostEx
HeadlessHost() : EmuHostEx(_cfg) {}
explicit HeadlessHost(const Properties& options) : EmuHostEx(_cfg) { updateEmulatorOptions(options); }
~HeadlessHost() override = default;
Properties& getProperties() { return _properties; }
Properties& getProperties() { return *_properties; }
IEmulationCore& emuCore() { return *_chipEmu; }
bool isHeadless() const override { return true; }
int getKeyPressed() override { return 0; }
Expand Down
6 changes: 3 additions & 3 deletions src/emulation/chip8generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void Chip8GenericEmulator::reset()
else if(_options.behaviorBase == Chip8GenericOptions::eMEGACHIP) {
_mcPalette.fill(0x00);
_mcPalette[1] = 0xffffffff;
_mcPalette[254] = 0xffffffff;
_mcPalette[255] = 0xffffffff;
}
}

Expand Down Expand Up @@ -1250,7 +1250,7 @@ void Chip8GenericEmulator::op02nn(uint16_t opcode)
{
auto numCols = opcode & 0xFF;
std::vector<uint32_t> cols;
cols.reserve(255);
cols.reserve(256);
size_t address = _rI;
for(size_t i = 0; i < numCols; ++i) {
auto a = _memory[address++ & ADDRESS_MASK];
Expand Down Expand Up @@ -1758,7 +1758,7 @@ void Chip8GenericEmulator::opDxyn_megaChip(uint16_t opcode)
*pixelBuffer32 = 0;
}
else {
*pixelBuffer = 254;
*pixelBuffer = 255;
*pixelBuffer32 = 0xffffffff;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/emulation/chip8realcorebase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class Chip8RealCoreBase : public IEmulationCore, public IChip8Emulator, public C
IChip8Emulator* chip8Core() override { return this; }

virtual Properties& getProperties() = 0;
virtual void updateProperties(Property& changedProp) = 0;

std::string dumpStateLine() const override {
uint16_t op = (readMemoryByte(getPC())<<8)|readMemoryByte(getPC() + 1);
Expand Down
Loading

0 comments on commit 2a101a9

Please sign in to comment.