# HG changeset patch # User Mike Pavone # Date 1358229414 28800 # Node ID 1db07e112bf74f22922eb143506fd278b714bf65 # Parent 1b4d856b067a0fc8f53a2e48af5720f9472709ce Prep work for handling games that modify code in RAM diff -r 1b4d856b067a -r 1db07e112bf7 m68k_to_x86.c --- a/m68k_to_x86.c Mon Jan 14 20:23:17 2013 -0800 +++ b/m68k_to_x86.c Mon Jan 14 21:56:54 2013 -0800 @@ -730,9 +730,22 @@ } } -void map_native_address(native_map_slot * native_code_map, uint32_t address, uint8_t * native_addr) +void map_native_address(m68k_context * context, uint32_t address, uint8_t * native_addr, uint8_t size, uint8_t native_size) { + native_map_slot * native_code_map = context->native_code_map; + x86_68k_options * opts = context->options; address &= 0xFFFFFF; + if (address > 0xE00000) { + context->ram_code_flags[(address & 0xC000) >> 14] |= 1 << ((address & 0x3800) >> 11); + if (((address & 0x3FFF) + size) & 0xC000) { + context->ram_code_flags[((address+size) & 0xC000) >> 14] |= 1 << (((address+size) & 0x3800) >> 11); + } + uint32_t slot = (address & 0xFFFF)/1024; + if (!opts->ram_inst_sizes[slot]) { + opts->ram_inst_sizes[slot] = malloc(sizeof(uint8_t) * 512); + } + opts->ram_inst_sizes[slot][((address & 0xFFFF)/2)%512] = native_size; + } address/= 2; uint32_t chunk = address / NATIVE_CHUNK_SIZE; if (!native_code_map[chunk].base) { @@ -2595,7 +2608,6 @@ { uint8_t * end_off, *zero_off, *norm_off; uint8_t dst_reg; - map_native_address(opts->native_code_map, inst->address, dst); dst = check_cycles_int(dst, inst->address); if (inst->op == M68K_MOVE) { return translate_m68k_move(dst, inst, opts); @@ -3622,11 +3634,14 @@ break; } next = m68k_decode(encoded, &instbuf, address); - address += (next-encoded)*2; + uint16_t m68k_size = (next-encoded)*2; + address += m68k_size; encoded = next; //m68k_disasm(&instbuf, disbuf); //printf("%X: %s\n", instbuf.address, disbuf); - dst = translate_m68k(dst, &instbuf, opts); + uint8_t * after = translate_m68k(dst, &instbuf, opts); + map_native_address(context, instbuf.address, dst, m68k_size, after-dst); + dst = after; } while(instbuf.op != M68K_ILLEGAL && instbuf.op != M68K_INVALID && instbuf.op != M68K_TRAP && instbuf.op != M68K_RTS && instbuf.op != M68K_RTR && instbuf.op != M68K_RTE && !(instbuf.op == M68K_BCC && instbuf.extra.cond == COND_TRUE) && instbuf.op != M68K_JMP); process_deferred(opts); if (opts->deferred) { @@ -3744,6 +3759,7 @@ size_t size = 1024 * 1024; opts->cur_code = alloc_code(&size); opts->code_end = opts->cur_code + size; + opts->ram_inst_sizes = malloc(sizeof(uint8_t *) * 64); } void init_68k_context(m68k_context * context, native_map_slot * native_code_map, void * opts) diff -r 1b4d856b067a -r 1db07e112bf7 m68k_to_x86.h --- a/m68k_to_x86.h Mon Jan 14 20:23:17 2013 -0800 +++ b/m68k_to_x86.h Mon Jan 14 21:56:54 2013 -0800 @@ -27,6 +27,7 @@ deferred_addr *deferred; uint8_t *cur_code; uint8_t *code_end; + uint8_t **ram_inst_sizes; } x86_68k_options; typedef struct { @@ -46,6 +47,7 @@ native_map_slot *native_code_map; void *options; + uint8_t ram_code_flags[32/8]; } m68k_context; uint8_t * translate_m68k(uint8_t * dst, m68kinst * inst, x86_68k_options * opts);