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
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* ========================================================================
(C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Please see https://computerenhance.com for more information
======================================================================== */
struct decode_context
{
u32 DefaultSegment;
u32 AdditionalFlags;
};
static instruction_operand GetRegOperand(u32 IntelRegIndex, b32 Wide)
{
// NOTE(casey): This maps Intel's REG and RM field encodings for registers to our encoding for registers.
register_access RegTable[][2] =
{
{{Register_a, 0, 1}, {Register_a, 0, 2}},
{{Register_c, 0, 1}, {Register_c, 0, 2}},
{{Register_d, 0, 1}, {Register_d, 0, 2}},
{{Register_b, 0, 1}, {Register_b, 0, 2}},
{{Register_a, 1, 1}, {Register_sp, 0, 2}},
{{Register_c, 1, 1}, {Register_bp, 0, 2}},
{{Register_d, 1, 1}, {Register_si, 0, 2}},
{{Register_b, 1, 1}, {Register_di, 0, 2}},
};
instruction_operand Result = {};
Result.Type = Operand_Register;
Result.Register = RegTable[IntelRegIndex & 0x7][(Wide != 0)];
return Result;
}
// NOTE(casey): ParseDataValue is not a real function, it's basically just a macro that is used in
// TryParse. It should never be called otherwise, but that is not something you can do in C++.
// In other languages it would be a "local function".
static u32 ParseDataValue(segmented_access *Access, b32 Exists, b32 Wide, b32 SignExtended)
{
u32 Result = {};
if(Exists)
{
if(Wide)
{
u8 D0 = *AccessMemory(*Access, 0);
u8 D1 = *AccessMemory(*Access, 1);
Result = (D1 << 8) | D0;
Access->SegmentOffset += 2;
}
else
{
Result = *AccessMemory(*Access);
if(SignExtended)
{
Result = (s32)*(s8 *)&Result;
}
Access->SegmentOffset += 1;
}
}
return Result;
}
static instruction TryDecode(decode_context *Context, instruction_encoding *Inst, segmented_access At)
{
instruction Dest = {};
b32 Has[Bits_Count] = {};
u32 Bits[Bits_Count] = {};
b32 Valid = true;
u64 StartingAddress = GetAbsoluteAddressOf(At);
u8 BitsPendingCount = 0;
u8 BitsPending = 0;
for(u32 BitsIndex = 0; Valid && (BitsIndex < ArrayCount(Inst->Bits)); ++BitsIndex)
{
instruction_bits TestBits = Inst->Bits[BitsIndex];
if(TestBits.Usage == Bits_End)
{
// NOTE(casey): That's the end of the instruction format.
break;
}
u32 ReadBits = TestBits.Value;
if(TestBits.BitCount != 0)
{
if(BitsPendingCount == 0)
{
BitsPendingCount = 8;
BitsPending = *AccessMemory(At);
++At.SegmentOffset;
}
// NOTE(casey): If this assert fires, it means we have an error in our table,
// since there are no 8086 instructions that have bit values straddling a
// byte boundary.
assert(TestBits.BitCount <= BitsPendingCount);
BitsPendingCount -= TestBits.BitCount;
ReadBits = BitsPending;
ReadBits >>= BitsPendingCount;
ReadBits &= ~(0xff << TestBits.BitCount);
}
if(TestBits.Usage == Bits_Literal)
{
// NOTE(casey): This is a "required" sequence
Valid = Valid && (ReadBits == TestBits.Value);
}
else
{
Bits[TestBits.Usage] |= (ReadBits << TestBits.Shift);
Has[TestBits.Usage] = true;
}
}
if(Valid)
{
u32 Mod = Bits[Bits_MOD];
u32 RM = Bits[Bits_RM];
u32 W = Bits[Bits_W];
b32 S = Bits[Bits_S];
b32 D = Bits[Bits_D];
b32 HasDirectAddress = ((Mod == 0b00) && (RM == 0b110));
Has[Bits_Disp] = ((Has[Bits_Disp]) || (Mod == 0b10) || (Mod == 0b01) || HasDirectAddress);
b32 DisplacementIsW = ((Bits[Bits_DispAlwaysW]) || (Mod == 0b10) || HasDirectAddress);
b32 DataIsW = ((Bits[Bits_WMakesDataW]) && !S && W);
Bits[Bits_Disp] |= ParseDataValue(&At, Has[Bits_Disp], DisplacementIsW, (!DisplacementIsW));
Bits[Bits_Data] |= ParseDataValue(&At, Has[Bits_Data], DataIsW, S);
Dest.Op = Inst->Op;
Dest.Flags = Context->AdditionalFlags;
Dest.Address = StartingAddress;
Dest.Size = GetAbsoluteAddressOf(At) - StartingAddress;
Dest.SegmentOverride = Context->DefaultSegment;
if(W)
{
Dest.Flags |= Inst_Wide;
}
if(Bits[Bits_Far])
{
Dest.Flags |= Inst_Far;
}
if(Bits[Bits_Z])
{
Dest.Flags |= Inst_RepNE;
}
u32 Disp = Bits[Bits_Disp];
s16 Displacement = (s16)Disp;
instruction_operand *RegOperand = &Dest.Operands[D ? 0 : 1];
instruction_operand *ModOperand = &Dest.Operands[D ? 1 : 0];
if(Has[Bits_SR])
{
*RegOperand = RegisterOperand(Register_es + (Bits[Bits_SR] & 0x3), 2);
}
if(Has[Bits_REG])
{
*RegOperand = GetRegOperand(Bits[Bits_REG], W);
}
if(Has[Bits_MOD])
{
if(Mod == 0b11)
{
*ModOperand = GetRegOperand(RM, W || (Bits[Bits_RMRegAlwaysW]));
}
else
{
register_mapping_8086 IntelTerm0[8] = { Register_b, Register_b, Register_bp, Register_bp, Register_si, Register_di, Register_bp, Register_b};
register_mapping_8086 IntelTerm1[8] = {Register_si, Register_di, Register_si, Register_di};
u32 I = RM&0x7;
register_mapping_8086 Term0 = IntelTerm0[I];
register_mapping_8086 Term1 = IntelTerm1[I];
if((Mod == 0b00) && (RM == 0b110))
{
Term0 = {};
Term1 = {};
}
*ModOperand = EffectiveAddressOperand(RegisterAccess(Term0, 0, 2), RegisterAccess(Term1, 0, 2), Displacement);
}
}
if(Has[Bits_Data] && Has[Bits_Disp] && !Has[Bits_MOD])
{
Dest.Operands[0] = IntersegmentAddressOperand(Bits[Bits_Data], Bits[Bits_Disp]);
}
else
{
//
// NOTE(casey): Because there are some strange opcodes that do things like have an immediate as
// a _destination_ ("out", for example), I define immediates and other "additional operands" to
// go in "whatever slot was not used by the reg and mod fields".
//
instruction_operand *LastOperand = &Dest.Operands[0];
if(LastOperand->Type)
{
LastOperand = &Dest.Operands[1];
}
if(Bits[Bits_RelJMPDisp])
{
*LastOperand = ImmediateOperand(Displacement, Immediate_RelativeJumpDisplacement);
}
else if(Has[Bits_Data])
{
*LastOperand = ImmediateOperand(Bits[Bits_Data]);
}
else if(Has[Bits_V])
{
if(Bits[Bits_V])
{
*LastOperand = RegisterOperand(Register_c, 1);
}
else
{
*LastOperand = ImmediateOperand(1);
}
}
}
}
return Dest;
}
static instruction DecodeInstruction(instruction_table Table, segmented_access At)
{
/* TODO(casey): Hmm. It seems like this is a very inefficient way to parse
instructions, isn't it? For every instruction, we check every entry in the
table until we find a match. Is this bad design? Or did the person who wrote
it know what they were doing, and has a plan for how it can be optimized
later? Only time will tell... :) */
decode_context Context = {};
instruction Result = {};
u32 StartingAddress = GetAbsoluteAddressOf(At);
u32 TotalSize = 0;
while(TotalSize < Table.MaxInstructionByteCount)
{
Result = {};
for(u32 Index = 0; Index < Table.EncodingCount; ++Index)
{
instruction_encoding Inst = Table.Encodings[Index];
Result = TryDecode(&Context, &Inst, At);
if(Result.Op)
{
At.SegmentOffset += Result.Size;
TotalSize += Result.Size;
break;
}
}
if(Result.Op == Op_lock)
{
Context.AdditionalFlags |= Inst_Lock;
}
else if(Result.Op == Op_rep)
{
Context.AdditionalFlags |= Inst_Rep | (Result.Flags & Inst_RepNE);
}
else if(Result.Op == Op_segment)
{
Context.AdditionalFlags |= Inst_Segment;
Context.DefaultSegment = Result.Operands[1].Register.Index;
}
else
{
break;
}
}
if(TotalSize <= Table.MaxInstructionByteCount)
{
Result.Address = StartingAddress;
Result.Size = TotalSize;
}
else
{
Result = {};
}
return Result;
}
|