diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 333b84d..dc50328 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -62,12 +62,12 @@ namespace ComSquare::PPU color = getRealColor(palette[reference]); if (tileData.tilePriority == this->priority) this->buffer[pos.x][pos.y] = color; + if (index == 7 || this->_bpp == 8) { + index = 0; + graphicAddress += 2; + } index++; pos.x++; - if (index == (8 / this->_bpp) - 1) { - index = 0; - graphicAddress++; - } } index = 0; pos.x -= this->_characterSize.x; @@ -83,25 +83,33 @@ namespace ComSquare::PPU for (int i = 0; i < 0xF; i++) { palette[i] = this->_cgram->read_internal(addr); palette[i] += this->_cgram->read_internal(addr + 1) << 8U; + addr += 2; } return palette; } uint8_t Background::getTilePixelReference(uint16_t addr, int nb) { - uint8_t reference = this->_vram->read_internal(addr); + 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; + // C000 switch (this->_bpp) { case 8: - return reference; + return highByte; case 4: - return (reference & (0xFU << ((1 - nb) * 4U))) >> (1 - nb) * 4U; + secondHightByte = this->_vram->read_internal((addr + 32) % VRAMSIZE); + secondLowByte = this->_vram->read_internal((addr + 33) %VRAMSIZE); + result = (((secondLowByte & (1U << (7U - nb))) + ((secondHightByte & (1U << (7U - nb))) << 1U)) << 2U) >> (7U - nb - 2); case 2: - return (reference & (0x3U << ((3 - nb) * 2U))) >> (3 - nb) * 2U; + result += ((lowByte & (1U << (7U - nb))) + ((highByte & (1U << (7U - nb))) << 1U)) >> (7U - nb - 1); default: break; } - return 0; + return result; } void Background::drawBasicTileMap(uint16_t baseAddress, Vector2 offset) @@ -113,8 +121,8 @@ namespace ComSquare::PPU while (vramAddress < 0x800 + baseAddress) { tileMapValue = this->_vram->read_internal(vramAddress); tileMapValue += this->_vram->read_internal(vramAddress + 1) << 8U; - vramAddress += 2; drawBgTile(tileMapValue, {(pos.x * this->_characterSize.x) + offset.x, (pos.y * this->_characterSize.y) + offset.y}); + vramAddress += 2; if (pos.x % 31 == 0 && pos.x) { pos.y++; pos.x = 0; @@ -124,5 +132,10 @@ namespace ComSquare::PPU } } + void Background::setTileMapStartAddress(uint16_t address) + { + this->_TileMapStartAddress = address; + } + } \ No newline at end of file diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 897230f..719770f 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -46,6 +46,8 @@ namespace ComSquare::PPU Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority); //! @brief Render a background on the screen void renderBackground(void); + //! @brief Set the tilemap start address + void setTileMapStartAddress(uint16_t address); }; } diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index fa371ea..0c094cd 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -15,9 +15,9 @@ namespace ComSquare::PPU { PPU::PPU(Renderer::IRenderer &renderer): - vram(new Ram::Ram(65536, ComSquare::VRam, "VRAM")), - oamram(new Ram::Ram(544, ComSquare::OAMRam, "OAMRAM")), - cgram(new Ram::Ram(512, ComSquare::CGRam, "CGRAM")), + vram(new Ram::Ram(VRAMSIZE, ComSquare::VRam, "VRAM")), + oamram(new Ram::Ram(OAMRAMSIZE, ComSquare::OAMRam, "OAMRAM")), + cgram(new Ram::Ram(CGRAMSIZE, ComSquare::CGRam, "CGRAM")), _renderer(renderer), _backgrounds{ Background(*this, 1, false), @@ -31,13 +31,105 @@ namespace ComSquare::PPU } { this->_registers._isLowByte = true; - for (int i = 0; i < 512; i++) { + /*for (int i = 0; i < 512; i++) { this->cgram->write_internal(i, random() % 255); + }*/ + + //colors for the cgram + this->cgram->write_internal(2, 0xE0); + this->cgram->write_internal(3, 0x7F); + this->cgram->write_internal(4, 0x1F); + this->cgram->write_internal(6, 0xFF); + this->cgram->write_internal(7, 0x03); + this->cgram->write_internal(66, 0xE0); + this->cgram->write_internal(67, 0x7F); + + //tiles + int vram_test[] = { + 00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, +0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff, +03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xff,0xff,0xff,0xff, +0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xff,0xff,0xff,0xff, +00,0xc0,0x00,0xe0,0x00,0x70,0x00,0x38,0x00,0x1c,0x00,0x0e,0x00,0x07,0x00,0x03, +00,0x03,0x00,0x07,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x70,0x00,0xe0,0x00,0xc0, +00,0x07,0x00,0x0f,0x00,0x18,0x00,0x30,0x00,0x60,0x00,0xc0,0x00,0xc0,0x00,0xc0, +00,0xe0,0x00,0xf0,0x00,0x18,0x00,0x0c,0x00,0x06,0x00,0x03,0x00,0x03,0x00,0x03, +0xfc,0x00,0xf8,0x00,0xf0,0x00,0xe0,0x00,0xc0,0x00,0x80,0x00,0x00,0x00,0x00,0x00, +0x3f,0x00,0x1f,0x00,0x0f,0x00,0x07,0x00,0x03,0x00,0x01,0x00,0x00,0x00,0x00,0x00, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, +0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0, +0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0xff,0xff,0xff,0xff,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, +0xff,0xff,0xff,0xff,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0, +00,0x03,0x00,0x07,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x70,0x00,0xe0,0x00,0xc0, +00,0xc0,0x00,0xe0,0x00,0x70,0x00,0x38,0x00,0x1c,0x00,0x0e,0x00,0x07,0x00,0x03, +00,0xc0,0x00,0xc0,0x00,0xc0,0x00,0x60,0x00,0x30,0x00,0x18,0x00,0x0f,0x00,0x07, +00,0x03,0x00,0x03,0x00,0x03,0x00,0x06,0x00,0x0c,0x00,0x18,0x00,0xf0,0x00,0xe0, +00,0x00,0x00,0x00,0x80,0x00,0xc0,0x00,0xe0,0x00,0xf0,0x00,0xf8,0x00,0xfc,0x00, +00,0x00,0x00,0x00,0x01,0x00,0x03,0x00,0x07,0x00,0x0f,00,0x1f,00,0x3f,00, -1 + }; + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i, vram_test[i]); } + int vram_test_2[] = {8, 00, 02, 00, 0x0A, 00, 02, 00, 0x0A, 00, 00, 00, 00, 00, 00, -1}; + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i + 0x8000, vram_test_2[i]); + } + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i, vram_test[i]); + } + int vram_test_3[] = {8, 00, 02, 00, 0x0A, 00, 02, 00, 0x0A, 00, 00, 00, 00, 00, 00, -1}; + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i + 0x8080, vram_test_3[i]); + } + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i, vram_test[i]); + } + int vram_test_4[] = {8, 00, 02, 00, 0x0A, 00, 02, 00, 0x0A, 00, 00, 00, 00, 00, 00, -1}; + for (int i = 0; vram_test[i] != -1; i++) { + this->vram->write_internal(i + 0x8100, vram_test_4[i]); + } + this->vram->write_internal(0x8040, 04); + this->vram->write_internal(0x8042, 06); + this->vram->write_internal(0x8044, 04); + this->vram->write_internal(0x8046, 06); + this->vram->write_internal(0x8048, 04); + + this->vram->write_internal(0x80C0, 04); + this->vram->write_internal(0x80C2, 06); + this->vram->write_internal(0x80C4, 04); + this->vram->write_internal(0x80C6, 06); + this->vram->write_internal(0x80C8, 04); + + //registers + + this->_registers._bgmode.bgMode = 0; + this->_registers._bgmode.characterSizeBg1 = true; + this->_registers._bgmode.characterSizeBg2 = true; + + this->_registers._bgsc[0].tilemapAddress = 0x4000U >> 10U; + this->_registers._bgsc[1].tilemapAddress = 0x6000U >> 10U; + this->_backgrounds[0].setTileMapStartAddress(this->getTileMapStartAddress(1)); + this->_backgrounds[1].setTileMapStartAddress(this->getTileMapStartAddress(1)); + this->_backgrounds[2].setTileMapStartAddress(this->getTileMapStartAddress(2)); + this->_backgrounds[3].setTileMapStartAddress(this->getTileMapStartAddress(2)); + + this->_registers._t[0].enableWindowDisplayBg1 = true; + this->_registers._t[0].enableWindowDisplayBg2 = true; } uint8_t PPU::read(uint24_t addr) { + return 0; switch (addr) { case ppuRegisters::mpyl: return this->_registers._mpy.mpyl; @@ -66,6 +158,7 @@ namespace ComSquare::PPU void PPU::write(uint24_t addr, uint8_t data) { + return; switch (addr) { case ppuRegisters::inidisp: this->_registers._inidisp.raw = data; @@ -469,7 +562,7 @@ namespace ComSquare::PPU uint16_t PPU::getTileMapStartAddress(int bgNumber) { - return this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U; + return this->_registers._bgsc[bgNumber - 1].tilemapAddress << 11U; } uint16_t PPU::getTileSetAddress(int bgNumber) @@ -498,9 +591,9 @@ namespace ComSquare::PPU colorPalette = this->cgram->read_internal(0); colorPalette += this->cgram->read_internal(1) << 8U; - for (unsigned long i = 0; i < this->_mainScreen.size(); i++) - for (unsigned long j = 0; j < this->_mainScreen[i].size(); j++) - this->_mainScreen[i][j] = getRealColor(colorPalette); + for (unsigned long i = 0; i < this->_subScreen.size(); i++) + for (unsigned long j = 0; j < this->_subScreen[i].size(); j++) + 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 switch (this->_registers._bgmode.bgMode) { @@ -603,8 +696,8 @@ namespace ComSquare::PPU int i = bg.bgNumber + bg.priority; if (this->_registers._t[0].raw & (1U << (bg.bgNumber - 1U))) - this->add_buffer(this->_mainScreen, this->_backgrounds[i].buffer); + this->add_buffer(this->_mainScreen, bg.buffer); if (this->_registers._t[1].raw & (1U << (bg.bgNumber - 1U))) - this->add_buffer(this->_subScreen, this->_backgrounds[i].buffer); + this->add_buffer(this->_subScreen, bg.buffer); } } \ No newline at end of file diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index d2c1401..31454fa 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -18,6 +18,10 @@ //#define max4BitTiles 2048 //#define max8BitTiles 1024 +#define VRAMSIZE 65536 +#define CGRAMSIZE 512 +#define OAMRAMSIZE 544 + namespace ComSquare::PPU { class Background; diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 11f963e..16d90e3 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -21,4 +21,4 @@ namespace ComSquare::PPU uint16_t raw; }; } -#endif //COMSQUARE_PPU_UTILS_HPP +#endif //COMSQUARE_PPU_UTILS_HPP \ No newline at end of file