comparison m68k_to_x86.c @ 192:1db07e112bf7

Prep work for handling games that modify code in RAM
author Mike Pavone <pavone@retrodev.com>
date Mon, 14 Jan 2013 21:56:54 -0800
parents 062e3aa549eb
children c66e4636f991
comparison
equal deleted inserted replaced
191:1b4d856b067a 192:1db07e112bf7
728 cur = cur->next; 728 cur = cur->next;
729 } 729 }
730 } 730 }
731 } 731 }
732 732
733 void map_native_address(native_map_slot * native_code_map, uint32_t address, uint8_t * native_addr) 733 void map_native_address(m68k_context * context, uint32_t address, uint8_t * native_addr, uint8_t size, uint8_t native_size)
734 { 734 {
735 native_map_slot * native_code_map = context->native_code_map;
736 x86_68k_options * opts = context->options;
735 address &= 0xFFFFFF; 737 address &= 0xFFFFFF;
738 if (address > 0xE00000) {
739 context->ram_code_flags[(address & 0xC000) >> 14] |= 1 << ((address & 0x3800) >> 11);
740 if (((address & 0x3FFF) + size) & 0xC000) {
741 context->ram_code_flags[((address+size) & 0xC000) >> 14] |= 1 << (((address+size) & 0x3800) >> 11);
742 }
743 uint32_t slot = (address & 0xFFFF)/1024;
744 if (!opts->ram_inst_sizes[slot]) {
745 opts->ram_inst_sizes[slot] = malloc(sizeof(uint8_t) * 512);
746 }
747 opts->ram_inst_sizes[slot][((address & 0xFFFF)/2)%512] = native_size;
748 }
736 address/= 2; 749 address/= 2;
737 uint32_t chunk = address / NATIVE_CHUNK_SIZE; 750 uint32_t chunk = address / NATIVE_CHUNK_SIZE;
738 if (!native_code_map[chunk].base) { 751 if (!native_code_map[chunk].base) {
739 native_code_map[chunk].base = native_addr; 752 native_code_map[chunk].base = native_addr;
740 native_code_map[chunk].offsets = malloc(sizeof(int32_t) * NATIVE_CHUNK_SIZE); 753 native_code_map[chunk].offsets = malloc(sizeof(int32_t) * NATIVE_CHUNK_SIZE);
2593 2606
2594 uint8_t * translate_m68k(uint8_t * dst, m68kinst * inst, x86_68k_options * opts) 2607 uint8_t * translate_m68k(uint8_t * dst, m68kinst * inst, x86_68k_options * opts)
2595 { 2608 {
2596 uint8_t * end_off, *zero_off, *norm_off; 2609 uint8_t * end_off, *zero_off, *norm_off;
2597 uint8_t dst_reg; 2610 uint8_t dst_reg;
2598 map_native_address(opts->native_code_map, inst->address, dst);
2599 dst = check_cycles_int(dst, inst->address); 2611 dst = check_cycles_int(dst, inst->address);
2600 if (inst->op == M68K_MOVE) { 2612 if (inst->op == M68K_MOVE) {
2601 return translate_m68k_move(dst, inst, opts); 2613 return translate_m68k_move(dst, inst, opts);
2602 } else if(inst->op == M68K_LEA) { 2614 } else if(inst->op == M68K_LEA) {
2603 return translate_m68k_lea(dst, inst, opts); 2615 return translate_m68k_lea(dst, inst, opts);
3620 if (existing) { 3632 if (existing) {
3621 dst = jmp(dst, existing); 3633 dst = jmp(dst, existing);
3622 break; 3634 break;
3623 } 3635 }
3624 next = m68k_decode(encoded, &instbuf, address); 3636 next = m68k_decode(encoded, &instbuf, address);
3625 address += (next-encoded)*2; 3637 uint16_t m68k_size = (next-encoded)*2;
3638 address += m68k_size;
3626 encoded = next; 3639 encoded = next;
3627 //m68k_disasm(&instbuf, disbuf); 3640 //m68k_disasm(&instbuf, disbuf);
3628 //printf("%X: %s\n", instbuf.address, disbuf); 3641 //printf("%X: %s\n", instbuf.address, disbuf);
3629 dst = translate_m68k(dst, &instbuf, opts); 3642 uint8_t * after = translate_m68k(dst, &instbuf, opts);
3643 map_native_address(context, instbuf.address, dst, m68k_size, after-dst);
3644 dst = after;
3630 } 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); 3645 } 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);
3631 process_deferred(opts); 3646 process_deferred(opts);
3632 if (opts->deferred) { 3647 if (opts->deferred) {
3633 address = opts->deferred->address; 3648 address = opts->deferred->address;
3634 if ((address & 0xFFFFFF) < 0x400000) { 3649 if ((address & 0xFFFFFF) < 0x400000) {
3742 memset(opts->native_code_map, 0, sizeof(native_map_slot) * NATIVE_MAP_CHUNKS); 3757 memset(opts->native_code_map, 0, sizeof(native_map_slot) * NATIVE_MAP_CHUNKS);
3743 opts->deferred = NULL; 3758 opts->deferred = NULL;
3744 size_t size = 1024 * 1024; 3759 size_t size = 1024 * 1024;
3745 opts->cur_code = alloc_code(&size); 3760 opts->cur_code = alloc_code(&size);
3746 opts->code_end = opts->cur_code + size; 3761 opts->code_end = opts->cur_code + size;
3762 opts->ram_inst_sizes = malloc(sizeof(uint8_t *) * 64);
3747 } 3763 }
3748 3764
3749 void init_68k_context(m68k_context * context, native_map_slot * native_code_map, void * opts) 3765 void init_68k_context(m68k_context * context, native_map_slot * native_code_map, void * opts)
3750 { 3766 {
3751 memset(context, 0, sizeof(m68k_context)); 3767 memset(context, 0, sizeof(m68k_context));