From f96788127b3f5f996660f34118c3aea034aa70c8 Mon Sep 17 00:00:00 2001 From: Ivan Kosarev Date: Sat, 31 Oct 2020 17:55:34 +0200 Subject: [PATCH] [#5] Refine evaluation of flags for ALU SUB and CP instructions. --- z80.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/z80.h b/z80.h index 48ca5dd..46e6252 100644 --- a/z80.h +++ b/z80.h @@ -2410,6 +2410,8 @@ class i8080_executor : public internals::executor_base { 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 > 0xf ? hf_mask : 0; } + fast_u8 hf2(fast_u8 op1, fast_u8 op2) { + return (op1 & 0xf) >= (op2 & 0xf) ? hf_mask : 0; } void do_alu(alu k, fast_u8 n) { fast_u8 a = self().on_get_a(); @@ -2426,10 +2428,10 @@ class i8080_executor : public internals::executor_base { break; } case alu::sub: case alu::cp: { - fast_u8 t = sub8(a, n); - fast_u8 hf = (a & 0xf) >= (n & 0xf) ? 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); + 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); a = t; break; } case alu::sbc: {