⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unix.cpp

📁 著名SFC模拟器Snes9x的源代码。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    }    else    {	if (J & DSP_CAP_MMAP)	    printf ("DSP_CAP_MMAP supported\n");    }    void *ptr;    if ((ptr = mmap (0, so.buffer_size * 3, PROT_WRITE, 0, so.sound_fd, 0)) == 0)	printf ("mmap failed\n");    J = 0;    if (ioctl (so.sound_fd, SNDCTL_DSP_SETTRIGGER, &J) < 0)	perror ("ioctl SNDCTL_DSP_SETTRIGGER");#endif    J = AFMT_S16_LE;//    J = AFMT_U8;    if (ioctl (so.sound_fd, SNDCTL_DSP_SETFMT, &J) < 0)    {	perror ("ioctl SNDCTL_DSP_SETFMT");	return (FALSE);    }    if (J != AFMT_S16_LE)    {	so.sixteen_bit = FALSE;	J = AFMT_U8;	if (ioctl (so.sound_fd, SNDCTL_DSP_SETFMT, &J) < 0)	{	    perror ("ioctl SNDCTL_DSP_SETFMT");	    return (FALSE);	}    }    else	so.sixteen_bit = TRUE;    so.stereo = stereo;    if (ioctl (so.sound_fd, SNDCTL_DSP_STEREO, &so.stereo) < 0)    {	perror ("ioctl SNDCTL_DSP_STEREO");	return (FALSE);    }        so.playback_rate = Rates[mode & 0x07];    if (ioctl (so.sound_fd, SNDCTL_DSP_SPEED, &so.playback_rate) < 0)    {	perror ("ioctl SNDCTL_DSP_SPEED");	return (FALSE);    }    S9xSetPlaybackRate (so.playback_rate);    if (buffer_size == 0)	buffer_size = BufferSizes [mode & 7];    if (buffer_size > MAX_BUFFER_SIZE / 4)	buffer_size = MAX_BUFFER_SIZE / 4;    if (so.sixteen_bit)	buffer_size *= 2;    if (so.stereo)	buffer_size *= 2;    int power2 = log2 (buffer_size);    J = K = power2 | (3 << 16);    if (ioctl (so.sound_fd, SNDCTL_DSP_SETFRAGMENT, &J) < 0)    {	perror ("ioctl SNDCTL_DSP_SETFRAGMENT");	return (FALSE);    }    ioctl (so.sound_fd, SNDCTL_DSP_GETBLKSIZE, &so.buffer_size);    #ifdef MMAP_SOUND    J = PCM_ENABLE_OUTPUT;    if (ioctl (so.sound_fd, SNDCTL_DSP_SETTRIGGER, &J) < 0)	perror ("ioctl SNDCTL_DSP_SETTRIGGER");#endif#if 0    buffmem_desc buff;    buff.size = so.buffer_size * 3;    buff.buffer = ptr;    if (ioctl (so.sound_fd, SNDCTL_DSP_MAPOUTBUF, &buff) < 0)	perror ("ioctl SNDCTL_DSP_MAPOUTBUF");#endif    printf ("Rate: %d, Buffer size: %d, 16-bit: %s, Stereo: %s, Encoded: %s\n",	    so.playback_rate, so.buffer_size, so.sixteen_bit ? "yes" : "no",	    so.stereo ? "yes" : "no", so.encoded ? "yes" : "no");    return (TRUE);}#endif#if defined (__linux) || defined (__sun)void S9xUnixProcessSound (void){}static uint8 Buf[MAX_BUFFER_SIZE];#define FIXED_POINT 0x10000#define FIXED_POINT_SHIFT 16#define FIXED_POINT_REMAINDER 0xffffstatic volatile bool8 block_signal = FALSE;static volatile bool8 block_generate_sound = FALSE;static volatile bool8 pending_signal = FALSE;void S9xGenerateSound (){    int bytes_so_far = so.sixteen_bit ? (so.samples_mixed_so_far << 1) :				        so.samples_mixed_so_far;    if (Settings.SoundSync == 2)    {	// Assumes sound is signal driven	while (so.samples_mixed_so_far >= so.buffer_size && !so.mute_sound)	    pause ();    }    else    if (bytes_so_far >= so.buffer_size)	return;#ifdef USE_THREADS    if (Settings.ThreadSound)    {	if (block_generate_sound || pthread_mutex_trylock (&mutex))	    return;    }#endif    block_signal = TRUE;    so.err_counter += so.err_rate;    if (so.err_counter >= FIXED_POINT)    {        int sample_count = so.err_counter >> FIXED_POINT_SHIFT;	int byte_offset;	int byte_count;        so.err_counter &= FIXED_POINT_REMAINDER;	if (so.stereo)	    sample_count <<= 1;	byte_offset = bytes_so_far + so.play_position;	    	do	{	    int sc = sample_count;	    byte_count = sample_count;	    if (so.sixteen_bit)		byte_count <<= 1;	    	    if ((byte_offset & SOUND_BUFFER_SIZE_MASK) + byte_count > SOUND_BUFFER_SIZE)	    {		sc = SOUND_BUFFER_SIZE - (byte_offset & SOUND_BUFFER_SIZE_MASK);		byte_count = sc;		if (so.sixteen_bit)		    sc >>= 1;	    }	    if (bytes_so_far + byte_count > so.buffer_size)	    {		byte_count = so.buffer_size - bytes_so_far;		if (byte_count == 0)		    break;		sc = byte_count;		if (so.sixteen_bit)		    sc >>= 1;	    }	    S9xMixSamplesO (Buf, sc,			    byte_offset & SOUND_BUFFER_SIZE_MASK);	    so.samples_mixed_so_far += sc;	    sample_count -= sc;	    bytes_so_far = so.sixteen_bit ? (so.samples_mixed_so_far << 1) :	 	           so.samples_mixed_so_far;	    byte_offset += byte_count;	} while (sample_count > 0);    }    block_signal = FALSE;#ifdef USE_THREADS    if (Settings.ThreadSound)	pthread_mutex_unlock (&mutex);    else#endif        if (pending_signal)    {	S9xProcessSound (NULL);	pending_signal = FALSE;    }}void *S9xProcessSound (void *){#ifdef __linux    audio_buf_info info;    if (!Settings.ThreadSound &&	(ioctl (so.sound_fd, SNDCTL_DSP_GETOSPACE, &info) == -1 ||	 info.bytes < so.buffer_size))    {	return (NULL);    }#ifdef MMAP_SOUND    count_info count;    if (ioctl (so.sound_fd, SNDCTL_DSP_GETOPTR, &count) < 0)    {	printf ("F"); fflush (stdout);    }    else    {	printf ("<%d,%d>", count.blocks, count.bytes); fflush (stdout);    	return (NULL);    }#endif#endif#ifdef __sun    audio_info_t audio;    if (!Settings.ThreadSound)    {	if (ioctl (so.sound_fd, AUDIO_GETINFO, &audio) < 0)	    return (NULL);	if (audio.play.eof == 0)	    so.last_eof = -2;	else	if (audio.play.eof == so.last_eof)	    return (NULL);	so.last_eof++;    }#endif#ifdef USE_THREADS    do    {#endif    int sample_count = so.buffer_size;    int byte_offset;    if (so.sixteen_bit)	sample_count >>= 1; #ifdef USE_THREADS    if (Settings.ThreadSound)	pthread_mutex_lock (&mutex);    else#endif    if (block_signal)    {	pending_signal = TRUE;	return (NULL);    }    block_generate_sound = TRUE;    if (so.samples_mixed_so_far < sample_count)    {	byte_offset = so.play_position + 		      (so.sixteen_bit ? (so.samples_mixed_so_far << 1)				      : so.samples_mixed_so_far);//printf ("%d:", sample_count - so.samples_mixed_so_far); fflush (stdout);	if (Settings.SoundSync == 2)	{	    /*memset (Buf + (byte_offset & SOUND_BUFFER_SIZE_MASK), 0,		    sample_count - so.samples_mixed_so_far);*/	}	else	    S9xMixSamplesO (Buf, sample_count - so.samples_mixed_so_far,			    byte_offset & SOUND_BUFFER_SIZE_MASK);	so.samples_mixed_so_far = 0;    }    else	so.samples_mixed_so_far -= sample_count;    //    if (!so.mute_sound)    {	int I;	int J = so.buffer_size;	byte_offset = so.play_position;	so.play_position = (so.play_position + so.buffer_size) & SOUND_BUFFER_SIZE_MASK;#ifdef USE_THREADS	if (Settings.ThreadSound)	    pthread_mutex_unlock (&mutex);#endif	block_generate_sound = FALSE;	do	{	    if (byte_offset + J > SOUND_BUFFER_SIZE)	    {		I = write (so.sound_fd, (char *) Buf + byte_offset,			   SOUND_BUFFER_SIZE - byte_offset);		if (I > 0)		{		    J -= I;		    byte_offset = (byte_offset + I) & SOUND_BUFFER_SIZE_MASK;		}	    }	    else	    {		I = write (so.sound_fd, (char *) Buf + byte_offset, J);		if (I > 0)		{		    J -= I;		    byte_offset = (byte_offset + I) & SOUND_BUFFER_SIZE_MASK;		}	    }	} while ((I < 0 && errno == EINTR) || J > 0);    }#ifdef USE_THREADS    } while (Settings.ThreadSound);#endif#ifdef __sun    if (!Settings.ThreadSound)	write (so.sound_fd, NULL, 0);#endif    return (NULL);}#endifuint32 S9xReadJoypad (int which1){#ifdef _NETPLAY_SUPPORT    if (Settings.NetPlay)	return (S9xNetPlayGetJoypad (which1));#endif    if (which1 < NumControllers)	return (0x80000000 | joypads [which1]);    return (0);}#ifdef __sunuint8 int2ulaw(int ch){    int mask;    if (ch < 0) {      ch = -ch;      mask = 0x7f;    }    else {      mask = 0xff;    }    if (ch < 32) {	ch = 0xF0 | ( 15 - (ch/2) );    } else if (ch < 96) {        ch = 0xE0 | ( 15 - (ch-32)/4 );    } else if (ch < 224) {	ch = 0xD0 | ( 15 - (ch-96)/8 );    } else if (ch < 480) {	ch = 0xC0 | ( 15 - (ch-224)/16 );    } else if (ch < 992 ) {	ch = 0xB0 | ( 15 - (ch-480)/32 );    } else if (ch < 2016) {	ch = 0xA0 | ( 15 - (ch-992)/64 );    } else if (ch < 4064) {	ch = 0x90 | ( 15 - (ch-2016)/128 );    } else if (ch < 8160) {	ch = 0x80 | ( 15 - (ch-4064)/256 );    } else {	ch = 0x80;    }    return (uint8)(mask & ch);}#endif#if 0void S9xParseConfigFile (){    int i, t = 0;    char *b, buf[10];    struct ffblk f;    set_config_file("SNES9X.CFG");    if (findfirst("SNES9X.CFG", &f, 0) != 0)    {        set_config_int("Graphics", "VideoMode", -1);        set_config_int("Graphics", "AutoFrameskip", 1);        set_config_int("Graphics", "Frameskip", 0);        set_config_int("Graphics", "Shutdown", 1);        set_config_int("Graphics", "FrameTimePAL", 20000);        set_config_int("Graphics", "FrameTimeNTSC", 16667);        set_config_int("Graphics", "Transparency", 0);        set_config_int("Graphics", "HiColor", 0);        set_config_int("Graphics", "Hi-ResSupport", 0);        set_config_int("Graphics", "CPUCycles", 100);        set_config_int("Graphics", "Scale", 0);        set_config_int("Graphics", "VSync", 0);        set_config_int("Sound", "APUEnabled", 1);        set_config_int("Sound", "SoundPlaybackRate", 4);        set_config_int("Sound", "Stereo", 1);        set_config_int("Sound", "SoundBufferSize", 256);        set_config_int("Sound", "SPCToCPURatio", 2);        set_config_int("Sound", "Echo", 1);        set_config_int("Sound", "SampleCaching", 1);        set_config_int("Sound", "MasterVolume", 1);        set_config_int("Peripherals", "Mouse", 1);        set_config_int("Peripherals", "SuperScope", 1);        set_config_int("Peripherals", "MultiPlayer5", 1);        set_config_int("Peripherals", "Controller", 0);        set_config_int("Controllers", "Type", JOY_TYPE_AUTODETECT);        set_config_string("Controllers", "Button1", "A");        set_config_string("Controllers", "Button2", "B");        set_config_string("Controllers", "Button3", "X");        set_config_string("Controllers", "Button4", "Y");        set_config_string("Controllers", "Button5", "TL");        set_config_string("Controllers", "Button6", "TR");        set_config_string("Controllers", "Button7", "START");        set_config_string("Controllers", "Button8", "SELECT");        set_config_string("Controllers", "Button9", "NONE");        set_config_string("Controllers", "Button10", "NONE");    }    mode = get_config_int("Graphics", "VideoMode", -1);    Settings.SkipFrames = get_config_int("Graphics", "AutoFrameskip", 1);    if (!Settings.SkipFrames)       Settings.SkipFrames = get_config_int("Graphics", "Frameskip", AUTO_FRAMERATE);    else       Settings.SkipFrames = AUTO_FRAMERATE;    Settings.ShutdownMaster = get_config_int("Graphics", "Shutdown", TRUE);    Settings.FrameTimePAL = get_config_int("Graphics", "FrameTimePAL", 20000);    Settings.FrameTimeNTSC = get_config_int("Graphics", "FrameTimeNTSC", 16667);    Settings.FrameTime = Settings.FrameTimeNTSC;    Settings.Transparency = get_config_int("Graphics", "Transparency", FALSE);    Settings.SixteenBit = get_config_int("Graphics", "HiColor", FALSE);    Settings.SupportHiRes = get_config_int("Graphics", "Hi-ResSupport", FALSE);    i = get_config_int("Graphics", "CPUCycles", 100);    Settings.H_Max = (i * SNES_CYCLES_PER_SCANLINE) / i;    stretch = get_config_int("Graphics", "Scale", 0);    _vsync = get_config_int("Graphics", "VSync", 0);    Settings.APUEnabled = get_config_int("Sound", "APUEnabled", TRUE);    Settings.SoundPlaybackRate = get_config_int("Sound", "SoundPlaybackRate", 4);    Settings.Stereo = get_config_int("Sound", "Stereo", TRUE);    Settings.SoundBufferSize = get_config_int("Sound", "SoundBufferSize", 256);    Settings.SPCTo65c816Ratio = get_config_int("Sound", "SPCToCPURatio", 2);    Settings.DisableSoundEcho = get_config_int("Sound", "Echo", TRUE) ? FALSE : TRUE;    Settings.DisableSampleCaching = get_config_int("Sound", "SampleCaching", TRUE) ? FALSE : TRUE;    Settings.DisableMasterVolume = get_config_int("Sound", "MasterVolume", TRUE) ? FALSE : TRUE;    Settings.Mouse = get_config_int("Peripherals", "Mouse", TRUE);    Settings.SuperScope = get_config_int("Peripherals", "SuperScope", TRUE);    Settings.MultiPlayer5 = get_config_int("Peripherals", "MultiPlayer5", TRUE);    Settings.ControllerOption = (uint32)get_config_int("Peripherals", "Controller", SNES_MULTIPLAYER5);    joy_type = get_config_int("Controllers", "Type", JOY_TYPE_AUTODETECT);    for (i = 0; i < 10; i++)    {        sprintf(buf, "Button%d", i+1);        b = get_config_string("Controllers", buf, "NONE");        if (!strcasecmp(b, "A"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_A_MASK;}        else if (!strcasecmp(b, "B"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_B_MASK;}        else if (!strcasecmp(b, "X"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_X_MASK;}        else if (!strcasecmp(b, "Y"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_Y_MASK;}        else if (!strcasecmp(b, "TL"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_TL_MASK;}        else if (!strcasecmp(b, "TR"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_TR_MASK;}        else if (!strcasecmp(b, "START"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_START_MASK;}        else if (!strcasecmp(b, "SELECT"))        {JOY_BUTTON_INDEX[t] = i; SNES_BUTTON_MASKS[t++] = SNES_SELECT_MASK;}    }}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -