Skip to content

Commit

Permalink
Fix GroupMutex and Clock Rescaling
Browse files Browse the repository at this point in the history
This commit mainly fixes GroupMutex and clock rescaling. In addition, clock rescaling is no longer performed if the CNTFRQ_EL0 of the host device is same as that of the Switch (19.2MHz) which is fairly common on higher end devices.
  • Loading branch information
PixelyIon committed Feb 15, 2020
1 parent 003e9c5 commit 66d20a9
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ captures/
.vscode/

# Discord plugin for IntelliJ IDEA
.idea\discord.xml
.idea/discord.xml
4 changes: 2 additions & 2 deletions .idea/scopes/SkylineCPP.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
</h1>

<p align="center">
<i>Skyline is an experimental emulator that runs on ARMv8 Android™ devices and emulates the functionality of a Nintendo Switch™ system. Skyline currently does not run any games, nor Homebrew. It's licensed under GPLv3, refer to the <a href="https://github.com/skyline-emu/skyline/blob/master/LICENSE">license file</a> for more information.</i><br/><br>
<i>Skyline is an experimental emulator that runs on ARMv8 Android™ devices and emulates the functionality of a Nintendo Switch™ system. It's licensed under GPLv3, refer to the <a href="https://github.com/skyline-emu/skyline/blob/master/LICENSE">license file</a> for more information.</i><br/><br>
</p>

### Contact
> You can contact the core developers of Skyline at our [Discord](https://discord.gg/XnbXNQM). If you have any questions, feel free to ask.
### Credit
[<img align="left" height="10%" width="10%" src="https://i.imgur.com/aOADoDM.png"/>](https://ryujinx.org/)
[**Ryujinx**](https://ryujinx.org/)<br>
We've used Ryujinx throughout the project for reference, the amount of accuracy of their HLE kernel implementation is what makes them such an amazing reference.
<br>
<br>
### Disclaimer
* Nintendo Switch is a trademark of Nintendo Co., Ltd.
* Android is a trademark of Google LLC.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
<activity
android:name="emu.skyline.GameActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="landscape">
android:screenOrientation="landscape"
android:launchMode="singleInstance">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="emu.skyline.MainActivity" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ extern "C" JNIEXPORT void Java_emu_skyline_GameActivity_executeRom(JNIEnv *env,
logger->Info("Launching ROM {}", romString);
env->ReleaseStringUTFChars(romJstring, romString);
os.Execute(romFd, static_cast<skyline::TitleFormat>(romType));
logger->Info("Emulation has ended");
} catch (std::exception &e) {
logger->Error(e.what());
} catch (...) {
logger->Error("An unknown exception has occurred");
}
logger->Info("Emulation has ended");

auto end = std::chrono::steady_clock::now();
logger->Info("Done in: {} ms", (std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()));
Expand Down
40 changes: 27 additions & 13 deletions app/src/main/cpp/skyline/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,37 @@ namespace skyline {

void GroupMutex::lock(Group group) {
auto none = Group::None;
constexpr u64 timeout = 1000; // The timeout in ns
auto start = utils::GetTimeNs();
while (next != group && !next.compare_exchange_weak(none, group)) {
if (flag == group && ((utils::GetTimeNs() - start) > timeout)) {
num++;
return;
}
constexpr u64 timeout = 100; // The timeout in ns
auto end = utils::GetTimeNs() + timeout;
while (true) {
if (next == group) {
if (flag == group) {
std::lock_guard lock(mtx);
if (flag == group) {
auto groupT = group;
next.compare_exchange_strong(groupT, Group::None);
num++;
return;
}
} else
flag.compare_exchange_weak(none, group);
} else if (flag == group && (next == Group::None || utils::GetTimeNs() >= end)) {
std::lock_guard lock(mtx);
if (flag == group) {
num++;
return;
}
} else
next.compare_exchange_weak(none, group);
none = Group::None;
asm volatile("yield");
}
while (flag != group && !flag.compare_exchange_weak(none, group))
asm volatile("yield");
num++;
}

void GroupMutex::unlock() {
std::lock_guard lock(mtx);
if (!--num)
flag.exchange(Group::None);
flag.exchange(next);
}

Settings::Settings(const int preferenceFd) {
Expand Down Expand Up @@ -118,6 +132,6 @@ namespace skyline {
gpu = std::move(std::make_shared<gpu::GPU>(*this));
}

thread_local std::shared_ptr<kernel::type::KThread> DeviceState::thread = 0;
thread_local ThreadContext *DeviceState::ctx = 0;
thread_local std::shared_ptr<kernel::type::KThread> DeviceState::thread = nullptr;
thread_local ThreadContext *DeviceState::ctx = nullptr;
}
16 changes: 8 additions & 8 deletions app/src/main/cpp/skyline/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace skyline {
constexpr u8 NumRegs = 30; //!< The amount of registers that ARMv8 has
constexpr u32 TpidrroEl0 = 0x5E83; //!< ID of TPIDRRO_EL0 in MRS
constexpr u32 CntfrqEl0 = 0x5F00; //!< ID of CNTFRQ_EL0 in MRS
constexpr u32 TegraX1Freq = 0x124F800; //!< The clock frequency of the Tegra X1 (19.2 MHz)
constexpr u32 TegraX1Freq = 19200000; //!< The clock frequency of the Tegra X1 (19.2 MHz)
constexpr u32 CntpctEl0 = 0x5F01; //!< ID of CNTPCT_EL0 in MRS
constexpr u32 CntvctEl0 = 0x5F02; //!< ID of CNTVCT_EL0 in MRS
// Kernel
Expand Down Expand Up @@ -101,18 +101,17 @@ namespace skyline {
* @return The current time in nanoseconds
*/
inline u64 GetTimeNs() {
static u64 frequencyMs{};
if (!frequencyMs) {
asm("MRS %0, CNTFRQ_EL0" : "=r"(frequencyMs));
frequencyMs *= 1000000000;
}
constexpr uint64_t NsInSecond = 1000000000;
static u64 frequency{};
if (!frequency)
asm("MRS %0, CNTFRQ_EL0" : "=r"(frequency));
u64 ticks;
asm("MRS %0, CNTVCT_EL0" : "=r"(ticks));
return ticks / frequencyMs;
return ((ticks / frequency) * NsInSecond) + (((ticks % frequency) * NsInSecond + (frequency / 2)) / frequency);
}

/**
* @brief Aligns up a value to a multiple of twoB
* @brief Aligns up a value to a multiple of two
* @tparam Type The type of the values
* @param value The value to round up
* @param multiple The multiple to round up to (Should be a multiple of 2)
Expand Down Expand Up @@ -216,6 +215,7 @@ namespace skyline {
std::atomic<Group> flag = Group::None; //!< An atomic flag to hold which group holds the mutex
std::atomic<Group> next = Group::None; //!< An atomic flag to hold which group will hold the mutex next
std::atomic<u8> num = 0; //!< An atomic u8 keeping track of how many users are holding the mutex
Mutex mtx; //!< A mutex to lock before changing of num and flag
};

/**
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/cpp/skyline/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ namespace skyline::gpu {
resolution.width = static_cast<u32>(ANativeWindow_getWidth(window));
resolution.height = static_cast<u32>(ANativeWindow_getHeight(window));
format = ANativeWindow_getFormat(window);
surfaceUpdate = false;
} else if (Surface == nullptr) {
surfaceUpdate = true;
} else
surfaceUpdate = (Surface == nullptr);
return;
}
if (!bufferQueue.displayQueue.empty()) {
auto &buffer = bufferQueue.displayQueue.front();
bufferQueue.displayQueue.pop();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/skyline/gpu/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace skyline::gpu {
break;
}
}
sched_yield();
asm("yield");
}
struct {
u32 slot;
Expand Down
Loading

0 comments on commit 66d20a9

Please sign in to comment.