Search
lxdream.org :: lxdream/src/test/testsh4x86.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4x86.c
changeset 602:a3aae8cbd1d7
prev586:2a3ba82cf243
next624:b13c97bf4071
author nkeynes
date Tue Jan 22 11:28:43 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix testsh4x86 compile
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 mmio_region mmio_region_MMU;
    31 struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
    32 int sh4_breakpoint_count = 0;
    34 #define MAX_INS_SIZE 32
    36 char *option_list = "s:o:d:h";
    37 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    39 char *input_file = NULL;
    40 char *diff_file = NULL;
    41 char *output_file = NULL;
    42 gboolean sh4_starting;
    43 uint32_t start_addr = 0x8C010000;
    44 uint32_t sh4_cpu_period = 5;
    45 sh4ptr_t sh4_main_ram;
    46 FILE *in;
    48 char *inbuf;
    50 struct x86_symbol local_symbols[] = {
    51     { "_sh4_read_byte", sh4_read_byte },
    52     { "_sh4_read_word", sh4_read_word },
    53     { "_sh4_read_long", sh4_read_long },
    54     { "_sh4_write_byte", sh4_write_byte },
    55     { "_sh4_write_word", sh4_write_word },
    56     { "_sh4_write_long", sh4_write_long }
    57 };
    59 int32_t sh4_read_byte( uint32_t addr ) 
    60 {
    61     return *(uint8_t *)(inbuf+(addr-start_addr));
    62 }
    63 int32_t sh4_read_word( uint32_t addr ) 
    64 {
    65     return *(uint16_t *)(inbuf+(addr-start_addr));
    66 }
    67 int32_t sh4_read_long( uint32_t addr ) 
    68 {
    69     return *(uint32_t *)(inbuf+(addr-start_addr));
    70 }
    71 // Stubs
    72 gboolean sh4_execute_instruction( ) { }
    73 void sh4_accept_interrupt() {}
    74 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
    75 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
    76 gboolean sh4_is_using_xlat() { return TRUE; }
    77 int sh4_get_breakpoint( uint32_t pc ) { }
    78 void event_execute() {}
    79 void TMU_run_slice( uint32_t nanos ) {}
    80 void SCIF_run_slice( uint32_t nanos ) {}
    81 void sh4_write_byte( uint32_t addr, uint32_t val ) {}
    82 void sh4_write_word( uint32_t addr, uint32_t val ) {}
    83 void sh4_write_long( uint32_t addr, uint32_t val ) {}
    84 void mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
    85 void sh4_write_sr( uint32_t val ) { }
    86 gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
    87 void syscall_invoke( uint32_t val ) { }
    88 void dreamcast_stop() {} 
    89 void dreamcast_reset() {}
    90 uint32_t sh4_read_sr( void ) { }
    91 gboolean sh4_raise_reset( int exc ) {}
    92 gboolean sh4_raise_exception( int exc ) {}
    93 gboolean sh4_raise_tlb_exception( int exc ) {}
    94 gboolean sh4_raise_trap( int exc ) {}
    95 void sh4_sleep() { }
    96 void sh4_fsca( uint32_t angle, float *fr ) { }
    97 void sh4_ftrv( float *fv, float *xmtrx ) { }
    98 void signsat48(void) { }
    99 gboolean gui_error_dialog( const char *fmt, ... ) { }
   100 struct sh4_icache_struct sh4_icache;
   102 void usage()
   103 {
   104     fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
   105     fprintf( stderr, "Options:\n");
   106     fprintf( stderr, "  -d <filename>  Diff results against contents of file\n" );
   107     fprintf( stderr, "  -h             Display this help message\n" );
   108     fprintf( stderr, "  -o <filename>  Output disassembly to file [stdout]\n" );
   109     fprintf( stderr, "  -s <addr>      Specify start address of binary [8C010000]\n" );
   110 }
   112 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
   113 {
   114     va_list ap;
   115     va_start( ap, msg );
   116     vfprintf( stderr, msg, ap );
   117     fprintf( stderr, "\n" );
   118     va_end(ap);
   119 }
   122 struct sh4_registers sh4r;
   125 int main( int argc, char *argv[] )
   126 {
   127     struct stat st;
   128     int opt;
   129     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   130 	switch( opt ) {
   131 	case 'd':
   132 	    diff_file = optarg;
   133 	    break;
   134 	case 'o':
   135 	    output_file = optarg;
   136 	    break;
   137 	case 's':
   138 	    start_addr = strtoul(optarg, NULL, 0);
   139 	    break;
   140 	case 'h':
   141 	    usage();
   142 	    exit(0);
   143 	}
   144     }
   145     if( optind < argc ) {
   146 	input_file = argv[optind++];
   147     } else {
   148 	usage();
   149 	exit(1);
   150     }
   152     in = fopen( input_file, "ro" );
   153     if( in == NULL ) {
   154 	perror( "Unable to open input file" );
   155 	exit(2);
   156     }
   157     fstat( fileno(in), &st );
   158     inbuf = malloc( st.st_size );
   159     fread( inbuf, st.st_size, 1, in );
   161     xlat_cache_init();
   162     uint32_t pc;
   163     uint8_t *buf = sh4_translate_basic_block( start_addr );
   164     uint32_t buflen = xlat_get_block_size(buf);
   165     x86_disasm_init( buf, 0x8c010000, buflen );
   166     x86_set_symtab( local_symbols, 6 );
   167     for( pc = 0x8c010000; pc < 0x8c010000 + buflen;  ) {
   168 	char buf[256];
   169 	char op[256];
   170 	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
   171 	fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
   172 	pc = pc2;
   173     }
   174 }
.