Search
lxdream.org :: lxdream/src/test/testsh4x86.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4x86.c
changeset 931:430048ea8b71
prev930:07e5b11419db
next934:3acd3b3ee6d1
author nkeynes
date Tue Dec 23 05:48:05 2008 +0000 (13 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change More refactoring and general cleanup. Most things should be working again now.
Split off cache and start real implementation, breaking save states in the process
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Test cases for the SH4 => x86 translator core. Takes as
     5  * input a binary SH4 object (and VMA), generates the
     6  * corresponding x86 code, and outputs the disassembly.
     7  *
     8  * Copyright (c) 2005 Nathan Keynes.
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License as published by
    12  * the Free Software Foundation; either version 2 of the License, or
    13  * (at your option) any later version.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  */
    21 #include <stdio.h>
    22 #include <stdarg.h>
    23 #include <getopt.h>
    24 #include <sys/stat.h>
    25 #include "x86dasm/x86dasm.h"
    26 #include "sh4/sh4trans.h"
    27 #include "sh4/sh4core.h"
    28 #include "sh4/sh4mmio.h"
    30 struct dreamcast_module sh4_module;
    31 struct mmio_region mmio_region_MMU;
    32 struct mmio_region mmio_region_PMM;
    33 struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
    34 int sh4_breakpoint_count = 0;
    36 #define MAX_INS_SIZE 32
    39 struct mem_region_fn **sh4_address_space = (void *)0x12345432;
    40 char *option_list = "s:o:d:h";
    41 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    43 char *input_file = NULL;
    44 char *diff_file = NULL;
    45 char *output_file = NULL;
    46 gboolean sh4_starting;
    47 uint32_t start_addr = 0x8C010000;
    48 uint32_t sh4_cpu_period = 5;
    49 sh4ptr_t sh4_main_ram;
    50 FILE *in;
    52 char *inbuf;
    54 struct x86_symbol local_symbols[] = {
    55     { "sh4r+128", ((char *)&sh4r)+128 },
    56     { "sh4_cpu_period", &sh4_cpu_period },
    57     { "mmu_vma_to_phys_read", mmu_vma_to_phys_read },
    58     { "mmu_vma_to_phys_write", mmu_vma_to_phys_write },
    59     { "sh4_address_space", 0x12345432 },
    60     { "sh4_write_fpscr", sh4_write_fpscr },
    61     { "sh4_write_sr", sh4_write_sr },
    62     { "sh4_read_sr", sh4_read_sr },
    63     { "sh4_sleep", sh4_sleep },
    64     { "sh4_fsca", sh4_fsca },
    65     { "sh4_ftrv", sh4_ftrv },
    66     { "sh4_switch_fr_banks", sh4_switch_fr_banks },
    67     { "sh4_execute_instruction", sh4_execute_instruction },
    68     { "signsat48", signsat48 },
    69     { "sh4_read_byte", sh4_read_byte },
    70     { "sh4_read_word", sh4_read_word },
    71     { "sh4_read_long", sh4_read_long },
    72     { "sh4_write_byte", sh4_write_byte },
    73     { "sh4_write_word", sh4_write_word },
    74     { "sh4_write_long", sh4_write_long },
    75     { "xlat_get_code_by_vma", xlat_get_code_by_vma },
    76     { "xlat_get_code", xlat_get_code }
    77 };
    79 int32_t FASTCALL sh4_read_byte( uint32_t addr ) 
    80 {
    81     return *(uint8_t *)(inbuf+(addr-start_addr));
    82 }
    83 int32_t FASTCALL sh4_read_word( uint32_t addr ) 
    84 {
    85     return *(uint16_t *)(inbuf+(addr-start_addr));
    86 }
    87 int32_t FASTCALL sh4_read_long( uint32_t addr ) 
    88 {
    89     return *(uint32_t *)(inbuf+(addr-start_addr));
    90 }
    92 // Stubs
    93 gboolean sh4_execute_instruction( ) { return TRUE; }
    94 void sh4_accept_interrupt() {}
    95 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
    96 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { return TRUE; }
    97 gboolean dreamcast_is_running() { return FALSE; }
    98 int sh4_get_breakpoint( uint32_t pc ) { return 0; }
    99 void sh4_core_exit( int exit_code ){}
   100 void sh4_flush_icache(){}
   101 void event_execute() {}
   102 void TMU_run_slice( uint32_t nanos ) {}
   103 void CCN_set_cache_control( uint32_t val ) { }
   104 void PMM_write_control( int ctr, uint32_t val ) { }
   105 void SCIF_run_slice( uint32_t nanos ) {}
   106 void FASTCALL sh4_write_byte( uint32_t addr, uint32_t val ) {}
   107 void FASTCALL sh4_write_word( uint32_t addr, uint32_t val ) {}
   108 void FASTCALL sh4_write_long( uint32_t addr, uint32_t val ) {}
   109 void FASTCALL sh4_write_fpscr( uint32_t val ) { }
   110 void FASTCALL sh4_write_sr( uint32_t val ) { }
   111 uint32_t FASTCALL sh4_read_sr( void ) { return 0; }
   112 void FASTCALL sh4_sleep() { }
   113 void FASTCALL sh4_fsca( uint32_t angle, float *fr ) { }
   114 void FASTCALL sh4_ftrv( float *fv ) { }
   115 void FASTCALL signsat48(void) { }
   116 void sh4_switch_fr_banks() { }
   117 void mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
   118 gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
   119 void syscall_invoke( uint32_t val ) { }
   120 void dreamcast_stop() {} 
   121 void dreamcast_reset() {}
   122 gboolean FASTCALL sh4_raise_reset( int exc ) { return TRUE; }
   123 gboolean FASTCALL sh4_raise_exception( int exc ) { return TRUE; }
   124 gboolean FASTCALL sh4_raise_tlb_exception( int exc ) { return TRUE; }
   125 gboolean FASTCALL sh4_raise_trap( int exc ) { return TRUE; }
   126 uint32_t sh4_sleep_run_slice(uint32_t nanosecs) { return nanosecs; }
   127 gboolean gui_error_dialog( const char *fmt, ... ) { return TRUE; }
   128 struct sh4_icache_struct sh4_icache;
   129 struct mem_region_fn mem_region_unmapped;
   131 void usage()
   132 {
   133     fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
   134     fprintf( stderr, "Options:\n");
   135     fprintf( stderr, "  -d <filename>  Diff results against contents of file\n" );
   136     fprintf( stderr, "  -h             Display this help message\n" );
   137     fprintf( stderr, "  -o <filename>  Output disassembly to file [stdout]\n" );
   138     fprintf( stderr, "  -s <addr>      Specify start address of binary [8C010000]\n" );
   139 }
   141 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
   142 {
   143     va_list ap;
   144     va_start( ap, msg );
   145     vfprintf( stderr, msg, ap );
   146     fprintf( stderr, "\n" );
   147     va_end(ap);
   148 }
   151 struct sh4_registers sh4r;
   154 int main( int argc, char *argv[] )
   155 {
   156     struct stat st;
   157     int opt;
   158     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   159 	switch( opt ) {
   160 	case 'd':
   161 	    diff_file = optarg;
   162 	    break;
   163 	case 'o':
   164 	    output_file = optarg;
   165 	    break;
   166 	case 's':
   167 	    start_addr = strtoul(optarg, NULL, 0);
   168 	    break;
   169 	case 'h':
   170 	    usage();
   171 	    exit(0);
   172 	}
   173     }
   174     if( optind < argc ) {
   175 	input_file = argv[optind++];
   176     } else {
   177 	usage();
   178 	exit(1);
   179     }
   181     mmio_region_MMU.mem = malloc(4096);
   182     memset( mmio_region_MMU.mem, 0, 4096 );
   184     ((uint32_t *)mmio_region_MMU.mem)[4] = 1;
   186     in = fopen( input_file, "ro" );
   187     if( in == NULL ) {
   188 	perror( "Unable to open input file" );
   189 	exit(2);
   190     }
   191     fstat( fileno(in), &st );
   192     inbuf = malloc( st.st_size );
   193     fread( inbuf, st.st_size, 1, in );
   194     sh4_icache.mask = 0xFFFFF000;
   195     sh4_icache.page_vma = start_addr & 0xFFFFF000;
   196     sh4_icache.page = (unsigned char *)(inbuf - (sh4_icache.page_vma&0xFFF));
   197     sh4_icache.page_ppa = start_addr & 0xFFFFF000;
   199     xlat_cache_init();
   200     uintptr_t pc;
   201     uint8_t *buf = sh4_translate_basic_block( start_addr );
   202     uint32_t buflen = xlat_get_code_size(buf);
   203     x86_disasm_init( buf, buf, buflen );
   204     x86_set_symtab( local_symbols, sizeof(local_symbols)/sizeof(struct x86_symbol) );
   205     for( pc = buf; pc < buf + buflen;  ) {
   206 	char buf[256];
   207 	char op[256];
   208 	uintptr_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
   209 	fprintf( stdout, "%08x: %s\n", pc, buf );
   210 	pc = pc2;
   211     }
   212     return 0;
   213 }
.