Search
lxdream.org :: lxdream/src/test/testsh4x86.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testsh4x86.c
changeset 740:dd11269ee48b
prev672:cc2c2b0ab272
next775:b50989a63120
author nkeynes
date Wed Jul 16 10:40:10 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Rationalize the two SH4 run slice impls into sh4.c, and tidy up the vm exits.
Fixes broken soft-reset with emulator core
Fixes broken build without translator
file annotate diff log raw
nkeynes@363
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@363
     3
 *
nkeynes@363
     4
 * Test cases for the SH4 => x86 translator core. Takes as
nkeynes@363
     5
 * input a binary SH4 object (and VMA), generates the
nkeynes@363
     6
 * corresponding x86 code, and outputs the disassembly.
nkeynes@363
     7
 *
nkeynes@363
     8
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@363
     9
 *
nkeynes@363
    10
 * This program is free software; you can redistribute it and/or modify
nkeynes@363
    11
 * it under the terms of the GNU General Public License as published by
nkeynes@363
    12
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@363
    13
 * (at your option) any later version.
nkeynes@363
    14
 *
nkeynes@363
    15
 * This program is distributed in the hope that it will be useful,
nkeynes@363
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@363
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@363
    18
 * GNU General Public License for more details.
nkeynes@363
    19
 */
nkeynes@363
    20
nkeynes@363
    21
#include <stdio.h>
nkeynes@363
    22
#include <stdarg.h>
nkeynes@363
    23
#include <getopt.h>
nkeynes@365
    24
#include <sys/stat.h>
nkeynes@365
    25
#include "x86dasm/x86dasm.h"
nkeynes@363
    26
#include "sh4/sh4trans.h"
nkeynes@363
    27
#include "sh4/sh4core.h"
nkeynes@586
    28
#include "sh4/sh4mmio.h"
nkeynes@586
    29
nkeynes@586
    30
struct mmio_region mmio_region_MMU;
nkeynes@586
    31
struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
nkeynes@586
    32
int sh4_breakpoint_count = 0;
nkeynes@363
    33
nkeynes@363
    34
#define MAX_INS_SIZE 32
nkeynes@363
    35
nkeynes@363
    36
char *option_list = "s:o:d:h";
nkeynes@363
    37
struct option longopts[1] = { { NULL, 0, 0, 0 } };
nkeynes@363
    38
nkeynes@363
    39
char *input_file = NULL;
nkeynes@363
    40
char *diff_file = NULL;
nkeynes@363
    41
char *output_file = NULL;
nkeynes@602
    42
gboolean sh4_starting;
nkeynes@363
    43
uint32_t start_addr = 0x8C010000;
nkeynes@365
    44
uint32_t sh4_cpu_period = 5;
nkeynes@586
    45
sh4ptr_t sh4_main_ram;
nkeynes@363
    46
FILE *in;
nkeynes@363
    47
nkeynes@363
    48
char *inbuf;
nkeynes@365
    49
nkeynes@365
    50
struct x86_symbol local_symbols[] = {
nkeynes@365
    51
    { "_sh4_read_byte", sh4_read_byte },
nkeynes@365
    52
    { "_sh4_read_word", sh4_read_word },
nkeynes@365
    53
    { "_sh4_read_long", sh4_read_long },
nkeynes@365
    54
    { "_sh4_write_byte", sh4_write_byte },
nkeynes@365
    55
    { "_sh4_write_word", sh4_write_word },
nkeynes@365
    56
    { "_sh4_write_long", sh4_write_long }
nkeynes@365
    57
};
nkeynes@363
    58
nkeynes@363
    59
int32_t sh4_read_byte( uint32_t addr ) 
nkeynes@363
    60
{
nkeynes@363
    61
    return *(uint8_t *)(inbuf+(addr-start_addr));
nkeynes@363
    62
}
nkeynes@363
    63
int32_t sh4_read_word( uint32_t addr ) 
nkeynes@363
    64
{
nkeynes@363
    65
    return *(uint16_t *)(inbuf+(addr-start_addr));
nkeynes@363
    66
}
nkeynes@363
    67
int32_t sh4_read_long( uint32_t addr ) 
nkeynes@363
    68
{
nkeynes@363
    69
    return *(uint32_t *)(inbuf+(addr-start_addr));
nkeynes@363
    70
}
nkeynes@363
    71
// Stubs
nkeynes@363
    72
gboolean sh4_execute_instruction( ) { }
nkeynes@363
    73
void sh4_accept_interrupt() {}
nkeynes@586
    74
void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
nkeynes@586
    75
gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type ) { }
nkeynes@363
    76
int sh4_get_breakpoint( uint32_t pc ) { }
nkeynes@740
    77
void sh4_core_exit( int exit_code ){}
nkeynes@740
    78
void sh4_flush_icache(){}
nkeynes@363
    79
void event_execute() {}
nkeynes@363
    80
void TMU_run_slice( uint32_t nanos ) {}
nkeynes@363
    81
void SCIF_run_slice( uint32_t nanos ) {}
nkeynes@363
    82
void sh4_write_byte( uint32_t addr, uint32_t val ) {}
nkeynes@363
    83
void sh4_write_word( uint32_t addr, uint32_t val ) {}
nkeynes@363
    84
void sh4_write_long( uint32_t addr, uint32_t val ) {}
nkeynes@669
    85
void sh4_write_fpscr( uint32_t val ) { }
nkeynes@669
    86
void sh4_switch_fr_banks() { }
nkeynes@586
    87
void mem_copy_to_sh4( sh4addr_t addr, sh4ptr_t src, size_t size ) { }
nkeynes@374
    88
void sh4_write_sr( uint32_t val ) { }
nkeynes@602
    89
gboolean sh4_has_page( sh4vma_t vma ) { return TRUE; }
nkeynes@385
    90
void syscall_invoke( uint32_t val ) { }
nkeynes@586
    91
void dreamcast_stop() {} 
nkeynes@586
    92
void dreamcast_reset() {}
nkeynes@374
    93
uint32_t sh4_read_sr( void ) { }
nkeynes@586
    94
gboolean sh4_raise_reset( int exc ) {}
nkeynes@365
    95
gboolean sh4_raise_exception( int exc ) {}
nkeynes@586
    96
gboolean sh4_raise_tlb_exception( int exc ) {}
nkeynes@499
    97
gboolean sh4_raise_trap( int exc ) {}
nkeynes@499
    98
void sh4_sleep() { }
nkeynes@624
    99
uint32_t sh4_sleep_run_slice(uint32_t nanosecs) { }
nkeynes@499
   100
void sh4_fsca( uint32_t angle, float *fr ) { }
nkeynes@672
   101
void sh4_ftrv( float *fv ) { }
nkeynes@499
   102
void signsat48(void) { }
nkeynes@515
   103
gboolean gui_error_dialog( const char *fmt, ... ) { }
nkeynes@586
   104
struct sh4_icache_struct sh4_icache;
nkeynes@363
   105
nkeynes@363
   106
void usage()
nkeynes@363
   107
{
nkeynes@363
   108
    fprintf( stderr, "Usage: testsh4x86 [options] <input bin file>\n");
nkeynes@363
   109
    fprintf( stderr, "Options:\n");
nkeynes@363
   110
    fprintf( stderr, "  -d <filename>  Diff results against contents of file\n" );
nkeynes@363
   111
    fprintf( stderr, "  -h             Display this help message\n" );
nkeynes@363
   112
    fprintf( stderr, "  -o <filename>  Output disassembly to file [stdout]\n" );
nkeynes@363
   113
    fprintf( stderr, "  -s <addr>      Specify start address of binary [8C010000]\n" );
nkeynes@363
   114
}
nkeynes@363
   115
nkeynes@363
   116
void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@363
   117
{
nkeynes@363
   118
    va_list ap;
nkeynes@363
   119
    va_start( ap, msg );
nkeynes@363
   120
    vfprintf( stderr, msg, ap );
nkeynes@363
   121
    fprintf( stderr, "\n" );
nkeynes@363
   122
    va_end(ap);
nkeynes@363
   123
}
nkeynes@363
   124
nkeynes@363
   125
nkeynes@363
   126
struct sh4_registers sh4r;
nkeynes@363
   127
nkeynes@363
   128
nkeynes@363
   129
int main( int argc, char *argv[] )
nkeynes@363
   130
{
nkeynes@363
   131
    struct stat st;
nkeynes@363
   132
    int opt;
nkeynes@363
   133
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@363
   134
	switch( opt ) {
nkeynes@363
   135
	case 'd':
nkeynes@363
   136
	    diff_file = optarg;
nkeynes@363
   137
	    break;
nkeynes@363
   138
	case 'o':
nkeynes@363
   139
	    output_file = optarg;
nkeynes@363
   140
	    break;
nkeynes@363
   141
	case 's':
nkeynes@363
   142
	    start_addr = strtoul(optarg, NULL, 0);
nkeynes@363
   143
	    break;
nkeynes@363
   144
	case 'h':
nkeynes@363
   145
	    usage();
nkeynes@363
   146
	    exit(0);
nkeynes@363
   147
	}
nkeynes@363
   148
    }
nkeynes@363
   149
    if( optind < argc ) {
nkeynes@363
   150
	input_file = argv[optind++];
nkeynes@363
   151
    } else {
nkeynes@363
   152
	usage();
nkeynes@363
   153
	exit(1);
nkeynes@363
   154
    }
nkeynes@363
   155
nkeynes@363
   156
    in = fopen( input_file, "ro" );
nkeynes@363
   157
    if( in == NULL ) {
nkeynes@363
   158
	perror( "Unable to open input file" );
nkeynes@363
   159
	exit(2);
nkeynes@363
   160
    }
nkeynes@363
   161
    fstat( fileno(in), &st );
nkeynes@363
   162
    inbuf = malloc( st.st_size );
nkeynes@363
   163
    fread( inbuf, st.st_size, 1, in );
nkeynes@363
   164
nkeynes@365
   165
    xlat_cache_init();
nkeynes@363
   166
    uint32_t pc;
nkeynes@365
   167
    uint8_t *buf = sh4_translate_basic_block( start_addr );
nkeynes@365
   168
    uint32_t buflen = xlat_get_block_size(buf);
nkeynes@365
   169
    x86_disasm_init( buf, 0x8c010000, buflen );
nkeynes@365
   170
    x86_set_symtab( local_symbols, 6 );
nkeynes@363
   171
    for( pc = 0x8c010000; pc < 0x8c010000 + buflen;  ) {
nkeynes@363
   172
	char buf[256];
nkeynes@363
   173
	char op[256];
nkeynes@363
   174
	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
nkeynes@363
   175
	fprintf( stdout, "%08X: %-20s %s\n", pc, op, buf );
nkeynes@363
   176
	pc = pc2;
nkeynes@363
   177
    }
nkeynes@363
   178
}
.