From fa46edc8baa8a7e113fe040202a66f110e5e416b Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Mon, 10 Jun 2024 09:48:28 +0100 Subject: [PATCH] Instruction execution begins when the SYNC line is raised. --- M6502/ProfileLineEventArgs.cs | 4 +++- M6502/Profiler.cs | 28 ++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/M6502/ProfileLineEventArgs.cs b/M6502/ProfileLineEventArgs.cs index c245f8a..8667adb 100644 --- a/M6502/ProfileLineEventArgs.cs +++ b/M6502/ProfileLineEventArgs.cs @@ -1,9 +1,11 @@ namespace EightBit { - public class ProfileLineEventArgs(string source, int cycles) : EventArgs + public class ProfileLineEventArgs(string source, int cycles, int count) : EventArgs { public string Source { get; } = source; public int Cycles { get; } = cycles; + + public int Count { get; } = count; } } \ No newline at end of file diff --git a/M6502/Profiler.cs b/M6502/Profiler.cs index 596777b..7a8a64d 100644 --- a/M6502/Profiler.cs +++ b/M6502/Profiler.cs @@ -14,7 +14,6 @@ private readonly Disassembler disassembler; private readonly Files.Symbols.Parser symbols; - private int priorCycleCount; private ushort executingAddress; public Profiler(M6502 processor, Disassembler disassembler, Files.Symbols.Parser symbols, bool activate) @@ -29,7 +28,7 @@ if (activate) { - this.processor.ExecutingInstruction += this.Processor_ExecutingInstruction; + this.processor.RaisingSYNC += this.Processor_RaisingSYNC; this.processor.ExecutedInstruction += this.Processor_ExecutedInstruction; } @@ -40,7 +39,6 @@ this.scopeCycles = []; } - public event EventHandler? StartingOutput; public event EventHandler? FinishedOutput; @@ -82,13 +80,15 @@ { // If there are any cycles associated var cycles = this.addressProfiles[i]; + var count = this.addressCounts[i]; if (cycles > 0) { + Debug.Assert(count > 0); var address = (ushort)i; // Dump a profile/disassembly line var source = this.disassembler.Disassemble(address); - this.OnEmitLine(source, cycles); + this.OnEmitLine(source, cycles, count); } } } @@ -117,33 +117,29 @@ } } - private void Processor_ExecutingInstruction(object? sender, EventArgs e) + private void Processor_RaisingSYNC(object? sender, EventArgs e) { // Everything needs this - this.executingAddress = this.processor.PC.Word; + this.executingAddress = this.processor.Bus.Address.Word; - this.priorCycleCount = this.processor.Cycles; ++this.addressCounts[this.executingAddress]; - ++this.instructionCounts[this.processor.Bus.Peek(this.executingAddress)]; + ++this.instructionCounts[this.processor.Bus.Data]; } private void Processor_ExecutedInstruction(object? sender, EventArgs e) { this.TotalCycleCount += this.processor.Cycles; - var address = this.executingAddress; - var cycles = this.processor.Cycles - this.priorCycleCount; + this.addressProfiles[this.executingAddress] += this.processor.Cycles; - this.addressProfiles[address] += cycles; - - var scope = this.symbols.LookupScope(address); + var scope = this.symbols.LookupScope(this.executingAddress); if (scope != null) { var name = scope.Name; Debug.Assert(name != null); if (!this.scopeCycles.TryAdd(name, 0)) { - this.scopeCycles[name] += cycles; + this.scopeCycles[name] += this.processor.Cycles; } } } @@ -178,9 +174,9 @@ this.FinishedScopeOutput?.Invoke(this, EventArgs.Empty); } - private void OnEmitLine(string source, int cycles) + private void OnEmitLine(string source, int cycles, int count) { - this.EmitLine?.Invoke(this, new ProfileLineEventArgs(source, cycles)); + this.EmitLine?.Invoke(this, new ProfileLineEventArgs(source, cycles, count)); } private void OnEmitScope(string scope, int cycles, int count)