Search
lxdream.org :: lxdream/src/test/testsh4x86.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4x86.c
changeset 363:7d0bab24016c
next365:94cab5ad0ed8
author nkeynes
date Tue Aug 28 08:47:35 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add initial unit test programs
view annotate diff log raw
     1 /**
     2  * $Id: testsh4x86.c,v 1.1 2007-08-28 08:47:13 nkeynes Exp $
     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 "sh4/sh4trans.h"
    25 #include <sys/stat.h>
    26 #include "sh4/sh4core.h"
    28 #define MAX_INS_SIZE 32
    30 char *option_list = "s:o:d:h";
    31 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    33 char *input_file = NULL;
    34 char *diff_file = NULL;
    35 char *output_file = NULL;
    36 uint32_t start_addr = 0x8C010000;
    38 FILE *in;
    40 char *inbuf;
    41 char *outbuf;
    43 int32_t sh4_read_byte( uint32_t addr ) 
    44 {
    45     return *(uint8_t *)(inbuf+(addr-start_addr));
    46 }
    47 int32_t sh4_read_word( uint32_t addr ) 
    48 {
    49     return *(uint16_t *)(inbuf+(addr-start_addr));
    50 }
    51 int32_t sh4_read_long( uint32_t addr ) 
    52 {
    53     return *(uint32_t *)(inbuf+(addr-start_addr));
    54 }
    55 // Stubs
    56 gboolean sh4_execute_instruction( ) { }
    57 void sh4_accept_interrupt() {}
    58 void sh4_set_breakpoint( uint32_t pc, int type ) { }
    59 gboolean sh4_clear_breakpoint( uint32_t pc, int type ) { }
    60 int sh4_get_breakpoint( uint32_t pc ) { }
    61 void event_execute() {}
    62 void TMU_run_slice( uint32_t nanos ) {}
    63 void SCIF_run_slice( uint32_t nanos ) {}
    64 void sh4_write_byte( uint32_t addr, uint32_t val ) {}
    65 void sh4_write_word( uint32_t addr, uint32_t val ) {}
    66 void sh4_write_long( uint32_t addr, uint32_t val ) {}
    68 void usage()
    69 {
    70     fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
    71     fprintf( stderr, "Options:\n");
    72     fprintf( stderr, "  -d <filename>  Diff results against contents of file\n" );
    73     fprintf( stderr, "  -h             Display this help message\n" );
    74     fprintf( stderr, "  -o <filename>  Output disassembly to file [stdout]\n" );
    75     fprintf( stderr, "  -s <addr>      Specify start address of binary [8C010000]\n" );
    76 }
    78 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
    79 {
    80     va_list ap;
    81     va_start( ap, msg );
    82     vfprintf( stderr, msg, ap );
    83     fprintf( stderr, "\n" );
    84     va_end(ap);
    85 }
    88 struct sh4_registers sh4r;
    91 int main( int argc, char *argv[] )
    92 {
    93     struct stat st;
    94     int opt;
    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     in = fopen( input_file, "ro" );
   119     if( in == NULL ) {
   120 	perror( "Unable to open input file" );
   121 	exit(2);
   122     }
   123     fstat( fileno(in), &st );
   124     inbuf = malloc( st.st_size );
   125     fread( inbuf, st.st_size, 1, in );
   126     outbuf = malloc( st.st_size * MAX_INS_SIZE );
   127     xlat_output = outbuf;
   129     uint32_t pc;
   130     for( pc = start_addr; pc < start_addr + st.st_size; pc+=2 ) {
   131 	sh4_x86_translate_instruction( pc );
   132     }
   134     uint32_t buflen = (xlat_output - (uint8_t *)outbuf);
   135     x86_disasm_init( outbuf, 0x8c010000, buflen );
   136     for( pc = 0x8c010000; pc < 0x8c010000 + buflen;  ) {
   137 	char buf[256];
   138 	char op[256];
   139 	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
   140 	fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
   141 	pc = pc2;
   142     }
   143 }
.