diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 164be82..52658c0 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -54,9 +54,8 @@ namespace ComSquare::PPU uint8_t reference = 0; uint32_t color = 0; - //union isn't working as intended tileData.raw = data; - palette = getPalettes(tileData.palette); + palette = getPalette(tileData.palette); // X horizontal // Y vertical graphicAddress = this->_tilesetAddress + (tileData.posY * 16 * this->_bpp * 8) + (tileData.posX * this->_bpp * 8); @@ -74,12 +73,12 @@ namespace ComSquare::PPU } } - std::vector Background::getPalettes(int nbPalette) + std::vector Background::getPalette(int nbPalette) { - std::vector palette(0xF); - uint16_t addr = nbPalette * 0x10; + std::vector palette(0x10); + uint16_t addr = nbPalette * 0x20; - for (int i = 0; i < 0xF; i++) { + for (int i = 0; i < 0x10; i++) { palette[i] = this->_cgram->read_internal(addr); palette[i] += this->_cgram->read_internal(addr + 1) << 8U; addr += 2; @@ -119,8 +118,8 @@ namespace ComSquare::PPU case 8: return highByte; case 4: - secondHighByte = this->_vram->read_internal((tileAddress + 32) % VRAMSIZE); - secondLowByte = this->_vram->read_internal((tileAddress + 33) % VRAMSIZE); + secondHighByte = this->_vram->read_internal((tileAddress + 16) % VRAMSIZE); + secondLowByte = this->_vram->read_internal((tileAddress + 17) % VRAMSIZE); result = ((secondHighByte & (1U << shift)) | ((secondLowByte & (1U << shift)) << 1U)) >> (shift - 2U); case 2: result += ((highByte & (1U << shift)) | ((lowByte & (1U << shift)) << 1U)) >> shift; @@ -165,4 +164,12 @@ namespace ComSquare::PPU { this->_characterSize = size; } + + void Background::setBpp(int bpp) + { + if (bpp == 2 || bpp == 4 || bpp == 8 || bpp == 7) + this->_bpp = bpp; + else + this->_bpp = 2; + } } \ No newline at end of file diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 230d643..45f006f 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -39,7 +39,7 @@ namespace ComSquare::PPU //! @brief Get a palette from the number of the palette //! @param nbPalette The palette number (0 - 7) //! @return The array of color of the palette - std::vector getPalettes(int nbPalette); + std::vector getPalette(int nbPalette); //! @brief Get the color reference of a pixel from the address of the row //! @param tileAddress The address of the line of pixel //! @param pixelIndex The index of the pixel (0 - 7) @@ -71,6 +71,9 @@ namespace ComSquare::PPU void setCharacterSize(Vector2 size); //! @brief Set the tileset address of the background void setTilesetAddress(uint16_t address); + //! @brief Set the bpp (bits per pixels) of the Background + //! @info The bpp can be 2, 4 or 8 (7 can be possible when BgMode is 7) + void setBpp(int bpp); }; } diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 8dcfe71..e84c468 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -115,6 +115,12 @@ namespace ComSquare::PPU //registers this->_registers._bgmode.bgMode = 1; + this->_backgrounds[0].setBpp(this->getBPP(1)); + this->_backgrounds[1].setBpp(this->getBPP(1)); + this->_backgrounds[2].setBpp(this->getBPP(2)); + this->_backgrounds[3].setBpp(this->getBPP(2)); + this->_backgrounds[4].setBpp(this->getBPP(3)); + this->_backgrounds[5].setBpp(this->getBPP(3)); //this->_registers._bgmode.characterSizeBg1 = false; //this->_registers._bgmode.characterSizeBg2 = false; this->_registers._bgmode.mode1Bg3PriorityBit = true; @@ -159,6 +165,7 @@ namespace ComSquare::PPU uint8_t PPU::read(uint24_t addr) { + return 0; switch (addr) { case ppuRegisters::mpyl: return this->_registers._mpy.mpyl; @@ -187,6 +194,7 @@ namespace ComSquare::PPU void PPU::write(uint24_t addr, uint8_t data) { + return; switch (addr) { case ppuRegisters::inidisp: this->_registers._inidisp.raw = data; @@ -211,8 +219,10 @@ namespace ComSquare::PPU case ppuRegisters::bgmode: this->_registers._bgmode.raw = data; // update backgrounds - for (int i = 0; i < 8; i++) + for (int i = 0; i < 8; i++) { + this->_backgrounds[i].setBpp(this->getBPP((i / 2) + 1)); this->_backgrounds[i].setCharacterSize(this->getCharacterSize((i / 2) + 1)); + } break; case ppuRegisters::mosaic: this->_registers._mosaic.raw = data; @@ -634,6 +644,9 @@ namespace ComSquare::PPU this->_subScreen[i][j] = getRealColor(colorPalette); // the buffer is overwrite if necessary by a new bg so the background priority is from back to front // the starting palette index isn't implemented + this->addToMainSubScreen(this->_backgrounds[bgName::bg1NoPriority]); + this->addToMainSubScreen(this->_backgrounds[bgName::bg1Priority]); + return; switch (this->_registers._bgmode.bgMode) { case 0: this->addToMainSubScreen(this->_backgrounds[bgName::bg4NoPriority]);