Repository navigation
Fix SBC HL half-carry with carry-in - #218
Merged
Merged
Conversation
Add Z80 core regressions for 12-bit operand overflow and 16-bit RHS wrap. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validation
x80:
sbc hl,rpsets the wrong H (half-carry) flag when carry-in is setSummary
z80_op_sub_16insrc/dcc_debug_host/x80.cxxcomputes the H flag incorrectlyfor
SBC HL,rrwhen the carry flag is set on entry. The emulated flag differsfrom real Z80 behaviour for operand values whose low 12 bits are
0xFFF.Affected code
z80_op_sub_16( uint16_t lhs, uint16_t rhs, bool borrow ), used bySBC HL,rr(ED 42/52/62/72).
The carry-in is folded into
rhsbefore the 12-bit mask. When(rhs & 0xfff) == 0xfff, the increment carries out of the 12-bit field and themasked value becomes
0, so the comparison no longer sees the borrow. Whenrhs == 0xFFFF, the 16-bit increment wraps to0for the same reason.Expected behaviour
H is set when there is a borrow from bit 12:
(rhs & 0xFFF) + carry_in > (lhs & 0xFFF).Reproduction
Also reproduced with
de = 0FFFFhand carry set. The error was found bythe arithmetic soak vectors in Z80ROMlessSBC (
math_soak_z80_test.py), whichcompare flags against a reference after
sbc hl,rr.Fix
Add the carry after masking instead of before:
One-line change;
rhsis no longer modified. Verified against the soakvectors (all flags/registers) after the change.
Notes
The same file is vendored in Z80ROMlessSBC (
src/cpm/z80_debug_host/x80.cxx)and must stay byte-identical with this copy; both carry the same fix.