Skip to content

Commit ad9e11f

Browse files
authored
Ensure that post-decrement ops decrement from 0 to 0xFFFF_FFFF. (#196)
1 parent 42677d7 commit ad9e11f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pioemu/emulation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ def _apply_side_effects(
329329
output_shift_register=new_output_shift_register,
330330
)
331331
elif (opcode & 0xE0E0) == 0x0040:
332-
return replace(state, x_register=state.x_register - 1)
332+
return replace(state, x_register=(state.x_register - 1) & 0xFFFF_FFFF)
333333
elif (opcode & 0xE0E0) == 0x0080:
334-
return replace(state, y_register=state.y_register - 1)
334+
return replace(state, y_register=(state.y_register - 1) & 0xFFFF_FFFF)
335335

336336
return state
337337

tests/instructions/test_jmp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def test_jump_when_y_is_non_zero_post_decrement():
8282
assert y_register_series == [0, 3, 2, 1, 0]
8383

8484

85+
def test_jump_x_post_decrement_wraps_from_zero_to_max():
86+
_, new_state = emulate_single_instruction(0x0041, initial_state=State(x_register=0))
87+
assert new_state.x_register == 0xFFFF_FFFF
88+
89+
90+
def test_jump_y_post_decrement_wraps_from_zero_to_max():
91+
_, new_state = emulate_single_instruction(0x0081, initial_state=State(y_register=0))
92+
assert new_state.y_register == 0xFFFF_FFFF
93+
94+
8595
@pytest.mark.parametrize(
8696
"jmp_pin, initial_state, expected_program_counter",
8797
[

0 commit comments

Comments
 (0)