@@ -4,14 +4,19 @@ import kr.pe.ecmaxp.openpie.arch.consts.*
44import kr.pe.ecmaxp.openpie.arch.state.FileHandle
55import kr.pe.ecmaxp.openpie.arch.types.Interrupt
66import kr.pe.ecmaxp.openpie.arch.types.call.*
7- import kr.pe.ecmaxp.thumbsf.consts.R0
87import kr.pe.ecmaxp.thumbsf.consts.R7
98import kr.pe.ecmaxp.thumbsf.signal.ControlPauseSignal
109import kr.pe.ecmaxp.thumbsf.signal.ControlStopSignal
10+ import li.cil.oc.api.Driver
11+ import li.cil.oc.api.driver.item.MutableProcessor
12+ import li.cil.oc.api.driver.item.Processor
1113import li.cil.oc.api.machine.*
1214import li.cil.oc.api.network.Component
15+ import li.cil.oc.api.network.Connector
16+ import net.minecraft.item.ItemStack
1317import java.io.FileNotFoundException
1418import java.nio.charset.StandardCharsets
19+ import li.cil.oc.api.Machine as MachineAPI
1520
1621class OpenPieInterruptHandler (val vm : OpenPieVirtualMachine ) {
1722 operator fun invoke (intr : Interrupt , synchronized : Boolean ) {
@@ -86,16 +91,16 @@ class OpenPieInterruptHandler(val vm: OpenPieVirtualMachine) {
8691
8792 private fun handleSignal (intr : Interrupt , @Suppress(" UNUSED_PARAMETER" ) synchronized : Boolean ): Int {
8893 return when (intr.imm) {
89- SYS_SIGNAL_REQUEST -> {
94+ SYS_SIGNAL_POP -> {
9095 val signal: Signal ? = machine.popSignal()
9196 if (signal == null ) {
92- intr.cpu.regs[R7 ] = SYS_SIGNAL_PENDING
97+ intr.cpu.regs[R7 ] = SYS_SIGNAL_POP_WAKEUP
9398 throw ControlPauseSignal (ExecutionResult .Sleep (intr.r0))
9499 }
95100
96101 intr.responseValue(signal)
97102 }
98- SYS_SIGNAL_PENDING -> {
103+ SYS_SIGNAL_POP_WAKEUP -> {
99104 val signal: Signal = machine.popSignal() ? : return intr.responseNone()
100105 intr.responseValue(signal)
101106 }
@@ -152,6 +157,26 @@ class OpenPieInterruptHandler(val vm: OpenPieVirtualMachine) {
152157 }
153158 }
154159 }
160+ SYS_COMPONENT_TYPE -> {
161+ val req = intr.readObject() as Array <* >
162+ val address = req[0 ] as String
163+ val components = machine.components()
164+
165+ return if (components.containsKey(address))
166+ intr.responseValue(components[address])
167+ else
168+ intr.responseNone()
169+ }
170+ SYS_COMPONENT_SLOT -> {
171+ val req = intr.readObject() as Array <* >
172+ val address = req[0 ] as String
173+ val components = machine.components()
174+
175+ return if (components.containsKey(address))
176+ intr.responseValue(machine.host().componentSlot(address))
177+ else
178+ intr.responseNone()
179+ }
155180 SYS_COMPONENT_METHODS -> {
156181 val req = intr.readObject() as Array <* >
157182 val node = machine.node().network().node(req[0 ] as String )
@@ -270,7 +295,6 @@ class OpenPieInterruptHandler(val vm: OpenPieVirtualMachine) {
270295
271296 private fun handleComputer (intr : Interrupt , @Suppress(" UNUSED_PARAMETER" ) synchronized : Boolean ): Int {
272297 when (intr.imm) {
273- SYS_COMPUTER_GET_COST_PER_TICK -> return intr.responseValue(machine.costPerTick)
274298 SYS_COMPUTER_LAST_ERROR -> return intr.responseValue(machine.lastError())
275299 SYS_COMPUTER_BEEP_1 -> {
276300 val pattern = intr.readString()
@@ -298,6 +322,73 @@ class OpenPieInterruptHandler(val vm: OpenPieVirtualMachine) {
298322 }
299323 SYS_COMPUTER_COMPUTER_ADDRESS -> return intr.responseValue(machine.node().address())
300324 SYS_COMPUTER_TMP_ADDRESS -> return intr.responseValue(machine.tmpAddress())
325+ SYS_COMPUTER_ENERGY -> {
326+ return intr.responseValue((machine.node() as Connector ).globalBuffer())
327+ }
328+ SYS_COMPUTER_MAX_ENERGY -> {
329+ return intr.responseValue((machine.node() as Connector ).globalBufferSize())
330+ }
331+ SYS_COMPUTER_GET_ARCHITECTURES -> {
332+ val architectures = ArrayList <Class <out Architecture >>()
333+ for (item in machine.host().internalComponents()) {
334+ val driver = Driver .driverFor(item)
335+ when (driver) {
336+ is MutableProcessor -> architectures.addAll(driver.allArchitectures())
337+ is Processor -> architectures.add(driver.architecture(item))
338+ }
339+ }
340+
341+ val names = ArrayList <String >()
342+ for (architecture in architectures) {
343+ val name = MachineAPI .getArchitectureName(architecture)
344+ names.add(name)
345+ }
346+
347+ return intr.responseValue(names)
348+ }
349+ SYS_COMPUTER_GET_ARCHITECTURE -> {
350+ val architectures = ArrayList <Class <out Architecture >>()
351+ for (item in machine.host().internalComponents()) {
352+ val driver = Driver .driverFor(item)
353+ when (driver) {
354+ is Processor -> architectures.add(driver.architecture(item))
355+ }
356+ }
357+
358+ val architecture = architectures.firstOrNull()
359+ ? : return intr.responseNone()
360+
361+ val name = MachineAPI .getArchitectureName(architecture)
362+ return intr.responseValue(name)
363+ }
364+ SYS_COMPUTER_SET_ARCHITECTURE -> {
365+ val archName = intr.readString()
366+ var itemStack: ItemStack ? = null
367+ var processor: MutableProcessor ? = null
368+ for (item in machine.host().internalComponents()) {
369+ val driver = Driver .driverFor(item)
370+ when (driver) {
371+ is MutableProcessor -> {
372+ itemStack = item
373+ processor = driver
374+ }
375+ }
376+ }
377+
378+ if (processor == null )
379+ return intr.responseError(Exception (" missing processor" ))
380+
381+ val archClass = processor.allArchitectures().find {
382+ MachineAPI .getArchitectureName(it) == archName
383+ } ? : return intr.responseError(Exception (" unknown architecture" ))
384+
385+ if (archClass != processor.architecture(itemStack!! )) {
386+ processor.setArchitecture(itemStack, archClass)
387+ return intr.responseValue(true );
388+ } else {
389+ return intr.responseValue(false );
390+ }
391+ }
301392 else -> throw UnknownInterrupt ()
302393 }
303394 }
@@ -315,6 +406,7 @@ class OpenPieInterruptHandler(val vm: OpenPieVirtualMachine) {
315406 SYS_TIMER_TICKS_MS -> System .currentTimeMillis().toInt()
316407 SYS_TIMER_TICKS_US -> System .nanoTime().toInt()
317408 SYS_TIMER_TICKS_CPU -> intr.cpu.totalInsnCount.toInt()
409+ SYS_TIMER_REAL_TIME -> intr.responseValue(System .currentTimeMillis() / 1000 )
318410 SYS_TIMER_WORLD_TIME -> intr.responseValue(machine.worldTime())
319411 SYS_TIMER_UP_TIME -> intr.responseValue(machine.upTime())
320412 SYS_TIMER_CPU_TIME -> intr.responseValue(machine.cpuTime())
0 commit comments