foxygit / fake-08-xbox360 Log in
commit b84fd457a26890401fcb0ee9bef91fbfeeaf8336
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Tue Jun 22 22:31:58 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Jun 22 22:31:58 2021 -0600

    Fix tline wrapping bug and honor color bitmask
---
 source/PicoRam.h       |  2 ++
 source/graphics.cpp    | 22 +++++++++++++++++++---
 source/picoluaapi.cpp  |  4 ++--
 test/audiotests.cpp    |  2 +-
 test/graphicstests.cpp |  2 +-
 test/vmtests.cpp       | 13 ++++++++-----
 6 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/source/PicoRam.h b/source/PicoRam.h
index 750f815..ec090ec 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -227,6 +227,8 @@ struct PicoRam
         memset(data, 0, 0x4300);
         //leave general use memory
         memset(data + 0x5600, 0, 0x8000 - 0x5600);
+        //colorBitmask starts at 255
+        hwState.colorBitmask = 0xff;
     }

     union
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 39d9ca5..2378e2c 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -405,6 +405,13 @@ void Graphics::_setPixelFromSprite(int x, int y, uint8_t col) {

 	col = getDrawPalMappedColor(col);

+	//from pico 8 wiki:
+	//dst_color = (dst_color & ~write_mask) | (src_color & write_mask & read_mask)
+	uint8_t writeMask = _memory->hwState.colorBitmask & 15;
+	uint8_t readMask = _memory->hwState.colorBitmask >> 4;
+    uint8_t source = pget(x, y);
+    col = (col & writeMask) | (source & ~writeMask & readMask);
+
 	setPixelNibble(x, y, col, _memory->screenBuffer);
 }

@@ -435,7 +442,16 @@ void Graphics::_setPixelFromPen(int x, int y) {
 		finalC = col1;
 	}

-	setPixelNibble(x, y, getDrawPalMappedColor(finalC), _memory->screenBuffer);
+	finalC = getDrawPalMappedColor(finalC);
+
+	//from pico 8 wiki:
+	//dst_color = (dst_color & ~write_mask) | (src_color & write_mask & read_mask)
+	uint8_t writeMask = _memory->hwState.colorBitmask & 15;
+	uint8_t readMask = _memory->hwState.colorBitmask >> 4;
+    uint8_t source = pget(x, y);
+    finalC = (finalC & writeMask) | (source & ~writeMask & readMask);
+
+	setPixelNibble(x, y, finalC, _memory->screenBuffer);
 }
 //end helper methods

@@ -648,8 +664,8 @@ void Graphics::tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my, fix32 m

 	for (;;) {
         // Find sprite in map memory
-        int sx = (ds.tlineMapXOffset + int(mx)) & 0x7f;
-        int sy = (ds.tlineMapYOffset + int(my)) & 0x3f;
+        int sx = (ds.tlineMapXOffset + int(mx));
+        int sy = (ds.tlineMapYOffset + int(my));
 		uint8_t sprite = mget(sx, sy);
         //uint8_t bits = fget(sprite);

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 2fc0ce4..ef88661 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -988,9 +988,9 @@ int peek(lua_State *L) {

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

-    _vmForLuaApi->vm_poke(dest, (uint8_t)val);
+    _vmForLuaApi->vm_poke(dest, val);

     return 0;
 }
diff --git a/test/audiotests.cpp b/test/audiotests.cpp
index da19d7f..d816b8c 100644
--- a/test/audiotests.cpp
+++ b/test/audiotests.cpp
@@ -5,7 +5,7 @@
 TEST_CASE("audio class behaves as expected") {
     //general setup
     PicoRam picoRam;
-    picoRam = {0};
+    picoRam.Reset();
     Audio* audio = new Audio(&picoRam);
     audioState* audioState = audio->getAudioState();

diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index d061b83..5669437 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -45,7 +45,7 @@ TEST_CASE("graphics class behaves as expected") {
     //general setup
     std::string fontdata = get_font_data();
     PicoRam picoRam;
-    picoRam = {0};
+    picoRam.Reset();
     Graphics* graphics = new Graphics(fontdata, &picoRam);
     graphics->cls();

diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 5a51157..cf7ea7b 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -23,21 +23,24 @@ TEST_CASE("Vm memory functions") {
     SUBCASE("memory data stats with 0"){
         CHECK(memory->data[0] == 0);
     }
-    SUBCASE("resetting memory zeroes out everything except general use"){
+    SUBCASE("resetting memory zeroes out everything except general use and color bitmask"){
         for(int i = 0; i < 0x8000; ++i) {
             memory->data[i] = i & 255;
         }

         memory->Reset();

-        bool allZeroes = true;
+        bool correctValues = true;
         for(int i = 0; i < 0x8000; ++i) {
-            if (i < 0x4300 || i > 0x55ff) {
-                allZeroes &= memory->data[i] == 0;
+            if (i == 0x5f5e) {
+                correctValues &= memory->data[i] == 255;
+            }
+            else if (i < 0x4300 || i > 0x55ff) {
+                correctValues &= memory->data[i] == 0;
             }
         }

-        CHECK(allZeroes);
+        CHECK(correctValues);
     }
     SUBCASE("size of PicoRam struct corrct"){
         CHECK(sizeof(PicoRam) == 0x8000);