Add BackgroundLayer to PPU, Create Loggable class

This commit is contained in:
scawful
2023-08-20 16:15:18 -04:00
parent 7448f80119
commit ac7d8b6af9
6 changed files with 92 additions and 27 deletions

View File

@@ -5,6 +5,7 @@
#include <iostream>
#include <vector>
#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;

43
src/app/emu/log.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef YAZE_APP_EMU_LOG_H_
#define YAZE_APP_EMU_LOG_H_
#include <iostream>
#include <string>
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_

View File

@@ -5,6 +5,8 @@
#include <iostream>
#include <vector>
#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<uint8_t>& romData) {
const size_t ROM_CHUNK_SIZE = 0x8000; // 32 KB

View File

@@ -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

View File

@@ -1,6 +1,7 @@
#ifndef YAZE_APP_EMU_PPU_H
#define YAZE_APP_EMU_PPU_H
#include <array>
#include <cstdint>
#include <vector>
@@ -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<Tile> tilemap; // Tilemap data
std::vector<uint8_t> 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<uint8_t> frame_buffer_;
Tilemap tilemap_;
BackgroundMode bg_mode_;
std::array<BackgroundLayer, 4> bg_layers_;
std::vector<SpriteAttributes> sprites_;
std::vector<uint8_t> 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<uint8_t, 64 * 1024> vram_;
@@ -694,17 +730,6 @@ class PPU {
// The CGRAM memory area holds the color palette data.
std::array<uint8_t, 512> cgram_;
uint16_t oam_address_;
BackgroundMode bg_mode_;
std::vector<SpriteAttributes> sprites_;
std::vector<uint8_t> tileData_;
Tilemap tilemap_;
uint16_t tileDataSize_;
uint16_t oamBaseAddress_;
uint16_t vramBaseAddress_;
uint16_t tilemapBaseAddress_;
};
} // namespace emu

View File

@@ -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;
}