foxygit / fake-08-xbox360 Log in
commit 17d47ce2f57d5b078b6ce93724477c80dddbacb7
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Nov 14 21:50:25 2020 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Nov 14 21:50:25 2020 -0700

    Various fixes for reload()
---
 source/picoluaapi.cpp      | 16 ++++++++++++----
 source/vm.cpp              |  6 +++---
 test/carts/reloadininit.p8 | 19 +++++++++++++++++++
 test/endtoendtests.cpp     |  6 ++++++
 4 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 6e50417..14bbfa9 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -686,11 +686,19 @@ int poke4(lua_State *L) {
 }

 int reload(lua_State *L) {
-    int dest = lua_tonumber(L,1);
-    int src = lua_tonumber(L,2);
-    int len = lua_tonumber(L,3);
+    int dest = 0;
+    int src = 0;
+    int len = 0x4300;
     const char * str = "";
-
+    if (lua_gettop(L) > 0) {
+        dest = lua_tonumber(L,1);
+    }
+    if (lua_gettop(L) > 1) {
+        src = lua_tonumber(L,2);
+    }
+    if (lua_gettop(L) > 2) {
+        len = lua_tonumber(L,3);
+    }
     if (lua_gettop(L) > 3) {
         str = lua_tolstring(L, 4, nullptr);
     }
diff --git a/source/vm.cpp b/source/vm.cpp
index 2c2ecb9..410981d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -127,6 +127,8 @@ bool Vm::loadCart(Cart* cart) {
     //copy data from cart rom to ram
     vm_reload(0, 0, sizeof(cart->CartRom), cart);

+    _loadedCart = cart;
+
     // initialize Lua interpreter
     _luaState = luaL_newstate();

@@ -268,8 +270,6 @@ bool Vm::loadCart(Cart* cart) {
     }
     lua_pop(_luaState, 0);

-    _loadedCart = cart;
-
     _cartLoadError = "";

     return true;
@@ -526,7 +526,7 @@ void Vm::vm_dset(uint8_t n, int32_t value){
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, Cart* cart){
-    memcpy(&_memory->data[destaddr], &cart->CartRom.data[sourceaddr], sizeof(cart->CartRom));
+    memcpy(&_memory->data[destaddr], &cart->CartRom.data[sourceaddr], len);
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
diff --git a/test/carts/reloadininit.p8 b/test/carts/reloadininit.p8
new file mode 100644
index 0000000..919a17a
--- /dev/null
+++ b/test/carts/reloadininit.p8
@@ -0,0 +1,19 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _init()
+  reload()
+end
+__gfx__
+ff102030555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__gff__
+0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__map__
+0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__sfx__
+001d00000d0300e0500e0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0010000036050310502a05019050192501925019250192501925019250192501925019250192501b2501b250192501b2501b2501b2501b2501b2501b2501b2501b2501925019250192501b250000000000000000
+__music__
+00 01424344
+
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 621250f..191e1c3 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -417,6 +417,12 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("calling reload from init doesn't crash") {
+        vm->LoadCart("carts/reloadininit.p8");
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+    }

     delete vm;
 }
\ No newline at end of file