Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 954:59e17ce91c55
prev953:f4a156508ad1
next975:007bf7eb944f
author nkeynes
date Thu Jan 15 04:15:11 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Add support for the Intel ICC compiler (C only, icc doesn't support Obj-C)
- Rename Obj-C source to .m
- Separate paths.c into paths_unix.c and paths_osx.m
- Add configuration detection of ICC, along with specific opt flags
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * SH4 translation core module. This part handles the non-target-specific
     5  * section of the translation.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    19 #include <assert.h>
    20 #include "eventq.h"
    21 #include "syscall.h"
    22 #include "clock.h"
    23 #include "dreamcast.h"
    24 #include "sh4/sh4core.h"
    25 #include "sh4/sh4trans.h"
    26 #include "sh4/xltcache.h"
    29 /**
    30  * Execute a timeslice using translated code only (ie translate/execute loop)
    31  */
    32 uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
    33 {
    34     void * (*code)() = NULL;
    35     while( sh4r.slice_cycle < nanosecs ) {
    36         if( sh4r.event_pending <= sh4r.slice_cycle ) {
    37             if( sh4r.event_types & PENDING_EVENT ) {
    38                 event_execute();
    39             }
    40             /* Eventq execute may (quite likely) deliver an immediate IRQ */
    41             if( sh4r.event_types & PENDING_IRQ ) {
    42                 sh4_accept_interrupt();
    43                 code = NULL;
    44             }
    45         }
    47         if( code == NULL ) {
    48             if( sh4r.pc > 0xFFFFFF00 ) {
    49                 syscall_invoke( sh4r.pc );
    50                 sh4r.in_delay_slot = 0;
    51                 sh4r.pc = sh4r.pr;
    52             }
    54             code = xlat_get_code_by_vma( sh4r.pc );
    55             if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
    56                 code = sh4_translate_basic_block( sh4r.pc );
    57             }
    58         } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
    59             if( !IS_IN_ICACHE(sh4r.pc) ) {
    60                 /* If TLB is off, we may have gotten here without updating
    61                  * the icache, so do it now. This should never fail, so...
    62                  */
    63                 mmu_update_icache(sh4r.pc);
    64                 assert( IS_IN_ICACHE(sh4r.pc) ); 
    65             }
    66             code = sh4_translate_basic_block( sh4r.pc );
    67         }
    68         code = code();
    69     }
    70     return nanosecs;
    71 }
    73 uint8_t *xlat_output;
    74 xlat_cache_block_t xlat_current_block;
    75 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
    76 uint32_t xlat_recovery_posn;
    78 void sh4_translate_add_recovery( uint32_t icount )
    79 {
    80     xlat_recovery[xlat_recovery_posn].xlat_offset = 
    81         ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
    82     xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
    83     xlat_recovery_posn++;
    84 }
    86 /**
    87  * Translate a linear basic block, ie all instructions from the start address
    88  * (inclusive) until the next branch/jump instruction or the end of the page
    89  * is reached.
    90  * @param start VMA of the block start (which must already be in the icache)
    91  * @return the address of the translated block
    92  * eg due to lack of buffer space.
    93  */
    94 void * sh4_translate_basic_block( sh4addr_t start )
    95 {
    96     sh4addr_t pc = start;
    97     sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
    98     int done, i;
    99     xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
   100     xlat_output = (uint8_t *)xlat_current_block->code;
   101     xlat_recovery_posn = 0;
   102     uint8_t *eob = xlat_output + xlat_current_block->size;
   104     if( GET_ICACHE_END() < lastpc ) {
   105         lastpc = GET_ICACHE_END();
   106     }
   108     sh4_translate_begin_block(pc);
   110     do {
   111         /* check for breakpoints at this pc */
   112         for( i=0; i<sh4_breakpoint_count; i++ ) {
   113             if( sh4_breakpoints[i].address == pc ) {
   114                 sh4_translate_emit_breakpoint(pc);
   115                 break;
   116             }
   117         }
   118         if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
   119             uint8_t *oldstart = xlat_current_block->code;
   120             xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
   121             xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   122             eob = xlat_current_block->code + xlat_current_block->size;
   123         }
   124         done = sh4_translate_instruction( pc ); 
   125         assert( xlat_output <= eob );
   126         pc += 2;
   127         if ( pc >= lastpc ) {
   128             done = 2;
   129         }
   130     } while( !done );
   131     pc += (done - 2);
   133     // Add end-of-block recovery for post-instruction checks
   134     sh4_translate_add_recovery( (pc - start)>>1 ); 
   136     int epilogue_size = sh4_translate_end_block_size();
   137     uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
   138     uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
   139     if( xlat_current_block->size < finalsize ) {
   140         uint8_t *oldstart = xlat_current_block->code;
   141         xlat_current_block = xlat_extend_block( finalsize );
   142         xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   143     }	
   144     sh4_translate_end_block(pc);
   145     assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
   147     /* Write the recovery records onto the end of the code block */
   148     memcpy( xlat_output, xlat_recovery, recovery_size);
   149     xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
   150     xlat_current_block->recover_table_size = xlat_recovery_posn;
   151     xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
   152     xlat_commit_block( finalsize, pc-start );
   153     return xlat_current_block->code;
   154 }
   156 /**
   157  * "Execute" the supplied recovery record. Currently this only updates
   158  * sh4r.pc and sh4r.slice_cycle according to the currently executing
   159  * instruction. In future this may be more sophisticated (ie will
   160  * call into generated code).
   161  */
   162 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
   163 {
   164     sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
   165     sh4r.pc += (recovery->sh4_icount<<1);
   166 }
   168 /**
   169  * Same as sh4_translate_run_recovery, but is used to recover from a taken
   170  * exception - that is, it fixes sh4r.spc rather than sh4r.pc
   171  */
   172 void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
   173 {
   174     sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
   175     sh4r.spc += (recovery->sh4_icount<<1);
   176 }    
   178 void sh4_translate_exit_recover( )
   179 {
   180     void *code = xlat_get_code_by_vma( sh4r.pc );
   181     if( code != NULL ) {
   182         uint32_t size = xlat_get_code_size( code );
   183         void *pc = xlat_get_native_pc( code, size );
   184         if( pc != NULL ) {
   185             // could be null if we're not actually running inside the translator
   186             xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
   187             if( recover != NULL ) {
   188                 // Can be null if there is no recovery necessary
   189                 sh4_translate_run_recovery(recover);
   190             }
   191         }
   192     }
   193 }
   195 void sh4_translate_exception_exit_recover( )
   196 {
   197     void *code = xlat_get_code_by_vma( sh4r.spc );
   198     if( code != NULL ) {
   199         uint32_t size = xlat_get_code_size( code );
   200         void *pc = xlat_get_native_pc( code, size );
   201         if( pc != NULL ) {
   202             // could be null if we're not actually running inside the translator
   203             xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
   204             if( recover != NULL ) {
   205                 // Can be null if there is no recovery necessary
   206                 sh4_translate_run_exception_recovery(recover);
   207             }
   208         }
   209     }
   211 }
   213 void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
   214 {
   215     if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
   216         return;
   217     }
   218     sh4_core_exit( CORE_EXIT_BREAKPOINT );
   219 }
   221 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
   222 {
   223     void *result = NULL;
   225     if( IS_IN_ICACHE(vma) ) {
   226         return xlat_get_code( GET_ICACHE_PHYS(vma) );
   227     }
   229     if( vma > 0xFFFFFF00 ) {
   230         // lxdream hook
   231         return NULL;
   232     }
   234     if( !mmu_update_icache(vma) ) {
   235         // fault - off to the fault handler
   236         if( !mmu_update_icache(sh4r.pc) ) {
   237             // double fault - halt
   238             ERROR( "Double fault - halting" );
   239             sh4_core_exit(CORE_EXIT_HALT);
   240             return NULL;
   241         }
   242     }
   244     assert( IS_IN_ICACHE(sh4r.pc) );
   245     result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
   246     return result;
   247 }
.