foxygit / fake-08-xbox360 Log in
commit c5245c104b7cfdda91bbd4efd3d732b24b8ae461
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Apr 5 21:53:44 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Mon Apr 5 21:53:44 2021 -0600

    cartdata working on desktop sdl
---
 platform/SDL2Desktop/source/SDL2Host.cpp |  5 ++
 source/host.h                            |  5 ++
 source/hostCommonFunctions.cpp           | 20 +++++++
 source/picoluaapi.cpp                    |  2 +-
 source/vm.cpp                            | 89 +++++++++++++++++++-------------
 source/vm.h                              |  2 +-
 test/stubhost.cpp                        | 11 ++++
 test/vmtests.cpp                         | 63 ++++++++++++++++------
 8 files changed, 144 insertions(+), 53 deletions(-)

diff --git a/platform/SDL2Desktop/source/SDL2Host.cpp b/platform/SDL2Desktop/source/SDL2Host.cpp
index 728f0c1..abd7364 100644
--- a/platform/SDL2Desktop/source/SDL2Host.cpp
+++ b/platform/SDL2Desktop/source/SDL2Host.cpp
@@ -46,6 +46,11 @@ Host::Host()
     if (res == 0 && stat(_desktopSdl2SettingsDir.c_str(), &st) == -1) {
         res = mkdir(_desktopSdl2SettingsDir.c_str(), 0777);
     }
+
+    string cartdatadir = _desktopSdl2SettingsPrefix + "cdata";
+    if (res == 0 && stat(cartdatadir.c_str(), &st) == -1) {
+        res = mkdir(cartdatadir.c_str(), 0777);
+    }

     setPlatformParams(
         WINDOW_SIZE_X,
diff --git a/source/host.h b/source/host.h
index 6a0bc1a..83c58f6 100644
--- a/source/host.h
+++ b/source/host.h
@@ -30,6 +30,7 @@ class Host {
     void loadSettingsIni();
     void saveSettingsIni();

+    std::string getCartDataFile(std::string cartDataKey);

     public:
     Host();
@@ -64,6 +65,10 @@ class Host {

     std::string customBiosLua();

+    std::string getCartDataFileContents(std::string cartDataKey);
+
+    void saveCartData(std::string cartDataKey, std::string contents);
+
     void setPlatformParams(
         int windowWidth,
         int windowHeight,
diff --git a/source/hostCommonFunctions.cpp b/source/hostCommonFunctions.cpp
index 64686fc..3f02139 100644
--- a/source/hostCommonFunctions.cpp
+++ b/source/hostCommonFunctions.cpp
@@ -40,6 +40,26 @@ void Host::saveSettingsIni(){

         fflush(file);

+        fclose(file);
+	}
+}
+
+std::string Host::getCartDataFile(std::string cartDataKey) {
+    return _logFilePrefix + "cdata/" + cartDataKey + ".p8d.txt";
+}
+
+std::string Host::getCartDataFileContents(std::string cartDataKey) {
+    return get_file_contents(getCartDataFile(cartDataKey));
+}
+
+void Host::saveCartData(std::string cartDataKey, std::string contents) {
+    FILE * file = freopen(getCartDataFile(cartDataKey).c_str(), "w", stderr);
+    if( file != NULL ) {
+		//Initialize data
+        fprintf(file, "%s", contents.c_str());
+
+        fflush(file);
+
         fclose(file);
 	}
 }
\ No newline at end of file
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index acebceb..407fc10 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -882,7 +882,7 @@ int reload(lua_State *L) {
 int cartdata(lua_State *L) {
     const char * str = lua_tolstring(L, 1, nullptr);

-    _vmForLuaApi->vm_cartdata(str);
+    std::string result = _vmForLuaApi->vm_cartdata(str);

     return 0;
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index e26487a..cb636c3 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -6,6 +6,9 @@
 #include <algorithm>

 #include <string.h>
+#include <sstream>
+#include <iostream>
+#include <iomanip>

 #include "vm.h"
 #include "graphics.h"
@@ -375,29 +378,16 @@ 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;
+std::vector<int32_t> hexToInts(std::string hex) {
+  std::vector<int32_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);
+  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);
     bytes.push_back(byte);
   }

@@ -405,25 +395,28 @@ std::vector<uint8_t> hexToBytes(std::string hex) {
 }

 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";
+    std::stringstream outputstr;
+
+    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;

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

 void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
     //populate from string (assume correct length? TODO: validation)
-    auto byteVector = hexToBytes(cartDataStr);
+    auto intsVector = hexToInts(cartDataStr);

-    for(size_t i = 0; i < byteVector.size(); i++) {
-        _memory->cartData[i] = byteVector[i];
+    for(size_t i = 0; i < intsVector.size(); i++) {
+        vm_dset(i, fix32::frombits(intsVector[i]));
     }

 }
@@ -521,7 +514,6 @@ void Vm::QueueCartChange(std::string filename){
     _nextCartKey = filename;
     _cartChangeQueued = true;
     _pauseMenu = false;
-
 }

 int Vm::GetTargetFps() {
@@ -679,10 +671,34 @@ void Vm::vm_poke4(int addr, fix32 value){
     _memory->data[addr + 3] = (uint8_t)(ubits >> 24);
 }

-void Vm::vm_cartdata(string key) {
+std::string Vm::vm_cartdata(string key) {
+    //match pico 8 errors
+    if (_cartdataKey != "") {
+        QueueCartChange("__FAKE08-BIOS.p8");
+        _cartLoadError = "cartdata() can only be called once";
+        return _cartLoadError;
+    }
+    if (key.length() == 0 || key.length() > 64) {
+        QueueCartChange("__FAKE08-BIOS.p8");
+        _cartLoadError = "cart data id too long";
+        return _cartLoadError;
+    }
+    //todo: validate chars
+
     _cartdataKey = key;

+    auto cartDataStr = _host->getCartDataFileContents(_cartdataKey);
+
+    //todo: validate hex format
+    if (cartDataStr.length() > 0) {
+        deserializeCartDataToMemory(cartDataStr);
+    }
+
+    return "";
+
     //call host to get current cart data and init- set memory
+    //file name should match pico 8: {key}.p8d.txt in the cdata directory
+    //call host to get that string if anything exists
 }

 fix32 Vm::vm_dget(uint8_t n) {
@@ -696,9 +712,12 @@ fix32 Vm::vm_dget(uint8_t n) {
 void Vm::vm_dset(uint8_t n, fix32 value){
     if (_cartdataKey.length() > 0 && n < 64) {
         vm_poke4(0x5e00 + 4 * n, value);
-    }

-    //write to file via host
+        //pico 8 seems to write immediately
+        if (_cartdataKey.length() > 0) {
+            _host->saveCartData(_cartdataKey, getSerializedCartData());
+        }
+    }
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, Cart* cart){
diff --git a/source/vm.h b/source/vm.h
index 1a37908..d677285 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -106,7 +106,7 @@ class Vm {
     void vm_poke2(int addr, int16_t value);
     void vm_poke4(int addr, fix32 value);

-    void vm_cartdata(string key);
+    std::string vm_cartdata(string key);
     fix32 vm_dget(uint8_t n);
     void vm_dset(uint8_t n, fix32 value);

diff --git a/test/stubhost.cpp b/test/stubhost.cpp
index e8ff6e8..e2747a3 100644
--- a/test/stubhost.cpp
+++ b/test/stubhost.cpp
@@ -93,3 +93,14 @@ vector<string> Host::listcarts(){
 std::string Host::customBiosLua() {
     return "";
 }
+
+std::string Host::getCartDataFile(std::string cartDataKey) {
+    return "";
+}
+
+std::string Host::getCartDataFileContents(std::string cartDataKey) {
+    return "";
+}
+
+void Host::saveCartData(std::string cartDataKey, std::string contents) {
+}
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 7f156c2..0fb90d3 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -116,6 +116,13 @@ TEST_CASE("Vm memory functions") {
         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");
+
+        CHECK_EQ(vm->vm_peek4(0x5e80), (fix32)133);
+        CHECK_EQ(vm->vm_dget(32), (fix32)133);
+    }
     SUBCASE("poking draw palette data"){
         //21 : 0001 0101 (transparet: true) (color mapped 5)
         vm->vm_poke(0x5f02, 21);
@@ -483,6 +490,7 @@ TEST_CASE("Vm memory functions") {
     }

     SUBCASE("getSerializedCartData for all zeroes"){
+        vm->vm_cartdata("serializeTest");
         for(int i = 0; i < 256; i++) {
             memory->cartData[i] = 0;
         }
@@ -502,6 +510,7 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(expected, result);
     }
     SUBCASE("getSerializedCartData with 0-255"){
+        vm->vm_cartdata("serializeTest");
         for(int i = 0; i < 256; i++) {
             memory->cartData[i] = (uint8_t)i;
         }
@@ -509,18 +518,19 @@ TEST_CASE("Vm memory functions") {
         auto result = vm->getSerializedCartData();

         std::string expected =
-            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\n"
-            "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f\n"
-            "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f\n"
-            "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f\n"
-            "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f\n"
-            "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf\n"
-            "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf\n"
-            "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff\n";
+            "03020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c\n"
+            "23222120272625242b2a29282f2e2d2c33323130373635343b3a39383f3e3d3c\n"
+            "43424140474645444b4a49484f4e4d4c53525150575655545b5a59585f5e5d5c\n"
+            "63626160676665646b6a69686f6e6d6c73727170777675747b7a79787f7e7d7c\n"
+            "83828180878685848b8a89888f8e8d8c93929190979695949b9a99989f9e9d9c\n"
+            "a3a2a1a0a7a6a5a4abaaa9a8afaeadacb3b2b1b0b7b6b5b4bbbab9b8bfbebdbc\n"
+            "c3c2c1c0c7c6c5c4cbcac9c8cfcecdccd3d2d1d0d7d6d5d4dbdad9d8dfdedddc\n"
+            "e3e2e1e0e7e6e5e4ebeae9e8efeeedecf3f2f1f0f7f6f5f4fbfaf9f8fffefdfc\n";

         CHECK_EQ(expected, result);
     }
     SUBCASE("deserializeCartDataToMemory for all zeroes"){
+        vm->vm_cartdata("serializeTest");
         for(int i = 0; i < 256; i++) {
             memory->cartData[i] = 1;
         }
@@ -545,19 +555,20 @@ TEST_CASE("Vm memory functions") {
         CHECK(matches);
     }
     SUBCASE("getDeserializedCartData with 0-255"){
+        vm->vm_cartdata("serializeTest");
         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";
+            "03020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c\n"
+            "23222120272625242b2a29282f2e2d2c33323130373635343b3a39383f3e3d3c\n"
+            "43424140474645444b4a49484f4e4d4c53525150575655545b5a59585f5e5d5c\n"
+            "63626160676665646b6a69686f6e6d6c73727170777675747b7a79787f7e7d7c\n"
+            "83828180878685848b8a89888f8e8d8c93929190979695949b9a99989f9e9d9c\n"
+            "a3a2a1a0a7a6a5a4abaaa9a8afaeadacb3b2b1b0b7b6b5b4bbbab9b8bfbebdbc\n"
+            "c3c2c1c0c7c6c5c4cbcac9c8cfcecdccd3d2d1d0d7d6d5d4dbdad9d8dfdedddc\n"
+            "e3e2e1e0e7e6e5e4ebeae9e8efeeedecf3f2f1f0f7f6f5f4fbfaf9f8fffefdfc\n";

         vm->deserializeCartDataToMemory(values);

@@ -568,6 +579,26 @@ TEST_CASE("Vm memory functions") {

         CHECK(matches);
     }
+    SUBCASE("dset then getSerializedCartData returns correct values"){
+        vm->vm_cartdata("serializeTest");
+        vm->vm_dset(32, 128);
+        vm->vm_dset(0, 1);
+        vm->vm_dset(1, 2);
+
+        auto actual = vm->getSerializedCartData();
+
+        std::string expected =
+            "0001000000020000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0080000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n"
+            "0000000000000000000000000000000000000000000000000000000000000000\n";
+
+        CHECK_EQ(expected, actual);
+    }

     delete stubHost;
     delete graphics;