diff --git a/Sim80C51.Core/Processors/Attributes/IVAttribute.cs b/Sim80C51.Core/Processors/Attributes/IVAttribute.cs index 0c42719..b17664c 100644 --- a/Sim80C51.Core/Processors/Attributes/IVAttribute.cs +++ b/Sim80C51.Core/Processors/Attributes/IVAttribute.cs @@ -1,17 +1,10 @@ namespace Sim80C51.Processors.Attributes { [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] - public class IVAttribute : Attribute + public class IVAttribute(ushort address, byte priority, string name) : Attribute { - public ushort Address { get; } - public byte Priority { get; } - public string Name { get; } - - public IVAttribute(ushort address, byte priority, string name) - { - Address = address; - Priority = priority; - Name = name; - } + public ushort Address => address; + public byte Priority => priority; + public string Name => name; } } diff --git a/Sim80C51.Core/Processors/Attributes/SFR16Attribute.cs b/Sim80C51.Core/Processors/Attributes/SFR16Attribute.cs index c72e656..15cdca3 100644 --- a/Sim80C51.Core/Processors/Attributes/SFR16Attribute.cs +++ b/Sim80C51.Core/Processors/Attributes/SFR16Attribute.cs @@ -1,15 +1,9 @@ namespace Sim80C51.Processors.Attributes { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class SFR16Attribute : Attribute + public class SFR16Attribute(string sfrHName, string sfrLName) : Attribute { - public string SFRHName { get; } - public string SFRLName { get; } - - public SFR16Attribute(string SFRHName, string SFRLName) - { - this.SFRHName = SFRHName; - this.SFRLName = SFRLName; - } + public string SFRHName => sfrHName; + public string SFRLName => sfrLName; } } diff --git a/Sim80C51.Core/Processors/Attributes/SFRAttribute.cs b/Sim80C51.Core/Processors/Attributes/SFRAttribute.cs index 071b790..7e6f0b7 100644 --- a/Sim80C51.Core/Processors/Attributes/SFRAttribute.cs +++ b/Sim80C51.Core/Processors/Attributes/SFRAttribute.cs @@ -1,16 +1,10 @@ namespace Sim80C51.Processors.Attributes { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class SFRAttribute : Attribute + public class SFRAttribute(ushort address, byte resetValue) : Attribute { - public ushort Address { get; } + public ushort Address => address; - public byte ResetValue { get; } - - public SFRAttribute(ushort address, byte resetValue) - { - Address = address; - ResetValue = resetValue; - } + public byte ResetValue => resetValue; } } diff --git a/Sim80C51.Core/Processors/Attributes/SFRBitAttribute.cs b/Sim80C51.Core/Processors/Attributes/SFRBitAttribute.cs index b13ed7d..5aac1b0 100644 --- a/Sim80C51.Core/Processors/Attributes/SFRBitAttribute.cs +++ b/Sim80C51.Core/Processors/Attributes/SFRBitAttribute.cs @@ -1,17 +1,10 @@ namespace Sim80C51.Processors.Attributes { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class SFRBitAttribute : Attribute + public class SFRBitAttribute(string sfrName, byte bit, bool addressable = false) : Attribute { - public string SFRName { get; } - public byte Bit { get; } - public bool Addressable { get; } - - public SFRBitAttribute(string SFRName, byte Bit, bool Addressable = false) - { - this.SFRName = SFRName; - this.Bit = Bit; - this.Addressable = Addressable; - } + public string SFRName => sfrName; + public byte Bit => bit; + public bool Addressable => addressable; } } diff --git a/Sim80C51.Core/Processors/ByteRow.cs b/Sim80C51.Core/Processors/ByteRow.cs index b2bc56f..4ae75f1 100644 --- a/Sim80C51.Core/Processors/ByteRow.cs +++ b/Sim80C51.Core/Processors/ByteRow.cs @@ -16,7 +16,7 @@ public string Ansi { get { - string str = System.Text.Encoding.Default.GetString(Row.ToArray()); + string str = System.Text.Encoding.Default.GetString([.. Row]); return new string(str.Select(c => c < 0x20 ? '.' : c > 0x7f ? '.' : c).ToArray()); } } @@ -88,7 +88,7 @@ public static ByteRow[] InitRows(int size, bool enableMT48 = false) public static ByteRow[] FromStream(Stream stream) { - List res = new(); + List res = []; byte[] buf = new byte[ROW_WIDTH]; int row = 0; @@ -98,7 +98,7 @@ public static ByteRow[] FromStream(Stream stream) res.Add(new(row++, buf)); } - return res.ToArray(); + return [.. res]; } public static MemoryStream ToMemoryStream(IEnumerable Rows) diff --git a/Sim80C51.Core/Processors/C80C51.Logic.cs b/Sim80C51.Core/Processors/C80C51.Logic.cs index 3c7915f..797251d 100644 --- a/Sim80C51.Core/Processors/C80C51.Logic.cs +++ b/Sim80C51.Core/Processors/C80C51.Logic.cs @@ -14,22 +14,22 @@ public abstract partial class C80C51 : I80C51 /// /// map for sfr addresses /// - private readonly Dictionary sfrMap = new(); + private readonly Dictionary sfrMap = []; /// /// map for bits to sfr /// - private readonly Dictionary sfrBitMap = new(); + private readonly Dictionary sfrBitMap = []; /// /// map for 16bit values /// - private readonly Dictionary sfr16Map = new(); + private readonly Dictionary sfr16Map = []; - protected readonly SortedList ivList = new(); + protected readonly SortedList ivList = []; - private readonly Dictionary> bitCallback = new(); - private readonly Dictionary> sfrCallback = new(); + private readonly Dictionary> bitCallback = []; + private readonly Dictionary> sfrCallback = []; /// /// Constructor @@ -111,25 +111,25 @@ public void RefreshUIProperies() public void RegisterBitChangeCallback(string bitName, Action callback) { - if (bitCallback.ContainsKey(bitName)) + if (bitCallback.TryGetValue(bitName, out List? callbackList)) { - bitCallback[bitName].Add(callback); + callbackList.Add(callback); } else { - bitCallback.Add(bitName, new() { callback }); + bitCallback.Add(bitName, [callback]); } } public void RegisterSfrChangeCallback(string sfrName, Action callback) { - if (sfrCallback.ContainsKey(sfrName)) + if (sfrCallback.TryGetValue(sfrName, out List? callbackList)) { - sfrCallback[sfrName].Add(callback); + callbackList.Add(callback); } else { - sfrCallback.Add(sfrName, new() { callback }); + sfrCallback.Add(sfrName, [callback]); } } @@ -142,28 +142,26 @@ public void RegisterSfrChangeCallback(string sfrName, Action callback) /// if register not found protected void SetMem16FromProp(ushort value, [CallerMemberName] string sfr16Name = "") { - if (!sfr16Map.ContainsKey(sfr16Name)) + if (!sfr16Map.TryGetValue(sfr16Name, out SFR16Attribute? sfrAttr)) { throw new ArgumentException(sfr16Name); } - string sfrhName = sfr16Map[sfr16Name].SFRHName; - string sfrlName = sfr16Map[sfr16Name].SFRLName; - if (!sfrMap.ContainsKey(sfrhName)) + if (!sfrMap.ContainsKey(sfrAttr.SFRHName)) { - throw new ArgumentException(sfrhName); + throw new ArgumentException(sfrAttr.SFRHName); } - if (!sfrMap.ContainsKey(sfrlName)) + if (!sfrMap.ContainsKey(sfrAttr.SFRLName)) { - throw new ArgumentException(sfrlName); + throw new ArgumentException(sfrAttr.SFRLName); } byte lValue = (byte)(value & 0xff); byte hValue = (byte)(value >> 8 & 0xff); - SetMemFromProp(hValue, sfrhName); - SetMemFromProp(lValue, sfrlName); + SetMemFromProp(hValue, sfrAttr.SFRHName); + SetMemFromProp(lValue, sfrAttr.SFRLName); } /// @@ -174,24 +172,22 @@ protected void SetMem16FromProp(ushort value, [CallerMemberName] string sfr16Nam /// if register not found protected ushort GetMem16FromProp([CallerMemberName] string sfr16Name = "") { - if (!sfr16Map.ContainsKey(sfr16Name)) + if (!sfr16Map.TryGetValue(sfr16Name, out SFR16Attribute? sfrAttr)) { throw new ArgumentException(sfr16Name); } - string sfrhName = sfr16Map[sfr16Name].SFRHName; - string sfrlName = sfr16Map[sfr16Name].SFRLName; - if (!sfrMap.ContainsKey(sfrhName)) + if (!sfrMap.TryGetValue(sfrAttr.SFRHName, out SFRAttribute? sfrH)) { - throw new ArgumentException(sfrhName); + throw new ArgumentException(sfrAttr.SFRHName); } - if (!sfrMap.ContainsKey(sfrlName)) + if (!sfrMap.TryGetValue(sfrAttr.SFRLName, out SFRAttribute? sfrL)) { - throw new ArgumentException(sfrlName); + throw new ArgumentException(sfrAttr.SFRLName); } - return (ushort)(GetMem(sfrMap[sfrhName].Address) << 8 | GetMem(sfrMap[sfrlName].Address)); + return (ushort)(GetMem(sfrH.Address) << 8 | GetMem(sfrL.Address)); } /// @@ -203,23 +199,23 @@ protected ushort GetMem16FromProp([CallerMemberName] string sfr16Name = "") /// if bit or register not found protected bool SetBitFromProp(bool value, [CallerMemberName] string sfrBitName = "") { - if (!sfrBitMap.ContainsKey(sfrBitName)) + if (!sfrBitMap.TryGetValue(sfrBitName, out SFRBitAttribute? sfrBit)) { throw new ArgumentException(sfrBitName); } - string sfrName = sfrBitMap[sfrBitName].SFRName; - if (!sfrMap.ContainsKey(sfrName)) + string sfrName = sfrBit.SFRName; + if (!sfrMap.TryGetValue(sfrName, out SFRAttribute? sfrAttr)) { throw new ArgumentException(sfrName); } - if (GetBit(sfrMap[sfrName].Address, sfrBitMap[sfrBitName].Bit) == value) + if (GetBit(sfrAttr.Address, sfrBit.Bit) == value) { return false; } - SetBit(sfrMap[sfrName].Address, sfrBitMap[sfrBitName].Bit, value); + SetBit(sfrAttr.Address, sfrBit.Bit, value); DoPropertyChanged(sfrName); DoPropertyChanged(sfrBitName); foreach (string sfr16name in sfr16Map.Where(sf => sf.Value.SFRHName == sfrName || sf.Value.SFRLName == sfrName).Select(sf => sf.Key)) @@ -237,18 +233,18 @@ protected bool SetBitFromProp(bool value, [CallerMemberName] string sfrBitName = /// if bit or register not found protected bool GetBitFromProp([CallerMemberName] string sfrBitName = "") { - if (!sfrBitMap.ContainsKey(sfrBitName)) + if (!sfrBitMap.TryGetValue(sfrBitName, out SFRBitAttribute? sfrBit)) { throw new ArgumentException(sfrBitName); } - string sfrName = sfrBitMap[sfrBitName].SFRName; - if (!sfrMap.ContainsKey(sfrName)) + string sfrName = sfrBit.SFRName; + if (!sfrMap.TryGetValue(sfrName, out SFRAttribute? sfrAttr)) { throw new ArgumentException(sfrName); } - return GetBit(sfrMap[sfrName].Address, sfrBitMap[sfrBitName].Bit); + return GetBit(sfrAttr.Address, sfrBit.Bit); } /// @@ -261,20 +257,20 @@ protected bool GetBitFromProp([CallerMemberName] string sfrBitName = "") /// if register not found protected bool SetMemFromProp(byte value, [CallerMemberName] string sfrName = "") { - if (!sfrMap.ContainsKey(sfrName)) + if (!sfrMap.TryGetValue(sfrName, out SFRAttribute? sfrAttr)) { throw new ArgumentException(sfrName); } - if (GetMem(sfrMap[sfrName].Address) == value) + if (GetMem(sfrAttr.Address) == value) { return false; } - byte bitMask = SetMem(sfrMap[sfrName].Address, value); - if (sfrCallback.ContainsKey(sfrName)) + byte bitMask = SetMem(sfrAttr.Address, value); + if (sfrCallback.TryGetValue(sfrName, out List? callbackList)) { - foreach(Action callback in sfrCallback[sfrName]) + foreach(Action callback in callbackList) { callback.Invoke(); } @@ -285,9 +281,9 @@ protected bool SetMemFromProp(byte value, [CallerMemberName] string sfrName = "" { if (((1 << sfrBit.Value.Bit) & bitMask) != 0) { - if (bitCallback.ContainsKey(sfrBit.Key)) + if (bitCallback.TryGetValue(sfrBit.Key, out List? bitCallbackList)) { - foreach (Action callback in bitCallback[sfrBit.Key]) + foreach (Action callback in bitCallbackList) { callback.Invoke(); } @@ -312,12 +308,12 @@ protected bool SetMemFromProp(byte value, [CallerMemberName] string sfrName = "" /// if register not found protected byte GetMemFromProp([CallerMemberName] string sfrName = "") { - if (!sfrMap.ContainsKey(sfrName)) + if (!sfrMap.TryGetValue(sfrName, out SFRAttribute? sfrAttr)) { throw new ArgumentException(sfrName); } - return GetMem(sfrMap[sfrName].Address); + return GetMem(sfrAttr.Address); } /// @@ -395,17 +391,17 @@ protected byte GetMem(ushort address) /// Bitmask of bits updated protected byte SetMem(string sfrName, byte value) { - if (!sfrMap.ContainsKey(sfrName)) + if (!sfrMap.TryGetValue(sfrName, out SFRAttribute? sfrAttr)) { throw new ArgumentException(sfrName); } - if (GetMem(sfrMap[sfrName].Address) == value) + if (GetMem(sfrAttr.Address) == value) { return 0; } - return SetMem(sfrMap[sfrName].Address, value); + return SetMem(sfrAttr.Address, value); } /// @@ -426,12 +422,12 @@ protected byte SetMem(ushort address, byte value) public static byte ParseIntermediateByte(string value) { - if (!value.StartsWith("#")) + if (!value.StartsWith('#')) { throw new ArgumentOutOfRangeException(value); } - if (value.EndsWith("h")) + if (value.EndsWith('h')) { return Convert.ToByte(value[1..3], 16); } @@ -441,12 +437,12 @@ public static byte ParseIntermediateByte(string value) public static ushort ParseIntermediateUShort(string value) { - if (!value.StartsWith("#")) + if (!value.StartsWith('#')) { throw new ArgumentOutOfRangeException(value); } - if (value.EndsWith("h")) + if (value.EndsWith('h')) { return Convert.ToUInt16(value[1..5], 16); } diff --git a/Sim80C51.Core/Processors/C80C51.Process.cs b/Sim80C51.Core/Processors/C80C51.Process.cs index eaf5f49..05e6cb5 100644 --- a/Sim80C51.Core/Processors/C80C51.Process.cs +++ b/Sim80C51.Core/Processors/C80C51.Process.cs @@ -8,7 +8,7 @@ namespace Sim80C51.Processors /// public abstract partial class C80C51 : I80C51 { - private static readonly string[] regNames = new string[] { nameof(R0), nameof(R1), nameof(R2), nameof(R3), nameof(R4), nameof(R5), nameof(R6), nameof(R7) }; + private static readonly string[] regNames = [nameof(R0), nameof(R1), nameof(R2), nameof(R3), nameof(R4), nameof(R5), nameof(R6), nameof(R7)]; private bool irqHighInProgress = false; private bool irqLowInProgress = false; diff --git a/Sim80C51.Core/Processors/C80C51.cs b/Sim80C51.Core/Processors/C80C51.cs index a379a5a..f75ea70 100644 --- a/Sim80C51.Core/Processors/C80C51.cs +++ b/Sim80C51.Core/Processors/C80C51.cs @@ -34,7 +34,7 @@ public abstract partial class C80C51 : I80C51 /// /// Call Stack /// - public ObservableCollection CallStack { get; } = new(); + public ObservableCollection CallStack { get; } = []; public byte R0 { get => GetRegister(); set { SetRegister(value); } } public byte R1 { get => GetRegister(); set { SetRegister(value); } } diff --git a/Sim80C51.Core/Processors/IV.cs b/Sim80C51.Core/Processors/IV.cs index f6bd8f2..37856f5 100644 --- a/Sim80C51.Core/Processors/IV.cs +++ b/Sim80C51.Core/Processors/IV.cs @@ -1,18 +1,10 @@ namespace Sim80C51.Processors { - public class IV + public class IV(ushort vectorAddress, Func priorityBit, Func check, Action? clear = null) { - public IV(ushort vectorAddress, Func priorityBit, Func check, Action? clear = null) - { - VectorAddress = vectorAddress; - PriorityBit = priorityBit; - Check = check; - Clear = clear; - } - - public ushort VectorAddress { get; } - public Func PriorityBit { get; } - public Func Check { get; } - public Action? Clear { get; } + public ushort VectorAddress => vectorAddress; + public Func PriorityBit => priorityBit; + public Func Check => check; + public Action? Clear => clear; } } diff --git a/Sim80C51.Core/Sim80C51.Core.csproj b/Sim80C51.Core/Sim80C51.Core.csproj index 55cc5b2..731c9ef 100644 --- a/Sim80C51.Core/Sim80C51.Core.csproj +++ b/Sim80C51.Core/Sim80C51.Core.csproj @@ -1,7 +1,7 @@  - net6.0-windows + net8.0-windows enable enable Sven Fabricius diff --git a/Sim80C51.TanningBed/MainWindowModel.cs b/Sim80C51.TanningBed/MainWindowModel.cs index e49a6ed..168918f 100644 --- a/Sim80C51.TanningBed/MainWindowModel.cs +++ b/Sim80C51.TanningBed/MainWindowModel.cs @@ -57,18 +57,18 @@ public class MainWindowModel : INotifyPropertyChanged public SAA1064 SAA1064_3B { get; } = new(true, true); - private II2CDevice[] i2CDevices; + private readonly II2CDevice[] i2CDevices; public MainWindowModel() { - i2CDevices = new II2CDevice[] - { + i2CDevices = + [ PCF8574_20, PCF8574_21, PCF8574_22, SAA1064_38, SAA1064_3B - }; + ]; } public void Loaded(MainWindow mainWindow) diff --git a/Sim80C51.TanningBed/Sim80C51.TanningBed.csproj b/Sim80C51.TanningBed/Sim80C51.TanningBed.csproj index 65bfa89..80515b9 100644 --- a/Sim80C51.TanningBed/Sim80C51.TanningBed.csproj +++ b/Sim80C51.TanningBed/Sim80C51.TanningBed.csproj @@ -1,7 +1,7 @@ - net6.0-windows + net8.0-windows enable enable true diff --git a/Sim80C51.Toolbox/ObservableDictionary.cs b/Sim80C51.Toolbox/ObservableDictionary.cs index 26808d3..cb03f81 100644 --- a/Sim80C51.Toolbox/ObservableDictionary.cs +++ b/Sim80C51.Toolbox/ObservableDictionary.cs @@ -77,7 +77,7 @@ public void AddRange(IEnumerable> items) public bool Remove(TKey key) { - List> remove = ThisAsCollection().Where(pair => Equals(key, pair.Key)).ToList(); + List> remove = ThisAsCollection().Where(pair => ObservableDictionary.Equals(key, pair.Key)).ToList(); foreach (ObservableKeyValuePair pair in remove) { ThisAsCollection().Remove(pair); @@ -99,7 +99,7 @@ public bool Remove(KeyValuePair item) public bool ContainsKey(TKey key) { - return !Equals(default(ObservableKeyValuePair), ThisAsCollection().FirstOrDefault(i => Equals(key, i.Key))); + return !Equals(default(ObservableKeyValuePair), ThisAsCollection().FirstOrDefault(i => ObservableDictionary.Equals(key, i.Key))); } public bool Contains(KeyValuePair item) @@ -147,7 +147,7 @@ public bool IsReadOnly return (from i in ThisAsCollection() select new KeyValuePair(i.Key, i.Value)).ToList().GetEnumerator(); } - private bool Equals(TKey firstKey, TKey secondKey) + private static bool Equals(TKey firstKey, TKey secondKey) { return EqualityComparer.Default.Equals(firstKey, secondKey); } diff --git a/Sim80C51.Toolbox/ObservableKeyValuePair.cs b/Sim80C51.Toolbox/ObservableKeyValuePair.cs index cc976b7..367066e 100644 --- a/Sim80C51.Toolbox/ObservableKeyValuePair.cs +++ b/Sim80C51.Toolbox/ObservableKeyValuePair.cs @@ -2,14 +2,10 @@ namespace Sim80C51.Toolbox { - public sealed class ObservableKeyValuePair: NotifyPropertyChanged + public sealed class ObservableKeyValuePair(TKey key, TValue _value) : NotifyPropertyChanged { - public TKey Key { get { return _key; } set { _key = value; DoPropertyChanged(); } } - private TKey _key; + public TKey Key { get { return key; } set { key = value; DoPropertyChanged(); } } public TValue Value { get { return _value; } set { _value = value; DoPropertyChanged(); } } - private TValue _value; - - public ObservableKeyValuePair(TKey key, TValue value) { _key = key; _value = value; } } } diff --git a/Sim80C51.Toolbox/Sim80C51.Toolbox.csproj b/Sim80C51.Toolbox/Sim80C51.Toolbox.csproj index a00abee..e68da07 100644 --- a/Sim80C51.Toolbox/Sim80C51.Toolbox.csproj +++ b/Sim80C51.Toolbox/Sim80C51.Toolbox.csproj @@ -1,10 +1,11 @@  - net6.0-windows + net8.0-windows enable enable true + true diff --git a/Sim80C51.Toolbox/Wpf/IconHelper.cs b/Sim80C51.Toolbox/Wpf/IconHelper.cs index dfa864e..39cc1e9 100644 --- a/Sim80C51.Toolbox/Wpf/IconHelper.cs +++ b/Sim80C51.Toolbox/Wpf/IconHelper.cs @@ -4,19 +4,20 @@ namespace Sim80C51.Toolbox.Wpf { - public static class IconHelper + public static partial class IconHelper { - [DllImport("user32.dll")] - static extern int GetWindowLong(IntPtr hwnd, int index); + [LibraryImport("user32.dll")] + private static partial int GetWindowLong(IntPtr hwnd, int index); - [DllImport("user32.dll")] - static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); + [LibraryImport("user32.dll")] + private static partial int SetWindowLong(IntPtr hwnd, int index, int newStyle); - [DllImport("user32.dll")] - static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); - [DllImport("user32.dll")] - static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam); + [LibraryImport("user32.dll")] + private static partial IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam); const int GWL_EXSTYLE = -20; const int WS_EX_DLGMODALFRAME = 0x0001; diff --git a/Sim80C51.Toolbox/Wpf/RelayCommand.cs b/Sim80C51.Toolbox/Wpf/RelayCommand.cs index a27f491..2e1e125 100644 --- a/Sim80C51.Toolbox/Wpf/RelayCommand.cs +++ b/Sim80C51.Toolbox/Wpf/RelayCommand.cs @@ -4,20 +4,11 @@ namespace Sim80C51.Toolbox.Wpf { [DataContract] - public class RelayCommand : ICommand + public class RelayCommand(Action execute, Predicate canExecute) : ICommand { - private Action execute; - - private Predicate canExecute; - private event EventHandler? CanExecuteChangedInternal; public RelayCommand(Action execute) : this(execute, DefaultCanExecute) { } - public RelayCommand(Action execute, Predicate canExecute) - { - this.execute = execute; - this.canExecute = canExecute; - } public event EventHandler? CanExecuteChanged { diff --git a/Sim80C51.Toolbox/Wpf/SevenSegments.cs b/Sim80C51.Toolbox/Wpf/SevenSegments.cs index daae9be..3e28e6e 100644 --- a/Sim80C51.Toolbox/Wpf/SevenSegments.cs +++ b/Sim80C51.Toolbox/Wpf/SevenSegments.cs @@ -320,7 +320,7 @@ protected override void OnRender(DrawingContext drawingContext) foreach (GeometryWithSegm entry in GeometryFigures) { - if (SegmentsBrush.Any()) + if (SegmentsBrush.Count != 0) { Tuple? brush = SegmentsBrush.SingleOrDefault(s => s.Item1 == entry.SegmentNumber); Pen figurePen = new(new SolidColorBrush(brush != null ? brush.Item3 : PenColor), PenThickness); @@ -344,8 +344,8 @@ protected override void OnRender(DrawingContext drawingContext) public void ClearSegments() { Value = 0; - SelectedSegments = new List(); - SegmentsBrush = new List>(); + SelectedSegments = []; + SegmentsBrush = []; } /// @@ -353,8 +353,8 @@ public void ClearSegments() /// protected virtual void AssignSegments() { - GeometryFigures = new List - { + GeometryFigures = + [ new GeometryWithSegm(LeftBottomSegement(), 4), new GeometryWithSegm(LeftTopSegement(), 5), new GeometryWithSegm(RightTopSegement(), 1), @@ -362,7 +362,7 @@ protected virtual void AssignSegments() new GeometryWithSegm(MiddleSegement(), 6), new GeometryWithSegm(TopSegement(), 0), new GeometryWithSegm(BottomSegement(), 3), - }; + ]; } /// @@ -370,7 +370,7 @@ protected virtual void AssignSegments() /// protected void SetSegments() { - if (SelectedSegments.Any()) + if (SelectedSegments.Count != 0) { for (int i = 0; i < SelectedSegments.Count; i++) { @@ -420,7 +420,7 @@ private void CalculateMeasures() /// protected virtual void ValueSegmentsSelection() { - List segments = new(); + List segments = []; for (int i = 0; i < 7; i++) { if ((Value & 1 << i) != 0) @@ -431,7 +431,7 @@ protected virtual void ValueSegmentsSelection() if (segments.Count > 0) { - SelectSegments(segments.ToArray()); + SelectSegments([.. segments]); } } @@ -566,8 +566,8 @@ protected PointCollection GetLeftTopSegmPoints() startTopY = VirtualHeight / 2 - GapW / 2; double x5 = XByAngle(startTopY - startPointThickness); - return new() - { + return + [ // three top points, starting from the left point new Point(x1 + startPointThickness, HorizSegSmallPartH + GapW + VertSegSmallPartH + startPointThickness), new Point(x2 + VertSegPartW + startPointThickness, HorizSegSmallPartH + startPointThickness + GapShift()), @@ -580,7 +580,7 @@ protected PointCollection GetLeftTopSegmPoints() // the point for rounded Bezier curve bezPoint - }; + ]; } /// @@ -604,8 +604,8 @@ protected PointCollection GetLeftBottomSegmPoints() ? new Point(xBezier + startPointThickness, yBezier) : new Point(startPointThickness, figureStartPointY - startPointThickness); - return new() - { + return + [ // three top points, starting from left top point new Point(x1 + startPointThickness, VirtualHeight / 2 + GapW / 2), new Point(x1 + VertSegBotPartW + startPointThickness, VirtualHeight / 2 + GapW / 2), @@ -618,7 +618,7 @@ protected PointCollection GetLeftBottomSegmPoints() // the point for rounded Bezier curve bezPoint - }; + ]; } /// @@ -627,8 +627,8 @@ protected PointCollection GetLeftBottomSegmPoints() /// protected PointCollection GetRightBottomSegmPoints() { - return new PointCollection - { + return + [ // three top points, starting from the left point new Point(VirtualWidth - LeftTopSegmPoints[3].X, VirtualHeight - LeftTopSegmPoints[3].Y), new Point(VirtualWidth - LeftTopSegmPoints[4].X, VirtualHeight - LeftTopSegmPoints[4].Y), @@ -641,7 +641,7 @@ protected PointCollection GetRightBottomSegmPoints() new Point(VirtualWidth - LeftTopSegmPoints[0].X, VirtualHeight - LeftTopSegmPoints[0].Y), new Point(VirtualWidth - LeftTopSegmPoints[1].X, VirtualHeight - LeftTopSegmPoints[1].Y), new Point(VirtualWidth - LeftTopSegmPoints[2].X, VirtualHeight - LeftTopSegmPoints[2].Y) - }; + ]; } @@ -650,8 +650,8 @@ protected PointCollection GetRightBottomSegmPoints() /// protected PointCollection GetRightTopSegmPoints() { - return new() - { + return + [ // three top points, starting from the left point new Point(VirtualWidth - LeftBottomSegmPoints[3].X, VirtualHeight - LeftBottomSegmPoints[3].Y), new Point(VirtualWidth - LeftBottomSegmPoints[4].X, VirtualHeight - LeftBottomSegmPoints[4].Y), @@ -664,7 +664,7 @@ protected PointCollection GetRightTopSegmPoints() new Point(VirtualWidth - LeftBottomSegmPoints[0].X, VirtualHeight - LeftBottomSegmPoints[0].Y), new Point(VirtualWidth - LeftBottomSegmPoints[1].X, VirtualHeight - LeftBottomSegmPoints[1].Y), new Point(VirtualWidth - LeftBottomSegmPoints[2].X, VirtualHeight - LeftBottomSegmPoints[2].Y) - }; + ]; } /// @@ -677,8 +677,8 @@ protected PointCollection GetMiddleSegmPoints() double x1 = XByAngle(VirtualHeight / 2) + VertSegBotPartW + GapW; double x2 = XByAngle(VirtualHeight / 2 - HorizSegH / 2) + VertSegW + GapW; - return new() - { + return + [ // three left points, starting from the bottom point new Point(x, VirtualHeight / 2 + HorizSegH / 2), new Point(x1, VirtualHeight / 2), @@ -688,7 +688,7 @@ protected PointCollection GetMiddleSegmPoints() new Point(VirtualWidth - x, RightTopSegmPoints[6].Y + GapW / 2), new Point(VirtualWidth - x1, VirtualHeight / 2), new Point(VirtualWidth - x2, RightBottomSegmPoints[0].Y - GapW / 2) - }; + ]; } @@ -702,8 +702,8 @@ protected PointCollection GetTopSegmPoints() double topRightX = RightTopSegmPoints[1].X - HorizSegSmallPartW; double coefRound = RoundedCorners ? VirtualWidth / horizRoundCoef : 0; - return new() - { + return + [ // three left points, starting from the bottom point new Point(LeftTopSegmPoints[2].X + GapW, HorizSegH + startPointThickness), new Point(LeftTopSegmPoints[1].X + GapShift(), HorizSegSmallPartH + startPointThickness), @@ -717,7 +717,7 @@ protected PointCollection GetTopSegmPoints() new Point(topRightX, startPointThickness), new Point(RightTopSegmPoints[1].X - GapShift(), HorizSegSmallPartH + startPointThickness), new Point(RightTopSegmPoints[0].X - GapW, HorizSegH + startPointThickness) - }; + ]; } @@ -731,8 +731,8 @@ protected PointCollection GetBottomSegmPoints() double botRightX = RightBottomSegmPoints[5].X - HorizSegSmallPartW; double coefRound = RoundedCorners ? VirtualWidth / horizRoundCoef : 0; - return new() - { + return + [ // three left points, starting from the bottom point new Point(botLeftX, VirtualHeight - startPointThickness), new Point(LeftBottomSegmPoints[4].X + GapShift(), VirtualHeight - HorizSegSmallPartH - startPointThickness), @@ -746,7 +746,7 @@ protected PointCollection GetBottomSegmPoints() // two bottom Bezier points starting from the right point new Point(botRightX - coefRound, VirtualHeight - startPointThickness), new Point(botLeftX + coefRound, VirtualHeight - startPointThickness) - }; + ]; } @@ -764,12 +764,12 @@ protected PathGeometry RightTopSegement() Point startPoint = RightTopSegmPoints[0]; // The Bezier curve for rounded corners - PointCollection pointsBezier = new() - { + PointCollection pointsBezier = + [ RightTopSegmPoints[1], RightTopSegmPoints[2], RightTopSegmPoints[3] - }; + ]; PolyBezierSegment bez = new() { @@ -818,12 +818,12 @@ protected PathGeometry RightBottomSegement() Point startPoint = RightBottomSegmPoints[0]; // The Bezier curve for rounded corners - PointCollection pointsBezier = new() - { + PointCollection pointsBezier = + [ RightBottomSegmPoints[3], RightBottomSegmPoints[4], RightBottomSegmPoints[5] - }; + ]; PolyBezierSegment bez = new() { @@ -858,10 +858,10 @@ protected PathGeometry TopSegement() Point startPoint = TopSegmPoints[0]; // The left Bezier curve for rounded corners - PointCollection pointsBezierLeft = new() - { + PointCollection pointsBezierLeft = + [ TopSegmPoints[1], TopSegmPoints[2], TopSegmPoints[3] - }; + ]; PolyBezierSegment bezLeft = new() { @@ -870,10 +870,10 @@ protected PathGeometry TopSegement() // The right Bezier curve for rounded corners - PointCollection pointsBezierRight = new() - { + PointCollection pointsBezierRight = + [ TopSegmPoints[4], TopSegmPoints[5], TopSegmPoints[6] - }; + ]; PolyBezierSegment bezRight = new() { @@ -912,12 +912,12 @@ protected PathGeometry LeftTopSegement() Point startPoint = LeftTopSegmPoints[6]; // The Bezier curve for rounded corners - PointCollection pointsBezier = new() - { + PointCollection pointsBezier = + [ LeftTopSegmPoints[6], LeftTopSegmPoints[0], LeftTopSegmPoints[1] - }; + ]; PolyBezierSegment bez = new() { @@ -950,12 +950,12 @@ protected PathGeometry LeftBottomSegement() Point startPoint = LeftBottomSegmPoints[0]; // The Bezier curve for rounded corners - PointCollection pointsBezier = new() - { + PointCollection pointsBezier = + [ LeftBottomSegmPoints[4], LeftBottomSegmPoints[5], LeftBottomSegmPoints[6] - }; + ]; PolyBezierSegment bez = new() { @@ -991,10 +991,10 @@ protected PathGeometry BottomSegement() Point startPoint = BottomSegmPoints[1]; // The right Bezier curve for rounded corners - PointCollection pointsBezierRight = new() - { + PointCollection pointsBezierRight = + [ BottomSegmPoints[4], BottomSegmPoints[5], BottomSegmPoints[6] - }; + ]; PolyBezierSegment bezRight = new() { @@ -1002,10 +1002,10 @@ protected PathGeometry BottomSegement() }; // The left Bezier curve for rounded corners - PointCollection pointsBezierLeft = new() - { + PointCollection pointsBezierLeft = + [ BottomSegmPoints[7], BottomSegmPoints[0], BottomSegmPoints[1] - }; + ]; PolyBezierSegment bezLeft = new() { @@ -1035,18 +1035,11 @@ protected PathGeometry BottomSegement() /// /// The class to detect selected segment /// - public class GeometryWithSegm + public class GeometryWithSegm(PathGeometry geometry, int segm, bool isSelected = false) { - public PathGeometry Geometry { get; set; } - public int SegmentNumber { get; set; } - public bool IsSelected { get; set; } - - public GeometryWithSegm(PathGeometry geometry, int segm, bool isSelected = false) - { - Geometry = geometry; - SegmentNumber = segm; - IsSelected = isSelected; - } + public PathGeometry Geometry { get; set; } = geometry; + public int SegmentNumber { get; set; } = segm; + public bool IsSelected { get; set; } = isSelected; } #endregion } diff --git a/Sim80C51.Toolbox/Wpf/TwoColumnGrid.cs b/Sim80C51.Toolbox/Wpf/TwoColumnGrid.cs index 5587181..9c66282 100644 --- a/Sim80C51.Toolbox/Wpf/TwoColumnGrid.cs +++ b/Sim80C51.Toolbox/Wpf/TwoColumnGrid.cs @@ -19,7 +19,7 @@ namespace Sim80C51.Toolbox.Wpf public class TwoColumnGrid : Panel { private double Column1Width; - private readonly List RowHeights = new(); + private readonly List RowHeights = []; /// /// Gets or sets the amount of spacing (in device independent pixels) between the rows. diff --git a/Sim80C51/Common/IntelHex.cs b/Sim80C51/Common/IntelHex.cs index 0e2131a..21a7d94 100644 --- a/Sim80C51/Common/IntelHex.cs +++ b/Sim80C51/Common/IntelHex.cs @@ -8,87 +8,87 @@ namespace Sim80C51.Common /// the IntelHex class access to the internal storage. /// public class IntelHexStructure - { - public ushort address; //< The 16-bit address field. - //< The 8-bit array data field, which has a maximum size of 256 bytes. - public byte[] data = new byte[IntelHex.IHEX_MAX_DATA_LEN / 2]; - public int dataLen; //< The number of bytes of data stored in this record. - public int type; //< The Intel HEX8 record type of this record. - public byte checksum; //< The checksum of this record. + { + public ushort address; //< The 16-bit address field. + //< The 8-bit array data field, which has a maximum size of 256 bytes. + public byte[] data = new byte[IntelHex.IHEX_MAX_DATA_LEN / 2]; + public int dataLen; //< The number of bytes of data stored in this record. + public int type; //< The Intel HEX8 record type of this record. + public byte checksum; //< The checksum of this record. - private byte CalcChecksum() - { - // Add the data count, type, address, and data bytes together - byte cChecksum = (byte)dataLen; - cChecksum += (byte)type; - cChecksum += (byte)address; - cChecksum += (byte)((address & 0xFF00) >> 8); - for (int i = 0; i < dataLen; i++) - { - cChecksum += data[i]; - } - // Two's complement on checksum - return (byte)(~cChecksum + 1); - } + private byte CalcChecksum() + { + // Add the data count, type, address, and data bytes together + byte cChecksum = (byte)dataLen; + cChecksum += (byte)type; + cChecksum += (byte)address; + cChecksum += (byte)((address & 0xFF00) >> 8); + for (int i = 0; i < dataLen; i++) + { + cChecksum += data[i]; + } + // Two's complement on checksum + return (byte)(~cChecksum + 1); + } - public void SetChecksum() - { - checksum = CalcChecksum(); - } + public void SetChecksum() + { + checksum = CalcChecksum(); + } - public bool Verify() + public bool Verify() { - byte chec = CalcChecksum(); + byte chec = CalcChecksum(); return checksum == chec; } } - /// - /// IntelHex is the base class to work with Intel Hex records. - /// This class will contain all necessary functions to process data using the Intel Hex record standard. - /// - public static class IntelHex - { - // Offsets and lengths of various fields in an Intel HEX8 record - const int IHEX_COUNT_OFFSET = 1; - const int IHEX_COUNT_LEN = 2; - const int IHEX_ADDRESS_OFFSET = 3; - const int IHEX_ADDRESS_LEN = 4; - const int IHEX_TYPE_OFFSET = 7; - const int IHEX_TYPE_LEN = 2; - const int IHEX_DATA_OFFSET = 9; - const int IHEX_CHECKSUM_LEN = 2; - public const int IHEX_MAX_DATA_LEN = 512; - // Ascii hex encoded length of a single byte - const int IHEX_ASCII_HEX_BYTE_LEN = 2; - // Start code offset and value - const int IHEX_START_CODE_OFFSET = 0; - const char IHEX_START_CODE = ':'; + /// + /// IntelHex is the base class to work with Intel Hex records. + /// This class will contain all necessary functions to process data using the Intel Hex record standard. + /// + public static class IntelHex + { + // Offsets and lengths of various fields in an Intel HEX8 record + const int IHEX_COUNT_OFFSET = 1; + const int IHEX_COUNT_LEN = 2; + const int IHEX_ADDRESS_OFFSET = 3; + const int IHEX_ADDRESS_LEN = 4; + const int IHEX_TYPE_OFFSET = 7; + const int IHEX_TYPE_LEN = 2; + const int IHEX_DATA_OFFSET = 9; + const int IHEX_CHECKSUM_LEN = 2; + public const int IHEX_MAX_DATA_LEN = 512; + // Ascii hex encoded length of a single byte + const int IHEX_ASCII_HEX_BYTE_LEN = 2; + // Start code offset and value + const int IHEX_START_CODE_OFFSET = 0; + const char IHEX_START_CODE = ':'; - public const int IHEX_TYPE_DATA = 0; //< Data Record - public const int IHEX_TYPE_EOF = 1; //< End of File Record - public const int IHEX_TYPE_02 = 2; //< Extended Segment Address Record - public const int IHEX_TYPE_03 = 3; //< Start Segment Address Record - public const int IHEX_TYPE_04 = 4; //< Extended Linear Address Record - public const int IHEX_TYPE_05 = 5; //< Start Linear Address Record + public const int IHEX_TYPE_DATA = 0; //< Data Record + public const int IHEX_TYPE_EOF = 1; //< End of File Record + public const int IHEX_TYPE_02 = 2; //< Extended Segment Address Record + public const int IHEX_TYPE_03 = 3; //< Start Segment Address Record + public const int IHEX_TYPE_04 = 4; //< Extended Linear Address Record + public const int IHEX_TYPE_05 = 5; //< Start Linear Address Record - /// - /// Initializes a new IntelHex structure that is returned upon successful completion of the function, - /// including up to 16-bit integer address, 8-bit data array, and size of 8-bit data array. - /// - /// The type of Intel HEX record to be defined by the record. - /// The 16-, 24-, or 32-bit address of the record. - /// An array of 8-bit data bytes. - /// The number of data bytes passed in the array. - /// IntelHexStructure instance or null, if null then query Status class variable for the error. - public static IntelHexStructure? NewRecord(int type, ushort address, byte[] data, int dataLen) - { - // Data length size check, assertion of irec pointer - if (dataLen < 0 || dataLen > IHEX_MAX_DATA_LEN / 2) - { - return null; - } + /// + /// Initializes a new IntelHex structure that is returned upon successful completion of the function, + /// including up to 16-bit integer address, 8-bit data array, and size of 8-bit data array. + /// + /// The type of Intel HEX record to be defined by the record. + /// The 16-, 24-, or 32-bit address of the record. + /// An array of 8-bit data bytes. + /// The number of data bytes passed in the array. + /// IntelHexStructure instance or null, if null then query Status class variable for the error. + public static IntelHexStructure? NewRecord(int type, ushort address, byte[] data, int dataLen) + { + // Data length size check, assertion of irec pointer + if (dataLen < 0 || dataLen > IHEX_MAX_DATA_LEN / 2) + { + return null; + } IntelHexStructure irec = new() { @@ -102,133 +102,133 @@ public static class IntelHex } irec.dataLen = dataLen; - irec.SetChecksum(); + irec.SetChecksum(); - return irec; - } + return irec; + } - /// - /// Utility function to read an Intel HEX8 record from a file - /// - /// An instance of the StreamReader class to allow reading the file data. - /// IntelHexStructure instance or null, if null then query Status class variable for the error. - public static IntelHexStructure? Read(StreamReader inStream) - { + /// + /// Utility function to read an Intel HEX8 record from a file + /// + /// An instance of the StreamReader class to allow reading the file data. + /// IntelHexStructure instance or null, if null then query Status class variable for the error. + public static IntelHexStructure? Read(StreamReader inStream) + { string? recordBuff; - int dataCount, i; + int dataCount, i; - try - { - // Read Line will return a line from the file. - recordBuff = inStream.ReadLine(); - } - catch (Exception) - { - return null; - } + try + { + // Read Line will return a line from the file. + recordBuff = inStream.ReadLine(); + } + catch (Exception) + { + return null; + } - // Check if we hit a newline - if (recordBuff == null || recordBuff.Length == 0) - { - return null; - } + // Check if we hit a newline + if (recordBuff == null || recordBuff.Length == 0) + { + return null; + } - // Size check for start code, count, address, and type fields - if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN)) - { - return null; - } + // Size check for start code, count, address, and type fields + if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN)) + { + return null; + } - // Check the for colon start code - if (recordBuff[IHEX_START_CODE_OFFSET] != IHEX_START_CODE) - { - return null; - } + // Check the for colon start code + if (recordBuff[IHEX_START_CODE_OFFSET] != IHEX_START_CODE) + { + return null; + } - IntelHexStructure irec = new(); + IntelHexStructure irec = new(); - // Copy the ASCII hex encoding of the count field into hexBuff, convert it to a usable integer - dataCount = Convert.ToInt16(recordBuff.Substring(IHEX_COUNT_OFFSET, IHEX_COUNT_LEN), 16); + // Copy the ASCII hex encoding of the count field into hexBuff, convert it to a usable integer + dataCount = Convert.ToInt16(recordBuff.Substring(IHEX_COUNT_OFFSET, IHEX_COUNT_LEN), 16); - // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer - irec.address = Convert.ToUInt16(recordBuff.Substring(IHEX_ADDRESS_OFFSET, IHEX_ADDRESS_LEN), 16); + // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer + irec.address = Convert.ToUInt16(recordBuff.Substring(IHEX_ADDRESS_OFFSET, IHEX_ADDRESS_LEN), 16); - // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer - irec.type = Convert.ToInt16(recordBuff.Substring(IHEX_TYPE_OFFSET, IHEX_TYPE_LEN), 16); + // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer + irec.type = Convert.ToInt16(recordBuff.Substring(IHEX_TYPE_OFFSET, IHEX_TYPE_LEN), 16); - // Size check for start code, count, address, type, data and checksum fields - if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN + dataCount * 2 + IHEX_CHECKSUM_LEN)) - { - return null; - } + // Size check for start code, count, address, type, data and checksum fields + if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN + dataCount * 2 + IHEX_CHECKSUM_LEN)) + { + return null; + } - // Loop through each ASCII hex byte of the data field, pull it out into hexBuff, - // convert it and store the result in the data buffer of the Intel HEX8 record - for (i = 0; i < dataCount; i++) - { - // Times two i because every byte is represented by two ASCII hex characters - irec.data[i] = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + 2 * i, IHEX_ASCII_HEX_BYTE_LEN), 16); - } - irec.dataLen = dataCount; + // Loop through each ASCII hex byte of the data field, pull it out into hexBuff, + // convert it and store the result in the data buffer of the Intel HEX8 record + for (i = 0; i < dataCount; i++) + { + // Times two i because every byte is represented by two ASCII hex characters + irec.data[i] = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + 2 * i, IHEX_ASCII_HEX_BYTE_LEN), 16); + } + irec.dataLen = dataCount; - // Copy the ASCII hex encoding of the checksum field into hexBuff, convert it to a usable integer - irec.checksum = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + dataCount * 2, IHEX_CHECKSUM_LEN), 16); + // Copy the ASCII hex encoding of the checksum field into hexBuff, convert it to a usable integer + irec.checksum = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + dataCount * 2, IHEX_CHECKSUM_LEN), 16); - if (!irec.Verify()) - { - return null; - } + if (!irec.Verify()) + { + return null; + } - return irec; - } + return irec; + } - // Utility function to write an Intel HEX8 record to a file - public static void Write(IntelHexStructure irec, StreamWriter outStream) - { - // Check that the data length is in range - if (irec.dataLen > IHEX_MAX_DATA_LEN / 2) - { - return; - } + // Utility function to write an Intel HEX8 record to a file + public static void Write(IntelHexStructure irec, StreamWriter outStream) + { + // Check that the data length is in range + if (irec.dataLen > IHEX_MAX_DATA_LEN / 2) + { + return; + } - try - { - // Write the start code, data count, address, and type fields - outStream.Write(string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type)); - // Write the data bytes - for (int i = 0; i < irec.dataLen; i++) + try + { + // Write the start code, data count, address, and type fields + outStream.Write(string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type)); + // Write the data bytes + for (int i = 0; i < irec.dataLen; i++) { outStream.Write(string.Format("{0:X2}", irec.data[i])); } // Last but not least, the checksum outStream.WriteLine(string.Format("{0:X2}", irec.checksum)); - } - catch (Exception) - { - return; - } + } + catch (Exception) + { + return; + } - return; - } + return; + } - /// - /// Utility function to print the information stored in an Intel HEX8 record - /// - /// A boolean set to false by default, if set to true will provide extended information. - /// String which provides the output of the function, this does not write directly to the console. - public static string Print(IntelHexStructure irec, bool verbose = false) - { - int i; - string returnString; + /// + /// Utility function to print the information stored in an Intel HEX8 record + /// + /// A boolean set to false by default, if set to true will provide extended information. + /// String which provides the output of the function, this does not write directly to the console. + public static string Print(IntelHexStructure irec, bool verbose = false) + { + int i; + string returnString; - if (verbose) - { - returnString = string.Format("Intel HEX8 Record Type: \t{0}\n", irec.type); - returnString += string.Format("Intel HEX8 Record Address: \t0x{0:X4}\n", irec.address); - returnString += string.Format("Intel HEX8 Record Data: \t["); - for (i = 0; i < irec.dataLen; i++) - { - if (i + 1 < irec.dataLen) + if (verbose) + { + returnString = string.Format("Intel HEX8 Record Type: \t{0}\n", irec.type); + returnString += string.Format("Intel HEX8 Record Address: \t0x{0:X4}\n", irec.address); + returnString += string.Format("Intel HEX8 Record Data: \t["); + for (i = 0; i < irec.dataLen; i++) + { + if (i + 1 < irec.dataLen) { returnString += string.Format("0x{0:X02}, ", irec.data[i]); } @@ -237,20 +237,20 @@ public static string Print(IntelHexStructure irec, bool verbose = false) returnString += string.Format("0x{0:X02}", irec.data[i]); } } - returnString += string.Format("]\n"); - returnString += string.Format("Intel HEX8 Record Checksum: \t0x{0:X2}\n", irec.checksum); - } - else - { - returnString = string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type); - for (i = 0; i < irec.dataLen; i++) + returnString += string.Format("]\n"); + returnString += string.Format("Intel HEX8 Record Checksum: \t0x{0:X2}\n", irec.checksum); + } + else + { + returnString = string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type); + for (i = 0; i < irec.dataLen; i++) { returnString += string.Format("{0:X2}", irec.data[i]); } returnString += string.Format("{0:X2}", irec.checksum); - } - return (returnString); - } - } + } + return (returnString); + } + } } diff --git a/Sim80C51/Common/ListingCollection.cs b/Sim80C51/Common/ListingCollection.cs index 2aeed84..22db45a 100644 --- a/Sim80C51/Common/ListingCollection.cs +++ b/Sim80C51/Common/ListingCollection.cs @@ -4,8 +4,8 @@ namespace Sim80C51.Common { public class ListingCollection : ObservableCollection { - private readonly Dictionary entriesByAddress = new(); - private readonly Dictionary entriesByLabel = new(); + private readonly Dictionary entriesByAddress = []; + private readonly Dictionary entriesByLabel = []; public new void Remove(ListingEntry entry) { @@ -45,9 +45,8 @@ public class ListingCollection : ObservableCollection public void RemoveByAddress(ushort address) { - if (entriesByAddress.ContainsKey(address)) + if (entriesByAddress.TryGetValue(address, out ListingEntry? toRemove)) { - ListingEntry toRemove = entriesByAddress[address]; base.Remove(toRemove); entriesByAddress.Remove(address); if (!string.IsNullOrEmpty(toRemove.Label)) @@ -69,9 +68,9 @@ public bool Contains(string label) public ListingEntry? GetByAddress(ushort address) { - if (entriesByAddress.ContainsKey(address)) + if (entriesByAddress.TryGetValue(address, out ListingEntry? entry)) { - return entriesByAddress[address]; + return entry; } return null; @@ -79,9 +78,9 @@ public bool Contains(string label) public ListingEntry? GetByLabel(string label) { - if (entriesByLabel.ContainsKey(label)) + if (entriesByLabel.TryGetValue(label, out ListingEntry? entry)) { - return entriesByLabel[label]; + return entry; } return null; } diff --git a/Sim80C51/Common/ListingEntry.cs b/Sim80C51/Common/ListingEntry.cs index b52d0e2..6b76ac5 100644 --- a/Sim80C51/Common/ListingEntry.cs +++ b/Sim80C51/Common/ListingEntry.cs @@ -10,7 +10,7 @@ public class ListingEntry : NotifyPropertyChanged, IListingInstruction private ushort address; public List Data { get => data; set { data = value; DoPropertyChanged(); } } - private List data = new(); + private List data = []; public string DataString { get => dataString; private set { dataString = value; DoPropertyChanged(); } } private string dataString = string.Empty; @@ -22,7 +22,7 @@ public class ListingEntry : NotifyPropertyChanged, IListingInstruction private InstructionType instruction; public List Arguments { get => arguments; set { arguments = value; DoPropertyChanged(); } } - private List arguments = new(); + private List arguments = []; public string ArgumentString { get => argumentString; private set { argumentString = value; DoPropertyChanged(); } } private string argumentString = string.Empty; @@ -38,7 +38,7 @@ public class ListingEntry : NotifyPropertyChanged, IListingInstruction public void UpdateStrings() { - DataString = BitConverter.ToString(Data.ToArray()).Replace('-', ' '); + DataString = BitConverter.ToString([.. Data]).Replace('-', ' '); ArgumentString = string.Join(", ", arguments); } diff --git a/Sim80C51/Common/ListingFactory.cs b/Sim80C51/Common/ListingFactory.cs index 2e3cb37..e885510 100644 --- a/Sim80C51/Common/ListingFactory.cs +++ b/Sim80C51/Common/ListingFactory.cs @@ -76,10 +76,10 @@ public class ListingFactory public ListingCollection? Listing => listing; private ListingCollection? listing; - private readonly Dictionary ivBits = new(); - private readonly Dictionary reverseSfrMap = new(); - private readonly Dictionary reverseSfrBitMap = new(); - private readonly Dictionary ivMap = new(); + private readonly Dictionary ivBits = []; + private readonly Dictionary reverseSfrMap = []; + private readonly Dictionary reverseSfrBitMap = []; + private readonly Dictionary ivMap = []; public ListingFactory(Type processorType) { @@ -145,9 +145,9 @@ public ListingFactory WithListing(ListingCollection listing) public ListingCollection Build(BinaryReader br, string? label = "RESET") { - listing ??= new(); + listing ??= []; - Stack branches = new(); + Stack branches = []; ListingEntry entry; @@ -248,12 +248,12 @@ public ListingCollection Build(BinaryReader br, string? label = "RESET") if (ivBits.ContainsKey(entry.Arguments[0])) { string ivName = entry.Arguments[0][1..]; - if (ivMap.ContainsKey(ivName)) + if (ivMap.TryGetValue(ivName, out ushort ivAddress)) { branches.Push(new ListingEntry() { - TargetAddress = ivMap[ivName], - Arguments = new() { ivName } + TargetAddress = ivAddress, + Arguments = [ivName] }); } } @@ -368,7 +368,7 @@ private void CheckAddIv(string IEN, string valueStr, int maxBits, Stack entryD Instruction = InstructionType.DB, Data = entryData, Comment = entryData.ToAnsiString(), - Arguments = new() { "'" + entryData.ToEscapedAnsiString() + "'" } + Arguments = ["'" + entryData.ToEscapedAnsiString() + "'"] }; entry.UpdateStrings(); @@ -1367,7 +1367,7 @@ private void RestoreDbEntries(BinaryReader br) { Address = address, Instruction = InstructionType.DB, - Data = buffer.ToList(), + Data = [.. buffer], Comment = buffer.ToAnsiString(), Arguments = buffer.Select(c => c.ToString("x2") + 'h').ToList() }; diff --git a/Sim80C51/Controls/ListingEditor.xaml.cs b/Sim80C51/Controls/ListingEditor.xaml.cs index 63b2e7b..ae376d4 100644 --- a/Sim80C51/Controls/ListingEditor.xaml.cs +++ b/Sim80C51/Controls/ListingEditor.xaml.cs @@ -52,8 +52,8 @@ private void ListView_KeyUp(object sender, KeyEventArgs e) private void ListingView_KeyDown(object sender, KeyEventArgs e) { - Key[] ignore = new[] - { + Key[] ignore = + [ Key.B, Key.C, Key.J, @@ -62,7 +62,7 @@ private void ListingView_KeyDown(object sender, KeyEventArgs e) Key.S, Key.U, Key.X, - }; + ]; if (ignore.Contains(e.Key)) { diff --git a/Sim80C51/Controls/ListingEditorContext.cs b/Sim80C51/Controls/ListingEditorContext.cs index fb59cc1..65b7b08 100644 --- a/Sim80C51/Controls/ListingEditorContext.cs +++ b/Sim80C51/Controls/ListingEditorContext.cs @@ -13,7 +13,7 @@ namespace Sim80C51.Controls { public class ListingEditorContext : NotifyPropertyChanged { - private static readonly InstructionType[] jumpInstructions = new[] { + private static readonly InstructionType[] jumpInstructions = [ //Instruction.JMP, Address based Jump InstructionType.LJMP, InstructionType.AJMP, @@ -29,12 +29,12 @@ public class ListingEditorContext : NotifyPropertyChanged InstructionType.JNC, InstructionType.JZ, InstructionType.JNZ - }; + ]; private ListingFactory? factory; private BinaryReader? reader; - public ListingCollection Listing { get; } = new(); + public ListingCollection Listing { get; } = []; public ListingEntry? SelectedListingEntry { get => selectedListingEntry; set { selectedListingEntry = value; DoPropertyChanged(); } } private ListingEntry? selectedListingEntry; @@ -153,9 +153,9 @@ public class ListingEditorContext : NotifyPropertyChanged ushort currentAddress = entry.Address; int currentIndex = Listing.IndexOf(entry); string displayText = string.Empty; - List bytes = new(); + List bytes = []; - List entries = new(); + List entries = []; while (currentIndex < Listing.Count) { if (Listing[currentIndex].Instruction != InstructionType.DB) @@ -185,7 +185,7 @@ public class ListingEditorContext : NotifyPropertyChanged startIdx = dlg.StartIndex; endIdx = dlg.EndIndex; - factory.CreateString(reader, (ushort)(currentAddress + startIdx), bytes.ToArray()[startIdx..(endIdx + 1)].ToList()); + factory.CreateString(reader, (ushort)(currentAddress + startIdx), [.. bytes.ToArray()[startIdx..(endIdx + 1)]]); Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { })); diff --git a/Sim80C51/Sim80C51.csproj b/Sim80C51/Sim80C51.csproj index 5f41e37..4d89d1f 100644 --- a/Sim80C51/Sim80C51.csproj +++ b/Sim80C51/Sim80C51.csproj @@ -2,7 +2,7 @@ WinExe - net6.0-windows + net8.0-windows enable enable true @@ -10,7 +10,7 @@ - + diff --git a/Sim80C51/SimulatorWindowContext.cs b/Sim80C51/SimulatorWindowContext.cs index 3aaad10..5ddbd6b 100644 --- a/Sim80C51/SimulatorWindowContext.cs +++ b/Sim80C51/SimulatorWindowContext.cs @@ -22,7 +22,7 @@ public class SimulatorWindowContext : NotifyPropertyChanged private SimulatorWindow? owner; private Interfaces.I80C51? CPU; private Controls.ListingEditorContext? listingCtx; - private readonly SortedDictionary xmemCtx = new(); + private readonly SortedDictionary xmemCtx = []; private readonly DispatcherTimer stepTimer = new(); #endregion @@ -69,7 +69,7 @@ public class SimulatorWindowContext : NotifyPropertyChanged Processors.ByteRow[] rows = LoadByteRowsFromCompBase64(wsp.InternalMemory); for (int i = 0; i < rows.Length && i < CPU.CoreMemory.Count; i++) { - CPU.CoreMemory[i].UpdateFromBuffer(rows[i].Row.ToArray()); + CPU.CoreMemory[i].UpdateFromBuffer([.. rows[i].Row]); } CPU.RefreshUIProperies(); } @@ -472,7 +472,7 @@ public class SimulatorWindowContext : NotifyPropertyChanged #endregion #region Property Bindings - public Dictionary ProcessorList { get; } = new(); + public Dictionary ProcessorList { get; } = []; public Type? SelectedProcessor { get => selectedProcessor; set { selectedProcessor = value; DoPropertyChanged(); } } private Type? selectedProcessor; @@ -480,9 +480,9 @@ public class SimulatorWindowContext : NotifyPropertyChanged public bool ProcessorActivated { get => processorActivated; set { processorActivated = value; DoPropertyChanged(); } } private bool processorActivated = false; - public ObservableCollection Breakpoints { get; } = new(); + public ObservableCollection Breakpoints { get; } = []; - public ObservableDictionary MemoryPointer { get; } = new(); + public ObservableDictionary MemoryPointer { get; } = []; public string DptrAddValue { get => dptrAddValue; set { dptrAddValue = value; DoPropertyChanged(); } } private string dptrAddValue = string.Empty; @@ -726,7 +726,7 @@ private void SetRamByte(ushort address, byte value) StopStepTimer(); } - if (stepTimer.IsEnabled && MemoryPointer.ContainsKey(address) && MemoryPointer[address].Write) + if (stepTimer.IsEnabled && MemoryPointer.TryGetValue(address, out MemoryAccess? access) && access.Write) { StopStepTimer(); } @@ -739,7 +739,7 @@ private byte GetRamByte(ushort address) return 0xff; } - if (stepTimer.IsEnabled && MemoryPointer.ContainsKey(address) && MemoryPointer[address].Read) + if (stepTimer.IsEnabled && MemoryPointer.TryGetValue(address, out MemoryAccess? access) && access.Read) { StopStepTimer(); } @@ -755,7 +755,7 @@ private byte GetRamByte(ushort address) private byte GetCodeByte(ushort address) { - if (stepTimer.IsEnabled && MemoryPointer.ContainsKey(address) && MemoryPointer[address].Read) + if (stepTimer.IsEnabled && MemoryPointer.TryGetValue(address, out MemoryAccess? access) && access.Read) { StopStepTimer(); } diff --git a/Sim80C51/WSpace/Workspace.cs b/Sim80C51/WSpace/Workspace.cs index 92c95cd..0093878 100644 --- a/Sim80C51/WSpace/Workspace.cs +++ b/Sim80C51/WSpace/Workspace.cs @@ -13,10 +13,10 @@ public class Workspace [YamlMember(ScalarStyle = ScalarStyle.Literal)] public string Listing { get; set; } = string.Empty; - public Dictionary XMem { get; set; } = new(); - public List Breakpoints { get; set; } = new(); - public Dictionary AdditionalSettings { get; set; } = new(); - public Dictionary MemoryWatches { get; set; } = new(); - public List CallStack { get; set; } = new(); + public Dictionary XMem { get; set; } = []; + public List Breakpoints { get; set; } = []; + public Dictionary AdditionalSettings { get; set; } = []; + public Dictionary MemoryWatches { get; set; } = []; + public List CallStack { get; set; } = []; } } diff --git a/Sim80C51/XRefWindow.xaml.cs b/Sim80C51/XRefWindow.xaml.cs index d38491c..34122d2 100644 --- a/Sim80C51/XRefWindow.xaml.cs +++ b/Sim80C51/XRefWindow.xaml.cs @@ -10,7 +10,7 @@ namespace Sim80C51 /// public partial class XRefWindow : Window { - public List XRefs { get; set; } = new(); + public List XRefs { get; set; } = []; public ushort Target;