foxygit / fake-08-xbox360 Log in
commit 6940b0aa4347e34dbe499d89c81385b18f2d46a3
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Jan 18 19:40:00 2022 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Jan 18 19:40:00 2022 -0700

    Add extra user data to memory
---
 source/PicoRam.h |  4 +++-
 source/vm.cpp    | 21 +++++++++++----------
 test/vmtests.cpp | 20 ++++++++++++++++----
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/source/PicoRam.h b/source/PicoRam.h
index 401ea91..7963091 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -260,8 +260,10 @@ struct PicoRam
             hwState_t hwState;

             uint8_t screenBuffer[128 * 64];
+
+            uint8_t userData[0x8000];
         };

-        uint8_t data[0x8000];
+        uint8_t data[0x10000];
     };
 };
\ No newline at end of file
diff --git a/source/vm.cpp b/source/vm.cpp
index 84f50b7..9da8dcc 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -619,9 +619,10 @@ bool Vm::ExecuteLua(string luaString, string callbackFunction){
     return true;
 }

+const int MemoryUpperBound = 0x10000;

 uint8_t Vm::vm_peek(int addr){
-    if (addr < 0 || addr > 0x8000){
+    if (addr < 0 || addr > MemoryUpperBound){
         return 0;
     }

@@ -634,10 +635,10 @@ int16_t Vm::vm_peek2(int addr){
     for (int i = 0; i < 2; ++i)
     {
         /* This code handles partial reads by adding zeroes */
-        if (addr + i < 0x8000)
+        if (addr + i < MemoryUpperBound)
             bits |= _memory->data[addr + i] << (8 * i);
-        else if (addr + i >= 0x8000)
-            bits |= _memory->data[addr + i - 0x8000] << (8 * i);
+        else if (addr + i >= MemoryUpperBound)
+            bits |= _memory->data[addr + i - MemoryUpperBound] << (8 * i);
     }

     return bits;
@@ -650,10 +651,10 @@ fix32 Vm::vm_peek4(int addr){
     for (int i = 0; i < 4; ++i)
     {
         /* This code handles partial reads by adding zeroes */
-        if (addr + i < 0x8000)
+        if (addr + i < MemoryUpperBound)
             bits |= _memory->data[addr + i] << (8 * i);
-        else if (addr + i >= 0x8000)
-            bits |= _memory->data[addr + i - 0x8000] << (8 * i);
+        else if (addr + i >= MemoryUpperBound)
+            bits |= _memory->data[addr + i - MemoryUpperBound] << (8 * i);
     }

     return fix32::frombits(bits);
@@ -661,7 +662,7 @@ fix32 Vm::vm_peek4(int addr){

 void Vm::vm_poke(int addr, uint8_t value){
     //todo: check how pico 8 handles out of bounds
-    if (addr < 0 || addr > 0x8000){
+    if (addr < 0 || addr > MemoryUpperBound){
         return;
     }

@@ -669,7 +670,7 @@ void Vm::vm_poke(int addr, uint8_t value){
 }

 void Vm::vm_poke2(int addr, int16_t value){
-    if (addr < 0 || addr > 0x8000 - 1){
+    if (addr < 0 || addr > MemoryUpperBound - 1){
         return;
     }

@@ -679,7 +680,7 @@ void Vm::vm_poke2(int addr, int16_t value){
 }

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

diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 4be3221..001cac2 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -43,10 +43,10 @@ TEST_CASE("Vm memory functions") {
         CHECK(correctValues);
     }
     SUBCASE("size of PicoRam struct corrct"){
-        CHECK(sizeof(PicoRam) == 0x8000);
+        CHECK(sizeof(PicoRam) == 0x10000);
     }
     SUBCASE("size of data correct"){
-        CHECK(sizeof(memory->data) == 0x8000);
+        CHECK(sizeof(memory->data) == 0x10000);
     }
     SUBCASE("simple peek and poke"){
         vm->vm_poke(2, 232);
@@ -312,7 +312,7 @@ TEST_CASE("Vm memory functions") {

         CHECK_EQ(memory->hwState.gpioPins[0], 192);
     }
-    SUBCASE("poking screen data (first two pixels") {
+    SUBCASE("poking screen data (first two pixels)") {
         //195: 1100 0011 (left pixel 3, right pixel 12)
         vm->vm_poke(0x6000, 195);

@@ -320,7 +320,7 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(graphics->pget(0, 0), 3);
         CHECK_EQ(graphics->pget(1, 0), 12);
     }
-    SUBCASE("poking screen data (last two pixels") {
+    SUBCASE("poking screen data (last two pixels)") {
         //210: 1101 0010
         vm->vm_poke(0x7fff, 210);

@@ -328,6 +328,18 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(graphics->pget(126, 127), 2);
         CHECK_EQ(graphics->pget(127, 127), 13);
     }
+    SUBCASE("poking user data (first byte)") {
+        vm->vm_poke(0x8000, 129);
+
+        CHECK_EQ(memory->data[0x8000], 129);
+        CHECK_EQ(memory->userData[0], 129);
+    }
+    SUBCASE("poking user data (last byte)") {
+        vm->vm_poke(0xffff, 211);
+
+        CHECK_EQ(memory->data[0xffff], 211);
+        CHECK_EQ(memory->userData[32767], 211);
+    }
     SUBCASE("Loading cart copies cart rom to memory") {
         vm->LoadCart("cartparsetest.p8.png");