Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 19:9da7a8e38f9d
prev18:9a1b5d75703f
next25:fa2d21d57942
author nkeynes
date Fri Dec 23 11:44:55 2005 +0000 (18 years ago)
permissions -rw-r--r--
last change Start of "real" time slices, general structure in place now
view annotate diff log raw
     1 /*
     2  *
     3  */
     5 #include <stdio.h>
     6 #include <fcntl.h>
     7 #include <sys/stat.h>
     8 #include <sys/mman.h>
     9 #include <errno.h>
    10 #include <stdint.h>
    11 #include "gui.h"
    12 #include "ipbin.h"
    13 #include "sh4core.h"
    14 #include "pvr2.h"
    15 #include "mem.h"
    17 char ip_bin_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
    18 char iso_magic[6] = "\001CD001";
    20 #define CDI_V2 0x80000004
    21 #define CDI_V3 0x80000005
    23 int open_file( const gchar *filename )
    24 {
    25     char buf[32];
    26     uint32_t tmpa[2];
    27     struct stat st;
    29     int fd = open( filename, O_RDONLY );
    30     if( fd == -1 ) {
    31         ERROR( "Unable to open file: '%s' (%s)", filename,
    32                strerror(errno) );
    33         return;
    34     }
    36     fstat( fd, &st );
    37     if( st.st_size < 32768 ) {
    38         ERROR( "File '%s' too small to be a dreamcast image", filename );
    39         close(fd);
    40         return;
    41     }
    43     /* begin magic */
    44     if( read( fd, buf, 32 ) != 32 ) {
    45         ERROR( "Unable to read from file '%s'", filename );
    46         close(fd);
    47         return;
    48     }
    49     if( memcmp( buf, ip_bin_magic, 32 ) == 0 ) {
    50         /* we have a DC bootstrap */
    51         if( st.st_size == BOOTSTRAP_SIZE ) {
    52             char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
    53             /* Just the bootstrap, no image... */
    54             WARN( "File '%s' contains bootstrap only, loading anyway",
    55                   filename );
    56             lseek( fd, 0, SEEK_SET );
    57             read( fd, load, BOOTSTRAP_SIZE );
    58             parse_ipbin( load );
    59             sh4_reset();
    60             sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
    61             set_disassembly_region( main_debug, BOOTSTRAP_LOAD_ADDR );
    62             set_disassembly_pc( main_debug, sh4r.pc, TRUE );
    63             update_gui();
    64         } else {
    65             /* look for a valid ISO9660 header */
    66             lseek( fd, 32768, SEEK_SET );
    67             read( fd, buf, 8 );
    68             if( memcmp( buf, iso_magic, 6 ) == 0 ) {
    69                 /* Alright, got it */
    70                 INFO( "Loading ISO9660 filesystem from '%s'",
    71                       filename );
    72             }
    73         }
    74     } else {
    75         /* check if it's a CDI file: */
    76         lseek( fd, -8, SEEK_END );
    77         read( fd, &tmpa, 8 );
    78         if( tmpa[0] == CDI_V2 || tmpa[0] == CDI_V3 ) {
    79             /* Yup, it is */
    80             INFO( "Loading CDI file '%s'", filename );
    81         } else {
    82             ERROR( "Don't know what to do with '%s'", filename );
    83         }
    84     }
    85     close(fd);
    86     return 0;
    87 }
    89 int load_bin_file( const gchar *filename ) {
    90     mem_load_block( filename, 0x8c010000, -1 );
    91     sh4_set_pc( 0x8c010000 );
    92     set_disassembly_region( main_debug, 0x8c010000 );
    93     set_disassembly_pc( main_debug, sh4r.pc, TRUE );
    94     pvr2_set_base_address( 0x00025940 );
    95     update_gui();
    96 }
.