filename | src/test/testsh4x86.c |
changeset | 1263:b3de98d19faf |
prev | 1189:1540105786c8 |
next | 1302:765e514f99a6 |
author | nkeynes |
date | Sat Aug 04 08:46:28 2012 +1000 (11 years ago) |
permissions | -rw-r--r-- |
last change | Handle corner case in pvr2_run_slice when we've previously slightly overrun the end of the time slice |
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 <string.h>
27 #include "xlat/xlatdasm.h"
28 #include "sh4/sh4trans.h"
29 #include "sh4/sh4core.h"
30 #include "sh4/sh4mmio.h"
31 #include "sh4/mmu.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;
38 gboolean sh4_profile_blocks = FALSE;
40 #define MAX_INS_SIZE 32
43 struct mem_region_fn **sh4_address_space = (void *)0x12345432;
44 struct mem_region_fn **sh4_user_address_space = (void *)0x12345678;
45 char *option_list = "s:o:d:h";
46 struct option longopts[1] = { { NULL, 0, 0, 0 } };
48 char *input_file = NULL;
49 char *diff_file = NULL;
50 char *output_file = NULL;
51 gboolean sh4_starting;
52 uint32_t start_addr = 0x8C010000;
53 uint32_t sh4_cpu_period = 5;
54 unsigned char dc_main_ram[4096];
55 unsigned char dc_boot_rom[4096];
56 FILE *in;
58 char *inbuf;
60 struct xlat_symbol local_symbols[] = {
61 { "sh4r+128", ((char *)&sh4r)+128 },
62 { "sh4_cpu_period", &sh4_cpu_period },
63 { "sh4_address_space", (void *)0x12345432 },
64 { "sh4_user_address_space", (void *)0x12345678 },
65 { "sh4_write_fpscr", sh4_write_fpscr },
66 { "sh4_write_sr", sh4_write_sr },
67 { "sh4_read_sr", sh4_read_sr },
68 { "sh4_sleep", sh4_sleep },
69 { "sh4_fsca", sh4_fsca },
70 { "sh4_ftrv", sh4_ftrv },
71 { "sh4_switch_fr_banks", sh4_switch_fr_banks },
72 { "sh4_execute_instruction", sh4_execute_instruction },
73 { "signsat48", signsat48 },
74 { "xlat_get_code_by_vma", xlat_get_code_by_vma },
75 { "xlat_get_code", xlat_get_code }
76 };
78 // Stubs
79 gboolean sh4_execute_instruction( ) { return TRUE; }
80 void sh4_accept_interrupt() {}
81 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
82 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { return TRUE; }
83 gboolean dreamcast_is_running() { return FALSE; }
84 int sh4_get_breakpoint( uint32_t pc ) { return 0; }
85 void sh4_finalize_instruction() { }
86 void sh4_core_exit( int exit_code ){}
87 void sh4_crashdump() {}
88 void event_execute() {}
89 void TMU_run_slice( uint32_t nanos ) {}
90 void CCN_set_cache_control( int val ) { }
91 void PMM_write_control( int ctr, uint32_t val ) { }
92 void SCIF_run_slice( uint32_t nanos ) {}
93 void FASTCALL sh4_write_fpscr( uint32_t val ) { }
94 void FASTCALL sh4_write_sr( uint32_t val ) { }
95 uint32_t FASTCALL sh4_read_sr( void ) { return 0; }
96 void FASTCALL sh4_sleep() { }
97 void FASTCALL sh4_fsca( uint32_t angle, float *fr ) { }
98 void FASTCALL sh4_ftrv( float *fv ) { }
99 void FASTCALL signsat48(void) { }
100 void sh4_switch_fr_banks() { }
101 void mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
102 gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
103 void syscall_invoke( uint32_t val ) { }
104 void dreamcast_stop() {}
105 void dreamcast_reset() {}
106 void FASTCALL sh4_raise_reset( int exc ) { }
107 void FASTCALL sh4_raise_exception( int exc ) { }
108 void FASTCALL sh4_raise_tlb_exception( int exc, sh4vma_t vma ) { }
109 void FASTCALL sh4_raise_tlb_multihit( sh4vma_t vma) { }
110 void FASTCALL sh4_raise_trap( int exc ) { }
111 void FASTCALL sh4_flush_store_queue( sh4addr_t addr ) { }
112 void FASTCALL sh4_flush_store_queue_mmu( sh4addr_t addr, void *exc ) { }
113 void sh4_handle_pending_events() { }
114 uint32_t sh4_sleep_run_slice(uint32_t nanosecs) { return nanosecs; }
115 gboolean gui_error_dialog( const char *fmt, ... ) { return TRUE; }
116 gboolean FASTCALL mmu_update_icache( sh4vma_t addr ) { return TRUE; }
117 void MMU_ldtlb() { }
118 void event_schedule(int event, uint32_t nanos) { }
119 struct sh4_icache_struct sh4_icache;
120 struct mem_region_fn mem_region_unmapped;
121 const struct cpu_desc_struct sh4_cpu_desc;
122 sh4addr_t FASTCALL mmu_vma_to_phys_disasm( sh4vma_t vma ) { return vma; }
124 void usage()
125 {
126 fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
127 fprintf( stderr, "Options:\n");
128 fprintf( stderr, " -d <filename> Diff results against contents of file\n" );
129 fprintf( stderr, " -h Display this help message\n" );
130 fprintf( stderr, " -o <filename> Output disassembly to file [stdout]\n" );
131 fprintf( stderr, " -s <addr> Specify start address of binary [8C010000]\n" );
132 }
134 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
135 {
136 va_list ap;
137 va_start( ap, msg );
138 vfprintf( stderr, msg, ap );
139 fprintf( stderr, "\n" );
140 va_end(ap);
141 }
144 struct sh4_registers sh4r;
147 int main( int argc, char *argv[] )
148 {
149 struct stat st;
150 int opt;
151 while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
152 switch( opt ) {
153 case 'd':
154 diff_file = optarg;
155 break;
156 case 'o':
157 output_file = optarg;
158 break;
159 case 's':
160 start_addr = strtoul(optarg, NULL, 0);
161 break;
162 case 'h':
163 usage();
164 exit(0);
165 }
166 }
167 if( optind < argc ) {
168 input_file = argv[optind++];
169 } else {
170 usage();
171 exit(1);
172 }
174 mmio_region_MMU.mem = malloc(4096);
175 memset( mmio_region_MMU.mem, 0, 4096 );
177 ((uint32_t *)mmio_region_MMU.mem)[4] = 1;
179 in = fopen( input_file, "ro" );
180 if( in == NULL ) {
181 perror( "Unable to open input file" );
182 exit(2);
183 }
184 fstat( fileno(in), &st );
185 inbuf = malloc( st.st_size );
186 fread( inbuf, st.st_size, 1, in );
187 sh4_icache.mask = 0xFFFFF000;
188 sh4_icache.page_vma = start_addr & 0xFFFFF000;
189 sh4_icache.page = (unsigned char *)(inbuf - (sh4_icache.page_vma&0xFFF));
190 sh4_icache.page_ppa = start_addr & 0xFFFFF000;
192 xlat_cache_init();
193 uintptr_t pc;
194 uint8_t *buf = sh4_translate_basic_block( start_addr );
195 uint32_t buflen = xlat_get_code_size(buf);
196 xlat_disasm_init( local_symbols, sizeof(local_symbols)/sizeof(struct xlat_symbol) );
197 for( pc = (uintptr_t)buf; pc < ((uintptr_t)buf) + buflen; ) {
198 char buf[256];
199 char op[256];
200 uintptr_t pc2 = xlat_disasm_instruction( pc, buf, sizeof(buf), op );
201 fprintf( stdout, "%p: %s\n", (void *)pc, buf );
202 pc = pc2;
203 }
204 return 0;
205 }
.