commit a31451273f1cd9e938a6cbdda1dd3d81cadc9e81
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Sep 19 19:30:31 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Sep 19 19:30:31 2020 -0600
Use screen fb in pico memory
Also use memory for btn delay params and add more test coverage to
memory
---
platform/3ds/source/3dsHost.cpp | 9 ++--
platform/switch/source/SwitchHost.cpp | 3 +-
source/Input.cpp | 31 ++++++++++----
source/Input.h | 5 ++-
source/graphics.cpp | 44 ++++++-------------
source/graphics.h | 1 -
source/nibblehelpers.cpp | 30 +++++++++++++
source/nibblehelpers.h | 9 ++++
source/vm.cpp | 2 +-
test/Makefile | 8 ++--
test/endtoendtests.cpp | 7 ++--
test/graphicstests.cpp | 4 +-
test/vmtests.cpp | 79 ++++++++++++++++++++++++++++++++++-
13 files changed, 173 insertions(+), 59 deletions(-)
diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index f0de869..348acc0 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -14,6 +14,7 @@ namespace fs = std::filesystem;
#include "../../../source/host.h"
#include "../../../source/hostVmShared.h"
+#include "../../../source/nibblehelpers.h"
#define SCREEN_WIDTH 400;
#define SCREEN_HEIGHT 240;
@@ -269,7 +270,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
//todo: test if it is faster to convert colors to uint24_ts and write one instead of 3 (assuming these are )
for(x = 0; x < 128; x++) {
for(y = 0; y < 128; y++) {
- uint8_t c = picoFb[x*128 + y];
+ uint8_t c = getPixelNibble(x, y, picoFb);
Color col = paletteColors[screenPaletteMap[c]];
int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
@@ -291,7 +292,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
for(y = 0; y < __3ds_TopScreenHeight; y++) {
int picoX = (int)(x / ratio);
int picoY = (int)(y / ratio);
- uint8_t c = picoFb[picoX*128 + picoY];
+ uint8_t c = getPixelNibble(picoX, picoY, picoFb);
Color col = paletteColors[screenPaletteMap[c]];
int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
@@ -315,7 +316,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
for(y = 0; y < __3ds_TopScreenHeight; y++) {
int picoX = (int)(x / ratio);
int picoY = (int)(y / ratio);
- uint8_t c = picoFb[picoX*128 + picoY];
+ uint8_t c = getPixelNibble(picoX, picoY, picoFb);
Color col = paletteColors[screenPaletteMap[c]];
int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
@@ -335,7 +336,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
for(y = 0; y < overflowHeight; y++) {
int picoX = (int)(x / ratio);
int picoY = (int)((y + __3ds_TopScreenHeight) / ratio);
- uint8_t c = picoFb[picoX*128 + picoY];
+ uint8_t c = getPixelNibble(picoX, picoY, picoFb);
Color col = paletteColors[screenPaletteMap[c]];
int pixIdx = (((x + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y + yOffset)))*3;
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index 06d0d90..6ca09fd 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -12,6 +12,7 @@ namespace fs = std::filesystem;
#include "../../../source/host.h"
#include "../../../source/hostVmShared.h"
+#include "../../../source/nibblehelpers.h"
#define FB_WIDTH 1280
#define FB_HEIGHT 720
@@ -274,7 +275,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
{
int picoX = (int)(x / ratio);
int picoY = (int)(y / ratio);
- uint8_t c = picoFb[picoX*128 + picoY];
+ uint8_t c = getPixelNibble(picoX, picoY, picoFb);
//uint8_t c = picoFb[x*128 + y];
Color col = paletteColors[screenPaletteMap[c]];
diff --git a/source/Input.cpp b/source/Input.cpp
index 471d1a7..106634f 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -2,21 +2,34 @@
#include "Input.h"
#include "hostVmShared.h"
+#include "PicoRam.h"
-Input::Input():
- _currentKDown(0),
- _currentKHeld(0)
+Input::Input(PicoRam* memory):
+ _currentKDown(0)
{
+ _memory = memory;
+
std::fill(_framesHeld, _framesHeld + 8, 0);
}
void Input::SetState(uint8_t kdown, uint8_t kheld){
_currentKDown = kdown;
- _currentKHeld = kheld;
+ _memory->hwState.buttonStates[0] = kheld;
+
+ uint8_t repeatDelay = _memory->hwState.btnpRepeatDelay == 0
+ ? 15
+ : _memory->hwState.btnpRepeatDelay;
+
+ if (repeatDelay == 255){
+ return;
+ }
+
+ uint8_t repeatInterval = _memory->hwState.btnpRepeatInterval == 0
+ ? 4
+ : _memory->hwState.btnpRepeatDelay;
- //TODO: this can be overridden by poking certain memory address
for (int i = 0; i < 8; i ++) {
- bool down = BITMASK(i) & _currentKHeld;
+ bool down = BITMASK(i) & kheld;
_framesHeld[i] = down ? _framesHeld[i] + 1 : 0;
@@ -28,8 +41,8 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
//player releases the button.
bool repeatPressed =
- (_framesHeld[i] == 15) ||
- (_framesHeld[i] / 15 >= 1 && _framesHeld[i] % 4 == 0);
+ (_framesHeld[i] == repeatDelay) ||
+ (_framesHeld[i] / repeatDelay >= 1 && _framesHeld[i] % repeatInterval == 0);
if (repeatPressed) {
_currentKDown = _currentKDown | BITMASK(i);
@@ -38,7 +51,7 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
}
uint8_t Input::btn(){
- return _currentKHeld;
+ return _memory->hwState.buttonStates[0];
}
//todo: repetition behavior to match pico 8
diff --git a/source/Input.h b/source/Input.h
index a41600c..84789ae 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -1,15 +1,16 @@
#pragma once
#include <stdint.h>
+#include "PicoRam.h"
class Input{
+ PicoRam* _memory;
uint8_t _currentKDown;
- uint8_t _currentKHeld;
uint16_t _framesHeld[8];
public:
- Input();
+ Input(PicoRam* memory);
void SetState(uint8_t kdown, uint8_t kheld);
uint8_t btn();
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 74a48da..7027985 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -7,6 +7,7 @@
#include "graphics.h"
#include "hostVmShared.h"
+#include "nibblehelpers.h"
#include "stringToDataHelpers.h"
@@ -50,7 +51,7 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {
uint8_t* Graphics::GetP8FrameBuffer(){
//TODO: replace with ram's screen buffer
- return this->_pico8_fb;
+ return _memory->screenBuffer;
}
uint8_t* Graphics::GetScreenPaletteMap(){
@@ -358,7 +359,7 @@ int Graphics::clampYCoordToCLip(int y) {
void Graphics::_private_safe_pset(int x, int y, uint8_t col) {
if (isWithinClip(x, y)){
- _pico8_fb[(x * 128) + y] = getDrawPalMappedColor(col);
+ _private_pset(x, y, col);
}
}
@@ -366,7 +367,9 @@ void Graphics::_private_pset(int x, int y, uint8_t col) {
x = x & 127;
y = y & 127;
- _pico8_fb[(x * 128) + y] = getDrawPalMappedColor(col);
+ col = getDrawPalMappedColor(col);
+
+ setPixelNibble(x, y, col, _memory->screenBuffer);
}
//end helper methods
@@ -375,7 +378,9 @@ void Graphics::cls() {
}
void Graphics::cls(uint8_t color) {
- memset(_pico8_fb, color, sizeof(_pico8_fb));
+ color = color & 15;
+ uint8_t val = color << 4 | color;
+ memset(_memory->screenBuffer, val, sizeof(_memory->screenBuffer));
_memory->drawState.text_x = 0;
_memory->drawState.text_y = 0;
@@ -399,7 +404,7 @@ uint8_t Graphics::pget(int x, int y){
applyCameraToPoint(&x, &y);
if (isOnScreen(x, y)){
- return _pico8_fb[(x * 128) + y];
+ return getPixelNibble(x, y, _memory->screenBuffer);
}
return 0;
@@ -449,10 +454,6 @@ void Graphics::_private_h_line (int x1, int x2, int y, uint8_t col){
int maxx = clampXCoordToClip(std::max(x1, x2));
int minx = clampXCoordToClip(std::min(x1, x2));
-
- //possible todo: check if memset is any better here? this seems to be wrong
- //uint8_t* fb_line = _pico8_fb + y * PicoScreenWidth;
- //memset(fb_line + minx, col, maxx - minx);
for (int x = minx; x <= maxx; x++){
_private_pset(x, y, col);
}
@@ -730,32 +731,11 @@ void Graphics::fset(uint8_t n, uint8_t v){
}
uint8_t Graphics::sget(uint8_t x, uint8_t y){
- int combinedIdx = y * 64 + (x / 2);
-
- uint8_t combinedPix = _memory->spriteSheetData[combinedIdx];
-
- uint8_t c = x % 2 == 0
- ? combinedPix & 0x0f //just first 4 bits
- : combinedPix >> 4; //just last 4 bits
-
- return c;
+ return getPixelNibble(x, y, _memory->spriteSheetData);
}
void Graphics::sset(uint8_t x, uint8_t y, uint8_t c){
- int combinedIdx = y * 64 + (x / 2);
-
- uint8_t currentByte = _memory->spriteSheetData[combinedIdx];
- uint8_t mask;
- // set just 4 bits: https://stackoverflow.com/a/4439221
- if (x % 2 == 0) {
- mask = 0x0f;
- }
- else {
- c = c << 4;
- mask = 0xf0;
- }
-
- _memory->spriteSheetData[combinedIdx] = (currentByte & ~mask) | (c & mask);
+ setPixelNibble(x, y, c, _memory->spriteSheetData);
}
void Graphics::camera() {
diff --git a/source/graphics.h b/source/graphics.h
index 48e3cb8..431e276 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -25,7 +25,6 @@
class Graphics {
- uint8_t _pico8_fb[128*128];
uint8_t fontSpriteData[128 * 64];
Color _paletteColors[16];
diff --git a/source/nibblehelpers.cpp b/source/nibblehelpers.cpp
new file mode 100644
index 0000000..44718a8
--- /dev/null
+++ b/source/nibblehelpers.cpp
@@ -0,0 +1,30 @@
+
+#include <string>
+
+#include "nibblehelpers.h"
+
+int getCombinedIdx(int x, int y){
+ return y * 64 + (x / 2);
+}
+
+void setPixelNibble(int x, int y, uint8_t value, uint8_t* targetBuffer) {
+ int combinedIdx = getCombinedIdx(x, y);
+
+ uint8_t currentByte = targetBuffer[combinedIdx];
+ uint8_t mask = x % 2 == 0 ? 0x0f : 0xf0;
+ value = mask == 0xf0 ? value << 4 : value;
+
+ targetBuffer[combinedIdx] = (currentByte & ~mask) | (value & mask);
+}
+
+uint8_t getPixelNibble(int x, int y, uint8_t* targetBuffer) {
+ int combinedIdx = getCombinedIdx(x, y);
+
+ uint8_t combinedPix = targetBuffer[combinedIdx];
+
+ uint8_t c = x % 2 == 0
+ ? combinedPix & 0x0f //just first 4 bits
+ : combinedPix >> 4; //just last 4 bits
+
+ return c;
+}
\ No newline at end of file
diff --git a/source/nibblehelpers.h b/source/nibblehelpers.h
new file mode 100644
index 0000000..012ebc4
--- /dev/null
+++ b/source/nibblehelpers.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <string>
+
+int getCombinedIdx(int x, int y);
+
+void setPixelNibble(int x, int y, uint8_t value, uint8_t* targetBuffer);
+
+uint8_t getPixelNibble(int x, int y, uint8_t* targetBuffer);
diff --git a/source/vm.cpp b/source/vm.cpp
index 0f592fe..1a09b3c 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -51,7 +51,7 @@ Vm::Vm(
_graphics = graphics;
if (input == nullptr){
- input = new Input();
+ input = new Input(memory);
_cleanupDeps = true;
}
_input = input;
diff --git a/test/Makefile b/test/Makefile
index 8e3b983..cc72cea 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -12,7 +12,7 @@ testrunner.a: testroot.o \
g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
testroot.o \
fontdata.o fontdatatests.o \
- graphics.o graphicstests.o stringToDataHelpers.o \
+ graphics.o graphicstests.o stringToDataHelpers.o nibblehelpers.o \
Audio.o synth.o audiotests.o \
cartPatcher.o cartpatchertests.o \
cart.o carttests.o nooplogger.o lodepng.o emojiconversion.o filehelpers.o utf8-util.o \
@@ -30,8 +30,10 @@ fontdatatests.o: fontdatatests.cpp ../source/fontdata.cpp ../source/fontdata.h
graphicstests.o: fontdatatests.o graphicstests.cpp ../source/graphics.cpp ../source/graphics.h \
../source/fontdata.cpp ../source/fontdata.h \
../source/PicoRam.h ../source/hostVmShared.h \
- ../source/stringToDataHelpers.h ../source/stringToDataHelpers.cpp
- g++ $(CFLAGS) graphicstests.cpp ../source/graphics.cpp ../source/stringToDataHelpers.cpp
+ ../source/stringToDataHelpers.h ../source/stringToDataHelpers.cpp \
+ ../source/nibblehelpers.h ../source/nibblehelpers.cpp
+ g++ $(CFLAGS) graphicstests.cpp ../source/graphics.cpp \
+ ../source/stringToDataHelpers.cpp ../source/nibblehelpers.cpp
audiotests.o: audiotests.cpp ../source/Audio.cpp ../source/Audio.h \
../source/synth.cpp ../source/synth.h \
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 89d6db5..17c56e0 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -6,6 +6,7 @@
#include "../source/vm.h"
#include "../source/hostVmShared.h"
+#include "../source/nibblehelpers.h"
bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
std::vector<unsigned char> png;
@@ -41,16 +42,16 @@ bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
int pixIdx = i / 4;
int x = pixIdx % 128;
int y = pixIdx / 128;
- uint8_t c = picoFb[(x * 128) + y];
+ uint8_t c = getPixelNibble(x, y, picoFb);
Color col = paletteColors[screenPaletteMap[c]];
bool pixelMatches = r == col.Red && g == col.Green && b == col.Blue;
if (!pixelMatches) {
- printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);
+ //printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);
- printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
+ //printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
}
pixelsMatch &= pixelMatches;
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 2eb3b39..14dea62 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -120,7 +120,7 @@ TEST_CASE("graphics class behaves as expected") {
bool isAllColor = true;
for(int x = 0; x < 128; x++){
for(int y = 0; y < 128; y++){
- isAllColor &= graphics->pget(0, 0) == testColor;
+ isAllColor &= graphics->pget(x, y) == testColor;
}
}
CHECK(isAllColor);
@@ -130,7 +130,7 @@ TEST_CASE("graphics class behaves as expected") {
graphics->cls();
for(int x = 0; x < 128; x++){
for(int y = 0; y < 128; y++){
- isAllColor &= graphics->pget(0, 0) == 0;
+ isAllColor &= graphics->pget(x, y) == 0;
}
}
CHECK(isAllColor);
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 86cc749..a9b9edd 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -12,7 +12,7 @@
TEST_CASE("Vm memory functions") {
PicoRam* memory = new PicoRam();
Graphics* graphics = new Graphics(get_font_data(), memory);
- Input* input = new Input();
+ Input* input = new Input(memory);
Audio* audio = new Audio(memory);
Vm* vm = new Vm(memory, graphics, input, audio);
@@ -229,6 +229,83 @@ TEST_CASE("Vm memory functions") {
CHECK_EQ(memory->drawState.line_y, 262);
}
+ SUBCASE("poking audio hardware state") {
+ vm->ram_poke(0x5f40, 3);
+
+ CHECK_EQ(memory->hwState.audioHardwareState[0], 3);
+ }
+ SUBCASE("poking rng state") {
+ vm->ram_poke(0x5f44, 20);
+
+ CHECK_EQ(memory->hwState.rngState[0], 20);
+ }
+ SUBCASE("poking button state") {
+ //0000 1010
+ vm->ram_poke(0x5f4c, 10);
+
+ CHECK_EQ(memory->hwState.buttonStates[0], 10);
+ CHECK_EQ(input->btn(), 10);
+ CHECK_EQ(input->btn(0), false);
+ CHECK_EQ(input->btn(1), true);
+ CHECK_EQ(input->btn(2), false);
+ CHECK_EQ(input->btn(3), true);
+ }
+ SUBCASE("poking unknown input block") {
+ vm->ram_poke(0x5f54, 53);
+
+ CHECK_EQ(memory->hwState.unknownInputBlock[0], 53);
+ }
+ SUBCASE("poking btnp repeat delay") {
+ vm->ram_poke(0x5f5c, 10);
+
+ CHECK_EQ(memory->hwState.btnpRepeatDelay, 10);
+ }
+ SUBCASE("poking btnp repeat interval") {
+ vm->ram_poke(0x5f5d, 25);
+
+ CHECK_EQ(memory->hwState.btnpRepeatInterval, 25);
+ }
+ SUBCASE("poking color bitmask") {
+ vm->ram_poke(0x5f5e, 42);
+
+ CHECK_EQ(memory->hwState.colorBitmask, 42);
+ }
+ SUBCASE("poking alternate palette flag") {
+ vm->ram_poke(0x5f5f, 10);
+
+ CHECK_EQ(memory->hwState.alternatePaletteFlag, 10);
+ }
+ SUBCASE("poking alternate palette map") {
+ vm->ram_poke(0x5f60, 11);
+
+ CHECK_EQ(memory->hwState.alternatePaletteMap[0], 11);
+ }
+ SUBCASE("poking alternate palette map") {
+ vm->ram_poke(0x5f70, 7);
+
+ CHECK_EQ(memory->hwState.alternatePaletteScreenLineBitfield[0], 7);
+ }
+ SUBCASE("poking gpio pins") {
+ vm->ram_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);
+
+ CHECK_EQ(memory->screenBuffer[0], 195);
+ CHECK_EQ(graphics->pget(0, 0), 3);
+ CHECK_EQ(graphics->pget(1, 0), 12);
+ }
+ SUBCASE("poking screen data (last two pixels") {
+ //210: 1101 0010
+ vm->ram_poke(0x7fff, 210);
+
+ CHECK_EQ(memory->screenBuffer[8191], 210);
+ CHECK_EQ(graphics->pget(126, 127), 2);
+ CHECK_EQ(graphics->pget(127, 127), 13);
+ }
delete graphics;