-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops_0xC.cpp
178 lines (158 loc) · 3.95 KB
/
ops_0xC.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
#include "hw/cpu.hpp"
#include "modrm.hpp"
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
uint32_t result;
uint8_t carry;
} ShiftRes32;
typedef struct
{
uint16_t result;
uint8_t carry;
} ShiftRes16;
void mov_rm32_imm32(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint32_t value = cpu->bus->read32(cpu->getLinearAddr());
cpu->ip.regs_32 += 4;
set_rm32(cpu, &modrm, value);
}
void shr_rm32_imm8(Pentium* cpu, ModRM* modrm)
{
uint8_t imm8_val = cpu->bus->read(cpu->getLinearAddr());
while (imm8_val > 32)
{
imm8_val -= 32;
}
cpu->ip.regs_32++;
uint64_t ext_val = (uint64_t)get_rm32(cpu, modrm);
uint64_t ext_carry = (uint64_t)cpu->eflags & CARRY_FLAG;
uint64_t cf_added = (ext_val << 1) | ext_carry;
uint64_t result = cf_added >> imm8_val;
ShiftRes32 res;
res.result = (uint32_t)(result >> 1);
res.carry = (result & 1) != 0;
set_rm32(cpu, modrm, res.result);
if (res.carry)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
}
void code_c1(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
switch (modrm.opcode)
{
case 5:
shr_rm32_imm8(cpu, &modrm);
break;
default:
printf("Not implemented: Op: C1 with ModR/M Op: %d\n", modrm.opcode);
exit(-1);
}
}
void shr_rm16_imm8(Pentium* cpu, ModRM* modrm)
{
uint8_t imm8_val = cpu->bus->read(cpu->getLinearAddr());
while (imm8_val > 16)
{
imm8_val -= 16;
}
cpu->ip.regs_32++;
uint64_t ext_val = (uint32_t)get_rm16(cpu, modrm);
uint64_t ext_carry = (uint32_t)cpu->eflags & CARRY_FLAG;
uint64_t cf_added = (ext_val << 1) | ext_carry;
uint64_t result = cf_added >> imm8_val;
ShiftRes16 res;
res.result = (uint32_t)(result >> 1);
res.carry = (result & 1) != 0;
set_rm16(cpu, modrm, res.result);
if (res.carry)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
}
void shl_rm16_imm8(Pentium* cpu, ModRM* modrm)
{
uint8_t imm8_val = cpu->bus->read(cpu->getLinearAddr());
while (imm8_val > 16)
{
imm8_val -= 16;
}
cpu->ip.regs_32++;
uint32_t ext_val = (uint32_t)get_rm16(cpu, modrm);
uint32_t result = ext_val << imm8_val;
ShiftRes16 res;
res.result = (uint16_t)(result);
res.carry = (result & 0x100000000) != 0;
set_rm16(cpu, modrm, res.result);
if (res.carry)
{
cpu->eflags |= CARRY_FLAG;
}
else
{
cpu->eflags &= ~CARRY_FLAG;
}
}
void code_c1_16(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
switch (modrm.opcode)
{
case 4:
shl_rm16_imm8(cpu, &modrm);
break;
case 5:
shr_rm16_imm8(cpu, &modrm);
break;
default:
printf("Not implemented: Op: 66 C1 with ModR/M Op: %d\n", modrm.opcode);
exit(-1);
}
}
void ret(Pentium* cpu)
{
cpu->ip.regs_32 = cpu->pop32();
printf("RET 0x%x\n (EIP 0x%x)\n", cpu->getLinearAddr(), cpu->ip.regs_32);
}
void load_seg_r32(Pentium* cpu, int seg_index, ModRM* modrm)
{
uint32_t address = calc_memory_address(cpu, modrm);
uint16_t seg_val = cpu->bus->read16(cpu->seg_to_linear(SGRegister::DS, address));
uint32_t offset = cpu->bus->read32(cpu->seg_to_linear(SGRegister::DS, address + 2));
cpu->sgregs[seg_index].base = seg_val;
cpu->gpregs[modrm->reg_index].regs_32 = offset;
}
void les(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
load_seg_r32(cpu, (int)SGRegister::ES, &modrm);
}
void mov_rm8_imm8(Pentium* cpu)
{
cpu->ip.regs_32++;
ModRM modrm = create_modrm();
parse_modrm(&modrm, cpu);
uint8_t value = cpu->bus->read(cpu->getLinearAddr());
cpu->ip.regs_32++;
set_rm8(cpu, &modrm, value);
}