mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 11:51:49 +09:00
REWIND command for serial device
This commit is contained in:
@@ -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}
|
||||
@@ -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.
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user