Skip to content

Commit

Permalink
Improve comments generated for 'CP 0' and subsequent CALL/JP/JR/RET
Browse files Browse the repository at this point in the history
  • Loading branch information
skoolkid committed Jan 7, 2025
1 parent ea86965 commit 51934bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 8 additions & 1 deletion skoolkit/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

DEFAULT_FLAGS = {SF: SIGN, ZF: ZERO, PF: PARITY, CF: NA}
CP_FLAGS = {SF: NA, ZF: COMPARE, PF: OFLOW, CF: COMPARE}
CP0_FLAGS = {SF: NA, ZF: COMPARE, PF: OFLOW, CF: NA}
PARITY_FLAGS = {SF: SIGN, ZF: ZERO, PF: PARITY_REG, CF: NA}
OFLOW_FLAGS = {SF: SIGN, ZF: ZERO, PF: OFLOW, CF: NA}
OFLOW_INC_FLAGS = {SF: SIGN, ZF: ZERO, PF: OFLOW_INC, CF: NA}
Expand Down Expand Up @@ -239,7 +240,7 @@ def __init__(self):
0xFB: (None, 'Enable interrupts', None),
0xFC: (self.call_cc, (SF, 1), None),
0xFD: (self.fd_arg, None, None),
0xFE: (self.byte_arg, CP.format(BYTE), (BYTE.format('{1}'), CP_FLAGS))
0xFE: (self.cp_n, None, None)
}

self.after_DD = {
Expand Down Expand Up @@ -508,6 +509,12 @@ def xor_n(self, address, values):
return f'Flip bits {bits_str} of #REGa'
return 'Flip every bit of #REGa'

def cp_n(self, address, values):
n = BYTE.format(values[1])
if values[1]:
return CP.format(n), (n, CP_FLAGS)
return 'Set the zero flag if #REGa=0, and reset the carry flag', (n, CP0_FLAGS)

def addr_arg(self, template, address, values):
if values[0] in PREFIXES:
addr = values[2] + 256 * values[3]
Expand Down
19 changes: 16 additions & 3 deletions tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
'FA0000': "Jump to #R0 if the sign flag is set (negative)",
'FB': "Enable interrupts",
'FC0000': "CALL #R0 if the sign flag is set (negative)",
'FE00': "Set the zero flag if #REGa=#N(0,2,,1)($), or the carry flag if #REGa<#N(0,2,,1)($)",
'FE00': "Set the zero flag if #REGa=0, and reset the carry flag",
'FF': "CALL #R56",
'CB00': RLC.format('#REGb'),
'CB01': RLC.format('#REGc'),
Expand Down Expand Up @@ -1209,6 +1209,15 @@ def test_xor_n(self):
exp_comment = 'Flip every bit of #REGa'
self.assertEqual(cg.get_comment(Instruction(0x8000, (0xEE, n))), exp_comment)

def test_cp_n(self):
cg = CommentGenerator()
for n in range(256):
if n:
exp_comment = f'Set the zero flag if #REGa=#N({n},2,,1)($), or the carry flag if #REGa<#N({n},2,,1)($)'
else:
exp_comment = 'Set the zero flag if #REGa=0, and reset the carry flag'
self.assertEqual(cg.get_comment(Instruction(0x8000, (0xFE, n))), exp_comment)

class ConditionalCallJumpRetTest(SkoolKitTestCase):
def _inc_dec_opcodes(self, base):
opcodes = []
Expand Down Expand Up @@ -1290,7 +1299,7 @@ def test_or(self):
for op_hex in self._alo_opcodes(0xB0):
self._test_conditionals(cg, op_hex, '#REGa', 'SF_SIGN', 'ZF_ZERO', 'PF_PARITY_REG', 'CF_NA')

def test_cp(self):
def test_cp_r(self):
cg = CommentGenerator()
for op_hex, reg in (
('B8', '#REGb'),
Expand All @@ -1300,7 +1309,6 @@ def test_cp(self):
('BC', '#REGh'),
('BD', '#REGl'),
('BE', 'PEEK #REGhl'),
('FE01', '#N(1,2,,1)($)'),
('DDBC', '#REGixh'),
('DDBD', '#REGixl'),
('DDBE00', 'PEEK (#REGix+#N(0,2,,1)($))'),
Expand All @@ -1310,6 +1318,11 @@ def test_cp(self):
):
self._test_conditionals(cg, op_hex, reg, 'SF_NA', 'ZF_COMPARE', 'PF_OFLOW', 'CF_COMPARE')

def test_cp_n(self):
cg = CommentGenerator()
for n in range(256):
self._test_conditionals(cg, f'FE{n:02X}', f'#N({n},2,,1)($)', 'SF_NA', 'ZF_COMPARE', 'PF_OFLOW', 'CF_COMPARE' if n else 'CF_NA')

def test_rlc(self):
cg = CommentGenerator()
for op_hex, reg in self._sro_opcodes(0x00):
Expand Down

0 comments on commit 51934bb

Please sign in to comment.