Skip to content

Commit

Permalink
clarify and test cycle counting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
docmarionum1 committed Dec 30, 2021
1 parent b611215 commit 38bcf57
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
19 changes: 12 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ Example Usage:::
c.step()

# You can check the registers and memory values to determine what has changed
print c.r.a # A register
print c.r.x # X register
print c.r.y # Y register
print c.r.s # Stack Pointer
print c.r.pc # Program Counter
print(c.r.a) # A register
print(c.r.x) # X register
print(c.r.y) # Y register
print(c.r.s) # Stack Pointer
print(c.r.pc) # Program Counter

print c.r.getFlag('C') # Get the value of a flag from the flag register.
print(c.cc) # Print the number of cycles that passed during the last step.
# This number resets for each call to `.step()`

print mmu.read(0xff) # Read a value from memory
print(c.r.getFlag('C')) # Get the value of a flag from the flag register.

print(mmu.read(0xff)) # Read a value from memory


The full set of parameters for CPU is::

Expand Down
2 changes: 1 addition & 1 deletion py65emu/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self, mmu=None, pc=None, stack_page=0x1, magic=0xee):
"""
self.mmu = mmu
self.r = Registers()
# Hold the number of CPU cycles used during the last call to `self.step()`
self.cc = 0
# Which page the stack is in. 0x1 means that the stack is from
# 0x100-0x1ff. In the 6502 this is always true but it's different
Expand All @@ -95,7 +96,6 @@ def reset(self):

def step(self):
self.cc = 0
# pc = self.r.pc
opcode = self.nextByte()
self.ops[opcode]()

Expand Down
18 changes: 18 additions & 0 deletions tests/test_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,24 @@ def test_run_rom(self):
c.step()
self.assertEqual(c.r.a, 0x55)

def test_cycle_counting(self):
# Adapted from @InvalidCo's test in #7
c = self._cpu(romInit=[
0xA0, 0x80,
0xB9, 0x00, 0x00,
0xB9, 0x80, 0x00,
0x18,
0xB0, 0xFF,
0x90, 0x01,
0x90, 0x8F
])

expected_cycles = [2, 4, 5, 2, 2, 3, 4]

for expected_cycle in expected_cycles:
c.step()
self.assertEqual(c.cc, expected_cycle)

def tearDown(self):
pass

Expand Down

0 comments on commit 38bcf57

Please sign in to comment.