Skip to content

CX4 program cache behaviour improvements - #377

Merged
Screwtapello merged 2 commits into
bsnes-emu:masterfrom
AkiteruSDA:cx4-timing
Feb 16, 2026
Merged

Screwtapello merged 2 commits into
bsnes-emu:masterfrom
AkiteruSDA:cx4-timing

Conversation

@AkiteruSDA

Copy link
Copy Markdown
Contributor
  • When triggering a cache preload by writing to $7f48, update the pb register with the prepared program page
  • When preloading a page, ignore all runtime checks and reload the page immediately

I have been looking into CX4 emulation lately, as it historically hasn't gotten a ton of attention compared to other chips and has been known to run quite fast compared to original hardware. By observing how Mega Man X2/X3 sets up the program cache on the CX4, I believe these two changes are consistent with assumptions made by the games on the CX4's caching behaviour.

I have created a comparison video of the Mega Man X2 opening sequence, showing the difference between current master, my commit 6c5f03e, and footage recorded from my SNES and original cart. These changes result in a significant accuracy improvement on the wireframe head section of the opening.

I am certainly open to feedback on these changes as I am not a hardware expert. Just looking to do what I can to improve emulation accuracy for X2/X3. Thanks!

- When triggering a cache preload by writing to $7f48, update the pb register with the prepared program page
- When preloading a page, ignore all runtime checks and reload the page immediately
@Screwtapello

Copy link
Copy Markdown
Contributor

This looks plausible, and your video comparison is convincing. There's a lot of experienced SNES researchers watching this repo, though, I'll give them a day or two to think about it and chime in if they have objections or improvements.

@AkiteruSDA

Copy link
Copy Markdown
Contributor Author

Thank you for the response! I tested this again while turning off fast mode for coprocessors, and that caused this to start running fast. That seems like a strange result, so I want to make sure I fully understand why that might happen. Maybe it's really sensitive to how the chip is clocked? I'll try to look into it more.

@carmiker

Copy link
Copy Markdown
Contributor

@AkiteruSDA, do you mean with your change it speeds up with fast mode for coprocessors turned off? Was it the correct speed before the change with fast coprocessor mode turned off?

@AkiteruSDA

AkiteruSDA commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor Author

It speeds up with fast coprocessor mode turned off both before and after my change. Before my change, both fast coprocessor mode turned on and off were running fast. Here are some frame counts for each combination, timed from the first black frame after the Capcom logo to the first black frame after the Rockman X2 logo fades out:

master, fast coprocessor off: 4380
master, fast coprocessor on:  4397
6c5f03, fast coprocessor off: 4396
6c5f03, fast coprocessor on:  4412 (correct speed)

Edit: The CX4 setup subroutine RMX2 runs every frame at $7E2695 waits in a loop 3 different times for bit 6 of $7F5E, which is the CX4 running flag, to be 0. It waits for the CX4 to stop running, disables NMI and IRQs, starts the program preload, waits again, starts execution by writing to $7F4F, then waits again. Coprocessor fast mode appears to synchronize the CX4 once per scanline rather than once per CPU step. I can see why this would make it appear slower if the CX4 is flagged as running for the rest of an entire scanline before the CPU can continue. But it also makes me think there's a missing piece with regards to how and when the CX4 is no longer flagged as running. I wouldn't expect how often you sync to affect the timing that much, since it runs independently on real hardware anyway.

@Screwtapello

Copy link
Copy Markdown
Contributor

I wonder how accurate the timing of bsnes' CX4 emulation is. It looks like most instructions are assumed to be one cycle, except for a few that add two more, which seems... optimistic?

Making every CX4 operation take at least a scanline might be the kind of bug that cancels out inaccurate timing. I'm not sure if Near ever found a datasheet with timing information for the CX4, it might require additional research.

@carmiker

Copy link
Copy Markdown
Contributor

One more thing of note: changing the coprocessor settings also determines how the "attract mode" with the intro level boss works. I don't have my MMX2 cartridge handy right now to test on hardware, but you will notice that X will jump around erratically and die unless "Fast mode" is on. He also dies with HLE mode on.

- Step 1+WS on both reads for cache, not just one
- Remove extra cycle on bus reads/writes
@AkiteruSDA

AkiteruSDA commented Feb 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I wonder how accurate the timing of bsnes' CX4 emulation is. It looks like most instructions are assumed to be one cycle, except for a few that add two more, which seems... optimistic?

Making every CX4 operation take at least a scanline might be the kind of bug that cancels out inaccurate timing. I'm not sure if Near ever found a datasheet with timing information for the CX4, it might require additional research.

According to ikari's tests, those timings seem to be the case, though I can't test that for myself to confirm. It certainly would be nice to have a datasheet though.

One more thing of note: changing the coprocessor settings also determines how the "attract mode" with the intro level boss works. I don't have my MMX2 cartridge handy right now to test on hardware, but you will notice that X will jump around erratically and die unless "Fast mode" is on. He also dies with HLE mode on.

Yeah, that seems to be a common issue if CX4 timing is off. I know Mesen and previously MiSTer had the same thing. To note, the attract mode boss plays as expected with both fast mode on/off with these changes.

I pushed a couple of changes that I think were inaccurate before. With these changes, the wireframe is much closer to console with fast mode off (comparison). It's still two frames slower, so maybe there's some very minor thing that can be tweaked somewhere else, but it's much closer than it was before with fast mode off.

I fixed what seems like an off-by-one error on each bus read/write instruction. According to ikari's tests, bus reads/writes should be 1+waitstates, but it looks like there was an extra cycle happening in bsnes:

assume WS = 3
LD -> set pending, pending = 1 + WS = 4
step(1) -> decrement pending, pending = 3
INC -> sees pending == 3
step(1) -> decrement pending, pending = 2
WAIT -> step(2), set pending = 0
step(1) -> pending == 0, data read
next instruction

total cycles until next instruction are 5, expected 4

I also added another step(wait(address)); when reading the second byte per loop during cache loading. It seems likely that both of those reads require waiting, as there are no other instances of a 16-bit read happening while only having to wait once.

@Screwtapello
Screwtapello merged commit b730433 into bsnes-emu:master Feb 16, 2026
5 checks passed
@Screwtapello

Copy link
Copy Markdown
Contributor

Looks good to me, thanks!

carmiker added a commit to libretro/bsnes-jg that referenced this pull request Feb 20, 2026
- When triggering a cache preload by writing to $7f48, update the pb
  register with the prepared program page
- When preloading a page, ignore all runtime checks and reload the page
  immediately
- Step 1+WS on both reads for cache, not just one
- Remove extra cycle on bus reads/writes

This work comes from AkiteruDSA:
  bsnes-emu/bsnes#377
@joepogo

joepogo commented Mar 26, 2026

Copy link
Copy Markdown

Is it possible to add this to the retroarch core bsnes v 115 core? 😊

@Morilli

Morilli commented May 24, 2026

Copy link
Copy Markdown
Contributor

Since these changes, this cx4 test rom no longer boots correctly, so something must be wrong or missing here. As far as I'm aware that test rom works correctly on hardware.

@joepogo

joepogo commented May 25, 2026 •

Copy link
Copy Markdown

I just tested this test rom on the current retroarch version of bsnes v115 with the accuracy core and all tests passed. With the less accurate settings only a couple tests pass.

I also tried the bsnesjg retroarch core and it's just a black screen. I'm assuming you're getting the same issue?

@Morilli

Morilli commented May 25, 2026

Copy link
Copy Markdown
Contributor

I also tried the bsnesjg retroarch core and it's just a black screen. I'm assuming you're getting the same issue?

Yes, it's now just a black screen and doesn't display any test results. From my debugging it seems that the cpu is now trying to read from the dsp rom while the caching is in progress, which gets blocked by this line of code (during caching, busy() is true because io.cache.enable is set):

if(hitachidsp.active() || !busy()) {

This could be fixed in numerous ways, for example by adding || io.cache.preload to allow this specific case, but its unclear what the correct behavior is. I don't have hardware to experiment with so I can't verify that the test rom actually does work consistently on hardware or modify the test rom and do tests with that.

@AkiteruSDA

AkiteruSDA commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor Author

That appears to be an issue with the old test ROM where it writes to 7f48 to test the value without taking into account that it triggers a cache load. The test ROM doesn't have any CX4 code to load anyway, so it's an illegal operation. This patched ROM runs on the latest nightly: https://github.com/AkiteruSDA/cx4test/raw/refs/heads/main/cx4test.sfc

Edit: The real issue is that cx4test triggered a cache preload while running from ROM, and the CX4 cuts the SNES off from ROM when it acquires the bus (https://forums.nesdev.org/viewtopic.php?t=14647 "the Cx4 only acquires the bus when it is actually accessing it" and $7f53 bit 7), causing a crash. My updated ROM now retains the $7f48 tests, but writes $7f48 from WRAM and waits for CX4 to go idle before continuing from ROM. This matches MMX2/3's own behaviour when preloading cache.

image

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.

5 participants