Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 953:f4a156508ad1
prev892:126aa7db6162
next1034:7044e01148f0
author nkeynes
date Mon Feb 09 00:13:46 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Fail cleanly if the display doesn't actually support GLX, rather than crashing horribly
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * Central switchboard for the system. This pulls all the individual modules
     4  * together into some kind of coherent structure. This is also where you'd
     5  * add Naomi support, if I ever get a board to play with...
     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  */
    20 #include <errno.h>
    21 #include <glib.h>
    22 #include "lxdream.h"
    23 #include "dream.h"
    24 #include "mem.h"
    25 #include "dreamcast.h"
    26 #include "asic.h"
    27 #include "syscall.h"
    28 #include "gui.h"
    29 #include "aica/aica.h"
    30 #include "gdrom/ide.h"
    31 #include "maple/maple.h"
    32 #include "pvr2/pvr2.h"
    33 #include "sh4/sh4.h"
    34 #include "sh4/sh4core.h"
    36 /**
    37  * Current state of the DC virtual machine
    38  */
    39 typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
    40                STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
    42 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
    43 static gboolean dreamcast_has_bios = FALSE;
    44 static gboolean dreamcast_has_flash = FALSE;
    45 static gboolean dreamcast_exit_on_stop = FALSE;
    46 static gchar *dreamcast_program_name = NULL;
    47 static sh4addr_t dreamcast_entry_point = 0xA0000000;
    48 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    49 static uint64_t run_time_nanosecs = 0;
    51 #define MAX_MODULES 32
    52 static int num_modules = 0;
    53 dreamcast_module_t modules[MAX_MODULES];
    55 /**
    56  * The unknown module is used for logging files without an actual module
    57  * declaration
    58  */
    59 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    60         NULL, NULL, NULL };
    62 extern struct mem_region_fn mem_region_sdram;
    63 extern struct mem_region_fn mem_region_vram32;
    64 extern struct mem_region_fn mem_region_vram64;
    65 extern struct mem_region_fn mem_region_audioram;
    66 extern struct mem_region_fn mem_region_audioscratch;
    67 extern struct mem_region_fn mem_region_flashram;
    68 extern struct mem_region_fn mem_region_bootrom;
    69 extern struct mem_region_fn mem_region_pvr2ta;
    70 extern struct mem_region_fn mem_region_pvr2yuv;
    71 extern struct mem_region_fn mem_region_pvr2vdma1;
    72 extern struct mem_region_fn mem_region_pvr2vdma2;
    74 unsigned char dc_main_ram[16 MB];
    75 unsigned char dc_boot_rom[2 MB];
    76 unsigned char dc_flash_ram[128 KB];
    78 /**
    79  * This function is responsible for defining how all the pieces of the
    80  * dreamcast actually fit together. 
    81  *
    82  * Note currently the locations of the various MMIO pages are hard coded in
    83  * the MMIO definitions - they should probably be moved here.
    84  */
    85 void dreamcast_configure( )
    86 {
    87     const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
    88     const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
    90     dreamcast_register_module( &eventq_module );
    91     /* Register the memory framework */
    92     dreamcast_register_module( &mem_module );
    94     /* Setup standard memory map */
    95     mem_map_region( dc_boot_rom,     0x00000000, 2 MB,   MEM_REGION_BIOS,         &mem_region_bootrom, MEM_FLAG_ROM, 2 MB, 0 );
    96     mem_map_region( dc_flash_ram,    0x00200000, 128 KB, MEM_REGION_FLASH,        &mem_region_flashram, MEM_FLAG_RAM, 128 KB, 0 );
    97     mem_map_region( aica_main_ram,   0x00800000, 2 MB,   MEM_REGION_AUDIO,        &mem_region_audioram, MEM_FLAG_RAM, 2 MB, 0 );
    98     mem_map_region( aica_scratch_ram,0x00703000, 8 KB,   MEM_REGION_AUDIO_SCRATCH,&mem_region_audioscratch, MEM_FLAG_RAM, 8 KB, 0 );
    99     mem_map_region( NULL,            0x04000000, 8 MB,   MEM_REGION_VIDEO64,      &mem_region_vram64, 0, 8 MB, 0 );
   100     mem_map_region( pvr2_main_ram,   0x05000000, 8 MB,   MEM_REGION_VIDEO,        &mem_region_vram32, MEM_FLAG_RAM, 8 MB, 0 ); 
   101     mem_map_region( dc_main_ram,     0x0C000000, 16 MB,  MEM_REGION_MAIN,         &mem_region_sdram, MEM_FLAG_RAM, 0x01000000, 0x0F000000 );
   102     mem_map_region( NULL,            0x10000000, 8 MB,   MEM_REGION_PVR2TA,       &mem_region_pvr2ta, 0, 0x02000000, 0x12000000 );
   103     mem_map_region( NULL,            0x10800000, 8 MB,   MEM_REGION_PVR2YUV,      &mem_region_pvr2yuv, 0, 0x02000000, 0x12800000 );
   104     mem_map_region( NULL,            0x11000000, 16 MB,  MEM_REGION_PVR2VDMA1,    &mem_region_pvr2vdma1, 0, 16 MB, 0 );
   105     mem_map_region( NULL,            0x13000000, 16 MB,  MEM_REGION_PVR2VDMA2,    &mem_region_pvr2vdma2, 0, 16 MB, 0 );
   107     dreamcast_has_bios = mem_load_rom( dc_boot_rom, bios_path, 2 MB, 0x89f2b1a1 );
   108     if( flash_path != NULL && flash_path[0] != '\0' ) {
   109         mem_load_block( flash_path, 0x00200000, 0x00020000 );
   110     }
   111     dreamcast_has_flash = TRUE;
   113     /* Load in the rest of the core modules */
   114     dreamcast_register_module( &sh4_module );
   115     dreamcast_register_module( &asic_module );
   116     dreamcast_register_module( &pvr2_module );
   117     dreamcast_register_module( &aica_module );
   118     dreamcast_register_module( &maple_module );
   119     dreamcast_register_module( &ide_module );
   120 }
   122 void dreamcast_config_changed(void)
   123 {
   124     const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
   125     const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
   126     dreamcast_has_bios = mem_load_rom( dc_boot_rom, bios_path, 2 MB, 0x89f2b1a1 );
   127     if( flash_path != NULL && flash_path[0] != '\0' ) {
   128         mem_load_block( flash_path, 0x00200000, 0x00020000 );
   129     }
   130 }
   132 void dreamcast_save_flash()
   133 {
   134     if( dreamcast_has_flash ) {
   135         const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
   136         mem_save_block( file, 0x00200000, 0x00020000 );
   137     }
   138 }
   140 /**
   141  * Constructs a system configuration for the AICA in standalone mode,
   142  * ie sound chip only.
   143  */
   144 void dreamcast_configure_aica_only( )
   145 {
   146     dreamcast_register_module( &mem_module );
   147     mem_map_region( aica_main_ram, 0x00800000, 2 MB, MEM_REGION_AUDIO, &mem_region_audioram, MEM_FLAG_RAM, 2 MB, 0 );
   148     mem_map_region( aica_scratch_ram, 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH, &mem_region_audioscratch, MEM_FLAG_RAM, 8 KB, 0 );
   149     dreamcast_register_module( &aica_module );
   150     aica_enable();
   151     dreamcast_state = STATE_STOPPED;
   152 }
   154 void dreamcast_register_module( dreamcast_module_t module ) 
   155 {
   156     modules[num_modules++] = module;
   157     if( module->init != NULL )
   158         module->init();
   159 }
   161 void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
   162 {
   163     run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
   164 }
   166 void dreamcast_set_exit_on_stop( gboolean flag )
   167 {
   168     dreamcast_exit_on_stop = flag;
   169 }
   171 void dreamcast_init( void )
   172 {
   173     dreamcast_configure();
   174     dreamcast_state = STATE_STOPPED;
   175 }
   177 void dreamcast_reset( void )
   178 {
   179     sh4_core_exit(CORE_EXIT_SYSRESET);
   180     int i;
   181     for( i=0; i<num_modules; i++ ) {
   182         if( modules[i]->reset != NULL )
   183             modules[i]->reset();
   184     }
   185 }
   187 void dreamcast_run( void )
   188 {
   189     int i;
   191     if( !dreamcast_can_run() ) {
   192         ERROR(_("No program is loaded, and no BIOS is configured (required to boot a CD image). To continue, either load a binary program, or set the path to your BIOS file in the Path Preferences"));
   193         return;
   194     }
   196     if( dreamcast_state != STATE_RUNNING ) {
   197         for( i=0; i<num_modules; i++ ) {
   198             if( modules[i]->start != NULL )
   199                 modules[i]->start();
   200         }
   201     }
   203     if( maple_should_grab() ) {
   204         gui_set_use_grab(TRUE);
   205     }
   207     dreamcast_state = STATE_RUNNING;
   209     if( run_time_nanosecs != 0 ) {
   210         while( dreamcast_state == STATE_RUNNING ) {
   211             uint32_t time_to_run = timeslice_length;
   212             if( run_time_nanosecs < time_to_run ) {
   213                 time_to_run = (uint32_t)run_time_nanosecs;
   214             }
   216             for( i=0; i<num_modules; i++ ) {
   217                 if( modules[i]->run_time_slice != NULL )
   218                     time_to_run = modules[i]->run_time_slice( time_to_run );
   219             }
   221             if( run_time_nanosecs > time_to_run ) {
   222                 run_time_nanosecs -= time_to_run;
   223             } else {
   224                 run_time_nanosecs = 0; // Finished
   225                 break;
   226             }
   227         }
   228     } else {
   229         while( dreamcast_state == STATE_RUNNING ) {
   230             int time_to_run = timeslice_length;
   231             for( i=0; i<num_modules; i++ ) {
   232                 if( modules[i]->run_time_slice != NULL )
   233                     time_to_run = modules[i]->run_time_slice( time_to_run );
   234             }
   236         }
   237     }
   239     gui_set_use_grab(FALSE);
   241     for( i=0; i<num_modules; i++ ) {
   242         if( modules[i]->stop != NULL )
   243             modules[i]->stop();
   244     }
   245     dreamcast_state = STATE_STOPPED;
   247     if( dreamcast_exit_on_stop ) {
   248         dreamcast_shutdown();
   249         exit(0);
   250     }
   251 }
   253 void dreamcast_stop( void )
   254 {
   255     sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
   256     if( dreamcast_state == STATE_RUNNING )
   257         dreamcast_state = STATE_STOPPING;
   258 }
   260 void dreamcast_shutdown()
   261 {
   262     // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
   263     // it's a shutdown-and-quit event
   264     if( dreamcast_state == STATE_RUNNING )
   265         dreamcast_state = STATE_STOPPING;
   266     dreamcast_save_flash();
   267 #ifdef ENABLE_SH4STATS
   268     sh4_stats_print(stdout);
   269 #endif
   270 }
   272 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
   273 {
   274     if( dreamcast_program_name != NULL ) {
   275         g_free(dreamcast_program_name);
   276     }
   277     dreamcast_program_name = g_strdup(name);
   278     dreamcast_entry_point = entry_point;
   279     sh4_set_pc(entry_point);
   280     sh4_write_sr( sh4_read_sr() & (~SR_BL) ); /* Unmask interrupts */
   281     bios_install();
   282     dcload_install();
   283     gui_update_state();
   284 }
   286 gboolean dreamcast_is_running( void )
   287 {
   288     return dreamcast_state == STATE_RUNNING;
   289 }
   291 gboolean dreamcast_can_run(void)
   292 {
   293     return dreamcast_state != STATE_UNINIT &&
   294     (dreamcast_has_bios || dreamcast_program_name != NULL);
   295 }
   297 /********************************* Save States *****************************/
   299 struct save_state_header {
   300     char magic[16];
   301     uint32_t version;
   302     uint32_t module_count;
   303 };
   305 struct chunk_header {
   306     char marker[4]; /* Always BLCK */
   307     char name[8]; /* Block (module) name */
   308     uint32_t block_length;
   309 };
   311 /**
   312  * Check the save state header to ensure that it is a valid, supported
   313  * file. 
   314  * @return the number of blocks following, or 0 if the file is invalid.
   315  */
   316 int dreamcast_read_save_state_header( FILE *f, char *error, int errorlen )
   317 {
   318     struct save_state_header header;
   319     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   320         return 0;
   321     }
   322     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   323     	if( error != NULL )
   324     		snprintf( error, errorlen, _("File is not a %s save state"), APP_NAME );
   325         return 0;
   326     }
   327     if( header.version != DREAMCAST_SAVE_VERSION ) {
   328     	if( error != NULL )
   329     		snprintf( error, errorlen, _("Unsupported %s save state version"), APP_NAME );
   330         return 0;
   331     }
   332     if( header.module_count > MAX_MODULES ) {
   333     	if( error != NULL )
   334     		snprintf( error, errorlen, _("%s save state is corrupted (bad module count)"), APP_NAME );
   335         return 0;
   336     }
   337     return header.module_count;
   338 }
   340 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   341 {
   342     struct chunk_header head;
   344     memcpy( head.marker, "BLCK", 4 );
   345     memset( head.name, 0, 8 );
   346     memcpy( head.name, name, strlen(name) );
   347     head.block_length = length;
   348     return fwrite( &head, sizeof(head), 1, f );
   349 }
   352 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   353 {
   354     int i;
   355     FILE *f = fopen( filename, "r" );
   356     if( f == NULL ) return NULL;
   358     int module_count = dreamcast_read_save_state_header(f, NULL, 0);
   359     if( module_count <= 0 ) {
   360         fclose(f);
   361         return NULL;
   362     }
   363     for( i=0; i<module_count; i++ ) {
   364         struct chunk_header head;
   365         if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   366             fclose(f);
   367             return NULL;
   368         }
   369         if( memcmp("BLCK", head.marker, 4) != 0 ) {
   370             fclose(f);
   371             return NULL;
   372         }
   374         if( strcmp("PVR2", head.name) == 0 ) {
   375             uint32_t buf_count;
   376             int has_front;
   377             fread( &buf_count, sizeof(buf_count), 1, f );
   378             fread( &has_front, sizeof(has_front), 1, f );
   379             if( buf_count != 0 && has_front ) {
   380                 frame_buffer_t result = read_png_from_stream(f);
   381                 fclose(f);
   382                 return result;
   383             }
   384             break;
   385         } else {
   386             fseek( f, head.block_length, SEEK_CUR );
   387         }
   388     }
   389     return NULL;
   390 }
   392 int dreamcast_load_state( const gchar *filename )
   393 {
   394     int i,j;
   395     int module_count;
   396     char error[128];
   397     int have_read[MAX_MODULES];
   399     FILE *f = fopen( filename, "r" );
   400     if( f == NULL ) return errno;
   402     module_count = dreamcast_read_save_state_header(f, error, sizeof(error));
   403     if( module_count <= 0 ) {
   404     	ERROR( error );
   405         fclose(f);
   406         return 1;
   407     }
   409     for( i=0; i<MAX_MODULES; i++ ) {
   410         have_read[i] = 0;
   411     }
   413     for( i=0; i<module_count; i++ ) {
   414         struct chunk_header chunk;
   415         fread( &chunk, sizeof(chunk), 1, f );
   416         if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   417             ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   418             fclose(f);
   419             return 2;
   420         }
   422         /* Find the matching module by name */
   423         for( j=0; j<num_modules; j++ ) {
   424             if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   425                 have_read[j] = 1;
   426                 if( modules[j]->load == NULL ) {
   427                     ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   428                     fclose(f);
   429                     return 2;
   430                 } else if( modules[j]->load(f) != 0 ) {
   431                     ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   432                     fclose(f);
   433                     return 2;
   434                 }
   435                 break;
   436             }
   437         }
   438         if( j == num_modules ) {
   439             fclose(f);
   440             ERROR( "%s save state contains unrecognized section", APP_NAME );
   441             return 2;
   442         }
   443     }
   445     /* Any modules that we didn't load - reset to the default state.
   446      * (ie it's not an error to skip a module if you don't actually
   447      * care about its state).
   448      */
   449     for( j=0; j<num_modules; j++ ) {
   450         if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   451             modules[j]->reset();
   452         }
   453     }
   454     fclose(f);
   455     INFO( "Save state read from %s", filename );
   456     return 0;
   457 }
   459 int dreamcast_save_state( const gchar *filename )
   460 {
   461     int i;
   462     FILE *f;
   463     struct save_state_header header;
   465     f = fopen( filename, "w" );
   466     if( f == NULL )
   467         return errno;
   468     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   469     header.version = DREAMCAST_SAVE_VERSION;
   470     header.module_count = 0;
   472     for( i=0; i<num_modules; i++ ) {
   473         if( modules[i]->save != NULL )
   474             header.module_count++;
   475     }
   476     fwrite( &header, sizeof(header), 1, f );
   477     for( i=0; i<num_modules; i++ ) {
   478         if( modules[i]->save != NULL ) {
   479             uint32_t blocklen, posn1, posn2;
   480             dreamcast_write_chunk_header( modules[i]->name, 0, f );
   481             posn1 = ftell(f);
   482             modules[i]->save(f);
   483             posn2 = ftell(f);
   484             blocklen = posn2 - posn1;
   485             fseek( f, posn1-4, SEEK_SET );
   486             fwrite( &blocklen, sizeof(blocklen), 1, f );
   487             fseek( f, posn2, SEEK_SET );
   488         }
   489     }
   490     fclose( f );
   491     INFO( "Save state written to %s", filename );
   492     return 0;
   493 }
   495 /********************* The Boot ROM address space **********************/
   496 static int32_t FASTCALL ext_bootrom_read_long( sh4addr_t addr )
   497 {
   498     return *((int32_t *)(dc_boot_rom + (addr&0x001FFFFF)));
   499 }
   500 static int32_t FASTCALL ext_bootrom_read_word( sh4addr_t addr )
   501 {
   502     return SIGNEXT16(*((int16_t *)(dc_boot_rom + (addr&0x001FFFFF))));
   503 }
   504 static int32_t FASTCALL ext_bootrom_read_byte( sh4addr_t addr )
   505 {
   506     return SIGNEXT8(*((int16_t *)(dc_boot_rom + (addr&0x001FFFFF))));
   507 }
   508 static void FASTCALL ext_bootrom_read_burst( unsigned char *dest, sh4addr_t addr )
   509 {
   510     memcpy( dest, dc_boot_rom +(addr&0x001FFFFF), 32 );
   511 }
   513 struct mem_region_fn mem_region_bootrom = { 
   514         ext_bootrom_read_long, unmapped_write_long, 
   515         ext_bootrom_read_word, unmapped_write_long, 
   516         ext_bootrom_read_byte, unmapped_write_long, 
   517         ext_bootrom_read_burst, unmapped_write_burst }; 
   519 /********************* The Flash RAM address space **********************/
   520 static int32_t FASTCALL ext_flashram_read_long( sh4addr_t addr )
   521 {
   522     return *((int32_t *)(dc_flash_ram + (addr&0x0001FFFF)));
   523 }
   524 static int32_t FASTCALL ext_flashram_read_word( sh4addr_t addr )
   525 {
   526     return SIGNEXT16(*((int16_t *)(dc_flash_ram + (addr&0x0001FFFF))));
   527 }
   528 static int32_t FASTCALL ext_flashram_read_byte( sh4addr_t addr )
   529 {
   530     return SIGNEXT8(*((int16_t *)(dc_flash_ram + (addr&0x0001FFFF))));
   531 }
   532 static void FASTCALL ext_flashram_write_long( sh4addr_t addr, uint32_t val )
   533 {
   534     *(uint32_t *)(dc_flash_ram + (addr&0x0001FFFF)) = val;
   535     asic_g2_write_word();
   536 }
   537 static void FASTCALL ext_flashram_write_word( sh4addr_t addr, uint32_t val )
   538 {
   539     *(uint16_t *)(dc_flash_ram + (addr&0x0001FFFF)) = (uint16_t)val;
   540     asic_g2_write_word();
   541 }
   542 static void FASTCALL ext_flashram_write_byte( sh4addr_t addr, uint32_t val )
   543 {
   544     *(uint8_t *)(dc_flash_ram + (addr&0x0001FFFF)) = (uint8_t)val;
   545     asic_g2_write_word();
   546 }
   547 static void FASTCALL ext_flashram_read_burst( unsigned char *dest, sh4addr_t addr )
   548 {
   549     memcpy( dest, dc_flash_ram+(addr&0x0001FFFF), 32 );
   550 }
   551 static void FASTCALL ext_flashram_write_burst( sh4addr_t addr, unsigned char *src )
   552 {
   553     memcpy( dc_flash_ram+(addr&0x0001FFFF), src, 32 );
   554 }
   556 struct mem_region_fn mem_region_flashram = { ext_flashram_read_long, ext_flashram_write_long, 
   557         ext_flashram_read_word, ext_flashram_write_word, 
   558         ext_flashram_read_byte, ext_flashram_write_byte, 
   559         ext_flashram_read_burst, ext_flashram_write_burst }; 
.