ComSquare
PPU.hpp
Go to the documentation of this file.
1 //
2 // Created by cbihan on 1/27/20.
3 //
4 
5 #pragma once
6 
7 #include <cstdint>
8 #include "Memory/AMemory.hpp"
9 #include "Memory/MemoryBus.hpp"
10 #include "Renderer/IRenderer.hpp"
11 #include "Ram/Ram.hpp"
12 #include "Models/Vector2.hpp"
13 #include <algorithm>
14 #include "Background.hpp"
15 #include "PPU/PPUUtils.hpp"
16 #include "PPU/PPURegisters.hpp"
17 
18 #ifdef DEBUGGER_ENABLED
20 #endif
21 
22 #define FALLTHROUGH __attribute__((fallthrough));
23 
24 namespace ComSquare::PPU::Utils
25 {
26  struct PpuState;
27 }
28 
29 namespace ComSquare::PPU
30 {
31  static constexpr uint32_t VramSize = 65536;
32  static constexpr uint32_t CGRamSize = 512;
33  static constexpr uint32_t OAMRamSize = 544;
34 
35 
36  class Background;
38  enum BgName {
43  };
44 
46  class PPU : public Memory::AMemory {
47  public:
52  private:
59  std::array<std::array<uint32_t, 1024>, 1024> _mainScreen;
60  std::array<std::array<uint8_t, 1024>, 1024> _mainScreenLevelMap;
62  std::array<std::array<uint8_t, 1024>, 1024> _subScreenLevelMap;
63  std::array<std::array<uint32_t, 1024>, 1024> _subScreen;
65  std::array<std::array<uint32_t, 1024>, 1024> _screen;
67  uint16_t _vramReadBuffer = 0;
70 
71  public:
72 
73  explicit PPU(Renderer::IRenderer &renderer);
74  PPU(const PPU &) = delete;
75  ~PPU() override = default;
76  PPU &operator=(const PPU &) = delete;
77 
82  uint8_t read(uint24_t addr) override;
87  void write(uint24_t addr, uint8_t data) override;
89  [[nodiscard]] std::string getName() const override;
91  [[nodiscard]] Component getComponent() const override;
94  [[nodiscard]] uint24_t getSize() const override;
95 
98  virtual void update(unsigned cycles);
100  [[nodiscard]] uint16_t getVramAddress() const;
102  [[nodiscard]] std::string getValueName(uint24_t addr) const override;
104  uint16_t cgramRead(uint16_t addr);
106  [[nodiscard]] int getBPP(int bgNumber) const;
108  [[nodiscard]] Vector2<int> getCharacterSize(int bgNumber) const;
110  [[nodiscard]] uint16_t getTileMapStartAddress(int bgNumber) const;
112  [[nodiscard]] uint16_t getTilesetAddress(int bgNumber) const;
114  [[nodiscard]] Vector2<bool> getBackgroundMirroring(int bgNumber) const;
116  void renderMainAndSubScreen();
118  template<int levelLow, int levelHigh>
120  {
121  if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) {
122  Background::mergeBackgroundBuffer<levelLow, levelHigh>(this->_mainScreen, this->_mainScreenLevelMap, bg);
123  }
124  if (this->_registers._t[1].raw & (1U << (bg.getBgNumber() - 1U))) {
125  Background::mergeBackgroundBuffer<levelLow, levelHigh>(this->_subScreen, this->_subScreenLevelMap, bg);
126  }
127  }
129  [[nodiscard]] int getBgMode() const;
131  void updateVramReadBuffer();
133  [[nodiscard]] Vector2<int> getBgScroll(int bgNumber) const;
135  [[nodiscard]] const Registers &getWriteRegisters() const;
136  };
137 }
ComSquare::PPU::Background3
@ Background3
Definition: PPU.hpp:41
MemoryBus.hpp
ComSquare::PPU::PPU::write
void write(uint24_t addr, uint8_t data) override
Write data to this component.
Definition: PPU.cpp:82
ComSquare::Ram::Ram
Definition: Ram.hpp:14
ComSquare::Component
Component
Definition: Components.hpp:9
ComSquare::PPU::PPU::oamram
Ram::Ram oamram
Definition: PPU.hpp:50
ComSquare::PPU::PPU::_screen
std::array< std::array< uint32_t, 1024 >, 1024 > _screen
Final Screen buffer.
Definition: PPU.hpp:65
ComSquare::PPU::PPU::getName
std::string getName() const override
Get the name of this accessor (used for debug purpose)
Definition: PPU.cpp:336
ComSquare::PPU::PPU::getSize
uint24_t getSize() const override
Get the size of the data. This size can be lower than the mapped data.
Definition: PPU.cpp:277
Vector2.hpp
ComSquare::PPU::PPU::getBackgroundMirroring
Vector2< bool > getBackgroundMirroring(int bgNumber) const
Tells if the tilemap is expanded for the x and y directions.
Definition: PPU.cpp:545
ComSquare::PPU::Registers::_t
union ComSquare::PPU::Registers::@96 _t[2]
TM - TS Registers (Main & Sub Screen Designation)
ComSquare::PPU::Background::getBgNumber
int getBgNumber() const
Get the BackGround Number.
Definition: Background.cpp:153
ComSquare::PPU::PPU::_registers
Registers _registers
Init ppuRegisters.
Definition: PPU.hpp:54
ComSquare::PPU::PPU::_mainScreenLevelMap
std::array< std::array< uint8_t, 1024 >, 1024 > _mainScreenLevelMap
Definition: PPU.hpp:60
ComSquare::PPU::PPU::PPU
PPU(Renderer::IRenderer &renderer)
Definition: PPU.cpp:18
ComSquare::PPU::CGRamSize
static constexpr uint32_t CGRamSize
Definition: PPU.hpp:32
ComSquare::PPU::PPU::getTilesetAddress
uint16_t getTilesetAddress(int bgNumber) const
Give the address to find the correct tileset for a given x and y.
Definition: PPU.cpp:536
AMemory.hpp
ComSquare::PPU::Background1
@ Background1
Definition: PPU.hpp:39
ComSquare::PPU::Registers
Definition: PPURegisters.hpp:145
ComSquare::PPU::PPU::renderMainAndSubScreen
void renderMainAndSubScreen()
Render the Main and sub screen correctly.
Definition: PPU.cpp:553
RAMTileRenderer.hpp
ComSquare::PPU
Definition: Background.cpp:11
uint24_t
unsigned uint24_t
Definition: Ints.hpp:10
ComSquare::PPU::PPU::_mainScreen
std::array< std::array< uint32_t, 1024 >, 1024 > _mainScreen
Main Screen buffer.
Definition: PPU.hpp:59
ComSquare::PPU::PPU::_subScreenLevelMap
std::array< std::array< uint8_t, 1024 >, 1024 > _subScreenLevelMap
Sub Screen buffer.
Definition: PPU.hpp:62
ComSquare::PPU::PPU::getBPP
int getBPP(int bgNumber) const
get the bpp depending of the bgNumber and the Bgmode
Definition: PPU.cpp:487
ComSquare::PPU::PPU::getBgScroll
Vector2< int > getBgScroll(int bgNumber) const
update the Vram buffer
Definition: PPU.cpp:673
ComSquare::PPU::Background
Definition: Background.hpp:20
ComSquare::PPU::Utils::PpuState
Struct to save all specific variables needed for the registers (prev values for example)
Definition: PPUUtils.hpp:51
PPUUtils.hpp
ComSquare::PPU::BgName
BgName
Enum to access more easily the ppu background array.
Definition: PPU.hpp:38
ComSquare::PPU::OAMRamSize
static constexpr uint32_t OAMRamSize
Definition: PPU.hpp:33
ComSquare::PPU::PPU::updateVramReadBuffer
void updateVramReadBuffer()
update the Vram buffer
Definition: PPU.cpp:667
ComSquare::PPU::PPU::getWriteRegisters
const Registers & getWriteRegisters() const
Allow to look the value of each write register (used by Register debugger)
Definition: PPU.cpp:678
ComSquare::PPU::Registers::raw
uint8_t raw
Definition: PPURegisters.hpp:155
ComSquare::PPU::PPU::_backgrounds
Background _backgrounds[4]
Backgrounds buffers.
Definition: PPU.hpp:57
ComSquare::PPU::PPU::update
virtual void update(unsigned cycles)
Update the PPU of n cycles.
Definition: PPU.cpp:299
ComSquare::PPU::PPU::getBgMode
int getBgMode() const
Get the current background Mode.
Definition: PPU.cpp:662
ComSquare::PPU::PPU::_subScreen
std::array< std::array< uint32_t, 1024 >, 1024 > _subScreen
Definition: PPU.hpp:63
ComSquare::PPU::PPU::cgram
Ram::Ram cgram
Definition: PPU.hpp:51
ComSquare::PPU::Background4
@ Background4
Definition: PPU.hpp:42
ComSquare::PPU::PPU::addToMainSubScreen
void addToMainSubScreen(Background &bg)
Add a bg to the sub and/or main screen.
Definition: PPU.hpp:119
ComSquare::PPU::PPU::getTileMapStartAddress
uint16_t getTileMapStartAddress(int bgNumber) const
Give the address where the tilemap starts.
Definition: PPU.cpp:531
ComSquare::Memory::AMemory
Abstract class representing a continuous block of memory.
Definition: AMemory.hpp:18
ComSquare::Renderer::IRenderer
Definition: IRenderer.hpp:15
ComSquare::PPU::PPU::~PPU
~PPU() override=default
ComSquare::PPU::PPU::cgramRead
uint16_t cgramRead(uint16_t addr)
Allow others components to read the CGRAM.
Definition: PPU.cpp:482
ComSquare::PPU::PPU::getValueName
std::string getValueName(uint24_t addr) const override
Give the name of the Address register (used for debug)
Definition: PPU.cpp:341
ComSquare::PPU::PPU::_renderer
Renderer::IRenderer & _renderer
Definition: PPU.hpp:55
ComSquare::PPU::PPU::getCharacterSize
Vector2< int > getCharacterSize(int bgNumber) const
Give the correct character size depending of the bgMode.
Definition: PPU.cpp:521
Background.hpp
ComSquare::PPU::PPU::vram
Ram::Ram vram
Rams.
Definition: PPU.hpp:49
PPURegisters.hpp
ComSquare::PPU::PPU::operator=
PPU & operator=(const PPU &)=delete
ComSquare::PPU::VramSize
static constexpr uint32_t VramSize
Definition: PPU.hpp:31
ComSquare::Vector2< int >
ComSquare::PPU::PPU::getVramAddress
uint16_t getVramAddress() const
Give the Vram Address with the right Address remapping.
Definition: PPU.cpp:282
ComSquare::PPU::PPU::read
uint8_t read(uint24_t addr) override
Read data from the component.
Definition: PPU.cpp:37
ComSquare::PPU::PPU::_vramReadBuffer
uint16_t _vramReadBuffer
Used for vram read registers (0x2139 - 0x213A)
Definition: PPU.hpp:67
Ram.hpp
ComSquare::PPU::PPU::getComponent
Component getComponent() const override
Get the component of this accessor (used for debug purpose)
Definition: PPU.cpp:477
ComSquare::PPU::Utils
Definition: PPU.cpp:12
ComSquare::PPU::Background2
@ Background2
Definition: PPU.hpp:40
IRenderer.hpp
ComSquare::PPU::PPU
The class containing all the registers of the PPU.
Definition: PPU.hpp:46
ComSquare::PPU::PPU::_ppuState
struct Utils::PpuState _ppuState
Struct that contain all necessary vars for the use of the registers.
Definition: PPU.hpp:69