mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-12 07:44:03 +09:00
writing multiblock messages thru serial
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -7,6 +7,7 @@ import net.torvald.tsvm.peripheral.BlockTransferInterface.Companion.END_OF_SEND_
|
|||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import kotlin.experimental.and
|
import kotlin.experimental.and
|
||||||
import kotlin.experimental.or
|
import kotlin.experimental.or
|
||||||
|
import kotlin.math.ceil
|
||||||
|
|
||||||
object SerialHelper {
|
object SerialHelper {
|
||||||
|
|
||||||
@@ -20,7 +21,6 @@ object SerialHelper {
|
|||||||
|
|
||||||
fun sendMessage(vm: VM, portNo: Int, message: ByteArray) {
|
fun sendMessage(vm: VM, portNo: Int, message: ByteArray) {
|
||||||
if (!checkIfDeviceIsThere(vm, portNo)) throw IllegalStateException("Device not connected")
|
if (!checkIfDeviceIsThere(vm, portNo)) throw IllegalStateException("Device not connected")
|
||||||
if (message.size > BLOCK_SIZE) throw NotImplementedError("sending message greater than 4096 is a future work :p")
|
|
||||||
|
|
||||||
/*UnsafeHelper.memcpyRaw(
|
/*UnsafeHelper.memcpyRaw(
|
||||||
message, UnsafeHelper.getArrayOffset(message),
|
message, UnsafeHelper.getArrayOffset(message),
|
||||||
@@ -28,11 +28,15 @@ object SerialHelper {
|
|||||||
minOf(BLOCK_SIZE, message.size).toLong()
|
minOf(BLOCK_SIZE, message.size).toLong()
|
||||||
)*/
|
)*/
|
||||||
|
|
||||||
for (k in 0 until BLOCK_SIZE) {
|
for (blockCount in 0 until ceil(message.size.toFloat() / BLOCK_SIZE).toInt()) {
|
||||||
vm.getIO().blockTransferTx[portNo][k.toLong()] = if (k >= message.size) 0 else message[k]
|
for (k in 0 until BLOCK_SIZE) {
|
||||||
|
val index = BLOCK_SIZE * blockCount + k
|
||||||
|
vm.getIO().blockTransferTx[portNo][k.toLong()] = if (index >= message.size) 0 else message[index]
|
||||||
|
}
|
||||||
|
initiateWriting(vm, portNo)
|
||||||
|
waitUntilReady(vm, portNo)
|
||||||
}
|
}
|
||||||
|
|
||||||
initiateWriting(vm, portNo)
|
|
||||||
|
|
||||||
// TODO assuming the write operation is finished... (wait for something?)
|
// TODO assuming the write operation is finished... (wait for something?)
|
||||||
getReady(vm, portNo)
|
getReady(vm, portNo)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class IOSpace(val vm: VM) : PeriBase, InputProcessor {
|
|||||||
private val keyEventBuffers = ByteArray(8)
|
private val keyEventBuffers = ByteArray(8)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
//blockTransferPorts[0].attachDevice(TestFunctionGenerator())
|
blockTransferPorts[1].attachDevice(TestFunctionGenerator())
|
||||||
blockTransferPorts[0].attachDevice(TestDiskDrive(0))
|
blockTransferPorts[0].attachDevice(TestDiskDrive(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.torvald.tsvm.peripheral
|
package net.torvald.tsvm.peripheral
|
||||||
|
|
||||||
import net.torvald.tsvm.VM
|
import net.torvald.tsvm.VM
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
class TestFunctionGenerator : BlockTransferInterface(true, false) {
|
class TestFunctionGenerator : BlockTransferInterface(true, false) {
|
||||||
@@ -87,6 +88,10 @@ Nunc mollis nibh vitae sapien consequat, ut vestibulum sem pharetra. Aliquam iac
|
|||||||
|
|
||||||
private var blockSendBuffer = ByteArray(1)
|
private var blockSendBuffer = ByteArray(1)
|
||||||
private var blockSendCount = 0
|
private var blockSendCount = 0
|
||||||
|
private var writeMode = false
|
||||||
|
private var writeModeLength = -1
|
||||||
|
|
||||||
|
private val writeBuffer = ByteArrayOutputStream()
|
||||||
|
|
||||||
fun composeSerialAns(vararg msg: String): ByteArray {
|
fun composeSerialAns(vararg msg: String): ByteArray {
|
||||||
val sb = ArrayList<Byte>()
|
val sb = ArrayList<Byte>()
|
||||||
@@ -124,27 +129,51 @@ Nunc mollis nibh vitae sapien consequat, ut vestibulum sem pharetra. Aliquam iac
|
|||||||
return (blockSendCount * BLOCK_SIZE < blockSendBuffer.size)
|
return (blockSendCount * BLOCK_SIZE < blockSendBuffer.size)
|
||||||
}
|
}
|
||||||
override fun writeoutImpl(inputData: ByteArray) {
|
override fun writeoutImpl(inputData: ByteArray) {
|
||||||
val inputString = inputData.toString(VM.CHARSET)
|
if (writeMode) {
|
||||||
|
inputData.forEach {
|
||||||
|
if (writeModeLength > 0) {
|
||||||
|
writeBuffer.write(it.toInt())
|
||||||
|
writeModeLength -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (writeModeLength <= 0) {
|
||||||
|
writeMode = false
|
||||||
|
|
||||||
|
// test printout
|
||||||
|
val bufout = writeBuffer.toByteArray()
|
||||||
|
println("[TestFunctionGenerator] written bytes: ${bufout.size}")
|
||||||
|
println("[TestFunctionGenerator] written data: ${bufout.toString(VM.CHARSET)}")
|
||||||
|
|
||||||
|
writeBuffer.reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val inputString = trimNull(inputData).toString(VM.CHARSET)
|
||||||
|
|
||||||
|
if (inputString.startsWith("DEVRST\u0017")) {
|
||||||
|
fileOpen = false
|
||||||
|
blockSendCount = 0
|
||||||
|
} else if (inputString.startsWith("DEVTYP\u0017"))
|
||||||
|
recipient?.writeout(composeSerialAns("STOR"))
|
||||||
|
else if (inputString.startsWith("DEVNAM\u0017"))
|
||||||
|
recipient?.writeout(composeSerialAns("Testtec Signal Generator"))
|
||||||
|
else if (inputString.startsWith("OPENR\""))
|
||||||
|
fileOpen = true
|
||||||
|
else if (inputString.startsWith("CLOSE"))
|
||||||
|
fileOpen = false
|
||||||
|
//else if (inputString.startsWith("READ"))
|
||||||
|
// readModeLength = inputString.substring(4 until inputString.length).toInt()
|
||||||
|
else if (inputString.startsWith("LIST"))
|
||||||
|
recipient?.writeout("\"LOREM.TXT\" TXT\nTotal 1 files on the disk".toByteArray())
|
||||||
|
else if (inputString.startsWith("WRITE")) {
|
||||||
|
writeMode = true
|
||||||
|
writeModeLength = inputString.substring(5, inputString.length).toInt()
|
||||||
|
statusCode = 0
|
||||||
|
}
|
||||||
|
|
||||||
if (inputString.startsWith("DEVRST\u0017")) {
|
|
||||||
fileOpen = false
|
|
||||||
blockSendCount = 0
|
blockSendCount = 0
|
||||||
}
|
}
|
||||||
else if (inputString.startsWith("DEVTYP\u0017"))
|
|
||||||
recipient?.writeout(composeSerialAns("STOR"))
|
|
||||||
else if (inputString.startsWith("DEVNAM\u0017"))
|
|
||||||
recipient?.writeout(composeSerialAns("Testtec Signal Generator"))
|
|
||||||
else if (inputString.startsWith("OPENR\""))
|
|
||||||
fileOpen = true
|
|
||||||
else if (inputString.startsWith("CLOSE"))
|
|
||||||
fileOpen = false
|
|
||||||
//else if (inputString.startsWith("READ"))
|
|
||||||
// readModeLength = inputString.substring(4 until inputString.length).toInt()
|
|
||||||
else if (inputString.startsWith("LIST"))
|
|
||||||
recipient?.writeout("\"LOREM.TXT\" TXT\nTotal 1 files on the disk".toByteArray())
|
|
||||||
|
|
||||||
|
|
||||||
blockSendCount = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setMode(sendmode: Boolean) {
|
override fun setMode(sendmode: Boolean) {
|
||||||
|
|||||||
Reference in New Issue
Block a user