fixing some issues from those reported by the pr

This commit is contained in:
Clément Le Bihan
2021-05-21 00:12:00 +02:00
parent 89c9d71168
commit 11bca5fe97
3 changed files with 81 additions and 57 deletions

View File

@@ -8,7 +8,7 @@ project(ComSquare)
add_compile_options(-W -Wall -Wextra -Wshadow) add_compile_options(-W -Wall -Wextra -Wshadow)
# make unit tests # make unit tests
add_executable(unit_tests add_executable(unit_tests EXCLUDE_FROM_ALL
tests/CPU/testAddressingMode.cpp tests/CPU/testAddressingMode.cpp
tests/CPU/testInterupts.cpp tests/CPU/testInterupts.cpp
tests/testMemoryBus.cpp tests/testMemoryBus.cpp
@@ -240,6 +240,7 @@ add_executable(ComSquare
) )
target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED)
include_directories(ComSquare sources)
find_package(Qt5 COMPONENTS Widgets REQUIRED) find_package(Qt5 COMPONENTS Widgets REQUIRED)

View File

@@ -6,44 +6,43 @@
#include "PPU.hpp" #include "PPU.hpp"
#include "Background.hpp" #include "Background.hpp"
#include <cmath> #include <cmath>
#include "../Models/Vector2.hpp" #include "Models/Vector2.hpp"
namespace ComSquare::PPU namespace ComSquare::PPU
{ {
Background::Background(ComSquare::PPU::PPU &ppu, int backGroundNumber, bool hasPriority): Background::Background(ComSquare::PPU::PPU &ppu, int backGroundNumber, bool hasPriority):
_ppu(ppu),
_tileMapsConfig(ppu.getBackgroundSize(backGroundNumber)),
_characterNbPixels(ppu.getCharacterSize(backGroundNumber)),
_bpp(ppu.getBPP(backGroundNumber)),
_directColor(false),
_highRes(false),
_tileMapStartAddress(ppu.getTileMapStartAddress(backGroundNumber)),
_tilesetAddress(ppu.getTilesetAddress(backGroundNumber)),
_priority(hasPriority), _priority(hasPriority),
_bgNumber(backGroundNumber), _bgNumber(backGroundNumber),
_ppu(ppu) _vram(ppu.vram),
_cgram(ppu.cgram),
buffer({{{0}}})
{ {
_cgram = ppu.cgram;
_vram = ppu.vram;
_bpp = ppu.getBPP(backGroundNumber);
_characterSize = ppu.getCharacterSize(backGroundNumber);
_tileMapStartAddress = ppu.getTileMapStartAddress(backGroundNumber);
_tilesetAddress = ppu.getTilesetAddress(backGroundNumber);
_tileMaps = ppu.getBackgroundSize(backGroundNumber);
_directColor = false;
_highRes = false;
this->buffer = {{{0}}};
} }
void Background::renderBackground() void Background::renderBackground()
{ {
uint16_t vramAddress = this->_tileMapStartAddress; uint16_t vramAddress = this->_tileMapStartAddress;
Vector2<int> offset = this->_ppu.getBgScroll(this->_bgNumber); Vector2<int> offset = this->_ppu.getBgScroll(this->_bgNumber);
this->backgroundSize.x = this->_tileMaps.x * this->_characterSize.x * NB_CHARACTER_WIDTH; this->backgroundSize.x = this->_tileMapsConfig.x * this->_characterNbPixels.x * NbCharacterWidth;
this->backgroundSize.y = this->_tileMaps.y * this->_characterSize.y * NB_CHARACTER_HEIGHT; this->backgroundSize.y = this->_tileMapsConfig.y * this->_characterNbPixels.y * NbCharacterHeight;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (!(i == 1 && this->_tileMaps.x == 1) && !(i > 1 && this->_tileMaps.y == 1)) { if (!(i == 1 && this->_tileMapsConfig.x == 1) && !(i > 1 && this->_tileMapsConfig.y == 1)) {
drawBasicTileMap(vramAddress, offset); drawBasicTileMap(vramAddress, offset);
} }
vramAddress += 0x800; vramAddress += TileMapByteSize;
offset.x += NB_CHARACTER_WIDTH * this->_characterSize.x; offset.x += NbCharacterWidth * this->_characterNbPixels.x;
if (i == 2) { if (i == 2) {
offset.x = 0; offset.x = 0;
offset.y += NB_CHARACTER_HEIGHT * this->_characterSize.y; offset.y += NbCharacterHeight * this->_characterNbPixels.y;
} }
} }
} }
@@ -61,14 +60,14 @@ namespace ComSquare::PPU
palette = getPalette(tileData.palette); palette = getPalette(tileData.palette);
// X horizontal // X horizontal
// Y vertical // Y vertical
graphicAddress = this->_tilesetAddress + (tileData.posY * NB_TILE_PER_ROW * this->_bpp * TILE_SIZE) + (tileData.posX * this->_bpp * TILE_SIZE); graphicAddress = this->_tilesetAddress + (tileData.posY * NbTilePerRow * this->_bpp * TileBaseByteSize) + (tileData.posX * this->_bpp * TileBaseByteSize);
for (int i = 0; i < this->_characterSize.y; i++) { for (int i = 0; i < this->_characterNbPixels.y; i++) {
index = i * this->_characterSize.x; index = i * this->_characterNbPixels.x;
if (tileData.verticalFlip) if (tileData.verticalFlip)
index = (this->_characterSize.y - 1 - i) * this->_characterSize.x; index = (this->_characterNbPixels.y - 1 - i) * this->_characterNbPixels.x;
if (tileData.horizontalFlip) if (tileData.horizontalFlip)
index += this->_characterSize.x - 1; index += this->_characterNbPixels.x - 1;
for (int j = 0; j < this->_characterSize.x; j++) { for (int j = 0; j < this->_characterNbPixels.x; j++) {
reference = getPixelReferenceFromTile(graphicAddress, index); reference = getPixelReferenceFromTile(graphicAddress, index);
color = getRealColor(palette[reference]); color = getRealColor(palette[reference]);
if (tileData.tilePriority == this->_priority) // reference 0 is considered as transparency if (tileData.tilePriority == this->_priority) // reference 0 is considered as transparency
@@ -76,7 +75,7 @@ namespace ComSquare::PPU
index += (tileData.horizontalFlip) ? -1 : 1; index += (tileData.horizontalFlip) ? -1 : 1;
pos.x++; pos.x++;
} }
pos.x -= this->_characterSize.x; pos.x -= this->_characterNbPixels.x;
pos.y++; pos.y++;
} }
} }
@@ -105,18 +104,18 @@ namespace ComSquare::PPU
uint8_t Background::getPixelReferenceFromTile(uint16_t tileAddress, uint8_t pixelIndex) uint8_t Background::getPixelReferenceFromTile(uint16_t tileAddress, uint8_t pixelIndex)
{ {
uint8_t row = pixelIndex / this->_characterSize.x; uint8_t row = pixelIndex / this->_characterNbPixels.x;
uint8_t column = pixelIndex % this->_characterSize.y; uint8_t column = pixelIndex % this->_characterNbPixels.y;
if (row >= TILE_PIXEL_HEIGHT) { if (row >= TileNbPixelsHeight) {
tileAddress += 0x80 * this->_bpp; tileAddress += 0x80 * this->_bpp;
row -= TILE_PIXEL_HEIGHT; row -= TileNbPixelsHeight;
} }
if (column >= TILE_PIXEL_WIDTH) { if (column >= TileNbPixelsWidth) {
tileAddress += 0x8 * this->_bpp; tileAddress += 0x8 * this->_bpp;
column -= TILE_PIXEL_WIDTH; column -= TileNbPixelsWidth;
} }
// might not work with 8 bpp must check // TODO might not work with 8 bpp must check
tileAddress += 2 * row; tileAddress += 2 * row;
return this->getPixelReferenceFromTileRow(tileAddress, column); return this->getPixelReferenceFromTileRow(tileAddress, column);
@@ -129,7 +128,7 @@ namespace ComSquare::PPU
uint8_t secondHighByte; uint8_t secondHighByte;
uint8_t secondLowByte; uint8_t secondLowByte;
uint16_t result = 0; uint16_t result = 0;
uint8_t shift = (TILE_PIXEL_WIDTH - 1U - pixelIndex); uint8_t shift = TileNbPixelsWidth - 1U - pixelIndex;
switch (this->_bpp) { switch (this->_bpp) {
case 8: case 8:
@@ -154,11 +153,11 @@ namespace ComSquare::PPU
Vector2<int> pos(0, 0); Vector2<int> pos(0, 0);
uint16_t vramAddress = baseAddress; uint16_t vramAddress = baseAddress;
while (vramAddress < baseAddress + 0x800) { while (vramAddress < baseAddress + TileMapByteSize) {
// TODO function to read 2 bytes (LSB order or bits reversed) // TODO function to read 2 bytes (LSB order or bits reversed)
tileMapValue = this->_vram->read(vramAddress); tileMapValue = this->_vram->read(vramAddress);
tileMapValue += this->_vram->read(vramAddress + 1) << 8U; tileMapValue += this->_vram->read(vramAddress + 1) << 8U;
drawBgTile(tileMapValue, {(pos.x * this->_characterSize.x) + offset.x, (pos.y * this->_characterSize.y) + offset.y}); drawBgTile(tileMapValue, {(pos.x * this->_characterNbPixels.x) + offset.x, (pos.y * this->_characterNbPixels.y) + offset.y});
vramAddress += 2; vramAddress += 2;
if (pos.x % 31 == 0 && pos.x) { if (pos.x % 31 == 0 && pos.x) {
pos.y++; pos.y++;
@@ -181,7 +180,7 @@ namespace ComSquare::PPU
void Background::setCharacterSize(Vector2<int> size) void Background::setCharacterSize(Vector2<int> size)
{ {
this->_characterSize = size; this->_characterNbPixels = size;
} }
void Background::setBpp(int bpp) void Background::setBpp(int bpp)
@@ -194,7 +193,7 @@ namespace ComSquare::PPU
void Background::setTilemaps(Vector2<int> tileMaps) void Background::setTilemaps(Vector2<int> tileMaps)
{ {
this->_tileMaps = tileMaps; this->_tileMapsConfig = tileMaps;
} }
void Background::setBgNumber(int bgNumber) void Background::setBgNumber(int bgNumber)

View File

@@ -19,26 +19,48 @@ namespace ComSquare::PPU
{ {
class PPU; class PPU;
class Background { class Background {
#define NB_CHARACTER_WIDTH 32 //! @brief The number of character a TileMap has in width
#define NB_CHARACTER_HEIGHT 32 static constexpr int NbCharacterWidth = 32;
#define TILE_PIXEL_WIDTH 8U //! @brief The number of character a TileMap has in height
#define TILE_PIXEL_HEIGHT 8U static constexpr int NbCharacterHeight = 32;
#define TILE_SIZE 8 //! @brief The minimum number of pixel a tile can have in width
#define NB_TILE_PER_ROW 16 static constexpr int TileNbPixelsWidth = 8;
//! @brief The minimum number of pixel a tile can have in height
static constexpr int TileNbPixelsHeight = 8;
//! @brief The number of bytes used by a range of pixels (1 pixel per byte)
//! @note Used like: bpp * TileBaseByteSize to get the size of byte of 1 row of pixels
static constexpr unsigned TileBaseByteSize = 8;
//! @brief The number of rows in one line of VRAM
//! @note If you're lost by this description, open a tile viewer in an emulator, and set the number of tiles in width to 16 graphics
static constexpr unsigned NbTilePerRow = 16;
//! @brief The size of a TileMap in memory
static constexpr unsigned short TileMapByteSize = 0x800;
private: private:
Vector2<int> _tileMaps; //! @brief the ppu used to get registers values (ex: bg scroll)
Vector2<int> _characterSize;
int _bpp;
bool _directColor;
bool _highRes;
uint16_t _tileMapStartAddress;
uint16_t _tilesetAddress;
bool _priority;
int _bgNumber;
ComSquare::PPU::PPU &_ppu; ComSquare::PPU::PPU &_ppu;
//! @brief The tilemap configuration nb of tileMap vertically and horizontally
Vector2<int> _tileMapsConfig;
//! @brief The number of pixels of a character (x: width, y:height)
Vector2<int> _characterNbPixels;
//! @brief The number of bits per pixels to currently look for each pixel
int _bpp;
// TODO make better doc for direct color & high res
//! @brief PPU official direct color mode
bool _directColor;
//! @brief PPU offical highRes mode
bool _highRes;
//! @brief The first address of the tilemap data
uint16_t _tileMapStartAddress;
//! @brief The first address for tileset data
uint16_t _tilesetAddress;
//! @brief If pixel from this background should be treated as primarily
bool _priority;
//! @brief The bg number (used to get the corresponding scroll)
int _bgNumber;
//! @brief the access to vram
std::shared_ptr<Ram::Ram> _vram; std::shared_ptr<Ram::Ram> _vram;
//! @brief The access to cgram
std::shared_ptr<Ram::Ram> _cgram; std::shared_ptr<Ram::Ram> _cgram;
//! @brief Draw a tile on the screen at x y pos //! @brief Draw a tile on the screen at x y pos
void drawBgTile(uint16_t data, Vector2<int> pos); void drawBgTile(uint16_t data, Vector2<int> pos);
@@ -61,7 +83,9 @@ namespace ComSquare::PPU
//! @param offset The rendering offeset in pixels //! @param offset The rendering offeset in pixels
void drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset); void drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset);
public: public:
Vector2<int> backgroundSize; //! @brief The size of the background (x, y)
Vector2<unsigned> backgroundSize;
//! @brief The output buffer (pixels are written on it)
std::array<std::array<uint32_t, 1024>, 1024> buffer; std::array<std::array<uint32_t, 1024>, 1024> buffer;
Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority); Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority);
//! @brief Render a background on his internal buffer //! @brief Render a background on his internal buffer