diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
index d315ac74..bb3ada16 100644
--- a/.idea/codeStyles/Project.xml
+++ b/.idea/codeStyles/Project.xml
@@ -8,9 +8,40 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -34,9 +65,18 @@
+
+
+
+
+
+
+
+
+
-
+
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index 35ee3ce2..22d9867e 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -1,6 +1,941 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index f8df4406..2dace671 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -9,7 +9,7 @@
-
+
diff --git a/.idea/scopes/SkylineCPP.xml b/.idea/scopes/SkylineCPP.xml
new file mode 100644
index 00000000..179a6bef
--- /dev/null
+++ b/.idea/scopes/SkylineCPP.xml
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 5d61195a..edc831d5 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.8)
-project(Lightswitch VERSION 1 LANGUAGES CXX)
+project(Skyline VERSION 1 LANGUAGES CXX)
set(BUILD_TESTING OFF)
set(CMAKE_CXX_STANDARD 17)
@@ -13,19 +13,24 @@ set(source_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp)
include_directories(${source_DIR})
-add_library(lightswitch SHARED
- ${source_DIR}/lightswitch.cpp
- ${source_DIR}/switch/common.cpp
- ${source_DIR}/switch/nce.cpp
- ${source_DIR}/switch/os.cpp
- ${source_DIR}/switch/loader/nro.cpp
- ${source_DIR}/switch/kernel/ipc.cpp
- ${source_DIR}/switch/kernel/svc.cpp
- ${source_DIR}/switch/kernel/service.cpp
- ${source_DIR}/switch/kernel/types/KProcess.cpp
- ${source_DIR}/switch/kernel/types/KThread.cpp
- ${source_DIR}/switch/kernel/types/KSharedMemory.cpp
- ${source_DIR}/switch/kernel/types/KPrivateMemory.cpp
+add_library(skyline SHARED
+ ${source_DIR}/main.cpp
+ ${source_DIR}/skyline/common.cpp
+ ${source_DIR}/skyline/nce.cpp
+ ${source_DIR}/skyline/os.cpp
+ ${source_DIR}/skyline/loader/nro.cpp
+ ${source_DIR}/skyline/kernel/ipc.cpp
+ ${source_DIR}/skyline/kernel/svc.cpp
+ ${source_DIR}/skyline/kernel/types/KSyncObject.cpp
+ ${source_DIR}/skyline/kernel/types/KProcess.cpp
+ ${source_DIR}/skyline/kernel/types/KThread.cpp
+ ${source_DIR}/skyline/kernel/types/KSharedMemory.cpp
+ ${source_DIR}/skyline/kernel/types/KPrivateMemory.cpp
+ ${source_DIR}/skyline/kernel/services/serviceman.cpp
+ ${source_DIR}/skyline/kernel/services/sm/sm.cpp
+ ${source_DIR}/skyline/kernel/services/set/sys.cpp
+ ${source_DIR}/skyline/kernel/services/apm/apm.cpp
+ ${source_DIR}/skyline/kernel/services/am/appletOE.cpp
)
-target_link_libraries(lightswitch fmt tinyxml2 android)
-target_compile_options(lightswitch PRIVATE -Wno-c++17-extensions)
+target_link_libraries(skyline fmt tinyxml2)
+target_compile_options(skyline PRIVATE -Wno-c++17-extensions)
diff --git a/app/build.gradle b/app/build.gradle
index 8970de6f..08b3565c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -2,9 +2,9 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 29
- buildToolsVersion "29.0.0"
+ buildToolsVersion '29.0.2'
defaultConfig {
- applicationId "lightswitch.emu"
+ applicationId "skyline.emu"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
@@ -32,13 +32,17 @@ android {
jni.srcDirs = ['src/main/cpp/unicorn/lib']
}
}
+ compileOptions {
+ sourceCompatibility = 1.8
+ targetCompatibility = 1.8
+ }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'androidx.appcompat:appcompat:1.0.2'
+ implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
- implementation 'androidx.preference:preference:1.1.0-rc01'
+ implementation 'androidx.preference:preference:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'me.xdrop:fuzzywuzzy:1.2.0'
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 4e046992..63625854 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,7 +1,7 @@
+ package="emu.skyline">
@@ -9,29 +9,29 @@
+ tools:ignore="GoogleAppIndexingWarning"
+ android:fullBackupContent="@xml/backup_descriptor">
+ android:parentActivityName="emu.skyline.MainActivity">
+ android:value="emu.skyline.MainActivity" />
+ android:parentActivityName="emu.skyline.MainActivity">
+ android:value="emu.skyline.MainActivity" />
-
+
diff --git a/app/src/main/cpp/lightswitch.cpp b/app/src/main/cpp/lightswitch.cpp
deleted file mode 100644
index 7a56e7d2..00000000
--- a/app/src/main/cpp/lightswitch.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#include
-#include
-#include
-#include
-#include
-#include "switch/common.h"
-#include "switch/os.h"
-
-std::thread *emu_thread;
-bool halt = false;
-
-void thread_main(std::string rom_path, std::string pref_path, std::string log_path) {
- auto log = std::make_shared(log_path);
- auto settings = std::make_shared(pref_path);
- try {
- lightSwitch::kernel::OS os(log, settings);
- log->Write(lightSwitch::Logger::INFO, "Launching ROM {}", rom_path);
- os.Execute(rom_path);
- log->Write(lightSwitch::Logger::INFO, "Emulation has ended");
- } catch (std::exception &e) {
- log->Write(lightSwitch::Logger::ERROR, e.what());
- } catch (...) {
- log->Write(lightSwitch::Logger::ERROR, "An unknown exception has occurred");
- }
-}
-
-extern "C" JNIEXPORT void JNICALL Java_emu_lightswitch_MainActivity_loadFile(JNIEnv *env, jobject instance, jstring rom_path_, jstring pref_path_, jstring log_path_) {
- const char *rom_path = env->GetStringUTFChars(rom_path_, nullptr);
- const char *pref_path = env->GetStringUTFChars(pref_path_, nullptr);
- const char *log_path = env->GetStringUTFChars(log_path_, nullptr);
-
- if (emu_thread) {
- halt = true; // This'll cause execution to stop after the next breakpoint
- emu_thread->join();
- halt = false; // Or the current instance will halt immediately
- }
-
- // Running on UI thread is not a good idea as the UI will remain unresponsive
- emu_thread = new std::thread(thread_main, std::string(rom_path, strlen(rom_path)), std::string(pref_path, strlen(pref_path)), std::string(log_path, strlen(log_path)));
-
- env->ReleaseStringUTFChars(rom_path_, rom_path);
- env->ReleaseStringUTFChars(pref_path_, pref_path);
- env->ReleaseStringUTFChars(log_path_, log_path);
-}
diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp
new file mode 100644
index 00000000..f422a20e
--- /dev/null
+++ b/app/src/main/cpp/main.cpp
@@ -0,0 +1,45 @@
+#include
+#include
+#include
+#include
+#include
+#include "skyline/common.h"
+#include "skyline/os.h"
+
+std::thread *EmuThread;
+bool Halt = false;
+
+void ThreadMain(const std::string romPath, const std::string prefPath, const std::string logPath) {
+ auto log = std::make_shared(logPath);
+ auto settings = std::make_shared(prefPath);
+ settings->List(log);
+ try {
+ skyline::kernel::OS os(log, settings);
+ log->Write(skyline::Logger::Info, "Launching ROM {}", romPath);
+ os.Execute(romPath);
+ log->Write(skyline::Logger::Info, "Emulation has ended");
+ } catch (std::exception &e) {
+ log->Write(skyline::Logger::Error, e.what());
+ } catch (...) {
+ log->Write(skyline::Logger::Error, "An unknown exception has occurred");
+ }
+}
+
+extern "C" JNIEXPORT void JNICALL Java_emu_skyline_MainActivity_loadFile(JNIEnv *env, jobject instance, jstring romPathJni, jstring prefPathJni, jstring logPathJni) {
+ const char *romPath = env->GetStringUTFChars(romPathJni, nullptr);
+ const char *prefPath = env->GetStringUTFChars(prefPathJni, nullptr);
+ const char *logPath = env->GetStringUTFChars(logPathJni, nullptr);
+
+ if (EmuThread) {
+ Halt = true; // This'll cause execution to stop after the next breakpoint
+ EmuThread->join();
+ Halt = false; // Or the current instance will halt immediately
+ }
+
+ // Running on UI thread is not a good idea as the UI will remain unresponsive
+ EmuThread = new std::thread(ThreadMain, std::string(romPath, strlen(romPath)), std::string(prefPath, strlen(prefPath)), std::string(logPath, strlen(logPath)));
+
+ env->ReleaseStringUTFChars(romPathJni, romPath);
+ env->ReleaseStringUTFChars(prefPathJni, prefPath);
+ env->ReleaseStringUTFChars(logPathJni, logPath);
+}
diff --git a/app/src/main/cpp/skyline/common.cpp b/app/src/main/cpp/skyline/common.cpp
new file mode 100644
index 00000000..e1632065
--- /dev/null
+++ b/app/src/main/cpp/skyline/common.cpp
@@ -0,0 +1,67 @@
+#include "common.h"
+#include
+#include
+
+namespace skyline {
+ Settings::Settings(const std::string &prefXml) {
+ tinyxml2::XMLDocument pref;
+ if (pref.LoadFile(prefXml.c_str()))
+ throw exception("TinyXML2 Error: " + std::string(pref.ErrorStr()));
+ tinyxml2::XMLElement *elem = pref.LastChild()->FirstChild()->ToElement();
+ while (elem) {
+ switch (elem->Value()[0]) {
+ case 's':
+ stringMap.insert(
+ std::pair(elem->FindAttribute("name")->Value(), elem->GetText()));
+ break;
+ case 'b':
+ boolMap.insert(std::pair(elem->FindAttribute("name")->Value(), elem->FindAttribute("value")->BoolValue()));
+ default:
+ break;
+ };
+ if (elem->NextSibling())
+ elem = elem->NextSibling()->ToElement();
+ else break;
+ }
+ pref.Clear();
+ }
+
+ std::string Settings::GetString(const std::string& key) {
+ return stringMap.at(key);
+ }
+
+ bool Settings::GetBool(const std::string& key) {
+ return boolMap.at(key);
+ }
+
+ void Settings::List(std::shared_ptr& logger) {
+ for (auto& iter : stringMap)
+ logger->Write(Logger::Info, "Key: {}, Value: {}, Type: String", iter.first, GetString(iter.first));
+ for (auto& iter : boolMap)
+ logger->Write(Logger::Info, "Key: {}, Value: {}, Type: Bool", iter.first, GetBool(iter.first));
+ }
+
+ Logger::Logger(const std::string &logPath) {
+ logFile.open(logPath, std::ios::app);
+ WriteHeader("Logging started");
+ }
+
+ Logger::~Logger() {
+ WriteHeader("Logging ended");
+ }
+
+ void Logger::WriteHeader(const std::string &str) {
+ syslog(LOG_ALERT, "%s", str.c_str());
+ logFile << "0|" << str << "\n";
+ logFile.flush();
+ }
+
+ void Logger::Write(const LogLevel level, const std::string &str) {
+ #ifdef NDEBUG
+ if (level == DEBUG) return;
+ #endif
+ syslog(levelSyslog[level], "%s", str.c_str());
+ logFile << "1|" << levelStr[level] << "|" << str << "\n";
+ logFile.flush();
+ }
+}
diff --git a/app/src/main/cpp/skyline/common.h b/app/src/main/cpp/skyline/common.h
new file mode 100644
index 00000000..bb8ad277
--- /dev/null
+++ b/app/src/main/cpp/skyline/common.h
@@ -0,0 +1,260 @@
+#pragma once
+
+#include