From fd9bb1fab24c709c1318b417a73a90afc0c240c5 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Thu, 28 Nov 2024 16:39:50 -0500 Subject: [PATCH] Chaops: persist memory cells in Frolic/Glee/Lark json. As weird as this sounds, it should be possible to have multiple Frolic/Glee/Lark instances in a patch, and a single Chaops. You should be able to move the Chaops around and attach it to different chaos modules without it bringing memory cells with it. The memory cells must belong to the chaos modules because their regions of stability are not compatible with each other. Therefore, we store the 16 memory cells inside each chaos module. Frolic/Glee/Lark are responsible for serializing the memory cells to JSON and deserializing them back from JSON. --- src/sapphire_chaos_module.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/sapphire_chaos_module.hpp b/src/sapphire_chaos_module.hpp index 3da84e2..54ea38b 100644 --- a/src/sapphire_chaos_module.hpp +++ b/src/sapphire_chaos_module.hpp @@ -124,6 +124,19 @@ namespace Sapphire json_t *root = SapphireModule::dataToJson(); json_object_set_new(root, "turboMode", json_boolean(turboMode)); json_object_set_new(root, "chaosMode", json_integer(circuit.getMode())); + + // Save the memory cells as a JSON array. + json_t* memoryArray = json_array(); + for (unsigned i = 0; i < ChaosOperators::MemoryCount; ++i) + { + json_t* cell = json_object(); + json_object_set_new(cell, "x", json_real(memory[i].x)); + json_object_set_new(cell, "y", json_real(memory[i].y)); + json_object_set_new(cell, "z", json_real(memory[i].z)); + json_array_append_new(memoryArray, cell); + } + json_object_set_new(root, "memory", memoryArray); + return root; } @@ -140,6 +153,26 @@ namespace Sapphire static_cast(json_integer_value(mode)) : circuit.getLegacyMode() ); + + json_t* memoryArray = json_object_get(root, "memory"); + if (json_is_array(memoryArray)) + { + const unsigned n = static_cast(json_array_size(memoryArray)); + const unsigned limit = std::min(n, ChaosOperators::MemoryCount); + for (unsigned i = 0; i < limit; ++i) + { + json_t* cell = json_array_get(memoryArray, i); + json_t* jx = json_object_get(cell, "x"); + json_t* jy = json_object_get(cell, "y"); + json_t* jz = json_object_get(cell, "z"); + if (json_is_real(jx) && json_is_real(jy) && json_is_real(jz)) + { + memory[i].x = json_real_value(jx); + memory[i].y = json_real_value(jy); + memory[i].z = json_real_value(jz); + } + } + } } void process(const ProcessArgs& args) override