Skip to content

fix: decrement SP by 3 during reset sequence#549

Merged
mattgodbolt merged 1 commit intomattgodbolt:mainfrom
rob-smallshire:fix-sp-reset-547
Jan 31, 2026
Merged

fix: decrement SP by 3 during reset sequence#549
mattgodbolt merged 1 commit intomattgodbolt:mainfrom
rob-smallshire:fix-sp-reset-547

Conversation

@rob-smallshire
Copy link
Contributor

Summary

Fixes #547

The real 6502 reset sequence reuses the interrupt/BRK logic internally, performing 3 "dummy" stack operations where SP decrements but R/W is held in read mode (no actual writes). This results in SP being 0xFD after reset when starting from 0x00.

This PR adds the missing SP decrement to both Cpu6502.reset() and Tube6502.reset() methods.

Changes

  • src/6502.js: Added this.s = (this.s - 3) & 0xff after setting PC and I flag in both reset methods
  • tests/unit/test-sp-reset.js: New test file covering SP reset behavior

Test plan

  • New unit tests pass (5 tests covering NMOS/CMOS, hard/soft reset, wrap-around)
  • Full unit test suite passes (255 tests)
  • Integration tests pass (45 tests)
  • CPU compatibility test suite passes
  • Linting passes

The real 6502 reset sequence reuses the interrupt/BRK logic internally,
performing 3 "dummy" stack operations where SP decrements but R/W is
held in read mode (no actual writes). This results in SP being 0xFD
after reset when starting from 0x00.

Added tests for both Cpu6502 and Tube6502 reset methods, covering:
- Initial SP after power-on reset
- SP decrement on hard and soft reset
- Correct wrap-around behavior near zero

Fixes mattgodbolt#547
Copy link
Owner

@mattgodbolt mattgodbolt left a comment

Choose a reason for hiding this comment

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

Love it! thanks so much, and for the tests too 🎉

@mattgodbolt mattgodbolt merged commit a08f9ff into mattgodbolt:main Jan 31, 2026
1 check passed
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.

SP not decremented during reset sequence (should be 0xFD, not 0x00)

2 participants