From 77f0c777232135af2b27e6140491b40e8f57f8fa Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 14 Dec 2022 21:02:14 +0900 Subject: [PATCH] stopping and restarting the vm --- tsvm_core/src/net/torvald/tsvm/VM.kt | 15 ++++++++++++--- .../src/net/torvald/tsvm/VMEmuExecutable.kt | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/tsvm_core/src/net/torvald/tsvm/VM.kt b/tsvm_core/src/net/torvald/tsvm/VM.kt index 610c3c1..019f31a 100644 --- a/tsvm_core/src/net/torvald/tsvm/VM.kt +++ b/tsvm_core/src/net/torvald/tsvm/VM.kt @@ -79,14 +79,22 @@ class VM( contexts.clear() } + /** + * Makes the VM stop suddenly without disposing of. + */ + fun park() { + killAllContexts() + startTime = -1 + } + fun init() { killAllContexts() - - startTime = System.currentTimeMillis() - + usermem.fillWith(0) mallocMap.clear() mallocSizes.clear() allocatedBlockCount = 0 + + startTime = System.currentTimeMillis() } @@ -115,6 +123,7 @@ class VM( disposed = true } + /** * To check if the VM has started, check if startTime >= 0 */ diff --git a/tsvm_executable/src/net/torvald/tsvm/VMEmuExecutable.kt b/tsvm_executable/src/net/torvald/tsvm/VMEmuExecutable.kt index 04c7cbf..d6aad5d 100644 --- a/tsvm_executable/src/net/torvald/tsvm/VMEmuExecutable.kt +++ b/tsvm_executable/src/net/torvald/tsvm/VMEmuExecutable.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.graphics.* import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.utils.GdxRuntimeException import com.badlogic.gdx.utils.JsonReader import com.badlogic.gdx.utils.JsonValue import com.badlogic.gdx.utils.JsonWriter @@ -192,7 +193,10 @@ class VMEmuExecutable(val windowWidth: Int, val windowHeight: Int, var panelsX: internal fun initVMenv(vm: VM) { vm.init() - vm.peripheralTable.getOrNull(1)?.peripheral?.dispose() + try { + vm.peripheralTable.getOrNull(1)?.peripheral?.dispose() + } + catch (_: GdxRuntimeException) {} val gpu = ReferenceGraphicsAdapter2("./assets", vm) vm.peripheralTable[1] = PeripheralEntry(gpu)//, GraphicsAdapter.VRAM_SIZE, 16, 0) @@ -206,17 +210,18 @@ class VMEmuExecutable(val windowWidth: Int, val windowHeight: Int, var panelsX: } internal fun killVMenv(vm: VM) { - vm.dispose() - vm.peripheralTable.fill(PeripheralEntry()) + vm.park() + + for (i in 1 until vm.peripheralTable.size) { + vm.peripheralTable[i].peripheral?.dispose() + } + vm.getPrintStream = { TODO() } vm.getErrorStream = { TODO() } vm.getInputStream = { TODO() } vmRunners[vm.id]?.close() - coroutineJobs[vm.id]?.cancel() - - // re-create the IOSpace (peripheral index 0) - vm.peripheralTable[0] = PeripheralEntry(IOSpace(vm)) + coroutineJobs[vm.id]?.cancel("VM kill command received") } private fun setCameraPosition(newX: Float, newY: Float) {