Search
lxdream.org :: lxdream/src/test/testsh4x86.c :: diff
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
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/test/testsh4x86.c Tue Aug 28 08:47:35 2007 +0000
1.3 @@ -0,0 +1,143 @@
1.4 +/**
1.5 + * $Id: testsh4x86.c,v 1.1 2007-08-28 08:47:13 nkeynes Exp $
1.6 + *
1.7 + * Test cases for the SH4 => x86 translator core. Takes as
1.8 + * input a binary SH4 object (and VMA), generates the
1.9 + * corresponding x86 code, and outputs the disassembly.
1.10 + *
1.11 + * Copyright (c) 2005 Nathan Keynes.
1.12 + *
1.13 + * This program is free software; you can redistribute it and/or modify
1.14 + * it under the terms of the GNU General Public License as published by
1.15 + * the Free Software Foundation; either version 2 of the License, or
1.16 + * (at your option) any later version.
1.17 + *
1.18 + * This program is distributed in the hope that it will be useful,
1.19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.21 + * GNU General Public License for more details.
1.22 + */
1.23 +
1.24 +#include <stdio.h>
1.25 +#include <stdarg.h>
1.26 +#include <getopt.h>
1.27 +#include "sh4/sh4trans.h"
1.28 +#include <sys/stat.h>
1.29 +#include "sh4/sh4core.h"
1.30 +
1.31 +#define MAX_INS_SIZE 32
1.32 +
1.33 +char *option_list = "s:o:d:h";
1.34 +struct option longopts[1] = { { NULL, 0, 0, 0 } };
1.35 +
1.36 +char *input_file = NULL;
1.37 +char *diff_file = NULL;
1.38 +char *output_file = NULL;
1.39 +uint32_t start_addr = 0x8C010000;
1.40 +
1.41 +FILE *in;
1.42 +
1.43 +char *inbuf;
1.44 +char *outbuf;
1.45 +
1.46 +int32_t sh4_read_byte( uint32_t addr )
1.47 +{
1.48 + return *(uint8_t *)(inbuf+(addr-start_addr));
1.49 +}
1.50 +int32_t sh4_read_word( uint32_t addr )
1.51 +{
1.52 + return *(uint16_t *)(inbuf+(addr-start_addr));
1.53 +}
1.54 +int32_t sh4_read_long( uint32_t addr )
1.55 +{
1.56 + return *(uint32_t *)(inbuf+(addr-start_addr));
1.57 +}
1.58 +// Stubs
1.59 +gboolean sh4_execute_instruction( ) { }
1.60 +void sh4_accept_interrupt() {}
1.61 +void sh4_set_breakpoint( uint32_t pc, int type ) { }
1.62 +gboolean sh4_clear_breakpoint( uint32_t pc, int type ) { }
1.63 +int sh4_get_breakpoint( uint32_t pc ) { }
1.64 +void event_execute() {}
1.65 +void TMU_run_slice( uint32_t nanos ) {}
1.66 +void SCIF_run_slice( uint32_t nanos ) {}
1.67 +void sh4_write_byte( uint32_t addr, uint32_t val ) {}
1.68 +void sh4_write_word( uint32_t addr, uint32_t val ) {}
1.69 +void sh4_write_long( uint32_t addr, uint32_t val ) {}
1.70 +
1.71 +void usage()
1.72 +{
1.73 + fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
1.74 + fprintf( stderr, "Options:\n");
1.75 + fprintf( stderr, " -d <filename> Diff results against contents of file\n" );
1.76 + fprintf( stderr, " -h Display this help message\n" );
1.77 + fprintf( stderr, " -o <filename> Output disassembly to file [stdout]\n" );
1.78 + fprintf( stderr, " -s <addr> Specify start address of binary [8C010000]\n" );
1.79 +}
1.80 +
1.81 +void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
1.82 +{
1.83 + va_list ap;
1.84 + va_start( ap, msg );
1.85 + vfprintf( stderr, msg, ap );
1.86 + fprintf( stderr, "\n" );
1.87 + va_end(ap);
1.88 +}
1.89 +
1.90 +
1.91 +struct sh4_registers sh4r;
1.92 +
1.93 +
1.94 +int main( int argc, char *argv[] )
1.95 +{
1.96 + struct stat st;
1.97 + int opt;
1.98 + while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
1.99 + switch( opt ) {
1.100 + case 'd':
1.101 + diff_file = optarg;
1.102 + break;
1.103 + case 'o':
1.104 + output_file = optarg;
1.105 + break;
1.106 + case 's':
1.107 + start_addr = strtoul(optarg, NULL, 0);
1.108 + break;
1.109 + case 'h':
1.110 + usage();
1.111 + exit(0);
1.112 + }
1.113 + }
1.114 + if( optind < argc ) {
1.115 + input_file = argv[optind++];
1.116 + } else {
1.117 + usage();
1.118 + exit(1);
1.119 + }
1.120 +
1.121 + in = fopen( input_file, "ro" );
1.122 + if( in == NULL ) {
1.123 + perror( "Unable to open input file" );
1.124 + exit(2);
1.125 + }
1.126 + fstat( fileno(in), &st );
1.127 + inbuf = malloc( st.st_size );
1.128 + fread( inbuf, st.st_size, 1, in );
1.129 + outbuf = malloc( st.st_size * MAX_INS_SIZE );
1.130 + xlat_output = outbuf;
1.131 +
1.132 + uint32_t pc;
1.133 + for( pc = start_addr; pc < start_addr + st.st_size; pc+=2 ) {
1.134 + sh4_x86_translate_instruction( pc );
1.135 + }
1.136 +
1.137 + uint32_t buflen = (xlat_output - (uint8_t *)outbuf);
1.138 + x86_disasm_init( outbuf, 0x8c010000, buflen );
1.139 + for( pc = 0x8c010000; pc < 0x8c010000 + buflen; ) {
1.140 + char buf[256];
1.141 + char op[256];
1.142 + uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
1.143 + fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
1.144 + pc = pc2;
1.145 + }
1.146 +}
.