In the intel 8080 manual the DAA description says to follow these steps:
-
If the value of the least significant 4 bits of the
accumulator is greater than 9 or if the AC flag
is set, 6 is added to the accumulator. -
If the value of the most significant 4 bits of the
accumulator is now greater than 9, or if the CY
flag is set, 6 is added to the most significant 4
bits of the accumulator.
I’m not sure if the second step relies on the changes that happened to the accumulator during the 1st step, here is my try:
void DAA(State8080 *state) {
uint8_t acc_val = state->registers[A];
uint8_t val = 0;
if (((state->registers[A] & 0x0F) > 0x09) || state->cc.ac) {
val += 0x06;
state->registers[A] += 0x06;
}
if (((state->registers[A] >> 4) > 0x09) || state->cc.cy) {
val += 0x60;
state->registers[A] += 0x60;
state->cc.cy = 1;
}
uint8_t res = state->registers[A];
_update_flag_z(state, res);
_update_flag_s(state, res);
_update_flag_p(state, res);
_update_flag_ac_add(state, acc_val, val, false);
}
Wassim Touyar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.