mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 19:51:51 +09:00
more work at decodemov.js
This commit is contained in:
@@ -3,15 +3,19 @@ let filename = exec_args[1]
|
||||
|
||||
const FBUF_SIZE = 560*448
|
||||
const MAGIC = [0x1F, 0x54, 0x53, 0x56, 0x4D, 0x4D, 0x4F, 0x56]
|
||||
|
||||
let status = filesystem.open("A", filename, "R")
|
||||
if (status) return status
|
||||
const port = filesystem._toPorts("A")[0]
|
||||
|
||||
println("Reading...")
|
||||
|
||||
com.sendMessage(port, "DEVRST\x17")
|
||||
com.sendMessage(port, `OPENR"${filename}",1`)
|
||||
let statusCode = com.getStatusCode(port)
|
||||
|
||||
if (statusCode != 0) return statusCode
|
||||
|
||||
//let bytes = filesystem.readAllBytes("A")
|
||||
|
||||
con.clear()
|
||||
//con.clear()
|
||||
|
||||
let readCount = 0
|
||||
|
||||
@@ -25,7 +29,6 @@ function readBytes(length) {
|
||||
|
||||
let ptr = sys.malloc(length)
|
||||
let requiredBlocks = Math.floor((readCount + length) / 4096) - Math.floor(readCount / 4096)
|
||||
let port = filesystem._toPorts("A")[0]
|
||||
|
||||
let completedReads = 0
|
||||
|
||||
@@ -35,38 +38,55 @@ function readBytes(length) {
|
||||
if (completedReads >= length) break
|
||||
|
||||
if (readCount % 4096 == 0) {
|
||||
serial.println("READ from serial")
|
||||
com.sendMessage(port, "READ")
|
||||
statusCode = com.getStatusCode(port)
|
||||
if (statusCode != 0) {
|
||||
printerrln("READ failed with "+statusCode)
|
||||
return statusCode
|
||||
}
|
||||
// pull the actual message
|
||||
sys.poke(-4093 - port, 6);sys.sleep(0) // spinning is required as Graal run is desynced with the Java side
|
||||
|
||||
// FIXME READ won't fetch next block!
|
||||
|
||||
|
||||
let blockTransferStatus = ((sys.peek(-4085 - port*2) & 255) | ((sys.peek(-4086 - port*2) & 255) << 8))
|
||||
let thisBlockLen = blockTransferStatus & 4095
|
||||
if (thisBlockLen == 0) thisBlockLen = 4096 // [1, 4096]
|
||||
let hasMore = (blockTransferStatus & 0x8000 != 0)
|
||||
|
||||
|
||||
serial.println(`thisBlockLen = ${thisBlockLen}`)
|
||||
serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - k) & 255).toString(16).padStart(2,'0')).join()}]`)
|
||||
|
||||
let remaining = Math.min(thisBlockLen, length - completedReads)
|
||||
|
||||
serial.println(`Pulled a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}`)
|
||||
serial.println(`Pulled a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}, remaining = ${remaining}`)
|
||||
|
||||
// copy from read buffer to designated position
|
||||
sys.memcpy(-4097, ptr + readCount, remaining)
|
||||
sys.memcpy(-4097, ptr + completedReads, remaining)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += remaining
|
||||
completedReads += remaining
|
||||
}
|
||||
else {
|
||||
let padding = 4096 - (readCount % 4096)
|
||||
let remaining = Math.min(padding, length - completedReads)
|
||||
let padding = readCount % 4096
|
||||
let remaining = length - completedReads
|
||||
let thisBlockLen = Math.min(4096 - padding, length - completedReads)
|
||||
|
||||
serial.println(`Reusing a block (${remaining}); readCount = ${readCount}, completedReads = ${completedReads}`)
|
||||
serial.println(`padding = ${padding}; remaining = ${remaining}`)
|
||||
|
||||
serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - padding - k) & 255).toString(16).padStart(2,'0')).join()}]`)
|
||||
|
||||
serial.println(`Reusing a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}`)
|
||||
|
||||
// copy from read buffer to designated position
|
||||
sys.memcpy(-4097 - padding, ptr + readCount, remaining)
|
||||
sys.memcpy(-4097 - padding, ptr + completedReads, thisBlockLen)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += remaining
|
||||
completedReads += remaining
|
||||
readCount += thisBlockLen
|
||||
completedReads += thisBlockLen
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +98,9 @@ function readBytes(length) {
|
||||
function readInt() {
|
||||
let b = readBytes(4)
|
||||
let i = (sys.peek(b) & 255) | ((sys.peek(b+1) & 255) << 8) | ((sys.peek(b+2) & 255) << 16) | ((sys.peek(b+3) & 255) << 24)
|
||||
|
||||
serial.println(`readInt(); bytes: ${sys.peek(b)}, ${sys.peek(b+1)}, ${sys.peek(b+2)}, ${sys.peek(b+3)} = ${i}\n`)
|
||||
|
||||
sys.free(b)
|
||||
return i
|
||||
}
|
||||
@@ -85,18 +108,29 @@ function readInt() {
|
||||
function readShort() {
|
||||
let b = readBytes(2)
|
||||
let i = (sys.peek(b) & 255) | ((sys.peek(b+1) & 255) << 8)
|
||||
|
||||
serial.println(`readShort(); bytes: ${sys.peek(b)}, ${sys.peek(b+1)} = ${i}\n`)
|
||||
|
||||
sys.free(b)
|
||||
return i
|
||||
}
|
||||
|
||||
|
||||
let magic = readBytes(8)
|
||||
let magicMatching = true
|
||||
|
||||
// check if magic number matches
|
||||
MAGIC.forEach((b,i) => {
|
||||
if (sys.peek(magic + i) & 255 != b) return 1
|
||||
let testb = sys.peek(magic + i) & 255 // for some reason this must be located here
|
||||
if (testb != b) {
|
||||
magicMatching = false
|
||||
}
|
||||
})
|
||||
sys.free(magic)
|
||||
if (!magicMatching) {
|
||||
println("Not a movie file (MAGIC mismatch)")
|
||||
return 1
|
||||
}
|
||||
|
||||
|
||||
let width = readShort()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.torvald.tsvm.peripheral
|
||||
|
||||
import net.torvald.tsvm.VM
|
||||
import net.torvald.tsvm.VMJSR223Delegate
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
@@ -15,7 +14,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
const val STATE_CODE_OPERATION_FAILED = 1
|
||||
|
||||
const val STATE_CODE_ILLEGAL_COMMAND = 128
|
||||
const val STATE_CODE_FILE_NOT_FOUND = 129
|
||||
const val STATE_CODE_NO_SUCH_FILE_EXISTS = 129
|
||||
const val STATE_CODE_FILE_ALREADY_OPENED = 130
|
||||
const val STATE_CODE_OPERATION_NOT_PERMITTED = 131
|
||||
const val STATE_CODE_READ_ONLY = 132
|
||||
@@ -33,7 +32,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
errorMsgs[STATE_CODE_OPERATION_FAILED] = "OPERATION FAILED"
|
||||
|
||||
errorMsgs[STATE_CODE_ILLEGAL_COMMAND] = "SYNTAX ERROR"
|
||||
errorMsgs[STATE_CODE_FILE_NOT_FOUND] = "FILE NOT FOUND"
|
||||
errorMsgs[STATE_CODE_NO_SUCH_FILE_EXISTS] = "NO SUCH FILE EXISTS"
|
||||
errorMsgs[STATE_CODE_FILE_ALREADY_OPENED] = "FILE ALREADY OPENED"
|
||||
errorMsgs[STATE_CODE_SYSTEM_IO_ERROR] = "IO ERROR ON SIMULATED DRIVE"
|
||||
errorMsgs[STATE_CODE_SYSTEM_SECURITY_ERROR] = "SECURITY ERROR ON SIMULATED DRIVE"
|
||||
@@ -203,7 +202,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
|
||||
if (openMode == 'R' && !file.exists()) {
|
||||
printdbg("! file not found")
|
||||
statusCode = STATE_CODE_FILE_NOT_FOUND
|
||||
statusCode = STATE_CODE_NO_SUCH_FILE_EXISTS
|
||||
return
|
||||
}
|
||||
|
||||
@@ -305,7 +304,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
val bootFile = File(rootPath, "!BOOTSEC")
|
||||
|
||||
if (!bootFile.exists()) {
|
||||
statusCode = STATE_CODE_FILE_NOT_FOUND
|
||||
statusCode = STATE_CODE_NO_SUCH_FILE_EXISTS
|
||||
return
|
||||
}
|
||||
val fis = FileInputStream(bootFile)
|
||||
|
||||
Reference in New Issue
Block a user