Skip to content

Commit

Permalink
Fix how 'ADC A,*' and 'SBC A,*' affect the half-carry flag
Browse files Browse the repository at this point in the history
  • Loading branch information
skoolkid committed Aug 13, 2024
1 parent c734fd5 commit 058268d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions c/csimulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static void init_ADC() {
ADC[c][a][i][1] = (
(v & 0xA8) // S.5.3.N.
+ (((v & 0xFF) == 0) ? 0x40 : 0) // .Z......
+ (((a & 0x0F) + ((v - a) & 0x0F)) & 0x10) // ...H....
+ ((v ^ a ^ (v - c - a)) & 0x10) // ...H....
+ (((a ^ (v - c - a) ^ 0x80) & (a ^ v) & 0x80) >> 5) // .....P..
+ ((v > 0xFF) ? 0x01 : 0) // .......C
);
Expand Down Expand Up @@ -561,7 +561,7 @@ static void init_SBC() {
SBC[c][a][i][1] = (
(v & 0xA8) // S.5.3...
+ (((v & 0xFF) == 0) ? 0x40 : 0) // .Z......
+ (((a & 0x0F) - ((a - v) & 0x0F)) & 0x10) // ...H....
+ ((v ^ a ^ (a - v - c)) & 0x10) // ...H....
+ (((a ^ (a - v - c)) & (a ^ v) & 0x80) >> 5) // .....P..
+ 0x02 // ......N.
+ ((v < 0) ? 1 : 0) // .......C
Expand Down
6 changes: 3 additions & 3 deletions skoolkit/simtables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Richard Dymond (rjdymond@gmail.com)
# Copyright 2022, 2024 Richard Dymond (rjdymond@gmail.com)
#
# This file is part of SkoolKit.
#
Expand Down Expand Up @@ -27,7 +27,7 @@
v % 256,
(v & 0xA8) # S.5.3.N.
+ (v % 256 == 0) * 0x40 # .Z......
+ (((a % 16) + ((v - a) % 16)) & 0x10) # ...H....
+ ((v ^ a ^ (v - c - a)) & 0x10) # ...H....
+ ((a ^ (v - c - a) ^ 0x80) & (a ^ v) & 0x80) // 32 # .....P..
+ (v > 0xFF) # .......C
) for v in range(a + c, a + c + 256)
Expand Down Expand Up @@ -236,7 +236,7 @@
v % 256,
(v & 0xA8) # S.5.3...
+ (v % 256 == 0) * 0x40 # .Z......
+ (((a % 16) - ((a - v) % 16)) & 0x10) # ...H....
+ ((v ^ a ^ (a - v - c)) & 0x10) # ...H....
+ ((a ^ (a - v - c)) & (a ^ v) & 0x80) // 32 # .....P..
+ 0x02 # ......N.
+ (v < 0) # .......C
Expand Down
1 change: 1 addition & 0 deletions sphinx/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

9.4b1
-----
* Fixed how the 'ADC A,*' and 'SBC A,*' instructions affect the half-carry flag

9.3 (2024-08-10)
----------------
Expand Down
4 changes: 2 additions & 2 deletions tests/slow_test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_add_a_a(self):
self._verify(AFTracer(135), '8a5b656618120683ff1e8b2c91e55315')

def test_adc_a_r(self):
self._verify(AFRTracer(136), '0ae2d98f422d958f8ca77645a3b17aad')
self._verify(AFRTracer(136), '88c87db326d118846416ca8c4a18ee8f')

def test_adc_a_a(self):
self._verify(AFTracer(143), 'af1966e41681c026ad6c9d6e12fc3ed8')
Expand All @@ -472,7 +472,7 @@ def test_sub_a(self):
self._verify(AFTracer(151), 'f83836b3beef2cf62227b74fa2db50d5')

def test_sbc_a_r(self):
self._verify(AFRTracer(152), 'b4cbffcca3bdc458fd705d0b3dbcd3d9')
self._verify(AFRTracer(152), '9fc1dc7e21f660f87fbcc7649e0303b6')

def test_sbc_a_a(self):
self._verify(AFTracer(159), '56af67d5c20e1fe7956207a4059edcc6')
Expand Down

0 comments on commit 058268d

Please sign in to comment.