commit 1278e5b1cea5c3a2363c041285ebe1528f0b36c1
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Sep 16 20:24:30 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Sep 16 20:24:30 2020 -0600
Get tests passing and memory refactor
---
source/Audio.cpp | 10 +++
source/Audio.h | 3 +
source/PicoRam.h | 14 ++--
source/graphics.cpp | 31 +++++----
source/vm.cpp | 11 +---
test/Makefile | 3 +-
test/audiotests.cpp | 17 ++---
test/graphicstests.cpp | 172 +++++++++++++++++++++++++------------------------
8 files changed, 136 insertions(+), 125 deletions(-)
diff --git a/source/Audio.cpp b/source/Audio.cpp
index f63d0e5..706697b 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -13,11 +13,21 @@
Audio::Audio(PicoRam* memory){
_memory = memory;
+
+ resetAudioState();
+}
+
+void Audio::resetAudioState() {
_audioState = {0};
for(int i = 0; i < 4; i++) {
_audioState._sfxChannels[i].sfxId = -1;
}
+ _audioState._musicChannel.pattern = -1;
+}
+
+audioState* Audio::getAudioState() {
+ return &_audioState;
}
void Audio::api_sfx(int sfx, int channel, int offset){
diff --git a/source/Audio.h b/source/Audio.h
index 78b6a0d..c6482b0 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -85,6 +85,9 @@ class Audio {
public:
Audio(PicoRam* memory);
+ void resetAudioState();
+ audioState* getAudioState();
+
void api_sfx(int sfx, int channel, int offset);
void api_music(int pattern, int16_t fade_len, int16_t mask);
diff --git a/source/PicoRam.h b/source/PicoRam.h
index 1d1bb3d..1f11cd1 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -96,7 +96,7 @@ struct audioState {
sfxChannel _sfxChannels[4];
};
-struct drawState {
+struct drawState_t {
uint8_t drawPaletteMap[16];
uint8_t screenPaletteMap[16];
@@ -112,8 +112,8 @@ struct drawState {
uint8_t text_x;
uint8_t text_y;
- uint8_t camera_x;
- uint8_t camera_y;
+ int16_t camera_x;
+ int16_t camera_y;
uint8_t drawMode;
@@ -144,7 +144,7 @@ struct drawState {
};
-struct hwState {
+struct hwState_t {
uint8_t audioHardwareState[4];
uint8_t rngState[8];
@@ -192,12 +192,10 @@ struct PicoRam
uint8_t generalUseRam[6912];
//0x5e00 0x5eff Persistent cart data (64 numbers = 256 bytes)
uint8_t cartData[256];
- musicChannel _musicChannel;
- sfxChannel _sfxChannels[4];
- drawState drawState;
+ drawState_t drawState;
- hwState hwState;
+ hwState_t hwState;
//TODO use draw state struct here
/*
diff --git a/source/graphics.cpp b/source/graphics.cpp
index f8dd406..74a48da 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -303,7 +303,7 @@ bool Graphics::isOnScreen(int x, int y) {
bool Graphics::isColorTransparent(uint8_t color) {
color = color & 15;
- return _memory->drawState.drawPaletteMap[color] | BITMASK(4); //4th bit is transparency
+ return (_memory->drawState.drawPaletteMap[color] >> 4) & 1U; //4th bit is transparency
}
uint8_t Graphics::getDrawPalMappedColor(uint8_t color) {
@@ -344,15 +344,15 @@ int clampCoordToScreenDims(int val) {
int Graphics::clampXCoordToClip(int x) {
return std::clamp(
x,
- _memory->drawState.clip_xb,
- _memory->drawState.clip_xe - 1);
+ (int)_memory->drawState.clip_xb,
+ (int)_memory->drawState.clip_xe - 1);
}
int Graphics::clampYCoordToCLip(int y) {
return std::clamp(
y,
- _memory->drawState.clip_yb,
- _memory->drawState.clip_ye - 1);
+ (int)_memory->drawState.clip_yb,
+ (int)_memory->drawState.clip_ye - 1);
}
@@ -650,7 +650,8 @@ int Graphics::print(std::string str, int x, int y, uint16_t c) {
uint8_t prevCol7Map = _memory->drawState.drawPaletteMap[7];
bool prevCol0Transp = isColorTransparent(0);
- _memory->drawState.drawPaletteMap[7] = c | BITMASK(4);
+ pal(7, c, 0);
+ palt(7, false);
palt(0, true);
@@ -836,21 +837,19 @@ void Graphics::pal(uint8_t c0, uint8_t c1, uint8_t p){
}
void Graphics::palt() {
- _memory->drawState.drawPaletteMap[0] &= ~(1UL << 4);
+ _memory->drawState.drawPaletteMap[0] |= 1UL << 4;
for (uint8_t c = 1; c < 16; c++) {
- _memory->drawState.drawPaletteMap[c] |= BITMASK(4);
+ _memory->drawState.drawPaletteMap[c] &= ~(1UL << 4);
}
}
void Graphics::palt(uint8_t c, bool t){
- if (c < 16) {
- if (t) {
- _memory->drawState.drawPaletteMap[c] |= BITMASK(4);
- }
- else {
- _memory->drawState.drawPaletteMap[c] &= ~(1UL << 4);
-
- }
+ c = c & 15;
+ if (t) {
+ _memory->drawState.drawPaletteMap[c] |= 1UL << 4;
+ }
+ else {
+ _memory->drawState.drawPaletteMap[c] &= ~(1UL << 4);
}
}
diff --git a/source/vm.cpp b/source/vm.cpp
index be1ea1e..6300958 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -32,10 +32,6 @@ Vm::Vm(){
Graphics* graphics = new Graphics(fontdata, &_memory);
_graphics = graphics;
- _memory._gfxState_color = 7;
- _graphics->clip();
- _graphics->pal();
-
Logger::Write("Creating Input object\n");
Input* input = new Input();
_input = input;
@@ -65,7 +61,7 @@ bool Vm::loadCart(Cart* cart) {
//reset memory (may have to be more selective about zeroing out to be accurate?)
_memory = {0};
//set graphics state
- _memory._gfxState_color = 7;
+ _graphics->color(7);
_graphics->clip();
_graphics->pal();
@@ -79,10 +75,7 @@ bool Vm::loadCart(Cart* cart) {
}
//reset audio
- for(int i = 0; i < 4; i++) {
- _memory._sfxChannels[i].sfxId = -1;
- }
- _memory._musicChannel.pattern = -1;
+ _audio->resetAudioState();
//copy data from cart rom to ram
for(size_t i = 0; i < sizeof(_memory.spriteSheetData); i++) {
diff --git a/test/Makefile b/test/Makefile
index f0dbc75..2751234 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -79,7 +79,8 @@ runtests: testrunner.a
./testrunner.a
clean:
- -rm -f testrunner.a *.o
+ -rm -f testrunner.a *.o && \
+ cd ../libs/lua-5.3.2 && make clean
echo:
@echo "CURDIR= $(CURDIR)"
diff --git a/test/audiotests.cpp b/test/audiotests.cpp
index 3b28e40..da19d7f 100644
--- a/test/audiotests.cpp
+++ b/test/audiotests.cpp
@@ -7,12 +7,13 @@ TEST_CASE("audio class behaves as expected") {
PicoRam picoRam;
picoRam = {0};
Audio* audio = new Audio(&picoRam);
+ audioState* audioState = audio->getAudioState();
SUBCASE("Audio constructor sets sfx channels to -1") {
bool allChannelsOff = true;
for(int i = 0; i < 4; i++) {
- allChannelsOff &= picoRam._sfxChannels[i].sfxId == -1;
+ allChannelsOff &= audioState->_sfxChannels[i].sfxId == -1;
}
CHECK(allChannelsOff);
@@ -22,7 +23,7 @@ TEST_CASE("audio class behaves as expected") {
int sfxId = 3;
audio->api_sfx(sfxId, channel, 0);
- CHECK_EQ(picoRam._sfxChannels[0].sfxId, sfxId);
+ CHECK_EQ(audioState->_sfxChannels[0].sfxId, sfxId);
}
SUBCASE("api_sfx() with -1 channel finds first available") {
audio->api_sfx(1, 0, 0);
@@ -30,26 +31,26 @@ TEST_CASE("audio class behaves as expected") {
audio->api_sfx(5, -1, 0);
- CHECK_EQ(picoRam._sfxChannels[2].sfxId, 5);
+ CHECK_EQ(audioState->_sfxChannels[2].sfxId, 5);
}
SUBCASE("api_sfx() -2 sfx id stops looping") {
- picoRam._sfxChannels[3].can_loop = true;
+ audioState->_sfxChannels[3].can_loop = true;
audio->api_sfx(-2, 3, 0);
- CHECK_EQ(picoRam._sfxChannels[3].can_loop, false);
+ CHECK_EQ(audioState->_sfxChannels[3].can_loop, false);
}
SUBCASE("api_sfx() -2 sfx id stops looping") {
- picoRam._sfxChannels[3].can_loop = true;
+ audioState->_sfxChannels[3].can_loop = true;
audio->api_sfx(-2, 3, 0);
- CHECK_EQ(picoRam._sfxChannels[3].can_loop, false);
+ CHECK_EQ(audioState->_sfxChannels[3].can_loop, false);
}
SUBCASE("api_music sets music pattern"){
audio->api_music(14, 0, 0);
- CHECK_EQ(picoRam._musicChannel.pattern, 14);
+ CHECK_EQ(audioState->_musicChannel.pattern, 14);
}
}
\ No newline at end of file
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 0d002f7..2eb3b39 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -85,13 +85,13 @@ TEST_CASE("graphics class behaves as expected") {
CHECK(colorsEqual(& palette[15], & testColor) == true);
}
SUBCASE("Constructor sets default clip") {
- CHECK(picoRam._gfxState_clip_xb == 0);
- CHECK(picoRam._gfxState_clip_yb == 0);
- CHECK(picoRam._gfxState_clip_xe == 128);
- CHECK(picoRam._gfxState_clip_ye == 128);
+ CHECK(picoRam.drawState.clip_xb == 0);
+ CHECK(picoRam.drawState.clip_yb == 0);
+ CHECK(picoRam.drawState.clip_xe == 128);
+ CHECK(picoRam.drawState.clip_ye == 128);
}
SUBCASE("Constructor sets default color") {
- CHECK(picoRam._gfxState_color == 7);
+ CHECK(picoRam.drawState.color == 7);
}
SUBCASE("Constructor sets default draw color palette") {
//colors mapped to themselves
@@ -144,38 +144,38 @@ TEST_CASE("graphics class behaves as expected") {
SUBCASE("pset() with no color uses pen color") {
graphics->pset(121, 6);
- CHECK(graphics->pget(121, 6) == picoRam._gfxState_color);
+ CHECK(graphics->pget(121, 6) == picoRam.drawState.color);
}
SUBCASE("color({color}) sets color in ram") {
graphics->color(12);
- CHECK(picoRam._gfxState_color == 12);
+ CHECK(picoRam.drawState.color == 12);
}
SUBCASE("line() clears line state") {
- picoRam._gfxState_line_x = 10;
- picoRam._gfxState_line_y = 30;
- picoRam._gfxState_line_valid = true;
+ picoRam.drawState.line_x = 10;
+ picoRam.drawState.line_y = 30;
+ picoRam.drawState.lineInvalid = false;
graphics->line();
CHECK(
- (picoRam._gfxState_line_x == 0 &&
- picoRam._gfxState_line_y == 0 &&
- picoRam._gfxState_line_valid == false) == true);
+ (picoRam.drawState.line_x == 0 &&
+ picoRam.drawState.line_y == 0 &&
+ picoRam.drawState.lineInvalid == true) == true);
}
SUBCASE("line({arg}) sets color and clears line") {
- picoRam._gfxState_line_x = 10;
- picoRam._gfxState_line_y = 30;
- picoRam._gfxState_line_valid = true;
- picoRam._gfxState_color = 2;
+ picoRam.drawState.line_x = 10;
+ picoRam.drawState.line_y = 30;
+ picoRam.drawState.lineInvalid = false;
+ picoRam.drawState.color = 2;
graphics->line(14);
CHECK(
- (picoRam._gfxState_line_x == 0 &&
- picoRam._gfxState_line_y == 0 &&
- picoRam._gfxState_line_valid == false &&
- picoRam._gfxState_color == 14) == true);
+ (picoRam.drawState.line_x == 0 &&
+ picoRam.drawState.line_y == 0 &&
+ picoRam.drawState.lineInvalid == true &&
+ picoRam.drawState.color == 14) == true);
}
SUBCASE("line({x}, {y}) without valid line state does nothing") {
graphics->cls();
@@ -190,29 +190,29 @@ TEST_CASE("graphics class behaves as expected") {
}
CHECK(
- (picoRam._gfxState_line_x == 0 &&
- picoRam._gfxState_line_y == 0 &&
- picoRam._gfxState_line_valid == false &&
+ (picoRam.drawState.line_x == 0 &&
+ picoRam.drawState.line_y == 0 &&
+ picoRam.drawState.lineInvalid == true &&
isAllColor) == true);
}
SUBCASE("line({x}, {y}) with valid line state updates state") {
graphics->cls();
- picoRam._gfxState_line_x = 10;
- picoRam._gfxState_line_y = 10;
- picoRam._gfxState_line_valid = true;
+ picoRam.drawState.line_x = 10;
+ picoRam.drawState.line_y = 10;
+ picoRam.drawState.lineInvalid = false;
graphics->line(13, 13);
CHECK(
- (picoRam._gfxState_line_x == 13 &&
- picoRam._gfxState_line_y == 13 &&
- picoRam._gfxState_line_valid == true) == true);
+ (picoRam.drawState.line_x == 13 &&
+ picoRam.drawState.line_y == 13 &&
+ picoRam.drawState.lineInvalid == false) == true);
}
SUBCASE("line({x}, {y}) draws (45 degree down-right)") {
graphics->cls();
- picoRam._gfxState_color = 2;
- picoRam._gfxState_line_x = 10;
- picoRam._gfxState_line_y = 10;
- picoRam._gfxState_line_valid = true;
+ picoRam.drawState.color = 2;
+ picoRam.drawState.line_x = 10;
+ picoRam.drawState.line_y = 10;
+ picoRam.drawState.lineInvalid = false;
graphics->line(12, 12);
std::vector<coloredPoint> expectedPoints = {
@@ -225,9 +225,9 @@ TEST_CASE("graphics class behaves as expected") {
}
SUBCASE("line({x}, {y}, {c}) draws (vertical down)") {
graphics->cls();
- picoRam._gfxState_line_x = 20;
- picoRam._gfxState_line_y = 20;
- picoRam._gfxState_line_valid = true;
+ picoRam.drawState.line_x = 20;
+ picoRam.drawState.line_y = 20;
+ picoRam.drawState.lineInvalid = false;
graphics->line(20, 24, 13);
std::vector<coloredPoint> expectedPoints = {
@@ -242,7 +242,7 @@ TEST_CASE("graphics class behaves as expected") {
}
SUBCASE("line({x1}, {y1}, {x2}, {y2}) draws (45 degree left)") {
graphics->cls();
- picoRam._gfxState_color = 10;
+ picoRam.drawState.color = 10;
graphics->line(20, 20, 18, 22);
std::vector<coloredPoint> expectedPoints = {
@@ -557,8 +557,8 @@ TEST_CASE("graphics class behaves as expected") {
SUBCASE("print({str}) uses current text location") {
graphics->cls();
graphics->color(3);
- picoRam._gfxState_text_x = 15;
- picoRam._gfxState_text_y = 98;
+ picoRam.drawState.text_x = 15;
+ picoRam.drawState.text_y = 98;
graphics->print("t");
@@ -576,34 +576,34 @@ TEST_CASE("graphics class behaves as expected") {
}
SUBCASE("print({str}) increments text location y by 6") {
graphics->cls();
- picoRam._gfxState_text_x = 15;
- picoRam._gfxState_text_y = 110;
+ picoRam.drawState.text_x = 15;
+ picoRam.drawState.text_y = 110;
graphics->print("doesnt matter");
- CHECK(picoRam._gfxState_text_y == 116);
+ CHECK(picoRam.drawState.text_y == 116);
}
SUBCASE("print({str}, {x}, {y}) updates text location ") {
graphics->cls();
- picoRam._gfxState_text_x = 3;
- picoRam._gfxState_text_y = 4;
+ picoRam.drawState.text_x = 3;
+ picoRam.drawState.text_y = 4;
graphics->print("doesnt matter", 42, 99);
- CHECK(picoRam._gfxState_text_x == 42);
- CHECK(picoRam._gfxState_text_y == 99);
+ CHECK(picoRam.drawState.text_x == 42);
+ CHECK(picoRam.drawState.text_y == 99);
}
SUBCASE("print({str}, {x}, {y}, {c}) updates text location and color") {
graphics->cls();
- picoRam._gfxState_text_x = 3;
- picoRam._gfxState_text_y = 4;
- picoRam._gfxState_color = 10;
+ picoRam.drawState.text_x = 3;
+ picoRam.drawState.text_y = 4;
+ picoRam.drawState.color = 10;
graphics->print("doesnt matter", 16, 18, 14);
- CHECK(picoRam._gfxState_text_x == 16);
- CHECK(picoRam._gfxState_text_y == 18);
- CHECK(picoRam._gfxState_color == 14);
+ CHECK(picoRam.drawState.text_x == 16);
+ CHECK(picoRam.drawState.text_y == 18);
+ CHECK(picoRam.drawState.color == 14);
}
SUBCASE("spr(...) draws to screen at location") {
graphics->cls();
@@ -971,23 +971,23 @@ TEST_CASE("graphics class behaves as expected") {
uint8_t y = 120;
graphics->camera(x, y);
- CHECK_EQ(picoRam._gfxState_camera_x, x);
- CHECK_EQ(picoRam._gfxState_camera_y, y);
+ CHECK_EQ(picoRam.drawState.camera_x, x);
+ CHECK_EQ(picoRam.drawState.camera_y, y);
}
SUBCASE("camera() sets values in memory to 0")
{
- picoRam._gfxState_camera_x = 33;
- picoRam._gfxState_camera_y = 120;
+ picoRam.drawState.camera_x = 33;
+ picoRam.drawState.camera_y = 120;
graphics->camera();
- CHECK_EQ(picoRam._gfxState_camera_x, 0);
- CHECK_EQ(picoRam._gfxState_camera_y, 0);
+ CHECK_EQ(picoRam.drawState.camera_x, 0);
+ CHECK_EQ(picoRam.drawState.camera_y, 0);
}
SUBCASE("camera values applies to pget")
{
uint8_t col = 13;
- picoRam._gfxState_camera_x = -44;
- picoRam._gfxState_camera_y = -3;
+ picoRam.drawState.camera_x = -44;
+ picoRam.drawState.camera_y = -3;
graphics->pset(14, 32, 13);
auto result = graphics->pget(14, 32);
@@ -997,8 +997,8 @@ TEST_CASE("graphics class behaves as expected") {
SUBCASE("camera values applies to pset")
{
uint8_t col = 13;
- picoRam._gfxState_camera_x = -44;
- picoRam._gfxState_camera_y = -3;
+ picoRam.drawState.camera_x = -44;
+ picoRam.drawState.camera_y = -3;
graphics->pset(14, 32, 13);
graphics->camera();
@@ -1170,26 +1170,26 @@ TEST_CASE("graphics class behaves as expected") {
}
SUBCASE("clip() resets clip memory to entire screen")
{
- picoRam._gfxState_clip_xb = 28;
- picoRam._gfxState_clip_xe = 28;
- picoRam._gfxState_clip_yb = 50;
- picoRam._gfxState_clip_ye = 50;
+ picoRam.drawState.clip_xb = 28;
+ picoRam.drawState.clip_xe = 28;
+ picoRam.drawState.clip_yb = 50;
+ picoRam.drawState.clip_ye = 50;
graphics->clip();
- CHECK_EQ(picoRam._gfxState_clip_xb, 0);
- CHECK_EQ(picoRam._gfxState_clip_xe, 128);
- CHECK_EQ(picoRam._gfxState_clip_yb, 0);
- CHECK_EQ(picoRam._gfxState_clip_ye, 128);
+ CHECK_EQ(picoRam.drawState.clip_xb, 0);
+ CHECK_EQ(picoRam.drawState.clip_xe, 128);
+ CHECK_EQ(picoRam.drawState.clip_yb, 0);
+ CHECK_EQ(picoRam.drawState.clip_ye, 128);
}
SUBCASE("clip({x}, {y}, {w}, {h}) sets clip in memory")
{
graphics->clip(50, 75, 25, 25);
- CHECK_EQ(picoRam._gfxState_clip_xb, 50);
- CHECK_EQ(picoRam._gfxState_clip_xe, 75);
- CHECK_EQ(picoRam._gfxState_clip_yb, 75);
- CHECK_EQ(picoRam._gfxState_clip_ye, 100);
+ CHECK_EQ(picoRam.drawState.clip_xb, 50);
+ CHECK_EQ(picoRam.drawState.clip_xe, 75);
+ CHECK_EQ(picoRam.drawState.clip_yb, 75);
+ CHECK_EQ(picoRam.drawState.clip_ye, 100);
}
SUBCASE("clip values applies to pset")
{
@@ -1522,6 +1522,12 @@ TEST_CASE("graphics class behaves as expected") {
CHECK(graphics->isColorTransparent(2) == true);
}
+ SUBCASE("palt({c}, {t}) sets transparency value for color") {
+ graphics->palt(4, true);
+ graphics->palt(4, false);
+
+ CHECK(graphics->isColorTransparent(4) == false);
+ }
SUBCASE("pal() resets all palette changes") {
graphics->pal(14, 5, 0);
graphics->pal(13, 4, 1);
@@ -1576,23 +1582,23 @@ TEST_CASE("graphics class behaves as expected") {
SUBCASE("cursor({x}, {y}) sets cursor loc in memory"){
graphics->cursor(4, 13);
- CHECK_EQ(picoRam._gfxState_text_x, 4);
- CHECK_EQ(picoRam._gfxState_text_y, 13);
+ CHECK_EQ(picoRam.drawState.text_x, 4);
+ CHECK_EQ(picoRam.drawState.text_y, 13);
}
SUBCASE("cursor({x}, {y}, {c}) sets cursor loc in memory and color"){
graphics->cursor(42, 99, 3);
- CHECK_EQ(picoRam._gfxState_text_x, 42);
- CHECK_EQ(picoRam._gfxState_text_y, 99);
- CHECK_EQ(picoRam._gfxState_color, 3);
+ CHECK_EQ(picoRam.drawState.text_x, 42);
+ CHECK_EQ(picoRam.drawState.text_y, 99);
+ CHECK_EQ(picoRam.drawState.color, 3);
}
SUBCASE("cursor() resets cursor but not color"){
graphics->cursor(42, 99, 3);
graphics->cursor();
- CHECK_EQ(picoRam._gfxState_text_x, 0);
- CHECK_EQ(picoRam._gfxState_text_y, 0);
- CHECK_EQ(picoRam._gfxState_color, 3);
+ CHECK_EQ(picoRam.drawState.text_x, 0);
+ CHECK_EQ(picoRam.drawState.text_y, 0);
+ CHECK_EQ(picoRam.drawState.color, 3);
}