diff 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
line wrap: on
line diff
--- a/backend_x86.c	Fri Sep 08 00:38:22 2017 -0700
+++ b/backend_x86.c	Wed Sep 13 21:06:25 2017 -0700
@@ -1,5 +1,6 @@
 #include "backend.h"
 #include "gen_x86.h"
+#include <string.h>
 
 void cycles(cpu_options *opts, uint32_t num)
 {
@@ -28,6 +29,41 @@
 	*jmp_off = code->cur - (jmp_off+1);
 }
 
+void retranslate_calc(cpu_options *opts)
+{
+	code_info *code = &opts->code;
+	code_info tmp = *code;
+	uint8_t cc;
+	if (opts->limit < 0) {
+		cmp_ir(code, 1, opts->cycles, SZ_D);
+		cc = CC_NS;
+	} else {
+		cmp_rr(code, opts->cycles, opts->limit, SZ_D);
+		cc = CC_A;
+	}
+	jcc(code, cc, code->cur+2);
+	opts->move_pc_off = code->cur - tmp.cur;
+	mov_ir(code, 0x1234, opts->scratch1, SZ_D);
+	opts->move_pc_size = code->cur - tmp.cur - opts->move_pc_off;
+	*code = tmp;
+}
+
+void patch_for_retranslate(cpu_options *opts, code_ptr native_address, code_ptr handler)
+{
+	if (!is_mov_ir(native_address)) {
+		//instruction is not already patched for either retranslation or a breakpoint
+		//copy original mov_ir instruction containing PC to beginning of native code area
+		memmove(native_address, native_address + opts->move_pc_off, opts->move_pc_size);
+	}
+	//jump to the retranslation handler
+	code_info tmp = {
+		.cur =  native_address + opts->move_pc_size,
+		.last = native_address + 256,
+		.stack_off = 0
+	};
+	jmp(&tmp, handler);
+}
+
 void check_cycles(cpu_options * opts)
 {
 	code_info *code = &opts->code;