diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index af9b2ac..1e249c4 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -147,16 +147,6 @@ return null; } - private static void AddressRange(Scope scope, out int start, out int end) - { - var symbol = scope.Symbol; - Debug.Assert(symbol != null); - start = symbol.Value; - end = start + scope.Size - 1; - } - - private static bool AddressContained(int address, int start, int end) => (address >= start) && (address <= end); - private int LocateScope(int address) { var low = 0; @@ -167,23 +157,27 @@ var mid = low + (high - low) / 2; var scope = this.AddressableScopes[mid]; - AddressRange(scope, out var start, out var end); - if (AddressContained(address, start, end)) - { - return mid; - } + var symbol = scope.Symbol; + Debug.Assert(symbol != null); + var start = symbol.Value; - // If referenced scope greater, ignore left half - if (end < address) - { - low = mid + 1; - } - // If referenced scope is smaller, ignore right half - else + if (address < start) { high = mid - 1; } + else + { + var end = start + scope.Size; + if (address >= end) + { + low = mid + 1; + } + else + { + return mid; + } + } } // If we reach here, then scope was not present diff --git a/M6502/M6502.Test/Board.cs b/M6502/M6502.Test/Board.cs index 240ff7a..c66ade7 100644 --- a/M6502/M6502.Test/Board.cs +++ b/M6502/M6502.Test/Board.cs @@ -37,7 +37,7 @@ namespace M6502.Test this.symbols.Parse(string.IsNullOrEmpty(this.configuration.Symbols) ? string.Empty : this.configuration.RomDirectory + "/" + this.configuration.Symbols); - this.profiler = new Profiler(this.CPU, this.disassembler, this.symbols, this.configuration.Profile, this.configuration.Profile); + this.profiler = new Profiler(this.CPU, this.disassembler, this.symbols, this.configuration.Profile); } public M6502 CPU { get; } diff --git a/M6502/Profiler.cs b/M6502/Profiler.cs index 53b04d0..596777b 100644 --- a/M6502/Profiler.cs +++ b/M6502/Profiler.cs @@ -1,5 +1,7 @@ namespace EightBit { + using System.Diagnostics; + public sealed class Profiler { private readonly int[] instructionCounts; @@ -15,7 +17,7 @@ private int priorCycleCount; private ushort executingAddress; - public Profiler(M6502 processor, Disassembler disassembler, Files.Symbols.Parser symbols, bool countInstructions, bool profileAddresses) + public Profiler(M6502 processor, Disassembler disassembler, Files.Symbols.Parser symbols, bool activate) { ArgumentNullException.ThrowIfNull(processor); ArgumentNullException.ThrowIfNull(disassembler); @@ -25,20 +27,10 @@ this.disassembler = disassembler; this.symbols = symbols; - if (profileAddresses || countInstructions) + if (activate) { - this.processor.ExecutingInstruction += this.Processor_ExecutingInstruction_Prequel; - this.processor.ExecutedInstruction += this.Processor_ExecutedInstruction_Sequal; - } - if (profileAddresses) - { - this.processor.ExecutingInstruction += this.Processor_ExecutingInstruction_ProfileAddresses; - this.processor.ExecutedInstruction += this.Processor_ExecutedInstruction_ProfileAddresses; - } - - if (countInstructions) - { - this.processor.ExecutingInstruction += this.Processor_ExecutingInstruction_CountInstructions; + this.processor.ExecutingInstruction += this.Processor_ExecutingInstruction; + this.processor.ExecutedInstruction += this.Processor_ExecutedInstruction; } this.instructionCounts = new int[0x100]; @@ -111,8 +103,11 @@ foreach (var scopeCycle in this.scopeCycles) { var name = scopeCycle.Key; + Debug.Assert(name != null); var cycles = scopeCycle.Value; - var count = this.addressCounts[this.symbols.LookupLabel(name).Value]; + var symbol = this.symbols.LookupLabel(name); + Debug.Assert(symbol != null); + var count = this.addressCounts[symbol.Value]; this.OnEmitScope(name, cycles, count); } } @@ -122,43 +117,34 @@ } } - private void Processor_ExecutingInstruction_Prequel(object? sender, EventArgs e) + private void Processor_ExecutingInstruction(object? sender, EventArgs e) { + // Everything needs this this.executingAddress = this.processor.PC.Word; - } - private void Processor_ExecutedInstruction_Sequal(object? sender, EventArgs e) - { - this.TotalCycleCount += this.processor.Cycles; - } - - private void Processor_ExecutingInstruction_ProfileAddresses(object? sender, EventArgs e) - { this.priorCycleCount = this.processor.Cycles; ++this.addressCounts[this.executingAddress]; - } - - private void Processor_ExecutingInstruction_CountInstructions(object? sender, EventArgs e) - { ++this.instructionCounts[this.processor.Bus.Peek(this.executingAddress)]; } - private void Processor_ExecutedInstruction_ProfileAddresses(object? sender, EventArgs e) + 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[address] += cycles; - var addressScope = this.symbols.LookupScope(address); - if (addressScope != null) + var scope = this.symbols.LookupScope(address); + if (scope != null) { - if (!this.scopeCycles.ContainsKey(addressScope.Name)) + var name = scope.Name; + Debug.Assert(name != null); + if (!this.scopeCycles.TryAdd(name, 0)) { - this.scopeCycles[addressScope.Name] = 0; + this.scopeCycles[name] += cycles; } - - this.scopeCycles[addressScope.Name] += cycles; } }