Outline PPU cycles per scanline, get APU cycles

This commit is contained in:
scawful
2023-08-20 22:59:24 -04:00
parent 2ec43cfb3f
commit 2084f2d5a5
4 changed files with 62 additions and 22 deletions

View File

@@ -21,15 +21,23 @@ void APU::Init() {
}
void APU::Reset() {
// Render background layers
// ...
// Reset the clock
ResetAccumulatedTime();
// Render sprites
// Reset the SPC700
// ...
}
void APU::Update() {
// ...
auto cycles_to_run = GetCycleCount();
for (auto i = 0; i < cycles_to_run; ++i) {
// Update the APU
// ...
// Update the SPC700
// ...
}
}
uint8_t APU::ReadRegister(uint16_t address) {

View File

@@ -13,14 +13,6 @@ namespace emu {
PPU::PPU(Memory& memory) : memory_(memory) {}
void PPU::RenderScanline() {
// Render background layers
// ...
// Render sprites
// ...
}
void PPU::Update() {
// Fetch the tile data from VRAM, tile map data from memory, and palette data
// from CGRAM
UpdateTileData(); // Fetches the tile data from VRAM and stores it in an
@@ -54,6 +46,39 @@ void PPU::Update() {
// (e.g., SDL2)
}
void PPU::Update() {
auto cycles_to_run = GetCycleCount();
UpdateInternalState(cycles_to_run);
// Render however many scanlines we're supposed to.
if (currentScanline < visibleScanlines) {
// Render the current scanline
// This involves fetching tile data, applying palette colors, handling
// sprite priorities, etc.
RenderScanline();
// Increment the current scanline
currentScanline++;
}
}
void PPU::UpdateInternalState(int cycles) {
// Update the PPU's internal state based on the number of cycles
cycleCount += cycles;
// Check if it's time to move to the next scanline
if (cycleCount >= cyclesPerScanline) {
currentScanline++;
cycleCount -= cyclesPerScanline;
// If we've reached the end of the frame, reset to the first scanline
if (currentScanline >= totalScanlines) {
currentScanline = 0;
}
}
}
// Reads a byte from the specified PPU register
uint8_t PPU::ReadRegister(uint16_t address) {
switch (address) {

View File

@@ -645,6 +645,7 @@ class PPU : public Clock {
// Runs the PPU for one frame.
void Update();
void UpdateInternalState(int cycles);
// Reads a byte from the specified PPU register
uint8_t ReadRegister(uint16_t address);
@@ -734,6 +735,12 @@ class PPU : public Clock {
// The CGRAM memory area holds the color palette data.
std::array<uint8_t, 512> cgram_;
int cycleCount = 0;
int currentScanline = 0;
const int cyclesPerScanline = 341; // SNES PPU has 341 cycles per scanline
const int totalScanlines = 262; // SNES PPU has 262 scanlines per frame
const int visibleScanlines = 224; // SNES PPU renders 224 visible scanlines
};
} // namespace emu