Fixed some TODOs, minor refactoring for tile viewer

This commit is contained in:
Sour 2022-04-30 20:42:26 -04:00
parent 66c8e281dd
commit 2b5416221a
9 changed files with 102 additions and 115 deletions

View file

@ -13,37 +13,58 @@ PpuTools::PpuTools(Debugger* debugger, Emulator *emu)
_debugger = debugger;
}
uint8_t PpuTools::GetTilePixelColor(const uint8_t* ram, const uint32_t ramMask, const uint8_t bpp, const uint32_t pixelStart, const uint8_t shift, const int secondByteOffset, const TileFormat format)
uint8_t PpuTools::GetTilePixelColor(const uint8_t* ram, const uint32_t ramMask, uint32_t pixelStart, uint8_t shift, const TileFormat format)
{
uint8_t color;
if(format == TileFormat::PceSpriteBpp4) {
//TODO FIX
uint16_t* ram2 = (uint16_t*)ram;
color = (((ram2[((pixelStart >> 1) + 0) & (ramMask >> 1)] >> shift) & 0x01) << 0);
color |= (((ram2[((pixelStart >> 1) + 16) & (ramMask >> 1)] >> shift) & 0x01) << 1);
color |= (((ram2[((pixelStart >> 1) + 32) & (ramMask >> 1)] >> shift) & 0x01) << 2);
color |= (((ram2[((pixelStart >> 1) + 48) & (ramMask >> 1)] >> shift) & 0x01) << 3);
} else if(bpp == 2) {
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
color |= (((ram[(pixelStart + secondByteOffset) & 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");
switch(format) {
case TileFormat::PceSpriteBpp4: {
if(shift >= 8) {
shift -= 8;
pixelStart++;
}
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
color |= (((ram[(pixelStart + 32) & ramMask] >> shift) & 0x01) << 1);
color |= (((ram[(pixelStart + 64) & ramMask] >> shift) & 0x01) << 2);
color |= (((ram[(pixelStart + 96) & ramMask] >> shift) & 0x01) << 3);
return color;
}
case TileFormat::Bpp2:
color = (((ram[pixelStart & ramMask] >> shift) & 0x01) << 0);
color |= (((ram[(pixelStart + 1) & ramMask] >> shift) & 0x01) << 1);
return color;
case TileFormat::NesBpp2:
color = (((ram[(pixelStart + 0) & ramMask] >> shift) & 0x01) << 0);
color |= (((ram[(pixelStart + 8) & ramMask] >> shift) & 0x01) << 1);
return color;
case TileFormat::Bpp4:
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);
return color;
case TileFormat::Bpp8:
case TileFormat::DirectColor:
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);
return color;
case TileFormat::Mode7:
case TileFormat::Mode7DirectColor:
return ram[(pixelStart + (7 - shift) * 2 + 1) & ramMask];
default:
throw std::runtime_error("unsupported format");
}
return color;
}
void PpuTools::BlendColors(uint8_t output[4], uint8_t input[4])
@ -56,17 +77,33 @@ void PpuTools::BlendColors(uint8_t output[4], uint8_t input[4])
output[3] = 0xFF;
}
uint32_t PpuTools::GetRgbPixelColor(uint32_t* colors, uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset)
uint32_t PpuTools::GetRgbPixelColor(TileFormat format, uint32_t* colors, uint8_t colorIndex, uint8_t palette)
{
if(bpp == 8 && directColorMode) {
uint32_t paletteColor = (
((((colorIndex & 0x07) << 1) | (palette & 0x01)) << 1) |
(((colorIndex & 0x38) | ((palette & 0x02) << 1)) << 4) |
(((colorIndex & 0xC0) | ((palette & 0x04) << 3)) << 7)
);
return SnesDefaultVideoFilter::ToArgb(paletteColor);
} else {
return colors[basePaletteOffset + (palette * (1 << bpp) + colorIndex)];
switch(format) {
case TileFormat::DirectColor:
return SnesDefaultVideoFilter::ToArgb(
((((colorIndex & 0x07) << 1) | (palette & 0x01)) << 1) |
(((colorIndex & 0x38) | ((palette & 0x02) << 1)) << 4) |
(((colorIndex & 0xC0) | ((palette & 0x04) << 3)) << 7)
);
case TileFormat::NesBpp2:
case TileFormat::Bpp2:
return colors[palette * 4 + colorIndex];
case TileFormat::Bpp4:
case TileFormat::PceSpriteBpp4:
return colors[palette * 16 + colorIndex];
case TileFormat::Bpp8:
case TileFormat::Mode7:
return colors[colorIndex];
case TileFormat::Mode7DirectColor:
return SnesDefaultVideoFilter::ToArgb(((colorIndex & 0x07) << 2) | ((colorIndex & 0x38) << 4) | ((colorIndex & 0xC0) << 7));
default:
throw std::runtime_error("unsupported format");
}
}
@ -78,18 +115,16 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t
uint8_t bpp;
int rowOffset = 2;
int secondByteOffset = 1;
bool directColor = false;
int tileWidth = 8;
int tileHeight = 8;
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;
case TileFormat::NesBpp2: bpp = 2; rowOffset = 1; secondByteOffset = 8; break;
case TileFormat::DirectColor: bpp = 8; break;
case TileFormat::Mode7: bpp = 16; rowOffset = 16; break;
case TileFormat::Mode7DirectColor: bpp = 16; rowOffset = 16; break;
case TileFormat::NesBpp2: bpp = 2; rowOffset = 1; break;
case TileFormat::PceSpriteBpp4: bpp = 4; rowOffset = 2; tileWidth = 16; tileHeight = 16; options.Width /= 2; options.Height /= 2; break;
default: bpp = 8; break;
}
@ -135,37 +170,14 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint8_t *source, uint32_t
baseOutputOffset = row * options.Width * tileWidth * tileHeight + column * tileWidth;
}
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 || options.Background == TileBackground::PaletteColor) {
uint32_t pos = baseOutputOffset + (y * options.Width * 8) + x;
if(pos < outputSize) {
uint32_t rgbColor;
if(directColor) {
rgbColor = SnesDefaultVideoFilter::ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7));
} else {
rgbColor = GetRgbPixelColor(colors, color, 0, 8, false, 0);
}
outBuffer[pos] = rgbColor;
}
}
}
}
} else {
for(int y = 0; y < tileHeight; y++) {
uint32_t pixelStart = addr + y * rowOffset;
for(int x = 0; x < tileWidth; x++) {
uint8_t color = GetTilePixelColor(ram, ramMask, bpp, pixelStart, tileWidth - 1 - x, secondByteOffset, options.Format);
if(color != 0 || options.Background == TileBackground::PaletteColor) {
uint32_t pos = baseOutputOffset + (y * options.Width * tileWidth) + x;
if(pos < outputSize) {
outBuffer[pos] = GetRgbPixelColor(colors, color, options.Palette, bpp, directColor, 0);
}
for(int y = 0; y < tileHeight; y++) {
uint32_t pixelStart = addr + y * rowOffset;
for(int x = 0; x < tileWidth; x++) {
uint8_t color = GetTilePixelColor(ram, ramMask, pixelStart, tileWidth - 1 - x, options.Format);
if(color != 0 || options.Background == TileBackground::PaletteColor) {
uint32_t pos = baseOutputOffset + (y * options.Width * tileWidth) + x;
if(pos < outputSize) {
outBuffer[pos] = GetRgbPixelColor(options.Format, colors, color, options.Palette);
}
}
}

View file

@ -157,11 +157,11 @@ protected:
Debugger* _debugger;
unordered_map<uint32_t, ViewerRefreshConfig> _updateTimings;
uint8_t GetTilePixelColor(const uint8_t* ram, const uint32_t ramMask, const uint8_t bpp, const uint32_t pixelStart, const uint8_t shift, const int secondByteOffset, const TileFormat format);
uint8_t GetTilePixelColor(const uint8_t* ram, const uint32_t ramMask, uint32_t pixelStart, uint8_t shift, const TileFormat format);
void BlendColors(uint8_t output[4], uint8_t input[4]);
uint32_t GetRgbPixelColor(uint32_t* colors, uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset);
uint32_t GetRgbPixelColor(TileFormat format, uint32_t* colors, uint8_t colorIndex, uint8_t palette);
public:
PpuTools(Debugger* debugger, Emulator *emu);

View file

@ -44,7 +44,7 @@ DebugTilemapInfo GbPpuTools::GetTilemap(GetTilemapOptions options, BaseState& ba
uint16_t pixelStart = tileStart + (vMirror ? (7 - y) : y) * 2;
for(int x = 0; x < 8; x++) {
uint8_t shift = hMirror ? (x & 0x07) : (7 - (x & 0x07));
uint8_t color = GetTilePixelColor(vram, vramMask, 2, pixelStart, shift, 1, TileFormat::Bpp2);
uint8_t color = GetTilePixelColor(vram, vramMask, pixelStart, shift, TileFormat::Bpp2);
outBuffer[((row * 8) + y) * 256 + column * 8 + x] = palette[bgPalette + color];
}
@ -205,7 +205,7 @@ void GbPpuTools::GetSpriteInfo(DebugSpriteInfo& sprite, uint16_t i, GetSpritePre
bool isCgb = state.CgbEnabled;
for(int x = 0; x < sprite.Width; x++) {
uint8_t shift = sprite.HorizontalMirror ? (x & 0x07) : (7 - (x & 0x07));
uint8_t color = GetTilePixelColor(vram, 0x3FFF, 2, pixelStart, shift, 1, TileFormat::Bpp2);
uint8_t color = GetTilePixelColor(vram, 0x3FFF, pixelStart, shift, TileFormat::Bpp2);
uint32_t outOffset = (y * sprite.Width) + x;
if(color > 0) {

View file

@ -276,7 +276,7 @@ void NesPpuTools::GetSpriteInfo(DebugSpriteInfo& sprite, uint32_t i, GetSpritePr
for(int x = 0; x < sprite.Width; x++) {
uint8_t shift = sprite.HorizontalMirror ? (x & 0x07) : (7 - (x & 0x07));
uint8_t color = GetTilePixelColor(vram, 0x3FFF, 2, pixelStart, shift, 8, TileFormat::NesBpp2);
uint8_t color = GetTilePixelColor(vram, 0x3FFF, pixelStart, shift, TileFormat::NesBpp2);
uint32_t outOffset = (y * sprite.Width) + x;
if(color > 0) {

View file

@ -78,7 +78,7 @@ DebugTilemapInfo PcePpuTools::GetTilemap(GetTilemapOptions options, BaseState& b
for(int y = 0; y < 8; y++) {
uint16_t tileAddr = tileIndex * 32 + y * 2;
for(int x = 0; x < 8; x++) {
uint8_t color = GetTilePixelColor(vram, 0xFFFF, 4, tileAddr, 7 - x, 0, TileFormat::Bpp4);
uint8_t color = GetTilePixelColor(vram, 0xFFFF, tileAddr, 7 - x, TileFormat::Bpp4);
uint16_t palAddr = color == 0 ? 0 : (palIndex * 16 + color);
uint32_t outPos = (row * 8 + y) * state.ColumnCount * 8 + column * 8 + x;
outBuffer[outPos] = palette[palAddr];
@ -136,6 +136,7 @@ void PcePpuTools::GetSpriteInfo(DebugSpriteInfo& sprite, uint16_t spriteIndex, G
uint8_t width = (flags & 0x100) ? 32 : 16;
uint8_t height;
switch((flags >> 12) & 0x03) {
default:
case 0: height = 16; break;
case 1: height = 32; break;
@ -207,14 +208,7 @@ void PcePpuTools::GetSpriteInfo(DebugSpriteInfo& sprite, uint16_t spriteIndex, G
uint16_t pixelStart = tileStart + yOffset;
uint8_t shift = 15 - xOffset;
//TODO FIX
uint16_t* ram = (uint16_t*)vram;
uint8_t color = (((ram[(pixelStart + 0) & 0x7FFF] >> shift) & 0x01) << 0);
color |= (((ram[(pixelStart + 16) & 0x7FFF] >> shift) & 0x01) << 1);
color |= (((ram[(pixelStart + 32) & 0x7FFF] >> shift) & 0x01) << 2);
color |= (((ram[(pixelStart + 48) & 0x7FFF] >> shift) & 0x01) << 3);
uint8_t color = GetTilePixelColor(vram, 0xFFFF, pixelStart * 2, shift, TileFormat::PceSpriteBpp4);
if(color != 0) {
sprite.SpritePreview[outOffset] = palette[color + sprite.Palette * 16 + 16*16];
} else {

View file

@ -4,6 +4,7 @@
#include "PCE/PceCdRom.h"
#include "PCE/PceConstants.h"
#include "Shared/Emulator.h"
#include "Shared/EmuSettings.h"
#include "Shared/MessageManager.h"
#include "Utilities/HexUtilities.h"
@ -16,8 +17,7 @@ PceAdpcm::PceAdpcm(Emulator* emu, PceCdRom* cdrom, PceScsiBus* scsi)
_scsi = scsi;
_ram = new uint8_t[0x10000];
//todo random
memset(_ram, 0, 0x10000);
emu->GetSettings()->InitializeRam(_ram, 0x10000);
emu->RegisterMemory(MemoryType::PceAdpcmRam, _ram, 0x10000);
}

View file

@ -90,18 +90,6 @@ PceCpu::PceCpu(Emulator* emu, PceConsole* console, PceMemoryManager* memoryManag
void PceCpu::Exec()
{
//TODO delete
/*uint32_t cycleCounts[256] = {};
for(int i = 0; i < 256; i++) {
uint64_t count = _state.CycleCount;
_instAddrMode = _addrMode[i];
_operand = FetchOperand();
(this->*_opTable[i])();
cycleCounts[i] = _state.CycleCount - count + 1;
}*/
#ifndef DUMMYCPU
_emu->ProcessInstruction<CpuType::Pce>();
#endif
@ -236,7 +224,6 @@ uint8_t PceCpu::GetOperandValue()
void PceCpu::DummyRead()
{
//TODO - is this supposed to be an idle cycle, or a dummy read?
MemoryRead(_state.PC, MemoryOperationType::DummyRead);
}

View file

@ -905,7 +905,6 @@ void PcePpu::WriteVdc(uint16_t addr, uint8_t value)
case 0x0A:
if(msb) {
//TODO - this probably has an impact on timing within the scanline?
_state.HorizDisplayStart = value & 0x7F;
} else {
_state.HorizSyncWidth = value & 0x1F;

View file

@ -62,12 +62,7 @@ DebugTilemapInfo SnesPpuTools::GetTilemap(GetTilemapOptions options, BaseState&
uint8_t color = vram[pixelStart + x * 2 + 1];
if(color != 0) {
uint32_t rgbColor;
if(directColor) {
rgbColor = SnesDefaultVideoFilter::ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7));
} else {
rgbColor = GetRgbPixelColor(palette, color, 0, 8, false, 0);
}
uint32_t rgbColor = GetRgbPixelColor(format, palette, color, 0);
outBuffer[((row * 8) + y) * outputSize.Width + column * 8 + x] = rgbColor;
}
}
@ -110,10 +105,10 @@ DebugTilemapInfo SnesPpuTools::GetTilemap(GetTilemapOptions options, BaseState&
uint16_t pixelStart = tileStart + yOffset * 2;
uint8_t shift = hMirror ? (x & 0x07) : (7 - (x & 0x07));
uint8_t color = GetTilePixelColor(vram, SnesPpu::VideoRamSize - 1, bpp, pixelStart, shift, 1, format);
uint8_t color = GetTilePixelColor(vram, SnesPpu::VideoRamSize - 1, pixelStart, shift, format);
if(color != 0) {
uint8_t paletteIndex = bpp == 8 ? 0 : (vram[addr + 1] >> 2) & 0x07;
outBuffer[((row * tileHeight) + y) * outputSize.Width + column * tileWidth + x] = GetRgbPixelColor(palette, color, paletteIndex, bpp, directColor, basePaletteOffset);
outBuffer[((row * tileHeight) + y) * outputSize.Width + column * tileWidth + x] = GetRgbPixelColor(format, palette + basePaletteOffset, color, paletteIndex);
}
}
}
@ -280,9 +275,9 @@ void SnesPpuTools::GetSpriteInfo(DebugSpriteInfo& sprite, uint16_t spriteIndex,
uint8_t tileIndex = (row << 4) | column;
uint16_t tileStart = ((state.OamBaseAddress + (tileIndex << 4) + (useSecondTable ? state.OamAddressOffset : 0)) & 0x7FFF) << 1;
uint8_t color = GetTilePixelColor(vram, SnesPpu::VideoRamSize - 1, 4, tileStart + yOffset * 2, 7 - xOffset, 1, TileFormat::Bpp4);
uint8_t color = GetTilePixelColor(vram, SnesPpu::VideoRamSize - 1, tileStart + yOffset * 2, 7 - xOffset, TileFormat::Bpp4);
if(color != 0) {
sprite.SpritePreview[outOffset] = GetRgbPixelColor(palette, color, sprite.Palette + 8, 4, false, 0);
sprite.SpritePreview[outOffset] = GetRgbPixelColor(TileFormat::Bpp4, palette, color, sprite.Palette + 8);
} else {
sprite.SpritePreview[outOffset] = 0;
}