Skip to content

Comments

Fix: Correct cycle counts for branch taken and branch page crossing#130

Open
schmelze wants to merge 2 commits intomre:masterfrom
schmelze:fix/branch-cycle-timing
Open

Fix: Correct cycle counts for branch taken and branch page crossing#130
schmelze wants to merge 2 commits intomre:masterfrom
schmelze:fix/branch-cycle-timing

Conversation

@schmelze
Copy link

@schmelze schmelze commented Feb 8, 2026

This PR fixes an issue where CPU cycles were not being incremented correctly during conditional branch instructions. According to the MOS 6502 specification:

  • Branch taken: +1 cycle
  • Branch taken & crosses page boundary: +1 cycle

This patch refactors all of the branch_if_* functions to call a new branch() function. This function centralizes the check to calculate page crossings and adjust cycle count as needed. This does remove the const qualifier on all branch functions, as they now mutate the cycle count.

A new test has been added to check this count: test_branch_cycle_timing()

Copy link
Owner

@mre mre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. This all sounds reasonable to me. @omarandlorraine, any thoughts before merging?

@mre
Copy link
Owner

mre commented Feb 8, 2026

Oh, and can you run cargo fmt to please the linter?

@omarandlorraine
Copy link
Collaborator

@omarandlorraine, any thoughts before merging?

Don't think so; it's definitely a positive change and feels DRYer than what we had as well

@schmelze
Copy link
Author

Ran fmt and updated PR

@mre
Copy link
Owner

mre commented Feb 17, 2026

Thanks. Can you fix the lint error as well? You can test locally with make lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants