From 25dcb0c4c01738a07cdfbd0e53af3c208f119b8d Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sun, 14 Jun 2026 21:53:56 +0100 Subject: [PATCH] More straightforward to read test harness results --- EightBit/TestHarness.cs | 48 +++++++++++++++++++++-------------------- Z80/Z80.Test/Program.cs | 2 +- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/EightBit/TestHarness.cs b/EightBit/TestHarness.cs index e07032e..e0ffcb9 100644 --- a/EightBit/TestHarness.cs +++ b/EightBit/TestHarness.cs @@ -6,47 +6,49 @@ namespace EightBit { using System.Diagnostics; - public class TestHarness(TBus board, TProcessor cpu) where TBus : Bus where TProcessor : Processor + public class TestHarness(TBus board, TProcessor cpu, int targetClocksPerSecond = 2_000_000) where TBus : Bus where TProcessor : Processor { private readonly Stopwatch _timer = new(); private readonly TBus _board = board; private readonly TProcessor _cpu = cpu; + private readonly int _targetClocksPerSecond = targetClocksPerSecond; private long _totalCycles; - private TimeSpan _totalUserProcessorTime; public long ElapsedMilliseconds => this._timer.ElapsedMilliseconds; - public float ElapsedSeconds => this.ElapsedMilliseconds / 1000.0f; + public TimeSpan ActualElapsed => TimeSpan.FromMilliseconds(this.ElapsedMilliseconds); - public float CyclesPerSecond => this._totalCycles / this.ElapsedSeconds; + public double CyclesPerSecond => this._totalCycles / this.ActualElapsed.TotalSeconds; + + public double ActualClockSpeed => this.CyclesPerSecond / 1_000_000; + + public int TargetClocksPerSecond => this._targetClocksPerSecond; + + public double TargetClockSpeed => this.TargetClocksPerSecond / 1_000_000; + + public long TotalCycles => this._totalCycles; + + public TimeSpan TargetElapsed => TimeSpan.FromSeconds(this.TotalCycles / (double)this.TargetClocksPerSecond); public void Run() { - using var process = Process.GetCurrentProcess(); + this._timer.Start(); + + this._board.Initialize(); + this._board.RaisePOWER(); + + while (this._cpu.Powered) { - this._timer.Start(); - var startingUsage = process.UserProcessorTime; - - this._board.Initialize(); - this._board.RaisePOWER(); - - while (this._cpu.Powered) - { - this._totalCycles += this._cpu.Step(); - } - - var finishingUsage = process.UserProcessorTime; - this._timer.Stop(); - this._totalUserProcessorTime = finishingUsage - startingUsage; + this._totalCycles += this._cpu.Step(); } + this._timer.Stop(); + Console.Out.WriteLine(); Console.Out.WriteLine($"Guest cycles = {this._totalCycles:N0}"); - Console.Out.WriteLine($"Seconds = {this.ElapsedSeconds}"); - - Console.Out.WriteLine($"{this.CyclesPerSecond / 1000000} MHz"); - Console.Out.WriteLine($"Processor time = {this._totalUserProcessorTime.TotalSeconds:g}"); + Console.Out.WriteLine($"Elapsed time (at {this.ActualClockSpeed:g3}MHz actual) = {this.ActualElapsed}"); + Console.Out.WriteLine($"Elapsed time (at {(this.TargetClockSpeed):g3}MHz target) = {this.TargetElapsed}"); } } } diff --git a/Z80/Z80.Test/Program.cs b/Z80/Z80.Test/Program.cs index 2ed1059..bb1e017 100644 --- a/Z80/Z80.Test/Program.cs +++ b/Z80/Z80.Test/Program.cs @@ -17,7 +17,7 @@ namespace Z80.Test #endif var board = new Board(configuration); - var harness = new TestHarness(board, board.CPU); + var harness = new TestHarness(board, board.CPU, 4_000_000); harness.Run(); } }