Compare commits

...

6 Commits

12 changed files with 283 additions and 59 deletions

View File

@ -6,13 +6,36 @@ namespace Essgee.Emulation.Audio
{
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%
{ 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%
};
// 第一行 (索引 0-7)
false, false, false, false, false, false, false, true,
// 第二行 (索引 8-15)
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
byte sweepPeriodReload, sweepShift;
@ -143,7 +166,10 @@ namespace Essgee.Emulation.Audio
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()

View File

@ -296,6 +296,8 @@ namespace Essgee.Emulation.Audio
sampleCycleCount = frameCycleCount = 0;
}
//独立声明,不在函数内部
private bool[] channelEnableFlags = new bool[4];
public void Step(int clockCyclesInStep)
{
if (!isSoundHwEnabled) return;
@ -385,10 +387,25 @@ namespace Essgee.Emulation.Audio
// new bool[] { !channel1ForceEnable, !channel2ForceEnable, !channel3ForceEnable, !channel4ForceEnable },
// mixedSampleBuffer.ToArray());
//有GC
//EnqueueSamplesEventArgs eventArgs = EnqueueSamplesEventArgs.Create(
// numChannels,
// channelSampleBuffer,
// new bool[] { !channel1ForceEnable, !channel2ForceEnable, !channel3ForceEnable, !channel4ForceEnable },
// mixedSampleBuffer,
// mixedSampleBuffer_writePos);
// 在函数中使用
channelEnableFlags[0] = !channel1ForceEnable;
channelEnableFlags[1] = !channel2ForceEnable;
channelEnableFlags[2] = !channel3ForceEnable;
channelEnableFlags[3] = !channel4ForceEnable;
EnqueueSamplesEventArgs eventArgs = EnqueueSamplesEventArgs.Create(
numChannels,
channelSampleBuffer,
new bool[] { !channel1ForceEnable, !channel2ForceEnable, !channel3ForceEnable, !channel4ForceEnable },
channelEnableFlags,
mixedSampleBuffer,
mixedSampleBuffer_writePos);

View File

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

View File

@ -44,7 +44,16 @@ namespace Essgee.Emulation
buffer[address + 3] = 0xFF;
}
public static void RGBCGBtoBGRA8888(int color, ref byte[] buffer, int address)
//public static void RGBCGBtoBGRA8888(int color, ref byte[] buffer, int address)
//{
// /* https://byuu.net/video/color-emulation -- "LCD emulation: Game Boy Color" */
// byte r = (byte)((color >> 0) & 0x1F), g = (byte)((color >> 5) & 0x1F), b = (byte)((color >> 10) & 0x1F);
// buffer[address + 0] = (byte)(Math.Min(960, (r * 6) + (g * 4) + (b * 22)) >> 2);
// buffer[address + 1] = (byte)(Math.Min(960, (g * 24) + (b * 8)) >> 2);
// buffer[address + 2] = (byte)(Math.Min(960, (r * 26) + (g * 4) + (b * 2)) >> 2);
// buffer[address + 3] = 0xFF;
//}
public static void RGBCGBtoBGRA8888(int color, ref byte* buffer, int address)
{
/* https://byuu.net/video/color-emulation -- "LCD emulation: Game Boy Color" */
byte r = (byte)((color >> 0) & 0x1F), g = (byte)((color >> 5) & 0x1F), b = (byte)((color >> 10) & 0x1F);

View File

@ -346,7 +346,7 @@ namespace Essgee.Emulation.Video.Nintendo
WriteColorToFramebuffer(c, ((y * displayActiveWidth) + (x % displayActiveWidth)) * 4);
}
private void WriteColorToFramebuffer(ushort c, int address)
private unsafe void WriteColorToFramebuffer(ushort c, int address)
{
RGBCGBtoBGRA8888(c, ref outputFramebuffer, address);
}

View File

@ -8,7 +8,7 @@ using static Essgee.Emulation.CPU.SM83;
namespace Essgee.Emulation.Video.Nintendo
{
public class DMGVideo : IVideo
public unsafe class DMGVideo : IVideo
{
protected const int displayActiveWidth = 160;
protected const int displayActiveHeight = 144;
@ -93,14 +93,26 @@ namespace Essgee.Emulation.Video.Nintendo
protected bool statIrqSignal, vBlankReady;
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 */
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 */
};
/* White */ 0xF8, 0xF8, 0xF8,
/* Light gray */0x9B, 0x9B, 0x9B,
/* Dark gray */ 0x3E, 0x3E, 0x3E,
/* Black */ 0x1F, 0x1F, 0x1F
};
protected const byte screenUsageEmpty = 0;
protected const byte screenUsageBackground = 1 << 0;
@ -109,7 +121,48 @@ namespace Essgee.Emulation.Video.Nintendo
protected byte[,] screenUsageFlags, screenUsageSpriteXCoords, screenUsageSpriteSlots;
protected int cycleCount, cycleDotPause, currentScanline;
protected byte[] outputFramebuffer;
//protected byte[] outputFramebuffer;
#region //指针化 outputFramebuffer
byte[] outputFramebuffer_src;
protected GCHandle outputFramebuffer_handle;
public byte* outputFramebuffer;
public int outputFramebufferLength;
public bool outputFramebuffer_IsNull => outputFramebuffer == null;
public byte[] outputFramebuffer_set
{
set
{
outputFramebuffer_handle.ReleaseGCHandle();
outputFramebuffer_src = value;
outputFramebufferLength = value.Length;
outputFramebuffer_src.GetObjectPtr(ref outputFramebuffer_handle, ref outputFramebuffer);
}
}
#endregion
#region //指针化 outputFramebufferCopy
byte[] outputFramebufferCopy_src;
GCHandle outputFramebufferCopy_handle;
public byte* outputFramebufferCopy;
public int outputFramebufferCopyLength;
public bool outputFramebufferCopy_IsNull => outputFramebufferCopy == null;
private IntPtr outputFramebufferCopy_IntPtr;
public byte[] outputFramebufferCopy_set
{
set
{
outputFramebufferCopy_handle.ReleaseGCHandle();
outputFramebufferCopy_src = value;
outputFramebufferCopyLength = value.Length;
outputFramebufferCopy_src.GetObjectPtr(ref outputFramebufferCopy_handle, ref outputFramebufferCopy);
outputFramebufferCopy_IntPtr = outputFramebufferCopy_handle.AddrOfPinnedObject();
}
}
#endregion
protected int clockCyclesPerLine;
@ -247,7 +300,10 @@ namespace Essgee.Emulation.Video.Nintendo
screenUsageFlags = new byte[displayActiveWidth, displayActiveHeight];
screenUsageSpriteXCoords = new byte[displayActiveWidth, displayActiveHeight];
screenUsageSpriteSlots = new byte[displayActiveWidth, displayActiveHeight];
outputFramebuffer = new byte[numDisplayPixels * 4];
//outputFramebuffer = new byte[numDisplayPixels * 4];
outputFramebuffer_set = new byte[numDisplayPixels * 4];
outputFramebufferCopy_set = new byte[numDisplayPixels * 4];
for (var y = 0; y < displayActiveHeight; y++)
SetLine(y, 0xFF, 0xFF, 0xFF);
@ -398,7 +454,8 @@ namespace Essgee.Emulation.Video.Nintendo
}
GCHandle? lasyRenderHandle;
GCHandle? lastRenderHandle;
protected virtual void EndHBlank()
{
/* End of scanline reached */
@ -423,16 +480,25 @@ namespace Essgee.Emulation.Video.Nintendo
/* Submit screen for rendering */
// 固定数组,防止垃圾回收器移动它
var bitmapcolorRect_handle = GCHandle.Alloc(outputFramebuffer.Clone() as byte[], GCHandleType.Pinned);
// 获取数组的指针
IntPtr mFrameDataPtr = bitmapcolorRect_handle.AddrOfPinnedObject();
//// 固定数组,防止垃圾回收器移动它
//var bitmapcolorRect_handle = GCHandle.Alloc(outputFramebuffer_src.Clone() as byte[], GCHandleType.Pinned);
//// 获取数组的指针
//IntPtr mFrameDataPtr = bitmapcolorRect_handle.AddrOfPinnedObject();
for (int i = 0; i < outputFramebufferLength; i++)
{
outputFramebufferCopy[i] = outputFramebuffer[i];
}
IntPtr mFrameDataPtr = outputFramebufferCopy_IntPtr;
var eventArgs = RenderScreenEventArgs.Create(displayActiveWidth, displayActiveHeight, mFrameDataPtr);
OnRenderScreen(eventArgs);
eventArgs.Release();
if (lasyRenderHandle != null)
lasyRenderHandle.Value.Free();
lasyRenderHandle = bitmapcolorRect_handle;
//if (lastRenderHandle.HasValue)
// lastRenderHandle.Value.Free();
//lastRenderHandle = bitmapcolorRect_handle;
//OnRenderScreen(new RenderScreenEventArgs(displayActiveWidth, displayActiveHeight, outputFramebuffer.Clone() as byte[]));
}
@ -665,9 +731,12 @@ namespace Essgee.Emulation.Video.Nintendo
protected virtual void WriteColorToFramebuffer(byte c, int address)
{
outputFramebuffer[address + 0] = colorValuesBgr[c & 0x03][0];
outputFramebuffer[address + 1] = colorValuesBgr[c & 0x03][1];
outputFramebuffer[address + 2] = colorValuesBgr[c & 0x03][2];
//outputFramebuffer[address + 0] = colorValuesBgr[c & 0x03][0];
//outputFramebuffer[address + 1] = colorValuesBgr[c & 0x03][1];
//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;
}

View File

@ -300,13 +300,13 @@ namespace AxibugEmuOnline.Client.ClientCore
{
case E_LogType.Debug:
case E_LogType.Info:
Debug.Log("[AxiNet]:" + msg);
Debug.Log(msg);
break;
case E_LogType.Warning:
Debug.LogWarning("[AxiNet]:" + msg);
Debug.LogWarning(msg);
break;
case E_LogType.Error:
Debug.LogError("[AxiNet]:" + msg);
Debug.LogError(msg);
break;
}
}

View File

@ -34,7 +34,7 @@ public class UEGSoundPlayer : MonoBehaviour, AxiAudioPull
private void OnEnable()
{
App.audioMgr.RegisterStream(nameof(UEssgee), AudioSettings.outputSampleRate, this);
App.audioMgr.RegisterStream(nameof(UEssgee), sampleRate, this);
}
void OnDisable()

View File

@ -37,7 +37,7 @@ namespace AxibugEmuOnline.Client
private void OnEnable()
{
App.audioMgr.RegisterStream(nameof(NesEmulator), AudioSettings.outputSampleRate, this);
App.audioMgr.RegisterStream(nameof(NesEmulator), null, this);
}
void OnDisable()

View File

@ -47,7 +47,7 @@ public class SGSoundPlayer : MonoBehaviour, AxiAudioPull
private void OnEnable()
{
App.audioMgr.RegisterStream(nameof(UStoicGoose), AudioSettings.outputSampleRate, this);
App.audioMgr.RegisterStream(nameof(UStoicGoose), null, this);
}
void OnDisable()

View File

@ -78,7 +78,7 @@ namespace AxibugEmuOnline.Client
//函数仅处理设备变化的情况,非设备变化不再本函数处理,避免核心采样率变化和本处循环调用
if (deviceWasChanged)
{
ResetAudioCfg();
ResetAudioCfg(AudioSettings.outputSampleRate);
//AudioConfiguration config = AudioSettings.GetConfiguration();
//AudioSettings.Reset(config);
//TODO 重新播放音效但是DSP不用若有UI BGM后续 这里加重播
@ -119,20 +119,22 @@ namespace AxibugEmuOnline.Client
/// </summary>
/// <param name="channelId">通道标识符 (e.g., "NES", "MAME")</param>
/// <param name="inputSampleRate">该通道的原始采样率</param>
public void RegisterStream(string channelId, int inputSampleRate, AxiAudioPull audioPullHandle)
public void RegisterStream(string channelId, int? inputSampleRate, AxiAudioPull audioPullHandle)
{
_audioStreams = null;
_audioStreams = new AudioStreamData(channelId, inputSampleRate, audioPullHandle);
ResetAudioCfg();
_audioStreams = new AudioStreamData(channelId,
inputSampleRate.HasValue ? inputSampleRate.Value : AudioSettings.outputSampleRate
, audioPullHandle);
ResetAudioCfg(inputSampleRate);
}
private void ResetAudioCfg()
private void ResetAudioCfg(int? inputSampleRate)
{
// 获取当前音频配置
AudioConfiguration config = AudioSettings.GetConfiguration();
// 设置目标音频配置
config.sampleRate = 48000; // 采样率为 44100Hz
config.sampleRate = inputSampleRate.HasValue ? inputSampleRate.Value : 48000; // 采样率为 44100Hz
config.numRealVoices = 32; // 设置最大音频源数量(可选)
config.numVirtualVoices = 512; // 设置虚拟音频源数量(可选)
config.dspBufferSize = 1024; // 设置 DSP 缓冲区大小(可选)

View File

@ -12,7 +12,7 @@ PlayerSettings:
targetDevice: 2
useOnDemandResources: 0
accelerometerFrequency: 60
companyName: AlienTechnology
companyName: axibug
productName: AxibugEmuOnline.Client
defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0}
@ -59,8 +59,8 @@ PlayerSettings:
iosShowActivityIndicatorOnLoading: -1
androidShowActivityIndicatorOnLoading: -1
iosUseCustomAppBackgroundBehavior: 0
allowedAutorotateToPortrait: 1
allowedAutorotateToPortraitUpsideDown: 1
allowedAutorotateToPortrait: 0
allowedAutorotateToPortraitUpsideDown: 0
allowedAutorotateToLandscapeRight: 1
allowedAutorotateToLandscapeLeft: 1
useOSAutorotation: 1
@ -141,7 +141,8 @@ PlayerSettings:
visionOSBundleVersion: 1.0
tvOSBundleVersion: 1.0
bundleVersion: 1.0
preloadedAssets: []
preloadedAssets:
- {fileID: -944628639613478452, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1
@ -163,7 +164,7 @@ PlayerSettings:
androidMaxAspectRatio: 2.1
androidMinAspectRatio: 1
applicationIdentifier:
Android: com.AlienTechnology.AxibugEmuOnline.Client
Android: com.axibug.AxibugEmuOnline.Client
Standalone: com.AlienTechnology.AxibugEmuOnline.Client
buildNumber:
Standalone: 0
@ -381,6 +382,103 @@ PlayerSettings:
m_Height: 36
m_Kind: 1
m_SubKind:
- m_BuildTarget: iPhone
m_Icons:
- m_Textures: []
m_Width: 180
m_Height: 180
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 167
m_Height: 167
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 152
m_Height: 152
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 76
m_Height: 76
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 87
m_Height: 87
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 60
m_Height: 60
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 20
m_Height: 20
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 1024
m_Height: 1024
m_Kind: 4
m_SubKind: App Store
m_BuildTargetBatching: []
m_BuildTargetShaderSettings: []
m_BuildTargetGraphicsJobs: []
@ -697,7 +795,7 @@ PlayerSettings:
additionalCompilerArguments: {}
platformArchitecture: {}
scriptingBackend:
Android: 0
Android: 1
Standalone: 1
il2cppCompilerConfiguration: {}
il2cppCodeGeneration: {}
@ -732,15 +830,15 @@ PlayerSettings:
m_RenderingPath: 1
m_MobileRenderingPath: 1
metroPackageName: AxibugEmuOnline.Client
metroPackageVersion:
metroCertificatePath:
metroPackageVersion: 1.0.0.0
metroCertificatePath: Assets/WSATestCertificate.pfx
metroCertificatePassword:
metroCertificateSubject:
metroCertificateIssuer:
metroCertificateNotAfter: 0000000000000000
metroCertificateSubject: AlienTechnology
metroCertificateIssuer: AlienTechnology
metroCertificateNotAfter: 80590dc13f48dd01
metroApplicationDescription: AxibugEmuOnline.Client
wsaImages: {}
metroTileShortName:
metroTileShortName: AxibugEmuOnline.Client
metroTileShowName: 0
metroMediumTileShowName: 0
metroLargeTileShowName: 0