mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-11 13:41:50 +09:00
tsvm mov encoder and decoder
This commit is contained in:
@@ -6,41 +6,70 @@ import java.io.ByteArrayOutputStream
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
object CompressorDelegate {
|
||||
class CompressorDelegate(val vm: VM) {
|
||||
|
||||
/*fun comp(ba: ByteArray): ByteArray {
|
||||
val bin = ByteArrayInputStream(ba)
|
||||
val bout = ByteArrayOutputStream(256)
|
||||
Lzma.compress(bin, bout)
|
||||
return bout.toByteArray()
|
||||
fun comp(str: String) = Companion.comp(str)
|
||||
fun comp(ba: ByteArray) = Companion.comp(ba)
|
||||
|
||||
fun compFromTo(input: Int, len: Int, output: Int): Int {
|
||||
val inbytes = ByteArray(len) { vm.peek(input.toLong() + it)!! }
|
||||
comp(inbytes).let {
|
||||
it.forEachIndexed { index, byte ->
|
||||
vm.poke(output.toLong() + index, byte)
|
||||
}
|
||||
return it.size
|
||||
}
|
||||
}
|
||||
|
||||
fun decomp(ba: ByteArray): ByteArray {
|
||||
val bin = ByteArrayInputStream(ba)
|
||||
val bout = ByteArrayOutputStream(256)
|
||||
Lzma.decompress(bin, bout)
|
||||
return bout.toByteArray()
|
||||
}*/
|
||||
|
||||
fun comp(str: String) = comp(str.toByteArray(VM.CHARSET))
|
||||
|
||||
fun comp(ba: ByteArray): ByteArray {
|
||||
val baos = ByteArrayOutputStream()
|
||||
val gz = GZIPOutputStream(baos)
|
||||
gz.write(ba); gz.flush(); gz.finish()
|
||||
baos.flush(); baos.close()
|
||||
return baos.toByteArray()
|
||||
fun compTo(ba: ByteArray, output: Int): Int {
|
||||
comp(ba).let {
|
||||
it.forEachIndexed { index, byte ->
|
||||
vm.poke(output.toLong() + index, byte)
|
||||
}
|
||||
return it.size
|
||||
}
|
||||
}
|
||||
|
||||
fun decomp(str: String) = decomp(str.toByteArray(VM.CHARSET))
|
||||
|
||||
fun decomp(ba: ByteArray): ByteArray {
|
||||
val bais = ByteArrayInputStream(ba)
|
||||
val gz = GZIPInputStream(bais)
|
||||
val ret = gz.readBytes()
|
||||
gz.close(); bais.close()
|
||||
return ret
|
||||
fun decomp(str: String) = Companion.decomp(str)
|
||||
fun decomp(ba: ByteArray) = Companion.decomp(ba)
|
||||
|
||||
fun decompTo(str: String, pointer: Int) {
|
||||
val bytes = decomp(str)
|
||||
bytes.forEachIndexed { index, byte ->
|
||||
vm.poke(pointer.toLong() + index, byte)
|
||||
}
|
||||
}
|
||||
|
||||
val GZIP_HEADER = byteArrayOf(31,-117,8) // .gz in DEFLATE
|
||||
fun decompTo(ba: ByteArray, pointer: Int) {
|
||||
val bytes = decomp(ba)
|
||||
bytes.forEachIndexed { index, byte ->
|
||||
vm.poke(pointer.toLong() + index, byte)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val GZIP_HEADER = byteArrayOf(31, -117, 8) // .gz in DEFLATE
|
||||
|
||||
fun comp(str: String) = comp(str.toByteArray(VM.CHARSET))
|
||||
|
||||
fun comp(ba: ByteArray): ByteArray {
|
||||
val baos = ByteArrayOutputStream()
|
||||
val gz = GZIPOutputStream(baos)
|
||||
gz.write(ba); gz.flush(); gz.finish()
|
||||
baos.flush(); baos.close()
|
||||
return baos.toByteArray()
|
||||
}
|
||||
|
||||
|
||||
fun decomp(str: String) = decomp(str.toByteArray(VM.CHARSET))
|
||||
|
||||
fun decomp(ba: ByteArray): ByteArray {
|
||||
val bais = ByteArrayInputStream(ba)
|
||||
val gz = GZIPInputStream(bais)
|
||||
val ret = gz.readBytes()
|
||||
gz.close(); bais.close()
|
||||
return ret
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ object VMRunnerFactory {
|
||||
bind.putMember("sys", VMJSR223Delegate(vm)) // TODO use delegator class to access peripheral (do not expose VM itself)
|
||||
bind.putMember("graphics", GraphicsJSR223Delegate(vm))
|
||||
bind.putMember("serial", VMSerialDebugger(vm))
|
||||
bind.putMember("gzip", CompressorDelegate)
|
||||
bind.putMember("gzip", CompressorDelegate(vm))
|
||||
bind.putMember("base64", Base64Delegate)
|
||||
bind.putMember("com", SerialHelperDelegate(vm))
|
||||
bind.putMember("dma", DMADelegate(vm))
|
||||
|
||||
@@ -68,6 +68,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
private var file = File(rootPath.toURI())
|
||||
//private var readModeLength = -1 // always 4096
|
||||
private var writeMode = false
|
||||
private var appendMode = false
|
||||
private var writeModeLength = -1
|
||||
|
||||
private val messageComposeBuffer = ByteArrayOutputStream(BLOCK_SIZE) // always use this and don't alter blockSendBuffer please
|
||||
@@ -127,7 +128,7 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
* Disk drive must create desired side effects in accordance with the input message.
|
||||
*/
|
||||
override fun writeoutImpl(inputData: ByteArray) {
|
||||
if (writeMode) {
|
||||
if (writeMode || appendMode) {
|
||||
//println("[DiskDrive] writeout with inputdata length of ${inputData.size}")
|
||||
//println("[DiskDriveMsg] ${inputData.toString(Charsets.UTF_8)}")
|
||||
|
||||
@@ -137,9 +138,14 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
writeBufferUsage += inputData.size
|
||||
|
||||
if (writeBufferUsage >= writeModeLength) {
|
||||
writeMode = false
|
||||
// commit to the disk
|
||||
file.writeBytes(writeBuffer)
|
||||
if (appendMode)
|
||||
file.appendBytes(writeBuffer)
|
||||
else if (writeMode)
|
||||
file.writeBytes(writeBuffer)
|
||||
|
||||
writeMode = false
|
||||
appendMode = false
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -385,7 +391,8 @@ class TestDiskDrive(private val vm: VM, private val driveNum: Int, theRootPath:
|
||||
statusCode = STATE_CODE_OPERATION_NOT_PERMITTED
|
||||
return
|
||||
}
|
||||
writeMode = true
|
||||
if (fileOpenMode == 1) { writeMode = true; appendMode = false }
|
||||
else if (fileOpenMode == 2) { writeMode = false; appendMode = true }
|
||||
writeModeLength = inputString.substring(5, inputString.length).toInt()
|
||||
writeBuffer = ByteArray(writeModeLength)
|
||||
writeBufferUsage = 0
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.torvald.tsvm.peripheral
|
||||
|
||||
import net.torvald.tsvm.CompressorDelegate
|
||||
import net.torvald.tsvm.CompressorDelegate.GZIP_HEADER
|
||||
import net.torvald.tsvm.CompressorDelegate.Companion.GZIP_HEADER
|
||||
import net.torvald.tsvm.VM
|
||||
import java.io.File
|
||||
|
||||
|
||||
Reference in New Issue
Block a user