Search
lxdream.org :: lxdream/src/test/testsh4xir.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4xir.c
changeset 1006:3a169c224c12
next1011:fdd58619b760
author nkeynes
date Tue Apr 07 10:55:03 2009 +0000 (15 years ago)
branchxlat-refactor
permissions -rw-r--r--
last change Commit current work-in-progress to xlat-refactor branch
view annotate diff log raw
     1 /**
     2  * $Id: testsh4x86.c 988 2009-01-15 11:23:20Z nkeynes $
     3  *
     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.
     7  *
     8  * Copyright (c) 2009 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  */
    20 #include <getopt.h>
    21 #include <stdlib.h>
    22 #include <sys/stat.h>
    23 #include <string.h>
    25 #include "lxdream.h"
    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;
    39 void MMU_ldtlb() { }
    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 },
    69 };
    71 extern struct xlat_source_machine sh4_source_machine;
    72 extern struct xlat_target_machine x86_target_machine;
    73 void usage()
    74 {
    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" );
    81 }
    83 int main( int argc, char *argv[] )
    84 {
    85     char *input_file;
    86     char *output_file;
    87     char *diff_file;
    88     char *inbuf;
    89     uint32_t start_addr = 0x8c010000;
    90     struct stat st;
    91     int opt;
    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 ) {
    96         switch( opt ) {
    97         case 'd':
    98             diff_file = optarg;
    99             break;
   100         case 'o':
   101             output_file = optarg;
   102             break;
   103         case 's':
   104             start_addr = strtoul(optarg, NULL, 0);
   105             break;
   106         case 'h':
   107             usage();
   108             exit(0);
   109         }
   110     }
   111     if( optind < argc ) {
   112         input_file = argv[optind++];
   113     } else {
   114         usage();
   115         exit(1);
   116     }
   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" );
   124     if( in == NULL ) {
   125         perror( "Unable to open input file" );
   126         exit(2);
   127     }
   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 );
   149     return 0;
   150 }
.