From 9aaa7689edeb8ae7a056fab7366d337cee069bb0 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 26 May 2022 20:31:34 +0900 Subject: [PATCH] exception caused by an app that is not handled by TVDOS will now print out stack trace before causing the computer to shut down --- assets/disk0/tvdos/TVDOS.SYS | 15 +++- assets/disk0/tvdos/bin/command.js | 5 +- .../tsvm/peripheral/SerialStdioHost.kt | 89 +++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 tsvm_core/src/net/torvald/tsvm/peripheral/SerialStdioHost.kt diff --git a/assets/disk0/tvdos/TVDOS.SYS b/assets/disk0/tvdos/TVDOS.SYS index d8a34d1..1738f89 100644 --- a/assets/disk0/tvdos/TVDOS.SYS +++ b/assets/disk0/tvdos/TVDOS.SYS @@ -397,7 +397,20 @@ var execApp = (cmdsrc, args) => { `var _appStub=function(exec_args){${injectIntChk(cmdsrc, intchkFunName)}\n};` + `_appStub`); // making 'exec_args' a app-level global - return execAppPrg(args); + try { + execAppPrg(args); + } + catch (e) { +printerrln( +` + \\|/ ____ \\|/ + "@'/ ,. \\'@" + /_| \\__/ |_\\ + \\__U_/ +Kernel panic - ${e.stack}` +) +serial.printerr(e.stack) + } } /////////////////////////////////////////////////////////////////////////////// diff --git a/assets/disk0/tvdos/bin/command.js b/assets/disk0/tvdos/bin/command.js index 35d2f24..649b448 100644 --- a/assets/disk0/tvdos/bin/command.js +++ b/assets/disk0/tvdos/bin/command.js @@ -377,7 +377,10 @@ shell.coreutils = { let contents = filesystem.readAll(CURRENT_DRIVE); // TODO just print out what's there print(contents); - } + }, + /*panic: function(args) { + throw Error("Artificial Kernel Panic Triggered") + }*/ }; shell.coreutils.chdir = shell.coreutils.cd; Object.freeze(shell.coreutils); diff --git a/tsvm_core/src/net/torvald/tsvm/peripheral/SerialStdioHost.kt b/tsvm_core/src/net/torvald/tsvm/peripheral/SerialStdioHost.kt new file mode 100644 index 0000000..9768ab4 --- /dev/null +++ b/tsvm_core/src/net/torvald/tsvm/peripheral/SerialStdioHost.kt @@ -0,0 +1,89 @@ +package net.torvald.tsvm.peripheral + +import net.torvald.tsvm.VM +import java.io.InputStream +import java.io.OutputStream + +/** + * Created by minjaesong on 2022-05-23. + */ +class SerialStdioHost(val hostVM: VM) : BlockTransferInterface(true, true) { + + var otherVM: VM? = null + + override fun attachDevice(device: BlockTransferInterface?) { + if (device !is SerialStdioHost) throw IllegalArgumentException("Other device is not SerialStdioHost: ${device?.javaClass?.canonicalName}") + super.attachDevice(device) + otherVM = device.hostVM + } + + private fun getOthersFirstGPU(): GraphicsAdapter? { + return otherVM!!.findPeribyType(VM.PERITYPE_GPU_AND_TERM)?.peripheral as? GraphicsAdapter + } + + val out = object : OutputStream() { + override fun write(p0: Int) { + getOthersFirstGPU()?.writeOut(p0.toByte()) + } + } + + val err = object : OutputStream() { + private val SGI_RED = byteArrayOf(0x1B, 0x5B, 0x33, 0x31, 0x6D) + private val SGI_RESET = byteArrayOf(0x1B, 0x5B, 0x6D) + + override fun write(p0: Int) { + getOthersFirstGPU()?.let { g -> + SGI_RED.forEach { g.writeOut(it) } + g.writeOut(p0.toByte()) + SGI_RESET.forEach { g.writeOut(it) } + } + } + + override fun write(p0: ByteArray) { + getOthersFirstGPU()?.let { g -> + SGI_RED.forEach { g.writeOut(it) } + p0.forEach { g.writeOut(it) } + SGI_RESET.forEach { g.writeOut(it) } + } + } + } + + val `in` = object : InputStream() { + init { + otherVM?.getIO()?.mmio_write(38L, 1) + } + + override fun read(): Int { + if (otherVM != null) { + var key: Byte + do { + Thread.sleep(4L) // if spinning rate is too fast, this function will fail. + // Possible cause: Input event handling of GDX is done on separate thread + key = otherVM!!.getIO().mmio_read(37L)!! + } while (key == (-1).toByte()) + + //println("[stdin] key = $key") + return key.toInt().and(255) + } + else return -1 + } + + override fun close() { + otherVM?.getIO()?.mmio_write(38L, 0) + } + } + + override fun startSendImpl(recipient: BlockTransferInterface): Int { + TODO("Not yet implemented") + } + + override fun writeoutImpl(inputData: ByteArray) { + TODO("Not yet implemented") + } + + override fun hasNext(): Boolean { + TODO("Not yet implemented") + } + + +} \ No newline at end of file