-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstructionType.hpp
290 lines (234 loc) · 7.77 KB
/
InstructionType.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef VM_Instruction_InstructionType
#define VM_Instruction_InstructionType
#include <stdint.h>
#include "HwError.hpp"
namespace Instruction
{
const unsigned int NumInstructionTypes = 7;
const unsigned int NumInstructions = 1<<7;
typedef enum
{
InstructionType_Null = 0,
InstructionType_R,
InstructionType_I,
InstructionType_S,
InstructionType_SB,
InstructionType_U,
InstructionType_UJ
} InstructionType;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint32_t garb : 25;
} ParsedIType_Null;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t rd : 5;
uint8_t funct3 : 3;
uint8_t rs1 : 5;
uint8_t rs2 : 5;
uint8_t funct7 : 7;
} ParsedIType_R;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t rd : 5;
uint8_t funct3 : 3;
uint8_t rs1 : 5;
uint16_t imm0to11 : 12;
} ParsedIType_I;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t imm0to4 : 5;
uint8_t funct3 : 3;
uint8_t rs1 : 5;
uint8_t rs2 : 5;
uint8_t imm5to11 : 7;
} ParsedIType_S;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t imm11and1to4 : 5;
uint8_t funct3 : 3;
uint8_t rs1 : 5;
uint8_t rs2 : 5;
uint8_t imm5to10and12 : 7;
} ParsedIType_SB;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t rd : 5;
uint32_t imm12to31 : 20;
} ParsedIType_U;
typedef struct __attribute__ ((__packed__))
{
uint8_t opcode : 7;
uint8_t rd : 5;
uint32_t imm12to19and11and1to10and20 : 20;
} ParsedIType_UJ;
typedef union
{
ParsedIType_Null Null;
ParsedIType_R R;
ParsedIType_I I;
ParsedIType_S S;
ParsedIType_SB SB;
ParsedIType_U U;
ParsedIType_UJ UJ;
} ParsedIType;
typedef struct __attribute__ ((__packed__))
{
ParsedIType ParsedInst;
InstructionType Type;
} ParsedInstruction;
class InstructionParser
{
private:
HwError LastHwError;
InstructionType ITypeArray[NumInstructions];
ParsedIType_Null ParseInstructionNull( uint32_t instruction )
{
ParsedIType_Null result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_R ParseInstructionR( uint32_t instruction )
{
ParsedIType_R result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_I ParseInstructionI( uint32_t instruction )
{
ParsedIType_I result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_S ParseInstructionS( uint32_t instruction )
{
ParsedIType_S result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_SB ParseInstructionSB( uint32_t instruction )
{
ParsedIType_SB result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_U ParseInstructionU( uint32_t instruction )
{
ParsedIType_U result;
*(uint32_t*)(&result) = instruction;
return result;
}
ParsedIType_UJ ParseInstructionUJ( uint32_t instruction )
{
ParsedIType_UJ result;
*(uint32_t*)(&result) = instruction;
return result;
}
public:
InstructionParser()
{
uint8_t iterator;
for( iterator = 0; iterator < NumInstructions; iterator++ )
{
ITypeArray[iterator] = InstructionType_Null;
}
// loads
ITypeArray[ 0b0000011 ] = InstructionType_I;
// imm ALU op 64 bit.
ITypeArray[ 0b0010011 ] = InstructionType_I;
// AUIPC
ITypeArray[ 0b0010111 ] = InstructionType_U;
// imm ALU op 32 bit.
ITypeArray[ 0b0011011 ] = InstructionType_I;
// stores
ITypeArray[ 0b0100011 ] = InstructionType_S;
// reg ALU op 64 bit.
ITypeArray[ 0b0110011 ] = InstructionType_R;
// LUI
ITypeArray[ 0b0110111 ] = InstructionType_U;
// reg ALU op 32 bit.
ITypeArray[ 0b0111011 ] = InstructionType_R;
// branch
ITypeArray[ 0b1100011 ] = InstructionType_SB;
// jalr
ITypeArray[ 0b1100111 ] = InstructionType_I;
// jal
ITypeArray[ 0b1101111 ] = InstructionType_UJ;
// environment, CSR
ITypeArray[ 0b1110011 ] = InstructionType_I;
}
HwError GetLastHwError()
{
return LastHwError;
}
InstructionType GetType( uint32_t instruction )
{
LastHwError = HwError_NoError;
uint8_t OpCode = instruction & 0b01111111;
return ITypeArray[OpCode];
}
ParsedInstruction ParseInstruction( uint32_t instruction )
{
static void* DispatchTable[NumInstructionTypes] =
{
&&NullInstruction, &&RInstruction, &&IInstruction, &&SInstruction, &&SBInstruction, &&UInstruction, &&UJInstruction
};
LastHwError = HwError_NoError;
ParsedInstruction result;
result.Type = GetType( instruction );
uint8_t jumpOffset = (uint8_t) result.Type;
if( jumpOffset < NumInstructionTypes )
goto *DispatchTable[jumpOffset];
else
goto BadInstruction;
NullInstruction:
{
result.ParsedInst.Null = ParseInstructionNull( instruction );
return result;
};
RInstruction:
{
result.ParsedInst.R = ParseInstructionR( instruction );
return result;
};
IInstruction:
{
result.ParsedInst.I = ParseInstructionI( instruction );
return result;
};
SInstruction:
{
result.ParsedInst.S = ParseInstructionS( instruction );
return result;
};
SBInstruction:
{
result.ParsedInst.SB = ParseInstructionSB( instruction );
return result;
};
UInstruction:
{
result.ParsedInst.U = ParseInstructionU( instruction );
return result;
};
UJInstruction:
{
result.ParsedInst.UJ = ParseInstructionUJ( instruction );
return result;
};
BadInstruction:
{
result.ParsedInst.Null = ParseInstructionNull( instruction );
LastHwError = HwError_UnknownInstructionType;
return result;
};
}
};
};
#endif