Search
lxdream.org :: lxdream/src/test/testsh4x86.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4x86.c
changeset 911:2f6ba75b84d1
prev905:4c17ebd9ef5e
next918:7ab30c886553
author nkeynes
date Fri Oct 31 02:57:59 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Declare mem_copy_* functions as FASTCALL
Split sh4_flush_store_queue into TLB/non-TLB versions, and optimize
slightly based on that
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
    38 char *option_list = "s:o:d:h";
    39 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    41 char *input_file = NULL;
    42 char *diff_file = NULL;
    43 char *output_file = NULL;
    44 gboolean sh4_starting;
    45 uint32_t start_addr = 0x8C010000;
    46 uint32_t sh4_cpu_period = 5;
    47 sh4ptr_t sh4_main_ram;
    48 FILE *in;
    50 char *inbuf;
    52 struct x86_symbol local_symbols[] = {
    53     { "_sh4_read_byte", sh4_read_byte },
    54     { "_sh4_read_word", sh4_read_word },
    55     { "_sh4_read_long", sh4_read_long },
    56     { "_sh4_write_byte", sh4_write_byte },
    57     { "_sh4_write_word", sh4_write_word },
    58     { "_sh4_write_long", sh4_write_long }
    59 };
    61 int32_t FASTCALL sh4_read_byte( uint32_t addr ) 
    62 {
    63     return *(uint8_t *)(inbuf+(addr-start_addr));
    64 }
    65 int32_t FASTCALL sh4_read_word( uint32_t addr ) 
    66 {
    67     return *(uint16_t *)(inbuf+(addr-start_addr));
    68 }
    69 int32_t FASTCALL sh4_read_long( uint32_t addr ) 
    70 {
    71     return *(uint32_t *)(inbuf+(addr-start_addr));
    72 }
    73 // Stubs
    74 gboolean sh4_execute_instruction( ) { return TRUE; }
    75 void sh4_accept_interrupt() {}
    76 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
    77 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { return TRUE; }
    78 gboolean dreamcast_is_running() { return FALSE; }
    79 int sh4_get_breakpoint( uint32_t pc ) { return 0; }
    80 void sh4_core_exit( int exit_code ){}
    81 void sh4_flush_icache(){}
    82 void event_execute() {}
    83 void TMU_run_slice( uint32_t nanos ) {}
    84 void PMM_write_control( int ctr, uint32_t val ) { }
    85 void SCIF_run_slice( uint32_t nanos ) {}
    86 void FASTCALL sh4_write_byte( uint32_t addr, uint32_t val ) {}
    87 void FASTCALL sh4_write_word( uint32_t addr, uint32_t val ) {}
    88 void FASTCALL sh4_write_long( uint32_t addr, uint32_t val ) {}
    89 void FASTCALL sh4_write_fpscr( uint32_t val ) { }
    90 void FASTCALL sh4_write_sr( uint32_t val ) { }
    91 uint32_t FASTCALL sh4_read_sr( void ) { return 0; }
    92 void FASTCALL sh4_sleep() { }
    93 void FASTCALL sh4_fsca( uint32_t angle, float *fr ) { }
    94 void FASTCALL sh4_ftrv( float *fv ) { }
    95 void FASTCALL signsat48(void) { }
    96 void sh4_switch_fr_banks() { }
    97 void FASTCALL mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
    98 gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
    99 void syscall_invoke( uint32_t val ) { }
   100 void dreamcast_stop() {} 
   101 void dreamcast_reset() {}
   102 gboolean FASTCALL sh4_raise_reset( int exc ) { return TRUE; }
   103 gboolean FASTCALL sh4_raise_exception( int exc ) { return TRUE; }
   104 gboolean FASTCALL sh4_raise_tlb_exception( int exc ) { return TRUE; }
   105 gboolean FASTCALL sh4_raise_trap( int exc ) { return TRUE; }
   106 uint32_t sh4_sleep_run_slice(uint32_t nanosecs) { return nanosecs; }
   107 gboolean gui_error_dialog( const char *fmt, ... ) { return TRUE; }
   108 struct sh4_icache_struct sh4_icache;
   110 void usage()
   111 {
   112     fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
   113     fprintf( stderr, "Options:\n");
   114     fprintf( stderr, "  -d <filename>  Diff results against contents of file\n" );
   115     fprintf( stderr, "  -h             Display this help message\n" );
   116     fprintf( stderr, "  -o <filename>  Output disassembly to file [stdout]\n" );
   117     fprintf( stderr, "  -s <addr>      Specify start address of binary [8C010000]\n" );
   118 }
   120 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
   121 {
   122     va_list ap;
   123     va_start( ap, msg );
   124     vfprintf( stderr, msg, ap );
   125     fprintf( stderr, "\n" );
   126     va_end(ap);
   127 }
   130 struct sh4_registers sh4r;
   133 int main( int argc, char *argv[] )
   134 {
   135     struct stat st;
   136     int opt;
   137     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   138 	switch( opt ) {
   139 	case 'd':
   140 	    diff_file = optarg;
   141 	    break;
   142 	case 'o':
   143 	    output_file = optarg;
   144 	    break;
   145 	case 's':
   146 	    start_addr = strtoul(optarg, NULL, 0);
   147 	    break;
   148 	case 'h':
   149 	    usage();
   150 	    exit(0);
   151 	}
   152     }
   153     if( optind < argc ) {
   154 	input_file = argv[optind++];
   155     } else {
   156 	usage();
   157 	exit(1);
   158     }
   160     in = fopen( input_file, "ro" );
   161     if( in == NULL ) {
   162 	perror( "Unable to open input file" );
   163 	exit(2);
   164     }
   165     fstat( fileno(in), &st );
   166     inbuf = malloc( st.st_size );
   167     fread( inbuf, st.st_size, 1, in );
   169     xlat_cache_init();
   170     uint32_t pc;
   171     uint8_t *buf = sh4_translate_basic_block( start_addr );
   172     uint32_t buflen = xlat_get_block_size(buf);
   173     x86_disasm_init( buf, 0x8c010000, buflen );
   174     x86_set_symtab( local_symbols, 6 );
   175     for( pc = 0x8c010000; pc < 0x8c010000 + buflen;  ) {
   176 	char buf[256];
   177 	char op[256];
   178 	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
   179 	fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
   180 	pc = pc2;
   181     }
   182     return 0;
   183 }
.