foxygit / fake-08-xbox360 Log in
commit 5f5b796de28a4b74b8cd4f251e053ca8432844fe
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Mar 15 20:54:21 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Mon Mar 15 20:54:21 2021 -0600

    Fix peek4, poke4, and dset/dget to use fix32
---
 source/picoluaapi.cpp                    |  12 +++---
 source/vm.cpp                            |  19 +++++-----
 source/vm.h                              |   8 ++--
 test/carts/peek4test.p8                  |  62 +++++++++++++++++++++++++++++++
 test/carts/screenshots/peek4test_f01.png | Bin 0 -> 999 bytes
 test/endtoendtests.cpp                   |  18 ++++++++-
 test/vmtests.cpp                         |  16 ++++----
 7 files changed, 106 insertions(+), 29 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 15ef97a..51b3373 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -760,18 +760,18 @@ int poke2(lua_State *L) {
 int peek4(lua_State *L) {
     int addr = lua_tonumber(L,1);

-    int32_t val = _vmForLuaApi->vm_peek4(addr);
+    fix32 val = _vmForLuaApi->vm_peek4(addr);

-    lua_pushinteger(L, val);
+    lua_pushnumber(L, val);

     return 1;
 }

 int poke4(lua_State *L) {
     int dest = lua_tonumber(L,1);
-    int val = lua_tonumber(L,2);
+    fix32 val = lua_tonumber(L,2);

-    _vmForLuaApi->vm_poke4(dest, (int32_t)val);
+    _vmForLuaApi->vm_poke4(dest, val);

     return 0;
 }
@@ -811,9 +811,9 @@ int cartdata(lua_State *L) {
 int dget(lua_State *L) {
     int addr = lua_tonumber(L,1);

-    int32_t val = _vmForLuaApi->vm_dget(addr);
+    fix32 val = _vmForLuaApi->vm_dget(addr);

-    lua_pushinteger(L, val);
+    lua_pushnumber(L, val);

     return 1;
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index 1540a4e..200834c 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -579,7 +579,7 @@ int16_t Vm::vm_peek2(int addr){
 }

 //note: this should return a 32 bit fixed point number
-int32_t Vm::vm_peek4(int addr){
+fix32 Vm::vm_peek4(int addr){
     //zepto8
     int32_t bits = 0;
     for (int i = 0; i < 4; ++i)
@@ -591,7 +591,7 @@ int32_t Vm::vm_peek4(int addr){
             bits |= _memory->data[addr + i - 0x8000] << (8 * i);
     }

-    return bits;
+    return fix32::frombits(bits);
 }

 void Vm::vm_poke(int addr, uint8_t value){
@@ -613,22 +613,23 @@ void Vm::vm_poke2(int addr, int16_t value){

 }

-void Vm::vm_poke4(int addr, int32_t value){
+void Vm::vm_poke4(int addr, fix32 value){
     if (addr < 0 || addr > 0x8000 - 3){
         return;
     }

-    _memory->data[addr + 0] = (uint8_t)value;
-    _memory->data[addr + 1] = (uint8_t)(value >> 8);
-    _memory->data[addr + 2] = (uint8_t)(value >> 16);
-    _memory->data[addr + 3] = (uint8_t)(value >> 24);
+    uint32_t ubits = (uint32_t)value.bits();
+    _memory->data[addr + 0] = (uint8_t)ubits;
+    _memory->data[addr + 1] = (uint8_t)(ubits >> 8);
+    _memory->data[addr + 2] = (uint8_t)(ubits >> 16);
+    _memory->data[addr + 3] = (uint8_t)(ubits >> 24);
 }

 void Vm::vm_cartdata(string key) {
     _cartdataKey = key;
 }

-int32_t Vm::vm_dget(uint8_t n) {
+fix32 Vm::vm_dget(uint8_t n) {
     if (_cartdataKey.length() > 0 && n < 64) {
         return vm_peek4(0x5e00 + 4 * n);
     }
@@ -636,7 +637,7 @@ int32_t Vm::vm_dget(uint8_t n) {
     return 0;
 }

-void Vm::vm_dset(uint8_t n, int32_t value){
+void Vm::vm_dset(uint8_t n, fix32 value){
     if (_cartdataKey.length() > 0 && n < 64) {
         vm_poke4(0x5e00 + 4 * n, value);
     }
diff --git a/source/vm.h b/source/vm.h
index 97da42e..afbcde6 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -97,15 +97,15 @@ class Vm {

     uint8_t vm_peek(int addr);
     int16_t vm_peek2(int addr);
-    int32_t vm_peek4(int addr); //note: this should return a 32 bit fixed point number
+    fix32 vm_peek4(int addr);

     void vm_poke(int addr, uint8_t value);
     void vm_poke2(int addr, int16_t value);
-    void vm_poke4(int addr, int32_t value); //note: this parameter should be a 32 bit fixed point number
+    void vm_poke4(int addr, fix32 value);

     void vm_cartdata(string key);
-    int32_t vm_dget(uint8_t n);
-    void vm_dset(uint8_t n, int32_t value); //note: this should return a 32 bit fixed point number
+    fix32 vm_dget(uint8_t n);
+    void vm_dset(uint8_t n, fix32 value);

     void vm_reload(int destaddr, int sourceaddr, int len, string filename);

diff --git a/test/carts/peek4test.p8 b/test/carts/peek4test.p8
new file mode 100644
index 0000000..f288014
--- /dev/null
+++ b/test/carts/peek4test.p8
@@ -0,0 +1,62 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+orig1 = peek4(0x500)
+orig2 = peek4(0x504)
+orig3 = peek4(0x508)
+orig4 = peek4(0x50c)
+cartdata("dummy")
+
+function set_layer(top)
+	if top then
+		poke4(0x5f00,0x0e00.0180)
+		poke4(0x5f04,0x0e00.0180)
+		poke4(0x5f08,0x0e00.0180)
+		poke4(0x5f0c,0x0e00.0180)
+	else
+		poke4(0x5f00,0x8080.8080)
+		poke4(0x5f04,0x0101.0101)
+		poke4(0x5f08,0x0000.0000)
+		poke4(0x5f0c,0x0e0e.0e0e)
+	end
+end
+
+function _draw()
+ cls()
+
+ print(orig1)
+	print(orig2)
+	print(orig3)
+	print(orig4)
+
+ print("")
+ set_layer(true)
+
+	print(peek4(0x5f00))
+	print(peek4(0x5f04))
+	print(peek4(0x5f08))
+	print(peek4(0x5f0c))
+
+	print("")
+ set_layer(false)
+
+ print(peek4(0x5f00))
+	print(peek4(0x5f04))
+	print(peek4(0x5f08))
+	print(peek4(0x5f0c))
+
+
+ print("")
+ poke4(0x5e00,0x0ef0.0189)
+
+ print(peek4(0x5e00))
+ print(dget(0))
+
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/peek4test_f01.png b/test/carts/screenshots/peek4test_f01.png
new file mode 100644
index 0000000..0e92107
Binary files /dev/null and b/test/carts/screenshots/peek4test_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 41ef4cc..3e31e72 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -52,9 +52,9 @@ bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {

         if (!pixelMatches) {

-            //printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);
+            printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);

-            //printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
+            printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
         }

         pixelsMatch &= pixelMatches;
@@ -436,6 +436,20 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("Peek4/poke4 test cart"){
+        vm->LoadCart("carts/peek4test.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/peek4test_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index eb877ba..8efd738 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -54,9 +54,9 @@ TEST_CASE("Vm memory functions") {
         CHECK(vm->vm_peek2(41) == -1031);
     }
     SUBCASE("simple peek4 and poke4"){
-        vm->vm_poke4(0x7123, 49249);
+        vm->vm_poke4(0x7123, 3584.0059);

-        CHECK(vm->vm_peek4(0x7123) == 49249);
+        CHECK(vm->vm_peek4(0x7123).bits() == ((z8::fix32)3584.0059).bits());
     }
     SUBCASE("poking spritesheet"){
         //147: 1001 0011
@@ -107,14 +107,14 @@ TEST_CASE("Vm memory functions") {
         vm->vm_cartdata("dummy");
         vm->vm_dset(13, 56);

-        CHECK_EQ(vm->vm_dget(13), 56);
+        CHECK_EQ(vm->vm_dget(13), (fix32)56);
     }
     SUBCASE("poking cart data"){
-        vm->vm_poke(0x5e00, 56);
+        vm->vm_poke(0x5e02, 56);
         vm->vm_cartdata("dummy");

-        CHECK_EQ(memory->cartData[0], 56);
-        CHECK_EQ(vm->vm_dget(0), 56);
+        CHECK_EQ(memory->cartData[2], 56);
+        CHECK_EQ(vm->vm_dget(0), (fix32)56);
     }
     SUBCASE("poking draw palette data"){
         //21 : 0001 0101 (transparet: true) (color mapped 5)
@@ -381,13 +381,13 @@ TEST_CASE("Vm memory functions") {
     SUBCASE("dget before setting cart data key does nothing") {
         vm->vm_dset(34, 1923);

-        CHECK_EQ(vm->vm_dget(34), 0);
+        CHECK_EQ(vm->vm_dget(34).bits(), 0);
     }
     SUBCASE("dget after setting cart data key stores value") {
         vm->vm_cartdata("uniquekey");
         vm->vm_dset(34, 1923);

-        CHECK_EQ(vm->vm_dget(34), 1923);
+        CHECK_EQ(vm->vm_dget(34), (fix32)1923);
     }
     SUBCASE("pico driller style input"){
         vm->LoadCart("carts/drillerinputtest.p8");