foxygit / fake-08-xbox360 Log in
commit a446749053885c31bd19d2bc61d01ca7c2011957
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Tue Sep 22 08:24:46 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 22 08:24:46 2020 -0600

    Wire up memory functions to lua

    Also add end to end test, anf fix default color bug exposed by end to end
    test
---
 source/graphics.cpp                       |   2 +-
 source/picoluaapi.cpp                     |  82 ++++++++++++++++++++++++++++--
 source/picoluaapi.h                       |   4 ++
 source/vm.cpp                             |   6 ++-
 test/carts/memorytest.p8                  |  39 ++++++++++++++
 test/carts/screenshots/memorytest_f01.png | Bin 0 -> 508 bytes
 test/graphicstests.cpp                    |  58 ++++++++++-----------
 7 files changed, 155 insertions(+), 36 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 7027985..03aa4a1 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -45,7 +45,7 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {
 	//set default clip
 	clip();
 	pal();
-	color(7);
+	color(6);
 }


diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index e363a19..cd21e89 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -595,23 +595,95 @@ int cstore(lua_State *L) {
 }

 int api_memcpy(lua_State *L) {
-    return noop("memcpy");
+    int dest = lua_tonumber(L,1);
+    int src = lua_tonumber(L,2);
+    int len = lua_tonumber(L,3);
+
+    _vmForLuaApi->vm_memcpy(dest, src, len);
+
+    return 0;
 }

 int api_memset(lua_State *L) {
-    return noop("memset");
+    int dest = lua_tonumber(L,1);
+    int val = lua_tonumber(L,2);
+    int len = lua_tonumber(L,3);
+
+    _vmForLuaApi->vm_memset(dest, val, len);
+
+    return 0;
 }

 int peek(lua_State *L) {
-    return noopreturns(L, "peek");
+    int addr = lua_tonumber(L,1);
+
+    uint8_t val = _vmForLuaApi->vm_peek(addr);
+
+    lua_pushinteger(L, val);
+
+    return 1;
 }

 int poke(lua_State *L) {
-    return noop("poke");
+    int dest = lua_tonumber(L,1);
+    int val = lua_tonumber(L,2);
+
+    _vmForLuaApi->vm_poke(dest, (uint8_t)val);
+
+    return 0;
+}
+
+int peek2(lua_State *L) {
+    int addr = lua_tonumber(L,1);
+
+    int16_t val = _vmForLuaApi->vm_peek2(addr);
+
+    lua_pushinteger(L, val);
+
+    return 1;
+}
+
+int poke2(lua_State *L) {
+    int dest = lua_tonumber(L,1);
+    int val = lua_tonumber(L,2);
+
+    _vmForLuaApi->vm_poke2(dest, (int16_t)val);
+
+    return 0;
+}
+
+int peek4(lua_State *L) {
+    int addr = lua_tonumber(L,1);
+
+    int32_t val = _vmForLuaApi->vm_peek4(addr);
+
+    lua_pushinteger(L, val);
+
+    return 1;
+}
+
+int poke4(lua_State *L) {
+    int dest = lua_tonumber(L,1);
+    int val = lua_tonumber(L,2);
+
+    _vmForLuaApi->vm_poke4(dest, (int32_t)val);
+
+    return 0;
 }

 int reload(lua_State *L) {
-    return noop("reload");
+    int dest = lua_tonumber(L,1);
+    int src = lua_tonumber(L,2);
+    int len = lua_tonumber(L,3);
+    const char * str = "";
+
+    if (lua_gettop(L) > 3) {
+        str = lua_tolstring(L, 4, nullptr);
+    }
+
+    _vmForLuaApi->vm_reload(dest, src, len, str);
+
+    return 0;
 }

 //cart data
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 6c76ccf..f203582 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -73,6 +73,10 @@ int api_memcpy(lua_State *L);
 int api_memset(lua_State *L);
 int peek(lua_State *L);
 int poke(lua_State *L);
+int peek2(lua_State *L);
+int poke2(lua_State *L);
+int peek4(lua_State *L);
+int poke4(lua_State *L);
 int reload(lua_State *L);

 //cart data
diff --git a/source/vm.cpp b/source/vm.cpp
index 9511c10..c340e0a 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -100,7 +100,7 @@ bool Vm::loadCart(Cart* cart) {
     _memory->Reset();

     //set graphics state
-    _graphics->color(7);
+    _graphics->color(6);
     _graphics->clip();
     _graphics->pal();

@@ -187,6 +187,10 @@ bool Vm::loadCart(Cart* cart) {
     lua_register(_luaState, "memset", api_memset);
     lua_register(_luaState, "peek", peek);
     lua_register(_luaState, "poke", poke);
+    lua_register(_luaState, "peek2", peek2);
+    lua_register(_luaState, "poke2", poke2);
+    lua_register(_luaState, "peek4", peek4);
+    lua_register(_luaState, "poke4", poke4);
     lua_register(_luaState, "reload", reload);

     //stubbed in cart data
diff --git a/test/carts/memorytest.p8 b/test/carts/memorytest.p8
new file mode 100644
index 0000000..7aa1547
--- /dev/null
+++ b/test/carts/memorytest.p8
@@ -0,0 +1,39 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _init()
+	--first sfx byte
+	sfxbyte = peek(0x3200)
+	--send sfx byte to sprite sheet
+	poke(0x0004, sfxbyte)
+	--read spr byte to verify
+	sprbyte = peek2(0x0004)
+	--set all sprite flags to 1
+	memset(0x3000, 1, 0xff)
+	--read sprite flag to verify
+	flag = fget(200)
+	--copy sprite flags to map
+	memcpy(0x2000, 0x3000, 128)
+end
+
+function _draw()
+	cls()
+	print(sfxbyte)
+	print(sprbyte)
+	spr(1, 20, 0)
+	print(flag)
+	map(0, 0, 0, 120, 8, 1)
+end
+__gfx__
+ff000000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__map__
+0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__sfx__
+001d00000d0300e0500e0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/memorytest_f01.png b/test/carts/screenshots/memorytest_f01.png
new file mode 100644
index 0000000..e0d0ec4
Binary files /dev/null and b/test/carts/screenshots/memorytest_f01.png differ
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 14dea62..b409151 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -91,7 +91,7 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK(picoRam.drawState.clip_ye == 128);
     }
     SUBCASE("Constructor sets default color") {
-        CHECK(picoRam.drawState.color == 7);
+        CHECK(picoRam.drawState.color == 6);
     }
     SUBCASE("Constructor sets default draw color palette") {
         //colors mapped to themselves
@@ -319,14 +319,14 @@ TEST_CASE("graphics class behaves as expected") {

         //quarter circle from 12 oclock to 3 oclock
         std::vector<coloredPoint> expectedPoints = {
-            {39, 36, 7},
-            {40, 36, 7},
-            {41, 36, 7},
-            {42, 37, 7},
-            {43, 37, 7},
-            {43, 38, 7},
-            {44, 39, 7},
-            {44, 40, 7}
+            {39, 36, 6},
+            {40, 36, 6},
+            {41, 36, 6},
+            {42, 37, 6},
+            {43, 37, 6},
+            {43, 38, 6},
+            {44, 39, 6},
+            {44, 40, 6}
         };

         checkPoints(graphics, expectedPoints);
@@ -386,15 +386,15 @@ TEST_CASE("graphics class behaves as expected") {

         //quarter circle from 12 oclock to 3 oclock
         std::vector<coloredPoint> expectedPoints = {
-            {39, 36, 7},
-            {40, 36, 7},
-            {41, 36, 7},
-            {42, 37, 7},
-            {43, 37, 7},
-            {43, 38, 7},
-            {44, 39, 7},
-            {44, 40, 7},
-            {40, 40, 7},//center point
+            {39, 36, 6},
+            {40, 36, 6},
+            {41, 36, 6},
+            {42, 37, 6},
+            {43, 37, 6},
+            {43, 38, 6},
+            {44, 39, 6},
+            {44, 40, 6},
+            {40, 40, 6},//center point
         };

         checkPoints(graphics, expectedPoints);
@@ -1098,13 +1098,13 @@ TEST_CASE("graphics class behaves as expected") {
         graphics->camera();

         std::vector<coloredPoint> expectedPoints = {
-            {100, 100, 7},
-            {101, 100, 7},
-            {102, 100, 7},
-            {101, 101, 7},
-            {101, 102, 7},
-            {101, 103, 7},
-            {101, 104, 7}
+            {100, 100, 6},
+            {101, 100, 6},
+            {102, 100, 6},
+            {101, 101, 6},
+            {101, 102, 6},
+            {101, 103, 6},
+            {101, 104, 6}
         };

         checkPoints(graphics, expectedPoints);
@@ -1326,10 +1326,10 @@ TEST_CASE("graphics class behaves as expected") {
             {100, 100, 0},
             {101, 100, 0},
             {102, 100, 0},
-            {101, 101, 7},
-            {101, 102, 7},
-            {101, 103, 7},
-            {101, 104, 7}
+            {101, 101, 6},
+            {101, 102, 6},
+            {101, 103, 6},
+            {101, 104, 6}
         };

         checkPoints(graphics, expectedPoints);