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

    WIP improving memory compat and adding tests
---
 source/Input.cpp       |   9 ++++
 source/Input.h         |   2 +-
 source/PicoRam.h       |  33 ++-----------
 source/vm.cpp          | 126 ++++++++++++++++++++++++++++++++++---------------
 source/vm.h            |  20 ++++++--
 test/endtoendtests.cpp |   1 -
 test/vmtests.cpp       |  79 ++++++++++++++++++++++++++++++-
 7 files changed, 197 insertions(+), 73 deletions(-)

diff --git a/source/Input.cpp b/source/Input.cpp
index f02c23b..471d1a7 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -1,6 +1,15 @@
+#include <algorithm>
+
 #include "Input.h"
 #include "hostVmShared.h"

+Input::Input():
+    _currentKDown(0),
+    _currentKHeld(0)
+{
+    std::fill(_framesHeld, _framesHeld + 8, 0);
+}
+
 void Input::SetState(uint8_t kdown, uint8_t kheld){
     _currentKDown = kdown;
     _currentKHeld = kheld;
diff --git a/source/Input.h b/source/Input.h
index bf0f370..a41600c 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -9,8 +9,8 @@ class Input{
 	uint16_t _framesHeld[8];

     public:
+    Input();
     void SetState(uint8_t kdown, uint8_t kheld);
-

 	uint8_t btn();
     uint8_t btnp();
diff --git a/source/PicoRam.h b/source/PicoRam.h
index 1f11cd1..5687cfc 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -1,5 +1,6 @@
 #pragma once

+#include <cstring>
 #include <string>

 /*
@@ -173,6 +174,10 @@ struct hwState_t {

 struct PicoRam
 {
+    void Reset() {
+        memset(data, 0, 0x8000);
+    }
+
     union
     {
         struct
@@ -197,34 +202,6 @@ struct PicoRam

             hwState_t hwState;

-            //TODO use draw state struct here
-            /*
-            uint8_t _gfxState_color;
-            uint8_t _gfxState_bgColor;
-
-            int _gfxState_text_x;
-            int _gfxState_text_y;
-
-            int _gfxState_camera_x;
-            int _gfxState_camera_y;
-
-            int _gfxState_clip_xb;
-            int _gfxState_clip_yb;
-            int _gfxState_clip_xe;
-            int _gfxState_clip_ye;
-
-            uint8_t _gfxState_drawPaletteMap[16];
-            uint8_t _gfxState_screenPaletteMap[16];
-            bool _gfxState_transparencyPalette[16];
-
-            //not actually part of graphics state memory?
-            int _gfxState_line_x;
-            int _gfxState_line_y;
-            bool _gfxState_line_valid;
-            */
-
-
-
             uint8_t screenBuffer[128 * 64];
         };

diff --git a/source/vm.cpp b/source/vm.cpp
index d95639a..0f592fe 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -21,22 +21,45 @@ extern "C" {
   #include <lauxlib.h>
 }

-Vm::Vm(){
-    Logger::Write("getting font string\n");
-    auto fontdata = get_font_data();
-
-    _memory = {0};
+Vm::Vm(
+    PicoRam* memory,
+    Graphics* graphics,
+    Input* input,
+    Audio* audio) :
+        _loadedCart(nullptr),
+        _luaState(nullptr),
+        _cleanupDeps(false),
+        _targetFps(30),
+        _picoFrameCount(0),
+        _hasUpdate(false),
+        _hasDraw(false),
+        _cartChangeQueued(false),
+        _nextCartKey(""),
+        _cartLoadError(""),
+        _cartdataKey("")
+{
+    if (memory == nullptr) {
+        memory = new PicoRam();
+        _cleanupDeps = true;
+    }
+    _memory = memory;

-
-    Logger::Write("Creating Graphics object\n");
-    Graphics* graphics = new Graphics(fontdata, &_memory);
+    if (graphics == nullptr) {
+        graphics = new Graphics(get_font_data(), _memory);
+        _cleanupDeps = true;
+    }
     _graphics = graphics;

-    Logger::Write("Creating Input object\n");
-    Input* input = new Input();
+    if (input == nullptr){
+        input = new Input();
+        _cleanupDeps = true;
+    }
     _input = input;

-    Audio* audio = new Audio(&_memory);
+    if (audio == nullptr) {
+        audio = new Audio(_memory);
+        _cleanupDeps = true;
+    }
     _audio = audio;

     //this can probably go away when I'm loading actual carts and just have to expose api to lua
@@ -44,26 +67,38 @@ Vm::Vm(){
     initPicoApi(_graphics, _input, this, _audio);
     //initGlobalApi(_graphics);

-    _targetFps = 30;
 }

 Vm::~Vm(){
     CloseCart();

-    delete _graphics;
-    delete _input;
-    delete _audio;
+    if (_cleanupDeps){
+        if (_input != nullptr) {
+            delete _input;
+        }
+        if (_graphics != nullptr) {
+            delete _graphics;
+        }
+        if (_audio != nullptr) {
+            delete _audio;
+        }
+        if (_memory != nullptr) {
+            delete _memory;
+        }
+    }
 }

 PicoRam* Vm::getPicoRam(){
-    return &_memory;
+    return _memory;
 }

 bool Vm::loadCart(Cart* cart) {
     _picoFrameCount = 0;
+    _cartdataKey = "";

     //reset memory (may have to be more selective about zeroing out to be accurate?)
-    _memory = {0};
+    _memory->Reset();
+
     //set graphics state
     _graphics->color(7);
     _graphics->clip();
@@ -82,22 +117,22 @@ bool Vm::loadCart(Cart* cart) {
     _audio->resetAudioState();

     //copy data from cart rom to ram
-    for(size_t i = 0; i < sizeof(_memory.spriteSheetData); i++) {
-        _memory.spriteSheetData[i] = cart->SpriteSheetData[i];
+    for(size_t i = 0; i < sizeof(_memory->spriteSheetData); i++) {
+        _memory->spriteSheetData[i] = cart->SpriteSheetData[i];
     }
-    for(size_t i = 0; i < sizeof(_memory.spriteFlags); i++) {
-        _memory.spriteFlags[i] = cart->SpriteFlagsData[i];
+    for(size_t i = 0; i < sizeof(_memory->spriteFlags); i++) {
+        _memory->spriteFlags[i] = cart->SpriteFlagsData[i];
     }
-    for(size_t i = 0; i < sizeof(_memory.mapData); i++) {
-        _memory.mapData[i] = cart->MapData[i];
+    for(size_t i = 0; i < sizeof(_memory->mapData); i++) {
+        _memory->mapData[i] = cart->MapData[i];
     }

     for(size_t i = 0; i < 64; i++) {
-        _memory.sfx[i] = cart->SfxData[i];
+        _memory->sfx[i] = cart->SfxData[i];
     }

     for(size_t i = 0; i < 64; i++) {
-        _memory.songs[i] = cart->SongData[i];
+        _memory->songs[i] = cart->SongData[i];
     }

     // initialize Lua interpreter
@@ -404,7 +439,7 @@ uint8_t Vm::ram_peek(int addr){
         return 0;
     }

-    return _memory.data[addr];
+    return _memory->data[addr];
 }

 int16_t Vm::ram_peek2(int addr){
@@ -414,9 +449,9 @@ int16_t Vm::ram_peek2(int addr){
     {
         /* This code handles partial reads by adding zeroes */
         if (addr + i < 0x8000)
-            bits |= _memory.data[addr + i] << (8 * i);
+            bits |= _memory->data[addr + i] << (8 * i);
         else if (addr + i >= 0x8000)
-            bits |= _memory.data[addr + i - 0x8000] << (8 * i);
+            bits |= _memory->data[addr + i - 0x8000] << (8 * i);
     }

     return bits;
@@ -430,9 +465,9 @@ int32_t Vm::ram_peek4(int addr){
     {
         /* This code handles partial reads by adding zeroes */
         if (addr + i < 0x8000)
-            bits |= _memory.data[addr + i] << (8 * i);
+            bits |= _memory->data[addr + i] << (8 * i);
         else if (addr + i >= 0x8000)
-            bits |= _memory.data[addr + i - 0x8000] << (8 * i);
+            bits |= _memory->data[addr + i - 0x8000] << (8 * i);
     }

     return bits;
@@ -444,7 +479,7 @@ void Vm::ram_poke(int addr, uint8_t value){
         return;
     }

-    _memory.data[addr] = value;
+    _memory->data[addr] = value;
 }

 void Vm::ram_poke2(int addr, int16_t value){
@@ -452,8 +487,8 @@ void Vm::ram_poke2(int addr, int16_t value){
         return;
     }

-    _memory.data[addr] = (uint8_t)value;
-    _memory.data[addr + 1] = (uint8_t)((uint16_t)value >> 8);
+    _memory->data[addr] = (uint8_t)value;
+    _memory->data[addr + 1] = (uint8_t)(value >> 8);

 }

@@ -462,8 +497,25 @@ void Vm::ram_poke4(int addr, int32_t value){
         return;
     }

-    _memory.data[addr + 0] = (uint8_t)value;
-    _memory.data[addr + 1] = (uint8_t)(value >> 8);
-    _memory.data[addr + 2] = (uint8_t)(value >> 16);
-    _memory.data[addr + 3] = (uint8_t)(value >> 24);
+    _memory->data[addr + 0] = (uint8_t)value;
+    _memory->data[addr + 1] = (uint8_t)(value >> 8);
+    _memory->data[addr + 2] = (uint8_t)(value >> 16);
+    _memory->data[addr + 3] = (uint8_t)(value >> 24);
+}
+
+void Vm::vm_cartdata(string key) {
+    _cartdataKey = key;
+}
+
+int32_t Vm::vm_dget(uint8_t n) {
+    if (_cartdataKey.length() > 0 && n < 64) {
+        return ram_peek4(0x5e00 + 4 * n);
+    }
+
+    return 0;
+}
+void Vm::vm_dset(uint8_t n, int32_t value){
+    if (_cartdataKey.length() > 0 && n < 64) {
+        ram_poke4(0x5e00 + 4 * n, value);
+    }
 }
diff --git a/source/vm.h b/source/vm.h
index c681dd4..23fb6fb 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -15,14 +15,17 @@ extern "C" {
 }

 class Vm {
-    PicoRam _memory;
+    PicoRam* _memory;

-    Cart* _loadedCart;
     Graphics* _graphics;
     Audio* _audio;
-    lua_State* _luaState;
     Input* _input;

+    Cart* _loadedCart;
+    lua_State* _luaState;
+
+    bool _cleanupDeps;
+
     int _targetFps;

     int _picoFrameCount;
@@ -34,12 +37,17 @@ class Vm {

     string _cartLoadError;

+    string _cartdataKey;
+
     vector<string> _cartList;

     bool loadCart(Cart* cart);

     public:
-    Vm();
+    Vm(PicoRam* memory = nullptr,
+       Graphics* graphics = nullptr,
+       Input* input = nullptr,
+       Audio* audio = nullptr);
     ~Vm();

     void LoadBiosCart();
@@ -79,5 +87,9 @@ class Vm {
     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_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
 };

diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 2771f1f..89d6db5 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -7,7 +7,6 @@
 #include "../source/vm.h"
 #include "../source/hostVmShared.h"

-
 bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
     std::vector<unsigned char> png;
     std::vector<unsigned char> image; //the raw pixels
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 2efbea5..4d37a90 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -6,14 +6,26 @@
 #include "../source/vm.h"
 #include "../source/hostVmShared.h"

+#include "../source/fontdata.h"
+

 TEST_CASE("Vm memory functions") {
-    Vm* vm = new Vm();
-    PicoRam* memory = vm->getPicoRam();
+    PicoRam* memory = new PicoRam();
+    Graphics* graphics = new Graphics(get_font_data(), memory);
+    Input* input = new Input();
+    Audio* audio = new Audio(memory);
+
+    Vm* vm = new Vm(memory, graphics, input, audio);

     SUBCASE("memory data stats with 0"){
         CHECK(memory->data[0] == 0);
     }
+    SUBCASE("size of PicoRam struct corrct"){
+        CHECK(sizeof(PicoRam) == 0x8000);
+    }
+    SUBCASE("size of data correct"){
+        CHECK(sizeof(memory->data) == 0x8000);
+    }
     SUBCASE("simple peek and poke"){
         vm->ram_poke(2, 232);

@@ -29,7 +41,70 @@ TEST_CASE("Vm memory functions") {

         CHECK(vm->ram_peek4(0x7123) == 49249);
     }
+    SUBCASE("poking spritesheet"){
+        //147: 1001 0011
+        //left pixel: 0011 = 3
+        //right pixel: 1001 = 9
+        vm->ram_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);
+
+        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);
+
+        CHECK_EQ(graphics->fget(0), 39);
+        CHECK_EQ(graphics->fget(0, 0), true);
+        CHECK_EQ(graphics->fget(0, 3), false);
+    }
+    SUBCASE("poking music"){
+        //78: 0100 1110
+        vm->ram_poke(0x3100, 78);
+
+        CHECK_EQ(memory->songs[0].sfx0, 78);
+        CHECK_EQ(memory->songs[0].start, 0);
+    }
+    SUBCASE("poking sfx"){
+        //97: 0110 0001
+        //key is first 6 bits: 10 0001 : 33
+        //waveform is next 3: (0) 01 : 1
+        vm->ram_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);
+
+        CHECK_EQ(memory->generalUseRam[0], 215);
+    }
+    SUBCASE("setting cart data"){
+        vm->vm_cartdata("dummy");
+        vm->vm_dset(13, 56);
+
+        CHECK_EQ(vm->vm_dget(13), 56);
+    }
+    SUBCASE("poking cart data"){
+        vm->ram_poke(0x5e00, 56);
+        vm->vm_cartdata("dummy");
+
+        CHECK_EQ(memory->cartData[0], 56);
+        CHECK_EQ(vm->vm_dget(0), 56);
+    }

+    delete graphics;
+    delete input;
+    delete audio;

     delete vm;
+
+    delete memory;
 }
\ No newline at end of file