Annotation of hatari/src/main.c, revision 1.1.1.7

1.1       root        1: /*
1.1.1.6   root        2:   Hatari - main.c
                      3: 
                      4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
                      6: 
                      7:   Main initialization and event handling routines.
1.1       root        8: */
1.1.1.7 ! root        9: static char rcsid[] = "Hatari $Id: main.c,v 1.43 2003/06/22 20:20:35 thothy Exp $";
1.1       root       10: 
                     11: #include <time.h>
1.1.1.2   root       12: #include <signal.h>
                     13: #include <sys/time.h>
1.1.1.5   root       14: #include <unistd.h>
1.1       root       15: 
                     16: #include <SDL.h>
                     17: 
                     18: #include "main.h"
                     19: #include "configuration.h"
                     20: #include "decode.h"
                     21: #include "dialog.h"
                     22: #include "createDiscImage.h"
                     23: #include "audio.h"
                     24: #include "debug.h"
                     25: #include "joy.h"
                     26: #include "errlog.h"
                     27: #include "file.h"
                     28: #include "floppy.h"
                     29: #include "gemdos.h"
1.1.1.4   root       30: #include "hdc.h"
1.1       root       31: #include "ikbd.h"
                     32: #include "intercept.h"
                     33: #include "reset.h"
1.1.1.5   root       34: #include "keymap.h"
1.1       root       35: #include "m68000.h"
                     36: #include "memorySnapShot.h"
                     37: #include "misc.h"
                     38: #include "printer.h"
                     39: #include "rs232.h"
                     40: #include "screen.h"
1.1.1.5   root       41: #include "sdlgui.h"
1.1       root       42: #include "shortcut.h"
                     43: #include "sound.h"
                     44: #include "tos.h"
1.1.1.5   root       45: #include "vdi.h"
1.1       root       46: #include "video.h"
                     47: #include "ymFormat.h"
1.1.1.2   root       48: #include "debugui.h"
1.1       root       49: 
                     50: #include "uae-cpu/hatari-glue.h"
                     51: 
                     52: 
1.1.1.4   root       53: #define FORCE_WORKING_DIR                 /* Set default directory to cwd */
                     54: 
1.1       root       55: 
                     56: BOOL bQuitProgram=FALSE;                  /* Flag to quit program cleanly */
                     57: BOOL bUseFullscreen=FALSE;
1.1.1.4   root       58: BOOL bEmulationActive=TRUE;               /* Run emulation when started */
1.1       root       59: BOOL bAppActive = FALSE;
1.1.1.2   root       60: BOOL bEnableDebug=FALSE;                  /* Enable debug UI? */
1.1       root       61: char szBootDiscImage[MAX_FILENAME_LENGTH] = { "" };
                     62: 
                     63: char szWorkingDir[MAX_FILENAME_LENGTH] = { "" };
                     64: char szCurrentDir[MAX_FILENAME_LENGTH] = { "" };
                     65: 
                     66: unsigned char STRam[16*1024*1024];        /* This is our ST Ram, includes all TOS/hardware areas for ease */
                     67: 
                     68: 
                     69: 
                     70: /*-----------------------------------------------------------------------*/
                     71: /*
                     72:   Save/Restore snapshot of local variables('MemorySnapShot_Store' handles type)
                     73: */
                     74: void Main_MemorySnapShot_Capture(BOOL bSave)
                     75: {
                     76:   int nBytes;
                     77: 
                     78:   /* Save/Restore details */
                     79:   /* Only save/restore area of memory machine ie set to, eg 1Mb */
                     80:   if (bSave) {
1.1.1.7 ! root       81:     nBytes = STRamEnd;  /* was: STRamEnd_BusErr */
1.1       root       82:     MemorySnapShot_Store(&nBytes,sizeof(nBytes));
                     83:     MemorySnapShot_Store(STRam,nBytes);
                     84:   }
                     85:   else {
                     86:     MemorySnapShot_Store(&nBytes,sizeof(nBytes));
                     87:     MemorySnapShot_Store(STRam,nBytes);
                     88:   }
                     89:   /* And Cart/TOS/Hardware area */
                     90:   MemorySnapShot_Store(&STRam[0xE00000],0x200000);
                     91:   MemorySnapShot_Store(szBootDiscImage,sizeof(szBootDiscImage));
                     92:   MemorySnapShot_Store(szWorkingDir,sizeof(szWorkingDir));
                     93:   MemorySnapShot_Store(szCurrentDir,sizeof(szCurrentDir));
                     94: }
                     95: 
                     96: 
                     97: /*-----------------------------------------------------------------------*/
                     98: /*
                     99:   Error handler
                    100: */
                    101: void Main_SysError(char *Error,char *Title)
                    102: {
1.1.1.2   root      103:   fprintf(stderr,"%s : %s\n",Title,Error);
1.1       root      104: }
                    105: 
1.1.1.2   root      106: 
1.1       root      107: /*-----------------------------------------------------------------------*/
                    108: /*
                    109:   Bring up message(handles full-screen as well as Window)
                    110: */
                    111: int Main_Message(char *lpText, char *lpCaption/*,unsigned int uType*/)
                    112: {
                    113:   int Ret=0;
                    114: 
                    115:   /* Show message */
1.1.1.5   root      116:   fprintf(stderr,"%s: %s\n", lpCaption, lpText);
1.1       root      117: 
                    118:   return(Ret);
                    119: }
                    120: 
1.1.1.7 ! root      121: 
1.1       root      122: /*-----------------------------------------------------------------------*/
                    123: /*
                    124:   Pause emulation, stop sound
                    125: */
                    126: void Main_PauseEmulation(void)
                    127: {
1.1.1.4   root      128:   if( bEmulationActive )
                    129:   {
1.1.1.6   root      130:     Audio_EnableAudio(FALSE);
1.1.1.4   root      131:     bEmulationActive = FALSE;
                    132:   }
1.1       root      133: }
                    134: 
1.1.1.2   root      135: /*-----------------------------------------------------------------------*/
1.1       root      136: /*
                    137:   Start emulation
                    138: */
                    139: void Main_UnPauseEmulation(void)
                    140: {
1.1.1.4   root      141:   if( !bEmulationActive )
                    142:   {
1.1.1.6   root      143:     Audio_EnableAudio(ConfigureParams.Sound.bEnableSound);
1.1.1.4   root      144:     bFullScreenHold = FALSE;      /* Release hold  */
                    145:     Screen_SetFullUpdate();       /* Cause full screen update(to clear all) */
1.1       root      146: 
1.1.1.4   root      147:     bEmulationActive = TRUE;
                    148:   }
1.1       root      149: }
                    150: 
1.1.1.7 ! root      151: 
1.1       root      152: /* ----------------------------------------------------------------------- */
                    153: /*
1.1.1.7 ! root      154:   Message handler
1.1       root      155:   Here we process the SDL events (keyboard, mouse, ...) and map it to
                    156:   Atari IKBD events.
                    157: */
                    158: void Main_EventHandler()
                    159: {
1.1.1.4   root      160:   SDL_Event event;
1.1       root      161: 
1.1.1.4   root      162:   if( SDL_PollEvent(&event) )
                    163:    switch( event.type )
1.1       root      164:    {
                    165:     case SDL_QUIT:
1.1.1.4   root      166:        bQuitProgram = TRUE;
1.1       root      167:        break;
1.1.1.4   root      168:     case SDL_MOUSEMOTION:               /* Read/Update internal mouse position */
                    169:        KeyboardProcessor.Mouse.dx += event.motion.xrel;
                    170:        KeyboardProcessor.Mouse.dy += event.motion.yrel;
1.1       root      171:        break;
                    172:     case SDL_MOUSEBUTTONDOWN:
                    173:        if( event.button.button==SDL_BUTTON_LEFT )
1.1.1.4   root      174:        {
                    175:          if(Keyboard.LButtonDblClk==0)
                    176:            Keyboard.bLButtonDown |= BUTTON_MOUSE;  /* Set button down flag */
                    177:        }
1.1       root      178:        else if( event.button.button==SDL_BUTTON_RIGHT )
1.1.1.4   root      179:          Keyboard.bRButtonDown |= BUTTON_MOUSE;
1.1       root      180:        else if( event.button.button==SDL_BUTTON_MIDDLE )
                    181:          Keyboard.LButtonDblClk = 1;    /* Start double-click sequence in emulation time */
                    182:        break;
                    183:     case SDL_MOUSEBUTTONUP:
                    184:        if( event.button.button==SDL_BUTTON_LEFT )
1.1.1.4   root      185:          Keyboard.bLButtonDown &= ~BUTTON_MOUSE;
1.1       root      186:        else if( event.button.button==SDL_BUTTON_RIGHT )
1.1.1.4   root      187:          Keyboard.bRButtonDown &= ~BUTTON_MOUSE;;
1.1       root      188:        break;
1.1.1.7 ! root      189:     case SDL_KEYDOWN:
        !           190:        Keymap_KeyDown(&event.key.keysym);
1.1       root      191:        break;
1.1.1.7 ! root      192:     case SDL_KEYUP:
        !           193:        Keymap_KeyUp(&event.key.keysym);
1.1       root      194:        break;
                    195:    }
                    196: }
                    197: 
                    198: 
1.1.1.2   root      199: /*-----------------------------------------------------------------------*/
1.1       root      200: /*
                    201:   Check for any passed parameters
                    202: */
                    203: void Main_ReadParameters(int argc, char *argv[])
                    204: {
1.1.1.4   root      205:   int i;
1.1       root      206: 
1.1.1.4   root      207:   /* Scan for any which we can use */
                    208:   for(i=1; i<argc; i++)
1.1       root      209:   {
1.1.1.4   root      210:     if (strlen(argv[i])>0)
1.1       root      211:     {
1.1.1.4   root      212:       if (!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h"))
1.1       root      213:       {
1.1.1.4   root      214:         printf("Usage:\n hatari [options] [disk image name]\n"
                    215:                "Where options are:\n"
                    216:                "  --help or -h          Print this help text and exit.\n"
                    217:                "  --version or -v       Print version number and exit.\n"
                    218:                "  --mono or -m          Start in monochrome mode instead of color.\n"
                    219:                "  --fullscreen or -f    Try to use fullscreen mode.\n"
                    220:                "  --joystick or -j      Emulate a ST joystick with the cursor keys.\n"
                    221:                "  --nosound             Disable sound (faster!).\n"
                    222:                "  --frameskip           Skip every second frame (speeds up emulation!).\n"
                    223:                "  --debug or -D         Allow debug interface.\n"
                    224:                "  --harddrive <dir>     Emulate an ST harddrive\n"
                    225:                "     or -d <dir>         (<dir> = root directory).\n"
                    226:                "  --hdimage <imagename> Emulate an ST harddrive with an image.\n"
                    227:                "  --tos <file>          Use TOS image <file>.\n"
1.1.1.7 ! root      228:                "  --cpulevel <x>        Set the CPU type (x => 680x0) (TOS 2.06 only!).\n"
1.1.1.4   root      229:                "  --compatible          Use a more compatible (but slower) 68000 CPU mode.\n"
1.1.1.5   root      230:                "  --blitter             Enable blitter emulation (unstable!)\n"
                    231:                "  --vdi                 Use extended VDI resolution\n"
1.1.1.7 ! root      232:                "  --memsize <x>         Memory size in MB (x = 0, 1, 2 or 4; 0 for 512kB)\n"
1.1.1.4   root      233:               );
                    234:         exit(0);
1.1       root      235:       }
                    236:       else if (!strcmp(argv[i],"--version") || !strcmp(argv[i],"-v"))
                    237:       {
1.1.1.4   root      238:         printf("This is %s.\n", PROG_NAME);
                    239:         printf("This program is free software licensed under the GNU GPL.\n");
                    240:         exit(0);
1.1       root      241:       }
1.1.1.2   root      242:       else if (!strcmp(argv[i],"--mono") || !strcmp(argv[i],"-m"))
1.1       root      243:       {
1.1.1.4   root      244:         bUseHighRes=TRUE;
                    245:         ConfigureParams.Screen.bUseHighRes=TRUE;
                    246:         STRes=PrevSTRes=ST_HIGH_RES;
1.1       root      247:       }
                    248:       else if (!strcmp(argv[i],"--fullscreen") || !strcmp(argv[i],"-f"))
                    249:       {
1.1.1.4   root      250:         bUseFullscreen=TRUE;
1.1       root      251:       }
                    252:       else if (!strcmp(argv[i],"--joystick") || !strcmp(argv[i],"-j"))
                    253:       {
1.1.1.4   root      254:         ConfigureParams.Joysticks.Joy[1].bCursorEmulation=TRUE;
1.1       root      255:       }
1.1.1.4   root      256:       else if ( !strcmp(argv[i],"--nosound") )
1.1.1.2   root      257:       {
1.1.1.4   root      258:         bDisableSound=TRUE;
                    259:         ConfigureParams.Sound.bEnableSound = FALSE;
1.1.1.2   root      260:       }
                    261:       else if ( !strcmp(argv[i],"--frameskip") )
                    262:       {
1.1.1.7 ! root      263:         ConfigureParams.Screen.bFrameSkip = TRUE;
1.1.1.2   root      264:       }
1.1.1.4   root      265:       else if (!strcmp(argv[i],"--debug") || !strcmp(argv[i],"-D"))
1.1.1.2   root      266:       {
1.1.1.4   root      267:         bEnableDebug=TRUE;
1.1.1.2   root      268:       }
1.1.1.4   root      269:       else if (!strcmp(argv[i],"--hdimage"))
1.1.1.3   root      270:       {
1.1.1.7 ! root      271:        if(i+1 >= argc)
        !           272:           fprintf(stderr, "Missing argument for --hdimage\n");
        !           273:         else
1.1.1.4   root      274:         {
1.1.1.7 ! root      275:           if(strlen(argv[i+1]) <= MAX_FILENAME_LENGTH)
1.1.1.4   root      276:           {
1.1.1.7 ! root      277:             ConfigureParams.HardDisc.bUseHardDiscImage = TRUE;
1.1.1.4   root      278:             strcpy(ConfigureParams.HardDisc.szHardDiscImage, argv[i+1]);
                    279:           }
1.1.1.7 ! root      280:           else fprintf(stderr, "HD image file name too long!\n");
        !           281:           i += 1;
1.1.1.4   root      282:         }
                    283:       }
                    284:       else if (!strcmp(argv[i],"--harddrive") || !strcmp(argv[i],"-d"))
                    285:       {
1.1.1.7 ! root      286:        if(i+1 >= argc)
        !           287:           fprintf(stderr, "Missing argument for --harddrive\n");
        !           288:         else
1.1.1.4   root      289:         {
1.1.1.7 ! root      290:           if(strlen(argv[i+1]) <= MAX_PATH )
        !           291:           {
        !           292:             ConfigureParams.HardDisc.bUseHardDiscDirectories = TRUE;
        !           293:             ConfigureParams.HardDisc.bBootFromHardDisc = TRUE;
        !           294:             strcpy(ConfigureParams.HardDisc.szHardDiscDirectories[0], argv[i+1]);
        !           295:           }
        !           296:           else fprintf(stderr, "HD directory name too long!\n");
1.1.1.4   root      297:           i += 1;
                    298:         }
1.1.1.3   root      299:       }
                    300:       else if (!strcmp(argv[i],"--tos"))
                    301:       {
1.1.1.4   root      302:         if(i+1>=argc)
                    303:           fprintf(stderr,"Missing argument for --tos.\n");
1.1.1.3   root      304:         else
1.1.1.4   root      305:           strncpy(ConfigureParams.TOSGEM.szTOSImageFileName, argv[++i], MAX_FILENAME_LENGTH);
1.1.1.3   root      306:       }
                    307:       else if (!strcmp(argv[i],"--cpulevel"))
                    308:       {
1.1.1.4   root      309:         if(i+1>=argc)
                    310:           fprintf(stderr,"Missing argument for --cpulevel.\n");
                    311:          else
                    312:           cpu_level = atoi(argv[++i]);
                    313:         if(cpu_level<0 || cpu_level>4)
                    314:           cpu_level = 0;
1.1.1.5   root      315:         ConfigureParams.System.nCpuLevel = cpu_level;
1.1.1.3   root      316:       }
                    317:       else if (!strcmp(argv[i],"--compatible") || !strcmp(argv[i],"-d"))
                    318:       {
1.1.1.4   root      319:         cpu_compatible = TRUE;
1.1.1.5   root      320:         ConfigureParams.System.bCompatibleCpu = TRUE;
                    321:       }
                    322:       else if (!strcmp(argv[i],"--blitter"))
                    323:       {
                    324:         bEnableBlitter = TRUE;
                    325:         ConfigureParams.System.bBlitter = TRUE;
                    326:       }
1.1.1.7 ! root      327:       else if (!strcmp(argv[i], "--vdi"))
1.1.1.5   root      328:       {
                    329:         bUseVDIRes = ConfigureParams.TOSGEM.bUseExtGEMResolutions = TRUE;
1.1.1.3   root      330:       }
1.1.1.7 ! root      331:       else if (!strcmp(argv[i], "--memsize"))
        !           332:       {
        !           333:         int memorysize = MEMORY_SIZE_1Mb;
        !           334:         if(i+1 >= argc)
        !           335:           fprintf(stderr,"Missing argument for --memsize.\n");
        !           336:         else
        !           337:           memorysize = atoi(argv[++i]);
        !           338:         if(memorysize == 0)
        !           339:           ConfigureParams.Memory.nMemorySize = MEMORY_SIZE_512Kb;
        !           340:         else if(memorysize == 2)
        !           341:           ConfigureParams.Memory.nMemorySize = MEMORY_SIZE_2Mb;
        !           342:         else if(memorysize == 4)
        !           343:           ConfigureParams.Memory.nMemorySize = MEMORY_SIZE_4Mb;
        !           344:         else  /* Use 1MB as default */
        !           345:           ConfigureParams.Memory.nMemorySize = MEMORY_SIZE_1Mb;
        !           346:       }
1.1       root      347:       else
                    348:       {
1.1.1.4   root      349:         /* Possible passed disc image filename, ie starts with character other than '-' */
                    350:         if (argv[i][0]!='-')
                    351:           strcpy(szBootDiscImage,argv[i]);
1.1.1.3   root      352:         else
1.1.1.4   root      353:           fprintf(stderr,"Illegal parameter: %s\n",argv[i]);
1.1       root      354:       }
                    355:     }
                    356:   }
                    357: }
                    358: 
1.1.1.2   root      359: 
                    360: /*-----------------------------------------------------------------------*/
1.1       root      361: /*
                    362:   Initialise emulation
                    363: */
                    364: void Main_Init(void)
                    365: {
1.1.1.7 ! root      366:   /* Init SDL's video subsystem. Note: Audio and joystick subsystems
1.1.1.5   root      367:      will be initialized later (failures there are not fatal). */
1.1.1.7 ! root      368:   if(SDL_Init(SDL_INIT_VIDEO) < 0)
1.1.1.4   root      369:   {
1.1.1.2   root      370:     fprintf(stderr, "Could not initialize the SDL library:\n %s\n", SDL_GetError() );
                    371:     exit(-1);
1.1.1.4   root      372:   }
1.1.1.2   root      373: 
1.1       root      374:   Misc_SeedRandom(1043618);
1.1.1.4   root      375:   SDLGui_Init();
1.1       root      376:   Printer_Init();
                    377:   RS232_Init();
                    378:   Screen_Init();
                    379:   Floppy_Init();
1.1.1.6   root      380:   Init680x0();                  /* Init CPU emulation */
                    381:   Audio_Init();
1.1.1.7 ! root      382:   Keymap_Init();
        !           383: 
        !           384:   /* Init HD emulation */
        !           385:   if(ConfigureParams.HardDisc.bUseHardDiscImage)
        !           386:   {
        !           387:     char *szHardDiscImage = ConfigureParams.HardDisc.szHardDiscImage;
        !           388:     if( HDC_Init(szHardDiscImage) )
        !           389:       printf("Hard drive image %s mounted.\n", szHardDiscImage);
        !           390:     else
        !           391:       printf("Couldn't open HD file: %s, or no partitions\n", szHardDiscImage);
        !           392:   }
        !           393:   GemDOS_Init();
        !           394:   if(ConfigureParams.HardDisc.bUseHardDiscDirectories)
        !           395:   {
        !           396:     GemDOS_InitDrives();
        !           397:   }
1.1.1.6   root      398: 
                    399:   if(Reset_Cold())              /* Reset all systems, load TOS image */
                    400:   {
                    401:     /* If loading of the TOS failed, we bring up the GUI to let the
                    402:      * user choose another TOS ROM file. */
                    403:     Dialog_DoProperty();
                    404:   }
                    405:   if(!bTosImageLoaded || bQuitProgram)
                    406:   {
                    407:     fprintf(stderr, "Failed to load TOS image!\n");
                    408:     SDL_Quit();
                    409:     exit(-2);
                    410:   }
                    411: 
1.1       root      412:   Intercept_Init();
                    413:   Joy_Init();
                    414:   Sound_Init();
                    415: 
1.1.1.2   root      416:   /* Check passed disc image parameter, boot directly into emulator */
1.1.1.6   root      417:   if(strlen(szBootDiscImage) > 0)
                    418:   {
1.1       root      419:     Floppy_InsertDiscIntoDrive(0,szBootDiscImage);
                    420:   }
                    421: }
                    422: 
1.1.1.6   root      423: 
1.1.1.2   root      424: /*-----------------------------------------------------------------------*/
1.1       root      425: /*
                    426:   Un-Initialise emulation
                    427: */
                    428: void Main_UnInit(void)
                    429: {
                    430:   Screen_ReturnFromFullScreen();
                    431:   Floppy_EjectBothDrives();
                    432:   Floppy_UnInit();
1.1.1.4   root      433:   HDC_UnInit();
1.1       root      434:   RS232_UnInit();
                    435:   Printer_UnInit();
                    436:   Intercept_UnInit();
1.1.1.4   root      437:   GemDOS_UnInitDrives();
1.1.1.5   root      438:   if(Sound_AreWeRecording())
                    439:     Sound_EndRecording();
1.1.1.2   root      440:   Audio_UnInit();
1.1       root      441:   YMFormat_FreeRecording();
1.1.1.4   root      442:   SDLGui_UnInit();
1.1       root      443:   Screen_UnInit();
                    444: 
1.1.1.2   root      445:   /* SDL uninit: */
                    446:   SDL_Quit();
1.1       root      447: }
                    448: 
1.1.1.6   root      449: 
1.1.1.2   root      450: /*-----------------------------------------------------------------------*/
1.1       root      451: /*
                    452:   Main
                    453: */
                    454: int main(int argc, char *argv[])
                    455: {
                    456: 
                    457:   /* Generate random seed */
                    458:   srand( time(NULL) );
                    459: 
                    460:   /* Get working directory, if in MSDev force */
1.1.1.4   root      461:   Misc_FindWorkingDirectory(argv[0]);
1.1       root      462: #ifdef FORCE_WORKING_DIR
                    463:   getcwd(szWorkingDir, MAX_FILENAME_LENGTH);
                    464: #endif
                    465: 
                    466:   /* Create debug files */
                    467:   Debug_OpenFiles();
                    468:   ErrLog_OpenFile();
                    469: 
1.1.1.2   root      470:   /* Set default configuration values: */
                    471:   Configuration_SetDefault();
                    472: 
1.1.1.7 ! root      473:   /* Now load the values from the configuration file */
        !           474:   Configuration_Load();
        !           475: 
1.1       root      476:   /* Check for any passed parameters */
1.1.1.2   root      477:   Main_ReadParameters(argc, argv);
1.1       root      478: 
                    479:   /* Init emulator system */
                    480:   Main_Init();
                    481: 
1.1.1.2   root      482:   /* Switch immediately to fullscreen if user wants to */
                    483:   if( bUseFullscreen )
                    484:     Screen_EnterFullScreen();
                    485: 
1.1.1.4   root      486:   /* Run emulation */
1.1       root      487:   Main_UnPauseEmulation();
1.1.1.4   root      488:   Start680x0();                 /* Start emulation */
1.1       root      489: 
                    490:   /* Un-init emulation system */
1.1.1.7 ! root      491:   Main_UnInit();
1.1       root      492: 
                    493:   /* Close debug files */
                    494:   ErrLog_CloseFile();
                    495:   Debug_CloseFiles();
                    496: 
                    497:   return(0);
                    498: }
1.1.1.3   root      499: 
                    500: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.