-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops_0x8.cpp
275 lines (248 loc) · 5.89 KB
/
ops_0x8.cpp
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
#include "modrm.hpp"
#include "hw/cpu.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string>
static void cmp_rm8_imm8(Pentium* cpu, ModRM* modrm)
{
uint8_t rm8 = get_rm8(cpu, modrm);
uint8_t imm8 = cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
uint16_t result = (uint16_t)rm8 - (uint16_t)imm8;
int sign1 = rm8 >> 31;
int sing2 = imm8 >> 31;
int signr = (result >> 31) & 1;
if (result >> 16)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
if (result == 0)
{
cpu->eflags |= ZERO_FLAG;
}
else
{
cpu->eflags &= ~ZERO_FLAG;
}
}
void code_80(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
switch(modrm.opcode)
{
case 7:
cmp_rm8_imm8(cpu, &modrm);
break;
default:
printf("Not implemented: Op: 80 with ModR/M Op: %d\n", modrm.opcode);
exit(-1);
}
}
/*
* cmp rm32 imm8: 3 bytes
* compares RM32 value and imm8 value by subtracting in order
* Op code 83 and ModR/M op code 111 executes this
* 1 byte: shared op (83)
* 1 byte: ModR/M
* 1 byte: imm8 to subtract
*/
static void cmp_rm32_imm8(Pentium* cpu, ModRM *modrm)
{
uint32_t rm32 = get_rm32(cpu, modrm);
uint32_t imm8 = (int32_t)cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
uint64_t result = (uint64_t)rm32 - (uint64_t)imm8;
int sign1 = rm32 >> 31;
int sing2 = imm8 >> 31;
int signr = (result >> 31) & 1;
printf("CMP 0x%x, 0x%x (0x%x)\n", rm32, imm8, result);
if (result >> 32)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
if (result == 0)
{
cpu->eflags |= ZERO_FLAG;
}
else
{
cpu->eflags &= ~ZERO_FLAG;
}
}
void sub_rm32_imm8(Pentium* cpu, ModRM* modrm)
{
uint32_t rm32 = get_rm32(cpu, modrm);
uint32_t imm8 = (int32_t)cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
uint64_t result = (uint64_t)rm32 - (uint64_t)imm8;
set_rm32(cpu, modrm, result);
int sign1 = rm32 >> 31;
int sing2 = imm8 >> 31;
int signr = (result >> 31) & 1;
if (result >> 32)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
if (result == 0)
{
cpu->eflags |= ZERO_FLAG;
}
else
{
cpu->eflags &= ~ZERO_FLAG;
}
}
static void or_rm32_imm8(Pentium* cpu, ModRM* modrm)
{
uint32_t rm32 = get_rm32(cpu, modrm);
uint32_t imm8 = (int32_t)cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
uint32_t result = rm32 | imm8;
set_rm32(cpu, modrm, result);
}
static void and_rm32_imm8(Pentium* cpu, ModRM* modrm)
{
uint32_t rm32 = get_rm32(cpu, modrm);
uint32_t imm8 = (int32_t)cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
uint32_t result = rm32 & imm8;
set_rm32(cpu, modrm, result);
}
void code_83(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
switch (modrm.opcode)
{
case 1:
or_rm32_imm8(cpu, &modrm);
break;
case 4:
and_rm32_imm8(cpu, &modrm);
break;
case 5:
sub_rm32_imm8(cpu, &modrm);
break;
case 7:
cmp_rm32_imm8(cpu, &modrm);
break;
default:
printf("Not implemented: Op: 83 with ModR/M Op: %d\n", modrm.opcode);
exit(-1);
}
}
void mov_rm8_r8(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint8_t r8;
if (modrm.reg_index < 4)
{
r8 = cpu->gpregs[modrm.reg_index].regs_8l;
}
else
{
r8 = cpu->gpregs[modrm.reg_index].regs_8h;
}
set_rm8(cpu, &modrm, r8);
}
/*
* mov rm32 r32: 2 bytes
* Copies value of register specified by reg to ModR/M
* 1 byte: op (89)
* 1 byte: ModR/M
*/
void mov_rm32_r32(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t r32 = cpu->gpregs[modrm.reg_index].regs_32;
set_rm32(cpu, &modrm, r32);
}
void mov_rm16_r16(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint16_t r16 = cpu->gpregs[modrm.reg_index].regs_16;
set_rm16(cpu, &modrm, r16);
}
void mov_r8_rm8(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t rm8 = get_rm8(cpu, &modrm);
cpu->gpregs[rm8].regs_8h = rm8;
}
void mov_r32_rm32(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t rm32 = get_rm32(cpu, &modrm);
cpu->gpregs[modrm.reg_index].regs_32 = rm32;
}
/*
* mov seg rm32: 2|3 bytes
* Copies rm32 to segment register
* 1 byte: op (8E)
* 1|2 byte ModR/M
*/
void mov_seg_rm32(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t rm32 = get_rm32(cpu, &modrm);
if (modrm.reg_index == 0)
return; // We don't do mov cs
printf("MOV 0x%x, 0x%x\n", rm32, modrm.reg_index);
cpu->sgregs[modrm.reg_index].base = rm32;
}
void mov_rm32_seg(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t seg_val = cpu->sgregs[modrm.reg_index].base;
set_rm32(cpu, &modrm, seg_val);
}
void mov_seg_rm16(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
if (modrm.reg_index == (int)SGRegister::CS)
{
printf("PANIC: INVALID MOV CS\n");
exit(-1);
}
uint16_t rm16 = get_rm16(cpu, &modrm);
cpu->sgregs[modrm.reg_index].base = rm16;
}
void mov_rm16_seg(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t seg_val = cpu->sgregs[modrm.reg_index].base;
set_rm16(cpu, &modrm, seg_val);
}