1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.Linq; 10 using System.Collections.Generic; 11 using ELFSharp.ELF; 12 using ELFSharp.UImage; 13 14 namespace Antmicro.Renode.Peripherals.CPU 15 { 16 public struct CPURegister 17 { CPURegisterAntmicro.Renode.Peripherals.CPU.CPURegister18 public CPURegister(int index, int width, bool isGeneral, bool isReadonly, string[] aliases = null) 19 { 20 if(width % 8 != 0) 21 { 22 throw new ArgumentException($"Unsupported width: {width}"); 23 } 24 25 Index = index; 26 IsGeneral = isGeneral; 27 Width = width; 28 IsReadonly = isReadonly; 29 Aliases = aliases; 30 } 31 ValueFromBytesAntmicro.Renode.Peripherals.CPU.CPURegister32 public RegisterValue ValueFromBytes(byte[] bytes, Endianess endianness) 33 { 34 if(bytes.Length > Width) 35 { 36 throw new ArgumentException($"Expected {Width} bytes, but {bytes.Length} received"); 37 } 38 bool needsByteSwap = (endianness == Endianess.LittleEndian) != BitConverter.IsLittleEndian; 39 40 RegisterValue result = 0; 41 42 var bytesWithPadding = Enumerable.Repeat<byte>(0, (Width / 8) - bytes.Length).Concat(bytes).ToArray(); 43 if(needsByteSwap) 44 { 45 bytesWithPadding = bytesWithPadding.Reverse().ToArray(); 46 } 47 48 switch(Width) 49 { 50 case 8: 51 result = bytesWithPadding[0]; 52 break; 53 case 16: 54 result = BitConverter.ToUInt16(bytesWithPadding, 0); 55 break; 56 case 32: 57 result = BitConverter.ToUInt32(bytesWithPadding, 0); 58 break; 59 case 64: 60 result = BitConverter.ToUInt64(bytesWithPadding, 0); 61 break; 62 default: 63 result = bytesWithPadding; 64 break; 65 } 66 67 return result; 68 } 69 70 public int Index { get; } 71 public bool IsGeneral { get; } 72 public int Width { get; } 73 public bool IsReadonly { get; } 74 public string[] Aliases { get; } 75 76 // this is to support monitor output ToStringAntmicro.Renode.Peripherals.CPU.CPURegister77 public override string ToString() 78 { 79 return Aliases != null 80 ? String.Join(" / ", Aliases) 81 : $"#{Index}"; 82 } 83 } 84 } 85 86