foxygit / fake-08-xbox360 Log in
commit 595c8f74d7d8fb7183356e33778815ad3dc1d59b
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Apr 2 16:57:07 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Fri Apr 2 16:57:07 2021 -0600

    Add cardata (de)serialization functions
---
 source/vm.cpp    | 53 ++++++++++++++++++++++++++++++++++
 source/vm.h      |  3 ++
 test/vmtests.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)

diff --git a/source/vm.cpp b/source/vm.cpp
index a6ae665..e26487a 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -3,6 +3,7 @@
 #include <chrono>
 #include <math.h>
 #include <setjmp.h>
+#include <algorithm>

 #include <string.h>

@@ -374,6 +375,58 @@ void Vm::togglePauseMenu(){

 }

+//https://codereview.stackexchange.com/a/78539
+constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
+                           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+std::string hexStr(unsigned char *data, int len)
+{
+  std::string s((len * 2), '0');
+  for (int i = 0; i < len; ++i) {
+    s[2 * i]     = hexmap[(data[i] & 0xF0) >> 4];
+    s[2 * i + 1] = hexmap[data[i] & 0x0F];
+  }
+  return s;
+}
+
+//https://stackoverflow.com/a/30606613
+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 += 2) {
+    std::string byteString = hex.substr(i, 2);
+    uint8_t byte = (uint8_t) strtol(byteString.c_str(), NULL, 16);
+    bytes.push_back(byte);
+  }
+
+  return bytes;
+}
+
+std::string Vm::getSerializedCartData() {
+    auto str =
+        hexStr(_memory->cartData +   0, 32) + "\n" +
+        hexStr(_memory->cartData +  32, 32) + "\n" +
+        hexStr(_memory->cartData +  64, 32) + "\n" +
+        hexStr(_memory->cartData +  96, 32) + "\n" +
+        hexStr(_memory->cartData + 128, 32) + "\n" +
+        hexStr(_memory->cartData + 160, 32) + "\n" +
+        hexStr(_memory->cartData + 192, 32) + "\n" +
+        hexStr(_memory->cartData + 224, 32) + "\n";
+
+    return str;
+}
+
+void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
+    //populate from string (assume correct length? TODO: validation)
+    auto byteVector = hexToBytes(cartDataStr);
+
+    for(size_t i = 0; i < byteVector.size(); i++) {
+        _memory->cartData[i] = byteVector[i];
+    }
+
+}

 void Vm::UpdateAndDraw() {
     update_buttons();
diff --git a/source/vm.h b/source/vm.h
index afbcde6..1a37908 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -95,6 +95,9 @@ class Vm {

     void togglePauseMenu();

+    std::string getSerializedCartData();
+    void deserializeCartDataToMemory(std::string cartDataStr);
+
     uint8_t vm_peek(int addr);
     int16_t vm_peek2(int addr);
     fix32 vm_peek4(int addr);
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 8efd738..7f156c2 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -482,6 +482,93 @@ TEST_CASE("Vm memory functions") {
         }
     }

+    SUBCASE("getSerializedCartData for all zeroes"){
+        for(int i = 0; i < 256; i++) {
+            memory->cartData[i] = 0;
+        }
+
+        auto result = vm->getSerializedCartData();
+
+        std::string expected =
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        CHECK_EQ(expected, result);
+    }
+    SUBCASE("getSerializedCartData with 0-255"){
+        for(int i = 0; i < 256; i++) {
+            memory->cartData[i] = (uint8_t)i;
+        }
+
+        auto result = vm->getSerializedCartData();
+
+        std::string expected =
+            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\n"
+            "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f\n"
+            "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f\n"
+            "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f\n"
+            "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f\n"
+            "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf\n"
+            "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf\n"
+            "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff\n";
+
+        CHECK_EQ(expected, result);
+    }
+    SUBCASE("deserializeCartDataToMemory for all zeroes"){
+        for(int i = 0; i < 256; i++) {
+            memory->cartData[i] = 1;
+        }
+
+        std::string allZeroes =
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        vm->deserializeCartDataToMemory(allZeroes);
+
+        bool matches = true;
+        for(int i = 0; i < 256; i++) {
+            matches &= memory->cartData[i] == 0;
+        }
+
+        CHECK(matches);
+    }
+    SUBCASE("getDeserializedCartData with 0-255"){
+        for(int i = 0; i < 256; i++) {
+            memory->cartData[i] = 1;
+        }
+
+        std::string values =
+            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\n"
+            "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f\n"
+            "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f\n"
+            "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f\n"
+            "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f\n"
+            "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf\n"
+            "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf\n"
+            "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff\n";
+
+        vm->deserializeCartDataToMemory(values);
+
+        bool matches = true;
+        for(int i = 0; i < 256; i++) {
+            matches &= memory->cartData[i] == i;
+        }
+
+        CHECK(matches);
+    }
+
     delete stubHost;
     delete graphics;
     delete input;