read() without inputstream

This commit is contained in:
minjaesong
2025-05-05 18:32:07 +09:00
parent 8603cf96a9
commit 6830be9026
6 changed files with 44 additions and 19 deletions

View File

@@ -1,11 +1,9 @@
package net.torvald.tsvm
import net.torvald.UnsafeHelper
import net.torvald.UnsafePtr
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUint
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUlong
import net.torvald.tsvm.peripheral.*
import java.nio.charset.Charset
/**
* Pass the instance of the class to the ScriptEngine's binding, preferably under the namespace of "vm"
@@ -177,10 +175,24 @@ class VMJSR223Delegate(private val vm: VM) {
* ^A-^Z: 1 through 26
*/
fun readKey(): Int {
val inputStream = vm.getInputStream()
/*val inputStream = vm.getInputStream()
var key: Int = inputStream.read()
inputStream.close()
return key
return key*/
// impl that doesn't rely on InputStream
vm.getIO().let {
it.mmio_write(38, 1)
vm.isIdle.set(true)
while (it.mmio_read(49L) == 0.toByte()) {
Thread.sleep(6L)
}
vm.isIdle.set(false)
it.mmio_write(38, 0)
return it.mmio_read(37L)!!.toUint()
}
}
/**
@@ -188,11 +200,12 @@ class VMJSR223Delegate(private val vm: VM) {
* characters (e.g. arrow keys) won't work.
*/
fun read(): String {
val inputStream = vm.getInputStream()
// val inputStream = vm.getInputStream()
val sb = StringBuilder()
var key: Int
do {
key = inputStream.read()
// key = inputStream.read()
key = readKey()
if ((key == 8 && sb.isNotEmpty()) || key in 0x20..0x7E) {
this.print("${key.toChar()}")
@@ -205,7 +218,7 @@ class VMJSR223Delegate(private val vm: VM) {
} while (key != 13 && key != 10)
this.print("\n") // printout \n
inputStream.close()
// inputStream.close()
return sb.toString()
}
@@ -214,11 +227,12 @@ class VMJSR223Delegate(private val vm: VM) {
* characters (e.g. arrow keys) won't work.
*/
fun readNoEcho(): String {
val inputStream = vm.getInputStream()
// val inputStream = vm.getInputStream()
val sb = StringBuilder()
var key: Int
do {
key = inputStream.read()
// key = inputStream.read()
key = readKey()
when (key) {
8 -> if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)
@@ -227,7 +241,7 @@ class VMJSR223Delegate(private val vm: VM) {
} while (key != 13 && key != 10)
this.println() // printout \n
inputStream.close()
// inputStream.close()
return sb.toString()
}

View File

@@ -1003,6 +1003,7 @@ open class GraphicsAdapter(private val assetsRoot: String, val vm: VM, val confi
}
override fun read(): Int {
vm.getIO().mmio_write(49L, 0)
var key: Byte
do {
Thread.sleep(4L) // if spinning rate is too fast, this function will fail.
@@ -1011,6 +1012,7 @@ open class GraphicsAdapter(private val assetsRoot: String, val vm: VM, val confi
} while (key == (-1).toByte())
//println("[stdin] key = $key")
vm.getIO().mmio_write(49L, 1)
return key.toInt().and(255)
}

View File

@@ -104,6 +104,7 @@ class IOSpace(val vm: VM) : PeriBase("io"), InputProcessor {
39L -> rawInputFunctionLatched.toInt().toByte()
in 40..47 -> keyEventBuffers[adi - 40]
48L -> (vm.resetDown.toInt(7) or vm.sysrqDown.toInt(6) or vm.stopDown.toInt()).toByte()
49L -> keyPushed.toInt().toByte()
in 64..67 -> vm.memsize.shr((adi - 64) * 8).toByte()
68L -> (uptimeCounterLatched.toInt() or RTClatched.toInt(1)).toByte()
@@ -161,6 +162,7 @@ class IOSpace(val vm: VM) : PeriBase("io"), InputProcessor {
}
private val hyveArea = ByteArray(2048)
private var keyPushed = false
override fun mmio_write(addr: Long, byte: Byte) {
val adi = addr.toInt()
@@ -180,6 +182,7 @@ class IOSpace(val vm: VM) : PeriBase("io"), InputProcessor {
39L -> rawInputFunctionLatched = (byte.isNonZero())
in 40..47 -> keyEventBuffers[adi - 40] = byte
49L -> keyPushed = (bi != 0)
68L -> {
uptimeCounterLatched = byte.and(0b01).isNonZero()
RTClatched = byte.and(0b10).isNonZero()
@@ -341,10 +344,14 @@ class IOSpace(val vm: VM) : PeriBase("io"), InputProcessor {
override fun keyTyped(p0: Char): Boolean {
if (keyboardInputRequested && p0.toInt() > 0) {
//println("[IO] key typed = ${p0.toInt()}")
keyPushed = true
keyboardBuffer.appendHead(p0.toByte())
Thread.sleep(6L)
keyPushed = false
return true
}
else {
keyPushed = false
return false
}
}