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

@@ -92,7 +92,8 @@ function getRGBfromScr(x, y) {
return [(rg >>> 4) / 15.0, (rg & 15) / 15.0, (ba >>> 4) / 15.0]
}
let oldBgcol = [0.0, 0.0, 0.0]
const BIAS_LIGHTING_MIN = 1.0 / 16.0
let oldBgcol = [BIAS_LIGHTING_MIN, BIAS_LIGHTING_MIN, BIAS_LIGHTING_MIN]
let stopPlay = false
if (interactive) {
con.move(1,1)
@@ -197,9 +198,9 @@ while (!stopPlay && seqread.getReadCount() < FILE_LENGTH) {
out[1] += rgb[1]
out[2] += rgb[2]
})
out[0] = out[0] / samples.length / 2.0 // darken a bit
out[1] = out[1] / samples.length / 2.0
out[2] = out[2] / samples.length / 2.0
out[0] = BIAS_LIGHTING_MIN + (out[0] / samples.length / 2.0) // darken a bit
out[1] = BIAS_LIGHTING_MIN + (out[1] / samples.length / 2.0)
out[2] = BIAS_LIGHTING_MIN + (out[2] / samples.length / 2.0)
let bgr = (oldBgcol[0]*5 + out[0]) / 6.0
let bgg = (oldBgcol[1]*5 + out[1]) / 6.0
@@ -255,9 +256,9 @@ while (!stopPlay && seqread.getReadCount() < FILE_LENGTH) {
out[1] += rgb[1]
out[2] += rgb[2]
})
out[0] = out[0] / samples.length / 2.0 // darken a bit
out[1] = out[1] / samples.length / 2.0
out[2] = out[2] / samples.length / 2.0
out[0] = BIAS_LIGHTING_MIN + (out[0] / samples.length / 2.0) // darken a bit
out[1] = BIAS_LIGHTING_MIN + (out[1] / samples.length / 2.0)
out[2] = BIAS_LIGHTING_MIN + (out[2] / samples.length / 2.0)
let bgr = (oldBgcol[0]*5 + out[0]) / 6.0
let bgg = (oldBgcol[1]*5 + out[1]) / 6.0

View File

@@ -76,6 +76,7 @@ MMIO
t: STOP button (should raise SIGTERM)
r: RESET button (hypervisor should reset the system)
q: SysRq button (hypervisor should respond to it)
49: set to 1 if a key has pushed into key buffer (or, if the system has a key press to pull) via MMIO 38; othewise 0
64..67 RO: User area memory size in bytes
68 WO: Counter latch

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
}
}

View File

@@ -43,8 +43,8 @@ public class AppLoader {
// VM vm = new VM("./assets", 64 << 10, new TheRealWorld(), new VMProgramRom[]{OEMBios.INSTANCE, BasicRom.INSTANCE}, 8, watchdogs);
// VM vm = new VM("./assets", 64 << 10, new TheRealWorld(), new VMProgramRom[]{TandemBios.INSTANCE, BasicRom.INSTANCE}, 2, watchdogs);
// VM vm = new VM("./assets", 128 << 10, new TheRealWorld(), new VMProgramRom[]{BasicBios.INSTANCE, WPBios.INSTANCE}, 2, watchdogs);
// VM vm = new VM("./assets", 8192 << 10, new TheRealWorld(), new VMProgramRom[]{TsvmBios.INSTANCE}, 8, watchdogs);
VM vm = new VM("./assets", 8192 << 10, new TheRealWorld(), new VMProgramRom[]{Mon.INSTANCE, HyveRom.INSTANCE}, 8, watchdogs);
VM vm = new VM("./assets", 8192 << 10, new TheRealWorld(), new VMProgramRom[]{TsvmBios.INSTANCE}, 8, watchdogs);
// VM vm = new VM("./assets", 8192 << 10, new TheRealWorld(), new VMProgramRom[]{Mon.INSTANCE, HyveRom.INSTANCE}, 8, watchdogs);
// VM vm = new VM("./assets", 8192 << 10, new TheRealWorld(), new VMProgramRom[]{OpenBios.INSTANCE}, 8, watchdogs);
// VM pipvm = new VM("./assets", 4096, new TheRealWorld(), new VMProgramRom[]{PipBios.INSTANCE, PipROM.INSTANCE}, 8, watchdogs);