From 276738682029cf02d77dca5df4581d24d1648d06 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 22 Apr 2021 14:18:03 +0900 Subject: [PATCH] disk drive: fixed baaaaad writeout implementation -- what the fuck was i thinking? --- assets/disk0/tvdos/bin/command.js | 4 ++-- .../torvald/tsvm/peripheral/TestDiskDrive.kt | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/assets/disk0/tvdos/bin/command.js b/assets/disk0/tvdos/bin/command.js index db045df..81c7957 100644 --- a/assets/disk0/tvdos/bin/command.js +++ b/assets/disk0/tvdos/bin/command.js @@ -378,13 +378,13 @@ shell.execute = function(line) { if (_G.shellProgramTitles === undefined) _G.shellProgramTitles = []; _G.shellProgramTitles.push(cmd.toUpperCase()) sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]); - serial.println(_G.shellProgramTitles); + //serial.println(_G.shellProgramTitles); let ret = execApp(programCode, tokens)|0; // return value of undefined will cast into 0 _G.shellProgramTitles.pop(); sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]); - serial.println(_G.shellProgramTitles); + //serial.println(_G.shellProgramTitles); return ret; } diff --git a/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt index d096c69..4340a86 100644 --- a/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt +++ b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt @@ -118,6 +118,9 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath: return sendSize } + private lateinit var writeBuffer: ByteArray + private var writeBufferUsage = 0 + /** Computer's attempt to startSend() will result in calling this very function. * In such cases, `inputData` will be the message the computer sends. * @@ -125,20 +128,18 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath: */ override fun writeoutImpl(inputData: ByteArray) { if (writeMode) { + //println("[DiskDrive] writeout with inputdata length of ${inputData.size}") + //println("[DiskDriveMsg] ${inputData.toString(Charsets.UTF_8)}") + if (!fileOpen) throw InternalError("File is not open but the drive is in write mode") - inputData.forEach { - if (writeModeLength > 0) { - //writeBuffer.write(it.toInt()) - //writeModeLength -= 1 - file.writeBytes(inputData.sliceArray(0 until writeModeLength)) - writeModeLength = 0 - } - } + System.arraycopy(inputData, 0, writeBuffer, writeBufferUsage, minOf(writeModeLength - writeBufferUsage, inputData.size, BLOCK_SIZE)) + writeBufferUsage += inputData.size - if (writeModeLength <= 0) { + if (writeBufferUsage >= writeModeLength) { writeMode = false - //writeBuffer.reset() + // commit to the disk + file.writeBytes(writeBuffer) } } else { @@ -375,6 +376,8 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath: } writeMode = true writeModeLength = inputString.substring(5, inputString.length).toInt() + writeBuffer = ByteArray(writeModeLength) + writeBufferUsage = 0 statusCode = STATE_CODE_STANDBY } else