diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ab29b5..a76c362 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,9 @@ add_executable(unit_tests tests/CPU/Math/testCMP.cpp sources/PPU/Background.cpp sources/PPU/Background.hpp - sources/PPU/PPUUtils.cpp) + sources/PPU/PPUUtils.cpp + tests/PPU/testBackground.cpp + ) # include criterion & coverage target_link_libraries(unit_tests criterion -lgcov) @@ -217,7 +219,8 @@ add_executable(ComSquare sources/Models/Vector2.hpp sources/PPU/Background.cpp sources/PPU/Background.hpp - sources/PPU/PPUUtils.cpp) + sources/PPU/PPUUtils.cpp + ) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index e7a8344..c304b0d 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -100,13 +100,15 @@ namespace ComSquare::PPU return palette; } - uint8_t Background::getTilePixelReference(uint16_t addr, int nb) + uint8_t Background::getTilePixelReference(uint16_t addr, int index) { + //line by line for each pixel of the line uint8_t highByte = this->_vram->read_internal(addr % VRAMSIZE); uint8_t lowByte = this->_vram->read_internal((addr + 1) % VRAMSIZE); uint8_t secondHightByte; uint8_t secondLowByte; uint8_t result = 0; + uint8_t shift = (TILE_PIXEL_WIDTH - 1 - index); // C000 switch (this->_bpp) { @@ -115,9 +117,9 @@ namespace ComSquare::PPU case 4: secondHightByte = this->_vram->read_internal((addr + 32) % VRAMSIZE); secondLowByte = this->_vram->read_internal((addr + 33) % VRAMSIZE); - result = ((secondHightByte & (1U << (7U - nb))) | ((secondLowByte & (1U << (7U - nb))) << 1U)) >> (7U - nb - 2); + result = ((secondHightByte & (1U << (7U - index))) | ((secondLowByte & (1U << (7U - index))) << 1U)) >> (7U - index - 2); case 2: - result += ((highByte & (1U << (7U - nb))) | ((lowByte & (1U << (7U - nb))) << 1U)) >> (7U - nb); + result += ((highByte & (1U << (7U - index))) | ((lowByte & (1U << (7U - index))) << 1U)) >> (7U - index); default: break; } diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 994d744..bca10cc 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -21,6 +21,8 @@ namespace ComSquare::PPU class Background { #define NB_CHARACTER_WIDTH 32 #define NB_CHARACTER_HEIGHT 32 + #define TILE_PIXEL_WIDTH 8 + #define TILE_PIXEL_HEIGHT 8 private: Vector2 _tileMaps; Vector2 _characterSize; @@ -36,8 +38,10 @@ namespace ComSquare::PPU void drawBgTile(uint16_t data, Vector2 pos); //! @brief Get a palette from the number of the palette (0 - 7) std::vector getPalette(int nbPalette); - //! @brief Get the color reference of a nb pixel tile - uint8_t getTilePixelReference(uint16_t addr, int nb); + //! @brief Get the color reference of a index pixel tile + //! @param addr The address of the line of pixel + //TODO support addr as the address of the start of the tile and index goes from 0 to 63 regardless of the bpp + uint8_t getTilePixelReference(uint16_t addr, int index); //! @brief draw a tilemap 32x32 starting at baseAddress void drawBasicTileMap(uint16_t baseAddress, Vector2 offset); public: diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index c19c735..bb1b14d 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -129,7 +129,7 @@ namespace ComSquare::PPU uint8_t PPU::read(uint24_t addr) { - return 0; + // return 0; switch (addr) { case ppuRegisters::mpyl: return this->_registers._mpy.mpyl; @@ -158,7 +158,7 @@ namespace ComSquare::PPU void PPU::write(uint24_t addr, uint8_t data) { - return; + //return; switch (addr) { case ppuRegisters::inidisp: this->_registers._inidisp.raw = data; diff --git a/tests/PPU/testBackground.cpp b/tests/PPU/testBackground.cpp new file mode 100644 index 0000000..1d5de58 --- /dev/null +++ b/tests/PPU/testBackground.cpp @@ -0,0 +1,21 @@ +// +// Created by cbihan on 9/29/20. +// + +#include +#include +#include +#include "../tests.hpp" +#include "../../sources/SNES.hpp" +#include "../../sources/Memory/MemoryBus.hpp" +#include "../../sources/PPU/PPU.hpp" + +using namespace ComSquare; + +Test(backgroundGetTilePixelReference, basicTest) +{ + Init() + snes._bus->write(0x2100, 0b11111111); + cr_assert_eq(snes.ppu->_registers._inidisp.fblank, true); + cr_assert_eq(snes.ppu->_registers._inidisp.brightness, 0xF); +} \ No newline at end of file