Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TSC for QueryPerformance* API #695

Merged
merged 2 commits into from
Jan 25, 2025
Merged

Use TSC for QueryPerformance* API #695

merged 2 commits into from
Jan 25, 2025

Conversation

thrimbor
Copy link
Member

The first of these two commits changes our implementation of QueryPerformanceCounter and QueryPerformanceFrequency to use the TSC (like later XDKs did) instead of the kernel's less accurate internal counter.

The second commit improves compatibility with non-stock Xbox systems by using a constructor to compute the TSC frequency on startup instead of using a hard-coded value.
It uses a lookup-table of known CPU clock multipliers and FSB divider and multiplier values from the northbridge to derive this frequency from the crystal frequency. This is done with 64 bit integer math and fractions of a Hz to not introduce any errors during the calculations.
The only way this could be wrong should be using a non-stock crystal frequency.

Using this code in xemu requires xemu-project/xemu#1551 so any reasonably recent version should do.
The code is almost unchanged from my old branch that wasn't upstreamed yet (https://github.com/thrimbor/nxdk/tree/tsc).

Supersedes #663

// Chapter 2.22 of https://www.intel.com/content/dam/develop/external/us/en/documents/335592-sdm-vol-4.pdf
const uint64_t multiplier_pattern = (poweron_reg >> 22) & 0b101111;

// https://web.archive.org/web/20040407043044/http://www.intel.com:80/design/mobile/datashts/29834006.pdf
Copy link
Contributor

Choose a reason for hiding this comment

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

image

Neither document mentioned in this PR has information on >10X multipliers, this is something I brought up (and wasn't answered) in xemu-project/xemu/pull/1551, I also attempted to search Linux and they too seem not to mention support for >10X. Where are you getting these numbers?

Copy link
Member Author

Choose a reason for hiding this comment

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

From the CPUs, specifically SL6BY, SL6C6 and SL6C7.

@mborgerson
Copy link
Member

The only way this could be wrong should be using a non-stock crystal frequency.

We can measure the tsc freq with a reference clock

@mborgerson mborgerson merged commit ea9da5a into XboxDev:master Jan 25, 2025
6 checks passed
@thrimbor thrimbor deleted the tsc2 branch January 25, 2025 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants