added add_eb_gb and add_ev_gv instructions, fixed some stuff ig
This commit is contained in:
158
emulator.h
158
emulator.h
@@ -41,7 +41,16 @@ bool load_binary(char* filename, size_t addr);
|
||||
ModRM parse_modrm(uint8_t byte);
|
||||
|
||||
uint8_t get_reg_byte(uint8_t code);
|
||||
uint8_t set_reg_byte(uint8_t code, uint8_t value);
|
||||
void set_reg_byte(uint8_t code, uint8_t value);
|
||||
uint16_t get_reg_word(uint8_t code);
|
||||
void set_reg_word(uint8_t code, uint16_t value);
|
||||
|
||||
uint16_t get_displacement(uint8_t size);
|
||||
uint32_t calc_effective_addr(ModRM modrm);
|
||||
|
||||
void update_flags_add(uint8_t b1, uint8_t b2, uint8_t res);
|
||||
void update_flags_add16(uint16_t w1, uint16_t w2, uint16_t res);
|
||||
|
||||
|
||||
uint32_t physical_addr(uint16_t seg, uint16_t offset) {
|
||||
return ((uint32_t) seg << 4) + offset;
|
||||
@@ -82,26 +91,8 @@ ModRM parse_modrm(uint8_t byte) {
|
||||
return modrm;
|
||||
}
|
||||
|
||||
uint8_t get_reg_byte(uint8_t code, uint8_t value) {
|
||||
switch (code)
|
||||
{
|
||||
case 0: AX.low = value; break;
|
||||
case 1: CX.low = value; break;
|
||||
case 2: DX.low = value; break;
|
||||
case 3: BX.low = value; break;
|
||||
case 4: AX.high = value; break;
|
||||
case 5: AX.high = value; break;
|
||||
case 6: AX.high = value; break;
|
||||
case 7: AX.high = value; break;
|
||||
default:
|
||||
std::cerr << "invalid reg code for 8-bit reg";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t set_reg_byte(uint8_t code) {
|
||||
switch (code)
|
||||
{
|
||||
uint8_t get_reg_byte(uint8_t code) {
|
||||
switch (code) {
|
||||
case 0: return AX.low;
|
||||
case 1: return CX.low;
|
||||
case 2: return DX.low;
|
||||
@@ -114,4 +105,129 @@ uint8_t set_reg_byte(uint8_t code) {
|
||||
std::cerr << "invalid reg code for 8-bit reg";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void set_reg_byte(uint8_t code, uint8_t value) {
|
||||
switch (code) {
|
||||
case 0: AX.low = value; break;
|
||||
case 1: CX.low = value; break;
|
||||
case 2: DX.low = value; break;
|
||||
case 3: BX.low = value; break;
|
||||
case 4: AX.high = value; break;
|
||||
case 5: CX.high = value; break;
|
||||
case 6: DX.high = value; break;
|
||||
case 7: BX.high = value; break;
|
||||
default:
|
||||
std::cerr << "invalid reg code for 8-bit reg";
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t get_reg_word(uint8_t code) {
|
||||
switch (code) {
|
||||
case 0: return AX.value;
|
||||
case 1: return CX.value;
|
||||
case 2: return DX.value;
|
||||
case 3: return BX.value;
|
||||
case 4: return SP;
|
||||
case 5: return BP;
|
||||
case 6: return SI;
|
||||
case 7: return DI;
|
||||
default:
|
||||
std::cerr << "invalid reg code for 8-bit reg";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void set_reg_word(uint8_t code, uint16_t value) {
|
||||
switch (code)
|
||||
{
|
||||
case 0: AX.value = value; break;
|
||||
case 1: CX.value = value; break;
|
||||
case 2: DX.value = value; break;
|
||||
case 3: BX.value = value; break;
|
||||
case 4: SP = value; break;
|
||||
case 5: BP = value; break;
|
||||
case 6: SI = value; break;
|
||||
case 7: DI = value; break;
|
||||
|
||||
default:
|
||||
std::cerr << "invalid reg code for 8-bit reg";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t get_displacement(uint8_t size) {
|
||||
uint16_t disp = 0;
|
||||
uint32_t addr = physical_addr(CS, IP);
|
||||
|
||||
if (size == 1) { // 8-bit
|
||||
disp = static_cast<int8_t>(ram[addr]);
|
||||
IP++;
|
||||
} else if (size == 2) {
|
||||
disp = ram[addr] | (ram[addr + 1] << 8);
|
||||
IP += 2;
|
||||
}
|
||||
|
||||
return disp;
|
||||
}
|
||||
|
||||
uint32_t calc_effective_addr(ModRM modrm) {
|
||||
uint16_t base = 0;
|
||||
uint16_t disp = 0;
|
||||
bool has_disp = false;
|
||||
|
||||
switch (modrm.rm) {
|
||||
case 0: base = BX.value + SI; break;
|
||||
case 1: base = BX.value + DI; break;
|
||||
case 2: base = BP + SI; break;
|
||||
case 3: base = BP + DI; break;
|
||||
case 4: base = SI; break;
|
||||
case 5: base = DI; break;
|
||||
case 6:
|
||||
if (modrm.mod == 0) {
|
||||
disp = get_displacement(2);
|
||||
base = 0; // Для прямого адреса
|
||||
} else base = BP;
|
||||
break;
|
||||
case 7: base = BX.value; break;
|
||||
}
|
||||
|
||||
if (modrm.mod == 1) {
|
||||
disp = get_displacement(1);
|
||||
has_disp = true;
|
||||
} else if (modrm.mod == 2) {
|
||||
disp = get_displacement(2);
|
||||
has_disp = true;
|
||||
}
|
||||
|
||||
return base + (has_disp ? disp : 0);
|
||||
}
|
||||
|
||||
void update_flags_add(uint8_t b1, uint8_t b2, uint8_t res) {
|
||||
CF = (static_cast<uint16_t>(b1) + static_cast<uint16_t>(b2)) > 0xFF;
|
||||
ZF = res == 0;
|
||||
SF = (res & 0x80) != 0;
|
||||
|
||||
uint8_t count = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
count += (res >> i) & 1;
|
||||
PF = (count % 2) == 0;
|
||||
|
||||
AF = ((b1 & 0xF) + (b2 & 0xF)) > 0xF;
|
||||
OF = (~(b1 ^ b2) & (b2 ^ res) & 0x80) != 0;
|
||||
}
|
||||
|
||||
void update_flags_add16(uint16_t w1, uint16_t w2, uint16_t res) {
|
||||
CF = (static_cast<uint32_t>(w1) + static_cast<uint32_t>(w2)) > 0xFFFF;
|
||||
ZF = (res == 0);
|
||||
SF = (res & 0x8000) != 0;
|
||||
|
||||
uint8_t low_byte = res & 0xFF;
|
||||
uint8_t count = 0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
count += (low_byte >> i) & 1;
|
||||
PF = (count % 2 == 0);
|
||||
|
||||
AF = ((w1 & 0xF) + (w2 & 0xF)) > 0xF;
|
||||
OF = ((w1 ^ res) & (w2 ^ res) & 0x8000) != 0;
|
||||
}
|
Reference in New Issue
Block a user