From 61f494e080ef3f329d2d324019c7790c272fd3ae Mon Sep 17 00:00:00 2001 From: Gabriel Correia Date: Tue, 23 Jan 2024 14:45:55 -0300 Subject: [PATCH] `Project`: Adds the latest fixes and updates the app to version 0.0.18 --- README.md | 4 +- app/build.gradle.kts | 4 +- app/src/main/AndroidManifest.xml | 2 + app/src/main/cpp/cosmic/common/except.cpp | 9 ++--- app/src/main/cpp/cosmic/common/except.h | 10 ++--- app/src/main/cpp/cosmic/vm/watch.cpp | 14 +++---- app/src/main/cpp/emu_user.cpp | 7 ++-- app/src/main/cpp/jvm_comm.cpp | 21 ++++++++++ .../main/java/emu/cosmic/EmulationActivity.kt | 38 +++++++++++++------ app/src/main/res/layout/about_dialog.xml | 8 +++- app/src/main/res/values/strings.xml | 6 ++- 11 files changed, 84 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 0f2a6f9c..e58980d1 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Cosmic Project ### Project Progress: -- Progression: ```13%``` -- Current top priority: Add more instructions to the respective EE, IOP, and VUs interpreters +- Progression: ```14%``` +- Current top priority: Add more instructions to the respective `EE`, `IOP`, and `VUs` interpreters - Target for the first demo release: 2025 ### Special thanks diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c138e906..49345ade 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -14,8 +14,8 @@ android { minSdk = 31 targetSdk = 34 - versionCode = 17 - versionName = "0.0.17" + versionCode = 18 + versionName = "0.0.18" ndk { abiFilters.clear() abiFilters.add("arm64-v8a") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f1d3484b..7232050d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + diff --git a/app/src/main/cpp/cosmic/common/except.cpp b/app/src/main/cpp/cosmic/common/except.cpp index 8c0f8f56..ebfc65b0 100644 --- a/app/src/main/cpp/cosmic/common/except.cpp +++ b/app/src/main/cpp/cosmic/common/except.cpp @@ -5,14 +5,14 @@ namespace cosmic { jclass exceptionActivity{}; CosmicException::CosmicException(const std::string& format) : std::runtime_error(format) { - user->error("{}", format); + user->error("An exception of type CosmicException was raised due to: {}", format); msg = cosmicEnv->NewStringUTF(format.c_str()); title = lookupByActivity(); alertUser(); } jstring CosmicException::lookupByActivity() { - const jclass emulation{cosmicEnv->FindClass("emu/cosmic/EmulationActivity$Companion")}; + const jclass emulation{cosmicEnv->FindClass("emu/cosmic/EmulationActivity")}; if (cosmicEnv->IsSameObject(exceptionActivity, emulation)) { return cosmicEnv->NewStringUTF("Emulation Scene"); } @@ -20,9 +20,8 @@ namespace cosmic { } void CosmicException::alertUser() { - alert = cosmicEnv->GetMethodID(exceptionActivity, + alert = cosmicEnv->GetStaticMethodID(exceptionActivity, "displayAlert", "(Ljava/lang/String;Ljava/lang/String;)V"); - if (alert) { cosmicEnv->CallStaticVoidMethod(exceptionActivity, alert, title, msg); } @@ -33,7 +32,7 @@ namespace cosmic { void CosmicException::setExceptionClass(jobject super) { const jclass emuClass{cosmicEnv->FindClass("emu/cosmic/EmulationActivity")}; if (cosmicEnv->IsInstanceOf(super, emuClass)) { - exceptionActivity = cosmicEnv->FindClass("emu/cosmic/EmulationActivity$Companion"); + exceptionActivity = cosmicEnv->FindClass("emu/cosmic/EmulationActivity"); } } } \ No newline at end of file diff --git a/app/src/main/cpp/cosmic/common/except.h b/app/src/main/cpp/cosmic/common/except.h index 5508326a..936a9c09 100644 --- a/app/src/main/cpp/cosmic/common/except.h +++ b/app/src/main/cpp/cosmic/common/except.h @@ -30,11 +30,11 @@ namespace cosmic { } DECLARE_EXCEPTION_TYPE(Cop0Fail, "Cop0"); - DECLARE_EXCEPTION_TYPE(TimerFail, "Timer"); - DECLARE_EXCEPTION_TYPE(MioFail, "Mio"); - DECLARE_EXCEPTION_TYPE(IoFail, "Io"); - DECLARE_EXCEPTION_TYPE(FsFail, "Fs"); - DECLARE_EXCEPTION_TYPE(GpuFail, "Gpu"); + DECLARE_EXCEPTION_TYPE(TimerFail, "EE.Timer"); + DECLARE_EXCEPTION_TYPE(MioFail, "MIO"); + DECLARE_EXCEPTION_TYPE(IoFail, "IO"); + DECLARE_EXCEPTION_TYPE(FsFail, "FS"); + DECLARE_EXCEPTION_TYPE(GpuFail, "GPU"); DECLARE_EXCEPTION_TYPE(AppFail, "Cosmic"); #undef DECLARE_EXCEPTION_TYPE diff --git a/app/src/main/cpp/cosmic/vm/watch.cpp b/app/src/main/cpp/cosmic/vm/watch.cpp index 373b82fe..f2553da0 100644 --- a/app/src/main/cpp/cosmic/vm/watch.cpp +++ b/app/src/main/cpp/cosmic/vm/watch.cpp @@ -37,22 +37,22 @@ namespace cosmic::vm { void WatchStatus::markStepsDone() { auto currentTime{std::chrono::high_resolution_clock::now()}; - auto elapsedMicro{ - std::chrono::duration_cast + auto elapsedMilli{ + std::chrono::duration_cast (currentTime - starts)}; std::array milestone{ - elapsedMicro.count() >= std::chrono::microseconds(1).count(), - isFrameCompleted + elapsedMilli.count() >= std::chrono::milliseconds(1).count(), + isFrameCompleted }; milestone[2] = milestone[0] || milestone[1]; executionCount++; - std::chrono::microseconds final; + std::chrono::milliseconds final; fmt::memory_buffer elapsed{}; if (milestone[1]) { - final = std::chrono::duration_cast(finish - starts); + final = std::chrono::duration_cast(finish - starts); fmt::format_to(std::back_inserter(elapsed), - "Complete frame within {}µs...\n", final.count()); + "Complete frame within {}ms...\n", final.count()); isFrameCompleted = false; } if (milestone[2]) { diff --git a/app/src/main/cpp/emu_user.cpp b/app/src/main/cpp/emu_user.cpp index cc7afa3e..96b8ba09 100644 --- a/app/src/main/cpp/emu_user.cpp +++ b/app/src/main/cpp/emu_user.cpp @@ -11,7 +11,7 @@ Java_emu_cosmic_EmulationActivity_swtSurfaceContext(JNIEnv* env, jobject thiz, j std::atomic is{false}; extern "C" JNIEXPORT void JNICALL -Java_emu_cosmic_EmulationActivity_runEmulatorVm(JNIEnv* env, [[maybe_unused]] jobject thiz) { +Java_emu_cosmic_EmulationActivity_startEmulator(JNIEnv* env, [[maybe_unused]] jobject thiz) { cosmic::CosmicException::setExceptionClass(thiz); cosmic::app->vm->resetVm(); @@ -24,6 +24,7 @@ Java_emu_cosmic_EmulationActivity_runEmulatorVm(JNIEnv* env, [[maybe_unused]] jo } extern "C" JNIEXPORT void JNICALL -Java_emu_cosmic_EmulationActivity_stopEmulatorVm(JNIEnv* env, jobject thiz) { - is = 0; +Java_emu_cosmic_EmulationActivity_stopEmulator(JNIEnv* env, jobject thiz) { + if (is != 0) + is = 0; } \ No newline at end of file diff --git a/app/src/main/cpp/jvm_comm.cpp b/app/src/main/cpp/jvm_comm.cpp index 0a26e864..b7843f29 100644 --- a/app/src/main/cpp/jvm_comm.cpp +++ b/app/src/main/cpp/jvm_comm.cpp @@ -1,8 +1,25 @@ #include +#include #include #include #include +static std::array signals; +void catchSystemSignals(cosmic::i32 sig, siginfo_t* ino, void* context) { + cosmic::u8 sid{}; + if (sig == SIGABRT || sig == SIGTRAP) { + if (sig == SIGTRAP) + sid = 1; + } + if (sig == SIGSEGV) { + sid = 2; + } + sigaction(sig, &signals[sid], nullptr); +} +static struct sigaction trap { + .sa_flags = SA_SIGINFO, + .sa_sigaction = catchSystemSignals, +}; // JNI_OnLoad function is called when the JVM has loaded our native code in the heap, this process // is started by Java Runtime using System.loadLibrary("cosmic") @@ -11,6 +28,10 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { cosmic::user = std::make_shared(); cosmic::device = std::make_unique(vm); + sigaction(SIGABRT, &trap, &signals[0]); + sigaction(SIGTRAP, &trap, &signals[1]); + sigaction(SIGSEGV, &trap, &signals[2]); + cosmic::app = std::make_shared(); return JNI_VERSION_1_6; } diff --git a/app/src/main/java/emu/cosmic/EmulationActivity.kt b/app/src/main/java/emu/cosmic/EmulationActivity.kt index 2c7b0545..61a69e8f 100644 --- a/app/src/main/java/emu/cosmic/EmulationActivity.kt +++ b/app/src/main/java/emu/cosmic/EmulationActivity.kt @@ -9,10 +9,14 @@ import android.view.SurfaceHolder import android.view.WindowInsets import androidx.activity.OnBackPressedCallback import androidx.activity.viewModels -import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import com.google.android.material.dialog.MaterialAlertDialogBuilder import emu.cosmic.databinding.EmulationActivityBinding import emu.cosmic.models.EmulationModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.withContext class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback { private var emuSurface: Surface? = null @@ -23,7 +27,7 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback { private val emuThread = Thread { status.checkRunning(true) - runEmulatorVm() + startEmulator() goBackToMain() } init { @@ -35,14 +39,26 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback { @Suppress("unused") @JvmStatic fun displayAlert(title: String, msg: String) { - AlertDialog.Builder(activity.applicationContext) - .setTitle(title) - .setMessage(msg) - .setPositiveButton(activity.getString(R.string.positive)) { _, _ -> - activity.goBackToMain() + val quit = Semaphore(1, 1) + activity.stopEmulator() + + activity.runOnUiThread { + MaterialAlertDialogBuilder(activity) + .setTitle(title) + .setMessage(msg) + .setCancelable(false) + .setPositiveButton(activity.getString(R.string.positive)) { _, _ -> + quit.release() }.show() + } + runBlocking { + withContext(Dispatchers.Default) { + quit.acquire() + activity.fps = 0 + activity.goBackToMain() + } + } - activity.fps = 0 } } @@ -73,7 +89,7 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback { private fun goBackToMain() { if (!status.isRunning()) return - stopEmulatorVm() + stopEmulator() runOnUiThread { val main = Intent(applicationContext, MainActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) startActivity(main) @@ -83,8 +99,8 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback { emuThread.join() } private external fun swtSurfaceContext(surface: Surface? = null) - private external fun runEmulatorVm() - private external fun stopEmulatorVm() + private external fun startEmulator() + private external fun stopEmulator() override fun surfaceCreated(holder: SurfaceHolder) { swtSurfaceContext(holder.surface) emuSurface = holder.surface diff --git a/app/src/main/res/layout/about_dialog.xml b/app/src/main/res/layout/about_dialog.xml index f9e7d9a8..f1d60e22 100644 --- a/app/src/main/res/layout/about_dialog.xml +++ b/app/src/main/res/layout/about_dialog.xml @@ -24,14 +24,18 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" + android:padding="8dp" - android:text="@string/about_details" /> - + android:text="@string/about_details" + android:textAlignment="center" + android:justificationMode="inter_word" + /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 908cd474..11aadf0f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,8 +1,10 @@ Cosmic - Ok + Continue - God be praised + Thank you to those who installed and tried to use the alpha\ + version of Cosmic. We do not have anything here just yet! Follow the official repository\ + for more details and project updates! Commit version About