|
|
@ -5,7 +5,7 @@ |
|
|
|
* SPDX-License-Identifier: MIT |
|
|
|
* SPDX-License-Identifier: MIT |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#include <cstdint> // uint32_t |
|
|
|
#include <cstdint> // uint8_t, uint16_t, uint32_t |
|
|
|
#include <memory> // std::make_shared |
|
|
|
#include <memory> // std::make_shared |
|
|
|
|
|
|
|
|
|
|
|
#include "glm/ext/vector_float3.hpp" // glm::vec3 |
|
|
|
#include "glm/ext/vector_float3.hpp" // glm::vec3 |
|
|
@ -56,7 +56,7 @@ void PPU::update() |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case State::PixelTransfer: |
|
|
|
case State::PixelTransfer: |
|
|
|
pixelFifo(); |
|
|
|
updatePixelFifo(); |
|
|
|
|
|
|
|
|
|
|
|
if (m_lcd_x_coordinate == 160) { |
|
|
|
if (m_lcd_x_coordinate == 160) { |
|
|
|
m_lcd_x_coordinate = 0; |
|
|
|
m_lcd_x_coordinate = 0; |
|
|
@ -112,31 +112,31 @@ void PPU::render() |
|
|
|
scene.addComponent<Inferno::SpriteComponent>(m_entity, glm::vec4 { 1.0f }, texture); |
|
|
|
scene.addComponent<Inferno::SpriteComponent>(m_entity, glm::vec4 { 1.0f }, texture); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PPU::pixelFifo() |
|
|
|
void PPU::resetFrame() |
|
|
|
{ |
|
|
|
{ |
|
|
|
LCDC lcd_control = static_cast<LCDC>(Emu::the().readMemory(0xff40)); |
|
|
|
m_state = State::OAMSearch; |
|
|
|
|
|
|
|
m_clocks_into_frame = 0; |
|
|
|
// Tile map
|
|
|
|
m_lcd_x_coordinate = 0; |
|
|
|
uint32_t bg_tile_map_address = (lcd_control & LCDC::BGTileMapArea) ? 0x9c00 : 0x9800; |
|
|
|
m_lcd_y_coordinate = 0; |
|
|
|
// uint32_t window_tile_map_address = (lcd_control & LCDC::WindowTileMapArea) ? 0x9c00 : 0x9800;
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tile data
|
|
|
|
// -----------------------------------------
|
|
|
|
uint32_t tile_data_address = (lcd_control & LCDC::BGandWindowTileDataArea) ? 0x8000 : 0x8800; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t PPU::getBgTileDataAddress(uint8_t tile_index) |
|
|
|
|
|
|
|
{ |
|
|
|
// https://gbdev.io/pandocs/Tile_Data.html
|
|
|
|
// https://gbdev.io/pandocs/Tile_Data.html
|
|
|
|
auto getBgTileDataAddress = [&](uint8_t tile_index) -> uint32_t { |
|
|
|
switch (m_pixel_fifo.tile_data_address) { |
|
|
|
switch (tile_data_address) { |
|
|
|
|
|
|
|
case 0x8000: |
|
|
|
case 0x8000: |
|
|
|
// 0x8000-0x8fff: index 0 => 255
|
|
|
|
// 0x8000-0x8fff: index 0 => 255
|
|
|
|
return tile_data_address + (tile_index * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
return m_pixel_fifo.tile_data_address + (tile_index * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
case 0x8800: |
|
|
|
case 0x8800: |
|
|
|
// 0x8800-0x8fff: index 128 => 255 (or -128 => -1)
|
|
|
|
// 0x8800-0x8fff: index 128 => 255 (or -128 => -1)
|
|
|
|
// 0x9000-0x97ff: index 0 => 127
|
|
|
|
// 0x9000-0x97ff: index 0 => 127
|
|
|
|
if (tile_index <= 127) { |
|
|
|
if (tile_index <= 127) { |
|
|
|
return tile_data_address + 0x800 + (tile_index * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
return m_pixel_fifo.tile_data_address + 0x800 + (tile_index * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
return tile_data_address + ((tile_index - 128) * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
return m_pixel_fifo.tile_data_address + ((tile_index - 128) * TILE_SIZE); // Each tile is 16 bytes
|
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
VERIFY_NOT_REACHED(); |
|
|
@ -144,11 +144,63 @@ void PPU::pixelFifo() |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
std::array<uint8_t, 3> PPU::getPixelColor(uint8_t color_index, Palette palette) |
|
|
|
// FIFO Pixel Fetcher
|
|
|
|
{ |
|
|
|
|
|
|
|
VERIFY(color_index < 4, "trying to fetch invalid color index '{}'", color_index); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (Emu::the().mode()) { |
|
|
|
|
|
|
|
case Emu::Mode::DMG: { |
|
|
|
|
|
|
|
uint8_t palette_data = Emu::the().readMemory(palette) & 0xff; |
|
|
|
|
|
|
|
uint8_t palette_value = palette_data >> (color_index * 2) & 0x3; |
|
|
|
|
|
|
|
switch (palette_value) { |
|
|
|
|
|
|
|
case 0: |
|
|
|
|
|
|
|
return { 200, 199, 168 }; |
|
|
|
|
|
|
|
case 1: |
|
|
|
|
|
|
|
return { 160, 160, 136 }; |
|
|
|
|
|
|
|
case 2: |
|
|
|
|
|
|
|
return { 104, 104, 80 }; |
|
|
|
|
|
|
|
case 3: |
|
|
|
|
|
|
|
return { 39, 40, 24 }; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case Emu::Mode::CGB: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PPU::updatePixelFifo() |
|
|
|
|
|
|
|
{ |
|
|
|
switch (m_pixel_fifo.state) { |
|
|
|
switch (m_pixel_fifo.state) { |
|
|
|
case PixelFifo::State::TileIndex: |
|
|
|
case PixelFifo::State::TileIndex: |
|
|
|
|
|
|
|
tileIndex(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case PixelFifo::State::TileDataLow: |
|
|
|
|
|
|
|
tileDataLow(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case PixelFifo::State::TileDataHigh: |
|
|
|
|
|
|
|
tileDataHigh(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case PixelFifo::State::Sleep: |
|
|
|
|
|
|
|
sleep(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case PixelFifo::State::Push: |
|
|
|
|
|
|
|
pushFifo(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pushPixel(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PPU::tileIndex() |
|
|
|
|
|
|
|
{ |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
} |
|
|
|
} |
|
|
@ -156,22 +208,33 @@ void PPU::pixelFifo() |
|
|
|
m_pixel_fifo.step = false; |
|
|
|
m_pixel_fifo.step = false; |
|
|
|
m_pixel_fifo.state = PixelFifo::State::TileDataLow; |
|
|
|
m_pixel_fifo.state = PixelFifo::State::TileDataLow; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LCDC lcd_control = static_cast<LCDC>(Emu::the().readMemory(0xff40)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tile map
|
|
|
|
|
|
|
|
uint32_t bg_tile_map_address = (lcd_control & LCDC::BGTileMapArea) ? 0x9c00 : 0x9800; |
|
|
|
|
|
|
|
// uint32_t window_tile_map_address = (lcd_control & LCDC::WindowTileMapArea) ? 0x9c00 : 0x9800;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tile data
|
|
|
|
|
|
|
|
m_pixel_fifo.tile_data_address = (lcd_control & LCDC::BGandWindowTileDataArea) ? 0x8000 : 0x8800; |
|
|
|
|
|
|
|
|
|
|
|
// Viewport
|
|
|
|
// Viewport
|
|
|
|
// https://gbdev.io/pandocs/Scrolling.html#mid-frame-behavior
|
|
|
|
// https://gbdev.io/pandocs/Scrolling.html#mid-frame-behavior
|
|
|
|
m_viewport_x = Emu::the().readMemory(0xff43); // TODO: only read lower 3-bits at beginning of scanline
|
|
|
|
m_pixel_fifo.viewport_x = Emu::the().readMemory(0xff43); // TODO: only read lower 3-bits at beginning of scanline
|
|
|
|
m_viewport_y = Emu::the().readMemory(0xff42); |
|
|
|
m_pixel_fifo.viewport_y = Emu::the().readMemory(0xff42); |
|
|
|
|
|
|
|
|
|
|
|
// Read the tile map index
|
|
|
|
// Read the tile map index
|
|
|
|
uint16_t offset = (((m_viewport_y + m_lcd_y_coordinate) / TILE_HEIGHT) * 32) |
|
|
|
uint16_t offset = (((m_pixel_fifo.viewport_y + m_lcd_y_coordinate) / TILE_HEIGHT) * 32) |
|
|
|
+ ((m_viewport_x + m_pixel_fifo.x_coordinate) / TILE_WIDTH); |
|
|
|
+ ((m_pixel_fifo.viewport_x + m_pixel_fifo.x_coordinate) / TILE_WIDTH); |
|
|
|
m_pixel_fifo.x_coordinate += 8; |
|
|
|
m_pixel_fifo.x_coordinate += 8; |
|
|
|
m_pixel_fifo.tile_index = Emu::the().readMemory(bg_tile_map_address + offset) & 0xff; |
|
|
|
m_pixel_fifo.tile_index = Emu::the().readMemory(bg_tile_map_address + offset); |
|
|
|
|
|
|
|
|
|
|
|
// Set the tile line we're currently on
|
|
|
|
// Set the tile line we're currently on
|
|
|
|
m_pixel_fifo.tile_line = (m_viewport_y + m_lcd_y_coordinate) % TILE_HEIGHT; |
|
|
|
m_pixel_fifo.tile_line = (m_pixel_fifo.viewport_y + m_lcd_y_coordinate) % TILE_HEIGHT; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case PixelFifo::State::TileDataLow: |
|
|
|
|
|
|
|
|
|
|
|
void PPU::tileDataLow() |
|
|
|
|
|
|
|
{ |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
} |
|
|
|
} |
|
|
@ -184,8 +247,10 @@ void PPU::pixelFifo() |
|
|
|
getBgTileDataAddress(m_pixel_fifo.tile_index) |
|
|
|
getBgTileDataAddress(m_pixel_fifo.tile_index) |
|
|
|
+ m_pixel_fifo.tile_line * 2); // Each tile line is 2 bytes
|
|
|
|
+ m_pixel_fifo.tile_line * 2); // Each tile line is 2 bytes
|
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case PixelFifo::State::TileDataHigh: |
|
|
|
|
|
|
|
|
|
|
|
void PPU::tileDataHigh() |
|
|
|
|
|
|
|
{ |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
if (!m_pixel_fifo.step) { |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
m_pixel_fifo.step = true; |
|
|
|
} |
|
|
|
} |
|
|
@ -199,29 +264,29 @@ void PPU::pixelFifo() |
|
|
|
+ m_pixel_fifo.tile_line * 2 // Each tile line is 2 bytes
|
|
|
|
+ m_pixel_fifo.tile_line * 2 // Each tile line is 2 bytes
|
|
|
|
+ 1); |
|
|
|
+ 1); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case PixelFifo::State::Sleep: |
|
|
|
|
|
|
|
if (m_pixel_fifo.background.size() <= 9) { |
|
|
|
void PPU::sleep() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (m_pixel_fifo.background.size() <= TILE_WIDTH + 1) { |
|
|
|
m_pixel_fifo.state = PixelFifo::State::Push; |
|
|
|
m_pixel_fifo.state = PixelFifo::State::Push; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case PixelFifo::State::Push: |
|
|
|
|
|
|
|
|
|
|
|
void PPU::pushFifo() |
|
|
|
|
|
|
|
{ |
|
|
|
m_pixel_fifo.state = PixelFifo::State::TileIndex; |
|
|
|
m_pixel_fifo.state = PixelFifo::State::TileIndex; |
|
|
|
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 8; ++i) { |
|
|
|
for (uint8_t i = 0; i < TILE_WIDTH; ++i) { |
|
|
|
uint8_t color_index = (m_pixel_fifo.pixels_lsb >> (7 - i) |
|
|
|
uint8_t color_index = (m_pixel_fifo.pixels_lsb >> (7 - i) |
|
|
|
| ((m_pixel_fifo.pixels_msb >> (7 - i)) << 1)) |
|
|
|
| ((m_pixel_fifo.pixels_msb >> (7 - i)) << 1)) |
|
|
|
& 0x3; |
|
|
|
& 0x3; |
|
|
|
m_pixel_fifo.background.push({ color_index, Palette::BGP }); |
|
|
|
m_pixel_fifo.background.push({ color_index, Palette::BGP }); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// Mode 3 Operation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PPU::pushPixel() |
|
|
|
|
|
|
|
{ |
|
|
|
// The pixel FIFO needs to contain more than 8 pixels to shift one out
|
|
|
|
// The pixel FIFO needs to contain more than 8 pixels to shift one out
|
|
|
|
if (m_pixel_fifo.background.size() > 8) { |
|
|
|
if (m_pixel_fifo.background.size() > 8) { |
|
|
|
auto pixel = m_pixel_fifo.background.front(); |
|
|
|
auto pixel = m_pixel_fifo.background.front(); |
|
|
@ -235,41 +300,3 @@ void PPU::pixelFifo() |
|
|
|
m_lcd_x_coordinate++; |
|
|
|
m_lcd_x_coordinate++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::array<uint8_t, 3> PPU::getPixelColor(uint8_t color_index, Palette palette) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
VERIFY(color_index < 4, "trying to fetch invalid color index '{}'", color_index); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (Emu::the().mode()) { |
|
|
|
|
|
|
|
case Emu::Mode::DMG: { |
|
|
|
|
|
|
|
uint8_t palette_data = Emu::the().readMemory(palette) & 0xff; |
|
|
|
|
|
|
|
uint8_t palette_value = palette_data >> (color_index * 2) & 0x3; |
|
|
|
|
|
|
|
switch (palette_value) { |
|
|
|
|
|
|
|
case 0: |
|
|
|
|
|
|
|
return { 200, 199, 168 }; |
|
|
|
|
|
|
|
case 1: |
|
|
|
|
|
|
|
return { 160, 160, 136 }; |
|
|
|
|
|
|
|
case 2: |
|
|
|
|
|
|
|
return { 104, 104, 80 }; |
|
|
|
|
|
|
|
case 3: |
|
|
|
|
|
|
|
return { 39, 40, 24 }; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case Emu::Mode::CGB: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
VERIFY_NOT_REACHED(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PPU::resetFrame() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_state = State::OAMSearch; |
|
|
|
|
|
|
|
m_clocks_into_frame = 0; |
|
|
|
|
|
|
|
m_lcd_x_coordinate = 0; |
|
|
|
|
|
|
|
m_lcd_y_coordinate = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|