Skip to content

sh4: end the current timeslice on Stop()#2381

Merged
flyinghead merged 1 commit into
flyinghead:devfrom
Immersion95:swapfix2
Jun 18, 2026
Merged

sh4: end the current timeslice on Stop()#2381
flyinghead merged 1 commit into
flyinghead:devfrom
Immersion95:swapfix2

Conversation

@Immersion95

Copy link
Copy Markdown
Contributor

This PR supersedes #2309 with a cheaper implementation of the same fix.

The previous PR fixed the issue by making rec-x64 observe CpuRunning between translated blocks, but that added work in the hottest part of the x64 dynarec.

This version takes a lower-cost approach instead:

  • Stop() now also ends the current SH4 timeslice immediately
  • rec-x64 exits before UpdateSystem_INTC() if CpuRunning is already false

In practice, this keeps the same useful behavior in the affected cases: execution stops before the following render starts, which restores the expected delayed frame swap behavior in single-threaded Win64 GL/DX11 and also fixes the PenPen loading screen cut.

So compared to #2309, the main benefit is that this avoids a per-block CpuRunning check in the hottest rec-x64 path while preserving the fix.

Fixes #1615
Fixes #2308

@flyinghead

flyinghead commented Jun 17, 2026

Copy link
Copy Markdown
Owner

This is much better. A few comments:

  • Please target the dev branch instead of master (this will also fix the failing CI jobs)
  • You can put the CpuRunning test after all cycle_counter processing has been done, so you won't need to reload rax:
  		test(ecx, ecx);
		jg(slice_loop);

		add(ecx, SH4_TIMESLICE);
		mov(dword[rax], ecx);

+		mov(rax, (size_t)&sh4ctx.CpuRunning);
+		mov(edx, dword[rax]);
+		test(edx, edx);
+		je(end_run_loop);

		call(UpdateSystem_INTC);
		jmp(run_loop);
  • Note that after UpdateSystem_INTC returns, it jumps to run_loop, and starts by testing CpuRunning. So I wonder if the newly added CpuRunning test is really needed. I don't think calling UpdateSystem_INTC before stopping would be a problem but I haven't tested it.

@Immersion95 Immersion95 changed the base branch from master to dev June 17, 2026 17:30
When Stop() is requested, also clear the SH4 cycle counter so the current
timeslice ends as soon as possible.

This prevents execution from continuing past a requested stop point.
@Immersion95

Copy link
Copy Markdown
Contributor Author

You are totally right, that's an even simpler fix :).

I tested that simpler fix based on your comment, and the extra CpuRunning check in rec-x64 is not needed. Just clearing cycle_counter in Stop() is enough to fix the affected cases on my side.

I've simplified the PR to only change Stop(), and I’ve retargeted it to dev :)

@flyinghead

Copy link
Copy Markdown
Owner

Nice

@flyinghead flyinghead merged commit bcbc43f into flyinghead:dev Jun 18, 2026
19 checks 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

2 participants