an option to limit the number of peripheral slots

This commit is contained in:
minjaesong
2022-08-08 18:12:39 +09:00
parent 4337768e23
commit f213dfe165
2 changed files with 31 additions and 17 deletions

View File

@@ -23,9 +23,12 @@ class VM(
val assetsDir: String, val assetsDir: String,
_memsize: Long, _memsize: Long,
val worldInterface: WorldInterface, val worldInterface: WorldInterface,
val roms: Array<VMProgramRom?> // first ROM must contain the BIOS val roms: Array<VMProgramRom?>, // first ROM must contain the BIOS
_peripheralSlots: Int = 8,
) { ) {
val peripheralSlots = _peripheralSlots.coerceIn(1,8)
val id = java.util.Random().nextInt() val id = java.util.Random().nextInt()
internal val contexts = ArrayList<Thread>() internal val contexts = ArrayList<Thread>()
@@ -36,7 +39,7 @@ class VM(
internal val usermem = UnsafeHelper.allocate(memsize) internal val usermem = UnsafeHelper.allocate(memsize)
val peripheralTable = Array(8) { PeripheralEntry() } val peripheralTable = Array(peripheralSlots) { PeripheralEntry() }
fun getIO(): IOSpace = peripheralTable[0].peripheral as IOSpace fun getIO(): IOSpace = peripheralTable[0].peripheral as IOSpace
@@ -85,7 +88,7 @@ class VM(
println("[VM] Creating new VM with ID of $id, memsize $memsize") println("[VM] Creating new VM with ID of $id, memsize $memsize")
startTime = System.nanoTime() startTime = System.currentTimeMillis()
mallocMap.clear() mallocMap.clear()
mallocSizes.clear() mallocSizes.clear()
@@ -94,7 +97,7 @@ class VM(
fun findPeribyType(searchTerm: String): PeripheralEntry? { fun findPeribyType(searchTerm: String): PeripheralEntry? {
for (i in 0..7) { for (i in 0..peripheralSlots) {
if (peripheralTable[i].type == searchTerm) return peripheralTable[i] if (peripheralTable[i].type == searchTerm) return peripheralTable[i]
} }
return null return null
@@ -110,7 +113,10 @@ class VM(
peripheralTable.forEach { it.peripheral?.dispose() } peripheralTable.forEach { it.peripheral?.dispose() }
} }
open fun getUptime() = System.nanoTime() - startTime /**
* @return system uptime in milliseconds
*/
open fun getUptime() = System.currentTimeMillis() - startTime
/* /*
NOTE: re-fill peripheralTable whenever the VM cold-boots! NOTE: re-fill peripheralTable whenever the VM cold-boots!
@@ -132,14 +138,14 @@ class VM(
return when (addr) { return when (addr) {
// DO note that numbers in Lua are double precision floats (ignore Lua 5.3 for now) // 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 0..8192.kB() - 1 -> usermem to addr
in -1024.kB()..-1 -> peripheralTable[0].peripheral to (-addr - 1) in -1024.kB()..-1 -> peripheralTable.getOrNull(0)?.peripheral to (-addr - 1)
in -2048.kB()..-1024.kB() - 1 -> peripheralTable[1].peripheral to (-addr - 1 - 1024.kB()) in -2048.kB()..-1024.kB() - 1 -> peripheralTable.getOrNull(1)?.peripheral to (-addr - 1 - 1024.kB())
in -3072.kB()..-2048.kB() - 1 -> peripheralTable[2].peripheral to (-addr - 1 - 2048.kB()) in -3072.kB()..-2048.kB() - 1 -> peripheralTable.getOrNull(2)?.peripheral to (-addr - 1 - 2048.kB())
in -4096.kB()..-3072.kB() - 1 -> peripheralTable[3].peripheral to (-addr - 1 - 3072.kB()) in -4096.kB()..-3072.kB() - 1 -> peripheralTable.getOrNull(3)?.peripheral to (-addr - 1 - 3072.kB())
in -5120.kB()..-4096.kB() - 1 -> peripheralTable[4].peripheral to (-addr - 1 - 4096.kB()) in -5120.kB()..-4096.kB() - 1 -> peripheralTable.getOrNull(4)?.peripheral to (-addr - 1 - 4096.kB())
in -6144.kB()..-5120.kB() - 1 -> peripheralTable[5].peripheral to (-addr - 1 - 5120.kB()) in -6144.kB()..-5120.kB() - 1 -> peripheralTable.getOrNull(5)?.peripheral to (-addr - 1 - 5120.kB())
in -7168.kB()..-6144.kB() - 1 -> peripheralTable[6].peripheral to (-addr - 1 - 6144.kB()) in -7168.kB()..-6144.kB() - 1 -> peripheralTable.getOrNull(6)?.peripheral to (-addr - 1 - 6144.kB())
in -8192.kB()..-7168.kB() - 1 -> peripheralTable[7].peripheral to (-addr - 1 - 7168.kB()) in -8192.kB()..-7168.kB() - 1 -> peripheralTable.getOrNull(7)?.peripheral to (-addr - 1 - 7168.kB())
else -> null to addr else -> null to addr
} }
} }

View File

@@ -44,6 +44,7 @@ class VMJSR223Delegate(val vm: VM) {
return vm.roms[vm.romMapping]!!.readAll() return vm.roms[vm.romMapping]!!.readAll()
} }
// @return in milliseconds
fun uptime(): Long { fun uptime(): Long {
vm.poke(-69, -1) vm.poke(-69, -1)
var r = 0L var r = 0L
@@ -53,11 +54,18 @@ class VMJSR223Delegate(val vm: VM) {
return r return r
} }
fun currentTimeInMills(): Long { fun currentTimeInMills(): Long {
vm.poke(-69, -1)
var r = 0L var r = 0L
for (i in 0L..7L) { // there's a "hardware bug" where trying to get current time for the first time since boot would just return 0
r = r or vm.peek(-81 - i)!!.toUlong().shl(8 * i.toInt()) // this dirty fix will continuously query the value until nonzero value is returned
} // var q = 1
do { // quick and dirty hack
// println("currentTimeInMills spin ${q++}")
vm.poke(-69, -1)
Thread.sleep(1L)
for (i in 0L..7L) {
r = r or vm.peek(-81 - i)!!.toUlong().shl(8 * i.toInt())
}
} while (r == 0L)
return r return r
} }