commit 69f8bed0e15e5843b0a74e6bb219cba5951a4071
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Sep 21 18:29:58 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Mon Sep 21 18:29:58 2020 -0600
Implement reload, use in initial cart loading
---
source/vm.cpp | 60 ++++++++++++++++++++++++++++++++++++++----------------
source/vm.h | 3 +++
test/carttests.cpp | 1 -
test/vmtests.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 100 insertions(+), 18 deletions(-)
diff --git a/source/vm.cpp b/source/vm.cpp
index 275eb05..5554d28 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -117,23 +117,7 @@ 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->CartRom.SpriteSheetData[i];
- }
- for(size_t i = 0; i < sizeof(_memory->spriteFlags); i++) {
- _memory->spriteFlags[i] = cart->CartRom.SpriteFlagsData[i];
- }
- for(size_t i = 0; i < sizeof(_memory->mapData); i++) {
- _memory->mapData[i] = cart->CartRom.MapData[i];
- }
-
- for(size_t i = 0; i < 64; i++) {
- _memory->sfx[i] = cart->CartRom.SfxData[i];
- }
-
- for(size_t i = 0; i < 64; i++) {
- _memory->songs[i] = cart->CartRom.SongData[i];
- }
+ vm_reload(0, 0, sizeof(cart->CartRom), cart);
// initialize Lua interpreter
_luaState = luaL_newstate();
@@ -514,8 +498,50 @@ int32_t Vm::vm_dget(uint8_t 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);
}
}
+
+void Vm::vm_reload(int destaddr, int sourceaddr, int len, Cart* cart){
+ memcpy(&_memory->data[destaddr], &cart->CartRom.data[sourceaddr], sizeof(cart->CartRom));
+}
+
+void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
+ if (destaddr < 0 || destaddr > 0x8000) {
+ //invalid dest address
+ return;
+ }
+ if (sourceaddr < 0 || sourceaddr > 0x4300) {
+ //invalid source address
+ return;
+ }
+ if (len < 0 || destaddr + len > 0x4300) {
+ //invalid length address
+ return;
+ }
+
+ Cart* cart = _loadedCart;
+ bool multicart = false;
+
+ if (filename.length() > 0) {
+ cart = new Cart(filename);
+ if (cart->LoadError.length() > 0) {
+ //error, can't load cart
+ //todo: see what kind of error pico 8 throws, emulate
+ delete cart;
+
+ return;
+ }
+
+ multicart = true;
+ }
+
+ vm_reload(destaddr, sourceaddr, len, cart);
+
+ if (multicart) {
+ delete cart;
+ }
+}
diff --git a/source/vm.h b/source/vm.h
index 23fb6fb..7dff467 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -42,6 +42,7 @@ class Vm {
vector<string> _cartList;
bool loadCart(Cart* cart);
+ void vm_reload(int destaddr, int sourceaddr, int len, Cart* cart);
public:
Vm(PicoRam* memory = nullptr,
@@ -91,5 +92,7 @@ class Vm {
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);
};
diff --git a/test/carttests.cpp b/test/carttests.cpp
index a909230..6f82893 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -155,7 +155,6 @@ TEST_CASE("Load simple png cart") {
CHECK(cart->CartRom.SongData[0].stop == 0);
CHECK(cart->CartRom.SongData[0].sfx3 == 68);
CHECK(cart->CartRom.SongData[0].mode == 0);
-
}
delete cart;
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index a9b9edd..0c83768 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -306,7 +306,61 @@ TEST_CASE("Vm memory functions") {
CHECK_EQ(graphics->pget(126, 127), 2);
CHECK_EQ(graphics->pget(127, 127), 13);
}
+ SUBCASE("Loading cart copies cart rom to memory") {
+ vm->LoadCart("carts/cartparsetest.p8.png");
+
+ SUBCASE("Gfx data is populated") {
+ CHECK(memory->spriteSheetData[0] == 255);
+ CHECK(memory->spriteSheetData[1] == 1);
+ CHECK(memory->spriteSheetData[2] == 2);
+ CHECK(memory->spriteSheetData[3] == 3);
+ }
+ SUBCASE("Sprite flags is populated") {
+ CHECK(memory->spriteFlags[0] == 0);
+ CHECK(memory->spriteFlags[1] == 1);
+ }
+ SUBCASE("Map data is populated") {
+ CHECK(memory->mapData[0] == 1);
+ CHECK(memory->mapData[1] == 0);
+ }
+ SUBCASE("Sfx data is populated") {
+ CHECK(memory->sfx[0].data[0] == 13);
+ CHECK(memory->sfx[0].editorMode == 0);
+ CHECK(memory->sfx[0].loopRangeEnd == 0);
+ CHECK(memory->sfx[0].loopRangeEnd == 0);
+ CHECK(memory->sfx[0].speed == 29);
+
+ CHECK(memory->sfx[0].notes[0].waveform == 0);
+ CHECK(memory->sfx[0].notes[0].effect == 0);
+ CHECK(memory->sfx[0].notes[0].key == 13);
+ CHECK(memory->sfx[0].notes[0].volume == 3);
+ CHECK(memory->sfx[0].notes[0].data[0] == 13);
+ CHECK(memory->sfx[0].notes[0].data[1] == 6);
+ }
+ SUBCASE("Music data is populated") {
+ CHECK(memory->songs[0].sfx0 == 1);
+ CHECK(memory->songs[0].start == 0);
+ CHECK(memory->songs[0].sfx1 == 66);
+ CHECK(memory->songs[0].loop == 0);
+ CHECK(memory->songs[0].sfx2 == 67);
+ CHECK(memory->songs[0].stop == 0);
+ CHECK(memory->songs[0].sfx3 == 68);
+ CHECK(memory->songs[0].mode == 0);
+ }
+ }
+ SUBCASE("reload writes cart rom over changes") {
+ vm->LoadCart("carts/cartparsetest.p8.png");
+
+ graphics->sset(0, 0, 4);
+ graphics->fset(0, 3);
+ graphics->mset(0, 0, 10);
+ vm->vm_reload(0, 0, 0x4300, "");
+
+ CHECK_EQ(graphics->sget(0, 0), 15);
+ CHECK_EQ(graphics->fget(0), 0);
+ CHECK_EQ(graphics->mget(0, 0), 1);
+ }
delete graphics;
delete input;