Skip to content

Commit

Permalink
[#5] Refine evaluation of flags for ALU SBC instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Oct 31, 2020
1 parent 51b2524 commit 21f65bb
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -2410,8 +2410,8 @@ class i8080_executor : public internals::executor_base<B> {
return (res >> (8 - base::cf_bit)) & cf_mask; }
fast_u8 hf1(fast_u8 op1, fast_u8 op2, fast_u8 cfv) {
return ((op1 & 0xf) + (op2 & 0xf) + cfv) & hf_mask; }
fast_u8 hf2(fast_u8 op1, fast_u8 op2) {
return ((op1 & 0xf) - (op2 & 0xf) + hf_mask) & hf_mask; }
fast_u8 hf2(fast_u8 op1, fast_u8 op2, fast_u8 cfv) {
return (hf_mask + (op1 & 0xf) - (op2 & 0xf) - cfv) & hf_mask; }

void do_alu(alu k, fast_u8 n) {
fast_u8 a = self().on_get_a();
Expand All @@ -2431,19 +2431,15 @@ class i8080_executor : public internals::executor_base<B> {
fast_u16 t16 = a - n;
fast_u8 t = mask8(t16);
f = sf(t) | yf(f) | xf(f) | nf(f) |
zf1(t) | hf2(a, n) | pf1(t) | cf1(t16);
zf1(t) | hf2(a, n, 0) | pf1(t) | cf1(t16);
a = t;
break; }
case alu::sbc: {
// TODO: The cf evaluation can be simplified by
// comparing the sum before truncation. +Revisit the
// other cases in this switch.
fast_u8 cfv = (f & cf_mask) ? 1 : 0;
fast_u8 t = mask8(a - n - cfv);
fast_u8 hf = (a & 0xf) >= (n & 0xf) + cfv ? hf_mask : 0;
f = (t & sf_mask) | (f & (yf_mask | xf_mask | nf_mask)) |
zf_ari(t) | hf | pf_log(t) |
cf_ari(t > a || (cfv && n == 0xff));
fast_u8 cfv = cf(f);
fast_u16 t16 = a - n - cfv;
fast_u8 t = mask8(t16);
f = sf(t) | yf(f) | xf(f) | nf(f) |
zf1(t) | hf2(a, n, cfv) | pf1(t) | cf1(t16);
a = t;
break; }
case alu::and_a: {
Expand Down

0 comments on commit 21f65bb

Please sign in to comment.