From a5eed89b26912ac348cb782ae3150e7efad64df8 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Mon, 12 May 2025 21:08:39 +0100 Subject: [PATCH] Tidy up all the 6809 stuff --- MC6809/Disassembler.cs | 172 +++++++++++++---------------- MC6809/MC6809.UnitTest/AbxTests.cs | 1 + MC6809/MC6809.UnitTest/AdcTests.cs | 1 + MC6809/MC6809.UnitTest/AddTests.cs | 1 + MC6809/MC6809.UnitTest/AndTests.cs | 1 + MC6809/MC6809.UnitTest/AslTests.cs | 1 + MC6809/MC6809.UnitTest/AsrTests.cs | 1 + MC6809/MC6809.UnitTest/BgtTests.cs | 1 + MC6809/MC6809.UnitTest/BhiTests.cs | 1 + MC6809/MC6809.UnitTest/BitTests.cs | 1 + MC6809/MC6809.UnitTest/BleTests.cs | 1 + MC6809/MC6809.UnitTest/Board.cs | 2 + MC6809/MC6809.UnitTest/ClrTests.cs | 1 + MC6809/MC6809.UnitTest/CmpTests.cs | 1 + MC6809/MC6809.UnitTest/ComTests.cs | 1 + MC6809/MC6809.UnitTest/DaaTests.cs | 1 + MC6809/MC6809.UnitTest/DecTests.cs | 1 + MC6809/MC6809.UnitTest/EorTests.cs | 1 + MC6809/MC6809.UnitTest/ExgTests.cs | 1 + MC6809/MC6809.UnitTest/IncTests.cs | 1 + MC6809/MC6809.UnitTest/JsrTests.cs | 1 + MC6809/MC6809.UnitTest/LdTests.cs | 1 + MC6809/MC6809.UnitTest/LeaTests.cs | 1 + MC6809/MC6809.UnitTest/LsrTests.cs | 11 +- MC6809/MC6809.UnitTest/MulTests.cs | 5 +- MC6809/MC6809.UnitTest/NegTests.cs | 1 + MC6809/MC6809.UnitTest/OrTests.cs | 1 + MC6809/MC6809.UnitTest/PshTests.cs | 1 + MC6809/MC6809.UnitTest/PulTests.cs | 1 + MC6809/MC6809.UnitTest/RolTests.cs | 1 + MC6809/MC6809.UnitTest/RorTests.cs | 3 +- MC6809/MC6809.UnitTest/RtsTests.cs | 1 + MC6809/MC6809.UnitTest/SbcTests.cs | 1 + MC6809/MC6809.UnitTest/SexTests.cs | 1 + MC6809/MC6809.UnitTest/SubTests.cs | 1 + MC6809/MC6809.UnitTest/TfrTests.cs | 1 + MC6809/MC6809.UnitTest/TstTests.cs | 1 + MC6809/MC6809.cs | 69 ++++++------ MC6809/ProfileEventArgs.cs | 10 +- MC6809/ProfileLineEventArgs.cs | 16 +-- MC6809/ProfileScopeEventArgs.cs | 19 +--- MC6809/Profiler.cs | 43 +++----- MC6809/StatusBits.cs | 6 +- 43 files changed, 187 insertions(+), 201 deletions(-) diff --git a/MC6809/Disassembler.cs b/MC6809/Disassembler.cs index 49dcbcf..956adc1 100644 --- a/MC6809/Disassembler.cs +++ b/MC6809/Disassembler.cs @@ -1,35 +1,28 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; - using System.Collections.Generic; + using EightBit; - public sealed class Disassembler + public sealed class Disassembler(Bus bus, MC6809 targetProcessor) { private ushort address = 0xffff; - private bool prefix10 = false; - private bool prefix11 = false; + private bool prefix10; + private bool prefix11; - public Disassembler(Bus bus, MC6809 targetProcessor) - { - this.BUS = bus; - this.CPU = targetProcessor; - } - - public bool Pause { get; set; } = false; + public bool Pause { get; set; } public bool Ignore => this.CPU.HALT.Lowered() || this.CPU.RESET.Lowered() || this.CPU.NMI.Lowered() - || (this.CPU.FIRQ.Lowered() && this.CPU.FastInterruptMasked == 0) - || (this.CPU.INT.Lowered() && this.CPU.InterruptMasked == 0); + || this.CPU.FIRQ.Lowered() && this.CPU.FastInterruptMasked == 0 + || this.CPU.INT.Lowered() && this.CPU.InterruptMasked == 0; - private Bus BUS { get; } + private Bus BUS { get; } = bus; - private MC6809 CPU { get; } + private MC6809 CPU { get; } = targetProcessor; public string Trace(ushort current) { @@ -45,7 +38,11 @@ namespace EightBit return $"{current:x4}|{disassembled}\t\tcc={cc:x2} a={a:x2} b={b:x2} dp={dp:x2} x={x:x4} y={y:x4} u={u:x4} s={s:x4}"; } - public string Trace(Register16 current) => this.Trace(current.Word); + public string Trace(Register16 current) + { + ArgumentNullException.ThrowIfNull(current, nameof(current)); + return this.Trace(current.Word); + } public string Trace() => this.Trace(this.CPU.PC); @@ -67,19 +64,14 @@ namespace EightBit private static string RR(int which) { - switch (which) + return which switch { - case 0b00: - return "X"; - case 0b01: - return "Y"; - case 0b10: - return "U"; - case 0b11: - return "S"; - default: - throw new ArgumentOutOfRangeException(nameof(which), which, "Register specification is unknown"); - } + 0b00 => "X", + 0b01 => "Y", + 0b10 => "U", + 0b11 => "S", + _ => throw new ArgumentOutOfRangeException(nameof(which), which, "Register specification is unknown"), + }; } private static string WrapIndirect(string what, bool indirect) @@ -91,40 +83,28 @@ namespace EightBit private static string ReferenceTransfer8(int specifier) { - switch (specifier) + return specifier switch { - case 0b1000: - return "A"; - case 0b1001: - return "B"; - case 0b1010: - return "CC"; - case 0b1011: - return "DP"; - default: - throw new ArgumentOutOfRangeException(nameof(specifier), specifier, "8bit register specification is unknown"); - } + 0b1000 => "A", + 0b1001 => "B", + 0b1010 => "CC", + 0b1011 => "DP", + _ => throw new ArgumentOutOfRangeException(nameof(specifier), specifier, "8bit register specification is unknown"), + }; } private static string ReferenceTransfer16(int specifier) { - switch (specifier) + return specifier switch { - case 0b0000: - return "D"; - case 0b0001: - return "X"; - case 0b0010: - return "Y"; - case 0b0011: - return "U"; - case 0b0100: - return "S"; - case 0b0101: - return "PC"; - default: - throw new ArgumentOutOfRangeException(nameof(specifier), specifier, "16bit register specification is unknown"); - } + 0b0000 => "D", + 0b0001 => "X", + 0b0010 => "Y", + 0b0011 => "U", + 0b0100 => "S", + 0b0101 => "PC", + _ => throw new ArgumentOutOfRangeException(nameof(specifier), specifier, "16bit register specification is unknown"), + }; } private string Disassemble(int current) => this.Disassemble((ushort)current); @@ -609,66 +589,64 @@ namespace EightBit //// - private string Address_direct(string mnemomic) + private string Address_direct(string mnemonic) { var offset = this.GetByte(++this.address); - return $"{offset:x2}\t{mnemomic}\t${offset:x2}"; + return $"{offset:x2}\t{mnemonic}\t${offset:x2}"; } - private string Address_indexed(string mnemomic) + private string Address_indexed(string mnemonic) { var type = this.GetByte(++this.address); var r = RR((type & (byte)(Bits.Bit6 | Bits.Bit5)) >> 5); - - byte byte8 = 0xff; - ushort word = 0xffff; - var output = $"{type:x2}"; if ((type & (byte)Bits.Bit7) != 0) { var indirect = (type & (byte)Bits.Bit4) != 0; + ushort word; + byte byte8; switch (type & (byte)Mask.Four) { case 0b0000: // ,R+ - output += $"\t{mnemomic}\t{WrapIndirect($",{r}+", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($",{r}+", indirect)}"; break; case 0b0001: // ,R++ - output += $"\t{mnemomic}\t{WrapIndirect($",{r}++", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($",{r}++", indirect)}"; break; case 0b0010: // ,-R - output += $"\t{mnemomic}\t{WrapIndirect($",-{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($",-{r}", indirect)}"; break; case 0b0011: // ,--R - output += $"\t{mnemomic}\t{WrapIndirect($",--{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($",--{r}", indirect)}"; break; case 0b0100: // ,R - output += $"\t{mnemomic}\t{WrapIndirect($",{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($",{r}", indirect)}"; break; case 0b0101: // B,R - output += $"\t{mnemomic}\t{WrapIndirect($"B,{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($"B,{r}", indirect)}"; break; case 0b0110: // A,R - output += $"\t{mnemomic}\t{WrapIndirect($"A,{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($"A,{r}", indirect)}"; break; case 0b1000: // n,R (eight-bit) byte8 = this.GetByte(++this.address); - output += $"{byte8:x2}\t{mnemomic}\t{WrapIndirect($"{byte8:x2},{r}", indirect)}"; + output += $"{byte8:x2}\t{mnemonic}\t{WrapIndirect($"{byte8:x2},{r}", indirect)}"; break; case 0b1001: // n,R (sixteen-bit) word = this.GetWord(++this.address); - output += $"{word:x4}\t{mnemomic}\t{WrapIndirect($"{word:x4},{r}", indirect)}"; + output += $"{word:x4}\t{mnemonic}\t{WrapIndirect($"{word:x4},{r}", indirect)}"; break; case 0b1011: // D,R - output += $"\t{mnemomic}\t{WrapIndirect($"D,{r}", indirect)}"; + output += $"\t{mnemonic}\t{WrapIndirect($"D,{r}", indirect)}"; break; case 0b1100: // n,PCR (eight-bit) byte8 = this.GetByte(++this.address); - output += $"{byte8:x2}\t{mnemomic}\t{WrapIndirect("${(byte)byte8:D},PCR", indirect)}"; + output += $"{byte8:x2}\t{mnemonic}\t{WrapIndirect("${(byte)byte8:D},PCR", indirect)}"; break; case 0b1101: // n,PCR (sixteen-bit) word = this.GetWord(++this.address); - output += $"{word:x4}\t{mnemomic}\t{WrapIndirect("${(short)word:D},PCR", indirect)}"; + output += $"{word:x4}\t{mnemonic}\t{WrapIndirect("${(short)word:D},PCR", indirect)}"; break; case 0b1111: // [n] if (!indirect) @@ -677,7 +655,7 @@ namespace EightBit } word = this.GetWord(++this.address); - output += $"{word:x4}\t{mnemomic}\t{WrapIndirect("${word:x4}", indirect)}"; + output += $"{word:x4}\t{mnemonic}\t{WrapIndirect("${word:x4}", indirect)}"; break; default: throw new InvalidOperationException("Invalid index specification used"); @@ -686,53 +664,53 @@ namespace EightBit else { // EA = ,R + 5-bit offset - output += $"\t{mnemomic}\t{Processor.SignExtend(5, type & (byte)Mask.Five)},{r}"; + output += $"\t{mnemonic}\t{Processor.SignExtend(5, type & (byte)Mask.Five)},{r}"; } return output; } - private string Address_extended(string mnemomic) + private string Address_extended(string mnemonic) { var word = this.GetWord(++this.address); - return $"{word:x4}\t{mnemomic}\t${word:x4}"; + return $"{word:x4}\t{mnemonic}\t${word:x4}"; } - private string Address_relative_byte(string mnemomic) + private string Address_relative_byte(string mnemonic) { var byte8 = this.GetByte(++this.address); - return $"{byte8:x2}\t{mnemomic}\t${++this.address + (sbyte)byte8:x4}"; + return $"{byte8:x2}\t{mnemonic}\t${++this.address + (sbyte)byte8:x4}"; } - private string Address_relative_word(string mnemomic) + private string Address_relative_word(string mnemonic) { var word = this.GetWord(++this.address); - return $"{word:x4}\t{mnemomic}\t${++this.address + (short)word:x4}"; + return $"{word:x4}\t{mnemonic}\t${++this.address + (short)word:x4}"; } - private string AM_immediate_byte(string mnemomic) + private string AM_immediate_byte(string mnemonic) { var byte8 = this.GetByte(++this.address); - return $"{byte8:x2}\t{mnemomic}\t#${byte8:x2}"; + return $"{byte8:x2}\t{mnemonic}\t#${byte8:x2}"; } - private string AM_immediate_word(string mnemomic) + private string AM_immediate_word(string mnemonic) { var word = this.GetWord(++this.address); - return $"{word:x4}\t{mnemomic}\t#${word:x4}"; + return $"{word:x4}\t{mnemonic}\t#${word:x4}"; } - private string BranchShort(string mnemomic) => this.Address_relative_byte(mnemomic); + private string BranchShort(string mnemonic) => this.Address_relative_byte(mnemonic); - private string BranchLong(string mnemomic) => this.Address_relative_word(mnemomic); + private string BranchLong(string mnemonic) => this.Address_relative_word(mnemonic); - private string TFR(string mnemomic) + private string TFR(string mnemonic) { var data = this.GetByte(++this.address); var reg1 = Chip.HighNibble(data); var reg2 = Chip.LowNibble(data); - var output = $"{data:x2}\t{mnemomic}\t"; + var output = $"{data:x2}\t{mnemonic}\t"; var type8 = (reg1 & (byte)Bits.Bit3) != 0; // 8 bit? return type8 @@ -750,10 +728,10 @@ namespace EightBit private string PshU() => this.PshX("PSHU", "S"); - private string PulX(string mnemomic, string upon) + private string PulX(string mnemonic, string upon) { var data = this.GetByte(++this.address); - var output = $"{data:x2}\t{mnemomic}\t"; + var output = $"{data:x2}\t{mnemonic}\t"; var registers = new List(); if ((data & (byte)Bits.Bit0) != 0) @@ -799,10 +777,10 @@ namespace EightBit return output + string.Join(",", registers); } - private string PshX(string mnemomic, string upon) + private string PshX(string mnemonic, string upon) { var data = this.GetByte(++this.address); - var output = $"{data:x2}\t{mnemomic}\t"; + var output = $"{data:x2}\t{mnemonic}\t"; var registers = new List(); if ((data & (byte)Bits.Bit7) != 0) diff --git a/MC6809/MC6809.UnitTest/AbxTests.cs b/MC6809/MC6809.UnitTest/AbxTests.cs index 1d4d779..436b70f 100644 --- a/MC6809/MC6809.UnitTest/AbxTests.cs +++ b/MC6809/MC6809.UnitTest/AbxTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/AdcTests.cs b/MC6809/MC6809.UnitTest/AdcTests.cs index e67503a..5b89e8f 100644 --- a/MC6809/MC6809.UnitTest/AdcTests.cs +++ b/MC6809/MC6809.UnitTest/AdcTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/AddTests.cs b/MC6809/MC6809.UnitTest/AddTests.cs index 5e8f8e1..e9771e3 100644 --- a/MC6809/MC6809.UnitTest/AddTests.cs +++ b/MC6809/MC6809.UnitTest/AddTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/AndTests.cs b/MC6809/MC6809.UnitTest/AndTests.cs index 4c4bb87..a794c9b 100644 --- a/MC6809/MC6809.UnitTest/AndTests.cs +++ b/MC6809/MC6809.UnitTest/AndTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/AslTests.cs b/MC6809/MC6809.UnitTest/AslTests.cs index 5ed96ee..ef868c8 100644 --- a/MC6809/MC6809.UnitTest/AslTests.cs +++ b/MC6809/MC6809.UnitTest/AslTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/AsrTests.cs b/MC6809/MC6809.UnitTest/AsrTests.cs index e97b1ca..2f16f8d 100644 --- a/MC6809/MC6809.UnitTest/AsrTests.cs +++ b/MC6809/MC6809.UnitTest/AsrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/BgtTests.cs b/MC6809/MC6809.UnitTest/BgtTests.cs index 68ea0d4..24b73fc 100644 --- a/MC6809/MC6809.UnitTest/BgtTests.cs +++ b/MC6809/MC6809.UnitTest/BgtTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/BhiTests.cs b/MC6809/MC6809.UnitTest/BhiTests.cs index 8d12032..563e708 100644 --- a/MC6809/MC6809.UnitTest/BhiTests.cs +++ b/MC6809/MC6809.UnitTest/BhiTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/BitTests.cs b/MC6809/MC6809.UnitTest/BitTests.cs index 902c0ab..78c4d05 100644 --- a/MC6809/MC6809.UnitTest/BitTests.cs +++ b/MC6809/MC6809.UnitTest/BitTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/BleTests.cs b/MC6809/MC6809.UnitTest/BleTests.cs index 7bc6767..5bbfb8c 100644 --- a/MC6809/MC6809.UnitTest/BleTests.cs +++ b/MC6809/MC6809.UnitTest/BleTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/Board.cs b/MC6809/MC6809.UnitTest/Board.cs index 40e3e07..1da6556 100644 --- a/MC6809/MC6809.UnitTest/Board.cs +++ b/MC6809/MC6809.UnitTest/Board.cs @@ -4,6 +4,8 @@ namespace EightBit { + using MC6809; + public sealed class Board : EightBit.Bus { private readonly Ram ram = new Ram(0x10000); // 0000 - FFFF, 64K RAM diff --git a/MC6809/MC6809.UnitTest/ClrTests.cs b/MC6809/MC6809.UnitTest/ClrTests.cs index f7b3533..3ce082b 100644 --- a/MC6809/MC6809.UnitTest/ClrTests.cs +++ b/MC6809/MC6809.UnitTest/ClrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/CmpTests.cs b/MC6809/MC6809.UnitTest/CmpTests.cs index 97389c3..4d97ccb 100644 --- a/MC6809/MC6809.UnitTest/CmpTests.cs +++ b/MC6809/MC6809.UnitTest/CmpTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/ComTests.cs b/MC6809/MC6809.UnitTest/ComTests.cs index 511d54a..530412f 100644 --- a/MC6809/MC6809.UnitTest/ComTests.cs +++ b/MC6809/MC6809.UnitTest/ComTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/DaaTests.cs b/MC6809/MC6809.UnitTest/DaaTests.cs index 4f2e195..b2c7419 100644 --- a/MC6809/MC6809.UnitTest/DaaTests.cs +++ b/MC6809/MC6809.UnitTest/DaaTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/DecTests.cs b/MC6809/MC6809.UnitTest/DecTests.cs index 97a96d4..2e57d72 100644 --- a/MC6809/MC6809.UnitTest/DecTests.cs +++ b/MC6809/MC6809.UnitTest/DecTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/EorTests.cs b/MC6809/MC6809.UnitTest/EorTests.cs index 48bf8c2..7f2c251 100644 --- a/MC6809/MC6809.UnitTest/EorTests.cs +++ b/MC6809/MC6809.UnitTest/EorTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/ExgTests.cs b/MC6809/MC6809.UnitTest/ExgTests.cs index 90733ed..3ef61a6 100644 --- a/MC6809/MC6809.UnitTest/ExgTests.cs +++ b/MC6809/MC6809.UnitTest/ExgTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/IncTests.cs b/MC6809/MC6809.UnitTest/IncTests.cs index b92cf36..411fe3f 100644 --- a/MC6809/MC6809.UnitTest/IncTests.cs +++ b/MC6809/MC6809.UnitTest/IncTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/JsrTests.cs b/MC6809/MC6809.UnitTest/JsrTests.cs index bd06403..73535b5 100644 --- a/MC6809/MC6809.UnitTest/JsrTests.cs +++ b/MC6809/MC6809.UnitTest/JsrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; // https://github.com/sorenroug/osnine-java/blob/master/core/src/test/java/org/roug/osnine/BranchAndJumpTest.java diff --git a/MC6809/MC6809.UnitTest/LdTests.cs b/MC6809/MC6809.UnitTest/LdTests.cs index fb8269b..120d468 100644 --- a/MC6809/MC6809.UnitTest/LdTests.cs +++ b/MC6809/MC6809.UnitTest/LdTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/LeaTests.cs b/MC6809/MC6809.UnitTest/LeaTests.cs index 6e0a432..5b5c6e4 100644 --- a/MC6809/MC6809.UnitTest/LeaTests.cs +++ b/MC6809/MC6809.UnitTest/LeaTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/LsrTests.cs b/MC6809/MC6809.UnitTest/LsrTests.cs index 8280ca8..802ab24 100644 --- a/MC6809/MC6809.UnitTest/LsrTests.cs +++ b/MC6809/MC6809.UnitTest/LsrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -46,9 +47,9 @@ namespace EightBit public void TestLSR_a_two() { this.board.Poke(0xb00, 0x44); - this.cpu.CC &= (byte)~StatusBits.CF; - this.cpu.CC |= (byte)StatusBits.VF; - this.cpu.CC |= (byte)StatusBits.NF; + this.cpu.CC = MC6809.ClearBit(this.cpu.CC, (byte)StatusBits.CF); + this.cpu.CC = MC6809.SetBit(this.cpu.CC, (byte)StatusBits.VF); + this.cpu.CC = MC6809.SetBit(this.cpu.CC, (byte)StatusBits.NF); this.cpu.A = 1; this.cpu.PC.Word = 0xb00; @@ -65,8 +66,8 @@ namespace EightBit public void TestLSR_a_three() { this.board.Poke(0xb00, 0x44); - this.cpu.CC &= (byte)~StatusBits.CF; - this.cpu.CC &= (byte)~StatusBits.VF; + this.cpu.CC = MC6809.ClearBit(this.cpu.CC, (byte)StatusBits.CF); + this.cpu.CC = MC6809.ClearBit(this.cpu.CC, (byte)StatusBits.VF); this.cpu.A = 0xb8; this.cpu.PC.Word = 0xb00; diff --git a/MC6809/MC6809.UnitTest/MulTests.cs b/MC6809/MC6809.UnitTest/MulTests.cs index e50492d..6261d47 100644 --- a/MC6809/MC6809.UnitTest/MulTests.cs +++ b/MC6809/MC6809.UnitTest/MulTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -54,8 +55,8 @@ namespace EightBit { this.board.Poke(0xb00, 0x3d); this.cpu.CC = 0; - this.cpu.CC &= (byte)~StatusBits.CF; - this.cpu.CC |= (byte)StatusBits.ZF; + this.cpu.CC = MC6809.ClearBit(this.cpu.CC, (byte)StatusBits.CF); + this.cpu.CC = MC6809.SetBit(this.cpu.CC, (byte)StatusBits.ZF); this.cpu.A = 0xc; this.cpu.B = 0x00; this.cpu.PC.Word = 0xb00; diff --git a/MC6809/MC6809.UnitTest/NegTests.cs b/MC6809/MC6809.UnitTest/NegTests.cs index 78f30d3..119d04c 100644 --- a/MC6809/MC6809.UnitTest/NegTests.cs +++ b/MC6809/MC6809.UnitTest/NegTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/OrTests.cs b/MC6809/MC6809.UnitTest/OrTests.cs index 8a5d8b9..a38b69e 100644 --- a/MC6809/MC6809.UnitTest/OrTests.cs +++ b/MC6809/MC6809.UnitTest/OrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/PshTests.cs b/MC6809/MC6809.UnitTest/PshTests.cs index 2918934..04c1a5c 100644 --- a/MC6809/MC6809.UnitTest/PshTests.cs +++ b/MC6809/MC6809.UnitTest/PshTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/PulTests.cs b/MC6809/MC6809.UnitTest/PulTests.cs index 74f628d..2fd86e9 100644 --- a/MC6809/MC6809.UnitTest/PulTests.cs +++ b/MC6809/MC6809.UnitTest/PulTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/RolTests.cs b/MC6809/MC6809.UnitTest/RolTests.cs index a214b69..ba91e16 100644 --- a/MC6809/MC6809.UnitTest/RolTests.cs +++ b/MC6809/MC6809.UnitTest/RolTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/RorTests.cs b/MC6809/MC6809.UnitTest/RorTests.cs index 012d45f..04bb54f 100644 --- a/MC6809/MC6809.UnitTest/RorTests.cs +++ b/MC6809/MC6809.UnitTest/RorTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -47,7 +48,7 @@ namespace EightBit this.board.Poke(0xb00, 0x56); this.cpu.B = 0x89; this.cpu.CC = 0; - this.cpu.CC &= (byte)~StatusBits.CF; + this.cpu.CC = MC6809.ClearBit(this.cpu.CC, (byte)StatusBits.CF); this.cpu.PC.Word = 0xb00; this.cpu.Step(); diff --git a/MC6809/MC6809.UnitTest/RtsTests.cs b/MC6809/MC6809.UnitTest/RtsTests.cs index e2d9b3c..223f72a 100644 --- a/MC6809/MC6809.UnitTest/RtsTests.cs +++ b/MC6809/MC6809.UnitTest/RtsTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; // https://github.com/sorenroug/osnine-java/blob/master/core/src/test/java/org/roug/osnine/BranchAndJumpTest.java diff --git a/MC6809/MC6809.UnitTest/SbcTests.cs b/MC6809/MC6809.UnitTest/SbcTests.cs index 02a7f26..1d8b0ba 100644 --- a/MC6809/MC6809.UnitTest/SbcTests.cs +++ b/MC6809/MC6809.UnitTest/SbcTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/SexTests.cs b/MC6809/MC6809.UnitTest/SexTests.cs index e6a76e5..5911b8c 100644 --- a/MC6809/MC6809.UnitTest/SexTests.cs +++ b/MC6809/MC6809.UnitTest/SexTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/SubTests.cs b/MC6809/MC6809.UnitTest/SubTests.cs index 5aead4c..2ba8e1b 100644 --- a/MC6809/MC6809.UnitTest/SubTests.cs +++ b/MC6809/MC6809.UnitTest/SubTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/TfrTests.cs b/MC6809/MC6809.UnitTest/TfrTests.cs index ec9c2f7..a0b295a 100644 --- a/MC6809/MC6809.UnitTest/TfrTests.cs +++ b/MC6809/MC6809.UnitTest/TfrTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.UnitTest/TstTests.cs b/MC6809/MC6809.UnitTest/TstTests.cs index fda0837..9231bcd 100644 --- a/MC6809/MC6809.UnitTest/TstTests.cs +++ b/MC6809/MC6809.UnitTest/TstTests.cs @@ -4,6 +4,7 @@ namespace EightBit { + using MC6809; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] diff --git a/MC6809/MC6809.cs b/MC6809/MC6809.cs index bb0394b..a08fd83 100644 --- a/MC6809/MC6809.cs +++ b/MC6809/MC6809.cs @@ -1,8 +1,10 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { + using EightBit; + // Uses some information from: // http://www.cpu-world.com/Arch/6809.html // http://atjs.mbnet.fi/mc6809/Information/6809.htm @@ -73,6 +75,7 @@ namespace EightBit private void OnLoweredNMI() => this.LoweredNMI?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseNMI() { if (this.NMI.Lowered()) @@ -117,6 +120,7 @@ namespace EightBit private void OnLoweredFIRQ() => this.LoweredFIRQ?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseFIRQ() { if (this.FIRQ.Lowered()) @@ -163,6 +167,7 @@ namespace EightBit private void OnLoweredHALT() => this.LoweredHALT?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseHALT() { if (this.HALT.Lowered()) @@ -219,6 +224,7 @@ namespace EightBit private void OnLoweredBA() => this.LoweredBA?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseBA() { if (this.BA.Lowered()) @@ -261,6 +267,7 @@ namespace EightBit private void OnLoweredBS() => this.LoweredBS?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseBS() { if (this.BS.Lowered()) @@ -305,6 +312,7 @@ namespace EightBit private void OnLoweredRW() => this.LoweredRW?.Invoke(this, EventArgs.Empty); + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public void RaiseRW() { if (this.RW.Lowered()) @@ -369,15 +377,15 @@ namespace EightBit public int Carry => this.CC & (byte)StatusBits.CF; - private bool LS => (this.Carry != 0) || (this.Zero != 0); // (C OR Z) + private bool LS => this.Carry != 0 || this.Zero != 0; // (C OR Z) private bool HI => !this.LS; // !(C OR Z) - private bool LT => ((this.Negative >> 3) ^ (this.Overflow >> 1)) != 0; // (N XOR V) + private bool LT => (this.Negative >> 3 ^ this.Overflow >> 1) != 0; // (N XOR V) private bool GE => !this.LT; // !(N XOR V) - private bool LE => (this.Zero != 0) || this.LT; // (Z OR (N XOR V)) + private bool LE => this.Zero != 0 || this.LT; // (Z OR (N XOR V)) private bool GT => !this.LE; // !(Z OR (N XOR V)) @@ -425,14 +433,14 @@ namespace EightBit { var lowAfter = after.Low; var highAfter = after.High; - return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 7)) & (int)Bits.Bit7); + return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ highAfter << 7) & (int)Bits.Bit7); } private byte AdjustOverflow(ushort before, ushort data, uint after) { var lowAfter = (ushort)(after & (uint)Mask.Sixteen); var highAfter = (ushort)(after >> 16); - return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 15)) & (int)Bits.Bit15); + return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ highAfter << 15) & (int)Bits.Bit15); } private byte AdjustHalfCarry(byte before, byte data, byte after) => SetBit(this.CC, StatusBits.HF, (before ^ data ^ after) & (int)Bits.Bit4); @@ -614,19 +622,14 @@ namespace EightBit private Register16 RR(int which) { - switch (which) + return which switch { - case 0b00: - return this.X; - case 0b01: - return this.Y; - case 0b10: - return this.U; - case 0b11: - return this.S; - default: - throw new ArgumentOutOfRangeException(nameof(which), which, "Which does not specify a valid register"); - } + 0b00 => this.X, + 0b01 => this.Y, + 0b10 => this.U, + 0b11 => this.S, + _ => throw new ArgumentOutOfRangeException(nameof(which), which, "Which does not specify a valid register"), + }; } private Register16 Address_indexed() @@ -811,7 +814,7 @@ namespace EightBit this.Tick(2); // Pushing to the S stack means we must be pushing U - this.PushWord(stack, object.ReferenceEquals(stack, this.S) ? this.U : this.S); + this.PushWord(stack, ReferenceEquals(stack, this.S) ? this.U : this.S); } if ((data & (byte)Bits.Bit5) != 0) @@ -894,7 +897,7 @@ namespace EightBit this.Tick(2); // Pulling from the S stack means we must be pulling U - (object.ReferenceEquals(stack, this.S) ? this.U : this.S).Word = this.PopWord(stack).Word; + (ReferenceEquals(stack, this.S) ? this.U : this.S).Word = this.PopWord(stack).Word; } if ((data & (byte)Bits.Bit7) != 0) @@ -941,10 +944,10 @@ namespace EightBit private void EXG(byte data) { - var leftSpecifier = Chip.HighNibble(data); + var leftSpecifier = HighNibble(data); var leftType = leftSpecifier & (int)Bits.Bit3; - var rightSpecifier = Chip.LowNibble(data); + var rightSpecifier = LowNibble(data); var rightType = rightSpecifier & (int)Bits.Bit3; if (leftType == 0) @@ -981,10 +984,10 @@ namespace EightBit private void TFR(byte data) { - var sourceSpecifier = Chip.HighNibble(data); + var sourceSpecifier = HighNibble(data); var sourceType = sourceSpecifier & (int)Bits.Bit3; - var destinationSpecifier = Chip.LowNibble(data); + var destinationSpecifier = LowNibble(data); var destinationType = destinationSpecifier & (int)Bits.Bit3; if (sourceType == 0) @@ -1037,11 +1040,11 @@ namespace EightBit { this.HandleNMI(); } - else if (this.FIRQ.Lowered() && (this.FastInterruptMasked == 0)) + else if (this.FIRQ.Lowered() && this.FastInterruptMasked == 0) { this.HandleFIRQ(); } - else if (this.INT.Lowered() && (this.InterruptMasked == 0)) + else if (this.INT.Lowered() && this.InterruptMasked == 0) { this.HandleINT(); } @@ -1558,7 +1561,7 @@ namespace EightBit { this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7); this.CC = this.AdjustNZ(operand <<= 1); - var overflow = this.Carry ^ (this.Negative >> 3); + var overflow = this.Carry ^ this.Negative >> 3; this.CC = SetBit(this.CC, StatusBits.VF, overflow); return operand; } @@ -1566,7 +1569,7 @@ namespace EightBit private byte ASR(byte operand) { this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0); - var result = (byte)((operand >> 1) | (int)Bits.Bit7); + var result = (byte)(operand >> 1 | (int)Bits.Bit7); this.CC = this.AdjustNZ(result); return result; } @@ -1600,8 +1603,8 @@ namespace EightBit { this.CC = SetBit(this.CC, StatusBits.CF, operand > 0x99); - var lowAdjust = (this.HalfCarry != 0) || (Chip.LowNibble(operand) > 9); - var highAdjust = (this.Carry != 0) || (operand > 0x99); + var lowAdjust = this.HalfCarry != 0 || LowNibble(operand) > 9; + var highAdjust = this.Carry != 0 || operand > 0x99; if (lowAdjust) { @@ -1670,8 +1673,8 @@ namespace EightBit { var carryIn = this.Carry; this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7); - this.CC = SetBit(this.CC, StatusBits.VF, ((operand & (byte)Bits.Bit7) >> 7) ^ ((operand & (byte)Bits.Bit6) >> 6)); - var result = (byte)((operand << 1) | carryIn); + this.CC = SetBit(this.CC, StatusBits.VF, (operand & (byte)Bits.Bit7) >> 7 ^ (operand & (byte)Bits.Bit6) >> 6); + var result = (byte)(operand << 1 | carryIn); this.CC = this.AdjustNZ(result); return result; } @@ -1680,7 +1683,7 @@ namespace EightBit { var carryIn = this.Carry; this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0); - var result = (byte)((operand >> 1) | (carryIn << 7)); + var result = (byte)(operand >> 1 | carryIn << 7); this.CC = this.AdjustNZ(result); return result; } diff --git a/MC6809/ProfileEventArgs.cs b/MC6809/ProfileEventArgs.cs index c3db156..1310094 100644 --- a/MC6809/ProfileEventArgs.cs +++ b/MC6809/ProfileEventArgs.cs @@ -1,14 +1,10 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; - - public class ProfileEventArgs : EventArgs + public class ProfileEventArgs(string output) : EventArgs { - public ProfileEventArgs(string output) => this.Output = output; - - public string Output { get; } + public string Output { get; } = output; } } \ No newline at end of file diff --git a/MC6809/ProfileLineEventArgs.cs b/MC6809/ProfileLineEventArgs.cs index 1271c4e..82a1fd0 100644 --- a/MC6809/ProfileLineEventArgs.cs +++ b/MC6809/ProfileLineEventArgs.cs @@ -1,20 +1,12 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; - - public class ProfileLineEventArgs : EventArgs + public class ProfileLineEventArgs(string source, ulong cycles) : EventArgs { - public ProfileLineEventArgs(string source, ulong cycles) - { - this.Source = source; - this.Cycles = cycles; - } + public string Source { get; } = source; - public string Source { get; } - - public ulong Cycles { get; } + public ulong Cycles { get; } = cycles; } } \ No newline at end of file diff --git a/MC6809/ProfileScopeEventArgs.cs b/MC6809/ProfileScopeEventArgs.cs index 4b0320e..c0daca0 100644 --- a/MC6809/ProfileScopeEventArgs.cs +++ b/MC6809/ProfileScopeEventArgs.cs @@ -1,23 +1,14 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; - - public class ProfileScopeEventArgs : EventArgs + public class ProfileScopeEventArgs(string scope, ulong cycles, ulong count) : EventArgs { - public ProfileScopeEventArgs(string scope, ulong cycles, ulong count) - { - this.Scope = scope; - this.Cycles = cycles; - this.Count = count; - } + public string Scope { get; } = scope; - public string Scope { get; } + public ulong Cycles { get; } = cycles; - public ulong Cycles { get; } - - public ulong Count { get; } + public ulong Count { get; } = count; } } \ No newline at end of file diff --git a/MC6809/Profiler.cs b/MC6809/Profiler.cs index 098decf..5a116b7 100644 --- a/MC6809/Profiler.cs +++ b/MC6809/Profiler.cs @@ -1,42 +1,31 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; + using EightBit; - public sealed class Profiler + public sealed class Profiler(Bus board, MC6809 processor, Disassembler disassembler) { - private readonly ulong[] instructionCounts; - private readonly ulong[] addressProfiles; - private readonly ulong[] addressCounts; + private readonly ulong[] instructionCounts = new ulong[0x10000]; + private readonly ulong[] addressProfiles = new ulong[0x10000]; + private readonly ulong[] addressCounts = new ulong[0x10000]; - private readonly Bus board; - private readonly MC6809 processor; - private readonly Disassembler disassembler; + private readonly Bus board = board; + private readonly MC6809 processor = processor; + private readonly Disassembler disassembler = disassembler; private ushort address; - public Profiler(Bus board, MC6809 processor, Disassembler disassembler) - { - this.board = board; - this.processor = processor; - this.disassembler = disassembler; + public event EventHandler? StartingOutput; - this.instructionCounts = new ulong[0x10000]; - this.addressProfiles = new ulong[0x10000]; - this.addressCounts = new ulong[0x10000]; - } + public event EventHandler? FinishedOutput; - public event EventHandler StartingOutput; + public event EventHandler? StartingLineOutput; - public event EventHandler FinishedOutput; + public event EventHandler? FinishedLineOutput; - public event EventHandler StartingLineOutput; - - public event EventHandler FinishedLineOutput; - - public event EventHandler EmitLine; + public event EventHandler? EmitLine; public void Enable() { @@ -84,9 +73,9 @@ namespace EightBit } } - private void Processor_ExecutingInstruction(object sender, EventArgs e) => this.address = this.processor.PC.Word; + private void Processor_ExecutingInstruction(object? sender, EventArgs e) => this.address = this.processor.PC.Word; - private void Processor_ExecutedInstruction(object sender, EventArgs e) + private void Processor_ExecutedInstruction(object? sender, EventArgs e) { ushort opcode = this.board.Peek(this.address); if (opcode == 0x10 || opcode == 0x11) diff --git a/MC6809/StatusBits.cs b/MC6809/StatusBits.cs index 61342d7..9c7f7b9 100644 --- a/MC6809/StatusBits.cs +++ b/MC6809/StatusBits.cs @@ -2,12 +2,12 @@ // Copyright (c) Adrian Conlon. All rights reserved. // -namespace EightBit +namespace MC6809 { - using System; + using EightBit; [Flags] - public enum StatusBits : byte + public enum StatusBits { None = 0,