Skip to content

Commit

Permalink
Chaops: persist memory cells in Frolic/Glee/Lark json.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cosinekitty committed Nov 28, 2024
1 parent 38bb324 commit fd9bb1f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/sapphire_chaos_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -140,6 +153,26 @@ namespace Sapphire
static_cast<int>(json_integer_value(mode)) :
circuit.getLegacyMode()
);

json_t* memoryArray = json_object_get(root, "memory");
if (json_is_array(memoryArray))
{
const unsigned n = static_cast<unsigned>(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
Expand Down

0 comments on commit fd9bb1f

Please sign in to comment.