-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
309 lines (260 loc) · 9.01 KB
/
Copy pathmain.cpp
File metadata and controls
309 lines (260 loc) · 9.01 KB
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
304
305
306
307
308
309
/* ========================================================================
(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
======================================================================== */
#include <stdio.h>
#include "sim86_shared.h"
#include <fstream>
#include <cassert>
unsigned char ExampleDisassembly[1024 * 1024];
#define REGISTERS_COUNT 15
#define REGISTERS_VARIATIONS 3
#define FLAGS_REGISTER_INDEX 14
static s32 Registers[REGISTERS_COUNT] =
{
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000
};
static int32_t memory[1024 * 1024];
static int32_t ip;
static int16_t flags;
static char const* Names[REGISTERS_COUNT] =
{
"",
"ax",
"bx",
"cx",
"dx",
"sp",
"bp",
"si",
"di",
"es",
"cs",
"ss",
"ds",
"ip",
"flags"
};
#define ZERO_FLAG_SHIFT 6
#define SIGN_FLAG_SHIFT 7
struct DiffPrinter {
s32 RegistersSnapshot[REGISTERS_COUNT];
int16_t flagsSnapshot;
int32_t ipsnapshot;
DiffPrinter()
{
memcpy(&RegistersSnapshot, Registers, sizeof(Registers));
flagsSnapshot = flags;
ipsnapshot = ip;
}
void CompareFlags(int shift, const char* name) {
auto newValue = (flags >> shift) & 1;
auto oldValue = (flagsSnapshot >> shift) & 1;
if (newValue != oldValue) {
printf(" %s->%i", name, newValue);
}
}
~DiffPrinter()
{
for (size_t registerIndex = 0; registerIndex < REGISTERS_COUNT; registerIndex++)
{
if (RegistersSnapshot[registerIndex] != Registers[registerIndex])
{
printf("%s: 0x%02x -> 0x%02x", Names[registerIndex], RegistersSnapshot[registerIndex], Registers[registerIndex]);
}
}
if (flagsSnapshot != flags) {
printf(" flags:");
CompareFlags(ZERO_FLAG_SHIFT, "zero");
CompareFlags(SIGN_FLAG_SHIFT, "sign");
}
if (ipsnapshot != ip) {
printf(" ip: 0x%02x -> 0x%02x (%i->%i)", ipsnapshot, ip, ipsnapshot, ip);
}
}
};
void setFlag(int shift, bool value) {
if (value) {
flags |= (1 << shift);
}
else {
flags &= (~(1 << shift));
}
}
bool getFlag(int shift) {
return (flags >> shift) & 1;
}
s32 getDisplacement(instruction_operand& operand) {
s32 displacement = 0;
auto firstRegister = operand.Address.Terms[0];
if (firstRegister.Register.Index != 0) {
displacement += Registers[firstRegister.Register.Index];
}
auto secondRegister = operand.Address.Terms[1];
if (secondRegister.Register.Index != 0) {
displacement += Registers[secondRegister.Register.Index];
}
displacement += operand.Address.Displacement;
return displacement;
}
int main(void)
{
u32 Version = Sim86_GetVersion();
printf("Sim86 Version: %u (expected %u)\n", Version, SIM86_VERSION);
if (Version != SIM86_VERSION)
{
printf("ERROR: Header file version doesn't match DLL.\n");
return -1;
}
instruction_table Table;
Sim86_Get8086InstructionTable(&Table);
printf("8086 Instruction Instruction Encoding Count: %u\n", Table.EncodingCount);
FILE* File = {};
size_t readCount = 0;
if (fopen_s(&File, "resources/listing_0052_memory_add_loop", "rb") == 0)
{
readCount = fread(ExampleDisassembly, 1, sizeof(ExampleDisassembly), File);
fclose(File);
}
ip = 0;
while (ip < readCount)
{
DiffPrinter printer{};
instruction Decoded;
Sim86_Decode8086Instruction(sizeof(ExampleDisassembly) - ip, ExampleDisassembly + ip, &Decoded);
if (Decoded.Op)
{
ip += Decoded.Size;
switch (Decoded.Op)
{
case Op_mov:
{
auto destinationOperand = Decoded.Operands[0];
auto sourceOperand = Decoded.Operands[1];
bool word = true;
int16_t sourceValue = 0;
int32_t* destination = nullptr;
if (destinationOperand.Type == operand_type::Operand_Register) {
destination = &Registers[destinationOperand.Register.Index];
}
else if (destinationOperand.Type == operand_type::Operand_Memory) {
destination = &memory[getDisplacement(destinationOperand)];
}
else {
assert(false);
}
if (sourceOperand.Type == operand_type::Operand_Immediate) {
sourceValue = sourceOperand.Immediate.Value;
}
else if (sourceOperand.Type == operand_type::Operand_Register) {
sourceValue = Registers[sourceOperand.Register.Index];
}
else if (sourceOperand.Type == operand_type::Operand_Memory) {
sourceValue = memory[getDisplacement(sourceOperand)];
}
else {
assert(false);
}
*destination = sourceValue;
break;
}
case Op_sub:
{
auto destination = Decoded.Operands[0];
auto source = Decoded.Operands[1];
if (destination.Type == operand_type::Operand_Register) {
s32* destinationRegister = &Registers[destination.Register.Index];
int16_t sourceValue = 0;
if (source.Type == operand_type::Operand_Immediate) {
sourceValue = source.Immediate.Value;
}
else if (source.Type == operand_type::Operand_Register) {
sourceValue = Registers[source.Register.Index];
}
*destinationRegister = ((int16_t)*destinationRegister) - sourceValue;
setFlag(ZERO_FLAG_SHIFT, *destinationRegister == 0);
setFlag(SIGN_FLAG_SHIFT, *destinationRegister < 0);
}
break;
}
case Op_cmp: {
auto destination = Decoded.Operands[0];
auto source = Decoded.Operands[1];
if (destination.Type == operand_type::Operand_Register) {
s32* destinationRegister = &Registers[destination.Register.Index];
int16_t sourceValue = 0;
if (source.Type == operand_type::Operand_Immediate) {
sourceValue = source.Immediate.Value;
}
else if (source.Type == operand_type::Operand_Register) {
sourceValue = Registers[source.Register.Index];
}
auto value = ((int16_t)*destinationRegister) - sourceValue;
setFlag(ZERO_FLAG_SHIFT, value == 0);
setFlag(SIGN_FLAG_SHIFT, value < 0);
}
break;
}
case Op_add: {
auto destination = Decoded.Operands[0];
auto source = Decoded.Operands[1];
if (destination.Type == operand_type::Operand_Register) {
s32* destinationRegister = &Registers[destination.Register.Index];
int16_t sourceValue = 0;
if (source.Type == operand_type::Operand_Immediate) {
sourceValue = source.Immediate.Value;
}
else if (source.Type == operand_type::Operand_Register) {
sourceValue = Registers[source.Register.Index];
}
*destinationRegister = ((int16_t)*destinationRegister) + sourceValue;
setFlag(ZERO_FLAG_SHIFT, *destinationRegister == 0);
setFlag(SIGN_FLAG_SHIFT, *destinationRegister < 0);
}
break;
}
case Op_jne: {
if (!getFlag(ZERO_FLAG_SHIFT))
ip += Decoded.Operands[0].Immediate.Value;
break;
}
default:
printf("Unhandled instruction %i", Decoded.Op);
break;
}
printf("\n");
}
else
{
printf("Unrecognized instruction\n");
break;
}
}
printf("\n");
printf("Final state\n");
for (size_t registerIndex = 0; registerIndex < REGISTERS_COUNT; registerIndex++)
{
if (Registers[registerIndex] == 0)
continue;
printf("%s: 0x%02x\n", Names[registerIndex], Registers[registerIndex]);
}
printf("IP: %i\n", ip);
return 0;
}