foxygit / fake-08-xbox360 Log in
commit d13eb00780b5d848a5d9290672797e87ec3b7d23
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Mon Jan 30 19:30:21 2023 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Mon Jan 30 19:30:21 2023 -0700

    Implement poke from p8scii control code
---
 source/printHelper.cpp                        |  39 ++++++++++++++++++++++++--
 test/carts/print_mem_poke.p8                  |  26 +++++++++++++++++
 test/carts/screenshots/print_mem_poke_f01.png | Bin 0 -> 472 bytes
 test/endtoendtests.cpp                        |  14 +++++++++
 4 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index ab1de51..91151a7 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -12,7 +12,7 @@ Graphics* _ph_graphics;
 Vm* _ph_vm;
 Audio* _ph_audio;

-void oneOffCharToBytes(std::string hex, uint8_t byteBuff[]) {
+void hexStrToBytes(std::string hex, uint8_t byteBuff[]) {
   char buff[3];
   buff[2] = 0;
   for (unsigned int i = 0; i < hex.length(); i += 2) {
@@ -105,6 +105,7 @@ int print(std::string str, int x, int y, uint8_t c) {
     uint8_t fgColor = _ph_mem->drawState.color;
     uint8_t charBytes[8];
     uint8_t audioStrBytes[32];
+    bool cancelWrap = false;

     uint8_t printMode = _ph_mem->hwState.printAttributes;

@@ -264,7 +265,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                 if (commandChar == ':') {
                     std::string hexStr = str.substr(n+1, 16);
                     n+=16;
-                    oneOffCharToBytes(hexStr, charBytes);
+                    hexStrToBytes(hexStr, charBytes);
                 }
                 else {
                     std::string binStr = str.substr(n+1, 8);
@@ -320,7 +321,39 @@ int print(std::string str, int x, int y, uint8_t c) {
                     }
                 }
             }
+            else if (commandChar == '!'){
+                //no wraping after this print call (may be pico 8 bug?)
+                cancelWrap = true;

+                std::string addrHexStr = str.substr(n+1, 4);
+                n+=4;
+                int addr = (int)strtol(addrHexStr.c_str(), NULL, 16);
+
+                int size = length - n;
+                std::string binStr = str.substr(n + 1, size);
+                n+=size;
+
+                for(size_t i = 0; i < size; i++) {
+                    _ph_mem->data[addr + i] = binStr[i];
+                }
+            }
+            else if (commandChar == '@'){
+                std::string addrHexStr = str.substr(n+1, 4);
+                n+=4;
+                int addr = (int)strtol(addrHexStr.c_str(), NULL, 16);
+
+                std::string sizeHexStr = str.substr(n+1, 4);
+                n+=4;
+
+                int size = (int)strtol(sizeHexStr.c_str(), NULL, 16);
+
+                std::string binStr = str.substr(n + 1, size);
+                n+= size;
+
+                for(size_t i = 0; i < size; i++) {
+                    _ph_mem->data[addr + i] = binStr[i];
+                }
+            }
 		}
 		else if (ch == 7) { // "\a" audio command
             uint8_t nextChar = str[++n];
@@ -412,7 +445,7 @@ int print(std::string str, int x, int y, uint8_t c) {
         }
 	}

-    lineHeight = lineHeight > 0 ? lineHeight : 6;
+    lineHeight = lineHeight > 0 ? lineHeight : cancelWrap ? 0 : 6;
 	//todo: auto scrolling
 	_ph_mem->drawState.text_y = y + lineHeight;

diff --git a/test/carts/print_mem_poke.p8 b/test/carts/print_mem_poke.p8
new file mode 100644
index 0000000..79690b6
--- /dev/null
+++ b/test/carts/print_mem_poke.p8
@@ -0,0 +1,26 @@
+pico-8 cartridge // http://www.pico-8.com
+version 39
+__lua__
+function _init()
+
+end
+
+function _draw()
+ cls()
+
+ print("\^@70000004xxxxhello")
+
+ print("\^!7600aabcdefghello")
+
+ print(peek(0x7000))
+ print(peek(0x7001))
+ print(peek(0x7600))
+ print(peek(0x7601))
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/print_mem_poke_f01.png b/test/carts/screenshots/print_mem_poke_f01.png
new file mode 100644
index 0000000..4f49498
Binary files /dev/null and b/test/carts/screenshots/print_mem_poke_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index afd2bec..60429e1 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -682,6 +682,20 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("p8scii control code memory access"){
+        vm->LoadCart("print_mem_poke.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, host, "carts/screenshots/print_mem_poke_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;