comparison m68k_to_x86.c @ 235:d9bf8e61c33c

Get Z80 core working for simple programs
author Mike Pavone <pavone@retrodev.com>
date Thu, 25 Apr 2013 21:01:11 -0700
parents 1ed81ef2a3a2
children 0bcab0475a7f
comparison
equal deleted inserted replaced
234:f456ee23d372 235:d9bf8e61c33c
689 return NULL; 689 return NULL;
690 } 690 }
691 return native_code_map[chunk].base + native_code_map[chunk].offsets[offset]; 691 return native_code_map[chunk].base + native_code_map[chunk].offsets[offset];
692 } 692 }
693 693
694 uint8_t * get_native_from_context(m68k_context * context, uint32_t address)
695 {
696 return get_native_address(context->native_code_map, address);
697 }
698
694 uint32_t get_instruction_start(native_map_slot * native_code_map, uint32_t address) 699 uint32_t get_instruction_start(native_map_slot * native_code_map, uint32_t address)
695 { 700 {
696 address &= 0xFFFFFF; 701 address &= 0xFFFFFF;
697 address /= 2; 702 address /= 2;
698 uint32_t chunk = address / NATIVE_CHUNK_SIZE; 703 uint32_t chunk = address / NATIVE_CHUNK_SIZE;
707 --address; 712 --address;
708 chunk = address / NATIVE_CHUNK_SIZE; 713 chunk = address / NATIVE_CHUNK_SIZE;
709 offset = address % NATIVE_CHUNK_SIZE; 714 offset = address % NATIVE_CHUNK_SIZE;
710 } 715 }
711 return address*2; 716 return address*2;
712 }
713
714 deferred_addr * defer_address(deferred_addr * old_head, uint32_t address, uint8_t *dest)
715 {
716 deferred_addr * new_head = malloc(sizeof(deferred_addr));
717 new_head->next = old_head;
718 new_head->address = address & 0xFFFFFF;
719 new_head->dest = dest;
720 return new_head;
721 }
722
723 void process_deferred(x86_68k_options * opts)
724 {
725 deferred_addr * cur = opts->deferred;
726 deferred_addr **last_next = &(opts->deferred);
727 while(cur)
728 {
729 uint8_t * native = get_native_address(opts->native_code_map, cur->address);
730 if (native) {
731 int32_t disp = native - (cur->dest + 4);
732 uint8_t * out = cur->dest;
733 *(out++) = disp;
734 disp >>= 8;
735 *(out++) = disp;
736 disp >>= 8;
737 *(out++) = disp;
738 disp >>= 8;
739 *out = disp;
740 *last_next = cur->next;
741 free(cur);
742 cur = *last_next;
743 } else {
744 last_next = &(cur->next);
745 cur = cur->next;
746 }
747 }
748 } 717 }
749 718
750 void map_native_address(m68k_context * context, uint32_t address, uint8_t * native_addr, uint8_t size, uint8_t native_size) 719 void map_native_address(m68k_context * context, uint32_t address, uint8_t * native_addr, uint8_t size, uint8_t native_size)
751 { 720 {
752 native_map_slot * native_code_map = context->native_code_map; 721 native_map_slot * native_code_map = context->native_code_map;
3954 //printf("%X: %s\n", instbuf.address, disbuf); 3923 //printf("%X: %s\n", instbuf.address, disbuf);
3955 uint8_t * after = translate_m68k(dst, &instbuf, opts); 3924 uint8_t * after = translate_m68k(dst, &instbuf, opts);
3956 map_native_address(context, instbuf.address, dst, m68k_size, after-dst); 3925 map_native_address(context, instbuf.address, dst, m68k_size, after-dst);
3957 dst = after; 3926 dst = after;
3958 } while(instbuf.op != M68K_ILLEGAL && instbuf.op != M68K_RESET && 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); 3927 } while(instbuf.op != M68K_ILLEGAL && instbuf.op != M68K_RESET && 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);
3959 process_deferred(opts); 3928 process_deferred(&opts->deferred, context, (native_addr_func)get_native_from_context);
3960 if (opts->deferred) { 3929 if (opts->deferred) {
3961 address = opts->deferred->address; 3930 address = opts->deferred->address;
3962 if ((address & 0xFFFFFF) < 0x400000) { 3931 if ((address & 0xFFFFFF) < 0x400000) {
3963 encoded = context->mem_pointers[0] + (address & 0xFFFFFF)/2; 3932 encoded = context->mem_pointers[0] + (address & 0xFFFFFF)/2;
3964 } else if ((address & 0xFFFFFF) > 0xE00000) { 3933 } else if ((address & 0xFFFFFF) > 0xE00000) {