Debugger: Tilemap viewer - Ensure the PPU's state at the selected scanline+cycle is used
This commit is contained in:
parent
60067db242
commit
3d79d84437
7 changed files with 33 additions and 442 deletions
|
@ -159,12 +159,7 @@ struct CodeLineData
|
|||
|
||||
struct GetTilemapOptions
|
||||
{
|
||||
uint8_t BgMode;
|
||||
uint8_t Layer;
|
||||
|
||||
int8_t Bpp;
|
||||
int32_t TilemapAddr;
|
||||
int32_t ChrAddr;
|
||||
};
|
||||
|
||||
enum class TileFormat
|
||||
|
|
|
@ -164,19 +164,16 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t
|
|||
}
|
||||
}
|
||||
|
||||
void PpuTools::GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgram, uint32_t* outBuffer)
|
||||
void PpuTools::GetTilemap(GetTilemapOptions options, PpuState state, uint8_t* vram, uint8_t* cgram, uint32_t* outBuffer)
|
||||
{
|
||||
static constexpr uint8_t layerBpp[8][4] = {
|
||||
{ 2,2,2,2 }, { 4,4,2,0 }, { 4,4,0,0 }, { 8,4,0,0 }, { 8,2,0,0 }, { 4,2,0,0 }, { 4,0,0,0 }, { 8,0,0,0 }
|
||||
};
|
||||
|
||||
PpuState state = _ppu->GetState();
|
||||
options.BgMode = state.BgMode;
|
||||
|
||||
bool directColor = state.DirectColorMode && (options.BgMode == 3 || options.BgMode == 4 || options.BgMode == 7);
|
||||
bool directColor = state.DirectColorMode && (state.BgMode == 3 || state.BgMode == 4 || state.BgMode == 7);
|
||||
|
||||
uint16_t basePaletteOffset = 0;
|
||||
if(options.BgMode == 0) {
|
||||
if(state.BgMode == 0) {
|
||||
basePaletteOffset = options.Layer * 64;
|
||||
}
|
||||
|
||||
|
@ -185,15 +182,15 @@ void PpuTools::GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgr
|
|||
uint32_t bgColor = ToArgb((cgram[1] << 8) | cgram[0]);
|
||||
std::fill(outBuffer, outBuffer + 1024*1024, bgColor);
|
||||
|
||||
uint8_t bpp = layerBpp[options.BgMode][options.Layer];
|
||||
uint8_t bpp = layerBpp[state.BgMode][options.Layer];
|
||||
if(bpp == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool largeTileWidth = layer.LargeTiles || options.BgMode == 5 || options.BgMode == 6;
|
||||
bool largeTileWidth = layer.LargeTiles || state.BgMode == 5 || state.BgMode == 6;
|
||||
bool largeTileHeight = layer.LargeTiles;
|
||||
|
||||
if(options.BgMode == 7) {
|
||||
if(state.BgMode == 7) {
|
||||
for(int row = 0; row < 128; row++) {
|
||||
for(int column = 0; column < 128; column++) {
|
||||
uint32_t tileIndex = vram[row * 256 + column * 2];
|
||||
|
|
|
@ -23,7 +23,7 @@ public:
|
|||
PpuTools(Console *console, Ppu *ppu);
|
||||
|
||||
void GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t srcSize, uint8_t *cgram, uint32_t *outBuffer);
|
||||
void GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgram, uint32_t *outBuffer);
|
||||
void GetTilemap(GetTilemapOptions options, PpuState state, uint8_t* vram, uint8_t* cgram, uint32_t *outBuffer);
|
||||
void GetSpritePreview(GetSpritePreviewOptions options, PpuState state, uint8_t* vram, uint8_t* oamRam, uint8_t* cgram, uint32_t *outBuffer);
|
||||
|
||||
void SetViewerUpdateTiming(uint32_t viewerId, uint16_t scanline, uint16_t cycle);
|
||||
|
|
|
@ -81,7 +81,7 @@ extern "C"
|
|||
DllExport void __stdcall GetCdlData(uint32_t offset, uint32_t length, SnesMemoryType memoryType, uint8_t* cdlData) { GetDebugger()->GetCodeDataLogger()->GetCdlData(offset, length, memoryType, cdlData); }
|
||||
DllExport void __stdcall SetCdlData(uint8_t *cdlData, uint32_t length) { GetDebugger()->SetCdlData(cdlData, length); }
|
||||
|
||||
DllExport void __stdcall GetTilemap(GetTilemapOptions options, uint8_t *vram, uint8_t *cgram, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTilemap(options, vram, cgram, buffer); }
|
||||
DllExport void __stdcall GetTilemap(GetTilemapOptions options, PpuState state, uint8_t *vram, uint8_t *cgram, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTilemap(options, state, vram, cgram, buffer); }
|
||||
DllExport void __stdcall GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t srcSize, uint8_t *cgram, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTileView(options, source, srcSize, cgram, buffer); }
|
||||
DllExport void __stdcall GetSpritePreview(GetSpritePreviewOptions options, PpuState state, uint8_t* vram, uint8_t *oamRam, uint8_t *cgram, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetSpritePreview(options, state, vram, oamRam, cgram, buffer); }
|
||||
DllExport void __stdcall SetViewerUpdateTiming(uint32_t viewerId, uint16_t scanline, uint16_t cycle) { GetDebugger()->GetPpuTools()->SetViewerUpdateTiming(viewerId, scanline, cycle); }
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
private NotificationListener _notifListener;
|
||||
private GetTilemapOptions _options;
|
||||
private DebugState _state;
|
||||
private PpuState _state;
|
||||
private byte[] _cgram;
|
||||
private byte[] _vram;
|
||||
private byte[] _tilemapData;
|
||||
|
@ -66,8 +66,6 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
_autoRefresh = config.AutoRefresh;
|
||||
|
||||
_options.BgMode = 0;
|
||||
|
||||
RefreshData();
|
||||
RefreshViewer();
|
||||
}
|
||||
|
@ -133,39 +131,39 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
private void RefreshData()
|
||||
{
|
||||
_state = DebugApi.GetState();
|
||||
_state = DebugApi.GetState().Ppu;
|
||||
_vram = DebugApi.GetMemoryState(SnesMemoryType.VideoRam);
|
||||
_cgram = DebugApi.GetMemoryState(SnesMemoryType.CGRam);
|
||||
}
|
||||
|
||||
private bool IsDoubleWidthScreen
|
||||
{
|
||||
get { return _state.Ppu.HiResMode || _state.Ppu.BgMode == 5 || _state.Ppu.BgMode == 6; }
|
||||
get { return _state.HiResMode || _state.BgMode == 5 || _state.BgMode == 6; }
|
||||
}
|
||||
|
||||
private bool IsDoubleHeightScreen
|
||||
{
|
||||
get { return _state.Ppu.ScreenInterlace || _state.Ppu.BgMode == 5 || _state.Ppu.BgMode == 6; }
|
||||
get { return _state.ScreenInterlace || _state.BgMode == 5 || _state.BgMode == 6; }
|
||||
}
|
||||
|
||||
private bool IsLargeTileWidth
|
||||
{
|
||||
get { return _state.Ppu.Layers[_options.Layer].LargeTiles || _state.Ppu.BgMode == 5 || _state.Ppu.BgMode == 6; }
|
||||
get { return _state.Layers[_options.Layer].LargeTiles || _state.BgMode == 5 || _state.BgMode == 6; }
|
||||
}
|
||||
|
||||
private bool IsLargeTileHeight
|
||||
{
|
||||
get { return _state.Ppu.Layers[_options.Layer].LargeTiles; }
|
||||
get { return _state.Layers[_options.Layer].LargeTiles; }
|
||||
}
|
||||
|
||||
private int GetWidth()
|
||||
{
|
||||
if(_state.Ppu.BgMode == 7) {
|
||||
if(_state.BgMode == 7) {
|
||||
return 1024;
|
||||
}
|
||||
|
||||
LayerConfig layer = _state.Ppu.Layers[_options.Layer];
|
||||
bool largeTileWidth = layer.LargeTiles || _state.Ppu.BgMode == 5 || _state.Ppu.BgMode == 6;
|
||||
LayerConfig layer = _state.Layers[_options.Layer];
|
||||
bool largeTileWidth = layer.LargeTiles || _state.BgMode == 5 || _state.BgMode == 6;
|
||||
bool largeTileHeight = layer.LargeTiles;
|
||||
|
||||
int width = 256;
|
||||
|
@ -180,11 +178,11 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
private int GetHeight()
|
||||
{
|
||||
if(_state.Ppu.BgMode == 7) {
|
||||
if(_state.BgMode == 7) {
|
||||
return 1024;
|
||||
}
|
||||
|
||||
LayerConfig layer = _state.Ppu.Layers[_options.Layer];
|
||||
LayerConfig layer = _state.Layers[_options.Layer];
|
||||
|
||||
int height = 256;
|
||||
if(layer.DoubleHeight) {
|
||||
|
@ -198,11 +196,11 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
private void RefreshViewer()
|
||||
{
|
||||
if(_layerBpp[_state.Ppu.BgMode, _options.Layer] == 0) {
|
||||
if(_layerBpp[_state.BgMode, _options.Layer] == 0) {
|
||||
_options.Layer = 0;
|
||||
}
|
||||
|
||||
DebugApi.GetTilemap(_options, _vram, _cgram, _tilemapData);
|
||||
DebugApi.GetTilemap(_options, _state, _vram, _cgram, _tilemapData);
|
||||
|
||||
int mapWidth = GetWidth();
|
||||
int mapHeight = GetHeight();
|
||||
|
@ -222,10 +220,10 @@ namespace Mesen.GUI.Debugger
|
|||
btnLayer3.BackColor = _options.Layer == 2 ? SystemColors.GradientActiveCaption : Color.Empty;
|
||||
btnLayer4.BackColor = _options.Layer == 3 ? SystemColors.GradientActiveCaption : Color.Empty;
|
||||
|
||||
btnLayer1.Enabled = _layerBpp[_state.Ppu.BgMode, 0] > 0;
|
||||
btnLayer2.Enabled = _layerBpp[_state.Ppu.BgMode, 1] > 0;
|
||||
btnLayer3.Enabled = _layerBpp[_state.Ppu.BgMode, 2] > 0;
|
||||
btnLayer4.Enabled = _layerBpp[_state.Ppu.BgMode, 3] > 0;
|
||||
btnLayer1.Enabled = _layerBpp[_state.BgMode, 0] > 0;
|
||||
btnLayer2.Enabled = _layerBpp[_state.BgMode, 1] > 0;
|
||||
btnLayer3.Enabled = _layerBpp[_state.BgMode, 2] > 0;
|
||||
btnLayer4.Enabled = _layerBpp[_state.BgMode, 3] > 0;
|
||||
|
||||
ctrlImagePanel.ImageSize = new Size(GetWidth(), GetHeight());
|
||||
ctrlImagePanel.Selection = new Rectangle(_selectedColumn * 8, _selectedRow * 8, IsLargeTileWidth ? 16 : 8, IsLargeTileHeight ? 16 : 8);
|
||||
|
@ -234,10 +232,10 @@ namespace Mesen.GUI.Debugger
|
|||
ctrlImagePanel.GridSizeY = chkShowTileGrid.Checked ? (IsLargeTileHeight ? 16 : 8): 0;
|
||||
|
||||
if(chkShowScrollOverlay.Checked) {
|
||||
LayerConfig layer = _state.Ppu.Layers[_options.Layer];
|
||||
int hScroll = _state.Ppu.BgMode == 7 ? (int)_state.Ppu.Mode7.HScroll : layer.HScroll;
|
||||
int vScroll = _state.Ppu.BgMode == 7 ? (int)_state.Ppu.Mode7.VScroll : layer.VScroll;
|
||||
int height = _state.Ppu.OverscanMode ? 239 : 224;
|
||||
LayerConfig layer = _state.Layers[_options.Layer];
|
||||
int hScroll = _state.BgMode == 7 ? (int)_state.Mode7.HScroll : layer.HScroll;
|
||||
int vScroll = _state.BgMode == 7 ? (int)_state.Mode7.VScroll : layer.VScroll;
|
||||
int height = _state.OverscanMode ? 239 : 224;
|
||||
ctrlImagePanel.Overlay = new Rectangle(hScroll, vScroll, IsDoubleWidthScreen ? 512 : 256, IsDoubleHeightScreen ? height*2 : height);
|
||||
} else {
|
||||
ctrlImagePanel.Overlay = Rectangle.Empty;
|
||||
|
@ -247,7 +245,7 @@ namespace Mesen.GUI.Debugger
|
|||
|
||||
private void UpdateFields()
|
||||
{
|
||||
if(_state.Ppu.BgMode == 7) {
|
||||
if(_state.BgMode == 7) {
|
||||
//Selected tile
|
||||
txtMapNumber.Text = "0";
|
||||
txtPosition.Text = _selectedColumn.ToString() + ", " + _selectedRow.ToString();
|
||||
|
@ -272,7 +270,7 @@ namespace Mesen.GUI.Debugger
|
|||
int row = (IsLargeTileHeight ? _selectedRow / 2 : _selectedRow);
|
||||
int column = (IsLargeTileWidth ? _selectedColumn / 2 : _selectedColumn);
|
||||
|
||||
LayerConfig layer = _state.Ppu.Layers[_options.Layer];
|
||||
LayerConfig layer = _state.Layers[_options.Layer];
|
||||
int addrVerticalScrollingOffset = layer.DoubleHeight ? ((row & 0x20) << (layer.DoubleWidth ? 6 : 5)) : 0;
|
||||
int baseOffset = layer.TilemapAddress + addrVerticalScrollingOffset + ((row & 0x1F) << 5);
|
||||
int address = (baseOffset + (column & 0x1F) + (layer.DoubleWidth ? ((column & 0x20) << 5) : 0)) << 1;
|
||||
|
@ -294,7 +292,7 @@ namespace Mesen.GUI.Debugger
|
|||
txtMapAddress.Text = (layer.TilemapAddress << 1).ToString("X4");
|
||||
txtTilesetAddress.Text = (layer.ChrAddress << 1).ToString("X4");
|
||||
txtTileSize.Text = (IsLargeTileWidth ? "16" : "8") + "x" + (IsLargeTileHeight ? "16" : "8");
|
||||
txtBitDepth.Text = _layerBpp[_state.Ppu.BgMode, _options.Layer].ToString();
|
||||
txtBitDepth.Text = _layerBpp[_state.BgMode, _options.Layer].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ namespace Mesen.GUI
|
|||
return buffer;
|
||||
}
|
||||
|
||||
[DllImport(DllPath)] public static extern void GetTilemap(GetTilemapOptions options, byte[] vram, byte[] cgram, [In, Out] byte[] buffer);
|
||||
[DllImport(DllPath)] public static extern void GetTilemap(GetTilemapOptions options, PpuState state, byte[] vram, byte[] cgram, [In, Out] byte[] buffer);
|
||||
[DllImport(DllPath)] public static extern void GetTileView(GetTileViewOptions options, byte[] source, int srcSize, byte[] cgram, [In, Out] byte[] buffer);
|
||||
[DllImport(DllPath)] public static extern void GetSpritePreview(GetSpritePreviewOptions options, PpuState state, byte[] vram, byte[] oamRam, byte[] cgram, [In, Out] byte[] buffer);
|
||||
|
||||
|
@ -525,12 +525,7 @@ namespace Mesen.GUI
|
|||
|
||||
public struct GetTilemapOptions
|
||||
{
|
||||
public byte BgMode;
|
||||
public byte Layer;
|
||||
|
||||
public byte Bpp;
|
||||
public Int32 TilemapAddr;
|
||||
public Int32 ChrAddr;
|
||||
}
|
||||
|
||||
public struct GetTileViewOptions
|
||||
|
|
|
@ -1,394 +0,0 @@
|
|||
#include "stdafx.h"
|
||||
#include "PpuTools.h"
|
||||
#include "Ppu.h"
|
||||
#include "DebugTypes.h"
|
||||
#include "Console.h"
|
||||
#include "BaseCartridge.h"
|
||||
#include "MemoryManager.h"
|
||||
#include "NotificationManager.h"
|
||||
|
||||
PpuTools::PpuTools(Console *console, Ppu *ppu)
|
||||
{
|
||||
_console = console;
|
||||
_ppu = ppu;
|
||||
}
|
||||
|
||||
uint8_t PpuTools::GetTilePixelColor(const uint8_t* ram, const uint32_t ramMask, const uint8_t bpp, const uint32_t pixelStart, const uint8_t shift)
|
||||
{
|
||||
uint8_t color;
|
||||
if(bpp == 2) {
|
||||
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
|
||||
color |= (((ram[(pixelStart + 1) & ramMask] >> shift) & 0x01) << 1);
|
||||
} else if(bpp == 4) {
|
||||
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
|
||||
color |= (((ram[(pixelStart + 1) & ramMask] >> shift) & 0x01) << 1);
|
||||
color |= (((ram[(pixelStart + 16) & ramMask] >> shift) & 0x01) << 2);
|
||||
color |= (((ram[(pixelStart + 17) & ramMask] >> shift) & 0x01) << 3);
|
||||
} else if(bpp == 8) {
|
||||
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
|
||||
color |= (((ram[(pixelStart + 1) & ramMask] >> shift) & 0x01) << 1);
|
||||
color |= (((ram[(pixelStart + 16) & ramMask] >> shift) & 0x01) << 2);
|
||||
color |= (((ram[(pixelStart + 17) & ramMask] >> shift) & 0x01) << 3);
|
||||
color |= (((ram[(pixelStart + 32) & ramMask] >> shift) & 0x01) << 4);
|
||||
color |= (((ram[(pixelStart + 33) & ramMask] >> shift) & 0x01) << 5);
|
||||
color |= (((ram[(pixelStart + 48) & ramMask] >> shift) & 0x01) << 6);
|
||||
color |= (((ram[(pixelStart + 49) & ramMask] >> shift) & 0x01) << 7);
|
||||
} else {
|
||||
throw std::runtime_error("unsupported bpp");
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
uint32_t PpuTools::ToArgb(uint16_t color)
|
||||
{
|
||||
uint8_t b = (color >> 10) << 3;
|
||||
uint8_t g = ((color >> 5) & 0x1F) << 3;
|
||||
uint8_t r = (color & 0x1F) << 3;
|
||||
|
||||
return 0xFF000000 | (r << 16) | (g << 8) | b;
|
||||
}
|
||||
|
||||
void PpuTools::BlendColors(uint8_t output[4], uint8_t input[4])
|
||||
{
|
||||
uint8_t alpha = input[3] + 1;
|
||||
uint8_t invertedAlpha = 256 - input[3];
|
||||
output[0] = (uint8_t)((alpha * input[0] + invertedAlpha * output[0]) >> 8);
|
||||
output[1] = (uint8_t)((alpha * input[1] + invertedAlpha * output[1]) >> 8);
|
||||
output[2] = (uint8_t)((alpha * input[2] + invertedAlpha * output[2]) >> 8);
|
||||
output[3] = 0xFF;
|
||||
}
|
||||
|
||||
uint32_t PpuTools::GetRgbPixelColor(uint8_t* cgram, uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset)
|
||||
{
|
||||
uint16_t paletteColor;
|
||||
if(bpp == 8 && directColorMode) {
|
||||
paletteColor = (
|
||||
((((colorIndex & 0x07) << 1) | (palette & 0x01)) << 1) |
|
||||
(((colorIndex & 0x38) | ((palette & 0x02) << 1)) << 4) |
|
||||
(((colorIndex & 0xC0) | ((palette & 0x04) << 3)) << 7)
|
||||
);
|
||||
} else {
|
||||
uint16_t paletteRamOffset = basePaletteOffset + (palette * (1 << bpp) + colorIndex) * 2;
|
||||
paletteColor = cgram[paletteRamOffset] | (cgram[paletteRamOffset + 1] << 8);
|
||||
}
|
||||
return ToArgb(paletteColor);
|
||||
}
|
||||
|
||||
void PpuTools::GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t srcSize, uint8_t *cgram, uint32_t *outBuffer)
|
||||
{
|
||||
uint8_t* ram = source;
|
||||
uint32_t ramMask = srcSize - 1;
|
||||
uint8_t bpp;
|
||||
|
||||
bool directColor = false;
|
||||
switch(options.Format) {
|
||||
case TileFormat::Bpp2: bpp = 2; break;
|
||||
case TileFormat::Bpp4: bpp = 4; break;
|
||||
|
||||
case TileFormat::DirectColor:
|
||||
directColor = true;
|
||||
bpp = 8;
|
||||
break;
|
||||
|
||||
case TileFormat::Mode7:
|
||||
bpp = 16;
|
||||
break;
|
||||
|
||||
case TileFormat::Mode7DirectColor:
|
||||
directColor = true;
|
||||
bpp = 16;
|
||||
break;
|
||||
|
||||
default: bpp = 8; break;
|
||||
}
|
||||
|
||||
int bytesPerTile = 64 * bpp / 8;
|
||||
int tileCount = 0x10000 / bytesPerTile;
|
||||
|
||||
uint16_t bgColor = (cgram[1] << 8) | cgram[0];
|
||||
for(int i = 0; i < 512 * 512; i++) {
|
||||
outBuffer[i] = ToArgb(bgColor);
|
||||
}
|
||||
|
||||
int rowCount = tileCount / options.Width;
|
||||
|
||||
for(int row = 0; row < rowCount; row++) {
|
||||
uint32_t baseOffset = row * bytesPerTile * options.Width;
|
||||
|
||||
for(int column = 0; column < options.Width; column++) {
|
||||
uint32_t addr = baseOffset + bytesPerTile * column;
|
||||
|
||||
int baseOutputOffset;
|
||||
if(options.Layout == TileLayout::SingleLine8x16) {
|
||||
int displayColumn = column / 2 + ((row & 0x01) ? options.Width/2 : 0);
|
||||
int displayRow = (row & ~0x01) + ((column & 0x01) ? 1 : 0);
|
||||
baseOutputOffset = displayRow * options.Width * 64 + displayColumn * 8;
|
||||
} else if(options.Layout == TileLayout::SingleLine16x16) {
|
||||
int displayColumn = (column / 2) + (column & 0x01) + ((row & 0x01) ? options.Width/2 : 0) + ((column & 0x02) ? -1 : 0);
|
||||
int displayRow = (row & ~0x01) + ((column & 0x02) ? 1 : 0);
|
||||
baseOutputOffset = displayRow * options.Width * 64 + displayColumn * 8;
|
||||
} else {
|
||||
baseOutputOffset = row * options.Width * 64 + column * 8;
|
||||
}
|
||||
|
||||
if(options.Format == TileFormat::Mode7 || options.Format == TileFormat::Mode7DirectColor) {
|
||||
for(int y = 0; y < 8; y++) {
|
||||
uint32_t pixelStart = addr + y * 16;
|
||||
|
||||
for(int x = 0; x < 8; x++) {
|
||||
uint8_t color = ram[(pixelStart + x * 2 + 1) & ramMask];
|
||||
|
||||
if(color != 0) {
|
||||
uint32_t rgbColor;
|
||||
if(directColor) {
|
||||
rgbColor = ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7));
|
||||
} else {
|
||||
rgbColor = GetRgbPixelColor(cgram, color, 0, 8, false, 0);
|
||||
}
|
||||
outBuffer[baseOutputOffset + (y*options.Width*8) + x] = rgbColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for(int y = 0; y < 8; y++) {
|
||||
uint32_t pixelStart = addr + y * 2;
|
||||
for(int x = 0; x < 8; x++) {
|
||||
uint8_t color = GetTilePixelColor(ram, ramMask, bpp, pixelStart, 7 - x);
|
||||
if(color != 0) {
|
||||
outBuffer[baseOutputOffset + (y*options.Width*8) + x] = GetRgbPixelColor(cgram, color, options.Palette, bpp, directColor, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PpuTools::GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgram, uint32_t* outBuffer)
|
||||
{
|
||||
static constexpr uint8_t layerBpp[8][4] = {
|
||||
{ 2,2,2,2 }, { 4,4,2,0 }, { 4,4,0,0 }, { 8,4,0,0 }, { 8,2,0,0 }, { 4,2,0,0 }, { 4,0,0,0 }, { 8,0,0,0 }
|
||||
};
|
||||
|
||||
PpuState state = _ppu->GetState();
|
||||
options.BgMode = state.BgMode;
|
||||
|
||||
bool directColor = state.DirectColorMode && (options.BgMode == 3 || options.BgMode == 4 || options.BgMode == 7);
|
||||
|
||||
uint16_t basePaletteOffset = 0;
|
||||
if(options.BgMode == 0) {
|
||||
basePaletteOffset = options.Layer * 64;
|
||||
}
|
||||
|
||||
LayerConfig layer = state.Layers[options.Layer];
|
||||
|
||||
uint32_t bgColor = ToArgb((cgram[1] << 8) | cgram[0]);
|
||||
std::fill(outBuffer, outBuffer + 1024*1024, bgColor);
|
||||
|
||||
uint8_t bpp = layerBpp[options.BgMode][options.Layer];
|
||||
if(bpp == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool largeTileWidth = layer.LargeTiles || options.BgMode == 5 || options.BgMode == 6;
|
||||
bool largeTileHeight = layer.LargeTiles;
|
||||
|
||||
if(options.BgMode == 7) {
|
||||
for(int row = 0; row < 128; row++) {
|
||||
for(int column = 0; column < 128; column++) {
|
||||
uint32_t tileIndex = vram[row * 256 + column * 2];
|
||||
uint32_t tileAddr = tileIndex * 128;
|
||||
|
||||
for(int y = 0; y < 8; y++) {
|
||||
uint32_t pixelStart = tileAddr + y * 16;
|
||||
|
||||
for(int x = 0; x < 8; x++) {
|
||||
uint8_t color = vram[pixelStart + x * 2 + 1];
|
||||
|
||||
if(color != 0) {
|
||||
uint32_t rgbColor;
|
||||
if(directColor) {
|
||||
rgbColor = ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7));
|
||||
} else {
|
||||
rgbColor = GetRgbPixelColor(cgram, color, 0, 8, false, 0);
|
||||
}
|
||||
outBuffer[((row * 8) + y) * 1024 + column * 8 + x] = rgbColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int tileHeight = largeTileHeight ? 16 : 8;
|
||||
int tileWidth = largeTileWidth ? 16 : 8;
|
||||
for(int row = 0; row < (layer.DoubleHeight ? 64 : 32); row++) {
|
||||
uint16_t addrVerticalScrollingOffset = layer.DoubleHeight ? ((row & 0x20) << (layer.DoubleWidth ? 6 : 5)) : 0;
|
||||
uint16_t baseOffset = layer.TilemapAddress + addrVerticalScrollingOffset + ((row & 0x1F) << 5);
|
||||
|
||||
for(int column = 0; column < (layer.DoubleWidth ? 64 : 32); column++) {
|
||||
uint16_t addr = (baseOffset + (column & 0x1F) + (layer.DoubleWidth ? ((column & 0x20) << 5) : 0)) << 1;
|
||||
|
||||
bool vMirror = (vram[addr + 1] & 0x80) != 0;
|
||||
bool hMirror = (vram[addr + 1] & 0x40) != 0;
|
||||
uint16_t tileIndex = ((vram[addr + 1] & 0x03) << 8) | vram[addr];
|
||||
|
||||
for(int y = 0; y < tileHeight; y++) {
|
||||
uint8_t yOffset = vMirror ? (7 - (y & 0x07)) : (y & 0x07);
|
||||
|
||||
for(int x = 0; x < tileWidth; x++) {
|
||||
uint16_t tileOffset = (
|
||||
(largeTileHeight ? ((y & 0x08) ? (vMirror ? 0 : 16) : (vMirror ? 16 : 0)) : 0) +
|
||||
(largeTileWidth ? ((x & 0x08) ? (hMirror ? 0 : 1) : (hMirror ? 1 : 0)) : 0)
|
||||
);
|
||||
|
||||
uint16_t tileStart = (layer.ChrAddress << 1) + ((tileIndex + tileOffset) & 0x3FF) * 8 * bpp;
|
||||
uint16_t pixelStart = tileStart + yOffset * 2;
|
||||
|
||||
uint8_t shift = hMirror ? (x & 0x07) : (7 - (x & 0x07));
|
||||
uint8_t color = GetTilePixelColor(vram, Ppu::VideoRamSize - 1, bpp, pixelStart, shift);
|
||||
if(color != 0) {
|
||||
uint8_t palette = bpp == 8 ? 0 : (vram[addr + 1] >> 2) & 0x07;
|
||||
outBuffer[((row * tileHeight) + y) * 1024 + column * tileWidth + x] = GetRgbPixelColor(cgram, color, palette, bpp, directColor, basePaletteOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr uint8_t _oamSizes[8][2][2] = {
|
||||
{ { 1, 1 }, { 2, 2 } }, //8x8 + 16x16
|
||||
{ { 1, 1 }, { 4, 4 } }, //8x8 + 32x32
|
||||
{ { 1, 1 }, { 8, 8 } }, //8x8 + 64x64
|
||||
{ { 2, 2 }, { 4, 4 } }, //16x16 + 32x32
|
||||
{ { 2, 2 }, { 8, 8 } }, //16x16 + 64x64
|
||||
{ { 4, 4 }, { 8, 8 } }, //32x32 + 64x64
|
||||
{ { 2, 4 }, { 4, 8 } }, //16x32 + 32x64
|
||||
{ { 2, 4 }, { 4, 4 } } //16x32 + 32x32
|
||||
};
|
||||
|
||||
void PpuTools::GetSpritePreview(GetSpritePreviewOptions options, PpuState state, uint8_t *vram, uint8_t *oamRam, uint8_t *cgram, uint32_t *outBuffer)
|
||||
{
|
||||
//TODO
|
||||
//uint16_t baseAddr = state.EnableOamPriority ? (_internalOamAddress & 0x1FC) : 0;
|
||||
uint16_t baseAddr = 0;
|
||||
|
||||
bool filled[256 * 240] = {};
|
||||
int lastScanline = state.OverscanMode ? 239 : 224;
|
||||
std::fill(outBuffer, outBuffer + 256 * lastScanline, 0xFF888888);
|
||||
std::fill(outBuffer + 256 * lastScanline, outBuffer + 256 * 240, 0xFF000000);
|
||||
|
||||
for(int screenY = 0; screenY < lastScanline; screenY++) {
|
||||
for(int i = 508; i >= 0; i -= 4) {
|
||||
uint16_t addr = (baseAddr + i) & 0x1FF;
|
||||
uint8_t y = oamRam[addr + 1];
|
||||
|
||||
uint8_t highTableOffset = addr >> 4;
|
||||
uint8_t shift = ((addr >> 2) & 0x03) << 1;
|
||||
uint8_t highTableValue = oamRam[0x200 | highTableOffset] >> shift;
|
||||
uint8_t largeSprite = (highTableValue & 0x02) >> 1;
|
||||
uint8_t height = _oamSizes[state.OamMode][largeSprite][1] << 3;
|
||||
|
||||
if(state.ObjInterlace) {
|
||||
height /= 2;
|
||||
}
|
||||
|
||||
uint8_t endY = (y + height) & 0xFF;
|
||||
|
||||
bool visible = (screenY >= y && screenY < endY) || (endY < y && screenY < endY);
|
||||
if(!visible) {
|
||||
//Not visible on this scanline
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t width = _oamSizes[state.OamMode][largeSprite][0] << 3;
|
||||
uint16_t sign = (highTableValue & 0x01) << 8;
|
||||
int16_t x = (int16_t)((sign | oamRam[addr]) << 7) >> 7;
|
||||
|
||||
if(x != -256 && (x + width <= 0 || x > 255)) {
|
||||
//Sprite is not visible (and must be ignored for time/range flag calculations)
|
||||
//Sprites at X=-256 are always used when considering Time/Range flag calculations, but not actually drawn.
|
||||
continue;
|
||||
}
|
||||
|
||||
int tileRow = (oamRam[addr + 2] & 0xF0) >> 4;
|
||||
int tileColumn = oamRam[addr + 2] & 0x0F;
|
||||
|
||||
uint8_t flags = oamRam[addr + 3];
|
||||
bool useSecondTable = (flags & 0x01) != 0;
|
||||
uint8_t palette = (flags >> 1) & 0x07;
|
||||
//uint8_t priority = (flags >> 4) & 0x03;
|
||||
bool horizontalMirror = (flags & 0x40) != 0;
|
||||
bool verticalMirror = (flags & 0x80) != 0;
|
||||
|
||||
uint8_t yOffset;
|
||||
int rowOffset;
|
||||
|
||||
int yGap = (screenY - y);
|
||||
if(state.ObjInterlace) {
|
||||
yGap <<= 1;
|
||||
}
|
||||
if(verticalMirror) {
|
||||
yOffset = (height - 1 - yGap) & 0x07;
|
||||
rowOffset = (height - 1 - yGap) >> 3;
|
||||
} else {
|
||||
yOffset = yGap & 0x07;
|
||||
rowOffset = yGap >> 3;
|
||||
}
|
||||
|
||||
uint8_t row = (tileRow + rowOffset) & 0x0F;
|
||||
|
||||
for(int j = std::max<int16_t>(x, 0); j < x + width && j < 256; j++) {
|
||||
uint32_t outOffset = screenY * 256 + j;
|
||||
if(filled[outOffset]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t xOffset;
|
||||
int columnOffset;
|
||||
if(horizontalMirror) {
|
||||
xOffset = (width - (j - x) - 1) & 0x07;
|
||||
columnOffset = (width - (j - x) - 1) >> 3;
|
||||
} else {
|
||||
xOffset = (j - x) & 0x07;
|
||||
columnOffset = (j - x) >> 3;
|
||||
}
|
||||
|
||||
uint8_t column = (tileColumn + columnOffset) & 0x0F;
|
||||
uint8_t tileIndex = (row << 4) | column;
|
||||
uint16_t tileStart = ((state.OamBaseAddress + (tileIndex << 4) + (useSecondTable ? state.OamAddressOffset : 0)) & 0x7FFF) << 1;
|
||||
|
||||
uint8_t color = GetTilePixelColor(vram, Ppu::VideoRamSize - 1, 4, tileStart + yOffset * 2, 7 - xOffset);
|
||||
if(color != 0) {
|
||||
if(options.SelectedSprite == i / 4) {
|
||||
filled[outOffset] = true;
|
||||
}
|
||||
outBuffer[outOffset] = GetRgbPixelColor(cgram, color, palette, 4, false, 256);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PpuTools::SetViewerUpdateTiming(uint32_t viewerId, uint16_t scanline, uint16_t cycle)
|
||||
{
|
||||
//TODO Thread safety
|
||||
_updateTimings[viewerId] = (scanline << 16) | cycle;
|
||||
}
|
||||
|
||||
void PpuTools::RemoveViewer(uint32_t viewerId)
|
||||
{
|
||||
//TODO Thread safety
|
||||
_updateTimings.erase(viewerId);
|
||||
}
|
||||
|
||||
void PpuTools::UpdateViewers(uint16_t scanline, uint16_t cycle)
|
||||
{
|
||||
uint32_t currentCycle = (scanline << 16) | cycle;
|
||||
for(auto updateTiming : _updateTimings) {
|
||||
if(updateTiming.second == currentCycle) {
|
||||
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::ViewerRefresh, (void*)(uint64_t)updateTiming.first);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue