foxygit / fake-08-xbox360 Log in
commit 44accd7eeeb50fa90a8167ae5b40af71121a9aee
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Sep 21 22:19:40 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Mon Sep 21 22:19:40 2020 -0600

    implement memset and memcpy
---
 source/picoluaapi.cpp |   2 +
 source/vm.cpp         |  41 ++++++++++++++-----
 source/vm.h           |  15 ++++---
 test/vmtests.cpp      | 109 ++++++++++++++++++++++++++++----------------------
 4 files changed, 102 insertions(+), 65 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 3c3cff0..e363a19 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -589,6 +589,8 @@ int sfx(lua_State *L) {

 //Memory
 int cstore(lua_State *L) {
+    //this is supposed to copy data from ram to the file.
+    //for now, not implementing this
     return noop("cstore");
 }

diff --git a/source/vm.cpp b/source/vm.cpp
index 5554d28..9511c10 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -418,7 +418,7 @@ bool Vm::ExecuteLua(string luaString, string callbackFunction){
 }


-uint8_t Vm::ram_peek(int addr){
+uint8_t Vm::vm_peek(int addr){
     if (addr < 0 || addr > 0x8000){
         return 0;
     }
@@ -426,7 +426,7 @@ uint8_t Vm::ram_peek(int addr){
     return _memory->data[addr];
 }

-int16_t Vm::ram_peek2(int addr){
+int16_t Vm::vm_peek2(int addr){
     //zepto8
     int16_t bits = 0;
     for (int i = 0; i < 2; ++i)
@@ -442,7 +442,7 @@ int16_t Vm::ram_peek2(int addr){
 }

 //note: this should return a 32 bit fixed point number
-int32_t Vm::ram_peek4(int addr){
+int32_t Vm::vm_peek4(int addr){
     //zepto8
     int32_t bits = 0;
     for (int i = 0; i < 4; ++i)
@@ -457,7 +457,7 @@ int32_t Vm::ram_peek4(int addr){
     return bits;
 }

-void Vm::ram_poke(int addr, uint8_t value){
+void Vm::vm_poke(int addr, uint8_t value){
     //todo: check how pico 8 handles out of bounds
     if (addr < 0 || addr > 0x8000){
         return;
@@ -466,7 +466,7 @@ void Vm::ram_poke(int addr, uint8_t value){
     _memory->data[addr] = value;
 }

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

 }

-void Vm::ram_poke4(int addr, int32_t value){
+void Vm::vm_poke4(int addr, int32_t value){
     if (addr < 0 || addr > 0x8000 - 3){
         return;
     }
@@ -493,7 +493,7 @@ void Vm::vm_cartdata(string key) {

 int32_t Vm::vm_dget(uint8_t n) {
     if (_cartdataKey.length() > 0 && n < 64) {
-        return ram_peek4(0x5e00 + 4 * n);
+        return vm_peek4(0x5e00 + 4 * n);
     }

     return 0;
@@ -501,7 +501,7 @@ int32_t Vm::vm_dget(uint8_t n) {

 void Vm::vm_dset(uint8_t n, int32_t value){
     if (_cartdataKey.length() > 0 && n < 64) {
-        ram_poke4(0x5e00 + 4 * n, value);
+        vm_poke4(0x5e00 + 4 * n, value);
     }
 }

@@ -510,15 +510,15 @@ void Vm::vm_reload(int destaddr, int sourceaddr, int len, Cart* cart){
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
-    if (destaddr < 0 || destaddr > 0x8000) {
+    if (destaddr < 0 || destaddr > (int)sizeof(PicoRam)) {
         //invalid dest address
         return;
     }
-    if (sourceaddr < 0 || sourceaddr > 0x4300) {
+    if (sourceaddr < 0 || sourceaddr > (int)sizeof(CartRomData)) {
         //invalid source address
         return;
     }
-    if (len < 0 || destaddr + len > 0x4300) {
+    if (len < 0 || destaddr + len > (int)sizeof(CartRomData)) {
         //invalid length address
         return;
     }
@@ -545,3 +545,22 @@ void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
         delete cart;
     }
 }
+
+void Vm::vm_memset(int destaddr, uint8_t val, int len){
+    if (destaddr < 0 || destaddr + len > (int)sizeof(PicoRam)) {
+        return;
+    }
+
+    memset(&_memory->data[destaddr], val, len);
+
+}
+void Vm::vm_memcpy(int destaddr, int sourceaddr, int len){
+    if (sourceaddr < 0 || sourceaddr + len > (int)sizeof(PicoRam)) {
+        return;
+    }
+    if (destaddr < 0 || destaddr + len > (int)sizeof(PicoRam)) {
+        return;
+    }
+
+    memcpy(&_memory->data[destaddr], &_memory->data[sourceaddr], len);
+}
diff --git a/source/vm.h b/source/vm.h
index 7dff467..e4cd53e 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -81,18 +81,21 @@ class Vm {

     PicoRam* getPicoRam();

-    uint8_t ram_peek(int addr);
-    int16_t ram_peek2(int addr);
-    int32_t ram_peek4(int addr); //note: this should return a 32 bit fixed point number
+    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

-    void ram_poke(int addr, uint8_t value);
-    void ram_poke2(int addr, int16_t value);
-    void ram_poke4(int addr, int32_t value); //note: this parameter should be a 32 bit fixed point number
+    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_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

     void vm_reload(int destaddr, int sourceaddr, int len, string filename);
+
+    void vm_memset(int destaddr, uint8_t val, int len);
+    void vm_memcpy(int destaddr, int sourceaddr, int len);
 };

diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 0c83768..f64e979 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -41,39 +41,39 @@ TEST_CASE("Vm memory functions") {
         CHECK(sizeof(memory->data) == 0x8000);
     }
     SUBCASE("simple peek and poke"){
-        vm->ram_poke(2, 232);
+        vm->vm_poke(2, 232);

-        CHECK(vm->ram_peek(2) == 232);
+        CHECK(vm->vm_peek(2) == 232);
     }
     SUBCASE("simple peek2 and poke2"){
-        vm->ram_poke2(41, -1031);
+        vm->vm_poke2(41, -1031);

-        CHECK(vm->ram_peek2(41) == -1031);
+        CHECK(vm->vm_peek2(41) == -1031);
     }
     SUBCASE("simple peek4 and poke4"){
-        vm->ram_poke4(0x7123, 49249);
+        vm->vm_poke4(0x7123, 49249);

-        CHECK(vm->ram_peek4(0x7123) == 49249);
+        CHECK(vm->vm_peek4(0x7123) == 49249);
     }
     SUBCASE("poking spritesheet"){
         //147: 1001 0011
         //left pixel: 0011 = 3
         //right pixel: 1001 = 9
-        vm->ram_poke(0, 147);
+        vm->vm_poke(0, 147);

         CHECK_EQ(graphics->sget(0, 0), 3);
         CHECK_EQ(graphics->sget(1, 0), 9);
     }
     SUBCASE("poking map"){
-        vm->ram_poke(0x2000, 201);
-        vm->ram_poke(0x2003, 11);
+        vm->vm_poke(0x2000, 201);
+        vm->vm_poke(0x2003, 11);

         CHECK_EQ(graphics->mget(0, 0), 201);
         CHECK_EQ(graphics->mget(3, 0), 11);
     }
     SUBCASE("poking sprite flags"){
         //39: 0010 0111
-        vm->ram_poke(0x3000, 39);
+        vm->vm_poke(0x3000, 39);

         CHECK_EQ(graphics->fget(0), 39);
         CHECK_EQ(graphics->fget(0, 0), true);
@@ -81,7 +81,7 @@ TEST_CASE("Vm memory functions") {
     }
     SUBCASE("poking music"){
         //78: 0100 1110
-        vm->ram_poke(0x3100, 78);
+        vm->vm_poke(0x3100, 78);

         CHECK_EQ(memory->songs[0].sfx0, 78);
         CHECK_EQ(memory->songs[0].start, 0);
@@ -90,13 +90,13 @@ TEST_CASE("Vm memory functions") {
         //97: 0110 0001
         //key is first 6 bits: 10 0001 : 33
         //waveform is next 3: (0) 01 : 1
-        vm->ram_poke(0x3200, 97);
+        vm->vm_poke(0x3200, 97);

         CHECK_EQ(memory->sfx[0].notes[0].key, 33);
         CHECK_EQ(memory->sfx[0].notes[0].waveform, 1);
     }
     SUBCASE("poking general use ram"){
-        vm->ram_poke(0x4300, 215);
+        vm->vm_poke(0x4300, 215);

         CHECK_EQ(memory->generalUseRam[0], 215);
     }
@@ -107,7 +107,7 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(vm->vm_dget(13), 56);
     }
     SUBCASE("poking cart data"){
-        vm->ram_poke(0x5e00, 56);
+        vm->vm_poke(0x5e00, 56);
         vm->vm_cartdata("dummy");

         CHECK_EQ(memory->cartData[0], 56);
@@ -115,7 +115,7 @@ TEST_CASE("Vm memory functions") {
     }
     SUBCASE("poking draw palette data"){
         //21 : 0001 0101 (transparet: true) (color mapped 5)
-        vm->ram_poke(0x5f02, 21);
+        vm->vm_poke(0x5f02, 21);

         CHECK_EQ(memory->drawState.drawPaletteMap[2], 21);
         CHECK_EQ(graphics->getDrawPalMappedColor(2), 5);
@@ -123,63 +123,63 @@ TEST_CASE("Vm memory functions") {
     }
     SUBCASE("poking clip data"){
         //7 : 0000 0111
-        vm->ram_poke(0x5f21, 89);
+        vm->vm_poke(0x5f21, 89);

         CHECK_EQ(memory->drawState.clip_yb, 89);
     }
     SUBCASE("poking unknown ram at 0x5f24"){
         //7 : 0000 0111
-        vm->ram_poke(0x5f24, 254);
+        vm->vm_poke(0x5f24, 254);

         CHECK_EQ(memory->drawState.unknown05f24, 254);
     }
     SUBCASE("poking color"){
-        vm->ram_poke(0x5f25, 12);
+        vm->vm_poke(0x5f25, 12);

         CHECK_EQ(memory->drawState.color, 12);
     }
     SUBCASE("poking text cursor"){
-        vm->ram_poke(0x5f26, 74);
+        vm->vm_poke(0x5f26, 74);

         CHECK_EQ(memory->drawState.text_x, 74);
     }
     SUBCASE("poking camera position"){
-        vm->ram_poke2(0x5f2a, -72);
+        vm->vm_poke2(0x5f2a, -72);

         CHECK_EQ(memory->drawState.camera_y, -72);
     }
     SUBCASE("poking draw mode"){
-        vm->ram_poke(0x5f2c, 2);
+        vm->vm_poke(0x5f2c, 2);

         CHECK_EQ(memory->drawState.drawMode, 2);
     }
     SUBCASE("poking dev kit mode"){
-        vm->ram_poke(0x5f2d, 2);
+        vm->vm_poke(0x5f2d, 2);

         CHECK_EQ(memory->drawState.devkitMode, 2);
     }
     SUBCASE("poking persist palette"){
-        vm->ram_poke(0x5f2e, 1);
+        vm->vm_poke(0x5f2e, 1);

         CHECK_EQ(memory->drawState.persistPalette, 1);
     }
     SUBCASE("poking sound pause state"){
-        vm->ram_poke(0x5f2f, 4);
+        vm->vm_poke(0x5f2f, 4);

         CHECK_EQ(memory->drawState.soundPauseState, 4);
     }
     SUBCASE("poking suppress pause"){
-        vm->ram_poke(0x5f30, 1);
+        vm->vm_poke(0x5f30, 1);

         CHECK_EQ(memory->drawState.suppressPause, 1);
     }
     SUBCASE("poking fill pattern"){
-        vm->ram_poke(0x5f31, 24);
+        vm->vm_poke(0x5f31, 24);

         CHECK_EQ(memory->drawState.fillPattern[0], 24);
     }
     SUBCASE("poking fill pattern transparency bit"){
-        vm->ram_poke(0x5f33, 1);
+        vm->vm_poke(0x5f33, 1);

         CHECK_EQ(memory->drawState.fillPatternTransparencyBit, 1);
     }
@@ -190,58 +190,58 @@ TEST_CASE("Vm memory functions") {
 -- bits 0x00FF.0000 are the usual colour bits
 -- bits 0x0000.FFFF are interpreted as the fill pattern
         */
-        vm->ram_poke(0x5f34, 36);
+        vm->vm_poke(0x5f34, 36);

         CHECK_EQ(memory->drawState.colorSettingFlag, 36);
     }
     SUBCASE("poking line invalid"){
-        vm->ram_poke(0x5f35, 1);
+        vm->vm_poke(0x5f35, 1);

         CHECK_EQ(memory->drawState.lineInvalid, 1);
     }
     SUBCASE("poking tline width"){
-        vm->ram_poke(0x5f38, 65);
+        vm->vm_poke(0x5f38, 65);

         CHECK_EQ(memory->drawState.tlineMapWidth, 65);
     }
     SUBCASE("poking tline height"){
-        vm->ram_poke(0x5f39, 66);
+        vm->vm_poke(0x5f39, 66);

         CHECK_EQ(memory->drawState.tlineMapHeight, 66);
     }
     SUBCASE("poking tline x"){
-        vm->ram_poke(0x5f3a, 67);
+        vm->vm_poke(0x5f3a, 67);

         CHECK_EQ(memory->drawState.tlineMapXOffset, 67);
     }
     SUBCASE("poking tline y"){
-        vm->ram_poke(0x5f3b, 68);
+        vm->vm_poke(0x5f3b, 68);

         CHECK_EQ(memory->drawState.tlineMapYOffset, 68);
     }
     SUBCASE("poking line x"){
-        vm->ram_poke2(0x5f3c, -3);
+        vm->vm_poke2(0x5f3c, -3);

         CHECK_EQ(memory->drawState.line_x, -3);
     }
     SUBCASE("poking line y"){
-        vm->ram_poke2(0x5f3e, 262);
+        vm->vm_poke2(0x5f3e, 262);

         CHECK_EQ(memory->drawState.line_y, 262);
     }
     SUBCASE("poking audio hardware state") {
-        vm->ram_poke(0x5f40, 3);
+        vm->vm_poke(0x5f40, 3);

         CHECK_EQ(memory->hwState.audioHardwareState[0], 3);
     }
     SUBCASE("poking rng state") {
-        vm->ram_poke(0x5f44, 20);
+        vm->vm_poke(0x5f44, 20);

         CHECK_EQ(memory->hwState.rngState[0], 20);
     }
     SUBCASE("poking button state") {
         //0000 1010
-        vm->ram_poke(0x5f4c, 10);
+        vm->vm_poke(0x5f4c, 10);

         CHECK_EQ(memory->hwState.buttonStates[0], 10);
         CHECK_EQ(input->btn(), 10);
@@ -251,48 +251,48 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(input->btn(3), true);
     }
     SUBCASE("poking unknown input block") {
-        vm->ram_poke(0x5f54, 53);
+        vm->vm_poke(0x5f54, 53);

         CHECK_EQ(memory->hwState.unknownInputBlock[0], 53);
     }
     SUBCASE("poking btnp repeat delay") {
-        vm->ram_poke(0x5f5c, 10);
+        vm->vm_poke(0x5f5c, 10);

         CHECK_EQ(memory->hwState.btnpRepeatDelay, 10);
     }
     SUBCASE("poking btnp repeat interval") {
-        vm->ram_poke(0x5f5d, 25);
+        vm->vm_poke(0x5f5d, 25);

         CHECK_EQ(memory->hwState.btnpRepeatInterval, 25);
     }
     SUBCASE("poking color bitmask") {
-        vm->ram_poke(0x5f5e, 42);
+        vm->vm_poke(0x5f5e, 42);

         CHECK_EQ(memory->hwState.colorBitmask, 42);
     }
     SUBCASE("poking alternate palette flag") {
-        vm->ram_poke(0x5f5f, 10);
+        vm->vm_poke(0x5f5f, 10);

         CHECK_EQ(memory->hwState.alternatePaletteFlag, 10);
     }
     SUBCASE("poking alternate palette map") {
-        vm->ram_poke(0x5f60, 11);
+        vm->vm_poke(0x5f60, 11);

         CHECK_EQ(memory->hwState.alternatePaletteMap[0], 11);
     }
     SUBCASE("poking alternate palette map") {
-        vm->ram_poke(0x5f70, 7);
+        vm->vm_poke(0x5f70, 7);

         CHECK_EQ(memory->hwState.alternatePaletteScreenLineBitfield[0], 7);
     }
     SUBCASE("poking gpio pins") {
-        vm->ram_poke(0x5f80, 192);
+        vm->vm_poke(0x5f80, 192);

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

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

         CHECK_EQ(memory->screenBuffer[8191], 210);
         CHECK_EQ(graphics->pget(126, 127), 2);
@@ -361,6 +361,19 @@ TEST_CASE("Vm memory functions") {
         CHECK_EQ(graphics->fget(0), 0);
         CHECK_EQ(graphics->mget(0, 0), 1);
     }
+    SUBCASE("memset sets memory") {
+        vm->vm_memset(0x3000, 13, 4);
+
+        CHECK_EQ(graphics->fget(0), 13);
+        CHECK_EQ(graphics->fget(3), 13);
+    }
+    SUBCASE("memcpy copies memory") {
+        vm->LoadCart("carts/cartparsetest.p8.png");
+        vm->vm_memcpy(0x3000, 0, 4);
+
+        CHECK_EQ(memory->spriteSheetData[1], 1);
+    }
+

     delete graphics;
     delete input;