REWIND command for serial device

This commit is contained in:
minjaesong
2025-10-07 23:43:24 +09:00
parent 769b6481da
commit f918cd429c
3 changed files with 60 additions and 1 deletions

View File

@@ -155,4 +155,35 @@ function getReadCount() {
return readCount
}
exports = {fileHeader, prepare, readBytes, readInt, readShort, readFourCC, readOneByte, readString, skip, getReadCount}
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}

View File

@@ -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.

View File

@@ -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()