foxygit / fake-08-xbox360 Log in
commit 446314a0b365a7192941bfcac464a96017d3d2b9
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Fri Apr 8 08:29:07 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Fri Apr 8 08:29:07 2022 -0600

    make deserializing cart data platform agnostic

    maybe fix 3ds cart data loading
---
 source/vm.cpp    | 25 +++++++++++++++++--------
 test/vmtests.cpp | 25 ++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/source/vm.cpp b/source/vm.cpp
index fb3401f..6851a89 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -395,14 +395,14 @@ void Vm::togglePauseMenu(){


 //https://stackoverflow.com/a/30606613
-std::vector<int32_t> hexToInts(std::string hex) {
-  std::vector<int32_t> bytes;
+std::vector<uint8_t> HexToBytes(std::string hex) {
+  std::vector<uint8_t> bytes;

   hex.erase(std::remove(hex.begin(), hex.end(), '\n'), hex.end());

-  for (unsigned int i = 0; i < hex.length(); i += 8) {
-    std::string intString = hex.substr(i, 8);
-    int32_t byte = (int32_t) strtol(intString.c_str(), NULL, 16);
+  for (unsigned int i = 0; i < hex.length(); i += 2) {
+    std::string byteString = hex.substr(i, 2);
+    uint8_t byte = (uint8_t) strtol(byteString.c_str(), NULL, 16);
     bytes.push_back(byte);
   }

@@ -435,10 +435,19 @@ std::string Vm::getSerializedCartData() {

 void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
     //populate from string (assume correct length? TODO: validation)
-    auto intsVector = hexToInts(cartDataStr);
+    std::vector<uint8_t> bytesVector = HexToBytes(cartDataStr);

-    for(size_t i = 0; i < intsVector.size(); i++) {
-        vm_dset(i, fix32::frombits(intsVector[i]));
+    //ATTN: writing one byte at a time instead of one 32 bit int at a time
+    //to ensure same behavior across platforms and cpu architectures
+    for(int i = 0; i < 64; i++){
+        int idxStart = (i*4);
+        int idxEnd = idxStart + 3;
+        if (idxEnd < bytesVector.size()){
+            _memory->data[0x5e00 + idxStart + 0] = bytesVector[idxEnd];
+            _memory->data[0x5e00 + idxStart + 1] = bytesVector[idxEnd - 1];
+            _memory->data[0x5e00 + idxStart + 2] = bytesVector[idxEnd - 2];
+            _memory->data[0x5e00 + idxStart + 3] = bytesVector[idxEnd - 3];
+        }
     }

 }
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 9b779a8..234c07d 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -586,7 +586,7 @@ TEST_CASE("Vm memory functions") {

         CHECK(matches);
     }
-    SUBCASE("getDeserializedCartData with 0-255"){
+    SUBCASE("deserializeCartDataToMemory with 0-255"){
         vm->vm_cartdata("serializeTest");
         for(int i = 0; i < 256; i++) {
             memory->cartData[i] = 1;
@@ -652,6 +652,29 @@ TEST_CASE("Vm memory functions") {

         CHECK_EQ(expected, actual);
     }
+    SUBCASE("deserializeCartDataToMemory with negative ints"){
+        vm->vm_cartdata("serializeTest");
+        for(int i = 0; i < 256; i++) {
+            memory->cartData[i] = 0;
+        }
+
+        std::string values =
+            "ffff0000000100007fff00008000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        vm->deserializeCartDataToMemory(values);
+
+        CHECK_EQ(vm->vm_dget(0), (fix32)-1);
+        CHECK_EQ(vm->vm_dget(1), (fix32)1);
+        CHECK_EQ(vm->vm_dget(2), (fix32)32767);
+        CHECK_EQ(vm->vm_dget(3), (fix32)-32768);
+    }
     SUBCASE("SFX Note getters match expected values"){
         memory->sfx[0].notes[0].data[0] = 205;
         memory->sfx[0].notes[0].data[1] = 233;