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

1.1       root        1: /*
                      2:   Hatari
                      3: */
                      4: 
                      5: #include <time.h>
1.1.1.2   root        6: #include <signal.h>
                      7: #include <sys/time.h>
1.1       root        8: 
                      9: #include <SDL.h>
                     10: 
                     11: #include "main.h"
                     12: #include "configuration.h"
                     13: #include "decode.h"
                     14: #include "dialog.h"
                     15: #include "createDiscImage.h"
                     16: #include "audio.h"
                     17: #include "debug.h"
                     18: #include "joy.h"
                     19: #include "errlog.h"
                     20: #include "file.h"
                     21: #include "floppy.h"
                     22: #include "gemdos.h"
                     23: #include "ikbd.h"
                     24: #include "intercept.h"
                     25: #include "reset.h"
                     26: #include "m68000.h"
                     27: #include "memorySnapShot.h"
                     28: #include "misc.h"
                     29: #include "printer.h"
                     30: #include "rs232.h"
                     31: #include "screen.h"
                     32: #include "shortcut.h"
                     33: #include "sound.h"
                     34: #include "timer.h"
                     35: #include "tos.h"
                     36: #include "video.h"
                     37: #include "view.h"
                     38: #include "ymFormat.h"
1.1.1.2   root       39: #include "debugui.h"
1.1       root       40: 
                     41: #include "uae-cpu/hatari-glue.h"
                     42: 
                     43: 
                     44: extern int quit_program;                  /* Declared in newcpu.c */
                     45: 
1.1.1.2   root       46: SDL_TimerID hSoundTimer;                  /* Handle to sound playback */
1.1       root       47: 
                     48: BOOL bQuitProgram=FALSE;                  /* Flag to quit program cleanly */
                     49: BOOL bUseFullscreen=FALSE;
                     50: BOOL bEmulationActive=EMULATION_ACTIVE;   /* Run emulation when started (we'll be in window mouse mode!) */
                     51: BOOL bAppActive = FALSE;
1.1.1.2   root       52: BOOL bEnableDebug=FALSE;                  /* Enable debug UI? */
1.1       root       53: unsigned int TimerID;                     /* Timer ID for main window */
                     54: char szName[] = { "Hatari" };
                     55: char szBootDiscImage[MAX_FILENAME_LENGTH] = { "" };
                     56: 
                     57: char szWorkingDir[MAX_FILENAME_LENGTH] = { "" };
                     58: char szCurrentDir[MAX_FILENAME_LENGTH] = { "" };
                     59: 
                     60: unsigned char STRam[16*1024*1024];        /* This is our ST Ram, includes all TOS/hardware areas for ease */
                     61: 
                     62: int STSpeedMilliSeconds[] = {             /* Convert option 'nMinMaxSpeed' into milliseconds */
                     63:   1000/50,          /* MINMAXSPEED_MIN(20ms) */
                     64:   1000/66,          /* MINMAXSPEED_1(15ms) */
                     65:   1000/100,         /* MINMAXSPEED_2(10ms) */
                     66:   1000/200,         /* MINMAXSPEED_3(5ms) */
                     67:   1,                /* MINMAXSPEED_MAX(1ms) */
                     68: };
                     69: 
                     70: 
                     71: 
                     72: 
                     73: /*-----------------------------------------------------------------------*/
                     74: /*
                     75:   Save/Restore snapshot of local variables('MemorySnapShot_Store' handles type)
                     76: */
                     77: void Main_MemorySnapShot_Capture(BOOL bSave)
                     78: {
                     79:   int nBytes;
                     80: 
                     81:   /* Save/Restore details */
                     82:   /* Only save/restore area of memory machine ie set to, eg 1Mb */
                     83:   if (bSave) {
                     84:     nBytes = STRamEnd_BusErr;
                     85:     MemorySnapShot_Store(&nBytes,sizeof(nBytes));
                     86:     MemorySnapShot_Store(STRam,nBytes);
                     87:   }
                     88:   else {
                     89:     MemorySnapShot_Store(&nBytes,sizeof(nBytes));
                     90:     MemorySnapShot_Store(STRam,nBytes);
                     91:   }
                     92:   /* And Cart/TOS/Hardware area */
                     93:   MemorySnapShot_Store(&STRam[0xE00000],0x200000);
                     94:   MemorySnapShot_Store(szBootDiscImage,sizeof(szBootDiscImage));
                     95:   MemorySnapShot_Store(szWorkingDir,sizeof(szWorkingDir));
                     96:   MemorySnapShot_Store(szCurrentDir,sizeof(szCurrentDir));
                     97: }
                     98: 
                     99: 
                    100: /*-----------------------------------------------------------------------*/
                    101: /*
                    102:   Error handler
                    103: */
                    104: void Main_SysError(char *Error,char *Title)
                    105: {
1.1.1.2   root      106:   fprintf(stderr,"%s : %s\n",Title,Error);
1.1       root      107: }
                    108: 
1.1.1.2   root      109: 
1.1       root      110: /*-----------------------------------------------------------------------*/
                    111: /*
                    112:   Bring up message(handles full-screen as well as Window)
                    113: */
                    114: int Main_Message(char *lpText, char *lpCaption/*,unsigned int uType*/)
                    115: {
                    116:   int Ret=0;
                    117: 
                    118:   /* Are we in full-screen? */
1.1.1.2   root      119:   if (bInFullScreen)
                    120:     Screen_ReturnFromFullScreen();
1.1       root      121: 
                    122:   /* Show message */
                    123:   fprintf(stderr,"Message (%s):\n %s\n", lpCaption, lpText);
                    124: 
                    125:   return(Ret);
                    126: }
                    127: 
                    128: /*-----------------------------------------------------------------------*/
                    129: /*
                    130:   Pause emulation, stop sound
                    131: */
                    132: void Main_PauseEmulation(void)
                    133: {
1.1.1.2   root      134:   SDL_PauseAudio(1);
1.1       root      135:   bEmulationActive = EMULATION_INACTIVE;
                    136: }
                    137: 
1.1.1.2   root      138: /*-----------------------------------------------------------------------*/
1.1       root      139: /*
                    140:   Start emulation
                    141: */
                    142: void Main_UnPauseEmulation(void)
                    143: {
1.1.1.2   root      144:   SDL_PauseAudio(0);
                    145:   bFullScreenHold = FALSE;      /* Release hold  */
                    146:   Screen_SetFullUpdate();       /* Cause full screen update(to clear all) */
1.1       root      147: 
                    148:   bEmulationActive = EMULATION_ACTIVE;
1.1.1.2   root      149:   Audio_ResetBuffer();
1.1       root      150: }
                    151: 
                    152: /* ----------------------------------------------------------------------- */
                    153: /*
                    154:   Message handler  ( actually called from Video_InterruptHandler_VBL() )
                    155:   Here we process the SDL events (keyboard, mouse, ...) and map it to
                    156:   Atari IKBD events.
                    157: */
                    158: #ifndef SDL_BUTTON_LEFT       /* Seems not to be defined in old SDL versions */
                    159: #define SDL_BUTTON_LEFT    1
                    160: #define SDL_BUTTON_MIDDLE  2
                    161: #define SDL_BUTTON_RIGHT  3
                    162: #endif
                    163: void Main_EventHandler()
                    164: {
                    165:  SDL_Event event;
                    166: 
                    167:  if( SDL_PollEvent(&event) )
                    168:   switch( event.type )
                    169:    {
                    170:     case SDL_QUIT:
                    171:        quit_program=1;
                    172:        bQuitProgram=1;
                    173:        break;
                    174:     case SDL_MOUSEMOTION:
                    175:        View_UpdateSTMousePosition();    /* Read/Update internal mouse position */
                    176:        break;
                    177:     case SDL_MOUSEBUTTONDOWN:
                    178:        if( event.button.button==SDL_BUTTON_LEFT )
                    179:          View_LeftMouseButtonDown();
                    180:        else if( event.button.button==SDL_BUTTON_RIGHT )
                    181:          View_RightMouseButtonDown();
                    182:        else if( event.button.button==SDL_BUTTON_MIDDLE )
                    183:          Keyboard.LButtonDblClk = 1;    /* Start double-click sequence in emulation time */
                    184:        break;
                    185:     case SDL_MOUSEBUTTONUP:
                    186:        if( event.button.button==SDL_BUTTON_LEFT )
                    187:          View_LeftMouseButtonUp();
                    188:        else if( event.button.button==SDL_BUTTON_RIGHT )
                    189:          View_RightMouseButtonUp();
                    190:        break;
                    191:      case SDL_KEYDOWN:
                    192:        View_KeyDown( event.key.keysym.sym, event.key.keysym.mod );
                    193:        break;
                    194:      case SDL_KEYUP:
1.1.1.2   root      195:        View_KeyUp( event.key.keysym.sym, event.key.keysym.mod );
1.1       root      196:        break;
                    197:    }
                    198: }
                    199: 
                    200: 
                    201: 
1.1.1.2   root      202: /*-----------------------------------------------------------------------*/
1.1       root      203: /*
1.1.1.3 ! root      204:   This thread runs at 50fps and passes sound samples to the sound interface and also
1.1       root      205:   set the counter/events to govern emulation speed to match the two together.
                    206: */
1.1.1.3 ! root      207: Uint32 Main_SoundTimerFunc(Uint32 interval, void *param)
1.1       root      208: {
1.1.1.2   root      209:   /* Advance frame counter, used to draw screen to window at 50fps */
1.1       root      210:   VBLCounter++;
1.1.1.3 ! root      211:   return(interval);
1.1       root      212: }
                    213: 
1.1.1.2   root      214: 
                    215: /*-----------------------------------------------------------------------*/
1.1       root      216: /*
1.1.1.2   root      217:   Create sound timer to handle sound
1.1       root      218: */
1.1.1.2   root      219: void Main_CreateSoundTimer(void)
1.1       root      220: {
1.1.1.3 ! root      221:   /* Create thread to run every 20ms (50fps) to handle emulation samples */
        !           222:   hSoundTimer = SDL_AddTimer(20, Main_SoundTimerFunc, NULL);
1.1       root      223: }
                    224: 
1.1.1.2   root      225: /*-----------------------------------------------------------------------*/
1.1       root      226: /*
1.1.1.2   root      227:   Delete sound timer
1.1       root      228: */
1.1.1.2   root      229: void Main_RemoveSoundTimer(void)
1.1       root      230: {
1.1.1.3 ! root      231:   SDL_RemoveTimer(hSoundTimer);
1.1       root      232: }
1.1.1.2   root      233: 
                    234: 
1.1       root      235: 
                    236: /*-----------------------------------------------------------------------*/
                    237: /*
                    238:   Check for any passed parameters
                    239: */
                    240: void Main_ReadParameters(int argc, char *argv[])
                    241: {
                    242:  int i;
                    243: 
                    244:  /* Scan for any which we can use */
                    245:  for(i=1; i<argc; i++)
                    246:   {
                    247:    if (strlen(argv[i])>0)
                    248:     {
                    249:      if (!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h"))
                    250:       {
                    251:        printf("Usage:\n hatari [options] [disk image name]\n"
                    252:               "Where options are:\n"
1.1.1.3 ! root      253:               "  --help or -h          Print this help text and exit.\n"
        !           254:               "  --version or -v       Print version number and exit.\n"
        !           255:               "  --mono or -m          Start in monochrome mode instead of color.\n"
        !           256:               "  --fullscreen or -f    Try to use fullscreen mode.\n"
        !           257:               "  --joystick or -j      Emulate a ST joystick with the cursor keys.\n"
        !           258:               "  --sound or -s         Enable sound.\n"
        !           259:               "  --frameskip           Skip every second frame (speeds up emulation!).\n"
        !           260:               "  --debug or -d         Allow debug interface.\n"
        !           261:               "  --harddrive <dir>     Emulate an ST harddrive\n"
        !           262:               "     or -e <dir>         (<dir> = root directory).\n"
        !           263:               "  --tos <file>          Use TOS image <file>.\n"
        !           264:               "  --cpulevel x          Set the CPU type (x => 680x0) (TOS 2.06 only!).\n"
        !           265:               "  --compatible          Use a more compatible (but slower) 68000 CPU mode.\n"
1.1       root      266:              );
                    267:        exit(0);
                    268:       }
                    269:       else if (!strcmp(argv[i],"--version") || !strcmp(argv[i],"-v"))
                    270:       {
                    271:        printf("This is %s.\n", PROG_NAME);
                    272:        printf("This program is free software licensed under the GNU GPL.\n");
                    273:        exit(0);
                    274:       }
1.1.1.2   root      275:       else if (!strcmp(argv[i],"--mono") || !strcmp(argv[i],"-m"))
1.1       root      276:       {
1.1.1.2   root      277:        bUseHighRes=TRUE;
1.1.1.3 ! root      278:        ConfigureParams.Screen.bUseHighRes=TRUE;
1.1.1.2   root      279:        STRes=PrevSTRes=ST_HIGH_RES;
1.1       root      280:       }
                    281:       else if (!strcmp(argv[i],"--fullscreen") || !strcmp(argv[i],"-f"))
                    282:       {
                    283:        bUseFullscreen=TRUE;
                    284:       }
                    285:       else if (!strcmp(argv[i],"--joystick") || !strcmp(argv[i],"-j"))
                    286:       {
                    287:        ConfigureParams.Joysticks.Joy[1].bCursorEmulation=TRUE;
                    288:       }
1.1.1.2   root      289:       else if (!strcmp(argv[i],"--sound") || !strcmp(argv[i],"-s"))
                    290:       {
                    291:        bDisableSound=FALSE;
                    292:        ConfigureParams.Sound.bEnableSound = TRUE;
                    293:       }
                    294:       else if ( !strcmp(argv[i],"--frameskip") )
                    295:       {
                    296:        ConfigureParams.Screen.Advanced.bFrameSkip = TRUE;
                    297:       }
                    298:       else if (!strcmp(argv[i],"--debug") || !strcmp(argv[i],"-d"))
                    299:       {
                    300:        bEnableDebug=TRUE;
                    301:       }
1.1.1.3 ! root      302:       else if (!strcmp(argv[i],"--harddrive") || !strcmp(argv[i],"-e"))
        !           303:       {
        !           304:        if(i + 1 < argc && strlen(argv[i+1])<=MAX_PATH) { /* both parameters exist */
        !           305:          /* only 1 emulated drive allowed, as of yet.  */
        !           306:          emudrives = malloc( sizeof(EMULATEDDRIVE *) );
        !           307:          emudrives[0] = malloc( sizeof(EMULATEDDRIVE) );
        !           308:           ConfigureParams.HardDisc.nDriveList = DRIVELIST_C;
        !           309:          /* set emulation directory string */
        !           310:          if( argv[i+1][0] != '.' && argv[i+1][0] != '/' )
        !           311:            sprintf( emudrives[0]->hd_emulation_dir, "./%s", argv[i+1]);
        !           312:          else
        !           313:            sprintf( emudrives[0]->hd_emulation_dir, "%s", argv[i+1]);
        !           314:          
        !           315:          fprintf(stderr, "Hard drive emulation, C: <-> %s\n", emudrives[0]->hd_emulation_dir);
        !           316:          i ++;
        !           317:        }
        !           318:       }
        !           319:       else if (!strcmp(argv[i],"--tos"))
        !           320:       {
        !           321:        if(i+1>=argc)
        !           322:          fprintf(stderr,"Missing argument for --tos.\n");
        !           323:         else
        !           324:          strncpy(ConfigureParams.TOSGEM.szTOSImageFileName, argv[++i], MAX_FILENAME_LENGTH);
        !           325:       }
        !           326:       else if (!strcmp(argv[i],"--cpulevel"))
        !           327:       {
        !           328:        if(i+1>=argc)
        !           329:          fprintf(stderr,"Missing argument for --cpulevel.\n");
        !           330:         else
        !           331:          cpu_level = atoi(argv[++i]);
        !           332:        if(cpu_level<0 || cpu_level>4)
        !           333:          cpu_level = 0;
        !           334:       }
        !           335:       else if (!strcmp(argv[i],"--compatible") || !strcmp(argv[i],"-d"))
        !           336:       {
        !           337:        cpu_compatible = TRUE;
        !           338:       }
1.1       root      339:       else
                    340:       {
                    341:        /* Possible passed disc image filename, ie starts with character other than '-' */
                    342:        if (argv[i][0]!='-')
                    343:          strcpy(szBootDiscImage,argv[i]);
1.1.1.3 ! root      344:         else
        !           345:          fprintf(stderr,"Illegal parameter: %s\n",argv[i]);
1.1       root      346:       }
                    347:     }
                    348:   }
                    349: }
                    350: 
1.1.1.2   root      351: 
                    352: /*-----------------------------------------------------------------------*/
1.1       root      353: /*
                    354:   Initialise emulation
                    355: */
                    356: void Main_Init(void)
                    357: {
1.1.1.2   root      358:   /* SDL init: */
1.1.1.3 ! root      359:   if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) < 0 )
1.1.1.2   root      360:    {
                    361:     fprintf(stderr, "Could not initialize the SDL library:\n %s\n", SDL_GetError() );
                    362:     exit(-1);
                    363:    }
                    364: 
1.1       root      365:   Misc_SeedRandom(1043618);
                    366:   Printer_Init();
                    367:   RS232_Init();
                    368:   Configuration_Init();
                    369:   Timer_Init();
                    370:   File_Init();
                    371:   Screen_Init();
                    372:   Floppy_Init();
                    373:   Reset_Cold();
                    374:   GemDOS_Init();
                    375:   Intercept_Init();
                    376:   Joy_Init();
1.1.1.2   root      377:   Audio_Init();
1.1       root      378:   Sound_Init();
1.1.1.2   root      379:   Main_CreateSoundTimer();
1.1       root      380: 
1.1.1.2   root      381:   /* Check passed disc image parameter, boot directly into emulator */
1.1       root      382:   if (strlen(szBootDiscImage)>0) {
                    383:     Floppy_InsertDiscIntoDrive(0,szBootDiscImage);
                    384: //FM    View_ToggleWindowsMouse(MOUSE_ST);
                    385:   }
                    386: }
                    387: 
1.1.1.2   root      388: /*-----------------------------------------------------------------------*/
1.1       root      389: /*
                    390:   Un-Initialise emulation
                    391: */
                    392: void Main_UnInit(void)
                    393: {
                    394:   Screen_ReturnFromFullScreen();
1.1.1.2   root      395:   Main_RemoveSoundTimer();
1.1       root      396:   Floppy_EjectBothDrives();
                    397:   Floppy_UnInit();
                    398:   RS232_UnInit();
                    399:   Printer_UnInit();
                    400:   Intercept_UnInit();
1.1.1.2   root      401:   Audio_UnInit();
1.1       root      402:   YMFormat_FreeRecording();
                    403: //FM  View_LimitCursorToScreen();
                    404:   Screen_UnInit();
                    405: 
                    406: #ifdef USE_DEBUGGER
                    407:   FreeDebugDialog();
                    408: #endif
                    409:   Configuration_UnInit();
1.1.1.2   root      410: 
                    411:   /* SDL uninit: */
                    412:   SDL_Quit();
1.1       root      413: }
                    414: 
1.1.1.2   root      415: /*-----------------------------------------------------------------------*/
1.1       root      416: /*
                    417:   Main
                    418: */
                    419: int main(int argc, char *argv[])
                    420: {
                    421: 
                    422:   /* Generate random seed */
                    423:   srand( time(NULL) );
                    424: 
                    425:   /* Get working directory, if in MSDev force */
                    426:   Misc_FindWorkingDirectory();
                    427: #ifdef FORCE_WORKING_DIR
                    428:   getcwd(szWorkingDir, MAX_FILENAME_LENGTH);
                    429: #endif
                    430: 
                    431:   /* Create debug files */
                    432:   Debug_OpenFiles();
                    433:   ErrLog_OpenFile();
                    434: 
1.1.1.2   root      435:   /* Set default configuration values: */
                    436:   Configuration_SetDefault();
                    437: 
1.1       root      438:   /* Check for any passed parameters */
1.1.1.2   root      439:   Main_ReadParameters(argc, argv);
1.1       root      440: 
                    441:   /* Init emulator system */
                    442:   Main_Init();
                    443: 
1.1.1.2   root      444:   /* Switch immediately to fullscreen if user wants to */
                    445:   if( bUseFullscreen )
                    446:     Screen_EnterFullScreen();
                    447: 
1.1       root      448:   /* Set timing threads to govern timing and debug display */
                    449: //FM  Main_SetSpeedThreadTimer(ConfigureParams.Configure.nMinMaxSpeed);
                    450: //FM  TimerID = SetTimer(hWnd,1,1000,NULL);
                    451: 
                    452: #ifdef USE_DEBUGGER
                    453:   /* Run our debugger */
                    454:   Debugger_Init();
                    455:   Main_UnPauseEmulation();
                    456:   /* Run messages until quit */
                    457:   for(;;) {
                    458:     if (Main_ExecuteWindowsMessage())
                    459:       break;
                    460:   }
                    461: #else
                    462:   /* Run release emulation */
                    463:   Main_UnPauseEmulation();
                    464:   //RunIntructions();
                    465:   Init680x0();         /* Init CPU emulation */
                    466:   Start680x0();        /* Start emulation */
                    467: #endif
                    468: 
                    469:   /* Un-init emulation system */
                    470: //FM  KillTimer(hWnd,TimerID);
                    471:   Main_UnInit();  
                    472: 
                    473:   /* Close debug files */
                    474:   ErrLog_CloseFile();
                    475:   Debug_CloseFiles();
                    476: 
                    477:   return(0);
                    478: }
1.1.1.3 ! root      479: 
        !           480: 
        !           481: 
        !           482: 

unix.superglobalmegacorp.com

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