comparison backend_x86.c @ 1465:5d41d0574863

Preserve original address when retranslating instructions instead of switching to the lowest alias
author Michael Pavone <pavone@retrodev.com>
date Wed, 13 Sep 2017 21:06:25 -0700
parents 494234e7e88f
children 2a5649a767e7
comparison
equal deleted inserted replaced
1464:ffe45c5b8390 1465:5d41d0574863
1 #include "backend.h" 1 #include "backend.h"
2 #include "gen_x86.h" 2 #include "gen_x86.h"
3 #include <string.h>
3 4
4 void cycles(cpu_options *opts, uint32_t num) 5 void cycles(cpu_options *opts, uint32_t num)
5 { 6 {
6 if (opts->limit < 0) { 7 if (opts->limit < 0) {
7 sub_ir(&opts->code, num*opts->clock_divider, opts->cycles, SZ_D); 8 sub_ir(&opts->code, num*opts->clock_divider, opts->cycles, SZ_D);
24 code_ptr jmp_off = code->cur+1; 25 code_ptr jmp_off = code->cur+1;
25 jcc(code, cc, jmp_off+1); 26 jcc(code, cc, jmp_off+1);
26 mov_ir(code, address, opts->scratch1, SZ_D); 27 mov_ir(code, address, opts->scratch1, SZ_D);
27 call(code, opts->handle_cycle_limit_int); 28 call(code, opts->handle_cycle_limit_int);
28 *jmp_off = code->cur - (jmp_off+1); 29 *jmp_off = code->cur - (jmp_off+1);
30 }
31
32 void retranslate_calc(cpu_options *opts)
33 {
34 code_info *code = &opts->code;
35 code_info tmp = *code;
36 uint8_t cc;
37 if (opts->limit < 0) {
38 cmp_ir(code, 1, opts->cycles, SZ_D);
39 cc = CC_NS;
40 } else {
41 cmp_rr(code, opts->cycles, opts->limit, SZ_D);
42 cc = CC_A;
43 }
44 jcc(code, cc, code->cur+2);
45 opts->move_pc_off = code->cur - tmp.cur;
46 mov_ir(code, 0x1234, opts->scratch1, SZ_D);
47 opts->move_pc_size = code->cur - tmp.cur - opts->move_pc_off;
48 *code = tmp;
49 }
50
51 void patch_for_retranslate(cpu_options *opts, code_ptr native_address, code_ptr handler)
52 {
53 if (!is_mov_ir(native_address)) {
54 //instruction is not already patched for either retranslation or a breakpoint
55 //copy original mov_ir instruction containing PC to beginning of native code area
56 memmove(native_address, native_address + opts->move_pc_off, opts->move_pc_size);
57 }
58 //jump to the retranslation handler
59 code_info tmp = {
60 .cur = native_address + opts->move_pc_size,
61 .last = native_address + 256,
62 .stack_off = 0
63 };
64 jmp(&tmp, handler);
29 } 65 }
30 66
31 void check_cycles(cpu_options * opts) 67 void check_cycles(cpu_options * opts)
32 { 68 {
33 code_info *code = &opts->code; 69 code_info *code = &opts->code;