poke and peek commands are now owned by vm

This commit is contained in:
minjaesong
2020-04-10 07:06:19 +09:00
parent da7e2e7c77
commit 4630780a49
5 changed files with 127 additions and 78 deletions

View File

@@ -2,8 +2,11 @@ package net.torvald.tsvm
import net.torvald.UnsafeHelper
import net.torvald.UnsafePtr
import net.torvald.tsvm.firmware.Firmware
import net.torvald.tsvm.firmware.Firmware.Companion.toLuaValue
import net.torvald.tsvm.peripheral.IOSpace
import net.torvald.tsvm.peripheral.PeriBase
import org.luaj.vm2.LuaValue
/**
* 1 byte = 2 pixels
@@ -91,6 +94,44 @@ class VM(
const val PERITYPE_GRAPHICS = "gpu"
}
internal fun translateAddr(addr: Long): Pair<Any?, Long> {
return when (addr) {
// DO note that numbers in Lua are double precision floats (ignore Lua 5.3 for now)
in 0..8192.kB() - 1 -> usermem to addr
in -1024.kB()..-1 -> peripheralTable[0].peripheral to (-addr - 1)
in -2048.kB()..-1024.kB() - 1 -> peripheralTable[1].peripheral to (-addr - 1 - 1024.kB())
in -3072.kB()..-2048.kB() - 1 -> peripheralTable[2].peripheral to (-addr - 1 - 2048.kB())
in -4096.kB()..-3072.kB() - 1 -> peripheralTable[3].peripheral to (-addr - 1 - 3072.kB())
in -5120.kB()..-4096.kB() - 1 -> peripheralTable[4].peripheral to (-addr - 1 - 4096.kB())
in -6144.kB()..-5120.kB() - 1 -> peripheralTable[5].peripheral to (-addr - 1 - 5120.kB())
in -7168.kB()..-6144.kB() - 1 -> peripheralTable[6].peripheral to (-addr - 1 - 6144.kB())
in -8192.kB()..-7168.kB() - 1 -> peripheralTable[7].peripheral to (-addr - 1 - 7168.kB())
else -> null to addr
}
}
fun poke(addr: Long, value: Byte) {
val (memspace, offset) = translateAddr(addr)
if (memspace == null)
Firmware.errorIllegalAccess(addr)
else if (memspace is UnsafePtr)
memspace.set(offset, value)
else
(memspace as PeriBase).poke(offset, value)
}
fun peek(addr:Long): Byte? {
val (memspace, offset) = translateAddr(addr)
return if (memspace == null)
null
else if (memspace is UnsafePtr)
memspace.get(offset)
else
(memspace as PeriBase).peek(offset)
}
fun Byte.toLuaValue() = LuaValue.valueOf(this.toInt())
}
data class PeripheralEntry(

View File

@@ -11,6 +11,7 @@ import kotlin.math.roundToInt
class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter() {
val vm = VM(8192)
val vmLua = VMLuaAdapter(vm)
lateinit var gpu: GraphicsAdapter
lateinit var batch: SpriteBatch
@@ -35,6 +36,7 @@ class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter()
camera.update()
batch.projectionMatrix = camera.combined
Gdx.gl20.glViewport(0, 0, appConfig.width, appConfig.height)
}
private var updateAkku = 0.0
@@ -60,51 +62,56 @@ class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter()
private fun updateGame(delta: Float) {
paintTestPalette()
//vmLua.lua.load(gpuTestPalette).call()
}
private fun paintTestPalette() {
val peripheralSlot = vm.findPeribyType(VM.PERITYPE_GRAPHICS)!!
for (y in 0 until 360) {
for (x in 0 until GraphicsAdapter.WIDTH) {
val palnum = 20 * (y / (360 / 12)) + (x / (GraphicsAdapter.WIDTH / 20))
gpu.poke(y.toLong() * GraphicsAdapter.WIDTH + x, palnum.toByte())
vm.poke(-(y.toLong() * GraphicsAdapter.WIDTH + x + 1) - VM.HW_RESERVE_SIZE * peripheralSlot, palnum.toByte())
}
}
for (y in 360 until GraphicsAdapter.HEIGHT) {
for (x in 0 until GraphicsAdapter.WIDTH) {
val palnum = 240 + (x / (GraphicsAdapter.WIDTH / 16))
gpu.poke(y.toLong() * GraphicsAdapter.WIDTH + x, palnum.toByte())
vm.poke(-(y.toLong() * GraphicsAdapter.WIDTH + x + 1) - VM.HW_RESERVE_SIZE * peripheralSlot, palnum.toByte())
}
}
gpu.poke(262142L, Math.random().times(255.0).toByte())
gpu.poke(262143L, Math.random().times(255.0).toByte())
vm.poke(-262143L - VM.HW_RESERVE_SIZE * peripheralSlot, Math.random().times(255.0).toByte())
vm.poke(-262144L - VM.HW_RESERVE_SIZE * peripheralSlot, Math.random().times(255.0).toByte())
/*
local vm = require("rawmemoryaccess")
local w = 560
local h = 448
local peripheral_slot = 1
for y = 0, 359 do
for x = 0, w - 1 do
palnum = 20 * (y / (360 / 12)) + (x / (w / 20))
vm.poke(-(y * w + x + 1) - 1048576 * peripheral_slot, palnum)
end
end
for y = 360, h - 1 do
for x = 0, w - 1 do
palnum = 240 + (x / (w / 16))
vm.poke(-(y * w + x + 1) - 1048576 * peripheral_slot, palnum)
end
end
vm.poke(-262143 - 1048576 * peripheral_slot, math.floor(math.random() * 255.0))
vm.poke(-262144 - 1048576 * peripheral_slot, math.floor(math.random() * 255.0))
*/
}
private val gpuTestPalette = """
local vm = require("rawmem")
local w = 560
local h = 448
local peripheral_slot = 1
for y = 0, 359 do
for x = 0, w - 1 do
palnum = 20 * (y / (360 / 12)) + (x / (w / 20))
vm.poke(-(y * w + x + 1) - 1048576 * peripheral_slot, palnum)
end
end
for y = 360, h - 1 do
for x = 0, w - 1 do
palnum = 240 + (x / (w / 16))
vm.poke(-(y * w + x + 1) - 1048576 * peripheral_slot, palnum)
end
end
vm.poke(-262143 - 1048576 * peripheral_slot, math.floor(math.random() * 255.0))
vm.poke(-262144 - 1048576 * peripheral_slot, math.floor(math.random() * 255.0))
""".trimIndent()
private fun renderGame() {
gpu.render(batch, 0f, 0f)

View File

@@ -0,0 +1,14 @@
package net.torvald.tsvm
import net.torvald.tsvm.firmware.Firmware
import org.luaj.vm2.lib.jse.JsePlatform
class VMLuaAdapter(val vm: VM) {
val lua = JsePlatform.standardGlobals()
init {
lua.load(Firmware(vm))
}
}

View File

@@ -9,58 +9,53 @@ import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.TwoArgFunction
internal class Firmware(val vm: VM) {
internal class Firmware(val vm: VM) : TwoArgFunction() {
val t = LuaTable()
companion object {
fun errorIllegalAccess(addr: Long) {
fun errorIllegalAccess(addr: Long) {
}
private fun translateAddr(addr: LuaValue): Pair<Any?, Long> {
val addr = addr.checklong()
return when (addr) {
// DO note that numbers in Lua are double precision floats (ignore Lua 5.3 for now)
in 0..8192.kB() - 1 -> vm.usermem to addr
in -1024.kB()..-1 -> vm.peripheralTable[0].peripheral to (-addr - 1)
in -2048.kB()..-1024.kB()-1 -> vm.peripheralTable[1].peripheral to (-addr - 1 - 1024.kB())
in -3072.kB()..-2048.kB()-1 -> vm.peripheralTable[2].peripheral to (-addr - 1 - 2048.kB())
in -4096.kB()..-3072.kB()-1 -> vm.peripheralTable[3].peripheral to (-addr - 1 - 3072.kB())
in -5120.kB()..-4096.kB()-1 -> vm.peripheralTable[4].peripheral to (-addr - 1 - 4096.kB())
in -6144.kB()..-5120.kB()-1 -> vm.peripheralTable[5].peripheral to (-addr - 1 - 5120.kB())
in -7168.kB()..-6144.kB()-1 -> vm.peripheralTable[6].peripheral to (-addr - 1 - 6144.kB())
in -8192.kB()..-7168.kB()-1 -> vm.peripheralTable[7].peripheral to (-addr - 1 - 7168.kB())
else -> null to addr
}
}
init {
t["poke"] = object : TwoArgFunction() {
override fun call(addr: LuaValue, value: LuaValue): LuaValue {
val (memspace, offset) = translateAddr(addr)
if (memspace == null)
errorIllegalAccess(addr.checklong())
else if (memspace is UnsafePtr)
memspace.set(offset, value.checkint().toByte())
else
(memspace as PeriBase).poke(offset, value.checkint().toByte())
return LuaValue.NIL
internal fun translateAddr(vm : VM, addr: LuaValue): Pair<Any?, Long> {
val addr = addr.checklong()
return when (addr) {
// DO note that numbers in Lua are double precision floats (ignore Lua 5.3 for now)
in 0..8192.kB() - 1 -> vm.usermem to addr
in -1024.kB()..-1 -> vm.peripheralTable[0].peripheral to (-addr - 1)
in -2048.kB()..-1024.kB() - 1 -> vm.peripheralTable[1].peripheral to (-addr - 1 - 1024.kB())
in -3072.kB()..-2048.kB() - 1 -> vm.peripheralTable[2].peripheral to (-addr - 1 - 2048.kB())
in -4096.kB()..-3072.kB() - 1 -> vm.peripheralTable[3].peripheral to (-addr - 1 - 3072.kB())
in -5120.kB()..-4096.kB() - 1 -> vm.peripheralTable[4].peripheral to (-addr - 1 - 4096.kB())
in -6144.kB()..-5120.kB() - 1 -> vm.peripheralTable[5].peripheral to (-addr - 1 - 5120.kB())
in -7168.kB()..-6144.kB() - 1 -> vm.peripheralTable[6].peripheral to (-addr - 1 - 6144.kB())
in -8192.kB()..-7168.kB() - 1 -> vm.peripheralTable[7].peripheral to (-addr - 1 - 7168.kB())
else -> null to addr
}
}
t["peek"] = object : OneArgFunction() {
override fun call(addr: LuaValue): LuaValue {
val (memspace, offset) = translateAddr(addr)
return if (memspace == null)
LuaValue.NIL
else if (memspace is UnsafePtr)
memspace.get(offset).toLuaValue()
else
(memspace as PeriBase).peek(offset)?.toLuaValue() ?: LuaValue.NIL
}
fun Byte.toLuaValue() = LuaValue.valueOf(this.toInt())
}
class Poke(private val vm: VM) : TwoArgFunction() {
override fun call(addr: LuaValue, value: LuaValue): LuaValue {
vm.poke(addr.checklong(), value.checkint().toByte())
return LuaValue.NIL
}
}
class Peek(private val vm: VM) : OneArgFunction() {
override fun call(addr: LuaValue): LuaValue {
return vm.peek(addr.checklong())?.toLuaValue() ?: LuaValue.NIL
}
}
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
println("[Firmware] Loading package 'rawamem'")
val t = LuaTable()
t["poke"] = Poke(vm)
t["peek"] = Peek(vm)
if (!env["package"].isnil()) env["package"]["loaded"]["rawmem"] = t
return t
}
fun Byte.toLuaValue() = LuaValue.valueOf(this.toInt())
}

View File

@@ -31,14 +31,6 @@ class GraphicsAdapter : PeriBase {
framebuffer.blending = Pixmap.Blending.None
framebuffer.setColor(-1)
framebuffer.fill()
for (k in 0..1023) {
if (k != 0 && k % 4 == 0)
println()
print(paletteOfFloats[k])
print(" ")
}
}
override fun peek(addr: Long): Byte? {