diff --git a/assets/disk0/tvdos/include/seqread.mjs b/assets/disk0/tvdos/include/seqread.mjs index b871c94..0a780ae 100644 --- a/assets/disk0/tvdos/include/seqread.mjs +++ b/assets/disk0/tvdos/include/seqread.mjs @@ -155,4 +155,35 @@ function getReadCount() { return readCount } -exports = {fileHeader, prepare, readBytes, readInt, readShort, readFourCC, readOneByte, readString, skip, getReadCount} \ No newline at end of file +function rewind() { + // Send REWIND command to reset stream position + com.sendMessage(port, "REWIND") + let statusCode = com.getStatusCode(port) + if (statusCode != 0) { + throw Error("REWIND failed with "+statusCode) + } + readCount = 0 +} + +function seek(position) { + if (position < 0) { + throw Error("seek: position must be non-negative") + } + + let relPos = position - readCount + + if (relPos == 0) { + return // Already at target position + } else if (relPos < 0) { + // Seeking backward - must rewind and skip forward + rewind() + if (position > 0) { + skip(position) + } + } else { + // Seeking forward - skip the difference + skip(relPos) + } +} + +exports = {fileHeader, prepare, readBytes, readInt, readShort, readFourCC, readOneByte, readString, skip, getReadCount, seek, rewind} \ No newline at end of file diff --git a/serialdev.txt b/serialdev.txt index 440bdb4..17ce783 100644 --- a/serialdev.txt +++ b/serialdev.txt @@ -104,6 +104,13 @@ Description: reads one block of file. Any subsequent read attempts will return n than a single block, rest of the bytes will be filled with zero, and size-of-the-block (see terranmon.txt) will be set accordingly. + REWIND + +Description: resets the read position to the beginning of the file for the currently open file. This allows seeking + backwards in streaming read mode without closing and reopening the file. Only applicable for files opened + with OPENR. If no file is open or the file is not in read mode, returns error status code 135 + (NO_FILE_OPENED). + CLOSE Description: closes any file that is open. diff --git a/tsvm_core/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt b/tsvm_core/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt index 589aea7..bf8fc91 100644 --- a/tsvm_core/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt +++ b/tsvm_core/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt @@ -395,6 +395,27 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath: fileOpenMode = -1 statusCode.set(STATE_CODE_STANDBY) } + else if (inputString.startsWith("REWIND")) { + // Rewind the stream to beginning for seeking + if (readStreamActive && file.isFile) { + try { + closeReadStream() + // Reopen the file at position 0 + readInputStream = FileInputStream(file) + readStreamActive = true + readFileSize = file.length() + blockSendCount = 0 + statusCode.set(STATE_CODE_STANDBY) + } + catch (e: IOException) { + closeReadStream() + statusCode.set(STATE_CODE_SYSTEM_IO_ERROR) + } + } + else { + statusCode.set(STATE_CODE_NO_FILE_OPENED) + } + } else if (inputString.startsWith("READ")) { //readModeLength = inputString.substring(4 until inputString.length).toInt()