commit eec6bf343d4abb0ed5271ac3f5e554aee159ea08
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Thu Sep 17 20:18:05 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Sep 17 20:18:05 2020 -0600
Add peek and poke methods to vm
---
source/vm.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
source/vm.h | 10 ++++++++
test/Makefile | 8 +++++-
test/vmtests.cpp | 35 +++++++++++++++++++++++++++
4 files changed, 126 insertions(+), 1 deletion(-)
diff --git a/source/vm.cpp b/source/vm.cpp
index 6300958..d95639a 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -55,6 +55,10 @@ Vm::~Vm(){
delete _audio;
}
+PicoRam* Vm::getPicoRam(){
+ return &_memory;
+}
+
bool Vm::loadCart(Cart* cart) {
_picoFrameCount = 0;
@@ -393,3 +397,73 @@ bool Vm::ExecuteLua(string luaString, string callbackFunction){
return true;
}
+
+
+uint8_t Vm::ram_peek(int addr){
+ if (addr < 0 || addr > 0x8000){
+ return 0;
+ }
+
+ return _memory.data[addr];
+}
+
+int16_t Vm::ram_peek2(int addr){
+ //zepto8
+ int16_t bits = 0;
+ for (int i = 0; i < 2; ++i)
+ {
+ /* This code handles partial reads by adding zeroes */
+ if (addr + i < 0x8000)
+ bits |= _memory.data[addr + i] << (8 * i);
+ else if (addr + i >= 0x8000)
+ bits |= _memory.data[addr + i - 0x8000] << (8 * i);
+ }
+
+ return bits;
+}
+
+//note: this should return a 32 bit fixed point number
+int32_t Vm::ram_peek4(int addr){
+ //zepto8
+ int32_t bits = 0;
+ for (int i = 0; i < 4; ++i)
+ {
+ /* This code handles partial reads by adding zeroes */
+ if (addr + i < 0x8000)
+ bits |= _memory.data[addr + i] << (8 * i);
+ else if (addr + i >= 0x8000)
+ bits |= _memory.data[addr + i - 0x8000] << (8 * i);
+ }
+
+ return bits;
+}
+
+void Vm::ram_poke(int addr, uint8_t value){
+ //todo: check how pico 8 handles out of bounds
+ if (addr < 0 || addr > 0x8000){
+ return;
+ }
+
+ _memory.data[addr] = value;
+}
+
+void Vm::ram_poke2(int addr, int16_t value){
+ if (addr < 0 || addr > 0x8000 - 1){
+ return;
+ }
+
+ _memory.data[addr] = (uint8_t)value;
+ _memory.data[addr + 1] = (uint8_t)((uint16_t)value >> 8);
+
+}
+
+void Vm::ram_poke4(int addr, int32_t value){
+ if (addr < 0 || addr > 0x8000 - 3){
+ 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);
+}
diff --git a/source/vm.h b/source/vm.h
index eef4544..c681dd4 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -69,5 +69,15 @@ class Vm {
string GetBiosError();
bool ExecuteLua(string luaString, string callbackFunction);
+
+ 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
+
+ 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
};
diff --git a/test/Makefile b/test/Makefile
index 2751234..8e3b983 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -8,7 +8,7 @@ all: runtests
testrunner.a: testroot.o \
fontdatatests.o graphicstests.o audiotests.o cartpatchertests.o \
- carttests.o endtoendtests.o
+ carttests.o endtoendtests.o vmtests.o
g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
testroot.o \
fontdata.o fontdatatests.o \
@@ -17,6 +17,7 @@ testrunner.a: testroot.o \
cartPatcher.o cartpatchertests.o \
cart.o carttests.o nooplogger.o lodepng.o emojiconversion.o filehelpers.o utf8-util.o \
vm.o endtoendtests.o picoluaapi.o Input.o \
+ vmtests.o \
-llua -ldl \
-o testrunner.a
@@ -72,6 +73,11 @@ endtoendtests.o: cartpatchertests.o carttests.o graphicstests.o audiotests.o \
../source/picoluaapi.cpp ../source/picoluaapi.h
g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
endtoendtests.cpp ../source/vm.cpp ../source/picoluaapi.cpp ../source/Input.cpp
+
+vmtests.o: endtoendtests.o \
+ vmtests.cpp
+ g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
+ vmtests.cpp ../source/vm.cpp ../source/picoluaapi.cpp ../source/Input.cpp
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
new file mode 100644
index 0000000..2efbea5
--- /dev/null
+++ b/test/vmtests.cpp
@@ -0,0 +1,35 @@
+#include <string>
+#include <vector>
+
+#include "doctest.h"
+
+#include "../source/vm.h"
+#include "../source/hostVmShared.h"
+
+
+TEST_CASE("Vm memory functions") {
+ Vm* vm = new Vm();
+ PicoRam* memory = vm->getPicoRam();
+
+ SUBCASE("memory data stats with 0"){
+ CHECK(memory->data[0] == 0);
+ }
+ SUBCASE("simple peek and poke"){
+ vm->ram_poke(2, 232);
+
+ CHECK(vm->ram_peek(2) == 232);
+ }
+ SUBCASE("simple peek2 and poke2"){
+ vm->ram_poke2(41, -1031);
+
+ CHECK(vm->ram_peek2(41) == -1031);
+ }
+ SUBCASE("simple peek4 and poke4"){
+ vm->ram_poke4(0x7123, 49249);
+
+ CHECK(vm->ram_peek4(0x7123) == 49249);
+ }
+
+
+ delete vm;
+}
\ No newline at end of file