Merge branch 'PPU' into PPUDebugger

This commit is contained in:
Clément Le Bihan
2021-05-23 23:21:37 +02:00
6 changed files with 3820 additions and 111 deletions
+1
View File
@@ -244,6 +244,7 @@ add_executable(ComSquare
include_directories(ComSquare sources)
target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED)
include_directories(ComSquare sources)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
+39 -39
View File
@@ -6,43 +6,43 @@
#include "PPU.hpp"
#include "Background.hpp"
#include <cmath>
#include "../Models/Vector2.hpp"
#include "Models/Vector2.hpp"
namespace ComSquare::PPU
{
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),
_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;
}
void Background::renderBackground()
{
uint16_t vramAddress = this->_tileMapStartAddress;
Vector2<int> offset = this->_ppu.getBgScroll(this->_bgNumber);
this->backgroundSize.x = this->_tileMaps.x * this->_characterSize.x * NB_CHARACTER_WIDTH;
this->backgroundSize.y = this->_tileMaps.y * this->_characterSize.y * NB_CHARACTER_HEIGHT;
this->backgroundSize.x = this->_tileMapsConfig.x * this->_characterNbPixels.x * NbCharacterWidth;
this->backgroundSize.y = this->_tileMapsConfig.y * this->_characterNbPixels.y * NbCharacterHeight;
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);
}
vramAddress += 0x800;
offset.x += NB_CHARACTER_WIDTH * this->_characterSize.x;
vramAddress += TileMapByteSize;
offset.x += NbCharacterWidth * this->_characterNbPixels.x;
if (i == 2) {
offset.x = 0;
offset.y += NB_CHARACTER_HEIGHT * this->_characterSize.y;
offset.y += NbCharacterHeight * this->_characterNbPixels.y;
}
}
}
@@ -60,14 +60,14 @@ namespace ComSquare::PPU
palette = getPalette(tileData.palette);
// X horizontal
// Y vertical
graphicAddress = this->_tilesetAddress + (tileData.posY * NB_TILE_PER_ROW * this->_bpp * TILE_SIZE) + (tileData.posX * this->_bpp * TILE_SIZE);
for (int i = 0; i < this->_characterSize.y; i++) {
index = i * this->_characterSize.x;
graphicAddress = this->_tilesetAddress + (tileData.posY * NbTilePerRow * this->_bpp * TileBaseByteSize) + (tileData.posX * this->_bpp * TileBaseByteSize);
for (int i = 0; i < this->_characterNbPixels.y; i++) {
index = i * this->_characterNbPixels.x;
if (tileData.verticalFlip)
index = (this->_characterSize.y - 1 - i) * this->_characterSize.x;
index = (this->_characterNbPixels.y - 1 - i) * this->_characterNbPixels.x;
if (tileData.horizontalFlip)
index += this->_characterSize.x - 1;
for (int j = 0; j < this->_characterSize.x; j++) {
index += this->_characterNbPixels.x - 1;
for (int j = 0; j < this->_characterNbPixels.x; j++) {
reference = getPixelReferenceFromTile(graphicAddress, index);
color = getRealColor(palette[reference]);
if (tileData.tilePriority == this->_priority) // reference 0 is considered as transparency
@@ -75,7 +75,7 @@ namespace ComSquare::PPU
index += (tileData.horizontalFlip) ? -1 : 1;
pos.x++;
}
pos.x -= this->_characterSize.x;
pos.x -= this->_characterNbPixels.x;
pos.y++;
}
}
@@ -104,18 +104,18 @@ namespace ComSquare::PPU
uint8_t Background::getPixelReferenceFromTile(uint16_t tileAddress, uint8_t pixelIndex)
{
uint8_t row = pixelIndex / this->_characterSize.x;
uint8_t column = pixelIndex % this->_characterSize.y;
uint8_t row = pixelIndex / this->_characterNbPixels.x;
uint8_t column = pixelIndex % this->_characterNbPixels.y;
if (row >= TILE_PIXEL_HEIGHT) {
if (row >= TileNbPixelsHeight) {
tileAddress += 0x80 * this->_bpp;
row -= TILE_PIXEL_HEIGHT;
row -= TileNbPixelsHeight;
}
if (column >= TILE_PIXEL_WIDTH) {
if (column >= TileNbPixelsWidth) {
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;
return this->getPixelReferenceFromTileRow(tileAddress, column);
@@ -128,7 +128,7 @@ namespace ComSquare::PPU
uint8_t secondHighByte;
uint8_t secondLowByte;
uint16_t result = 0;
uint8_t shift = (TILE_PIXEL_WIDTH - 1U - pixelIndex);
uint8_t shift = TileNbPixelsWidth - 1U - pixelIndex;
switch (this->_bpp) {
case 8:
@@ -138,7 +138,7 @@ namespace ComSquare::PPU
secondLowByte = this->_vram->read((tileAddress + 17) % VRAMSIZE);
result = ((secondHighByte & (1U << shift)) | ((secondLowByte & (1U << shift)) << 1U));
result = (shift - 2 >= 0) ? result >> (shift - 2) : result << ((shift - 2) * -1);
__attribute__((fallthrough));
FALLTHROUGH
case 2:
result += ((highByte & (1U << shift)) | ((lowByte & (1U << shift)) << 1U)) >> shift;
default:
@@ -150,14 +150,14 @@ namespace ComSquare::PPU
void Background::drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset)
{
uint16_t tileMapValue = 0;
Vector2<int> pos(0,0);
Vector2<int> pos(0, 0);
uint16_t vramAddress = baseAddress;
while (vramAddress < baseAddress + 0x800) {
while (vramAddress < baseAddress + TileMapByteSize) {
// TODO function to read 2 bytes (LSB order or bits reversed)
tileMapValue = this->_vram->read(vramAddress);
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;
if (pos.x % 31 == 0 && pos.x) {
pos.y++;
@@ -180,7 +180,7 @@ namespace ComSquare::PPU
void Background::setCharacterSize(Vector2<int> size)
{
this->_characterSize = size;
this->_characterNbPixels = size;
}
void Background::setBpp(int bpp)
@@ -193,7 +193,7 @@ namespace ComSquare::PPU
void Background::setTilemaps(Vector2<int> tileMaps)
{
this->_tileMaps = tileMaps;
this->_tileMapsConfig = tileMaps;
}
void Background::setBgNumber(int bgNumber)
+53 -19
View File
@@ -19,26 +19,49 @@ namespace ComSquare::PPU
{
class PPU;
class Background {
#define NB_CHARACTER_WIDTH 32
#define NB_CHARACTER_HEIGHT 32
#define TILE_PIXEL_WIDTH 8U
#define TILE_PIXEL_HEIGHT 8U
#define TILE_SIZE 8
#define NB_TILE_PER_ROW 16
private:
Vector2<int> _tileMaps;
Vector2<int> _characterSize;
int _bpp;
bool _directColor;
bool _highRes;
uint16_t _tileMapStartAddress;
uint16_t _tilesetAddress;
bool _priority;
int _bgNumber;
ComSquare::PPU::PPU &_ppu;
//! @brief The number of character a TileMap has in width
static constexpr int NbCharacterWidth = 32;
//! @brief The number of character a TileMap has in height
static constexpr int NbCharacterHeight = 32;
//! @brief The minimum number of pixel a tile can have in width
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;
//! @brief the ppu used to get registers values (ex: bg scroll)
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;
//! @brief The access to cgram
std::shared_ptr<Ram::Ram> _cgram;
//! @brief Draw a tile on the screen at x y pos
void drawBgTile(uint16_t data, Vector2<int> pos);
@@ -61,9 +84,11 @@ namespace ComSquare::PPU
//! @param offset The rendering offeset in pixels
void drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset);
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;
Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority);
//! @brief Render a background on his internal buffer
void renderBackground();
//! @brief Set the tileMap start address
@@ -92,6 +117,15 @@ namespace ComSquare::PPU
//! @brief Get the Background priority
//! @return the current Background priority
bool getPriority() const;
//! @brief ctor
Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority);
//! @brief Default copy ctor
Background(const Background &) = default;
//! @brief Default destructor
~Background() = default;
//! @brief Delete assignment operator
Background &operator=(const Background &) = delete;
};
}
+19 -18
View File
@@ -27,15 +27,14 @@ namespace ComSquare::PPU
Background(*this, 3, true),
Background(*this, 4, false),
Background(*this, 4, true)
}
},
_mainScreen({{{0}}}),
_subScreen({{{0}}})
{
this->_registers._isLowByte = true;
/*for (int i = 0; i < 512; i++) {
this->cgram->write(i, random() % 255);
*/
//colors for the cgram
/* this->cgram->write(2, 0xE0);
this->cgram->write(2, 0xE0);
this->cgram->write(3, 0x7F);
this->cgram->write(4, 0x1F); // 0x1F
this->cgram->write(6, 0xFF);
@@ -75,17 +74,17 @@ namespace ComSquare::PPU
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
}; */
int *cgram_test = get_dump_cgram();
};
/*int *cgram_test = get_dump_cgram();
for (int i = 0; cgram_test[i] != -1; i++) {
this->cgram->write(i, cgram_test[i]);
}
}*/
int *vram_test = get_dump_vram();
// int *vram_test = get_dump_vram();
for (int i = 0; vram_test[i] != -1; i++) {
this->vram->write(i, vram_test[i]);
}
/* int vram_test_2[] = {8, 00, 02, 00, 0x0A, 00, 02, 00, 0x0A, 00, 00, 00, 00, 00, 00, -1};
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_2[i] != -1; i++) {
this->vram->write(i + 0x8000, vram_test_2[i]);
}
@@ -143,9 +142,10 @@ namespace ComSquare::PPU
//this->_registers._bgofs[2].raw = 0x03E0;
//this->_registers._bgofs[3].raw = 0x03DF;
this->_registers._t[0].enableWindowDisplayBg1 = true;
this->_registers._t[0].enableWindowDisplayBg2 = true; */
this->_registers._t[0].enableWindowDisplayBg2 = true;
//registers
/*
//registers aladin
this->_registers._bgmode.bgMode = 1;
this->_backgrounds[0].setBpp(this->getBPP(1));
@@ -199,7 +199,7 @@ namespace ComSquare::PPU
this->_registers._t[0].enableWindowDisplayBg2 = true;
this->_registers._t[0].enableWindowDisplayBg3 = true;
*/
}
uint8_t PPU::read(uint24_t addr)
@@ -300,7 +300,7 @@ namespace ComSquare::PPU
case PpuRegisters::bg1hofs:
// TODO need of special var for prev value for Mode 7
this->_registers._m7ofs[addr - PpuRegisters::bg1hofs].raw = data;
__attribute__((fallthrough));
FALLTHROUGH
case PpuRegisters::bg2hofs:
case PpuRegisters::bg3hofs:
case PpuRegisters::bg4hofs:
@@ -311,7 +311,7 @@ namespace ComSquare::PPU
case PpuRegisters::bg1vofs:
// TODO need of special var for prev value for Mode 7
this->_registers._bgnba[addr - PpuRegisters::bg12nba].raw = data;
__attribute__((fallthrough));
FALLTHROUGH
case PpuRegisters::bg2vofs:
case PpuRegisters::bg3vofs:
case PpuRegisters::bg4vofs:
@@ -665,7 +665,7 @@ namespace ComSquare::PPU
return 8;
return 7;
default:
return -1;
throw std::runtime_error("Invalid Background number");
}
}
@@ -796,10 +796,11 @@ namespace ComSquare::PPU
//sprites priority 1
//sprites priority 2
this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]);
//sprites priority 3
break;
//sprites priority
break;
case 7:
// Not implemented
throw std::runtime_error("not implemented");
default:
break;
}
+8 -9
View File
@@ -14,10 +14,9 @@
#include "Background.hpp"
#include "PPUUtils.hpp"
//#define max2BitTiles 4096
//#define max4BitTiles 2048
//#define max8BitTiles 1024
#define FALLTHROUGH __attribute__((fallthrough));
// TODO check if it usefull to have defines instead of constepxr
#define VRAMSIZE 65536
#define CGRAMSIZE 512
#define OAMRAMSIZE 544
@@ -25,6 +24,7 @@
namespace ComSquare::PPU
{
class Background;
//! @brief Enum to access more easily the ppu background array
enum BgName {
bg1NoPriority = 0,
bg1Priority,
@@ -573,8 +573,8 @@ namespace ComSquare::PPU
explicit PPU(Renderer::IRenderer &renderer);
PPU(const PPU &) = delete;
PPU &operator=(const PPU &) = delete;
~PPU() override = default;
PPU &operator=(const PPU &) = delete;
//! @brief Read data from the component.
//! @param addr The local address to read from (0x0 should refer to the first byte of this component).
@@ -632,10 +632,9 @@ namespace ComSquare::PPU
const Registers &getWriteRegisters() const;
};
//! @brief Transform SNES color code BGR to uint32_t RGB
uint32_t getRealColor(uint16_t color);
int *get_dump_vram();
int *get_dump_cgram();
}
//! @brief Transform SNES color code BGR to uint32_t RGB
uint32_t getRealColor(uint16_t color);
int *get_dump_vram();
int *get_dump_cgram();
#endif //COMSQUARE_PPU_HPP
+3700 -26
View File
File diff suppressed because one or more lines are too long