mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 19:51:51 +09:00
tevd disk drive wip
This commit is contained in:
BIN
lib/TerranVirtualDisk-src.jar
Normal file
BIN
lib/TerranVirtualDisk-src.jar
Normal file
Binary file not shown.
Binary file not shown.
@@ -63,7 +63,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
|||||||
if (DBGPRN) println("[TestDiskDrive] $msg")
|
if (DBGPRN) println("[TestDiskDrive] $msg")
|
||||||
}
|
}
|
||||||
|
|
||||||
private val rootPath = File(theRootPath) ?: File("test_assets/test_drive_$driveNum")
|
private val rootPath = File(theRootPath)
|
||||||
|
|
||||||
private var fileOpen = false
|
private var fileOpen = false
|
||||||
private var fileOpenMode = -1 // 1: 'W", 2: 'A'
|
private var fileOpenMode = -1 // 1: 'W", 2: 'A'
|
||||||
|
|||||||
99
tsvm_core/src/net/torvald/tsvm/peripheral/TevdDiskDrive.kt
Normal file
99
tsvm_core/src/net/torvald/tsvm/peripheral/TevdDiskDrive.kt
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package net.torvald.tsvm.peripheral
|
||||||
|
|
||||||
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.VDUtil
|
||||||
|
import net.torvald.tsvm.VM
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileNotFoundException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by minjaesong on 2022-12-15.
|
||||||
|
*/
|
||||||
|
class TevdDiskDrive(private val vm: VM, private val theRootPath: String) : BlockTransferInterface(false, true) {
|
||||||
|
|
||||||
|
|
||||||
|
private val DBGPRN = true
|
||||||
|
|
||||||
|
|
||||||
|
private fun printdbg(msg: Any) {
|
||||||
|
if (DBGPRN) println("[TestDiskDrive] $msg")
|
||||||
|
}
|
||||||
|
|
||||||
|
private val rootPath = File(theRootPath)
|
||||||
|
|
||||||
|
private var fileOpen = false
|
||||||
|
private var fileOpenMode = -1 // 1: 'W", 2: 'A'
|
||||||
|
private var file = File(rootPath.toURI())
|
||||||
|
//private var readModeLength = -1 // always 4096
|
||||||
|
private var writeMode = false
|
||||||
|
private var appendMode = false
|
||||||
|
private var writeModeLength = -1
|
||||||
|
|
||||||
|
private val messageComposeBuffer = ByteArrayOutputStream(BLOCK_SIZE) // always use this and don't alter blockSendBuffer please
|
||||||
|
private var blockSendBuffer = ByteArray(1)
|
||||||
|
private var blockSendCount = 0
|
||||||
|
|
||||||
|
|
||||||
|
init {
|
||||||
|
statusCode = TestDiskDrive.STATE_CODE_STANDBY
|
||||||
|
|
||||||
|
if (!rootPath.exists()) {
|
||||||
|
throw FileNotFoundException("Disk file '${theRootPath}' not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val DOM = VDUtil.readDiskArchive(rootPath, charset = VM.CHARSET)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/** How often the changes in DOM (disk object model) should be saved to the physical drive when there are changes. Seconds. */
|
||||||
|
const val COMMIT_INTERVAL = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
fun commit() {
|
||||||
|
VDUtil.dumpToRealMachine(DOM, rootPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun resetBuf() {
|
||||||
|
blockSendCount = 0
|
||||||
|
messageComposeBuffer.reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun hasNext(): Boolean {
|
||||||
|
|
||||||
|
|
||||||
|
return (blockSendCount * BLOCK_SIZE < blockSendBuffer.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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 startSendImpl(recipient: BlockTransferInterface): Int {
|
||||||
|
if (blockSendCount == 0) {
|
||||||
|
blockSendBuffer = messageComposeBuffer.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
val sendSize = if (blockSendBuffer.size - (blockSendCount * BLOCK_SIZE) < BLOCK_SIZE)
|
||||||
|
blockSendBuffer.size % BLOCK_SIZE
|
||||||
|
else BLOCK_SIZE
|
||||||
|
|
||||||
|
recipient.writeout(ByteArray(sendSize) {
|
||||||
|
blockSendBuffer[blockSendCount * BLOCK_SIZE + it]
|
||||||
|
})
|
||||||
|
|
||||||
|
blockSendCount += 1
|
||||||
|
|
||||||
|
return sendSize
|
||||||
|
}
|
||||||
|
|
||||||
|
private lateinit var writeBuffer: ByteArray
|
||||||
|
private var writeBufferUsage = 0
|
||||||
|
|
||||||
|
override fun writeoutImpl(inputData: ByteArray) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user