Add nes timing mechanism

This commit is contained in:
KallDrexx
2025-10-21 00:26:42 -04:00
parent 93045f1324
commit 795d5a11c2
2 changed files with 30 additions and 2 deletions
+29 -1
View File
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@@ -16,19 +17,25 @@ public class MonogameApp : Game, INesDisplay, INesInput
private readonly ControllerState _controllerState = new();
private readonly object _synchronizationLock = new();
private readonly Color[] _pixelColors = new Color[Width * Height];
private readonly bool _trackTime;
private SpriteBatch _spriteBatch = null!;
private Texture2D _texture = null!;
private bool _readyToContinue;
private bool _displayQuit; // Tells the PPU that the display quit, and thus Monitor will never be pulsed
private Stopwatch _timer = new();
private TimeSpan _totalTime;
private int _frameCountSinceLastTimer;
public Task? NesCodeTask { get; set; }
public MonogameApp()
public MonogameApp(bool trackTime)
{
_graphicsDeviceManager = new GraphicsDeviceManager(this);
IsMouseVisible = true;
Window.AllowUserResizing = true;
_trackTime = trackTime;
}
public void RenderFrame(RgbColor[] pixels)
@@ -39,6 +46,22 @@ public class MonogameApp : Game, INesDisplay, INesInput
throw new InvalidOperationException(message);
}
if (_trackTime)
{
_timer.Stop();
_totalTime += _timer.Elapsed;
_frameCountSinceLastTimer++;
if (_frameCountSinceLastTimer == 120)
{
var averageTime = _totalTime / _frameCountSinceLastTimer;
Console.WriteLine($"Average NES time: {averageTime.TotalMilliseconds}ms");
_totalTime = TimeSpan.Zero;
_frameCountSinceLastTimer = 0;
}
}
lock (_synchronizationLock)
{
for (var x = 0; x < _pixelColors.Length; x++)
@@ -52,6 +75,11 @@ public class MonogameApp : Game, INesDisplay, INesInput
Monitor.Wait(_synchronizationLock);
}
if (_trackTime)
{
_timer.Restart();
}
_readyToContinue = false;
}
}
+1 -1
View File
@@ -49,7 +49,7 @@ static (MonogameApp, CancellationTokenSource, MemoryBus, NesHal) SetupHardware(
{
Console.WriteLine("Setting up HAL and JIT compiler...");
var monogameApp = new MonogameApp();
var monogameApp = new MonogameApp(false);
var cancellationTokenSource = new CancellationTokenSource();
var ppu = new Ppu(chrRomData, romInfo2.MirroringType, monogameApp);