foxygit / fake-08-xbox360 Log in
commit f06141e6edd5ed2296f6170f9e5934e21a04bf8d
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Sep 27 19:04:48 2025 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Sep 27 19:04:48 2025 -0600

    WIP - add support for multiple cartdatas (up to 4)

    I think implementation is working, but need updates to stub host to get tests to pass
---
 source/vm.cpp    |  52 +++++++++++++++++++++------
 source/vm.h      |   4 ++-
 test/vmtests.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 150 insertions(+), 14 deletions(-)

diff --git a/source/vm.cpp b/source/vm.cpp
index 90867c4..cac3c29 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -51,7 +51,8 @@ Vm::Vm(
         _cartChangeQueued(false),
         _nextCartKey(""),
         _cartLoadError(""),
-        _cartdataKey("")
+        _cartdataKeyCount(0),
+        _currentCartdataKey("")
 {
     _host = host;

@@ -118,7 +119,8 @@ bool abortLua;
 bool Vm::loadCart(Cart* cart) {
     _picoFrameCount = 0;

-    _cartdataKey = "";
+    _cartdataKeyCount = 0;
+    _currentCartdataKey = "";

     //reset memory (may have to be more selective about zeroing out to be accurate?)
     _memory->Reset();
@@ -636,8 +638,8 @@ void Vm::CloseCart() {
     }

     Logger_Write("writing cart data\n");
-    if (_cartdataKey.length() > 0) {
-        _host->saveCartData(_cartdataKey, getSerializedCartData());
+    if (_currentCartdataKey.length() > 0) {
+        _host->saveCartData(_currentCartdataKey, getSerializedCartData());
     }

     Logger_Write("resetting state\n");
@@ -820,11 +822,6 @@ void Vm::vm_poke4(int addr, fix32 value){

 bool Vm::vm_cartdata(string key) {
     //match pico 8 errors
-    if (_cartdataKey != "") {
-        QueueCartChange(BiosCartName);
-        _cartLoadError = "cartdata() can only be called once";
-        return false;
-    }
     if (key.length() == 0 || key.length() > 64) {
         QueueCartChange(BiosCartName);
         _cartLoadError = "cart data id too long";
@@ -832,9 +829,42 @@ bool Vm::vm_cartdata(string key) {
     }
     //todo: validate chars

-    _cartdataKey = key;
+    // If we have a current key, save its data before switching
+    if (_currentCartdataKey.length() > 0) {
+        _host->saveCartData(_currentCartdataKey, getSerializedCartData());
+    }
+
+    // Check if this key is already in our list
+    bool keyExists = false;
+    for (int i = 0; i < _cartdataKeyCount; i++) {
+        if (_cartdataKeys[i] == key) {
+            keyExists = true;
+            break;
+        }
+    }
+
+    // If it's a new key, add it to our list (up to 4 keys max)
+    if (!keyExists) {
+        if (_cartdataKeyCount >= 4) {
+            QueueCartChange(BiosCartName);
+            _cartLoadError = "too many cart data keys (max 4)";
+            return false;
+        }
+        _cartdataKeys[_cartdataKeyCount] = key;
+        _cartdataKeyCount++;
+    }
+
+    // Only clear memory if we're switching to a different key
+    if (_currentCartdataKey != key) {
+        // Clear the cart data memory (0x5e00-0x5eff)
+        memset(&_memory->data[0x5e00], 0, 0x100);
+    }
+
+    // Set this as the current active key
+    _currentCartdataKey = key;

-    auto cartDataStr = _host->getCartDataFileContents(_cartdataKey);
+    // Try to load data from file for this key
+    auto cartDataStr = _host->getCartDataFileContents(_currentCartdataKey);

     //todo: validate hex format
     if (cartDataStr.length() > 0) {
diff --git a/source/vm.h b/source/vm.h
index e4c3031..b3a5252 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -47,7 +47,9 @@ class Vm {

     string _cartLoadError;

-    string _cartdataKey;
+    string _cartdataKeys[4];
+    int _cartdataKeyCount;
+    string _currentCartdataKey;

     string _cartBreadcrumb;
     string _cartParam;
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 0c3230e..4220ac1 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -125,15 +125,16 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(vm->vm_dget(13), (fix32)56);
     }
     SUBCASE("poking cart data"){
-        vm->vm_poke(0x5e02, 56);
+        //need to poke after cartdata is called to set the current key, otherwise it will get wiped
         vm->vm_cartdata("dummy");
+        vm->vm_poke(0x5e02, 56);

         CHECK_EQ(memory->cartData[2], 56);
         CHECK_EQ(vm->vm_dget(0), (fix32)56);
     }
     SUBCASE("poking and peeking cart data"){
-        vm->vm_poke4(0x5e80, (fix32)133);
         vm->vm_cartdata("dummy");
+        vm->vm_poke4(0x5e80, (fix32)133);

         CHECK_EQ(vm->vm_peek4(0x5e80), (fix32)133);
         CHECK_EQ(vm->vm_dget(32), (fix32)133);
@@ -648,6 +649,109 @@ TEST_CASE("Vm memory functions") {

         CHECK_EQ(expected, actual);
     }
+    SUBCASE("multiple cartdata keys can be used (up to 4)"){
+        vm->vm_cartdata("key1");
+        vm->vm_dset(0, 100);
+        vm->vm_cartdata("key2");
+        vm->vm_cartdata("key3");
+        vm->vm_cartdata("key4");
+        vm->vm_dset(0, 400);
+
+        CHECK_EQ(vm->vm_dget(0), (fix32)400);
+
+        vm->vm_cartdata("key2");
+        CHECK_EQ(vm->vm_dget(0), (fix32)0);
+
+        vm->vm_dset(0, 200);
+        CHECK_EQ(vm->vm_dget(0), (fix32)200);
+
+        vm->vm_cartdata("key1");
+        CHECK_EQ(vm->vm_dget(0), (fix32)100);
+    }
+    SUBCASE("error when trying to use more than 4 cartdata keys"){
+        vm->vm_cartdata("key1");
+        vm->vm_cartdata("key2");
+        vm->vm_cartdata("key3");
+        vm->vm_cartdata("key4");
+
+        // 5th key should fail
+        CHECK_FALSE(vm->vm_cartdata("key5"));
+        CHECK_EQ(vm->GetBiosError(), "too many cart data keys (max 4)");
+    }
+    SUBCASE("switching between different cartdata keys works correctly"){
+        vm->vm_cartdata("save1");
+        vm->vm_dset(0, 111);
+        vm->vm_dset(1, 222);
+
+        vm->vm_cartdata("save2");
+        vm->vm_dset(0, 333);
+        vm->vm_dset(1, 444);
+
+        vm->vm_cartdata("save1");
+        CHECK_EQ(vm->vm_dget(0), (fix32)111);
+        CHECK_EQ(vm->vm_dget(1), (fix32)222);
+
+        vm->vm_cartdata("save2");
+        CHECK_EQ(vm->vm_dget(0), (fix32)333);
+        CHECK_EQ(vm->vm_dget(1), (fix32)444);
+    }
+    SUBCASE("reusing same cartdata key multiple times works"){
+        // Use the same key multiple times - should work fine
+        vm->vm_cartdata("sharedkey");
+        vm->vm_dset(0, 555);
+
+        vm->vm_cartdata("sharedkey");
+        CHECK_EQ(vm->vm_dget(0), (fix32)555);
+
+        vm->vm_dset(1, 666);
+
+        vm->vm_cartdata("sharedkey");
+        CHECK_EQ(vm->vm_dget(0), (fix32)555);
+        CHECK_EQ(vm->vm_dget(1), (fix32)666);
+    }
+    SUBCASE("backward compatibility - single key usage still works"){
+        vm->vm_cartdata("singlekey");
+        vm->vm_dset(0, 777);
+        vm->vm_dset(1, 888);
+
+        CHECK_EQ(vm->vm_dget(0), (fix32)777);
+        CHECK_EQ(vm->vm_dget(1), (fix32)888);
+
+        vm->vm_cartdata("singlekey");
+        CHECK_EQ(vm->vm_dget(0), (fix32)777);
+        CHECK_EQ(vm->vm_dget(1), (fix32)888);
+    }
+    SUBCASE("current active key is tracked correctly for serialization"){
+        vm->vm_cartdata("active1");
+        vm->vm_dset(0, 999);
+        vm->vm_dset(1, 1111);
+
+        vm->vm_cartdata("active2");
+        vm->vm_dset(0, 2222);
+        vm->vm_dset(1, 3333);
+
+        vm->vm_cartdata("active1");
+        CHECK_EQ(vm->vm_dget(0), (fix32)999);
+        CHECK_EQ(vm->vm_dget(1), (fix32)1111);
+
+        vm->vm_dset(0, 4444);
+        vm->vm_dset(1, 5555);
+
+        auto serialized = vm->getSerializedCartData();
+
+        // 4444 = 0x115C, 5555 = 0x15B3 (in little-endian format)
+        std::string expected =
+            "115c000015b30000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        CHECK_EQ(expected, serialized);
+    }
     SUBCASE("deserializeCartDataToMemory with negative ints"){
         vm->vm_cartdata("serializeTest");
         for(int i = 0; i < 256; i++) {