Beispiel #1
0
static void SPCPlayFreeze(void)
{
	S9xSetSoundMute(true);

	oldNextAPUTimerPos = IAPU.NextAPUTimerPos;
	oldAPUTimerCounter = IAPU.APUTimerCounter;
	oldCPUCycles       = CPU.Cycles;

	for (int i = 0; i < 8; i++)
    {
		SoundData.channels[i].previous16[0] = (int16) SoundData.channels[i].previous[0];
		SoundData.channels[i].previous16[1] = (int16) SoundData.channels[i].previous[1];
    }

	memcpy(StoredAPU,          &APU,          sizeof(SAPU));
	memcpy(StoredAPURegisters, &APURegisters, sizeof(SAPURegisters));
	memcpy(StoredSoundData,    &SoundData,    sizeof(SSoundData));
	memcpy(StoredIAPURAM,      IAPU.RAM,      0x10000);

	S9xSetSoundMute(false);

	APU.Cycles = 0;
	IAPU.NextAPUTimerPos = 0;
	CPU.Cycles = Timings.H_Max;

	IAPU.APUExecuting = true;
}
Beispiel #2
0
static void Freeze()
{
   char buffer[1024];
   int i;

   S9xSetSoundMute(TRUE);

   S9xSRTCPreSaveState();

   for (i = 0; i < 8; i++)
   {
      SoundData.channels [i].previous16 [0] = (int16) SoundData.channels [i].previous [0];
      SoundData.channels [i].previous16 [1] = (int16) SoundData.channels [i].previous [1];
   }
   sprintf(buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION);
   statef_write(buffer, strlen(buffer));
   sprintf(buffer, "NAM:%06d:%s%c", strlen(Memory.ROMFilename) + 1,
           Memory.ROMFilename, 0);
   statef_write(buffer, strlen(buffer) + 1);
   FreezeStruct("CPU", &CPU, SnapCPU, COUNT(SnapCPU));
   FreezeStruct("REG", &Registers, SnapRegisters, COUNT(SnapRegisters));
   FreezeStruct("PPU", &PPU, SnapPPU, COUNT(SnapPPU));
   FreezeStruct("DMA", DMA, SnapDMA, COUNT(SnapDMA));

   // RAM and VRAM
   FreezeBlock("VRA", Memory.VRAM, 0x10000);
   FreezeBlock("RAM", Memory.RAM, 0x20000);
   FreezeBlock("SRA", SRAM, 0x20000);
   FreezeBlock("FIL", Memory.FillRAM, 0x8000);
   if (Settings.APUEnabled)
   {
      // APU
      FreezeStruct("APU", &APU, SnapAPU, COUNT(SnapAPU));
      // copy all SPC700 regs to savestate compatible struct
      SAPURegisters spcregs;
      spcregs.P  = IAPU.P;
      spcregs.YA.W = IAPU.YA.W;
      spcregs.X  = IAPU.X;
      spcregs.S  = IAPU.S;
      spcregs.PC = IAPU.PC - IAPU.RAM;
      FreezeStruct("ARE", &spcregs, SnapAPURegisters,
                   COUNT(SnapAPURegisters));

      FreezeBlock("ARA", IAPU.RAM, 0x10000);
      FreezeStruct("SOU", &SoundData, SnapSoundData,
                   COUNT(SnapSoundData));
   }
#ifdef USE_SA1
   if (Settings.SA1)
   {
      SA1Registers.PC = SA1.PC - SA1.PCBase;
      S9xSA1PackStatus();
      FreezeStruct("SA1", &SA1, SnapSA1, COUNT(SnapSA1));
      FreezeStruct("SAR", &SA1Registers, SnapSA1Registers,
                   COUNT(SnapSA1Registers));
   }
#endif
   S9xSetSoundMute(FALSE);
}
Beispiel #3
0
static void SPCPlayDefrost(void)
{
	uint16	savedswitch;
	short   i;

	savedswitch = so.stereo_switch;

	S9xResetAPU();
    S9xSetSoundMute(true);

	memcpy(&APU,          StoredAPU,          sizeof(SAPU));
	memcpy(&APURegisters, StoredAPURegisters, sizeof(SAPURegisters));
	memcpy(&SoundData,    StoredSoundData,    sizeof(SSoundData));
	memcpy(IAPU.RAM,      StoredIAPURAM,      0x10000);

	if (!mbxFinished)
	{
		for (i = 0; i < 4; i++)
			APU.OutPorts[i] = IAPU.RAM[0xf4 + i];

		for (i = 0; i < 3; i++)
		{
			if (IAPU.RAM[0xfa + i] == 0)
				APU.TimerTarget[i] = 0x100;
			else
				APU.TimerTarget[i] = IAPU.RAM[0xfa + i];
		}

		S9xSetAPUControl(IAPU.RAM[0xf1] & 0xcf);   // Don't reset I/O ports
	}

	S9xSetSoundMute(false);

	IAPU.PC = IAPU.RAM + APURegisters.PC;
	S9xAPUUnpackStatus();

	if (APUCheckDirectPage())
	    IAPU.DirectPage = IAPU.RAM + 0x100;
	else
	    IAPU.DirectPage = IAPU.RAM;

	IAPU.APUExecuting = true;

	IAPU.NextAPUTimerPos = oldNextAPUTimerPos;
	IAPU.APUTimerCounter = oldAPUTimerCounter;
	CPU.Cycles           = oldCPUCycles;

	S9xFixSoundAfterSnapshotLoad(SNAPSHOT_VERSION);

	so.stereo_switch = savedswitch;
}
Beispiel #4
0
void PowerCallback (int unknown, int pwrflags)
{
	if (pwrflags & PSP_POWER_CB_HOLD_SWITCH)
	{
		extern bool8 g_bShowProfilerIno;
		g_bShowProfilerInfo = (! g_bShowProfilerInfo);
	}
	
	if (pwrflags & PSP_POWER_CB_POWER_SWITCH){
		if (g_bROMLoaded) {
			scePowerSetClockFrequency (222, 222, 111);
			S9xSetSoundMute           (TRUE);
			g_bSleep = true;
			save_config     ();
			Memory.SaveSRAM (S9xGetFilename ("srm"));
		}
	} else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE) {
		g_bSleep = false;
	}

	if (pwrflags & PSP_POWER_CB_BATTERY_LOW){
		scePowerSetClockFrequency (222,222,111);
		S9xSetInfoString          ("WARNING: PSP Battery is Low! (Automatically Throttling CPU)");
	}

	int cbid;
	cbid = sceKernelCreateCallback ("Power Callback", (SceKernelCallbackFunction)PowerCallback, NULL);
	scePowerRegisterCallback       (0, cbid);
}
Beispiel #5
0
bool SnesEmu::init(const QString &diskPath, QString *error) {
	S9xSetSoundMute(FALSE);
	setDefaultSettings();
	S9xSetPlaybackRate();
	m_frame = QImage(512, 239, QImage::Format_RGB16);
	setVideoSrcRect(QRectF(0.0f, 0.0f, 256.0f, 224.f));
	setFrameRate(60);

	GFX.Pitch = 512 * 2;
	GFX.RealPitch = 512 * 2;
	GFX.PPL = GFX.Pitch >> 1;
	GFX.PPLx2 = GFX.Pitch;
	GFX.Screen = (u8 *)m_frame.bits();
	GFX.SubScreen = (u8 *) malloc(GFX.RealPitch * 239);
	GFX.ZBuffer = (u8 *) malloc(GFX.PPL * 239);
	GFX.SubZBuffer = (u8 *) malloc(GFX.PPL * 239);
	GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;

	if (!GFX.Screen || !GFX.SubScreen || !GFX.ZBuffer || !Memory.Init() || !S9xInitAPU() || !GFX.SubZBuffer ) {
		*error = tr("SNES emulation init failed!");
		return false;
	}
	S9xInitSound();
	if (!S9xGraphicsInit()) {
		*error = tr("SNES emulation init failed!");
		return false;
	}
	S9xReset();
	setDefaultSettings();
	*error = setDisk(diskPath);
	return error->isEmpty();
}
Beispiel #6
0
void snes_init()
{
   memset(&Settings, 0, sizeof(Settings));
   Settings.MouseMaster = TRUE;
   Settings.SuperScopeMaster = TRUE;
   Settings.JustifierMaster = TRUE;
   Settings.MultiPlayer5Master = TRUE;
   Settings.FrameTimePAL = 20000;
   Settings.FrameTimeNTSC = 16667;
   Settings.SixteenBitSound = TRUE;
   Settings.Stereo = TRUE;
   Settings.SoundPlaybackRate = 32000;
   Settings.SoundInputRate = 32000;
   Settings.SupportHiRes = TRUE;
   Settings.Transparency = TRUE;
   Settings.AutoDisplayMessages = TRUE;
   Settings.InitialInfoStringTimeout = 120;
   Settings.HDMATimingHack = 100;
   Settings.BlockInvalidVRAMAccessMaster = TRUE;
   Settings.StopEmulation = TRUE;
   Settings.WrongMovieStateProtection = TRUE;
   Settings.DumpStreamsMaxFrames = -1;
   Settings.StretchScreenshots = 1;
   Settings.SnapshotScreenshots = TRUE;
   Settings.SkipFrames = AUTO_FRAMERATE;
   Settings.TurboSkipFrames = 15;
   Settings.CartAName[0] = 0;
   Settings.CartBName[0] = 0;
   Settings.AutoSaveDelay = 1;

   CPU.Flags = 0;

   if (!Memory.Init() || !S9xInitAPU())
   {
      Memory.Deinit();
      S9xDeinitAPU();
      fprintf(stderr, "[libsnes]: Failed to init Memory or APU.\n");
      exit(1);
   }

   S9xInitSound(64, 0);
   S9xSetSoundMute(FALSE);
   S9xSetSamplesAvailableCallback(S9xAudioCallback, NULL);

   S9xSetRenderPixelFormat(RGB555);
   GFX.Pitch = 2048;
   GFX.Screen = (uint16*) calloc(1, GFX.Pitch * 512 * sizeof(uint16));
   S9xGraphicsInit();

   S9xInitInputDevices();
   for (int i = 0; i < 2; i++)
   {
      S9xSetController(i, CTL_JOYPAD, i, 0, 0, 0);
      snes_devices[i] = SNES_DEVICE_JOYPAD;
   }

   S9xUnmapAllControls();
   map_buttons();
}
Beispiel #7
0
gboolean
S9xIdleFunc (gpointer data)
{
    if (needs_fullscreening)
    {
        top_level->enter_fullscreen_mode();
        needs_fullscreening = FALSE;
    }

    if (Settings.Paused)
    {
        S9xSetSoundMute (gui_config->mute_sound);
        S9xSoundStop ();

#ifdef USE_JOYSTICK
        gui_config->flush_joysticks ();
#endif

#ifdef NETPLAY_SUPPORT
        if (Settings.NetPlay && NetPlay.Connected)
        {
            S9xNPSendPause (TRUE);
        }
#endif

        /* Move to a timer-based function to use less CPU */
        g_timeout_add (100, S9xPauseFunc, NULL);
        top_level->update_statusbar ();
        return FALSE;
    }

    if (syncing)
        S9xSyncSpeedFinish ();

    S9xCheckPointerTimer ();

    S9xProcessEvents (TRUE);

#ifdef NETPLAY_SUPPORT
    if (!S9xNetplayPush ())
    {
#endif
        
    if(top_level->user_rewind)
        top_level->user_rewind = stateMan.pop();
    else if(IPPU.TotalEmulatedFrames % gui_config->rewindGranularity == 0)
        stateMan.push();
        
    S9xMainLoop ();
    S9xMixSound ();

#ifdef NETPLAY_SUPPORT
        S9xNetplayPop ();
    }
#endif

    return TRUE;
}
Beispiel #8
0
/*******************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
 
  (c) Copyright 1996 - 2002 Gary Henderson ([email protected]) and
                            Jerremy Koot ([email protected])

  (c) Copyright 2001 - 2004 John Weidman ([email protected])

  (c) Copyright 2002 - 2004 Brad Jorsch ([email protected]),
                            funkyass ([email protected]),
                            Joel Yliluoma (http://iki.fi/bisqwit/)
                            Kris Bleakley ([email protected]),
                            Matthew Kendora,
                            Nach ([email protected]),
                            Peter Bortas ([email protected]) and
                            zones ([email protected])

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003 zsKnight ([email protected]),
                            _Demo_ ([email protected]), and Nach

  C4 C++ code
  (c) Copyright 2003 Brad Jorsch

  DSP-1 emulator code
  (c) Copyright 1998 - 2004 Ivar ([email protected]), _Demo_, Gary Henderson,
                            John Weidman, neviksti ([email protected]),
                            Kris Bleakley, Andreas Naive

  DSP-2 emulator code
  (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
                     Lord Nightmare ([email protected]

  OBC1 emulator code
  (c) Copyright 2001 - 2004 zsKnight, pagefault ([email protected]) and
                            Kris Bleakley
  Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code
  (c) Copyright 2002 Matthew Kendora with research by
                     zsKnight, John Weidman, and Dark Force

  S-DD1 C emulator code
  (c) Copyright 2003 Brad Jorsch with research by
                     Andreas Naive and John Weidman
 
  S-RTC C emulator code
  (c) Copyright 2001 John Weidman
  
  ST010 C++ emulator code
  (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora

  Super FX x86 assembler emulator code 
  (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 

  Super FX C emulator code 
  (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman


  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004 Marcus Comstedt ([email protected]) 

 
  Specific ports contains the works of other authors. See headers in
  individual files.
 
  Snes9x homepage: http://www.snes9x.com
 
  Permission to use, copy, modify and distribute Snes9x in both binary and
  source form, for non-commercial purposes, is hereby granted without fee,
  providing that this license information and copyright notice appear with
  all copies and any derived work.
 
  This software is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software.
 
  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes
  charging money for Snes9x or software derived from Snes9x.
 
  The copyright holders request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.
 
  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
*******************************************************************************/
void S9xSPCDump (const char *filename)
{
    FILE *fs;

    bool8 read_only = FALSE;
    char def [PATH_MAX];
    char title [PATH_MAX];
    char drive [_MAX_DRIVE];
    char dir [_MAX_DIR];
    char ext [_MAX_EXT];

    S9xSetSoundMute (TRUE);

#if 0
    _splitpath (Memory.ROMFilename, drive, dir, def, ext);
    strcat (def, ".spc");
    sprintf (title, "%s SPC filename",
		read_only ? "Select load" : "Choose save");
    const char *filename;

    filename = S9xSelectFilename (def, ".", "spc", title);
#endif

    fs = fopen (filename, "wb");
    fputs ("SNES-SPC700 Sound File Data 0.10", fs);
    fseek (fs, 37, SEEK_SET);
    fwrite (&APURegisters.PC, 2, 1, fs);

    fputc (APURegisters.YA.B.A, fs);
    fputc (APURegisters.X, fs);
    fputc (APURegisters.YA.B.Y, fs);
    fputc (APURegisters.P, fs);
    fputc (APURegisters.S - 0x100, fs); // ???
    fseek (fs, 256, SEEK_SET);

    fwrite (IAPU.RAM, 1, 65536, fs);

    fwrite (APU.DSP, 1, 192, fs);
    fwrite (APU.ExtraRAM, 1, 64, fs);

    fclose (fs);

    S9xSetSoundMute (FALSE);
}
Beispiel #9
0
bool SNESSystem::Load(const uint8 * rom, uint32 romsize, const uint8 * sram, uint32 sramsize)
{
	Term();

	InitSnes9X();

	if (!Memory.LoadROMSNSF(rom, romsize, sram, sramsize))
		return false;

	Settings.SoundPlaybackRate = soundSampleRate;
	S9xSetSoundMute(FALSE);

	return true;
}
Beispiel #10
0
static void S9xInit() 
{
	if (!Memory.Init () || !S9xInitAPU())
         DIE("Memory or APU failed");

	if (!S9xInitSound ())
		DIE("Sound failed");
	S9xSetSoundMute (TRUE);
	
	// TODO: PAL/NTSC something better than this
	Settings.PAL = Settings.ForcePAL;
	
	Settings.FrameTime = Settings.PAL?Settings.FrameTimePAL:Settings.FrameTimeNTSC;
	Memory.ROMFramesPerSecond = Settings.PAL?50:60;
	
	IPPU.RenderThisFrame = TRUE;
}
Beispiel #11
0
gboolean
S9xPauseFunc (gpointer data)
{
    S9xProcessEvents (TRUE);

    if (!gui_config->rom_loaded)
        return TRUE;

#ifdef NETPLAY_SUPPORT
    if (!S9xNetplayPush ())
    {
        S9xNetplayPop ();
    }
#endif

    if (!Settings.Paused) /* Coming out of pause */
    {
#ifdef USE_JOYSTICK
        /* Clear joystick queues */
        gui_config->flush_joysticks ();
#endif

        S9xSetSoundMute (FALSE);
        S9xSoundStart ();

#ifdef NETPLAY_SUPPORT
        if (Settings.NetPlay && NetPlay.Connected)
        {
            S9xNPSendPause (FALSE);
        }
#endif

        /* Resume high-performance callback */
        idle_func_id = g_idle_add_full (IDLE_FUNC_PRIORITY,
                                        S9xIdleFunc,
                                        NULL,
                                        NULL);
        top_level->update_statusbar ();
        return FALSE;
    }

    return TRUE;
}
Beispiel #12
0
void SnesEmu::sl() {
	if (emsl.save) {
		S9xSRTCPreSaveState();
	} else {
		S9xReset();
	}
	snesCpuSl();
	snesMemSl();
	snesPpuSl();
	snesDmaSl();
	snesSoundSl();
	snesSpuSl();
	if (!emsl.save) {
		S9xFixSoundAfterSnapshotLoad();
		S9xSetPCBase(ICPU.ShiftedPB + Registers.PC);
		S9xReschedule();
		S9xSRTCPostLoadState();
		if (Settings.SDD1)
			S9xSDD1PostLoadState();
		S9xSetSoundMute(FALSE);
	}
}
Beispiel #13
0
static
int Run(int sound)
{
  	int skip=0, done=0, doneLast=0,aim=0,i;
	Settings.NextAPUEnabled = Settings.APUEnabled = sound;
	sal_TimerInit(Settings.FrameTime);
	done=sal_TimerRead()-1;

	if (sound) {
		/*
		Settings.SoundPlaybackRate = mMenuOptions.soundRate;
		Settings.Stereo = mMenuOptions.stereo ? TRUE : FALSE;
		*/
		Settings.SixteenBitSound=true;

		sal_AudioInit(mMenuOptions.soundRate, 16,
					mMenuOptions.stereo, Memory.ROMFramesPerSecond);

		S9xInitSound (mMenuOptions.soundRate,
					mMenuOptions.stereo, sal_AudioGetBufferSize());
		S9xSetPlaybackRate(mMenuOptions.soundRate);
		S9xSetSoundMute (FALSE);

	} else {
		S9xSetSoundMute (TRUE);
	}

  	while(!mEnterMenu) 
  	{
		for (i=0;i<10;i++)
		{
			aim=sal_TimerRead();
			if (done < aim)
			{
				done++;
				if (mMenuOptions.frameSkip == 0) //Auto
					IPPU.RenderThisFrame = (done >= aim);
				else if (IPPU.RenderThisFrame = (--skip <= 0))
					skip = mMenuOptions.frameSkip;

				//Run SNES for one glorious frame
				S9xMainLoop ();

				if (sound) {
					S9xMixSamples((uint8 *) sal_GetCurrentAudioBuffer(),
								sal_AudioGetSampleCount());
					sal_SubmitSamples();
				}
//				HandleQuickStateRequests();
			}
			if (done>=aim) break; // Up to date now
			if (mEnterMenu) break;
		}
		done=aim; // Make sure up to date
		HandleQuickStateRequests();
  	}

	if (sound)
		sal_AudioClose();

	mEnterMenu=0;
	return mEnterMenu;

}
Beispiel #14
0
int SnesInit()
{
	ZeroMemory (&Settings, sizeof (Settings));

	Settings.JoystickEnabled = FALSE;
	Settings.SoundPlaybackRate = 44100;
	Settings.Stereo = TRUE;
	Settings.SoundBufferSize = 0;
	Settings.CyclesPercentage = 100;
	Settings.DisableSoundEcho = FALSE;
	Settings.APUEnabled = TRUE;
	Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
	Settings.SkipFrames = AUTO_FRAMERATE;
	Settings.Shutdown = Settings.ShutdownMaster = TRUE;
	Settings.FrameTimePAL = 20000;
	Settings.FrameTimeNTSC = 16667;
	Settings.FrameTime = Settings.FrameTimeNTSC;
	Settings.DisableSampleCaching = FALSE;
	Settings.DisableMasterVolume = TRUE;
	Settings.Mouse = FALSE;
	Settings.SuperScope = FALSE;
	Settings.MultiPlayer5 = FALSE;
	//	Settings.ControllerOption = SNES_MULTIPLAYER5;
	Settings.ControllerOption = 0;

	Settings.InterpolatedSound = TRUE;
	Settings.StarfoxHack = TRUE;
	
	Settings.ForceTransparency = FALSE;
	Settings.Transparency = TRUE;
	Settings.SixteenBit = TRUE;
	
	Settings.SupportHiRes = FALSE;
	Settings.NetPlay = FALSE;
	Settings.ServerName [0] = 0;
	Settings.AutoSaveDelay = 30;
	Settings.ApplyCheats = TRUE;
	Settings.TurboMode = FALSE;
	Settings.TurboSkipFrames = 15;
	Settings.ThreadSound = FALSE;
	Settings.SoundSync = FALSE;
	//Settings.NoPatch = true;		

	Settings.SuperFX = TRUE;
	Settings.DSP1Master = TRUE;
	Settings.SA1 = TRUE;
	Settings.C4 = TRUE;
	Settings.SDD1 = TRUE;

	GFX.Pitch = 320 * 2;
	GFX.RealPitch = 320 * 2;
	GFX.Screen = (uint8 *) sal_VideoGetBuffer();
	
	GFX.SubScreen = (uint8 *)malloc(GFX.RealPitch * 480 * 2); 
	GFX.ZBuffer =  (uint8 *)malloc(GFX.RealPitch * 480 * 2); 
	GFX.SubZBuffer = (uint8 *)malloc(GFX.RealPitch * 480 * 2);
	GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
	GFX.PPL = GFX.Pitch >> 1;
	GFX.PPLx2 = GFX.Pitch;
	GFX.ZPitch = GFX.Pitch >> 1;
	
	if (Settings.ForceNoTransparency)
         Settings.Transparency = FALSE;

	if (Settings.Transparency)
         Settings.SixteenBit = TRUE;

	Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;

	if (!Memory.Init () || !S9xInitAPU())
	{
		S9xMessage (0,0,"Failed to init memory");
		return SAL_ERROR;
	}

	//S9xInitSound ();
	
	//S9xSetRenderPixelFormat (RGB565);
	S9xSetSoundMute (TRUE);

	if (!S9xGraphicsInit ())
	{
         	S9xMessage (0,0,"Failed to init graphics");
		return SAL_ERROR;
	}

	return SAL_OK;
}
Beispiel #15
0
static pascal OSStatus MusicBoxEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *userData)
{
    #pragma unused (inHandlerCallRef)

    OSStatus	err, result = eventNotHandledErr;
	WindowRef	tWindowRef = (WindowRef) userData;

	switch (GetEventClass(inEvent))
	{
		case kEventClassCommand:
			switch (GetEventKind(inEvent))
			{
				HICommand	cmd;

				case kEventCommandUpdateStatus:
					err = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand, nil, sizeof(HICommand), nil, &cmd);
					if (err == noErr && cmd.commandID == 'clos')
					{
						UpdateMenuCommandStatus(false);
						result = noErr;
					}

					break;

				case kEventCommandProcess:
					err = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand, nil, sizeof(HICommand), nil, &cmd);
					if (err == noErr)
					{
						HIViewRef	root, c1, c2, c3, c4;
						HIViewID	cid;
						Rect		rct;

						switch (cmd.commandID)
						{
							case 'bar1': so.stereo_switch ^= (1 <<  0);	result = noErr;	break;
							case 'bar2': so.stereo_switch ^= (1 <<  1);	result = noErr;	break;
							case 'bar3': so.stereo_switch ^= (1 <<  2);	result = noErr;	break;
							case 'bar4': so.stereo_switch ^= (1 <<  3);	result = noErr;	break;
							case 'bar5': so.stereo_switch ^= (1 <<  4);	result = noErr;	break;
							case 'bar6': so.stereo_switch ^= (1 <<  5);	result = noErr;	break;
							case 'bar7': so.stereo_switch ^= (1 <<  6);	result = noErr;	break;
							case 'bar8': so.stereo_switch ^= (1 <<  7);	result = noErr;	break;
							case 'bar9': so.stereo_switch ^= (1 <<  8);	result = noErr;	break;
							case 'bara': so.stereo_switch ^= (1 <<  9);	result = noErr;	break;
							case 'barb': so.stereo_switch ^= (1 << 10);	result = noErr;	break;
							case 'barc': so.stereo_switch ^= (1 << 11);	result = noErr;	break;
							case 'bard': so.stereo_switch ^= (1 << 12);	result = noErr;	break;
							case 'bare': so.stereo_switch ^= (1 << 13);	result = noErr;	break;
							case 'barf': so.stereo_switch ^= (1 << 14);	result = noErr;	break;
							case 'bar0': so.stereo_switch ^= (1 << 15);	result = noErr;	break;

							case 'Paus':
								mboxPause = !mboxPause;
								S9xSetSoundMute(mboxPause);
								result = noErr;
								break;

							case 'Tr_i':
								showIndicator = !showIndicator;

								root = HIViewGetRoot(tWindowRef);
								cid.id = 0;

								cid.signature = 'Pane';
								HIViewFindByID(root, cid, &c1);
								HIViewSetVisible(c1, false);
								cid.signature = 'iMaG';
								HIViewFindByID(root, cid, &c2);
								HIViewSetVisible(c2, false);
								cid.signature = 'rCTL';
								HIViewFindByID(root, cid, &c3);
								HIViewSetVisible(c3, false);
								cid.signature = 'bCTL';
								HIViewFindByID(root, cid, &c4);
								HIViewSetVisible(c4, false);

								GetWindowBounds(tWindowRef, kWindowStructureRgn,  &rct);
								rct.bottom = rct.top + (showIndicator ? mbxOpenedHeight : mbxClosedHeight);

								err = TransitionWindow(tWindowRef, kWindowSlideTransitionEffect, kWindowResizeTransitionAction, &rct);

								HIViewSetVisible(c1, true);
								HIViewSetVisible(c2, true);
								HIViewSetVisible(c3, true);
								HIViewSetVisible(c4, true);

								result = noErr;
								break;

							case 'DONE':
								QuitAppModalLoopForWindow(tWindowRef);
								result = noErr;
								break;

							case 'HEAD':
								showIndicator = !showIndicator;
								SPCPlayDefrost();
								showIndicator = !showIndicator;
								result = noErr;
								break;

							case 'S_EF':
								HideWindow(tWindowRef);
								showIndicator = !showIndicator;
								ConfigureSoundEffects();
								showIndicator = !showIndicator;
								ShowWindow(tWindowRef);
								result = noErr;
						}
					}
			}
	}

	return result;
}
static int ReadOrigSnapshot (STREAM snap)
{
    char buffer [_MAX_PATH];
    char rom_filename [_MAX_PATH];
    int result;
    int i;
    int j;

    int version;
    int len = strlen (ORIG_SNAPSHOT_MAGIC) + 1 + 4 + 1;
    if (READ_STREAM (buffer, len, snap) != len)
	return (WRONG_FORMAT);
    if (strncmp (buffer, ORIG_SNAPSHOT_MAGIC, strlen (ORIG_SNAPSHOT_MAGIC)) != 0)
	return (WRONG_FORMAT);
    if ((version = atoi (&buffer [strlen (SNAPSHOT_MAGIC) + 1])) > ORIG_SNAPSHOT_VERSION)
	return (WRONG_VERSION);

    if ((result = ReadBlock ("NAM:", rom_filename, _MAX_PATH, snap)) != SUCCESS)
	return (result);

    if ((result = ReadBlock ("HiR:", buffer, 0x41, snap)) != SUCCESS)
	return (result);

    if (strcasecmp (rom_filename, Memory.ROMFilename) != 0 &&
	strcasecmp (S9xBasename (rom_filename), S9xBasename (Memory.ROMFilename)) != 0)
    {
	S9xMessage (S9X_WARNING, S9X_FREEZE_ROM_NAME,
		    "Current loaded ROM image doesn't match that required by freeze-game file.");
    }

    S9xReset ();
    S9xSetSoundMute (TRUE);
    if ((result = ReadBlock ("CPU:", &OrigCPU, sizeof (OrigCPU), snap)) != SUCCESS)
	return (result);
    OrigCPU.FastROMSpeed = OrigCPU.FastROMSpeed_old;
    Memory.FixROMSpeed ();
    if (version == 3)
    {
	OrigCPU.Cycles = OrigCPU.Cycles_old;
	OrigCPU.NextEvent = OrigCPU.NextEvent_old;
	OrigCPU.V_Counter = OrigCPU.V_Counter_old;
	OrigCPU.MemSpeed = OrigCPU.MemSpeed_old;
	OrigCPU.MemSpeedx2 = OrigCPU.MemSpeedx2_old;
	OrigCPU.FastROMSpeed = OrigCPU.FastROMSpeed_old;
    }
    CPU.Flags = OrigCPU.Flags;
    CPU.BranchSkip = OrigCPU.BranchSkip;
    CPU.NMIActive = OrigCPU.NMIActive;
    CPU.IRQActive = OrigCPU.IRQActive;
    CPU.WaitingForInterrupt = OrigCPU.WaitingForInterrupt;
    CPU.WhichEvent = OrigCPU.WhichEvent;
    CPU.Cycles = OrigCPU.Cycles;
    CPU.NextEvent = OrigCPU.NextEvent;
    CPU.V_Counter = OrigCPU.V_Counter;
    CPU.MemSpeed = OrigCPU.MemSpeed;
    CPU.MemSpeedx2 = OrigCPU.MemSpeedx2;
    CPU.FastROMSpeed = OrigCPU.FastROMSpeed;

    if ((result = ReadBlock ("REG:", &OrigRegisters, sizeof (OrigRegisters), snap)) != SUCCESS)
	return (result);

    Registers = *(struct SRegisters *) &OrigRegisters;

    if ((result = ReadBlock ("PPU:", &OrigPPU, sizeof (OrigPPU), snap)) != SUCCESS)
	return (result);

    if (version == 2)
    {
	OrigPPU.OBJNameSelect = OrigPPU.OBJNameSelect_old << 13;
	OrigPPU.OBJNameBase <<= 1;
	OrigPPU.OBJNameSelect <<= 13;
    }
    PPU.BGMode = OrigPPU.BGMode;
    PPU.BG3Priority = OrigPPU.BG3Priority;
    PPU.Brightness = OrigPPU.Brightness;

    PPU.VMA.High = OrigPPU.VMA.High;
    PPU.VMA.Increment = OrigPPU.VMA.Increment;
    PPU.VMA.Address = OrigPPU.VMA.Address;
    PPU.VMA.Mask1 = OrigPPU.VMA.Mask1;
    PPU.VMA.FullGraphicCount = OrigPPU.VMA.FullGraphicCount;
    PPU.VMA.Shift = OrigPPU.VMA.Shift;

    for (i = 0; i < 4; i++)
    {
	PPU.BG[i].SCBase = OrigPPU.BG[i].SCBase;
	PPU.BG[i].VOffset = OrigPPU.BG[i].VOffset;
	PPU.BG[i].HOffset = OrigPPU.BG[i].HOffset;
	PPU.BG[i].BGSize = OrigPPU.BG[i].BGSize;
	PPU.BG[i].NameBase = OrigPPU.BG[i].NameBase;
	PPU.BG[i].SCSize = OrigPPU.BG[i].SCSize;
    }

    PPU.CGFLIP = OrigPPU.CGFLIP;
    for (i = 0; i < 256; i++)
	PPU.CGDATA [i] = OrigPPU.CGDATA [i];
    PPU.FirstSprite = OrigPPU.FirstSprite;
    for (i = 0; i < 128; i++)
    {
	PPU.OBJ[i].HPos = OrigPPU.OBJ [i].HPos;
	PPU.OBJ[i].VPos = OrigPPU.OBJ [i].VPos;
	PPU.OBJ[i].Name = OrigPPU.OBJ [i].Name;
	PPU.OBJ[i].VFlip = OrigPPU.OBJ [i].VFlip;
	PPU.OBJ[i].HFlip = OrigPPU.OBJ [i].HFlip;
	PPU.OBJ[i].Priority = OrigPPU.OBJ [i].Priority;
	PPU.OBJ[i].Palette = OrigPPU.OBJ [i].Palette;
	PPU.OBJ[i].Size = OrigPPU.OBJ [i].Size;
    }
    PPU.OAMPriorityRotation = OrigPPU.OAMPriorityRotation;
    PPU.OAMAddr = OrigPPU.OAMAddr;

    PPU.OAMFlip = OrigPPU.OAMFlip;
    PPU.OAMTileAddress = OrigPPU.OAMTileAddress;
    PPU.IRQVBeamPos = OrigPPU.IRQVBeamPos;
    PPU.IRQHBeamPos = OrigPPU.IRQHBeamPos;
    PPU.VBeamPosLatched = OrigPPU.VBeamPosLatched;
    PPU.HBeamPosLatched = OrigPPU.HBeamPosLatched;

    PPU.HBeamFlip = OrigPPU.HBeamFlip;
    PPU.VBeamFlip = OrigPPU.VBeamFlip;
    PPU.HVBeamCounterLatched = OrigPPU.HVBeamCounterLatched;

    PPU.MatrixA = OrigPPU.MatrixA;
    PPU.MatrixB = OrigPPU.MatrixB;
    PPU.MatrixC = OrigPPU.MatrixC;
    PPU.MatrixD = OrigPPU.MatrixD;
    PPU.CentreX = OrigPPU.CentreX;
    PPU.CentreY = OrigPPU.CentreY;
    PPU.Joypad1ButtonReadPos = OrigPPU.Joypad1ButtonReadPos;
    PPU.Joypad2ButtonReadPos = OrigPPU.Joypad2ButtonReadPos;
    PPU.Joypad3ButtonReadPos = OrigPPU.Joypad3ButtonReadPos;

    PPU.CGADD = OrigPPU.CGADD;
    PPU.FixedColourRed = OrigPPU.FixedColourRed;
    PPU.FixedColourGreen = OrigPPU.FixedColourGreen;
    PPU.FixedColourBlue = OrigPPU.FixedColourBlue;
    PPU.SavedOAMAddr = OrigPPU.SavedOAMAddr;
    PPU.ScreenHeight = OrigPPU.ScreenHeight;
    PPU.WRAM = OrigPPU.WRAM;
    PPU.ForcedBlanking = OrigPPU.ForcedBlanking;
    PPU.OBJNameSelect = OrigPPU.OBJNameSelect;
    PPU.OBJSizeSelect = OrigPPU.OBJSizeSelect;
    PPU.OBJNameBase = OrigPPU.OBJNameBase;
    PPU.OAMReadFlip = OrigPPU.OAMReadFlip;
    memmove (PPU.OAMData, OrigPPU.OAMData, sizeof (PPU.OAMData));
    PPU.VTimerEnabled = OrigPPU.VTimerEnabled;
    PPU.HTimerEnabled = OrigPPU.HTimerEnabled;
    PPU.HTimerPosition = OrigPPU.HTimerPosition;
    PPU.Mosaic = OrigPPU.Mosaic;
    memmove (PPU.BGMosaic, OrigPPU.BGMosaic, sizeof (PPU.BGMosaic));
    PPU.Mode7HFlip = OrigPPU.Mode7HFlip;
    PPU.Mode7VFlip = OrigPPU.Mode7VFlip;
    PPU.Mode7Repeat = OrigPPU.Mode7Repeat;
    PPU.Window1Left = OrigPPU.Window1Left;
    PPU.Window1Right = OrigPPU.Window1Right;
    PPU.Window2Left = OrigPPU.Window2Left;
    PPU.Window2Right = OrigPPU.Window2Right;
    for (i = 0; i < 6; i++)
    {
	PPU.ClipWindowOverlapLogic [i] = OrigPPU.ClipWindowOverlapLogic [i];
	PPU.ClipWindow1Enable [i] = OrigPPU.ClipWindow1Enable [i];
	PPU.ClipWindow2Enable [i] = OrigPPU.ClipWindow2Enable [i];
	PPU.ClipWindow1Inside [i] = OrigPPU.ClipWindow1Inside [i];
	PPU.ClipWindow2Inside [i] = OrigPPU.ClipWindow2Inside [i];
    }
    PPU.CGFLIPRead = OrigPPU.CGFLIPRead;
    PPU.Need16x8Mulitply = OrigPPU.Need16x8Mulitply;

    IPPU.ColorsChanged = TRUE;
    IPPU.OBJChanged = TRUE;
    S9xFixColourBrightness ();
    IPPU.RenderThisFrame = FALSE;

    if ((result = ReadBlock ("DMA:", OrigDMA, sizeof (OrigDMA), snap)) != SUCCESS)
	return (result);

    for (i = 0; i < 8; i++)
    {
	DMA[i].TransferDirection = OrigDMA[i].TransferDirection;
	DMA[i].AAddressFixed = OrigDMA[i].AAddressFixed;
	DMA[i].AAddressDecrement = OrigDMA[i].AAddressDecrement;
	DMA[i].TransferMode = OrigDMA[i].TransferMode;
	DMA[i].ABank = OrigDMA[i].ABank;
	DMA[i].AAddress = OrigDMA[i].AAddress;
	DMA[i].Address = OrigDMA[i].Address;
	DMA[i].BAddress = OrigDMA[i].BAddress;
	DMA[i].TransferBytes = OrigDMA[i].TransferBytes;
	DMA[i].HDMAIndirectAddressing = OrigDMA[i].HDMAIndirectAddressing;
	DMA[i].IndirectAddress = OrigDMA[i].IndirectAddress;
	DMA[i].IndirectBank = OrigDMA[i].IndirectBank;
	DMA[i].Repeat = OrigDMA[i].Repeat;
	DMA[i].LineCount = OrigDMA[i].LineCount;
	DMA[i].FirstLine = OrigDMA[i].FirstLine;
    }

    if ((result = ReadBlock ("VRA:", Memory.VRAM, 0x10000, snap)) != SUCCESS)
	return (result);
    if ((result = ReadBlock ("RAM:", Memory.RAM, 0x20000, snap)) != SUCCESS)
	return (result);
    if ((result = ReadBlock ("SRA:", Memory.SRAM, 0x10000, snap)) != SUCCESS)
	return (result);
    if ((result = ReadBlock ("FIL:", Memory.FillRAM, 0x8000, snap)) != SUCCESS)
	return (result);
    if (ReadBlock ("APU:", &OrigAPU, sizeof (OrigAPU), snap) == SUCCESS)
    {
	APU = *(struct SAPU *) &OrigAPU;

	if ((result = ReadBlock ("ARE:", &OrigAPURegisters,
				 sizeof (OrigAPURegisters), snap)) != SUCCESS)
	    return (result);
	APURegisters = *(struct SAPURegisters *) &OrigAPURegisters;
	if ((result = ReadBlock ("ARA:", IAPU.RAM, 0x10000, snap)) != SUCCESS)
	    return (result);
	if ((result = ReadBlock ("SOU:", &OrigSoundData,
				 sizeof (SOrigSoundData), snap)) != SUCCESS)
	    return (result);

	SoundData.master_volume_left = OrigSoundData.master_volume_left;
	SoundData.master_volume_right = OrigSoundData.master_volume_right;
	SoundData.echo_volume_left = OrigSoundData.echo_volume_left;
	SoundData.echo_volume_right = OrigSoundData.echo_volume_right; 
	SoundData.echo_enable = OrigSoundData.echo_enable;
	SoundData.echo_feedback = OrigSoundData.echo_feedback;
	SoundData.echo_ptr = OrigSoundData.echo_ptr;
	SoundData.echo_buffer_size = OrigSoundData.echo_buffer_size;
	SoundData.echo_write_enabled = OrigSoundData.echo_write_enabled;
	SoundData.echo_channel_enable = OrigSoundData.echo_channel_enable;
	SoundData.pitch_mod = OrigSoundData.pitch_mod;

	for (i = 0; i < 3; i++)
	    SoundData.dummy [i] = OrigSoundData.dummy [i];
	for (i = 0; i < NUM_CHANNELS; i++)
	{
	    SoundData.channels [i].state = OrigSoundData.channels [i].state;
	    SoundData.channels [i].type = OrigSoundData.channels [i].type;
	    SoundData.channels [i].volume_left = OrigSoundData.channels [i].volume_left;
	    SoundData.channels [i].volume_right = OrigSoundData.channels [i].volume_right;
	    SoundData.channels [i].hertz = OrigSoundData.channels [i].frequency;
	    SoundData.channels [i].count = OrigSoundData.channels [i].count;
	    SoundData.channels [i].loop = OrigSoundData.channels [i].loop;
	    SoundData.channels [i].envx = OrigSoundData.channels [i].envx;
	    SoundData.channels [i].left_vol_level = OrigSoundData.channels [i].left_vol_level;
	    SoundData.channels [i].right_vol_level = OrigSoundData.channels [i].right_vol_level;
	    SoundData.channels [i].envx_target = OrigSoundData.channels [i].envx_target;
	    SoundData.channels [i].env_error = OrigSoundData.channels [i].env_error;
	    SoundData.channels [i].erate = OrigSoundData.channels [i].erate;
	    SoundData.channels [i].direction = OrigSoundData.channels [i].direction;
	    SoundData.channels [i].attack_rate = OrigSoundData.channels [i].attack_rate;
	    SoundData.channels [i].decay_rate = OrigSoundData.channels [i].decay_rate;
	    SoundData.channels [i].sustain_rate = OrigSoundData.channels [i].sustain_rate;
	    SoundData.channels [i].release_rate = OrigSoundData.channels [i].release_rate;
	    SoundData.channels [i].sustain_level = OrigSoundData.channels [i].sustain_level;
	    SoundData.channels [i].sample = OrigSoundData.channels [i].sample;
	    for (j = 0; j < 16; j++)
		SoundData.channels [i].decoded [j] = OrigSoundData.channels [i].decoded [j];

	    for (j = 0; j < 2; j++)
		SoundData.channels [i].previous [j] = OrigSoundData.channels [i].previous [j];

	    SoundData.channels [i].sample_number = OrigSoundData.channels [i].sample_number;
	    SoundData.channels [i].last_block = OrigSoundData.channels [i].last_block;
	    SoundData.channels [i].needs_decode = OrigSoundData.channels [i].needs_decode;
	    SoundData.channels [i].block_pointer = OrigSoundData.channels [i].block_pointer;
	    SoundData.channels [i].sample_pointer = OrigSoundData.channels [i].sample_pointer;
	    SoundData.channels [i].mode = OrigSoundData.channels [i].mode;
	}

	S9xSetSoundMute (FALSE);
	IAPU.PC = IAPU.RAM + APURegisters.PC;
	S9xAPUUnpackStatus ();
	if (APUCheckDirectPage ())
	    IAPU.DirectPage = IAPU.RAM + 0x100;
	else
	    IAPU.DirectPage = IAPU.RAM;
	Settings.APUEnabled = TRUE;
	IAPU.APUExecuting = TRUE;
    }
    else
    {
	Settings.APUEnabled = FALSE;
	IAPU.APUExecuting = FALSE;
	S9xSetSoundMute (TRUE);
    }
    S9xFixSoundAfterSnapshotLoad (1);
    ICPU.ShiftedPB = Registers.PB << 16;
    ICPU.ShiftedDB = Registers.DB << 16;
    S9xSetPCBase (ICPU.ShiftedPB + Registers.PC);
    S9xUnpackStatus ();
    S9xFixCycles ();
    S9xReschedule ();

    return (SUCCESS);
}
Beispiel #17
0
static int Unfreeze()
{
   // notaz: overflowing the damn Symbian stack again
   char buffer [16];
   char rom_filename [512];
   int result;

   int version;

   unsigned int len = strlen(SNAPSHOT_MAGIC) + 1 + 4 + 1;
   if (statef_read(buffer, len) != (int)len)
      return (WRONG_FORMAT);
   if (strncmp(buffer, SNAPSHOT_MAGIC, strlen(SNAPSHOT_MAGIC)) != 0)
      return (WRONG_FORMAT);
   if ((version = atoi(&buffer [strlen(SNAPSHOT_MAGIC) + 1])) > SNAPSHOT_VERSION)
      return (WRONG_VERSION);

   if ((result = UnfreezeBlock("NAM", (uint8*) rom_filename, 512)) != SUCCESS)
      return (result);

   if (strcasecmp(rom_filename, Memory.ROMFilename) != 0 &&
         strcasecmp(S9xBasename(rom_filename), S9xBasename(Memory.ROMFilename)) != 0)
   {
      S9xMessage(S9X_WARNING, S9X_FREEZE_ROM_NAME,
                 "Current loaded ROM image doesn't match that required by freeze-game file.");
   }



   uint32 old_flags = CPU.Flags;
#ifdef USE_SA1
   uint32 sa1_old_flags = SA1.Flags;
#endif
   S9xReset();
   S9xSetSoundMute(TRUE);

   if ((result = UnfreezeStruct("CPU", &CPU, SnapCPU,
                                COUNT(SnapCPU))) != SUCCESS)
      return (result);


   FixROMSpeed();
   CPU.Flags |= old_flags & (DEBUG_MODE_FLAG | TRACE_FLAG |
                             SINGLE_STEP_FLAG | FRAME_ADVANCE_FLAG);
   if ((result = UnfreezeStruct("REG", &Registers, SnapRegisters, COUNT(SnapRegisters))) != SUCCESS)
      return (result);
   if ((result = UnfreezeStruct("PPU", &PPU, SnapPPU, COUNT(SnapPPU))) != SUCCESS)
      return (result);


   IPPU.ColorsChanged = TRUE;
   IPPU.OBJChanged = TRUE;
   CPU.InDMA = FALSE;
   // Restore colors from PPU
   unsigned int i;
   for (i = 0; i < 256; i++)
   {
      IPPU.Red[i] = PPU.CGDATA[i] & 0x1f;
      IPPU.Green[i] = (PPU.CGDATA[i] >> 5) & 0x1f;
      IPPU.Blue[i] = (PPU.CGDATA[i] >> 10) & 0x1f;
   }

   S9xFixColourBrightness();
   IPPU.RenderThisFrame = FALSE;

   if ((result = UnfreezeStruct("DMA", DMA, SnapDMA,
                                COUNT(SnapDMA))) != SUCCESS)
      return (result);

   if ((result = UnfreezeBlock("VRA", Memory.VRAM, 0x10000)) != SUCCESS)
      return (result);

   if ((result = UnfreezeBlock("RAM", Memory.RAM, 0x20000)) != SUCCESS)
      return (result);

   if ((result = UnfreezeBlock("SRA", SRAM, 0x20000)) != SUCCESS)
      return (result);

   if ((result = UnfreezeBlock("FIL", Memory.FillRAM, 0x8000)) != SUCCESS)
      return (result);

   // Restore graphics shadow registers
   GFX.r212c_s = Memory.FillRAM[0x212c];
   GFX.r212d_s = Memory.FillRAM[0x212d];
   GFX.r212e_s = Memory.FillRAM[0x212e];
   GFX.r212f_s = Memory.FillRAM[0x212f];
   GFX.r2130_s = Memory.FillRAM[0x2130];
   GFX.r2131_s = Memory.FillRAM[0x2131];

   if (UnfreezeStruct("APU", &APU, SnapAPU, COUNT(SnapAPU)) == SUCCESS)
   {
      SAPURegisters spcregs;
      if ((result = UnfreezeStruct("ARE", &spcregs, SnapAPURegisters,
                                   COUNT(SnapAPURegisters))) != SUCCESS)
         return (result);
      // reload all SPC700 regs from savestate compatible struct
      IAPU.P = spcregs.P;
      IAPU.YA.W = spcregs.YA.W;
      IAPU.X = spcregs.X;
      IAPU.S = spcregs.S;
      IAPU.PC = IAPU.RAM + spcregs.PC;

      if ((result = UnfreezeBlock("ARA", IAPU.RAM, 0x10000)) != SUCCESS)
         return (result);

      if ((result = UnfreezeStruct("SOU", &SoundData, SnapSoundData,
                                   COUNT(SnapSoundData))) != SUCCESS)
         return (result);

      // notaz: just to be sure
      int u;
      for (u = 0; u < 8; u++)
      {
         SoundData.channels[u].env_ind_attack &= 0xf;
         SoundData.channels[u].env_ind_decay  &= 0x7;
         SoundData.channels[u].env_ind_sustain &= 0x1f;
      }

      S9xSetSoundMute(FALSE);
      S9xAPUUnpackStatus();
      if (APUCheckDirectPage())
         IAPU.DirectPage = IAPU.RAM + 0x100;
      else
         IAPU.DirectPage = IAPU.RAM;
      Settings.APUEnabled = TRUE;
      /*IAPU.APUExecuting*/CPU.APU_APUExecuting = TRUE;
   }
   else
   {
      Settings.APUEnabled = FALSE;
      /*IAPU.APUExecuting*/CPU.APU_APUExecuting = FALSE;
      S9xSetSoundMute(TRUE);
   }
#ifdef USE_SA1
   if ((result = UnfreezeStruct("SA1", &SA1, SnapSA1,
                                COUNT(SnapSA1))) == SUCCESS)
   {
      if ((result = UnfreezeStruct("SAR", &SA1Registers,
                                   SnapSA1Registers, COUNT(SnapSA1Registers))) != SUCCESS)
         return (result);

      S9xFixSA1AfterSnapshotLoad();
      SA1.Flags |= sa1_old_flags & (TRACE_FLAG);
   }
#endif
   S9xFixSoundAfterSnapshotLoad();
   ICPU.ShiftedPB = Registers.PB << 16;
   ICPU.ShiftedDB = Registers.DB << 16;
   S9xSetPCBase(ICPU.ShiftedPB + Registers.PC);

#ifndef ASMCPU
   S9xUnpackStatus();  // not needed
   S9xFixCycles();  // also not needed?
#endif
   S9xReschedule();

   S9xSRTCPostLoadState();
   if (Settings.SDD1)  S9xSDD1PostLoadState();

   return (SUCCESS);
}
Beispiel #18
0
extern "C" int SIStartWithROM(char* rom_filename)
{
  // notify that we're running
  SI_EmulationIsRunning = 1;
  
  // frameskip settings reset
  SI_NextFrameTime = (timeval){0, 0};
  SI_FrameTimeDebt = 0;
  SI_SleptLastFrame = 0;
  
  // ensure dirs exist
  mode_t dir_mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
	mkdir(SI_SRAMPath, dir_mode);
  
  // unix init
	ZeroMemory(&Settings, sizeof(Settings));
	Settings.MouseMaster = TRUE;
	Settings.SuperScopeMaster = TRUE;
	Settings.JustifierMaster = TRUE;
	Settings.MultiPlayer5Master = TRUE;
	Settings.FrameTimePAL = 20000;
	Settings.FrameTimeNTSC = 16667;
	Settings.SixteenBitSound = TRUE;
	Settings.Stereo = TRUE;
  //Settings.Stereo = FALSE;
	Settings.SoundPlaybackRate = 32000;
  //Settings.SoundPlaybackRate = 22050;
	//Settings.SoundInputRate = 32000;
  Settings.SoundInputRate = 32000;
  Settings.SoundSync = FALSE;
	Settings.SupportHiRes = TRUE;
	Settings.Transparency = TRUE;
	Settings.AutoDisplayMessages = TRUE;
	Settings.InitialInfoStringTimeout = 120;
	Settings.HDMATimingHack = 100;
	Settings.BlockInvalidVRAMAccessMaster = TRUE;
	Settings.StopEmulation = TRUE;
	Settings.WrongMovieStateProtection = TRUE;
	Settings.DumpStreamsMaxFrames = -1;
	Settings.StretchScreenshots = 1;
	Settings.SnapshotScreenshots = TRUE;
  if(SI_AutoFrameskip)
    Settings.SkipFrames = AUTO_FRAMERATE;
  else
    Settings.SkipFrames = SI_Frameskip;
  //Settings.SkipFrames = 1;
	Settings.TurboSkipFrames = 15;
	Settings.CartAName[0] = 0;
	Settings.CartBName[0] = 0;
#ifdef NETPLAY_SUPPORT
	Settings.ServerName[0] = 0;
#endif
  
	CPU.Flags = 0;
  
	/*S9xLoadConfigFiles(argv, argc);
	rom_filename = S9xParseArgs(argv, argc);
  
	make_snes9x_dirs();*/
  
	if (!Memory.Init() || !S9xInitAPU())
	{
		fprintf(stderr, "Snes9x: Memory allocation failure - not enough RAM/virtual memory available.\nExiting...\n");
		Memory.Deinit();
		S9xDeinitAPU();
		exit(1);
	}
  
  int samplecount = Settings.SoundPlaybackRate/(Settings.PAL ? 50 : 60);
  int soundBufferSize = samplecount<<(1+(Settings.Stereo?1:0));
  S9xInitSound(soundBufferSize, 0);
	S9xSetSoundMute(TRUE);
  
  S9xReset();
  
  S9xUnmapAllControls();
  S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
  //S9xSetController(1, CTL_JOYPAD, 1, 0, 0, 0);
  
  s9xcommand_t	cmd;
  
	ASSIGN_BUTTONf(SIOS_X,         "Joypad1 X");
	ASSIGN_BUTTONf(SIOS_A,         "Joypad1 A");
	ASSIGN_BUTTONf(SIOS_B,         "Joypad1 B");
	ASSIGN_BUTTONf(SIOS_Y,         "Joypad1 Y");
	ASSIGN_BUTTONf(SIOS_L,         "Joypad1 L");
	ASSIGN_BUTTONf(SIOS_R,         "Joypad1 R");
	ASSIGN_BUTTONf(SIOS_SELECT,    "Joypad1 Select");
	ASSIGN_BUTTONf(SIOS_START,     "Joypad1 Start");
	ASSIGN_BUTTONf(SIOS_UP,        "Joypad1 Up");
	ASSIGN_BUTTONf(SIOS_DOWN,      "Joypad1 Down");
	ASSIGN_BUTTONf(SIOS_LEFT,      "Joypad1 Left");
	ASSIGN_BUTTONf(SIOS_RIGHT,     "Joypad1 Right");
  
	S9xReportControllers();
  
#ifdef GFX_MULTI_FORMAT
	S9xSetRenderPixelFormat(RGB565);
#endif
  
	uint32	saved_flags = CPU.Flags;
	bool8	loaded = FALSE;
  
	if (Settings.Multi)
	{
		loaded = Memory.LoadMultiCart(Settings.CartAName, Settings.CartBName);
    
		if (!loaded)
		{
			char	s1[PATH_MAX + 1], s2[PATH_MAX + 1];
			char	drive[_MAX_DRIVE + 1], dir[_MAX_DIR + 1], fname[_MAX_FNAME + 1], ext[_MAX_EXT + 1];
      
			s1[0] = s2[0] = 0;
      
			if (Settings.CartAName[0])
			{
				_splitpath(Settings.CartAName, drive, dir, fname, ext);
				snprintf(s1, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname);
				if (ext[0] && (strlen(s1) <= PATH_MAX - 1 - strlen(ext)))
				{
					strcat(s1, ".");
					strcat(s1, ext);
				}
			}
      
			if (Settings.CartBName[0])
			{
				_splitpath(Settings.CartBName, drive, dir, fname, ext);
				snprintf(s2, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname);
				if (ext[0] && (strlen(s2) <= PATH_MAX - 1 - strlen(ext)))
				{
					strcat(s2, ".");
					strcat(s2, ext);
				}
			}
      
			loaded = Memory.LoadMultiCart(s1, s2);
		}
	}
	else
    if (rom_filename)
    {
      char rom_path[1024] = {0};
      sprintf(rom_path,"%s%s%s",SI_DocumentsPath,DIR_SEPERATOR,rom_filename);
      
      loaded = Memory.LoadROM(rom_path);
      
      /*if (!loaded && rom_filename[0])
      {
        char	s[PATH_MAX + 1];
        char	drive[_MAX_DRIVE + 1], dir[_MAX_DIR + 1], fname[_MAX_FNAME + 1], ext[_MAX_EXT + 1];
        
        _splitpath(rom_filename, drive, dir, fname, ext);
        snprintf(s, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname);
        if (ext[0] && (strlen(s) <= PATH_MAX - 1 - strlen(ext)))
        {
          strcat(s, ".");
          strcat(s, ext);
        }
        
        loaded = Memory.LoadROM(s);
      }*/
    }
  
	if (!loaded)
	{
		fprintf(stderr, "Error opening the ROM file.\n");
		exit(1);
	}
  
	//NSRTControllerSetup();
	//Memory.LoadSRAM(S9xGetFilename(".srm", SRAM_DIR));
  SILoadSRAM();
	//S9xLoadCheatFile(S9xGetFilename(".cht", CHEAT_DIR));
  
	CPU.Flags = saved_flags;
	Settings.StopEmulation = FALSE;
  
#ifdef DEBUGGER
	struct sigaction sa;
	sa.sa_handler = sigbrkhandler;
#ifdef SA_RESTART
	sa.sa_flags = SA_RESTART;
#else
	sa.sa_flags = 0;
#endif
	sigemptyset(&sa.sa_mask);
	sigaction(SIGINT, &sa, NULL);
#endif
  
  GFX.Pitch = SNES_WIDTH*2;
  /*vrambuffer = (uint8*) malloc (GFX.Pitch * SNES_HEIGHT_EXTENDED*2);
	memset (vrambuffer, 0, GFX.Pitch * SNES_HEIGHT_EXTENDED*2);
  GFX.Screen = (uint16*)vrambuffer;*/
  S9xGraphicsInit();
  
#ifdef NETPLAY_SUPPORT
	if (strlen(Settings.ServerName) == 0)
	{
		char	*server = getenv("S9XSERVER");
		if (server)
		{
			strncpy(Settings.ServerName, server, 127);
			Settings.ServerName[127] = 0;
		}
	}
  
	char	*port = getenv("S9XPORT");
	if (Settings.Port >= 0 && port)
		Settings.Port = atoi(port);
	else
    if (Settings.Port < 0)
      Settings.Port = -Settings.Port;
  
	if (Settings.NetPlay)
	{
		NetPlay.MaxFrameSkip = 10;
    
		if (!S9xNPConnectToServer(Settings.ServerName, Settings.Port, Memory.ROMName))
		{
			fprintf(stderr, "Failed to connect to server %s on port %d.\n", Settings.ServerName, Settings.Port);
			S9xExit();
		}
    
		fprintf(stderr, "Connected to server %s on port %d as player #%d playing %s.\n", Settings.ServerName, Settings.Port, NetPlay.Player, Memory.ROMName);
	}
#endif
  
  // HACK: disabling SMV
	/*if (play_smv_filename)
	{
		uint32	flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
		if (S9xMovieOpen(play_smv_filename, TRUE) != SUCCESS)
			exit(1);
		CPU.Flags |= flags;
	}
	else
    if (record_smv_filename)
    {
      uint32	flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
      if (S9xMovieCreate(record_smv_filename, 0xFF, MOVIE_OPT_FROM_RESET, NULL, 0) != SUCCESS)
        exit(1);
      CPU.Flags |= flags;
    }
    else
      if (snapshot_filename)
      {
        uint32	flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
        if (!S9xUnfreezeGame(snapshot_filename))
          exit(1);
        CPU.Flags |= flags;
      }*/
  
	//S9xGraphicsMode();
  
	sprintf(String, "\"%s\" %s: %s", Memory.ROMName, TITLE, VERSION);
	//S9xSetTitle(String);
  
#ifdef JOYSTICK_SUPPORT
	uint32	JoypadSkip = 0;
#endif
  
  SILoadRunningStateForGameNamed(rom_filename);
  SI_EmulationPaused = 0;
  
  //if(SI_SoundOn)
  SIDemuteSound(soundBufferSize);
	S9xSetSoundMute(FALSE);
  
#ifdef NETPLAY_SUPPORT
	bool8	NP_Activated = Settings.NetPlay;
#endif
  
	while (1)
	{
#ifdef NETPLAY_SUPPORT
		if (NP_Activated)
		{
			if (NetPlay.PendingWait4Sync && !S9xNPWaitForHeartBeatDelay(100))
			{
				S9xProcessEvents(FALSE);
				continue;
			}
      
			for (int J = 0; J < 8; J++)
				old_joypads[J] = MovieGetJoypad(J);
      
			for (int J = 0; J < 8; J++)
				MovieSetJoypad(J, joypads[J]);
      
			if (NetPlay.Connected)
			{
				if (NetPlay.PendingWait4Sync)
				{
					NetPlay.PendingWait4Sync = FALSE;
					NetPlay.FrameCount++;
					S9xNPStepJoypadHistory();
				}
			}
			else
			{
				fprintf(stderr, "Lost connection to server.\n");
				S9xExit();
			}
		}
#endif
    
#ifdef DEBUGGER
		if (!Settings.Paused || (CPU.Flags & (DEBUG_MODE_FLAG | SINGLE_STEP_FLAG)))
#else
    if (!Settings.Paused && !SI_EmulationPaused)
#endif
    {
      S9xMainLoop();
    }
    
#ifdef NETPLAY_SUPPORT
		if (NP_Activated)
		{
			for (int J = 0; J < 8; J++)
				MovieSetJoypad(J, old_joypads[J]);
		}
#endif
    
#ifdef DEBUGGER
		if(Settings.Paused || (CPU.Flags & DEBUG_MODE_FLAG))
#else
    if(Settings.Paused || SI_EmulationPaused)
#endif
        S9xSetSoundMute(TRUE);
    
#ifdef DEBUGGER
		if (CPU.Flags & DEBUG_MODE_FLAG)
			S9xDoDebug();
		else
#endif
      if(Settings.Paused || SI_EmulationPaused || !SI_EmulationRun)
      {
        SISaveSRAM();
        SISaveRunningStateForGameNamed(rom_filename);
        SI_EmulationDidPause = 1;
        
        do {
          //S9xProcessEvents(FALSE);
          if(!SI_EmulationRun)
            break;
          usleep(100000);
        } while (SI_EmulationPaused);
        
        if(!SI_EmulationRun)
        {
          SISaveSRAM();
          SISaveRunningStateForGameNamed(rom_filename);
          
          SIMuteSound();
          
          S9xGraphicsDeinit();
          Memory.Deinit();
          S9xDeinitAPU();
          break;
        }
      }
    
#ifdef JOYSTICK_SUPPORT
		if (unixSettings.JoystickEnabled && (JoypadSkip++ & 1) == 0)
			ReadJoysticks();
#endif
    
		//S9xProcessEvents(FALSE);
    
#ifdef DEBUGGER
		if(!Settings.Paused && !(CPU.Flags & DEBUG_MODE_FLAG))
#else
    if(!Settings.Paused && !SI_EmulationPaused)
#endif
      S9xSetSoundMute(FALSE);
	}
  SI_EmulationIsRunning = 0;
  
	return (0);
}
Beispiel #19
0
/* Restore SPC state
   ---------------------------------------------------------------- */
static void RestoreSPC()
{
    int i;

    APURegisters.PC = BackupAPURegisters.PC;
    APURegisters.YA.B.A = BackupAPURegisters.YA.B.A;
    APURegisters.X = BackupAPURegisters.X;
    APURegisters.YA.B.Y = BackupAPURegisters.YA.B.Y;
    APURegisters.P = BackupAPURegisters.P;
    APURegisters.S = BackupAPURegisters.S;
    memcpy (IAPU.RAM, BackupAPURAM, 65536);
    memcpy (APU.ExtraRAM, BackupAPUExtraRAM, 64);
    memcpy (APU.DSP, BackupDSPRAM, 128);

    for (i = 0; i < 4; i ++)
    {
        APU.OutPorts[i] = IAPU.RAM[0xf4 + i];
    }
    IAPU.TimerErrorCounter = 0;

    for (i = 0; i < 3; i ++)
    {
        if (IAPU.RAM[0xfa + i] == 0)
            APU.TimerTarget[i] = 0x100;
        else
            APU.TimerTarget[i] = IAPU.RAM[0xfa + i];
    }

    S9xSetAPUControl (IAPU.RAM[0xf1]);

  /* from snaporig.cpp (ReadOrigSnapshot) */
    S9xSetSoundMute (FALSE);
    IAPU.PC = IAPU.RAM + APURegisters.PC;
    S9xAPUUnpackStatus ();
    if (APUCheckDirectPage ())
        IAPU.DirectPage = IAPU.RAM + 0x100;
    else
        IAPU.DirectPage = IAPU.RAM;
    Settings.APUEnabled = TRUE;
    IAPU.APUExecuting = TRUE;

    S9xFixSoundAfterSnapshotLoad ();

    S9xSetFrequencyModulationEnable (APU.DSP[APU_PMON]);
    S9xSetMasterVolume (APU.DSP[APU_MVOL_LEFT], APU.DSP[APU_MVOL_RIGHT]);
    S9xSetEchoVolume (APU.DSP[APU_EVOL_LEFT], APU.DSP[APU_EVOL_RIGHT]);

    uint8 mask = 1;
    int type;
    for (i = 0; i < 8; i++, mask <<= 1) {
        // unused // Channel *ch = &SoundData.channels[i];
      
        S9xFixEnvelope (i,
                        APU.DSP[APU_GAIN + (i << 4)],
                        APU.DSP[APU_ADSR1 + (i << 4)],
                        APU.DSP[APU_ADSR2 + (i << 4)]);
        S9xSetSoundVolume (i,
                           APU.DSP[APU_VOL_LEFT + (i << 4)],
                           APU.DSP[APU_VOL_RIGHT + (i << 4)]);
        S9xSetSoundFrequency (i, ((APU.DSP[APU_P_LOW + (i << 4)]
                                   + (APU.DSP[APU_P_HIGH + (i << 4)] << 8))
                                  & FREQUENCY_MASK) * 8);
        if (APU.DSP [APU_NON] & mask)
            type = SOUND_NOISE;
        else
            type = SOUND_SAMPLE;
        S9xSetSoundType (i, type);
        if ((APU.DSP[APU_KON] & mask) != 0)
	{
            APU.KeyedChannels |= mask;
            S9xPlaySample (i);
	}
    }

#if 0
    unsigned char temp=IAPU.RAM[0xf2];
    for(i=0;i<128;i++){
        IAPU.RAM[0xf2]=i;
        S9xSetAPUDSP(APU.DSP[i]);
    }
    IAPU.RAM[0xf2]=temp;
#endif
}
Beispiel #20
0
/****************************************************************************
 * NGCFreezeGame
 *
 * Do freeze game for Nintendo Gamecube
 ***************************************************************************/
int
NGCFreezeGame (int method, bool8 silent)
{
	char filepath[1024];
	int offset = 0; // bytes written (actual)
	int woffset = 0; // bytes written (expected)
	char msg[100];

	ShowAction ((char*) "Saving...");

	if(method == METHOD_AUTO)
		method = autoSaveMethod();

	if(!MakeFilePath(filepath, FILE_SNAPSHOT, method))
		return 0;

	S9xSetSoundMute (TRUE);
	S9xPrepareSoundForSnapshotSave (FALSE);

	AllocSaveBuffer ();
	NGCFreezeMemBuffer (); // copy freeze mem into savebuffer
	woffset = bufoffset;

	S9xPrepareSoundForSnapshotSave (TRUE);
	S9xSetSoundMute (FALSE);

	if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC Slot A or B
	{
		// Copy in save icon
		woffset = sizeof (saveicon);
		memcpy (savebuffer, saveicon, woffset);

		// And the freezecomment
		sprintf (freezecomment[0], "%s Freeze", VERSIONSTR);
		sprintf (freezecomment[1], Memory.ROMName);
		memcpy (savebuffer + woffset, freezecomment, 64);
		woffset += 64;

		// Zip and copy in the freeze
		uLongf DestBuffSize = (uLongf) SAVEBUFFERSIZE;
		int err= compress2((Bytef*)(savebuffer+woffset+8), (uLongf*)&DestBuffSize, (const Bytef*)savebuffer, (uLongf)bufoffset, Z_BEST_COMPRESSION);

		if(err!=Z_OK)
		{
			sprintf (msg, "zip error %s ",zError(err));
			WaitPrompt (msg);
			return 0;
		}

		int zippedsize = (int)DestBuffSize;
		memcpy (savebuffer + woffset, &zippedsize, 4);
		woffset += 4;

		int decompressedsize = (int)bufoffset;
		memcpy (savebuffer + woffset, &decompressedsize, 4);
		woffset += 4;

		woffset += zippedsize;
	}

	offset = SaveFile(filepath, woffset, method, silent);

	FreeSaveBuffer ();

	if(offset > 0) // save successful!
	{
		if(!silent)
			WaitPrompt((char*) "Save successful");
		return 1;
	}
    return 0;
}
Beispiel #21
0
gboolean
S9xIdleFunc (gpointer data)
{
    if (needs_fullscreening)
    {
        top_level->enter_fullscreen_mode();
        needs_fullscreening = FALSE;
    }

    if (Settings.Paused)
    {
        S9xSetSoundMute (gui_config->mute_sound);
        S9xSoundStop ();

#ifdef USE_JOYSTICK
        gui_config->flush_joysticks ();
#endif

#ifdef NETPLAY_SUPPORT
        if (Settings.NetPlay && NetPlay.Connected)
        {
            S9xNPSendPause (TRUE);
        }
#endif

        /* Move to a timer-based function to use less CPU */
        g_timeout_add (100, S9xPauseFunc, NULL);
        top_level->update_statusbar ();
        return FALSE;
    }

    if (syncing)
        S9xSyncSpeedFinish ();

    S9xCheckPointerTimer ();

    S9xProcessEvents (TRUE);

#ifdef NETPLAY_SUPPORT
    if (!S9xNetplayPush ())
    {
#endif

    if(Settings.Rewinding)
    {
        uint16 joypads[8];
        for (int i = 0; i < 8; i++)
            joypads[i] = MovieGetJoypad(i);

        Settings.Rewinding = stateMan.pop();

        for (int i = 0; i < 8; i++)
            MovieSetJoypad (i, joypads[i]);
    }
    else if(IPPU.TotalEmulatedFrames % gui_config->rewind_granularity == 0)
        stateMan.push();

    static int muted_from_turbo = FALSE;
    static int mute_saved_state = FALSE;

    if (Settings.TurboMode && !muted_from_turbo && gui_config->mute_sound_turbo)
    {
        muted_from_turbo = TRUE;
        mute_saved_state = Settings.Mute;
        S9xSetSoundMute (TRUE);
    }

    if (!Settings.TurboMode && muted_from_turbo)
    {
        muted_from_turbo = FALSE;
        Settings.Mute = mute_saved_state;
    }

    S9xMainLoop ();

#ifdef NETPLAY_SUPPORT
        S9xNetplayPop ();
    }
#endif

    return TRUE;
}