一些Essgee核心优化

This commit is contained in:
sin365 2025-10-16 15:00:43 +08:00
parent a6178e991c
commit 5cb474d41a
3 changed files with 65 additions and 21 deletions

View File

@ -6,13 +6,36 @@ namespace Essgee.Emulation.Audio
{ {
public class Square : IDMGAudioChannel public class Square : IDMGAudioChannel
{ {
static readonly bool[,] dutyCycleTable = new bool[,] //static readonly bool[,] dutyCycleTable = new bool[,]
// {
// { false, false, false, false, false, false, false, true, }, // 00000001 12.5%
// { true, false, false, false, false, false, false, true, }, // 10000001 25%
// { true, false, false, false, false, true, true, true, }, // 10000111 50%
// { false, true, true, true, true, true, true, false, } // 01111110 75%
//};
// 1. 初始化 - 假设原始数组是 4行 x 8列
private const int Rows = 4;
private const int Cols = 8;
private readonly bool[] _dutyCycleTable1D = new bool[Rows * Cols]
{ {
{ false, false, false, false, false, false, false, true, }, // 00000001 12.5% // 第一行 (索引 0-7)
{ true, false, false, false, false, false, false, true, }, // 10000001 25% false, false, false, false, false, false, false, true,
{ true, false, false, false, false, true, true, true, }, // 10000111 50% // 第二行 (索引 8-15)
{ false, true, true, true, true, true, true, false, } // 01111110 75% true, false, false, false, false, false, false, true,
}; // 第三行 (索引 16-23)
true, false, false, false, false, true, true, true,
// 第四行 (索引 24-31)
false, true, true, true, true, true, true, false
};
// 2. 访问方法 - 替代原来的 dutyCycleTable[row, col]
public bool GetValue(int row, int col)
{
// 重要的边界检查(在稳定后可通过条件编译移除以极致优化)
// if (row < 0 || row >= Rows || col < 0 || col >= Cols) return false;
return _dutyCycleTable1D[row * Cols + col];
}
// NR10/20 // NR10/20
byte sweepPeriodReload, sweepShift; byte sweepPeriodReload, sweepShift;
@ -143,7 +166,10 @@ namespace Essgee.Emulation.Audio
dutyCounter %= 8; dutyCounter %= 8;
} }
OutputVolume = isDacEnabled && dutyCycleTable[dutyCycle, dutyCounter] ? volume : 0; //OutputVolume = isDacEnabled && dutyCycleTable[dutyCycle, dutyCounter] ? volume : 0;
//改为一维数组访问
OutputVolume = isDacEnabled && _dutyCycleTable1D[dutyCycle * Cols + dutyCounter] ? volume : 0;
} }
private void Trigger() private void Trigger()

View File

@ -445,9 +445,11 @@ namespace Essgee.Emulation.Machines
public void RunStep() public void RunStep()
{ {
var clockCyclesInStep = cpu.Step(); int clockCyclesInStep = cpu.Step();
int loopCount = clockCyclesInStep / 4; // 除法计算移出循环
for (var s = 0; s < clockCyclesInStep / 4; s++) // 在循环外检查 cartridge 是否为空,避免每次循环都检查
bool hasCartridge = cartridge != null;
for (var s = 0; s < loopCount; s++)
{ {
HandleTimerOverflow(); HandleTimerOverflow();
UpdateCycleCounter((ushort)(clockCycleCount + 4)); UpdateCycleCounter((ushort)(clockCycleCount + 4));
@ -456,7 +458,8 @@ namespace Essgee.Emulation.Machines
video.Step(4); video.Step(4);
audio.Step(4); audio.Step(4);
cartridge?.Step(4); if(hasCartridge)
cartridge.Step(4);
currentMasterClockCyclesInFrame += 4; currentMasterClockCyclesInFrame += 4;
} }

View File

@ -93,14 +93,26 @@ namespace Essgee.Emulation.Video.Nintendo
protected bool statIrqSignal, vBlankReady; protected bool statIrqSignal, vBlankReady;
protected int[] spritesOnLine; protected int[] spritesOnLine;
readonly byte[][] colorValuesBgr = new byte[][] // readonly byte[][] colorValuesBgr = new byte[][]
// {
// /* B G R */
// new byte[] { 0xF8, 0xF8, 0xF8 }, /* White */
// new byte[] { 0x9B, 0x9B, 0x9B }, /* Light gray */
// new byte[] { 0x3E, 0x3E, 0x3E }, /* Dark gray */
// new byte[] { 0x1F, 0x1F, 0x1F }, /* Black */
//};
//取值范例 colorValuesBgr[colorIndex * 3 + channelIndex];
const byte colorValuesBgr_singleLen = 3;
// 转换后的一维数组
readonly byte[] colorValuesBgr = new byte[]
{ {
/* B G R */ /* White */ 0xF8, 0xF8, 0xF8,
new byte[] { 0xF8, 0xF8, 0xF8 }, /* White */ /* Light gray */0x9B, 0x9B, 0x9B,
new byte[] { 0x9B, 0x9B, 0x9B }, /* Light gray */ /* Dark gray */ 0x3E, 0x3E, 0x3E,
new byte[] { 0x3E, 0x3E, 0x3E }, /* Dark gray */ /* Black */ 0x1F, 0x1F, 0x1F
new byte[] { 0x1F, 0x1F, 0x1F }, /* Black */ };
};
protected const byte screenUsageEmpty = 0; protected const byte screenUsageEmpty = 0;
protected const byte screenUsageBackground = 1 << 0; protected const byte screenUsageBackground = 1 << 0;
@ -719,9 +731,12 @@ namespace Essgee.Emulation.Video.Nintendo
protected virtual void WriteColorToFramebuffer(byte c, int address) protected virtual void WriteColorToFramebuffer(byte c, int address)
{ {
outputFramebuffer[address + 0] = colorValuesBgr[c & 0x03][0]; //outputFramebuffer[address + 0] = colorValuesBgr[c & 0x03][0];
outputFramebuffer[address + 1] = colorValuesBgr[c & 0x03][1]; //outputFramebuffer[address + 1] = colorValuesBgr[c & 0x03][1];
outputFramebuffer[address + 2] = colorValuesBgr[c & 0x03][2]; //outputFramebuffer[address + 2] = colorValuesBgr[c & 0x03][2];
outputFramebuffer[address + 0] = colorValuesBgr[(c & 0x03) * 3 + 0];
outputFramebuffer[address + 1] = colorValuesBgr[(c & 0x03) * 3 + 1];
outputFramebuffer[address + 2] = colorValuesBgr[(c & 0x03) * 3 + 2];
outputFramebuffer[address + 3] = 0xFF; outputFramebuffer[address + 3] = 0xFF;
} }