foxygit / fake-08-xbox360 Log in
commit 497bafebe7962fbb14fb796976878498002b3a69
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Mar 5 08:34:42 2022 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Mar 5 08:34:42 2022 -0700

    Fix handling of map width of 256
---
 source/graphics.cpp    |  4 ++--
 test/graphicstests.cpp | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 4c0fa8d..f520989 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1402,7 +1402,7 @@ std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> Graphics::clip(int x, int y, int

 uint8_t Graphics::mget(int celx, int cely){
 	const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
-	const int mapW = _memory->hwState.widthOfTheMap;
+	const int mapW = _memory->hwState.widthOfTheMap == 0 ? 256 : _memory->hwState.widthOfTheMap;
 	const int idx = cely * mapW + celx;

 	if (idx < 0) {
@@ -1432,7 +1432,7 @@ uint8_t Graphics::mget(int celx, int cely){

 void Graphics::mset(int celx, int cely, uint8_t snum){
 	const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
-	const int mapW = _memory->hwState.widthOfTheMap;
+	const int mapW = _memory->hwState.widthOfTheMap == 0 ? 256 : _memory->hwState.widthOfTheMap;
 	const int idx = cely * mapW + celx;

 	if (idx < 0) {
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 47282a1..bea62ce 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -2274,7 +2274,7 @@ TEST_CASE("graphics class behaves as expected") {

         checkPoints(graphics, expectedPoints);
     }
-SUBCASE("use extended ram for big map"){
+    SUBCASE("use extended ram for big map"){
         picoRam.data[0x5f56] = 0x80;

         for(int y = 0; y < 8; y++){
@@ -2366,6 +2366,39 @@ SUBCASE("use extended ram for big map"){

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("map width set to 256 functions as expected"){
+        picoRam.data[0x5f56] = 0x80;
+        picoRam.data[0x5f57] = 0;
+
+        for(int y = 0; y < 8; y++){
+            for(int x = 64; x < 96; x++) {
+                graphics->sset(x,y,(x/8) + 1);
+            }
+        }
+
+        graphics->mset(0, 0, 8);
+        graphics->mset(0, 1, 9);
+        graphics->mset(1, 0, 9);
+        graphics->mset(1, 1, 8);
+
+        graphics->mset(254, 126, 9);
+        graphics->mset(255, 0126, 10);
+        graphics->mset(254, 127, 10);
+        graphics->mset(255, 127, 9);
+
+        graphics->cls();
+
+        graphics->map(0, 0, 8, 8, 2, 2);
+        graphics->map(254, 126, 112, 112, 2, 2);
+
+        CHECK_EQ(picoRam.hwState.widthOfTheMap, 0);
+        CHECK_EQ(graphics->mget(0,0), 8);
+        CHECK_EQ(picoRam.data[0x8000], 8);
+        CHECK_EQ(picoRam.userData[0], 8);
+        CHECK_EQ(graphics->mget(255, 127), 9);
+        CHECK_EQ(picoRam.data[0xFFFF], 9);
+        CHECK_EQ(picoRam.userData[0x7FFF], 9);
+    }