foxygit / fake-08-xbox360 Log in
commit b044bce2ef9c21deaf61685a1e2d10928d61de49
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Apr 7 21:12:37 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Thu Apr 7 21:12:37 2022 -0600

    Refactor cartdata serialization

    hopefully works better across platforms
---
 source/vm.cpp    | 14 +++++++++-----
 test/vmtests.cpp | 21 +++++++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/source/vm.cpp b/source/vm.cpp
index e187f04..687fc4f 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -410,20 +410,24 @@ std::vector<int32_t> hexToInts(std::string hex) {
 }

 std::string Vm::getSerializedCartData() {
-    std::stringstream outputstr;
+    std::string outputstr = "";
+
+    char hex_string[9] = "00000000";

     for(int i = 0; i < 64; i++){
         fix32 val = vm_dget((uint8_t)i);
         int32_t bitsVal = val.bits();
-
-        outputstr << std::setfill('0') << std::setw(8) << std::hex << bitsVal;
+
+        sprintf(hex_string, "%08x", bitsVal);
+
+        outputstr.append(hex_string);

         if ((i + 1) % 8 == 0) {
-            outputstr << "\n";
+            outputstr.append("\n");
         }
     }

-    return outputstr.str();
+    return outputstr;
 }

 void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 576a770..9b779a8 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -631,6 +631,27 @@ TEST_CASE("Vm memory functions") {

         CHECK_EQ(expected, actual);
     }
+    SUBCASE("dset then getSerializedCartData with negatives returns correct values"){
+        vm->vm_cartdata("serializeTest");
+        vm->vm_dset(0, -1);
+	    vm->vm_dset(1, 1);
+	    vm->vm_dset(2, 32767);
+	    vm->vm_dset(3, -32768);
+
+        auto actual = vm->getSerializedCartData();
+
+        std::string expected =
+            "ffff0000000100007fff00008000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        CHECK_EQ(expected, actual);
+    }
     SUBCASE("SFX Note getters match expected values"){
         memory->sfx[0].notes[0].data[0] = 205;
         memory->sfx[0].notes[0].data[1] = 233;