Skip to content

Commit

Permalink
update net 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-sven committed Dec 11, 2024
1 parent d343649 commit 04c516e
Show file tree
Hide file tree
Showing 29 changed files with 387 additions and 444 deletions.
15 changes: 4 additions & 11 deletions Sim80C51.Core/Processors/Attributes/IVAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 3 additions & 9 deletions Sim80C51.Core/Processors/Attributes/SFR16Attribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 3 additions & 9 deletions Sim80C51.Core/Processors/Attributes/SFRAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 4 additions & 11 deletions Sim80C51.Core/Processors/Attributes/SFRBitAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions Sim80C51.Core/Processors/ByteRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public static ByteRow[] InitRows(int size, bool enableMT48 = false)

public static ByteRow[] FromStream(Stream stream)
{
List<ByteRow> res = new();
List<ByteRow> res = [];

byte[] buf = new byte[ROW_WIDTH];
int row = 0;
Expand All @@ -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<IByteRow> Rows)
Expand Down
104 changes: 50 additions & 54 deletions Sim80C51.Core/Processors/C80C51.Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ public abstract partial class C80C51 : I80C51
/// <summary>
/// map for sfr addresses
/// </summary>
private readonly Dictionary<string, SFRAttribute> sfrMap = new();
private readonly Dictionary<string, SFRAttribute> sfrMap = [];

/// <summary>
/// map for bits to sfr
/// </summary>
private readonly Dictionary<string, SFRBitAttribute> sfrBitMap = new();
private readonly Dictionary<string, SFRBitAttribute> sfrBitMap = [];

/// <summary>
/// map for 16bit values
/// </summary>
private readonly Dictionary<string, SFR16Attribute> sfr16Map = new();
private readonly Dictionary<string, SFR16Attribute> sfr16Map = [];

protected readonly SortedList<byte, IV> ivList = new();
protected readonly SortedList<byte, IV> ivList = [];

private readonly Dictionary<string, List<Action>> bitCallback = new();
private readonly Dictionary<string, List<Action>> sfrCallback = new();
private readonly Dictionary<string, List<Action>> bitCallback = [];
private readonly Dictionary<string, List<Action>> sfrCallback = [];

/// <summary>
/// Constructor
Expand Down Expand Up @@ -111,25 +111,25 @@ public void RefreshUIProperies()

public void RegisterBitChangeCallback(string bitName, Action callback)
{
if (bitCallback.ContainsKey(bitName))
if (bitCallback.TryGetValue(bitName, out List<Action>? 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<Action>? callbackList))
{
sfrCallback[sfrName].Add(callback);
callbackList.Add(callback);
}
else
{
sfrCallback.Add(sfrName, new() { callback });
sfrCallback.Add(sfrName, [callback]);
}
}

Expand All @@ -142,28 +142,26 @@ public void RegisterSfrChangeCallback(string sfrName, Action callback)
/// <exception cref="ArgumentException">if register not found</exception>
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);
}

/// <summary>
Expand All @@ -174,24 +172,22 @@ protected void SetMem16FromProp(ushort value, [CallerMemberName] string sfr16Nam
/// <exception cref="ArgumentException">if register not found</exception>
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));
}

/// <summary>
Expand All @@ -203,23 +199,23 @@ protected ushort GetMem16FromProp([CallerMemberName] string sfr16Name = "")
/// <exception cref="ArgumentException">if bit or register not found</exception>
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))
Expand All @@ -237,18 +233,18 @@ protected bool SetBitFromProp(bool value, [CallerMemberName] string sfrBitName =
/// <exception cref="ArgumentException">if bit or register not found</exception>
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);
}

/// <summary>
Expand All @@ -261,20 +257,20 @@ protected bool GetBitFromProp([CallerMemberName] string sfrBitName = "")
/// <exception cref="ArgumentException">if register not found</exception>
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<Action>? callbackList))
{
foreach(Action callback in sfrCallback[sfrName])
foreach(Action callback in callbackList)
{
callback.Invoke();
}
Expand All @@ -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<Action>? bitCallbackList))
{
foreach (Action callback in bitCallback[sfrBit.Key])
foreach (Action callback in bitCallbackList)
{
callback.Invoke();
}
Expand All @@ -312,12 +308,12 @@ protected bool SetMemFromProp(byte value, [CallerMemberName] string sfrName = ""
/// <exception cref="ArgumentException">if register not found</exception>
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);
}

/// <summary>
Expand Down Expand Up @@ -395,17 +391,17 @@ protected byte GetMem(ushort address)
/// <returns>Bitmask of bits updated</returns>
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);
}

/// <summary>
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 04c516e

Please sign in to comment.