Repository navigation
CX4 program cache behaviour improvements - #377
Conversation
- 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
|
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. |
|
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. |
|
@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? |
|
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: 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. |
|
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. |
|
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
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.
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: I also added another |
|
Looks good to me, 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 - 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
|
Is it possible to add this to the retroarch core bsnes v 115 core? 😊 |
|
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. |
|
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? |
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, This could be fixed in numerous ways, for example by adding |
|
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.
|

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 commit6c5f03e, 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!