mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-14 16:34:04 +09:00
sequentially-read-a-file as js module
This commit is contained in:
@@ -1,116 +1,20 @@
|
||||
|
||||
const FBUF_SIZE = 560*448
|
||||
const MAGIC = [0x1F, 0x54, 0x53, 0x56, 0x4D, 0x4D, 0x4F, 0x56]
|
||||
const port = _TVDOS.DRV.FS.SERIAL._toPorts("A")[0]
|
||||
|
||||
|
||||
const fullFilePath = _G.shell.resolvePathInput(exec_args[1])
|
||||
const FILE_LENGTH = files.open(fullFilePath.full).size
|
||||
|
||||
serial.println(`Reading ${FILE_LENGTH} bytes...`)
|
||||
|
||||
com.sendMessage(port, "DEVRST\x17")
|
||||
com.sendMessage(port, `OPENR"${fullFilePath.string}",1`)
|
||||
let statusCode = com.getStatusCode(port)
|
||||
|
||||
if (statusCode != 0) {
|
||||
printerrln(`No such file (${statusCode})`)
|
||||
return statusCode
|
||||
}
|
||||
|
||||
com.sendMessage(port, "READ")
|
||||
statusCode = com.getStatusCode(port)
|
||||
if (statusCode != 0) {
|
||||
printerrln("READ failed with "+statusCode)
|
||||
return statusCode
|
||||
}
|
||||
|
||||
con.clear(); con.curs_set(0)
|
||||
|
||||
let readCount = 0
|
||||
|
||||
function readBytes(length, ptrToDecode) {
|
||||
let ptr = (ptrToDecode === undefined) ? sys.malloc(length) : ptrToDecode
|
||||
let requiredBlocks = Math.floor((readCount + length) / 4096) - Math.floor(readCount / 4096)
|
||||
|
||||
let completedReads = 0
|
||||
|
||||
//serial.println(`readBytes(${length}); readCount = ${readCount}`)
|
||||
|
||||
for (let bc = 0; bc < requiredBlocks + 1; bc++) {
|
||||
if (completedReads >= length) break
|
||||
|
||||
if (readCount % 4096 == 0) {
|
||||
//serial.println("READ from serial")
|
||||
// pull the actual message
|
||||
sys.poke(-4093 - port, 6);sys.sleep(0) // spinning is required as Graal run is desynced with the Java side
|
||||
|
||||
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)
|
||||
con.clear();con.curs_set(0)
|
||||
|
||||
|
||||
//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}, remaining = ${remaining}`)
|
||||
|
||||
// copy from read buffer to designated position
|
||||
sys.memcpy(-4097, ptr + completedReads, remaining)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += remaining
|
||||
completedReads += remaining
|
||||
}
|
||||
else {
|
||||
let padding = readCount % 4096
|
||||
let remaining = length - completedReads
|
||||
let thisBlockLen = Math.min(4096 - padding, length - 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 + completedReads, thisBlockLen)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += thisBlockLen
|
||||
completedReads += thisBlockLen
|
||||
}
|
||||
}
|
||||
|
||||
//serial.println(`END readBytes(${length}); readCount = ${readCount}\n`)
|
||||
|
||||
return ptr
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 seqread = require("seqread")
|
||||
seqread.prepare(fullFilePath.full)
|
||||
|
||||
|
||||
let magic = readBytes(8)
|
||||
|
||||
let magic = seqread.readBytes(8)
|
||||
let magicMatching = true
|
||||
|
||||
// check if magic number matches
|
||||
@@ -127,19 +31,19 @@ if (!magicMatching) {
|
||||
}
|
||||
|
||||
|
||||
let width = readShort()
|
||||
let height = readShort()
|
||||
let fps = readShort(); if (fps == 0) fps = 9999
|
||||
let width = seqread.readShort()
|
||||
let height = seqread.readShort()
|
||||
let fps = seqread.readShort(); if (fps == 0) fps = 9999
|
||||
|
||||
//fps = 9999
|
||||
|
||||
let frameTime = 1.0 / fps
|
||||
let frameCount = readInt() % 16777216
|
||||
let globalType = readShort()
|
||||
sys.free(readBytes(12)) // skip 12 bytes
|
||||
let frameCount = seqread.readInt() % 16777216
|
||||
let globalType = seqread.readShort()
|
||||
sys.free(seqread.readBytes(12)) // skip 12 bytes
|
||||
let akku = frameTime
|
||||
let framesRendered = 0
|
||||
//serial.println(readCount) // must say 18
|
||||
//serial.println(seqread.getReadCount()) // must say 18
|
||||
//serial.println(`Dim: (${width}x${height}), FPS: ${fps}, Frames: ${frameCount}`)
|
||||
|
||||
/*if (type != 4 && type != 5 && type != 260 && type != 261) {
|
||||
@@ -165,7 +69,7 @@ audio.setPcmMode(0)
|
||||
audio.setMasterVolume(0, 255)
|
||||
|
||||
renderLoop:
|
||||
while (readCount < FILE_LENGTH) {
|
||||
while (seqread.getReadCount() < FILE_LENGTH) {
|
||||
let t1 = sys.nanoTime()
|
||||
|
||||
if (akku >= frameTime) {
|
||||
@@ -178,9 +82,9 @@ while (readCount < FILE_LENGTH) {
|
||||
|
||||
if (frameUnit != 0) {
|
||||
// skip frames if necessary
|
||||
while (frameUnit >= 1 && readCount < FILE_LENGTH) {
|
||||
while (frameUnit >= 1 && seqread.getReadCount() < FILE_LENGTH) {
|
||||
|
||||
let packetType = readShort()
|
||||
let packetType = seqread.readShort()
|
||||
|
||||
// ideally, first two packets will be audio packets
|
||||
|
||||
@@ -190,7 +94,7 @@ while (readCount < FILE_LENGTH) {
|
||||
}
|
||||
// background colour packets
|
||||
else if (65279 == packetType) {
|
||||
let rgbx = readInt()
|
||||
let rgbx = seqread.readInt()
|
||||
graphics.setBackground(
|
||||
(rgbx & 0xFF000000) >>> 24,
|
||||
(rgbx & 0x00FF0000) >>> 16,
|
||||
@@ -202,14 +106,14 @@ while (readCount < FILE_LENGTH) {
|
||||
// iPF
|
||||
if (packetType == 4 || packetType == 5 || packetType == 260 || packetType == 261) {
|
||||
let decodefun = (packetType > 255) ? graphics.decodeIpf2 : graphics.decodeIpf1
|
||||
let payloadLen = readInt()
|
||||
let payloadLen = seqread.readInt()
|
||||
|
||||
if (framesRead >= frameCount) {
|
||||
break renderLoop
|
||||
}
|
||||
|
||||
framesRead += 1
|
||||
let gzippedPtr = readBytes(payloadLen)
|
||||
let gzippedPtr = seqread.readBytes(payloadLen)
|
||||
framesRendered += 1
|
||||
|
||||
if (frameUnit == 1) {
|
||||
@@ -226,14 +130,16 @@ while (readCount < FILE_LENGTH) {
|
||||
sys.free(gzippedPtr)
|
||||
}
|
||||
else {
|
||||
throw Error(`Unknown Video Packet with type ${packetType} at offset ${readCount - 2}`)
|
||||
throw Error(`Unknown Video Packet with type ${packetType} at offset ${seqread.getReadCount() - 2}`)
|
||||
}
|
||||
}
|
||||
// audio packets
|
||||
else if (4096 <= packetType && packetType <= 6133) {
|
||||
if (4097 == packetType) {
|
||||
let readLength = readInt()
|
||||
let samples = readBytes(readLength)
|
||||
let readLength = seqread.readInt()
|
||||
let samples = seqread.readBytes(readLength)
|
||||
|
||||
if (readLength == 0) throw Error("Readlength is zero")
|
||||
|
||||
audio.putPcmDataByPtr(samples, readLength, 0)
|
||||
audio.setSampleUploadLength(0, readLength)
|
||||
@@ -242,11 +148,11 @@ while (readCount < FILE_LENGTH) {
|
||||
sys.free(samples)
|
||||
}
|
||||
else {
|
||||
throw Error(`Audio Packet with type ${packetType} at offset ${readCount - 2}`)
|
||||
throw Error(`Audio Packet with type ${packetType} at offset ${seqread.getReadCount() - 2}`)
|
||||
}
|
||||
}
|
||||
else {
|
||||
println(`Unknown Packet with type ${packetType} at offset ${readCount - 2}`)
|
||||
println(`Unknown Packet with type ${packetType} at offset ${seqread.getReadCount() - 2}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user