mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-10 23:04:04 +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 FBUF_SIZE = 560*448
|
||||||
const MAGIC = [0x1F, 0x54, 0x53, 0x56, 0x4D, 0x4D, 0x4F, 0x56]
|
const MAGIC = [0x1F, 0x54, 0x53, 0x56, 0x4D, 0x4D, 0x4F, 0x56]
|
||||||
|
const port = filesystem._toPorts("A")[0]
|
||||||
let status = filesystem.open("A", filename, "R")
|
|
||||||
if (status) return status
|
|
||||||
|
|
||||||
println("Reading...")
|
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")
|
//let bytes = filesystem.readAllBytes("A")
|
||||||
|
|
||||||
con.clear()
|
//con.clear()
|
||||||
|
|
||||||
let readCount = 0
|
let readCount = 0
|
||||||
|
|
||||||
@@ -25,7 +29,6 @@ function readBytes(length) {
|
|||||||
|
|
||||||
let ptr = sys.malloc(length)
|
let ptr = sys.malloc(length)
|
||||||
let requiredBlocks = Math.floor((readCount + length) / 4096) - Math.floor(readCount / 4096)
|
let requiredBlocks = Math.floor((readCount + length) / 4096) - Math.floor(readCount / 4096)
|
||||||
let port = filesystem._toPorts("A")[0]
|
|
||||||
|
|
||||||
let completedReads = 0
|
let completedReads = 0
|
||||||
|
|
||||||
@@ -35,38 +38,55 @@ function readBytes(length) {
|
|||||||
if (completedReads >= length) break
|
if (completedReads >= length) break
|
||||||
|
|
||||||
if (readCount % 4096 == 0) {
|
if (readCount % 4096 == 0) {
|
||||||
|
serial.println("READ from serial")
|
||||||
com.sendMessage(port, "READ")
|
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 blockTransferStatus = ((sys.peek(-4085 - port*2) & 255) | ((sys.peek(-4086 - port*2) & 255) << 8))
|
||||||
let thisBlockLen = blockTransferStatus & 4095
|
let thisBlockLen = blockTransferStatus & 4095
|
||||||
if (thisBlockLen == 0) thisBlockLen = 4096 // [1, 4096]
|
if (thisBlockLen == 0) thisBlockLen = 4096 // [1, 4096]
|
||||||
let hasMore = (blockTransferStatus & 0x8000 != 0)
|
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)
|
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
|
// copy from read buffer to designated position
|
||||||
sys.memcpy(-4097, ptr + readCount, remaining)
|
sys.memcpy(-4097, ptr + completedReads, remaining)
|
||||||
|
|
||||||
// increment readCount properly
|
// increment readCount properly
|
||||||
readCount += remaining
|
readCount += remaining
|
||||||
completedReads += remaining
|
completedReads += remaining
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let padding = 4096 - (readCount % 4096)
|
let padding = readCount % 4096
|
||||||
let remaining = Math.min(padding, length - completedReads)
|
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
|
// copy from read buffer to designated position
|
||||||
sys.memcpy(-4097 - padding, ptr + readCount, remaining)
|
sys.memcpy(-4097 - padding, ptr + completedReads, thisBlockLen)
|
||||||
|
|
||||||
// increment readCount properly
|
// increment readCount properly
|
||||||
readCount += remaining
|
readCount += thisBlockLen
|
||||||
completedReads += remaining
|
completedReads += thisBlockLen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +98,9 @@ function readBytes(length) {
|
|||||||
function readInt() {
|
function readInt() {
|
||||||
let b = readBytes(4)
|
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)
|
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)
|
sys.free(b)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@@ -85,18 +108,29 @@ function readInt() {
|
|||||||
function readShort() {
|
function readShort() {
|
||||||
let b = readBytes(2)
|
let b = readBytes(2)
|
||||||
let i = (sys.peek(b) & 255) | ((sys.peek(b+1) & 255) << 8)
|
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)
|
sys.free(b)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let magic = readBytes(8)
|
let magic = readBytes(8)
|
||||||
|
let magicMatching = true
|
||||||
|
|
||||||
// check if magic number matches
|
// check if magic number matches
|
||||||
MAGIC.forEach((b,i) => {
|
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)
|
sys.free(magic)
|
||||||
|
if (!magicMatching) {
|
||||||
|
println("Not a movie file (MAGIC mismatch)")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let width = readShort()
|
let width = readShort()
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package net.torvald.tsvm.peripheral
|
package net.torvald.tsvm.peripheral
|
||||||
|
|
||||||
import net.torvald.tsvm.VM
|
import net.torvald.tsvm.VM
|
||||||
import net.torvald.tsvm.VMJSR223Delegate
|
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
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_OPERATION_FAILED = 1
|
||||||
|
|
||||||
const val STATE_CODE_ILLEGAL_COMMAND = 128
|
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_FILE_ALREADY_OPENED = 130
|
||||||
const val STATE_CODE_OPERATION_NOT_PERMITTED = 131
|
const val STATE_CODE_OPERATION_NOT_PERMITTED = 131
|
||||||
const val STATE_CODE_READ_ONLY = 132
|
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_OPERATION_FAILED] = "OPERATION FAILED"
|
||||||
|
|
||||||
errorMsgs[STATE_CODE_ILLEGAL_COMMAND] = "SYNTAX ERROR"
|
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_FILE_ALREADY_OPENED] = "FILE ALREADY OPENED"
|
||||||
errorMsgs[STATE_CODE_SYSTEM_IO_ERROR] = "IO ERROR ON SIMULATED DRIVE"
|
errorMsgs[STATE_CODE_SYSTEM_IO_ERROR] = "IO ERROR ON SIMULATED DRIVE"
|
||||||
errorMsgs[STATE_CODE_SYSTEM_SECURITY_ERROR] = "SECURITY 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()) {
|
if (openMode == 'R' && !file.exists()) {
|
||||||
printdbg("! file not found")
|
printdbg("! file not found")
|
||||||
statusCode = STATE_CODE_FILE_NOT_FOUND
|
statusCode = STATE_CODE_NO_SUCH_FILE_EXISTS
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +304,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
|||||||
val bootFile = File(rootPath, "!BOOTSEC")
|
val bootFile = File(rootPath, "!BOOTSEC")
|
||||||
|
|
||||||
if (!bootFile.exists()) {
|
if (!bootFile.exists()) {
|
||||||
statusCode = STATE_CODE_FILE_NOT_FOUND
|
statusCode = STATE_CODE_NO_SUCH_FILE_EXISTS
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val fis = FileInputStream(bootFile)
|
val fis = FileInputStream(bootFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user