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 Antmicro.Renode.Logging.Profiling;
8 using Antmicro.Renode.Utilities;
9 
10 namespace Antmicro.Renode.Peripherals.CPU
11 {
12     public enum TraceFormat
13     {
14         PC,
15         Opcode,
16         PCAndOpcode,
17         Disassembly,
18         TraceBasedModel,
19     }
20 
21     public enum AdditionalDataType : byte
22     {
23         None = 0,
24         MemoryAccess = 1,
25         RiscVVectorConfiguration = 2,
26     }
27 
28     public abstract class AdditionalData
29     {
AdditionalData(ulong pc, AdditionalDataType type)30         public AdditionalData(ulong pc, AdditionalDataType type)
31         {
32             this.PC = pc;
33             this.Type = type;
34         }
35         public ulong PC { get; }
36         public AdditionalDataType Type { get; }
37 
GetStringRepresentation()38         public abstract string GetStringRepresentation();
GetBinaryRepresentation()39         public abstract byte[] GetBinaryRepresentation();
40     }
41 
42     public class MemoryAccessAdditionalData : AdditionalData
43     {
MemoryAccessAdditionalData(ulong pc, MemoryOperation operationType, ulong operationTargetVirtual, ulong operationTargetPhysical, ulong operationValue)44         public MemoryAccessAdditionalData(ulong pc, MemoryOperation operationType, ulong operationTargetVirtual, ulong operationTargetPhysical, ulong operationValue) : base(pc, AdditionalDataType.MemoryAccess)
45         {
46             this.OperationType = operationType;
47             this.OperationTargetVirtual = operationTargetVirtual;
48             this.OperationTargetPhysical = operationTargetPhysical;
49             this.OperationValue = operationValue;
50         }
51 
GetStringRepresentation()52         public override string GetStringRepresentation()
53         {
54             if(OperationTargetVirtual == OperationTargetPhysical)
55             {
56                 return $"{OperationType} with address 0x{OperationTargetVirtual:X}, value 0x{OperationValue:X}";
57             }
58             else
59             {
60                 return $"{OperationType} with address 0x{OperationTargetVirtual:X} => 0x{OperationTargetPhysical:X}, value 0x{OperationValue:X}";
61             }
62         }
63 
GetBinaryRepresentation()64         public override byte[] GetBinaryRepresentation()
65         {
66             /*
67               [0] = [operationType]
68               [1] = [operationTargetVirtual 63:56]
69               [2] = [operationTargetVirtual 55:48]
70               [3] = [operationTargetVirtual 47:40]
71               [4] = [operationTargetVirtual 39:32]
72               [5] = [operationTargetVirtual 31:24]
73               [6] = [operationTargetVirtual 23:16]
74               [7] = [operationTargetVirtual 15:8]
75               [8] = [operationTargetVirtual 7:0]
76               [9] = [operationValue 63:56]
77               [10] = [operationValue 55:48]
78               [11] = [operationValue 47:40]
79               [12] = [operationValue 39:32]
80               [13] = [operationValue 31:24]
81               [14] = [operationValue 23:16]
82               [15] = [operationValue 15:8]
83               [16] = [operationValue 7:0]
84               [17] = [operationTargetPhysical 63:56]
85               [18] = [operationTargetPhysical 55:48]
86               [19] = [operationTargetPhysical 47:40]
87               [20] = [operationTargetPhysical 39:32]
88               [21] = [operationTargetPhysical 31:24]
89               [22] = [operationTargetPhysical 23:16]
90               [23] = [operationTargetPhysical 15:8]
91               [24] = [operationTargetPhysical 7:0]
92             */
93             var byteLength = sizeof(ulong) + sizeof(ulong) + sizeof(ulong) + 1;
94             var output = new byte[byteLength];
95             output[0] = (byte)OperationType;
96             BitHelper.GetBytesFromValue(output, 1, OperationTargetVirtual, sizeof(ulong), true);
97             BitHelper.GetBytesFromValue(output, 9, OperationValue, sizeof(ulong), true);
98             BitHelper.GetBytesFromValue(output, 17, OperationTargetPhysical, sizeof(ulong), true);
99 
100             return output;
101         }
102 
103         public ulong OperationTargetVirtual { get; }
104         public ulong OperationTargetPhysical { get; }
105         public ulong OperationValue { get; }
106         public MemoryOperation OperationType { get; }
107     }
108 
109     public class RiscVVectorConfigurationData : AdditionalData
110     {
RiscVVectorConfigurationData(ulong pc, ulong vl, ulong vtype)111         public RiscVVectorConfigurationData(ulong pc, ulong vl, ulong vtype) : base(pc, AdditionalDataType.RiscVVectorConfiguration)
112         {
113             this.VectorLength = vl;
114             this.VectorType = vtype;
115         }
116 
GetStringRepresentation()117         public override string GetStringRepresentation()
118         {
119             return $"Vector configured to VL: 0x{VectorLength:X}, VTYPE: 0x{VectorType:X}";
120         }
121 
GetBinaryRepresentation()122         public override byte[] GetBinaryRepresentation()
123         {
124             /*
125               [0] = [VectorLength 63:56]
126               [1] = [VectorLength 55:48]
127               [2] = [VectorLength 47:40]
128               [3] = [VectorLength 39:32]
129               [4] = [VectorLength 31:24]
130               [5] = [VectorLength 23:16]
131               [6] = [VectorLength 15:8]
132               [7] = [VectorLength 7:0]
133               [8] = [VectorType 63:56]
134               [9] = [VectorType 55:48]
135               [10] = [VectorType 47:40]
136               [11] = [VectorType 39:32]
137               [12] = [VectorType 31:24]
138               [13] = [VectorType 23:16]
139               [14] = [VectorType 15:8]
140               [15] = [VectorType 7:0]
141             */
142             var byteLength = sizeof(ulong) * 2;
143             var output = new byte[byteLength];
144 
145             BitHelper.GetBytesFromValue(output, 0, VectorLength, sizeof(ulong), true);
146             BitHelper.GetBytesFromValue(output, sizeof(ulong), VectorType, sizeof(ulong), true);
147             return output;
148         }
149 
150         public ulong VectorLength { get; }
151         public ulong VectorType { get; }
152     }
153 }
154