From ac7d8b6af9d39dbc99012f978ff4c2d4044629ca Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 20 Aug 2023 16:15:18 -0400 Subject: [PATCH] Add BackgroundLayer to PPU, Create Loggable class --- src/app/emu/cpu.h | 8 ++++++-- src/app/emu/log.h | 43 +++++++++++++++++++++++++++++++++++++++++ src/app/emu/mem.h | 4 +++- src/app/emu/ppu.cc | 12 ++++-------- src/app/emu/ppu.h | 47 ++++++++++++++++++++++++++++++++++----------- src/app/emu/snes.cc | 5 ----- 6 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 src/app/emu/log.h diff --git a/src/app/emu/cpu.h b/src/app/emu/cpu.h index 78003e82..b8a4baad 100644 --- a/src/app/emu/cpu.h +++ b/src/app/emu/cpu.h @@ -5,6 +5,7 @@ #include #include +#include "app/emu/log.h" #include "app/emu/mem.h" namespace yaze { @@ -27,10 +28,13 @@ class Clock { unsigned long long cycleCount; // Total number of cycles executed }; -class CPU : public Memory, public Clock { +class CPU : public Memory, public Clock, public Loggable { public: explicit CPU(Memory& mem) : memory(mem) {} - void Init() { memory.ClearMemory(); } + void Init() { + + memory.ClearMemory(); + } uint8_t ReadByte(uint16_t address) const override; uint16_t ReadWord(uint16_t address) const override; diff --git a/src/app/emu/log.h b/src/app/emu/log.h new file mode 100644 index 00000000..92c7649f --- /dev/null +++ b/src/app/emu/log.h @@ -0,0 +1,43 @@ +#ifndef YAZE_APP_EMU_LOG_H_ +#define YAZE_APP_EMU_LOG_H_ + +#include +#include + +namespace yaze { +namespace app { +namespace emu { + +// Logger.h +class Logger { + public: + static Logger& GetInstance() { + static Logger instance; + return instance; + } + + void Log(const std::string& message) const { + // Write log messages to a file or console + std::cout << message << std::endl; + } + + private: + Logger() = default; + Logger(const Logger&) = delete; + Logger& operator=(const Logger&) = delete; +}; + +// Loggable.h +class Loggable { + protected: + Logger& logger_ = Logger::GetInstance(); + + virtual ~Loggable() = default; + virtual void LogMessage(const std::string& message) { logger_.Log(message); } +}; + +} // namespace emu +} // namespace app +} // namespace yaze + +#endif // YAZE_APP_EMU_LOG_H_ \ No newline at end of file diff --git a/src/app/emu/mem.h b/src/app/emu/mem.h index 00a94269..a30c6ebe 100644 --- a/src/app/emu/mem.h +++ b/src/app/emu/mem.h @@ -5,6 +5,8 @@ #include #include +#include "app/emu/log.h" + // LoROM (Mode 20): // Banks Offset Purpose @@ -138,7 +140,7 @@ class Memory { virtual uint8_t at(int i) const = 0; }; -class MemoryImpl : public Memory { +class MemoryImpl : public Memory, public Loggable { public: void Initialize(const std::vector& romData) { const size_t ROM_CHUNK_SIZE = 0x8000; // 32 KB diff --git a/src/app/emu/ppu.cc b/src/app/emu/ppu.cc index b5c79657..3926c652 100644 --- a/src/app/emu/ppu.cc +++ b/src/app/emu/ppu.cc @@ -140,7 +140,7 @@ void PPU::RenderSprites() { } uint32_t PPU::GetPaletteColor(uint8_t colorIndex) { - // ... + return memory_.ReadWordLong(colorIndex); } uint8_t PPU::ReadVRAM(uint16_t address) { @@ -151,17 +151,13 @@ void PPU::WriteVRAM(uint16_t address, uint8_t value) { // ... } -uint8_t PPU::ReadOAM(uint16_t address) { - // ... -} +uint8_t PPU::ReadOAM(uint16_t address) { return memory_.ReadByte(address); } void PPU::WriteOAM(uint16_t address, uint8_t value) { // ... } -uint8_t PPU::ReadCGRAM(uint16_t address) { - // ... -} +uint8_t PPU::ReadCGRAM(uint16_t address) { return memory_.ReadByte(address); } void PPU::WriteCGRAM(uint16_t address, uint8_t value) { // ... @@ -198,7 +194,7 @@ void PPU::UpdateTileData() { // Update the sprites based on the fetched tile data for (uint16_t spriteIndex = 0; spriteIndex < sprites_.size(); ++spriteIndex) { uint16_t spriteAddress = - oamBaseAddress_ + spriteIndex * sizeof(SpriteAttributes); + oam_address_ + spriteIndex * sizeof(SpriteAttributes); uint16_t spriteData = memory_.ReadWord( spriteAddress); // Assume ReadWord reads a 16-bit value from VRAM diff --git a/src/app/emu/ppu.h b/src/app/emu/ppu.h index 59d6ffaa..f78ae641 100644 --- a/src/app/emu/ppu.h +++ b/src/app/emu/ppu.h @@ -1,6 +1,7 @@ #ifndef YAZE_APP_EMU_PPU_H #define YAZE_APP_EMU_PPU_H +#include #include #include @@ -600,6 +601,30 @@ struct WramAccessRegisters { uint32_t address; // Register $2181/$2182/$2183 }; +struct Tile { + uint16_t index; // Index of the tile in VRAM + uint8_t palette; // Palette number used for this tile + bool flip_x; // Horizontal flip flag + bool flip_y; // Vertical flip flag + uint8_t priority; // Priority of this tile +}; + +struct BackgroundLayer { + enum class Size { SIZE_32x32, SIZE_64x32, SIZE_32x64, SIZE_64x64 }; + + enum class ColorDepth { BPP_2, BPP_4, BPP_8 }; + + Size size; // Size of the background layer + ColorDepth color_depth; // Color depth of the background layer + std::vector tilemap; // Tilemap data + std::vector tile_data; // Tile data in VRAM + uint16_t tilemap_base_address; // Base address of the tilemap in VRAM + uint16_t tile_data_base_address; // Base address of the tile data in VRAM + uint8_t scroll_x; // Horizontal scroll offset + uint8_t scroll_y; // Vertical scroll offset + bool enabled; // Whether the background layer is enabled +}; + class PPU { public: // Initializes the PPU with the necessary resources and dependencies @@ -686,6 +711,17 @@ class PPU { Memory& memory_; std::vector frame_buffer_; + Tilemap tilemap_; + BackgroundMode bg_mode_; + std::array bg_layers_; + std::vector sprites_; + std::vector tileData_; + + uint16_t oam_address_; + uint16_t tileDataSize_; + uint16_t vramBaseAddress_; + uint16_t tilemapBaseAddress_; + // The VRAM memory area holds tiles and tile maps. std::array vram_; @@ -694,17 +730,6 @@ class PPU { // The CGRAM memory area holds the color palette data. std::array cgram_; - - uint16_t oam_address_; - - BackgroundMode bg_mode_; - std::vector sprites_; - std::vector tileData_; - Tilemap tilemap_; - uint16_t tileDataSize_; - uint16_t oamBaseAddress_; - uint16_t vramBaseAddress_; - uint16_t tilemapBaseAddress_; }; } // namespace emu diff --git a/src/app/emu/snes.cc b/src/app/emu/snes.cc index c5c1538f..7a739d4f 100644 --- a/src/app/emu/snes.cc +++ b/src/app/emu/snes.cc @@ -251,11 +251,6 @@ void SNES::Init(ROM& rom) { // Misc memory_.WriteByte(0x2133, 0x00); // SETINI - // Load ROM data into memory - // TODO: Load memory based on memory mapping and ROM format. - memory_.SetMemory(rom.vector()); - - // Initialize other private member variables running_ = true; scanline = 0; }