Skip to content
This repository was archived by the owner on Apr 25, 2021. It is now read-only.

Commit 222da96

Browse files
committed
OpenPie internal/opos 파이썬 모듈 업데이트 및 추가 API 지원
1 parent d827ff9 commit 222da96

5 files changed

Lines changed: 131 additions & 33 deletions

File tree

src/main/java/kr/pe/ecmaxp/openpie/arch/OpenPieFirmware.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import kr.pe.ecmaxp.openpie.OpenPie
44
import kr.pe.ecmaxp.openpie.arch.types.Entry
55
import kr.pe.ecmaxp.thumbsf.CPU
66
import java.net.URL
7-
import java.time.Instant
87
import java.util.*
98

109
class OpenPieFirmware(val name: String = "debug") {

src/main/java/kr/pe/ecmaxp/openpie/arch/OpenPieInterruptHandler.kt

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import kr.pe.ecmaxp.openpie.arch.consts.*
44
import kr.pe.ecmaxp.openpie.arch.state.FileHandle
55
import kr.pe.ecmaxp.openpie.arch.types.Interrupt
66
import kr.pe.ecmaxp.openpie.arch.types.call.*
7-
import kr.pe.ecmaxp.thumbsf.consts.R0
87
import kr.pe.ecmaxp.thumbsf.consts.R7
98
import kr.pe.ecmaxp.thumbsf.signal.ControlPauseSignal
109
import 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
1113
import li.cil.oc.api.machine.*
1214
import li.cil.oc.api.network.Component
15+
import li.cil.oc.api.network.Connector
16+
import net.minecraft.item.ItemStack
1317
import java.io.FileNotFoundException
1418
import java.nio.charset.StandardCharsets
19+
import li.cil.oc.api.Machine as MachineAPI
1520

1621
class 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())

src/main/java/kr/pe/ecmaxp/openpie/arch/OpenPieVirtualMachine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import li.cil.oc.api.machine.ExecutionResult
1111
import li.cil.oc.api.machine.Machine
1212
import net.minecraft.nbt.NBTTagCompound
1313
import net.minecraft.nbt.NBTTagList
14-
import java.io.*
14+
import java.io.ByteArrayOutputStream
1515
import java.util.zip.GZIPInputStream
1616
import java.util.zip.GZIPOutputStream
1717

src/main/java/kr/pe/ecmaxp/openpie/arch/consts/OpenPieSystemCallTable.kt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@ const val SYS_CONTROL_INIT_COPY = SYS_CONTROL or 16
1111
const val SYS_CONTROL_INIT_ZERO = SYS_CONTROL or 17
1212

1313
const val SYS_SIGNAL = 0x030000
14-
const val SYS_SIGNAL_REQUEST = SYS_SIGNAL or 1
15-
const val SYS_SIGNAL_PENDING = SYS_SIGNAL or 2
14+
const val SYS_SIGNAL_POP = SYS_SIGNAL or 1
15+
const val SYS_SIGNAL_POP_WAKEUP = SYS_SIGNAL or 2
1616
const val SYS_SIGNAL_PUSH = SYS_SIGNAL or 3
1717

1818
const val SYS_COMPONENT = 0x040000
19-
const val SYS_COMPONENT_INVOKE = SYS_COMPONENT or 0
20-
const val SYS_COMPONENT_LIST = SYS_COMPONENT or 16
21-
const val SYS_COMPONENT_METHODS = SYS_COMPONENT or 19
22-
const val SYS_COMPONENT_DOC = SYS_COMPONENT or 20
19+
const val SYS_COMPONENT_INVOKE = SYS_COMPONENT or 1
20+
const val SYS_COMPONENT_LIST = SYS_COMPONENT or 8
21+
const val SYS_COMPONENT_TYPE = SYS_COMPONENT or 9
22+
const val SYS_COMPONENT_SLOT = SYS_COMPONENT or 10
23+
const val SYS_COMPONENT_METHODS = SYS_COMPONENT or 11
24+
const val SYS_COMPONENT_DOC = SYS_COMPONENT or 12
2325

2426
const val SYS_VALUE = 0x050000
2527
const val SYS_VALUE_INVOKE = SYS_VALUE or 1
2628
const val SYS_VALUE_CALL = SYS_VALUE or 2
2729
const val SYS_VALUE_APPLY = SYS_VALUE or 3
2830
const val SYS_VALUE_UNAPPLY = SYS_VALUE or 4
2931
const val SYS_VALUE_DISPOSE = SYS_VALUE or 5
30-
const val SYS_VALUE_METHODS = SYS_VALUE or 19
31-
const val SYS_VALUE_DOC = SYS_VALUE or 20
32+
const val SYS_VALUE_METHODS = SYS_VALUE or 11
33+
const val SYS_VALUE_DOC = SYS_VALUE or 12
3234

3335
const val SYS_COMPUTER = 0x060000
34-
const val SYS_COMPUTER_GET_COST_PER_TICK = SYS_COMPUTER or 16
35-
const val SYS_COMPUTER_LAST_ERROR = SYS_COMPUTER or 17
36-
const val SYS_COMPUTER_BEEP_1 = SYS_COMPUTER or 32
37-
const val SYS_COMPUTER_BEEP_2 = SYS_COMPUTER or 33
38-
const val SYS_COMPUTER_USERS = SYS_COMPUTER or 48
39-
const val SYS_COMPUTER_ADD_USER = SYS_COMPUTER or 49
40-
const val SYS_COMPUTER_REMOVE_USER = SYS_COMPUTER or 50
41-
const val SYS_COMPUTER_COMPUTER_ADDRESS = SYS_COMPUTER or 64
42-
const val SYS_COMPUTER_TMP_ADDRESS = SYS_COMPUTER or 65
36+
const val SYS_COMPUTER_BEEP_1 = SYS_COMPUTER or 1
37+
const val SYS_COMPUTER_BEEP_2 = SYS_COMPUTER or 2
38+
const val SYS_COMPUTER_LAST_ERROR = SYS_COMPUTER or 8
39+
const val SYS_COMPUTER_USERS = SYS_COMPUTER or 16
40+
const val SYS_COMPUTER_ADD_USER = SYS_COMPUTER or 17
41+
const val SYS_COMPUTER_REMOVE_USER = SYS_COMPUTER or 18
42+
const val SYS_COMPUTER_COMPUTER_ADDRESS = SYS_COMPUTER or 24
43+
const val SYS_COMPUTER_TMP_ADDRESS = SYS_COMPUTER or 25
44+
const val SYS_COMPUTER_ENERGY = SYS_COMPUTER or 32
45+
const val SYS_COMPUTER_MAX_ENERGY = SYS_COMPUTER or 33
46+
const val SYS_COMPUTER_GET_ARCHITECTURES = SYS_COMPUTER or 40
47+
const val SYS_COMPUTER_GET_ARCHITECTURE = SYS_COMPUTER or 41
48+
const val SYS_COMPUTER_SET_ARCHITECTURE = SYS_COMPUTER or 42
4349

4450
const val SYS_INFO = 0x070000
4551
const val SYS_INFO_VERSION = SYS_INFO or 1
@@ -49,9 +55,10 @@ const val SYS_TIMER = 0x080000
4955
const val SYS_TIMER_TICKS_MS = SYS_TIMER or 1
5056
const val SYS_TIMER_TICKS_US = SYS_TIMER or 2
5157
const val SYS_TIMER_TICKS_CPU = SYS_TIMER or 3
52-
const val SYS_TIMER_WORLD_TIME = SYS_TIMER or 16
53-
const val SYS_TIMER_UP_TIME = SYS_TIMER or 17
54-
const val SYS_TIMER_CPU_TIME = SYS_TIMER or 18
58+
const val SYS_TIMER_REAL_TIME = SYS_TIMER or 8
59+
const val SYS_TIMER_WORLD_TIME = SYS_TIMER or 9
60+
const val SYS_TIMER_UP_TIME = SYS_TIMER or 10
61+
const val SYS_TIMER_CPU_TIME = SYS_TIMER or 11
5562

5663
const val SYS_VFS = 0x090000
5764
const val SYS_VFS_OPEN = SYS_VFS or 1

src/main/resources/assets/openpie/opos/lib/component.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ucomponent import invoke, methods, doc, components as _components
1+
from ucomponent import invoke, get_methods, get_doc, get_list
22

33
__all__ = ['Component', 'get_component', 'find_components', 'components']
44

@@ -15,7 +15,7 @@ def __call__(self, *args):
1515

1616
@property
1717
def __doc__(self):
18-
return doc(self.component.address, self.name)
18+
return get_doc(self.component.address, self.name)
1919

2020
def __repr__(self):
2121
doc = self.__doc__
@@ -36,13 +36,13 @@ def __getattr__(self, name):
3636
return ComponentMethod(self, name)
3737

3838
def __dir__(self):
39-
return dir(object()) + ["address", "type"] + list(methods(self.address))
39+
return dir(object()) + ["address", "type"] + list(get_methods(self.address))
4040

4141
def __repr__(self):
4242
return "Component<{0}:{1}>".format(self.type, self.address)
4343

4444

45-
components = _components # TODO: ?
45+
components = get_list # TODO: ?
4646

4747

4848
def set_primary(compoent):
@@ -54,7 +54,7 @@ def get_component(component_type):
5454
if component:
5555
return component
5656

57-
for address in _components(component_type):
57+
for address in get_list(component_type):
5858
component = Component(address, component_type)
5959
set_primary(component)
6060
return component
@@ -67,7 +67,7 @@ def get_component(component_type):
6767

6868

6969
def find_components(component_type):
70-
return [Component(address, component_type) for address in _components(component_type)]
70+
return [Component(address, component_type) for address in get_list(component_type)]
7171

7272

7373
# alias

0 commit comments

Comments
 (0)