From b00ecf7847aca6ca5411db6b01e8b330108e5a07 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 20 Oct 2020 22:21:46 +0900 Subject: [PATCH] test disk drive wip --- serialdev.txt | 5 +- .../tsvm/peripheral/BlockTransferInterface.kt | 7 +- src/net/torvald/tsvm/peripheral/IOSpace.kt | 3 +- .../tsvm/peripheral/SerialDiskDrive.kt | 22 ---- .../torvald/tsvm/peripheral/TestDiskDrive.kt | 100 ++++++++++++++++++ .../tsvm/peripheral/TestFunctionGenerator.kt | 3 + 6 files changed, 114 insertions(+), 26 deletions(-) delete mode 100644 src/net/torvald/tsvm/peripheral/SerialDiskDrive.kt create mode 100644 src/net/torvald/tsvm/peripheral/TestDiskDrive.kt diff --git a/serialdev.txt b/serialdev.txt index 13847d8..fe23e67 100644 --- a/serialdev.txt +++ b/serialdev.txt @@ -17,7 +17,10 @@ All peripherals using serial connection must follow these standards 0x17 0x1E 0x17 - 0x17 + In case of positive acknowledge: + 0x06 0x17 + In case of negative acknowledge (e.g. error messages): + 0x15 0x17 All ENQUIRY commands are RECOMMENDED to be no larger than 4096 bytes STRINGs only consists with printable ASCII subset diff --git a/src/net/torvald/tsvm/peripheral/BlockTransferInterface.kt b/src/net/torvald/tsvm/peripheral/BlockTransferInterface.kt index 6b3f385..36104d1 100644 --- a/src/net/torvald/tsvm/peripheral/BlockTransferInterface.kt +++ b/src/net/torvald/tsvm/peripheral/BlockTransferInterface.kt @@ -18,7 +18,8 @@ abstract class BlockTransferInterface(val isMaster: Boolean, val isSlave: Boolea open fun areYouReady(): Boolean = recipient?.ready ?: false open fun areYouBusy(): Boolean = recipient?.busy ?: false - /** A method exposed to outside of the box */ + /** Writes a thing to the recipient. + * A method exposed to outside of the box */ abstract fun startSend() /** The actual implementation */ protected fun startSend(sendfun: ((BlockTransferInterface) -> Unit)? = null) { @@ -35,11 +36,13 @@ abstract class BlockTransferInterface(val isMaster: Boolean, val isSlave: Boolea } } + /** Ask the recipient to start send its thing to me so that I can 'read' + */ open fun startRead() { recipient?.startSend(null) } - /** A method exposed to outside of the box */ + /** A method called by the sender so it can ACTUALLY write its thing onto me. */ abstract fun writeout(inputData: ByteArray) /** The actual implementation; must be called by a sender class */ protected fun writeout(inputData: ByteArray, writeoutfun: (() -> Unit)? = null) { diff --git a/src/net/torvald/tsvm/peripheral/IOSpace.kt b/src/net/torvald/tsvm/peripheral/IOSpace.kt index af7f9cf..fdf285c 100644 --- a/src/net/torvald/tsvm/peripheral/IOSpace.kt +++ b/src/net/torvald/tsvm/peripheral/IOSpace.kt @@ -38,7 +38,8 @@ class IOSpace(val vm: VM) : PeriBase, InputProcessor { private val keyEventBuffers = ByteArray(8) init { - blockTransferPorts[0].attachDevice(TestFunctionGenerator()) + //blockTransferPorts[0].attachDevice(TestFunctionGenerator()) + blockTransferPorts[0].attachDevice(TestDiskDrive(0)) } private fun composeBlockTransferStatus(portno: Int): Int { diff --git a/src/net/torvald/tsvm/peripheral/SerialDiskDrive.kt b/src/net/torvald/tsvm/peripheral/SerialDiskDrive.kt deleted file mode 100644 index 9d7f65d..0000000 --- a/src/net/torvald/tsvm/peripheral/SerialDiskDrive.kt +++ /dev/null @@ -1,22 +0,0 @@ -package net.torvald.tsvm.peripheral - -import java.util.* - -class SerialDiskDrive : BlockTransferInterface(false, true) { - - override fun hasNext(): Boolean { - TODO("Not yet implemented") - } - - override fun startSend() { - TODO("Not yet implemented") - } - - override fun writeout(inputData: ByteArray) { - TODO("Not yet implemented") - } - - val diskID: UUID = UUID(0, 0) - - -} \ No newline at end of file diff --git a/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt new file mode 100644 index 0000000..83a7a0c --- /dev/null +++ b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt @@ -0,0 +1,100 @@ +package net.torvald.tsvm.peripheral + +import java.io.File +import java.util.* + +class TestDiskDrive(private val driveNum: Int) : BlockTransferInterface(false, true) { + + private var fileOpen = false + private var readModeLength = -1 + + private val rootPath = File("test_assets/test_drive_$driveNum") + + init { + if (!rootPath.exists()) { + rootPath.mkdirs() + } + } + + fun composePositiveAns(vararg msg: String): ByteArray { + val sb = ArrayList() + sb.add(0x06) + sb.addAll(msg[0].toByteArray().toTypedArray()) + for (k in 1 until msg.lastIndex) { + sb.add(0x1F) + sb.addAll(msg[k].toByteArray().toTypedArray()) + } + sb.add(0x17) + return sb.toByteArray() + } + + fun composeNegativeAns(vararg msg: String): ByteArray { + val sb = ArrayList() + sb.add(0x15) + sb.addAll(msg[0].toByteArray().toTypedArray()) + for (k in 1 until msg.lastIndex) { + sb.add(0x1F) + sb.addAll(msg[k].toByteArray().toTypedArray()) + } + sb.add(0x17) + return sb.toByteArray() + } + + override fun hasNext(): Boolean { + if (!fileOpen) return false + + + return false + } + + /** Computer's attempt to startRead() will result in calling this very function. + * + * Disk drive must send prepared message (or file transfer packet) to the computer. + */ + override fun startSend() { + TODO("Not yet implemented") + } + + /** Computer's attempt to startSend() will result in calling this very function. + * In such cases, `inputData` will be the message the computer sends. + * + * Disk drive must create desired side effects in accordance with the input message. + */ + override fun writeout(inputData: ByteArray) { + val inputString = inputData.toString() + + if (inputString.startsWith("DEVRST\u0017")) { + readModeLength = -1 + fileOpen = false + } + else if (inputString.startsWith("DEVTYP\u0017")) + startSend { it.writeout(composePositiveAns("STOR")) } + else if (inputString.startsWith("DEVNAM\u0017")) + startSend { it.writeout(composePositiveAns("Testtec Virtual Disk Drive")) } + 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")) { + startSend { it.writeout("\"LOREM.TXT\" TXT\nTotal 1 files on the disk".toByteArray()) } + } + } + + val diskID: UUID = UUID(0, 0) + + +} \ No newline at end of file diff --git a/src/net/torvald/tsvm/peripheral/TestFunctionGenerator.kt b/src/net/torvald/tsvm/peripheral/TestFunctionGenerator.kt index 764e120..19a2421 100644 --- a/src/net/torvald/tsvm/peripheral/TestFunctionGenerator.kt +++ b/src/net/torvald/tsvm/peripheral/TestFunctionGenerator.kt @@ -1,5 +1,7 @@ package net.torvald.tsvm.peripheral +import java.util.ArrayList + class TestFunctionGenerator : BlockTransferInterface(true, false) { val filecontent_lorem = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ipsum magna, ultrices eu leo eu, consequat eleifend arcu. Nam tempor nunc aliquam mi cursus mollis. Aenean dictum iaculis dolor eget porttitor. Fusce vulputate dui id mauris ultricies, non aliquet nulla pulvinar. Integer consectetur nulla at cursus cursus. Nullam enim nisl, elementum a fermentum sed, suscipit id sapien. Duis eget enim lacinia, aliquam sapien ac, commodo risus. Morbi at enim sem. Aenean sollicitudin purus et sem porttitor, convallis ultricies nulla posuere. Suspendisse euismod sagittis vestibulum. Mauris lorem nisl, placerat et finibus non, cursus non ex. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse finibus non dui vel tempor. Nam rhoncus ligula et massa sagittis fringilla. Cras convallis pellentesque nulla in rutrum. @@ -20,6 +22,7 @@ Nunc mollis nibh vitae sapien consequat, ut vestibulum sem pharetra. Aliquam iac fun composeSerialAns(vararg msg: String): ByteArray { val sb = ArrayList() + sb.add(0x06) // always positive ans sb.addAll(msg[0].toByteArray().toTypedArray()) for (k in 1 until msg.lastIndex) { sb.add(0x1F)