2 * $Id: testsh4x86.c 988 2009-01-15 11:23:20Z nkeynes $
4 * Test cases for the SH4 => XIR decoder. Takes as
5 * input a binary SH4 object (and VMA), generates the
6 * corresponding IR, and dumps it to stdout.
8 * Copyright (c) 2009 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.
26 #include "sh4/sh4core.h"
27 #include "sh4/sh4mmio.h"
28 #include "sh4/sh4xir.h"
31 struct mmio_region mmio_region_MMU;
32 struct mmio_region mmio_region_PMM;
33 struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
34 struct sh4_registers sh4r;
35 int sh4_breakpoint_count = 0;
36 uint32_t sh4_cpu_period = 5;
37 struct sh4_icache_struct sh4_icache;
40 void FASTCALL sh4_sleep() { }
41 void FASTCALL sh4_write_fpscr( uint32_t val ) { }
42 void sh4_switch_fr_banks() { }
43 void FASTCALL sh4_write_sr( uint32_t val ) { }
44 uint32_t FASTCALL sh4_read_sr( void ) { return 0; }
45 void FASTCALL sh4_raise_trap( int exc ) { }
46 void FASTCALL sh4_raise_exception( int exc ) { }
47 void log_message( void *ptr, int level, const gchar *source, const char *msg, ... ) { }
48 gboolean sh4_execute_instruction( ) { return TRUE; }
49 void **sh4_address_space;
50 void **sh4_user_address_space;
51 unsigned char *xlat_output;
53 #define MAX_INS_SIZE 32
54 #define MAX_XIR_OPS 16384
56 char *option_list = "s:o:d:h";
57 struct option longopts[1] = { { NULL, 0, 0, 0 } };
59 struct xir_symbol_entry debug_symbols[] = {
60 { "sh4_cpu_period", &sh4_cpu_period },
61 { "sh4_write_fpscr", sh4_write_fpscr },
62 { "sh4_write_sr", sh4_write_sr },
63 { "sh4_read_sr", sh4_read_sr },
64 { "sh4_sleep", sh4_sleep },
65 { "sh4_switch_fr_banks", sh4_switch_fr_banks },
66 { "sh4_raise_exception", sh4_raise_exception },
67 { "sh4_raise_trap", sh4_raise_trap },
68 { "sh4_execute_instruction", sh4_execute_instruction },
71 extern struct xlat_source_machine sh4_source_machine;
72 extern struct xlat_target_machine x86_target_machine;
75 fprintf( stderr, "Usage: testsh4xir [options] <input bin file>\n");
76 fprintf( stderr, "Options:\n");
77 fprintf( stderr, " -d <filename> Diff results against contents of file\n" );
78 fprintf( stderr, " -h Display this help message\n" );
79 fprintf( stderr, " -o <filename> Output disassembly to file [stdout]\n" );
80 fprintf( stderr, " -s <addr> Specify start address of binary [8C010000]\n" );
83 int main( int argc, char *argv[] )
89 uint32_t start_addr = 0x8c010000;
92 struct xir_op xir[MAX_XIR_OPS];
93 xir_op_t xir_ptr = &xir[0];
95 while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
101 output_file = optarg;
104 start_addr = strtoul(optarg, NULL, 0);
111 if( optind < argc ) {
112 input_file = argv[optind++];
118 mmio_region_MMU.mem = malloc(4096);
119 memset( mmio_region_MMU.mem, 0, 4096 );
121 ((uint32_t *)mmio_region_MMU.mem)[4] = 1;
123 FILE *in = fopen( input_file, "ro" );
125 perror( "Unable to open input file" );
128 fstat( fileno(in), &st );
129 inbuf = malloc( st.st_size );
130 fread( inbuf, st.st_size, 1, in );
131 sh4_icache.mask = 0xFFFFF000;
132 sh4_icache.page_vma = start_addr & 0xFFFFF000;
133 sh4_icache.page = (unsigned char *)(inbuf - (start_addr&0xFFF));
134 sh4_icache.page_ppa = start_addr & 0xFFFFF000;
136 struct xir_basic_block xbb;
137 xbb.source = &sh4_source_machine;
138 xbb.ir_alloc_begin = &xir[0];
139 xbb.ir_alloc_end = &xir[MAX_XIR_OPS];
140 xbb.ir_begin = xbb.ir_ptr = xbb.ir_end = xbb.ir_alloc_begin;
141 xbb.pc_begin = start_addr;
142 xbb.pc_end = start_addr+4096;
143 xbb.source->decode_basic_block( &xbb );
145 x86_target_machine.lower( &xbb, xbb.ir_begin, xbb.ir_end );
146 xir_set_register_names( sh4_source_machine.reg_names, x86_target_machine.reg_names );
147 xir_set_symbol_table( debug_symbols );
148 xir_dump_block( &xir[0], NULL );
.