faster partial reading of file wip

This commit is contained in:
minjaesong
2022-04-13 17:30:42 +09:00
parent 95bfaae1da
commit 95b0d4672e
3 changed files with 96 additions and 8 deletions

View File

@@ -11,6 +11,9 @@ class CompressorDelegate(val vm: VM) {
fun comp(str: String) = Companion.comp(str)
fun comp(ba: ByteArray) = Companion.comp(ba)
/**
* @return length of the bytes compressed
*/
fun compFromTo(input: Int, len: Int, output: Int): Int {
val inbytes = ByteArray(len) { vm.peek(input.toLong() + it)!! }
comp(inbytes).let {
@@ -48,6 +51,19 @@ class CompressorDelegate(val vm: VM) {
}
}
/**
* @return length of the bytes compressed
*/
fun decompFromTo(input: Int, len: Int, output: Int): Int {
val inbytes = ByteArray(len) { vm.peek(input.toLong() + it)!! }
decomp(inbytes).let {
it.forEachIndexed { index, byte ->
vm.poke(output.toLong() + index, byte)
}
return it.size
}
}
companion object {
val GZIP_HEADER = byteArrayOf(31, -117, 8) // .gz in DEFLATE

View File

@@ -3,7 +3,9 @@ package net.torvald.tsvm
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import net.torvald.UnsafeHelper
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUlong
import net.torvald.tsvm.peripheral.IOSpace
import java.nio.charset.Charset
/**
@@ -16,6 +18,24 @@ class VMJSR223Delegate(val vm: VM) {
fun nanoTime() = System.nanoTime()
fun malloc(size: Int) = vm.malloc(size)
fun free(ptr: Int) = vm.free(ptr)
fun memcpy(from: Int, to: Int, len: Int) {
val len = len.toLong()
// some special cases for native memcpy
val ioSpace = vm.peripheralTable[0].peripheral!! as IOSpace
// within scratchpad memory?
if (from in 0 until 8388608 && (to + len) in 0 until 8388608)
UnsafeHelper.memcpy(vm.usermem.ptr + from, vm.usermem.ptr + to, len)
// first serial read buffer -> usermem
else if (from in -4097 downTo -8192 && (to + len) in 0 until 8388608)
UnsafeHelper.memcpy(ioSpace.blockTransferRx[0].ptr + (-4097 - from), vm.usermem.ptr + to, len)
// usermem -> first serial write buffer
else if (from in 0 until 8388608 && (to + len) in -4097L downTo -8192L)
UnsafeHelper.memcpy(vm.usermem.ptr + from, ioSpace.blockTransferTx[0].ptr + (-4097 - to), len)
else
for (i in 0 until len) {
vm.poke(to + i, vm.peek(from + i)!!)
}
}
fun mapRom(slot: Int) {
vm.romMapping = slot.and(255)
}