Emulator: Add support for negative numbers to DAA opcode
This commit is contained in:
+29
-20
@@ -527,35 +527,44 @@ void CPU::daa()
|
|||||||
// bits) of a byte (a byte, since it is 8 bits has two nibbles) to store
|
// bits) of a byte (a byte, since it is 8 bits has two nibbles) to store
|
||||||
// a decimal digit.
|
// a decimal digit.
|
||||||
// Example: 64 split on each number gives a 6 and a 4, so 0110 0100 as a BCD
|
// Example: 64 split on each number gives a 6 and a 4, so 0110 0100 as a BCD
|
||||||
uint8_t higher_nibble = m_a & 0xf0;
|
|
||||||
uint8_t lower_nibble = m_a & 0xf;
|
|
||||||
|
|
||||||
// Step 1: Check lower nibble of the BCD stored in A.
|
// Step 1: Check upper nibble of the BCD stored in A.
|
||||||
// If it is greater than decimal 9 or half carry flag is set
|
|
||||||
// (meaning that the lower nibble value is > 15),
|
|
||||||
// add decimal 6 to the lower nibble to make it wrap around
|
|
||||||
if (lower_nibble > 9 || m_hf) {
|
|
||||||
lower_nibble += 6;
|
|
||||||
higher_nibble += isCarry(m_a, 6, 0x10);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2: Check higher nibble (after addition of the carry from the lower nibble).
|
|
||||||
// If it is greater than decimal 9 or the carry flag is set
|
// If it is greater than decimal 9 or the carry flag is set
|
||||||
// (meaning that the upper nibble value is > 15),
|
// (meaning that the upper nibble value is > 15),
|
||||||
// add decimal 6 to the upper nibble to make it wrap around and set carry flag to 1
|
// add decimal 6 to the upper nibble to make it wrap around and set carry flag to 1
|
||||||
uint32_t new_carry = 0;
|
|
||||||
if (higher_nibble > 9 || m_cf) {
|
// Step 2: Check lower nibble of the BCD stored in A.
|
||||||
higher_nibble += 6;
|
// If it is greater than decimal 9 or half carry flag is set
|
||||||
new_carry = 1;
|
// (meaning that the lower nibble value is > 15),
|
||||||
|
// add decimal 6 to the lower nibble to make it wrap around
|
||||||
|
|
||||||
|
if (!m_nf) {
|
||||||
|
if (m_cf || m_a > 0x99) {
|
||||||
|
m_a += 0x60;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_hf || (m_a & 0xf) > 0x9) {
|
||||||
|
m_a += 0x6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (m_cf) {
|
||||||
|
m_a -= 0x60;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_hf) {
|
||||||
|
m_a -= 0x6;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Accumulator to the correct BCD representation
|
// Carry flag
|
||||||
m_a = higher_nibble | lower_nibble;
|
m_cf = (m_a & 0x100) == 0x100;
|
||||||
|
|
||||||
|
m_a = m_a & 0xff;
|
||||||
|
|
||||||
// Set flags
|
// Set flags
|
||||||
m_zf = m_a == 0;
|
m_zf = (m_a == 0);
|
||||||
m_hf = 0;
|
m_hf = 0;
|
||||||
m_cf = new_carry;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user