2 * $Id: testsh4x86.c,v 1.5 2007-09-16 07:01:35 nkeynes Exp $
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.
8 * Copyright (c) 2005 Nathan Keynes.
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.
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.
25 #include "x86dasm/x86dasm.h"
26 #include "sh4/sh4trans.h"
27 #include "sh4/sh4core.h"
29 #define MAX_INS_SIZE 32
31 char *option_list = "s:o:d:h";
32 struct option longopts[1] = { { NULL, 0, 0, 0 } };
34 char *input_file = NULL;
35 char *diff_file = NULL;
36 char *output_file = NULL;
37 uint32_t start_addr = 0x8C010000;
38 uint32_t sh4_cpu_period = 5;
43 struct x86_symbol local_symbols[] = {
44 { "_sh4_read_byte", sh4_read_byte },
45 { "_sh4_read_word", sh4_read_word },
46 { "_sh4_read_long", sh4_read_long },
47 { "_sh4_write_byte", sh4_write_byte },
48 { "_sh4_write_word", sh4_write_word },
49 { "_sh4_write_long", sh4_write_long }
52 int32_t sh4_read_byte( uint32_t addr )
54 return *(uint8_t *)(inbuf+(addr-start_addr));
56 int32_t sh4_read_word( uint32_t addr )
58 return *(uint16_t *)(inbuf+(addr-start_addr));
60 int32_t sh4_read_long( uint32_t addr )
62 return *(uint32_t *)(inbuf+(addr-start_addr));
65 gboolean sh4_execute_instruction( ) { }
66 void sh4_accept_interrupt() {}
67 void sh4_set_breakpoint( uint32_t pc, int type ) { }
68 gboolean sh4_clear_breakpoint( uint32_t pc, int type ) { }
69 int sh4_get_breakpoint( uint32_t pc ) { }
70 void event_execute() {}
71 void TMU_run_slice( uint32_t nanos ) {}
72 void SCIF_run_slice( uint32_t nanos ) {}
73 void sh4_write_byte( uint32_t addr, uint32_t val ) {}
74 void sh4_write_word( uint32_t addr, uint32_t val ) {}
75 void sh4_write_long( uint32_t addr, uint32_t val ) {}
76 void sh4_flush_store_queue( uint32_t addr ) {}
77 void sh4_write_sr( uint32_t val ) { }
78 void syscall_invoke( uint32_t val ) { }
79 uint32_t sh4_read_sr( void ) { }
80 gboolean sh4_raise_exception( int exc ) {}
84 fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
85 fprintf( stderr, "Options:\n");
86 fprintf( stderr, " -d <filename> Diff results against contents of file\n" );
87 fprintf( stderr, " -h Display this help message\n" );
88 fprintf( stderr, " -o <filename> Output disassembly to file [stdout]\n" );
89 fprintf( stderr, " -s <addr> Specify start address of binary [8C010000]\n" );
92 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
96 vfprintf( stderr, msg, ap );
97 fprintf( stderr, "\n" );
102 struct sh4_registers sh4r;
105 int main( int argc, char *argv[] )
109 while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
115 output_file = optarg;
118 start_addr = strtoul(optarg, NULL, 0);
125 if( optind < argc ) {
126 input_file = argv[optind++];
132 in = fopen( input_file, "ro" );
134 perror( "Unable to open input file" );
137 fstat( fileno(in), &st );
138 inbuf = malloc( st.st_size );
139 fread( inbuf, st.st_size, 1, in );
143 uint8_t *buf = sh4_translate_basic_block( start_addr );
144 uint32_t buflen = xlat_get_block_size(buf);
145 x86_disasm_init( buf, 0x8c010000, buflen );
146 x86_set_symtab( local_symbols, 6 );
147 for( pc = 0x8c010000; pc < 0x8c010000 + buflen; ) {
150 uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
151 fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
.