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.
27 #include "x86dasm/x86dasm.h"
28 #include "sh4/sh4trans.h"
29 #include "sh4/sh4core.h"
30 #include "sh4/sh4mmio.h"
33 struct dreamcast_module sh4_module;
34 struct mmio_region mmio_region_MMU;
35 struct mmio_region mmio_region_PMM;
36 struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
37 int sh4_breakpoint_count = 0;
39 #define MAX_INS_SIZE 32
42 struct mem_region_fn **sh4_address_space = (void *)0x12345432;
43 struct mem_region_fn **sh4_user_address_space = (void *)0x12345678;
44 char *option_list = "s:o:d:h";
45 struct option longopts[1] = { { NULL, 0, 0, 0 } };
47 char *input_file = NULL;
48 char *diff_file = NULL;
49 char *output_file = NULL;
50 gboolean sh4_starting;
51 uint32_t start_addr = 0x8C010000;
52 uint32_t sh4_cpu_period = 5;
53 unsigned char dc_main_ram[4096];
54 unsigned char dc_boot_rom[4096];
59 struct x86_symbol local_symbols[] = {
60 { "sh4r+128", ((char *)&sh4r)+128 },
61 { "sh4_cpu_period", &sh4_cpu_period },
62 { "sh4_address_space", (void *)0x12345432 },
63 { "sh4_user_address_space", (void *)0x12345678 },
64 { "sh4_write_fpscr", sh4_write_fpscr },
65 { "sh4_write_sr", sh4_write_sr },
66 { "sh4_read_sr", sh4_read_sr },
67 { "sh4_sleep", sh4_sleep },
68 { "sh4_fsca", sh4_fsca },
69 { "sh4_ftrv", sh4_ftrv },
70 { "sh4_switch_fr_banks", sh4_switch_fr_banks },
71 { "sh4_execute_instruction", sh4_execute_instruction },
72 { "signsat48", signsat48 },
73 { "xlat_get_code_by_vma", xlat_get_code_by_vma },
74 { "xlat_get_code", xlat_get_code }
78 gboolean sh4_execute_instruction( ) { return TRUE; }
79 void sh4_accept_interrupt() {}
80 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
81 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { return TRUE; }
82 gboolean dreamcast_is_running() { return FALSE; }
83 int sh4_get_breakpoint( uint32_t pc ) { return 0; }
84 void sh4_finalize_instruction() { }
85 void sh4_core_exit( int exit_code ){}
86 void event_execute() {}
87 void TMU_run_slice( uint32_t nanos ) {}
88 void CCN_set_cache_control( int val ) { }
89 void PMM_write_control( int ctr, uint32_t val ) { }
90 void SCIF_run_slice( uint32_t nanos ) {}
91 void FASTCALL sh4_write_fpscr( uint32_t val ) { }
92 void FASTCALL sh4_write_sr( uint32_t val ) { }
93 uint32_t FASTCALL sh4_read_sr( void ) { return 0; }
94 void FASTCALL sh4_sleep() { }
95 void FASTCALL sh4_fsca( uint32_t angle, float *fr ) { }
96 void FASTCALL sh4_ftrv( float *fv ) { }
97 void FASTCALL signsat48(void) { }
98 void sh4_switch_fr_banks() { }
99 void mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
100 gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
101 void syscall_invoke( uint32_t val ) { }
102 void dreamcast_stop() {}
103 void dreamcast_reset() {}
104 void FASTCALL sh4_raise_reset( int exc ) { }
105 void FASTCALL sh4_raise_exception( int exc ) { }
106 void FASTCALL sh4_raise_tlb_exception( int exc, sh4vma_t vma ) { }
107 void FASTCALL sh4_raise_tlb_multihit( sh4vma_t vma) { }
108 void FASTCALL sh4_raise_trap( int exc ) { }
109 void FASTCALL sh4_flush_store_queue( sh4addr_t addr ) { }
110 void FASTCALL sh4_flush_store_queue_mmu( sh4addr_t addr, void *exc ) { }
111 uint32_t sh4_sleep_run_slice(uint32_t nanosecs) { return nanosecs; }
112 gboolean gui_error_dialog( const char *fmt, ... ) { return TRUE; }
113 gboolean FASTCALL mmu_update_icache( sh4vma_t addr ) { return TRUE; }
115 struct sh4_icache_struct sh4_icache;
116 struct mem_region_fn mem_region_unmapped;
120 fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
121 fprintf( stderr, "Options:\n");
122 fprintf( stderr, " -d <filename> Diff results against contents of file\n" );
123 fprintf( stderr, " -h Display this help message\n" );
124 fprintf( stderr, " -o <filename> Output disassembly to file [stdout]\n" );
125 fprintf( stderr, " -s <addr> Specify start address of binary [8C010000]\n" );
128 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
132 vfprintf( stderr, msg, ap );
133 fprintf( stderr, "\n" );
138 struct sh4_registers sh4r;
141 int main( int argc, char *argv[] )
145 while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
151 output_file = optarg;
154 start_addr = strtoul(optarg, NULL, 0);
161 if( optind < argc ) {
162 input_file = argv[optind++];
168 mmio_region_MMU.mem = malloc(4096);
169 memset( mmio_region_MMU.mem, 0, 4096 );
171 ((uint32_t *)mmio_region_MMU.mem)[4] = 1;
173 in = fopen( input_file, "ro" );
175 perror( "Unable to open input file" );
178 fstat( fileno(in), &st );
179 inbuf = malloc( st.st_size );
180 fread( inbuf, st.st_size, 1, in );
181 sh4_icache.mask = 0xFFFFF000;
182 sh4_icache.page_vma = start_addr & 0xFFFFF000;
183 sh4_icache.page = (unsigned char *)(inbuf - (sh4_icache.page_vma&0xFFF));
184 sh4_icache.page_ppa = start_addr & 0xFFFFF000;
188 uint8_t *buf = sh4_translate_basic_block( start_addr );
189 uint32_t buflen = xlat_get_code_size(buf);
190 x86_disasm_init( buf, (uintptr_t)buf, buflen );
191 x86_set_symtab( local_symbols, sizeof(local_symbols)/sizeof(struct x86_symbol) );
192 for( pc = (uintptr_t)buf; pc < ((uintptr_t)buf) + buflen; ) {
195 uintptr_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
196 fprintf( stdout, "%p: %s\n", (void *)pc, buf );
.