mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
engine: pass all checks with engine check
This fixes bugs in SUB, XOR, MUL, NOT, and gets register windows working. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
acc981638e
commit
4790686dd9
1 changed files with 86 additions and 517 deletions
|
|
@ -26,34 +26,33 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
this.engine = engine;
|
||||
this.size = size;
|
||||
this.data = new UInt32[size / 4];
|
||||
this.FIELD_MAX = new UInt256(BigInteger.Pow(2, 255) - 19);
|
||||
this.c0 = new UInt256(0);
|
||||
this.c1 = new UInt256(1);
|
||||
this.FIELD_MAX = BigInteger.Pow(2, 255) - 19;
|
||||
this.UINT_MAX = BigInteger.Pow(2, 256) - 1;
|
||||
this.c0 = new BigInteger(0);
|
||||
this.c1 = new BigInteger(1);
|
||||
|
||||
// (A-2)/4
|
||||
this.c2 = new UInt256(121665);
|
||||
this.c2 = new BigInteger(121665); // (A-2)/4
|
||||
this.c3 = this.FIELD_MAX;
|
||||
// (A+2)/4
|
||||
this.c4 = new UInt256(121666);
|
||||
this.c5 = new UInt256(5);
|
||||
this.c6 = new UInt256(10);
|
||||
this.c7 = new UInt256(20);
|
||||
this.c8 = new UInt256(50);
|
||||
this.c9 = new UInt256(100);
|
||||
this.c4 = new BigInteger(121666); // (A+2)/4
|
||||
this.c5 = new BigInteger(5);
|
||||
this.c6 = new BigInteger(10);
|
||||
this.c7 = new BigInteger(20);
|
||||
this.c8 = new BigInteger(50);
|
||||
this.c9 = new BigInteger(100);
|
||||
}
|
||||
|
||||
public UInt256 FIELD_MAX;
|
||||
// private UInt256 UINT_MAX;
|
||||
private UInt256 c0;
|
||||
private UInt256 c1;
|
||||
private UInt256 c2;
|
||||
private UInt256 c3;
|
||||
private UInt256 c4;
|
||||
private UInt256 c5;
|
||||
private UInt256 c6;
|
||||
private UInt256 c7;
|
||||
private UInt256 c8;
|
||||
private UInt256 c9;
|
||||
public BigInteger FIELD_MAX;
|
||||
private BigInteger UINT_MAX;
|
||||
private BigInteger c0;
|
||||
private BigInteger c1;
|
||||
private BigInteger c2;
|
||||
private BigInteger c3;
|
||||
private BigInteger c4;
|
||||
private BigInteger c5;
|
||||
private BigInteger c6;
|
||||
private BigInteger c7;
|
||||
private BigInteger c8;
|
||||
private BigInteger c9;
|
||||
private Machine machine;
|
||||
private Engine engine;
|
||||
private long size;
|
||||
|
|
@ -70,7 +69,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
return this.data[offset / 4];
|
||||
}
|
||||
|
||||
public UInt256 ReadRegister(ulong rf, ulong register, bool constant)
|
||||
public BigInteger ReadRegister(ulong rf, ulong register, bool constant)
|
||||
{
|
||||
// this.Log(LogLevel.Error, "Reading register {0} (const? {1}) from rf {2}", register, constant, rf);
|
||||
if (constant)
|
||||
|
|
@ -87,19 +86,19 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
case 7: return this.c7;
|
||||
case 8: return this.c8;
|
||||
case 9: return this.c9;
|
||||
default: return new UInt256(0);
|
||||
default: return new BigInteger(0);
|
||||
}
|
||||
}
|
||||
var src_bytes = new byte[32];
|
||||
var src_bytes = new byte[33]; // Keep leading byte 0 to force unsigned
|
||||
|
||||
if ((rf >= 16) || (register >= 32))
|
||||
{
|
||||
throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf));
|
||||
}
|
||||
|
||||
for (ulong i = 0; i < (ulong)src_bytes.Length; i += 4)
|
||||
for (ulong i = 0; i < 32; i += 4)
|
||||
{
|
||||
var offset = 0x10000 + (rf * 512) + (register * 32) + i;
|
||||
var offset = 0x10000 + (rf * 1024) + (register * 32) + i;
|
||||
var bytes = BitConverter.GetBytes(this.data[offset / 4]);
|
||||
if (bytes.Length != 4)
|
||||
{
|
||||
|
|
@ -115,22 +114,44 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
}
|
||||
}
|
||||
// this.Log(LogLevel.Error, "Returning read value from {0}-byte array", src_bytes.Length);
|
||||
return new UInt256(src_bytes);
|
||||
return new BigInteger(src_bytes);
|
||||
}
|
||||
|
||||
public void WriteRegister(long rf, long register, UInt256 value)
|
||||
public void WriteRegister(long rf, long register, BigInteger value)
|
||||
{
|
||||
if ((rf >= 16) || (register >= 32))
|
||||
{
|
||||
throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf));
|
||||
}
|
||||
var offset = 0x10000 + (rf * 512) + (register * 32);
|
||||
var bytes = value.ToByteArray();
|
||||
for (uint i = 0; i < (uint)bytes.Length; i += 4)
|
||||
{
|
||||
this.data[(offset + i) / 4] = (((uint)bytes[i + 0]) << 0) | (((uint)bytes[i + 1]) << 8) | (((uint)bytes[i + 2]) << 16) | ((uint)(bytes[i + 3]) << 24);
|
||||
|
||||
var bytes = (value & this.UINT_MAX).ToByteArray();
|
||||
if (bytes.Length < 32)
|
||||
{
|
||||
bytes = bytes.Concat(new byte[32 - bytes.Length]);
|
||||
}
|
||||
|
||||
var offset = 0x10000 + (rf * 1024) + (register * 32);
|
||||
for (uint i = 0; i < 32; i += 4)
|
||||
{
|
||||
this.data[(offset + i) / 4] = (((uint)bytes[i + 0]) << 0)
|
||||
| (((uint)bytes[i + 1]) << 8)
|
||||
| (((uint)bytes[i + 2]) << 16)
|
||||
| ((uint)(bytes[i + 3]) << 24);
|
||||
}
|
||||
}
|
||||
|
||||
public String FormatValue(BigInteger value)
|
||||
{
|
||||
var trimmed_value = value & this.UINT_MAX;
|
||||
var bytes = trimmed_value.ToByteArray();
|
||||
if (bytes.Length < 32)
|
||||
{
|
||||
bytes = bytes.Concat(new byte[32 - bytes.Length]);
|
||||
}
|
||||
StringBuilder hex = new StringBuilder(bytes.Length * 2);
|
||||
foreach (byte b in bytes.Reverse())
|
||||
hex.AppendFormat("{0:x2}", b);
|
||||
return hex.ToString();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
|
|
@ -176,7 +197,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
case 6: return "ten";
|
||||
case 7: return "twenty";
|
||||
case 8: return "fifty";
|
||||
case 9: return "one hundred";
|
||||
case 9: return "one_hundred";
|
||||
default: return "undef";
|
||||
}
|
||||
}
|
||||
|
|
@ -251,7 +272,6 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
{
|
||||
return "undef";
|
||||
}
|
||||
// return "unhandled";
|
||||
}
|
||||
|
||||
public UInt32 Op;
|
||||
|
|
@ -290,10 +310,12 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
|
||||
private void EngineThread()
|
||||
{
|
||||
bool shouldLog = false;
|
||||
try
|
||||
{
|
||||
// this.Log(LogLevel.Error, "ENGINE: Beginning execution. Current MPC: 0x{0:X}", this.mpc);
|
||||
this.mpc = this.mpstart;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (this.mpc >= this.mplen)
|
||||
|
|
@ -315,25 +337,24 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
break;
|
||||
|
||||
case 2: // MSK
|
||||
var bit = this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & 1;
|
||||
if (bit != 0)
|
||||
if ((this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & 1) != 0)
|
||||
{
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.ReadRegister(this.window, op.Rb, op.Cb));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new UInt256(0));
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0));
|
||||
}
|
||||
break;
|
||||
|
||||
case 3: // XOR
|
||||
this.engineRam.WriteRegister(this.window, op.Wd,
|
||||
this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) ^ this.engineRam.ReadRegister(this.window, op.Rb, op.Cb));
|
||||
(BigInteger.Pow(2, 256) | this.engineRam.ReadRegister(this.window, op.Ra, op.Ca)) ^ this.engineRam.ReadRegister(this.window, op.Rb, op.Cb));
|
||||
break;
|
||||
|
||||
case 4: // NOT
|
||||
this.engineRam.WriteRegister(this.window, op.Wd,
|
||||
~this.engineRam.ReadRegister(this.window, op.Ra, op.Ca));
|
||||
~(this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) | BigInteger.Pow(2, 256)));
|
||||
break;
|
||||
|
||||
case 5: // ADD
|
||||
|
|
@ -342,15 +363,24 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
break;
|
||||
|
||||
case 6: // SUB
|
||||
this.engineRam.WriteRegister(this.window, op.Wd,
|
||||
this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) - this.engineRam.ReadRegister(this.window, op.Rb, op.Cb));
|
||||
{
|
||||
var left = this.engineRam.ReadRegister(this.window, op.Ra, op.Ca);
|
||||
var right = this.engineRam.ReadRegister(this.window, op.Rb, op.Cb);
|
||||
var result = left - right;
|
||||
// Wrap integer
|
||||
if (result < 0)
|
||||
{
|
||||
result = BigInteger.Pow(2, 256) - result;
|
||||
}
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, result);
|
||||
}
|
||||
break;
|
||||
|
||||
case 7: // MUL
|
||||
// this.Log(LogLevel.Error, "Ra: {0} Rb: {1} Wd: {2}", this.engineRam.ReadRegister(this.window, op.Ra, op.Ca), this.engineRam.ReadRegister(this.window, op.Rb, op.Cb),
|
||||
// (this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) * this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)));
|
||||
this.engineRam.WriteRegister(this.window, op.Wd,
|
||||
(this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) * this.engineRam.ReadRegister(this.window, op.Rb, op.Cb))
|
||||
(this.engineRam.ReadRegister(this.window, op.Ra, op.Ca)
|
||||
* this.engineRam.ReadRegister(this.window, op.Rb, op.Cb))
|
||||
% this.engineRam.FIELD_MAX
|
||||
);
|
||||
break;
|
||||
|
||||
|
|
@ -361,7 +391,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
}
|
||||
else
|
||||
{
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new UInt256(0));
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0));
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -381,13 +411,13 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
break;
|
||||
|
||||
case 12: // XBT
|
||||
if ((this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & (new UInt256(1) << 254)) != 0)
|
||||
if ((this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & (new BigInteger(1) << 254)) != 0)
|
||||
{
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new UInt256(1));
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new UInt256(0));
|
||||
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0));
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -400,7 +430,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
{
|
||||
break;
|
||||
}
|
||||
// this.Log(LogLevel.Error, " (Result) {0}", this.engineRam.ReadRegister(this.window, op.Wd, false).ToString());
|
||||
// this.Log(LogLevel.Error, " (Result) {0}", this.engineRam.FormatValue(this.engineRam.ReadRegister(this.window, op.Wd, false)));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
|
@ -418,15 +448,15 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
private void DefineRegisters()
|
||||
{
|
||||
Registers.WINDOW.Define(this)
|
||||
.WithValueField(0, 4, name: "WINDOW", valueProviderCallback: (_) => { return this.window; }, changeCallback: (_, value) => { this.window = (byte)(value & 0xf); })
|
||||
.WithValueField(0, 4, name: "WINDOW", valueProviderCallback: (_) => this.window, changeCallback: (_, value) => this.window = (byte)(value & 0xf))
|
||||
;
|
||||
|
||||
Registers.MPSTART.Define(this)
|
||||
.WithValueField(0, 11, name: "MPSTART", valueProviderCallback: (_) => { return this.mpstart; }, changeCallback: (_, value) => { this.mpstart = (UInt16)(value & 0x7ff); })
|
||||
.WithValueField(0, 11, name: "MPSTART", valueProviderCallback: (_) => this.mpstart, changeCallback: (_, value) => this.mpstart = (UInt16)(value & 0x7ff))
|
||||
;
|
||||
|
||||
Registers.MPLEN.Define(this)
|
||||
.WithValueField(0, 11, name: "MPLEN", valueProviderCallback: _ => this.mplen, changeCallback: (_, value) => { this.mplen = (UInt16)(value & 0x7ff); })
|
||||
.WithValueField(0, 11, name: "MPLEN", valueProviderCallback: _ => this.mplen, changeCallback: (_, value) => this.mplen = (UInt16)(value & 0x7ff))
|
||||
;
|
||||
|
||||
Registers.CONTROL.Define(this)
|
||||
|
|
@ -566,466 +596,6 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public struct UInt256 : IComparable<UInt256>
|
||||
{
|
||||
private static readonly UInt256 _zero = new UInt256(new byte[0]);
|
||||
|
||||
// parts are big-endian
|
||||
private readonly UInt64 part1;
|
||||
private readonly UInt64 part2;
|
||||
private readonly UInt64 part3;
|
||||
private readonly UInt64 part4;
|
||||
private readonly int hashCode;
|
||||
private readonly bool notDefault;
|
||||
|
||||
public UInt256(byte[] value)
|
||||
{
|
||||
// if (value.Length > 32 && !(value.Length == 33 && value[32] == 0))
|
||||
// {
|
||||
// throw new Exception(String.Format("value.Length = {0}", value.Length));
|
||||
// // throw new ArgumentOutOfRangeException();
|
||||
// }
|
||||
|
||||
if (value.Length < 32)
|
||||
value = value.Concat(new byte[32 - value.Length]);
|
||||
|
||||
// read LE parts in reverse order to store in BE
|
||||
var part1Bytes = new byte[8];
|
||||
var part2Bytes = new byte[8];
|
||||
var part3Bytes = new byte[8];
|
||||
var part4Bytes = new byte[8];
|
||||
Buffer.BlockCopy(value, 0, part4Bytes, 0, 8);
|
||||
Buffer.BlockCopy(value, 8, part3Bytes, 0, 8);
|
||||
Buffer.BlockCopy(value, 16, part2Bytes, 0, 8);
|
||||
Buffer.BlockCopy(value, 24, part1Bytes, 0, 8);
|
||||
|
||||
// convert parts and store
|
||||
this.part1 = Bits.ToUInt64(part1Bytes);
|
||||
this.part2 = Bits.ToUInt64(part2Bytes);
|
||||
this.part3 = Bits.ToUInt64(part3Bytes);
|
||||
this.part4 = Bits.ToUInt64(part4Bytes);
|
||||
|
||||
this.hashCode = this.part1.GetHashCode() ^ this.part2.GetHashCode() ^ this.part3.GetHashCode() ^ this.part4.GetHashCode();
|
||||
|
||||
this.notDefault = true;
|
||||
}
|
||||
|
||||
private UInt256(UInt64 part1, UInt64 part2, UInt64 part3, UInt64 part4)
|
||||
{
|
||||
this.part1 = part1;
|
||||
this.part2 = part2;
|
||||
this.part3 = part3;
|
||||
this.part4 = part4;
|
||||
|
||||
this.hashCode = this.part1.GetHashCode() ^ this.part2.GetHashCode() ^ this.part3.GetHashCode() ^ this.part4.GetHashCode();
|
||||
|
||||
this.notDefault = true;
|
||||
}
|
||||
|
||||
public UInt256(int value)
|
||||
: this(Bits.GetBytes(value))
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
public UInt256(long value)
|
||||
: this(Bits.GetBytes(value))
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
public UInt256(uint value)
|
||||
: this(Bits.GetBytes(value))
|
||||
{ }
|
||||
|
||||
public UInt256(ulong value)
|
||||
: this(Bits.GetBytes(value))
|
||||
{ }
|
||||
|
||||
public UInt256(BigInteger value)
|
||||
: this(value.ToByteArray())
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
public bool IsDefault { get { return !this.notDefault; } }
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
var buffer = new byte[32];
|
||||
Buffer.BlockCopy(Bits.GetBytes(this.part4), 0, buffer, 0, 8);
|
||||
Buffer.BlockCopy(Bits.GetBytes(this.part3), 0, buffer, 8, 8);
|
||||
Buffer.BlockCopy(Bits.GetBytes(this.part2), 0, buffer, 16, 8);
|
||||
Buffer.BlockCopy(Bits.GetBytes(this.part1), 0, buffer, 24, 8);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public BigInteger ToBigInteger()
|
||||
{
|
||||
// add a trailing zero so that value is always positive
|
||||
return new BigInteger(ToByteArray().Concat(0));
|
||||
}
|
||||
|
||||
public int CompareTo(UInt256 other)
|
||||
{
|
||||
if (this == other)
|
||||
return 0;
|
||||
else if (this < other)
|
||||
return -1;
|
||||
else if (this > other)
|
||||
return +1;
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
public static UInt256 Zero
|
||||
{
|
||||
get { return _zero; }
|
||||
}
|
||||
|
||||
public static explicit operator BigInteger(UInt256 value)
|
||||
{
|
||||
return value.ToBigInteger();
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(byte value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(int value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(long value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(sbyte value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(short value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(uint value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(ulong value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static implicit operator UInt256(ushort value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static bool operator ==(UInt256 left, UInt256 right)
|
||||
{
|
||||
return left.part1 == right.part1 && left.part2 == right.part2 && left.part3 == right.part3 && left.part4 == right.part4;
|
||||
}
|
||||
|
||||
public static bool operator !=(UInt256 left, UInt256 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static bool operator <(UInt256 left, UInt256 right)
|
||||
{
|
||||
if (left.part1 < right.part1)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 < right.part2)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 < right.part3)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 == right.part3 && left.part4 < right.part4)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool operator <=(UInt256 left, UInt256 right)
|
||||
{
|
||||
if (left.part1 < right.part1)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 < right.part2)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 < right.part3)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 == right.part3 && left.part4 < right.part4)
|
||||
return true;
|
||||
|
||||
return left == right;
|
||||
}
|
||||
|
||||
public static bool operator >(UInt256 left, UInt256 right)
|
||||
{
|
||||
if (left.part1 > right.part1)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 > right.part2)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 > right.part3)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 == right.part3 && left.part4 > right.part4)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool operator >=(UInt256 left, UInt256 right)
|
||||
{
|
||||
if (left.part1 > right.part1)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 > right.part2)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 > right.part3)
|
||||
return true;
|
||||
else if (left.part1 == right.part1 && left.part2 == right.part2 && left.part3 == right.part3 && left.part4 > right.part4)
|
||||
return true;
|
||||
|
||||
return left == right;
|
||||
}
|
||||
|
||||
// TODO doesn't compare against other numerics
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is UInt256))
|
||||
return false;
|
||||
|
||||
var other = (UInt256)obj;
|
||||
return other.part1 == this.part1 && other.part2 == this.part2 && other.part3 == this.part3 && other.part4 == this.part4;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.hashCode;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var bytes = this.ToByteArray();
|
||||
StringBuilder hex = new StringBuilder(bytes.Length * 2);
|
||||
foreach (byte b in bytes.Reverse())
|
||||
hex.AppendFormat("{0:x2}", b);
|
||||
return hex.ToString();
|
||||
}
|
||||
|
||||
public static UInt256 Parse(string value)
|
||||
{
|
||||
return new UInt256(BigInteger.Parse("0" + value).ToByteArray());
|
||||
}
|
||||
|
||||
public static UInt256 Parse(string value, IFormatProvider provider)
|
||||
{
|
||||
return new UInt256(BigInteger.Parse("0" + value, provider).ToByteArray());
|
||||
}
|
||||
|
||||
public static UInt256 Parse(string value, NumberStyles style)
|
||||
{
|
||||
return new UInt256(BigInteger.Parse("0" + value, style).ToByteArray());
|
||||
}
|
||||
|
||||
public static UInt256 Parse(string value, NumberStyles style, IFormatProvider provider)
|
||||
{
|
||||
return new UInt256(BigInteger.Parse("0" + value, style, provider).ToByteArray());
|
||||
}
|
||||
|
||||
public static double Log(UInt256 value, double baseValue)
|
||||
{
|
||||
return BigInteger.Log(value.ToBigInteger(), baseValue);
|
||||
}
|
||||
|
||||
public static UInt256 operator %(UInt256 dividend, UInt256 divisor)
|
||||
{
|
||||
return new UInt256(dividend.ToBigInteger() % divisor.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 Pow(UInt256 value, int exponent)
|
||||
{
|
||||
return new UInt256(BigInteger.Pow(value.ToBigInteger(), exponent));
|
||||
}
|
||||
|
||||
public static UInt256 operator *(UInt256 left, UInt256 right)
|
||||
{
|
||||
return new UInt256((left.ToBigInteger() * right.ToBigInteger()) % ((BigInteger.Pow(2, 255) - 19)));
|
||||
}
|
||||
|
||||
public static UInt256 operator >>(UInt256 value, int shift)
|
||||
{
|
||||
return new UInt256(value.ToBigInteger() >> shift);
|
||||
}
|
||||
|
||||
public static UInt256 operator |(UInt256 left, UInt256 right)
|
||||
{
|
||||
return new UInt256(left.ToBigInteger() | right.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator ^(UInt256 left, UInt256 right)
|
||||
{
|
||||
return new UInt256(left.ToBigInteger() ^ right.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator <<(UInt256 value, int shift)
|
||||
{
|
||||
return new UInt256(value.ToBigInteger() << shift);
|
||||
}
|
||||
|
||||
public static UInt256 operator /(UInt256 dividend, UInt256 divisor)
|
||||
{
|
||||
return new UInt256(dividend.ToBigInteger() / divisor.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator &(UInt256 dividend, UInt256 divisor)
|
||||
{
|
||||
return new UInt256(dividend.ToBigInteger() & divisor.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator +(UInt256 dividend, UInt256 divisor)
|
||||
{
|
||||
return new UInt256(dividend.ToBigInteger() + divisor.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator -(UInt256 dividend, UInt256 divisor)
|
||||
{
|
||||
if (dividend < divisor)
|
||||
{
|
||||
return new UInt256((BigInteger.Pow(2, 255) - 1) - (divisor.ToBigInteger() - dividend.ToBigInteger()));
|
||||
}
|
||||
return new UInt256(dividend.ToBigInteger() - divisor.ToBigInteger());
|
||||
}
|
||||
|
||||
public static UInt256 operator ~(UInt256 value)
|
||||
{
|
||||
return new UInt256(~value.part1, ~value.part2, ~value.part3, ~value.part4);
|
||||
}
|
||||
|
||||
public static UInt256 DivRem(UInt256 dividend, UInt256 divisor, out UInt256 remainder)
|
||||
{
|
||||
BigInteger remainderBigInt;
|
||||
var result = new UInt256(BigInteger.DivRem(dividend.ToBigInteger(), divisor.ToBigInteger(), out remainderBigInt));
|
||||
remainder = new UInt256(remainderBigInt);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static explicit operator double(UInt256 value)
|
||||
{
|
||||
return (double)value.ToBigInteger();
|
||||
}
|
||||
}
|
||||
|
||||
public class Bits
|
||||
{
|
||||
private static readonly bool isLE = BitConverter.IsLittleEndian;
|
||||
|
||||
public static byte[] GetBytes(Int16 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(UInt16 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytesBE(UInt16 value)
|
||||
{
|
||||
return OrderBE(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(Int32 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(UInt32 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytesBE(UInt32 value)
|
||||
{
|
||||
return OrderBE(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(Int64 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(UInt64 value)
|
||||
{
|
||||
return Order(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytesBE(UInt64 value)
|
||||
{
|
||||
return OrderBE(BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(UInt256 value)
|
||||
{
|
||||
return value.ToByteArray();
|
||||
}
|
||||
|
||||
public static string ToString(byte[] value)
|
||||
{
|
||||
return BitConverter.ToString(Order(value));
|
||||
}
|
||||
|
||||
public static UInt16 ToUInt16(byte[] value)
|
||||
{
|
||||
return BitConverter.ToUInt16(Order(value), startIndex: 0);
|
||||
}
|
||||
|
||||
public static UInt16 ToUInt16BE(byte[] value)
|
||||
{
|
||||
return BitConverter.ToUInt16(OrderBE(value), startIndex: 0);
|
||||
}
|
||||
|
||||
public static UInt32 ToUInt32(byte[] value)
|
||||
{
|
||||
return BitConverter.ToUInt32(Order(value), startIndex: 0);
|
||||
}
|
||||
|
||||
public static UInt64 ToUInt64(byte[] value)
|
||||
{
|
||||
return BitConverter.ToUInt64(Order(value), startIndex: 0);
|
||||
}
|
||||
|
||||
public static UInt64 ToUInt64(byte[] value, int startIndex)
|
||||
{
|
||||
return BitConverter.ToUInt64(Order(value), startIndex);
|
||||
}
|
||||
|
||||
public static UInt256 ToUInt256(byte[] value)
|
||||
{
|
||||
return new UInt256(value);
|
||||
}
|
||||
|
||||
public static byte[] Order(byte[] value)
|
||||
{
|
||||
return isLE ? value : value.Reverse().ToArray();
|
||||
}
|
||||
|
||||
public static byte[] OrderBE(byte[] value)
|
||||
{
|
||||
return isLE ? value.Reverse().ToArray() : value;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExtensionMethods
|
||||
{
|
||||
|
||||
|
|
@ -1044,6 +614,5 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
|
|||
buffer[buffer.Length - 1] = second;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue