|
| 1 | +# |
| 2 | +# License: See LICENSE.md file |
| 3 | +# GitHub: https://github.com/Baekalfen/PyBoy |
| 4 | +# |
| 5 | + |
| 6 | +from posix cimport dlfcn |
| 7 | + |
| 8 | +cimport cython |
| 9 | +from libc.stdint cimport int64_t, uint8_t, uint16_t, uint32_t, uint64_t |
| 10 | + |
| 11 | +cimport pyboy |
| 12 | +cimport pyboy.core.cartridge.base_mbc |
| 13 | +cimport pyboy.core.cpu |
| 14 | +from pyboy.logging.logging cimport Logger |
| 15 | + |
| 16 | +from . cimport opcodes |
| 17 | + |
| 18 | + |
| 19 | +cdef Logger logger |
| 20 | +ctypedef int(*f_type)(pyboy.core.cpu.CPU, int64_t) noexcept nogil |
| 21 | + |
| 22 | +cdef class JIT: |
| 23 | + cdef pyboy.core.cpu.CPU cpu |
| 24 | + cdef pyboy.core.cartridge.base_mbc.BaseMBC cartridge |
| 25 | + cdef dict queue |
| 26 | + cdef bint thread_stop |
| 27 | + cdef object thread_queue |
| 28 | + cdef object thread |
| 29 | + |
| 30 | + cdef f_type[0xFFFFFF] array |
| 31 | + cdef int[0xFFFFFF] cycles |
| 32 | + |
| 33 | + cdef inline int load(self, str module_name, str module_path, str file_base, list block_manifest) except -1 with gil: |
| 34 | + # logger.debug("JIT LOAD %d", block_id) |
| 35 | + cdef void* handle = dlfcn.dlopen(module_path.encode(), dlfcn.RTLD_NOW | dlfcn.RTLD_GLOBAL) # RTLD_LAZY? |
| 36 | + if (handle == NULL): |
| 37 | + return -1 |
| 38 | + dlfcn.dlerror() # Clear error |
| 39 | + |
| 40 | + cdef f_type execute |
| 41 | + for func_name, block_id, block_max_cycles in block_manifest: |
| 42 | + execute = <f_type> dlfcn.dlsym(handle, func_name.encode()) |
| 43 | + if (execute == NULL): |
| 44 | + print(dlfcn.dlerror()) |
| 45 | + |
| 46 | + # block_id = int(func_name.split('_')[-1], 16) |
| 47 | + |
| 48 | + self.array[block_id] = execute |
| 49 | + self.cycles[block_id] = block_max_cycles |
| 50 | + |
| 51 | + cdef inline int execute(self, int block_id, int64_t cycles_target) noexcept nogil: |
| 52 | + # logger.debug("JIT EXECUTE %d", block_id) |
| 53 | + return self.array[block_id](self.cpu, cycles_target) |
| 54 | + |
| 55 | + cdef void stop(self) noexcept with gil |
| 56 | + |
| 57 | + cdef uint8_t getitem_bank(self, uint8_t, uint16_t) noexcept nogil |
| 58 | + |
| 59 | + cdef void _jit_clear(self) noexcept with gil |
| 60 | + cdef tuple get_module_name(self, str) with gil |
| 61 | + cdef void gen_files(self, str, str, list) noexcept with gil |
| 62 | + cdef void compile(self, str, str, str) noexcept with gil |
| 63 | + cdef object emit_code(self, object, str) with gil |
| 64 | + # @cython.locals(block_max_cycles=int64_t) |
| 65 | + # cdef bint analyze(self, int, int64_t, bint) noexcept with gil |
| 66 | + cdef void offload(self, int, int64_t, bint) noexcept with gil |
| 67 | + @cython.locals(block_id=int64_t, cycles_target=int64_t, interrupt_master_enable=bint, count=int64_t) |
| 68 | + cdef void process(self) noexcept with gil |
| 69 | + |
| 70 | +cpdef void threaded_processor(JIT) noexcept with gil |
0 commit comments