1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System.Runtime.InteropServices; 8 9 namespace Antmicro.Renode.Plugins.CoSimulationPlugin.Connection.Protocols 10 { 11 // ProtocolMessage must be in sync with the cosimulation library 12 [StructLayout(LayoutKind.Sequential, Pack = 2)] 13 public struct ProtocolMessage 14 { ProtocolMessageAntmicro.Renode.Plugins.CoSimulationPlugin.Connection.Protocols.ProtocolMessage15 public ProtocolMessage(ActionType actionId, ulong address, ulong data, int peripheralIndex) 16 { 17 this.ActionId = actionId; 18 this.Address = address; 19 this.Data = data; 20 this.PeripheralIndex = peripheralIndex; 21 } 22 SerializeAntmicro.Renode.Plugins.CoSimulationPlugin.Connection.Protocols.ProtocolMessage23 public byte[] Serialize() 24 { 25 var size = Marshal.SizeOf(this); 26 var result = new byte[size]; 27 var handler = default(GCHandle); 28 29 try 30 { 31 handler = GCHandle.Alloc(result, GCHandleType.Pinned); 32 Marshal.StructureToPtr(this, handler.AddrOfPinnedObject(), false); 33 } 34 finally 35 { 36 if(handler.IsAllocated) 37 { 38 handler.Free(); 39 } 40 } 41 42 return result; 43 } 44 DeserializeAntmicro.Renode.Plugins.CoSimulationPlugin.Connection.Protocols.ProtocolMessage45 public void Deserialize(byte[] message) 46 { 47 var handler = default(GCHandle); 48 try 49 { 50 handler = GCHandle.Alloc(message, GCHandleType.Pinned); 51 this = (ProtocolMessage)Marshal.PtrToStructure(handler.AddrOfPinnedObject(), typeof(ProtocolMessage)); 52 } 53 finally 54 { 55 if(handler.IsAllocated) 56 { 57 handler.Free(); 58 } 59 } 60 } 61 ToStringAntmicro.Renode.Plugins.CoSimulationPlugin.Connection.Protocols.ProtocolMessage62 public override string ToString() 63 { 64 return $"ProtocolMessage: ActionId={ActionId}, Address=0x{Address:X}, Data=0x{Data:X}, PeripheralIndex={PeripheralIndex}"; 65 } 66 67 public ActionType ActionId { get; set; } 68 public ulong Address { get; set; } 69 public ulong Data { get; set; } 70 public int PeripheralIndex { get; set; } 71 72 // Peripheral index used for messages that are not associated with any peripherals. 73 public const int NoPeripheralIndex = -1; 74 } 75 } 76