From 72141845f0eb8a3744768bc7268a75640b9f8b86 Mon Sep 17 00:00:00 2001 From: mjbudd77 Date: Fri, 3 Jul 2020 15:58:53 -0400 Subject: [PATCH] Changed gl_shm name to nes_shm. Added audio circular buffer to shared memory. --- fceux.pro | 2 +- src/drivers/Qt/GameApp.cpp | 2 +- src/drivers/Qt/GameViewerGL.cpp | 10 ++++---- src/drivers/Qt/GameViewerSDL.cpp | 10 ++++---- src/drivers/Qt/fceuWrapper.cpp | 8 +++---- src/drivers/Qt/{gl_win.cpp => nes_shm.cpp} | 18 +++++++-------- src/drivers/Qt/{gl_win.h => nes_shm.h} | 27 +++++++++++++++++----- src/drivers/Qt/sdl-sound.cpp | 20 ++++++++++++---- src/drivers/Qt/sdl-throttle.cpp | 16 +++++++++---- src/drivers/Qt/sdl-video.cpp | 16 ++++++------- 10 files changed, 82 insertions(+), 47 deletions(-) rename src/drivers/Qt/{gl_win.cpp => nes_shm.cpp} (67%) rename src/drivers/Qt/{gl_win.h => nes_shm.h} (61%) diff --git a/fceux.pro b/fceux.pro index afd60019..37eb3f9c 100644 --- a/fceux.pro +++ b/fceux.pro @@ -308,7 +308,7 @@ SOURCES += src/drivers/Qt/GameSoundConf.cpp SOURCES += src/drivers/Qt/fceuWrapper.cpp SOURCES += src/drivers/Qt/config.cpp SOURCES += src/drivers/Qt/input.cpp -SOURCES += src/drivers/Qt/gl_win.cpp +SOURCES += src/drivers/Qt/nes_shm.cpp SOURCES += src/drivers/Qt/keyscan.cpp SOURCES += src/drivers/Qt/sdl-sound.cpp SOURCES += src/drivers/Qt/sdl-video.cpp diff --git a/src/drivers/Qt/GameApp.cpp b/src/drivers/Qt/GameApp.cpp index dcba7508..dbbbb27b 100644 --- a/src/drivers/Qt/GameApp.cpp +++ b/src/drivers/Qt/GameApp.cpp @@ -24,7 +24,7 @@ gameWin_t::gameWin_t(QWidget *parent) connect( gameTimer, &QTimer::timeout, this, &gameWin_t::runGameFrame ); gameTimer->setTimerType( Qt::PreciseTimer ); - gameTimer->start( 10 ); + gameTimer->start( 16 ); gamePadConfWin = NULL; } diff --git a/src/drivers/Qt/GameViewerGL.cpp b/src/drivers/Qt/GameViewerGL.cpp index 851f0b96..51fe20cc 100644 --- a/src/drivers/Qt/GameViewerGL.cpp +++ b/src/drivers/Qt/GameViewerGL.cpp @@ -8,7 +8,7 @@ #include #include -#include "Qt/gl_win.h" +#include "Qt/nes_shm.h" #include "Qt/GameViewerGL.h" extern unsigned int gui_draw_area_width; @@ -111,8 +111,8 @@ void gameViewGL_t::resizeGL(int w, int h) void gameViewGL_t::paintGL(void) { - int texture_width = gl_shm->ncol; - int texture_height = gl_shm->nrow; + int texture_width = nes_shm->ncol; + int texture_height = nes_shm->nrow; int l=0, r=texture_width; int t=0, b=texture_height; @@ -149,7 +149,7 @@ void gameViewGL_t::paintGL(void) glTexSubImage2D(GL_TEXTURE_RECTANGLE, 0, 0, 0, GL_NES_WIDTH, GL_NES_HEIGHT, - GL_BGRA, GL_UNSIGNED_BYTE, gl_shm->pixbuf ); + GL_BGRA, GL_UNSIGNED_BYTE, nes_shm->pixbuf ); glBegin(GL_QUADS); glTexCoord2f( l, b); // Bottom left of picture. @@ -169,4 +169,4 @@ void gameViewGL_t::paintGL(void) glDisable(GL_TEXTURE_RECTANGLE); //printf("Paint GL!\n"); -} \ No newline at end of file +} diff --git a/src/drivers/Qt/GameViewerSDL.cpp b/src/drivers/Qt/GameViewerSDL.cpp index 50dffda1..be0b7039 100644 --- a/src/drivers/Qt/GameViewerSDL.cpp +++ b/src/drivers/Qt/GameViewerSDL.cpp @@ -6,7 +6,7 @@ #include #include -#include "Qt/gl_win.h" +#include "Qt/nes_shm.h" #include "Qt/GameViewerSDL.h" extern unsigned int gui_draw_area_width; @@ -147,10 +147,10 @@ void gameViewSDL_t::paintEvent( QPaintEvent *event ) int nesWidth = GL_NES_WIDTH; int nesHeight = GL_NES_HEIGHT; - if ( gl_shm != NULL ) + if ( nes_shm != NULL ) { - nesWidth = gl_shm->ncol; - nesHeight = gl_shm->nrow; + nesWidth = nes_shm->ncol; + nesHeight = nes_shm->nrow; } //printf(" %i x %i \n", nesWidth, nesHeight ); float xscale = (float)view_width / (float)nesWidth; @@ -183,7 +183,7 @@ void gameViewSDL_t::paintEvent( QPaintEvent *event ) int rowPitch; SDL_LockTexture( sdlTexture, nullptr, (void**)&textureBuffer, &rowPitch); { - memcpy( textureBuffer, gl_shm->pixbuf, GL_NES_HEIGHT*GL_NES_WIDTH*sizeof(uint32_t) ); + memcpy( textureBuffer, nes_shm->pixbuf, GL_NES_HEIGHT*GL_NES_WIDTH*sizeof(uint32_t) ); } SDL_UnlockTexture(sdlTexture); diff --git a/src/drivers/Qt/fceuWrapper.cpp b/src/drivers/Qt/fceuWrapper.cpp index b077a140..f2513509 100644 --- a/src/drivers/Qt/fceuWrapper.cpp +++ b/src/drivers/Qt/fceuWrapper.cpp @@ -12,7 +12,7 @@ #include "Qt/input.h" #include "Qt/sdl.h" #include "Qt/sdl-video.h" -#include "Qt/gl_win.h" +#include "Qt/nes_shm.h" #include "Qt/unix-netplay.h" #include "common/cheat.h" @@ -471,11 +471,11 @@ int fceuWrapperInit( int argc, char *argv[] ) return 0; } - gl_shm = open_video_shm(); + nes_shm = open_nes_shm(); - if ( gl_shm == NULL ) + if ( nes_shm == NULL ) { - printf("Error: Failed to open video Shared memory\n"); + printf("Error: Failed to open NES Shared memory\n"); return -1; } diff --git a/src/drivers/Qt/gl_win.cpp b/src/drivers/Qt/nes_shm.cpp similarity index 67% rename from src/drivers/Qt/gl_win.cpp rename to src/drivers/Qt/nes_shm.cpp index a3fe766e..59d1e948 100644 --- a/src/drivers/Qt/gl_win.cpp +++ b/src/drivers/Qt/nes_shm.cpp @@ -9,18 +9,18 @@ #include #include -#include "Qt/gl_win.h" +#include "Qt/nes_shm.h" -gl_win_shm_t *gl_shm = NULL; +nes_shm_t *nes_shm = NULL; //************************************************************************ -gl_win_shm_t *open_video_shm(void) +nes_shm_t *open_nes_shm(void) { int shmId; - gl_win_shm_t *vaddr; + nes_shm_t *vaddr; struct shmid_ds ds; - shmId = shmget( IPC_PRIVATE, sizeof(struct gl_win_shm_t), IPC_CREAT | S_IRWXU | S_IRWXG ); + shmId = shmget( IPC_PRIVATE, sizeof(struct nes_shm_t), IPC_CREAT | S_IRWXU | S_IRWXG ); if ( shmId == -1 ) { @@ -29,14 +29,14 @@ gl_win_shm_t *open_video_shm(void) } printf("Created ShmID: %i \n", shmId ); - vaddr = (gl_win_shm_t*)shmat( shmId, NULL, 0); + vaddr = (nes_shm_t*)shmat( shmId, NULL, 0); - if ( vaddr == (gl_win_shm_t*)-1 ) + if ( vaddr == (nes_shm_t*)-1 ) { - perror("Error: GLX shmat Failed:"); + perror("Error: NES shmat Failed:"); return NULL; } - memset( vaddr, 0, sizeof(struct gl_win_shm_t)); + memset( vaddr, 0, sizeof(struct nes_shm_t)); if ( shmctl( shmId, IPC_RMID, &ds ) != 0 ) { diff --git a/src/drivers/Qt/gl_win.h b/src/drivers/Qt/nes_shm.h similarity index 61% rename from src/drivers/Qt/gl_win.h rename to src/drivers/Qt/nes_shm.h index 88776ac8..0cc52bd6 100644 --- a/src/drivers/Qt/gl_win.h +++ b/src/drivers/Qt/nes_shm.h @@ -1,8 +1,8 @@ -// gl_win.cpp +// nes_shm.h // -#ifndef __GL_WIN_H__ -#define __GL_WIN_H__ +#ifndef __NES_SHM_H__ +#define __NES_SHM_H__ #include @@ -11,8 +11,9 @@ #define GL_NES_WIDTH 256 #define GL_NES_HEIGHT 256 +#define NES_AUDIO_BUFLEN 480000 -struct gl_win_shm_t +struct nes_shm_t { int pid; int run; @@ -59,10 +60,24 @@ struct gl_win_shm_t { memset( pixbuf, 0, sizeof(pixbuf) ); } + + struct sndBuf_t + { + int head; + int tail; + int16_t data[NES_AUDIO_BUFLEN]; + unsigned int starveCounter; + } sndBuf; + + void push_sound_sample( int16_t sample ) + { + sndBuf.data[ sndBuf.head ] = sample; + sndBuf.head = (sndBuf.head + 1) % NES_AUDIO_BUFLEN; + } }; -extern gl_win_shm_t *gl_shm; +extern nes_shm_t *nes_shm; -gl_win_shm_t *open_video_shm(void); +nes_shm_t *open_nes_shm(void); #endif diff --git a/src/drivers/Qt/sdl-sound.cpp b/src/drivers/Qt/sdl-sound.cpp index af01a355..2133d990 100644 --- a/src/drivers/Qt/sdl-sound.cpp +++ b/src/drivers/Qt/sdl-sound.cpp @@ -25,6 +25,7 @@ #include "common/configSys.h" #include "utils/memory.h" +#include "nes_shm.h" #include #include @@ -49,18 +50,27 @@ fillaudio(void *udata, uint8 *stream, int len) { + static int16_t sample = 0; int16 *tmps = (int16*)stream; len >>= 1; - while(len) { - int16 sample = 0; - if(s_BufferIn) { + while (len) + { + //int16 sample = 0; + if (s_BufferIn) + { sample = s_Buffer[s_BufferRead]; s_BufferRead = (s_BufferRead + 1) % s_BufferSize; s_BufferIn--; } else { - sample = 0; + // Retain last known sample value, helps avoid clicking + // noise when sound system is starved of audio data. + //sample = 0; + nes_shm->sndBuf.starveCounter++; + //printf("Starve:%u\n", nes_shm->sndBuf.starveCounter ); } + nes_shm->push_sound_sample( sample ); + *tmps = sample; tmps++; len--; @@ -179,6 +189,7 @@ WriteSound(int32 *buf, { extern int EmulationPaused; if (EmulationPaused == 0) + { while(Count) { while(s_BufferIn == s_BufferSize) @@ -196,6 +207,7 @@ WriteSound(int32 *buf, buf++; } + } } /** diff --git a/src/drivers/Qt/sdl-throttle.cpp b/src/drivers/Qt/sdl-throttle.cpp index 7afdb07e..6af554fb 100644 --- a/src/drivers/Qt/sdl-throttle.cpp +++ b/src/drivers/Qt/sdl-throttle.cpp @@ -9,7 +9,7 @@ static const double Fastest = 32; // 32x speed (around 1920 fps on NTSC) static const double Normal = 1.0; // 1x speed (around 60 fps on NTSC) static uint64 Lasttime, Nexttime; -static long double desired_frametime; +static double desired_frametime = (1.0 / 60.099823); static int InFrame; double g_fpsScale = Normal; // used by sdl.cpp bool MaxSpeed = false; @@ -30,8 +30,14 @@ bool MaxSpeed = false; void RefreshThrottleFPS() { - uint64 fps = FCEUI_GetDesiredFPS(); // Do >> 24 to get in Hz - desired_frametime = 16777216.0l / (fps * g_fpsScale); + double hz; + int32_t fps = FCEUI_GetDesiredFPS(); // Do >> 24 to get in Hz + + hz = ( ((double)fps) / 16777216.0 ); + + desired_frametime = 1.0 / ( hz * g_fpsScale ); + + printf("FrameTime: %llu %llu %f %lf \n", fps, fps >> 24, hz, desired_frametime ); Lasttime=0; Nexttime=0; @@ -44,6 +50,8 @@ RefreshThrottleFPS() int SpeedThrottle() { + return 0; + if(g_fpsScale >= 32) { return 0; /* Done waiting */ @@ -66,7 +74,7 @@ SpeedThrottle() else time_left = Nexttime - cur_time; - if(time_left > 50) + if (time_left > 50) { time_left = 50; /* In order to keep input responsive, don't wait too long at once */ diff --git a/src/drivers/Qt/sdl-video.cpp b/src/drivers/Qt/sdl-video.cpp index 4f784aa1..cbc8c441 100644 --- a/src/drivers/Qt/sdl-video.cpp +++ b/src/drivers/Qt/sdl-video.cpp @@ -22,7 +22,7 @@ /// \brief Handles the graphical game display for the SDL implementation. #include "Qt/sdl.h" -#include "Qt/gl_win.h" +#include "Qt/nes_shm.h" #include "common/vidblit.h" #include "../../fceu.h" #include "../../version.h" @@ -91,9 +91,9 @@ KillVideo() { //printf("Killing Video\n"); - if ( gl_shm != NULL ) + if ( nes_shm != NULL ) { - gl_shm->clear_pixbuf(); + nes_shm->clear_pixbuf(); } //destroy_gui_video(); @@ -337,7 +337,7 @@ static void WriteTestPattern(void) { for (j=0; jpixbuf[k] = 0xffffffff; k++; + nes_shm->pixbuf[k] = 0xffffffff; k++; } } } @@ -360,14 +360,14 @@ BlitScreen(uint8 *XBuf) // XXX soules - not entirely sure why this is being done yet XBuf += s_srendline * 256; - dest = (uint8*)gl_shm->pixbuf; + dest = (uint8*)nes_shm->pixbuf; w = GL_NES_WIDTH; h = GL_NES_HEIGHT; pitch = w*4; - gl_shm->ncol = NWIDTH; - gl_shm->nrow = s_tlines; - gl_shm->pitch = pitch; + nes_shm->ncol = NWIDTH; + nes_shm->nrow = s_tlines; + nes_shm->pitch = pitch; if ( dest == NULL ) return;