def libnative_libfuncs_open_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, name="my.library", id_string="bla", neg_size=36) lib.new_lib() # setup open func def open_func(op, pc): # return my seglist cpu.w_reg(REG_D0, 0xCAFEBABE) trap_id = traps.setup(open_func, auto_rts=True) exp_addr = lib.get_addr() - 6 mem.w16(exp_addr, trap_id | 0xA000) # add lib lf = LibFuncs(machine, alloc) lib_base = lf.open_library(lib.get_addr(), run_sp=sp) assert lib_base == 0xCAFEBABE # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_close_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc) # new lib lib = Library.alloc(alloc, name="my.library", id_string="bla", neg_size=36) lib.new_lib() # setup seglist seglist = SegList.alloc(alloc, [64]) segloader.register_seglist(seglist.get_baddr()) # setup expunge func def close_func(op, pc): # return my seglist cpu.w_reg(REG_D0, seglist.get_baddr()) trap_id = traps.setup(close_func, auto_rts=True) exp_addr = lib.get_addr() - 12 mem.w16(exp_addr, trap_id | 0xA000) # add lib lf = LibFuncs(machine, alloc) sl = lf.close_library(lib.get_addr(), segloader, run_sp=sp) assert seglist.get_baddr() == sl # cleanup lib.free() assert alloc.is_all_free() assert segloader.shutdown() == 0
def libnative_libfuncs_set_function_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, name="my.library", id_string="bla", neg_size=36) lib_addr = lib.get_addr() lib.new_lib() lib.fill_funcs(op_jmp, 0xCAFEBABE) assert lib.neg_size.val == 36 # patch function lvo = -30 addr = lib.get_addr() + lvo assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xCAFEBABE lf = LibFuncs(machine, alloc) old_addr = lf.set_function(lib_addr, lvo, 0xDEADBEEF) assert old_addr == 0xCAFEBABE assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xDEADBEEF assert lib.check_sum() # invalid function old_addr = lf.set_function(lib_addr, -36, 0) assert old_addr is None # cleanup lib.free() assert alloc.is_all_free()
def libnative_initres_init_test(buildlibnix): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() alloc = MemoryAlloc.for_machine(machine) init_addr = machine.get_ram_begin() - 4 sp = init_addr - 4 # load lib seglist, addr, size, end = load_lib(alloc, buildlibnix) # setup init func def init_func(op, pc): assert cpu.r_reg(REG_A0) == seglist.get_baddr() # return my lib_base cpu.w_reg(REG_D0, 0xcafebabe) trap_id = traps.setup(init_func, auto_rts=True) mem.w16(init_addr, trap_id | 0xa000) # build fake resident res = Resident.alloc(alloc, "bla.library", "blub") res.setup(flags=0, version=42, type=NodeType.NT_LIBRARY, pri=-7, init=init_addr) # init resident ir = InitRes(machine, alloc) lib_base, mem_obj = ir.init_resident( res.get_addr(), seglist.get_baddr(), run_sp=sp) assert lib_base == 0xcafebabe assert mem_obj is None seglist.free() res.free() traps.free(trap_id) assert alloc.is_all_free()
def libnative_libfuncs_close_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup seglist seglist = SegList.alloc(alloc, [64]) segloader.register_seglist(seglist.get_baddr()) # setup expunge func def close_func(op, pc): # return my seglist cpu.w_reg(REG_D0, seglist.get_baddr()) trap_id = traps.setup(close_func, auto_rts=True) exp_addr = lib.get_addr() - 12 mem.w16(exp_addr, trap_id | 0xa000) # add lib lf = LibFuncs(machine, alloc) sl = lf.close_library(lib.get_addr(), segloader, run_sp=sp) assert seglist.get_baddr() == sl # cleanup lib.free() assert alloc.is_all_free() assert segloader.shutdown() == 0
def create_machine(cpu_type=Machine.CPU_TYPE_68000): m = Machine(cpu_type, raise_on_main_run=False) cpu = m.get_cpu() mem = m.get_mem() code = m.get_ram_begin() stack = m.get_scratch_top() return m, cpu, mem, code, stack
def libnative_libfuncs_open_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup open func def open_func(op, pc): # return my seglist cpu.w_reg(REG_D0, 0xcafebabe) trap_id = traps.setup(open_func, auto_rts=True) exp_addr = lib.get_addr() - 6 mem.w16(exp_addr, trap_id | 0xa000) # add lib lf = LibFuncs(machine, alloc) lib_base = lf.open_library(lib.get_addr(), run_sp=sp) assert lib_base == 0xcafebabe # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_set_function_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib_addr = lib.get_addr() lib.setup() lib.fill_funcs(op_jmp, 0xcafebabe) assert lib.neg_size == 36 # patch function lvo = -30 addr = lib.get_addr() + lvo assert mem.r16(addr) == op_jmp assert mem.r32(addr+2) == 0xcafebabe lf = LibFuncs(machine, alloc) old_addr = lf.set_function(lib_addr, lvo, 0xdeadbeef) assert old_addr == 0xcafebabe assert mem.r16(addr) == op_jmp assert mem.r32(addr+2) == 0xdeadbeef assert lib.check_sum() # invalid function old_addr = lf.set_function(lib_addr, -36, 0) assert old_addr is None # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_set_function_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib_addr = lib.get_addr() lib.setup() lib.fill_funcs(op_jmp, 0xcafebabe) assert lib.neg_size == 36 # patch function lvo = -30 addr = lib.get_addr() + lvo assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xcafebabe lf = LibFuncs(machine, alloc) old_addr = lf.set_function(lib_addr, lvo, 0xdeadbeef) assert old_addr == 0xcafebabe assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xdeadbeef assert lib.check_sum() # invalid function old_addr = lf.set_function(lib_addr, -36, 0) assert old_addr is None # cleanup lib.free() assert alloc.is_all_free()
def create_machine(cpu_type=Machine.CPU_TYPE_68000): m = Machine(cpu_type, raise_on_main_run=False) cpu = m.get_cpu() mem = m.get_mem() code = m.get_ram_begin() stack = m.get_scratch_top() return m, cpu, mem, code, stack
def create_machine(): m = Machine(Machine.CPU_TYPE_68000, raise_on_main_run=False) cpu = m.get_cpu() mem = m.get_mem() traps = m.get_traps() code = m.get_ram_begin() stack = code + 0x1000 return m, cpu, mem, traps, code, stack
def setup(profiler_cfg=None): machine = Machine() alloc = MemoryAlloc(machine.get_mem(), machine.get_ram_begin()) mgr = VLibManager(machine, alloc, profiler_cfg=profiler_cfg) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() segloader = SegmentLoader(alloc) exec_ctx = ExecLibCtx(machine, alloc, segloader, None) mgr.add_ctx('exec.library', exec_ctx) return machine, alloc, mgr
def libnative_initres_autoinit_test(buildlibnix): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() alloc = MemoryAlloc.for_machine(machine) init_addr = machine.get_ram_begin() - 4 sp = init_addr - 4 # load lib seglist, addr, size, end = load_lib(alloc, buildlibnix) # setup init func def init_func(op, pc): assert cpu.r_reg(REG_A0) == seglist.get_baddr() trap_id = traps.setup(init_func, auto_rts=True) mem.w16(init_addr, trap_id | 0xA000) # fake vectors vectors = 0x100 mem.w32(vectors, 0x400) mem.w32(vectors + 4, 0x600) mem.w32(vectors + 8, 0x800) mem.w32(vectors + 12, 0xFFFFFFFF) # build fake resident res = Resident.alloc(alloc, "bla.library", "blub") res.setup(flags=ResidentFlags.RTF_AUTOINIT, version=42, type=NodeType.NT_LIBRARY, pri=-7) auto_init = AutoInit.alloc(alloc) auto_init.setup(functions=vectors, init_func=init_addr) res.set_auto_init(auto_init) # setup exec lib exec_lib = ExecLibrary.alloc(alloc, "exec.library", "bla", 36) exec_lib.setup() mem.w32(4, exec_lib.get_addr()) # init resident ir = InitRes(machine, alloc) lib_base, mem_obj = ir.init_resident(res.get_addr(), seglist.get_baddr(), run_sp=sp, exec_lib=exec_lib) assert lib_base assert mem_obj seglist.free() res.free() auto_init.free() alloc.free_memory(mem_obj) exec_lib.free() traps.free(trap_id) assert alloc.is_all_free()
def machine_disasm_default_test(): mach = Machine() disasm = DisAsm(mach) mem = mach.get_mem() cpu = mach.get_cpu() traps = mach.get_traps() # trap without func mem.w16(0, 0xa123) assert disasm.disassemble(0) == (2, "PyTrap #$123") # trap with func def bla(opcode, pc): pass tid = traps.setup(bla) mem.w16(2, 0xa000 | tid) assert disasm.disassemble(2) == (2, "PyTrap #$%03x ; bla" % tid) traps.free(tid)
def setup(main_profiler=None, prof_names=None, prof_calls=False): machine = Machine() alloc = MemoryAlloc(machine.get_mem(), machine.get_ram_begin()) mgr = VLibManager(machine, alloc, main_profiler=main_profiler, prof_names=prof_names, prof_calls=prof_calls) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() segloader = SegmentLoader(alloc) exec_ctx = ExecLibCtx(machine, alloc, segloader, None) mgr.add_ctx('exec.library', exec_ctx) mgr.add_impl_cls('exec.library', ExecLibrary) mgr.add_impl_cls('vamostest.library', VamosTestLibrary) mgr.add_impl_cls('vamostestdev.device', VamosTestDevice) return machine, alloc, mgr
def libnative_initres_autoinit_test(buildlibnix): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() alloc = MemoryAlloc.for_machine(machine) init_addr = machine.get_ram_begin() - 4 sp = init_addr - 4 # load lib seglist, addr, size, end = load_lib(alloc, buildlibnix) # setup init func def init_func(op, pc): assert cpu.r_reg(REG_A0) == seglist.get_baddr() trap_id = traps.setup(init_func, auto_rts=True) mem.w16(init_addr, trap_id | 0xa000) # fake vectors vectors = 0x100 mem.w32(vectors, 0x400) mem.w32(vectors+4, 0x600) mem.w32(vectors+8, 0x800) mem.w32(vectors+12, 0xffffffff) # build fake resident res = Resident.alloc(alloc, "bla.library", "blub") res.setup(flags=ResidentFlags.RTF_AUTOINIT, version=42, type=NodeType.NT_LIBRARY, pri=-7) auto_init = AutoInit.alloc(alloc) auto_init.setup(functions=vectors, init_func=init_addr) res.set_auto_init(auto_init) # setup exec lib exec_lib = ExecLibrary.alloc(alloc, "exec.library", "bla", 36) exec_lib.setup() mem.w32(4, exec_lib.get_addr()) # init resident ir = InitRes(machine, alloc) lib_base, mem_obj = ir.init_resident( res.get_addr(), seglist.get_baddr(), run_sp=sp, exec_lib=exec_lib) assert lib_base assert mem_obj seglist.free() res.free() auto_init.free() alloc.free_memory(mem_obj) exec_lib.free() traps.free(trap_id) assert alloc.is_all_free()
def setup(main_profiler=None, prof_names=None, prof_calls=False): machine = Machine() alloc = MemoryAlloc(machine.get_mem(), machine.get_ram_begin()) mgr = VLibManager(machine, alloc, main_profiler=main_profiler, prof_names=prof_names, prof_calls=prof_calls) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() segloader = SegmentLoader(alloc) exec_ctx = ExecLibCtx(machine, alloc, segloader, None) mgr.add_ctx('exec.library', exec_ctx) mgr.add_impl_cls('exec.library', ExecLibrary) mgr.add_impl_cls('vamostest.library', VamosTestLibrary) mgr.add_impl_cls('vamostestdev.device', VamosTestDevice) return machine, alloc, mgr
def machine_disasm_default_test(): mach = Machine() disasm = DisAsm(mach) mem = mach.get_mem() cpu = mach.get_cpu() traps = mach.get_traps() # trap without func mem.w16(0, 0xa123) assert disasm.disassemble(0) == (2, "PyTrap #$123") # trap with func def bla(opcode, pc): pass tid = traps.setup(bla) mem.w16(2, 0xa000 | tid) assert disasm.disassemble(2) == (2, "PyTrap #$%03x ; bla" % tid) traps.free(tid)
def setup(path_mgr=None, cfg=None, profiler_cfg=None): log_libmgr.setLevel(logging.INFO) log_exec.setLevel(logging.INFO) machine = Machine() # machine.show_instr(True) sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc, path_mgr) mgr = LibManager(machine, alloc, segloader, cfg=cfg, profiler_cfg=profiler_cfg) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() exec_ctx = ExecLibCtx(machine, alloc, segloader, path_mgr) mgr.add_ctx('exec.library', exec_ctx) dos_ctx = DosLibCtx(machine, alloc, segloader, path_mgr, None, None) mgr.add_ctx('dos.library', dos_ctx) return machine, alloc, mgr, sp
def setup(path_mgr=None): log_libmgr.setLevel(logging.INFO) log_exec.setLevel(logging.INFO) machine = Machine() # machine.show_instr(True) sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc, path_mgr) cfg = LibMgrCfg() mgr = LibManager(machine, alloc, segloader, cfg) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() exec_ctx = ExecLibCtx(machine, alloc, segloader, path_mgr) mgr.add_ctx('exec.library', exec_ctx) mgr.add_impl_cls('exec.library', ExecLibrary) dos_ctx = DosLibCtx(machine, alloc, segloader, path_mgr, None, None) mgr.add_ctx('dos.library', dos_ctx) mgr.add_impl_cls('dos.library', DosLibrary) mgr.add_impl_cls('vamostest.library', VamosTestLibrary) mgr.add_impl_cls('vamostestdev.device', VamosTestDevice) return machine, alloc, mgr, sp, cfg
def setup_env(main_profiler=None, prof_names=None, prof_calls=False): machine = Machine() alloc = MemoryAlloc(machine.get_mem(), machine.get_ram_begin()) mgr = VLibManager( machine, alloc, main_profiler=main_profiler, prof_names=prof_names, prof_calls=prof_calls, ) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() segloader = SegmentLoader(alloc) exec_ctx = ExecLibCtx(machine, alloc, segloader, None, None) # add extra attr to ctx mgr.set_ctx_extra_attr("foo", "bar") mgr.add_ctx("exec.library", exec_ctx) mgr.add_impl_cls("exec.library", ExecLibrary) mgr.add_impl_cls("vamostest.library", VamosTestLibrary) mgr.add_impl_cls("vamostestdev.device", VamosTestDevice) return machine, alloc, mgr
def setup(path_mgr=None): log_libmgr.setLevel(logging.INFO) log_exec.setLevel(logging.INFO) machine = Machine() # machine.show_instr(True) sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc, path_mgr) cfg = LibMgrCfg() mgr = LibManager(machine, alloc, segloader, cfg) # setup ctx map cpu = machine.get_cpu() mem = machine.get_mem() cpu_type = machine.get_cpu_type() exec_ctx = ExecLibCtx(machine, alloc, segloader, path_mgr, mgr) mgr.add_ctx("exec.library", exec_ctx) mgr.add_impl_cls("exec.library", ExecLibrary) dos_ctx = DosLibCtx(machine, alloc, segloader, path_mgr, None, None) mgr.add_ctx("dos.library", dos_ctx) mgr.add_impl_cls("dos.library", DosLibrary) mgr.add_impl_cls("vamostest.library", VamosTestLibrary) mgr.add_impl_cls("vamostestdev.device", VamosTestDevice) return machine, alloc, mgr, sp, cfg
def libnative_initres_init_test(buildlibnix): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() alloc = MemoryAlloc.for_machine(machine) init_addr = machine.get_ram_begin() - 4 sp = init_addr - 4 # load lib seglist, addr, size, end = load_lib(alloc, buildlibnix) # setup init func def init_func(op, pc): assert cpu.r_reg(REG_A0) == seglist.get_baddr() # return my lib_base cpu.w_reg(REG_D0, 0xCAFEBABE) trap_id = traps.setup(init_func, auto_rts=True) mem.w16(init_addr, trap_id | 0xA000) # build fake resident res = Resident.alloc(alloc, "bla.library", "blub") res.setup(flags=0, version=42, type=NodeType.NT_LIBRARY, pri=-7, init=init_addr) # init resident ir = InitRes(machine, alloc) lib_base, mem_obj = ir.init_resident(res.get_addr(), seglist.get_baddr(), run_sp=sp) assert lib_base == 0xCAFEBABE assert mem_obj is None seglist.free() res.free() traps.free(trap_id) assert alloc.is_all_free()