From aad81ad98ff18d464ce5faa9cb535cbfa3ae3058 Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 13 Oct 2009 10:07:27 +0000 Subject: [PATCH] 32x: interpreter-wrap drc works (demos only). SVP drc refactoring. git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@812 be3aeb3a-fb24-0410-a615-afba39da0efa --- linux/Makefile | 17 ++++- linux/host_dasm.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++ linux/host_dasm.h | 2 + 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 linux/host_dasm.c create mode 100644 linux/host_dasm.h diff --git a/linux/Makefile b/linux/Makefile index 8743a13..5c77220 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -2,7 +2,8 @@ use_musashi = 1 #use_fame = 1 #use_mz80 = 1 -#use_sh2drc = 1 +use_sh2drc = 1 +drc_debug = 1 #profile = 1 #fake_in_gp2x = 1 @@ -75,6 +76,16 @@ endif # sh2 OBJS += cpu/sh2/sh2.o ifeq "$(use_sh2drc)" "1" +DEFINES += DRC_SH2 DRC_TMP +OBJS += cpu/sh2/mame/sh2pico.o +OBJS += cpu/sh2/compiler.o +OBJS += cpu/sh2/stub_x86.o +ifeq "$(drc_debug)" "1" +DEFINES += DRC_DEBUG +OBJS += cpu/sh2/mame/sh2dasm.o +OBJS += host_dasm.o +LDFLAGS += -lbfd -lopcodes +endif else OBJS += cpu/sh2/mame/sh2pico.o endif @@ -89,6 +100,7 @@ endif CFLAGS += $(addprefix -D,$(DEFINES)) vpath %.c = ../.. +vpath %.asm = ../.. DIRS = platform platform/gp2x platform/common pico pico/cd pico/pico pico/sound pico/carthw/svp \ pico/32x zlib unzip cpu cpu/musashi cpu/fame cpu/mz80 cpu/cz80 cpu/sh2/mame cpu/drc @@ -130,6 +142,9 @@ cpu/mz80/mz80.o : ../../cpu/mz80/mz80.asm .s.o: @echo ">>>" $< $(CC) $(CFLAGS) -c $< -o $@ +%.o : %.asm + @echo ">>>" $< + nasm -f elf $< -o $@ cpu/fame/famec.o : ../../cpu/fame/famec.c ../../cpu/fame/famec_opcodes.h diff --git a/linux/host_dasm.c b/linux/host_dasm.c new file mode 100644 index 0000000..38a7e47 --- /dev/null +++ b/linux/host_dasm.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include + +#include "host_dasm.h" + +extern char **g_argv; + +static struct disassemble_info di; + +/* symbols */ +static asymbol **symbols; +static long symcount; + +/* Filter out (in place) symbols that are useless for disassembly. + COUNT is the number of elements in SYMBOLS. + Return the number of useful symbols. */ +static long +remove_useless_symbols (asymbol **symbols, long count) +{ + asymbol **in_ptr = symbols, **out_ptr = symbols; + + while (--count >= 0) + { + asymbol *sym = *in_ptr++; + + if (sym->name == NULL || sym->name[0] == '\0') + continue; + if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) + continue; + if (bfd_is_und_section (sym->section) + || bfd_is_com_section (sym->section)) + continue; + if (sym->value + sym->section->vma == 0) + continue; +/* + printf("sym: %08lx %04x %08x v %08x \"%s\"\n", + (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i, + (unsigned int)sym->section->vma, sym->name); +*/ + *out_ptr++ = sym; + } + return out_ptr - symbols; +} + +static void slurp_symtab(const char *filename) +{ + bfd *abfd; + long storage; + + symcount = 0; + + abfd = bfd_openr(filename, NULL); + if (abfd == NULL) { + fprintf(stderr, "failed to open: %s\n", filename); + goto no_symbols; + } + + if (!bfd_check_format(abfd, bfd_object)) + goto no_symbols; + + if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) + goto no_symbols; + + storage = bfd_get_symtab_upper_bound(abfd); + if (storage <= 0) + goto no_symbols; + + symbols = malloc(storage); + symcount = bfd_canonicalize_symtab(abfd, symbols); + if (symcount < 0) + goto no_symbols; + + symcount = remove_useless_symbols(symbols, symcount); +// bfd_close(abfd); + return; + +no_symbols: + fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); + if (abfd != NULL) + bfd_close(abfd); +} + + +/* Like target_read_memory, but slightly different parameters. */ +static int +dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len, + struct disassemble_info *info) +{ + memcpy(myaddr, (void *)(int)memaddr, len); + return 0; +} + +static void +dis_asm_memory_error(int status, bfd_vma memaddr, + struct disassemble_info *info) +{ + fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr); +} + +static void +dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) +{ + asymbol **sptr = symbols; + int i; + + printf("%08x", (int)addr); + + for (i = 0; i < symcount; i++) { + asymbol *sym = *sptr++; + + if (addr == sym->value + sym->section->vma) { + printf(" <%s>", sym->name); + break; + } + } +} + +static int insn_printf(void *f, const char *format, ...) +{ + va_list args; + size_t n; + + va_start(args, format); + n = vprintf(format, args); + va_end(args); + + return n; +} + +static void host_dasm_init(void) +{ + slurp_symtab(g_argv[0]); + + init_disassemble_info(&di, NULL, insn_printf); + di.flavour = bfd_target_unknown_flavour; + di.memory_error_func = dis_asm_memory_error; + di.print_address_func = dis_asm_print_address; +// di.symbol_at_address_func = dis_asm_symbol_at_address; + di.read_memory_func = dis_asm_read_memory; + di.arch = bfd_arch_i386; + di.mach = bfd_mach_i386_i386_intel_syntax; + di.endian = BFD_ENDIAN_LITTLE; + disassemble_init_for_target(&di); +} + +void host_dasm(void *addr, int len) +{ + bfd_vma vma_end, vma = (bfd_vma)(int)addr; + static int init_done = 0; + + if (!init_done) { + host_dasm_init(); + init_done = 1; + } + + vma_end = vma + len; + while (vma < vma_end) { + printf(" %p ", (void *)(long)vma); + vma += print_insn_i386_intel(vma, &di); + printf("\n"); + } +} + diff --git a/linux/host_dasm.h b/linux/host_dasm.h new file mode 100644 index 0000000..bf771d7 --- /dev/null +++ b/linux/host_dasm.h @@ -0,0 +1,2 @@ +void host_dasm(void *addr, int len); + -- 2.39.5