changeset 548:a3afee2271ce

Initial work on the x86-32 target
author Michael Pavone <pavone@retrodev.com>
date Tue, 18 Feb 2014 21:37:31 -0800
parents 3090d016c9e9
children 32da1e0d5e55
files Makefile blastem.c debug.c gdb_remote.c m68k_to_x86.c
diffstat 5 files changed, 67 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Feb 18 20:32:10 2014 -0800
+++ b/Makefile	Tue Feb 18 21:37:31 2014 -0800
@@ -3,11 +3,11 @@
 else
 LIBS=sdl glew gl
 endif
-LDFLAGS=-lm `pkg-config --libs $(LIBS)`
+LDFLAGS:=-lm $(shell pkg-config --libs $(LIBS))
 ifdef DEBUG
-CFLAGS=-ggdb -std=gnu99 `pkg-config --cflags-only-I $(LIBS)` -Wreturn-type -Werror=return-type
+CFLAGS:=-ggdb -std=gnu99 $(shell pkg-config --cflags-only-I $(LIBS)) -Wreturn-type -Werror=return-type
 else
-CFLAGS=-O2 -std=gnu99 `pkg-config --cflags-only-I $(LIBS)` -Wreturn-type -Werror=return-type
+CFLAGS:=-O2 -std=gnu99 $(shell pkg-config  --cflags-only-I $(LIBS)) -Wreturn-type -Werror=return-type
 endif
 
 ifdef PROFILE
@@ -18,16 +18,32 @@
 CFLAGS+= -DDISABLE_OPENGL
 endif
 
+ifndef CPU
+CPU:=$(shell uname -m)
+endif
+
 TRANSOBJS=gen_x86.o x86_backend.o mem.o
 M68KOBJS=68kinst.o m68k_to_x86.o runtime.o
 Z80OBJS=z80inst.o z80_to_x86.o zruntime.o
 AUDIOOBJS=ym2612.o psg.o wave.o
 CONFIGOBJS=config.o tern.o util.o
 
+MAINOBJS=blastem.o debug.o gdb_remote.o vdp.o render_sdl.o io.o $(CONFIGOBJS) gst.o $(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS)
+
+ifeq ($(CPU),x86_64)
+CFLAGS+=-DX86_64
+MAINOBJS+= $(Z80OBJS)
+else
+ifeq ($(CPU),i686)
+CFLAGS+=-DX86_32
+endif
+endif
+
+
 all : dis zdis stateview vgmplay blastem
 
-blastem : blastem.o debug.o gdb_remote.o vdp.o render_sdl.o io.o $(CONFIGOBJS) gst.o $(M68KOBJS) $(Z80OBJS) $(TRANSOBJS) $(AUDIOOBJS)
-	$(CC) -ggdb -o blastem blastem.o debug.o gdb_remote.o vdp.o render_sdl.o io.o $(CONFIGOBJS) gst.o $(M68KOBJS) $(Z80OBJS) $(TRANSOBJS) $(AUDIOOBJS) $(LDFLAGS)
+blastem : $(MAINOBJS)
+	$(CC) -ggdb -o blastem $(MAINOBJS) $(LDFLAGS)
 
 dis : dis.o 68kinst.o
 	$(CC) -o dis dis.o 68kinst.o
--- a/blastem.c	Tue Feb 18 20:32:10 2014 -0800
+++ b/blastem.c	Tue Feb 18 21:37:31 2014 -0800
@@ -182,6 +182,7 @@
 
 void sync_z80(z80_context * z_context, uint32_t mclks)
 {
+#ifdef X86_64
 	if (z80_enabled && !reset && !busreq) {
 		genesis_context * gen = z_context->system;
 		z_context->sync_cycle = mclks / MCLKS_PER_Z80;
@@ -201,7 +202,9 @@
 				dprintf("Z80 ran to cycle %d\n", z_context->current_cycle);
 			}
 		}
-	} else {
+	} else
+#endif
+	{
 		z_context->current_cycle = mclks / MCLKS_PER_Z80;
 	}
 }
@@ -469,7 +472,9 @@
 			location &= 0x7FFF;
 			if (location < 0x4000) {
 				z80_ram[location & 0x1FFF] = value;
+#ifdef X86_64
 				z80_handle_code_write(location & 0x1FFF, gen->z80);
+#endif
 			} else if (location < 0x6000) {
 				sync_sound(gen, context->current_cycle * MCLKS_PER_68K);
 				if (location & 1) {
@@ -986,7 +991,9 @@
 			insert_breakpoint(&context, pc, debugger);
 		}
 		adjust_int_cycle(gen->m68k, gen->vdp);
+#ifdef X86_64
 		gen->z80->native_pc =  z80_get_native_address_trans(gen->z80, gen->z80->pc);
+#endif
 		start_68k_context(&context, pc);
 	} else {
 		if (debugger) {
@@ -1231,8 +1238,10 @@
 
 	z80_context z_context;
 	x86_z80_options z_opts;
+#ifdef X86_64
 	init_x86_z80_opts(&z_opts);
 	init_z80_context(&z_context, &z_opts);
+#endif
 
 	z_context.system = &gen;
 	z_context.mem_pointers[0] = z80_ram;
--- a/debug.c	Tue Feb 18 20:32:10 2014 -0800
+++ b/debug.c	Tue Feb 18 21:37:31 2014 -0800
@@ -82,6 +82,8 @@
 	}
 }
 
+#ifdef X86_64
+
 void zdebugger_print(z80_context * context, char format_char, char * param)
 {
 	uint32_t value;
@@ -460,6 +462,8 @@
 	return context;
 }
 
+#endif
+
 m68k_context * debugger(m68k_context * context, uint32_t address)
 {
 	static char last_cmd[1024];
@@ -701,6 +705,7 @@
 				}
 				break;
 			}
+#ifdef X86_64
 			case 'z': {
 				genesis_context * gen = context->system;
 				//Z80 debug commands
@@ -731,6 +736,7 @@
 				}
 				break;
 			}
+#endif
 			case 'q':
 				puts("Quitting");
 				exit(0);
--- a/gdb_remote.c	Tue Feb 18 20:32:10 2014 -0800
+++ b/gdb_remote.c	Tue Feb 18 21:37:31 2014 -0800
@@ -145,7 +145,9 @@
 	} else if (address >= 0xA00000 && address < 0xA04000) {
 		z80_ram[address & 0x1FFF] = value;
 		genesis_context * gen = context->system;
+#ifdef X86_64
 		z80_handle_code_write(address & 0x1FFF, gen->z80);
+#endif
 		return;
 	} else {
 		return;
--- a/m68k_to_x86.c	Tue Feb 18 20:32:10 2014 -0800
+++ b/m68k_to_x86.c	Tue Feb 18 21:37:31 2014 -0800
@@ -15,18 +15,18 @@
 
 #define BUS 4
 #define PREDEC_PENALTY 2
+
 #define CYCLES RAX
 #define LIMIT RBP
+#define CONTEXT RSI
 #define SCRATCH1 RCX
-#define SCRATCH2 RDI
-#define CONTEXT RSI
 
-/*
-#define FLAG_N RBX
-#define FLAG_V BH
-#define FLAG_Z RDX
-#define FLAG_C DH
-*/
+#ifdef X86_64
+#define SCRATCH2 RDI
+#else
+#define SCRATCH2 RBX
+#endif
+
 enum {
 	FLAG_X,
 	FLAG_N,
@@ -4380,7 +4380,9 @@
 					dst = jcc(dst, CC_NZ, dst+2);
 					dst = call(dst, opts->save_context);
 					if (is_write) {
-						//SCRATCH2 is RDI, so no need to move it there
+						if (SCRATCH2 != RDI) {
+							dst = mov_rr(dst, SCRATCH2, RDI, SZ_D);
+						}
 						dst = mov_rr(dst, SCRATCH1, RDX, size);
 					} else {
 						dst = push_r(dst, CONTEXT);
@@ -4483,7 +4485,9 @@
 		} else if (cfun) {
 			dst = call(dst, opts->save_context);
 			if (is_write) {
-				//SCRATCH2 is RDI, so no need to move it there
+				if (SCRATCH2 != RDI) {
+					dst = mov_rr(dst, SCRATCH2, RDI, SZ_D);
+				}
 				dst = mov_rr(dst, SCRATCH1, RDX, size);
 			} else {
 				dst = push_r(dst, CONTEXT);
@@ -4536,6 +4540,7 @@
 	memset(opts, 0, sizeof(*opts));
 	for (int i = 0; i < 8; i++)
 		opts->dregs[i] = opts->aregs[i] = -1;
+#ifdef X86_64
 	opts->dregs[0] = R10;
 	opts->dregs[1] = R11;
 	opts->dregs[2] = R12;
@@ -4550,6 +4555,15 @@
 	opts->flag_regs[2] = RDX;
 	opts->flag_regs[3] = BH;
 	opts->flag_regs[4] = DH;
+#else
+	opts->dregs[0] = RDX;
+	opts->aregs[7] = RDI;
+
+	for (int i = 0; i < 5; i++)
+		opts->flag_regs[i] = -1;
+#endif
+
+
 	opts->native_code_map = malloc(sizeof(native_map_slot) * NATIVE_MAP_CHUNKS);
 	memset(opts->native_code_map, 0, sizeof(native_map_slot) * NATIVE_MAP_CHUNKS);
 	opts->deferred = NULL;
@@ -4597,6 +4611,9 @@
 	dst = retn(dst);
 
 	opts->start_context = (start_fun)dst;
+	if (SCRATCH2 != RDI) {
+		dst = mov_rr(dst, RDI, SCRATCH2, SZ_Q);
+	}
 	//save callee save registers
 	dst = push_r(dst, RBP);
 	dst = push_r(dst, R12);
@@ -4604,7 +4621,7 @@
 	dst = push_r(dst, R14);
 	dst = push_r(dst, R15);
 	dst = call(dst, opts->load_context);
-	dst = call_r(dst, RDI);
+	dst = call_r(dst, SCRATCH2);
 	dst = call(dst, opts->save_context);
 	//restore callee save registers
 	dst = pop_r(dst, R15);