Skip to content

Commit

Permalink
-lots of changes to try and reduce total cpu load, notably reduced ho…
Browse files Browse the repository at this point in the history
…w often the main loop runs, how the ppu rendering works (using some optimizations from nestopia) and massively improving fds audio code to now take much less cpu and being much closer to real hardware
  • Loading branch information
FIX94 committed May 30, 2018
1 parent 7e83a3f commit 4da6b82
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 765 deletions.
80 changes: 52 additions & 28 deletions apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ extern uint16_t cpu_dmc_dma_addr;

#define M_2_PI 6.28318530717958647692

extern bool fdsMasterEnable;
extern uint32_t vrc7CycleTimer;

extern bool nesPAL;
void apuInitBufs()
{
Expand Down Expand Up @@ -189,7 +192,9 @@ void apuInitBufs()
//convert to 32bit int for calcs later
hpVal = (int32_t)((rc / (rc + dt))*32768.0);
#endif
apuBufSize = apuFrequency/60*2;
//just have something larger than 1 frame
//to hold changing data size
apuBufSize = apuFrequency/30*2;
#if AUDIO_FLOAT
apuBufSizeBytes = apuBufSize*sizeof(float);
apuOutBuf = (float*)malloc(apuBufSizeBytes);
Expand Down Expand Up @@ -298,6 +303,8 @@ static void apuChangeMode()
modeCurCtr = nesPAL ? 8315 : 7459;
}

static uint8_t vrc7Clock = 1;

void apuClockTimers()
{
if(p1freqCtr)
Expand Down Expand Up @@ -378,6 +385,19 @@ void apuClockTimers()
cpu_dmc_dma_addr = dmcCurAddr;
dmcCurLen--;
}

if(fdsEnabled)
fdsAudioMasterUpdate();
if(vrc7enabled)
{
if(vrc7Clock == vrc7CycleTimer)
{
vrc7AudioCycle();
vrc7Clock = 1;
}
else
vrc7Clock++;
}
}

#if AUDIO_FLOAT
Expand All @@ -391,32 +411,8 @@ extern bool emuSkipVsync, emuSkipFrame;

static float ampVol[7] = { 3.0f, 2.0f, 1.5f, 1.2f, 1.0f, 0.85f, 0.75f };

bool apuCycle()
void apuCycle()
{
if(curBufPos == apuBufSize)
{
int updateRes = audioUpdate();
if(updateRes == 0)
{
emuSkipFrame = false;
emuSkipVsync = false;
return false;
}
if(updateRes > 6)
{
emuSkipVsync = true;
emuSkipFrame = true;
}
else
{
emuSkipFrame = false;
if(updateRes > 2)
emuSkipVsync = true;
else
emuSkipVsync = false;
}
curBufPos = 0;
}
if(p1LengthCtr && (APU_IO_Reg[0x15] & P1_ENABLE))
{
if(!p1Sweep.mute && freq1 >= 8 && freq1 < 0x7FF)
Expand Down Expand Up @@ -536,7 +532,6 @@ bool apuCycle()
#endif
apuOutBuf[curBufPos+1] = apuOutBuf[curBufPos];
curBufPos+=2;
return true;
}

void doEnvelopeLogic(envelope_t *env)
Expand Down Expand Up @@ -893,10 +888,39 @@ uint8_t *apuGetBuf()

uint32_t apuGetBufSize()
{
return apuBufSizeBytes;
#if AUDIO_FLOAT
return curBufPos*sizeof(float);
#else
return curBufPos*sizeof(int16_t);
#endif
}

uint32_t apuGetFrequency()
{
return apuFrequency;
}

static bool waitForRefill = false;
bool apuUpdate()
{
int updateRes = audioUpdate();
//printf("%i\n",updateRes);
if(updateRes == 0)
{
emuSkipFrame = false;
waitForRefill = false;
return false;
}
if(waitForRefill && updateRes < 2)
{
waitForRefill = false;
emuSkipFrame = false;
}
else if(!waitForRefill && updateRes > 2)
{
emuSkipFrame = true;
waitForRefill = true;
}
curBufPos = 0;
return true;
}
5 changes: 3 additions & 2 deletions apu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#ifndef _apu_h_
#define _apu_h_

#define NUM_BUFFERS 10
#define NUM_BUFFERS 4

void apuInitBufs();
void apuDeinitBufs();
void apuInit();
bool apuCycle();
void apuCycle();
void apuClockTimers();
void apuWriteDMCBuf(uint8_t val);
uint8_t *apuGetBuf();
Expand All @@ -22,6 +22,7 @@ uint32_t apuGetFrequency();
void apuSet8(uint8_t reg, uint8_t val);
uint8_t apuGet8(uint8_t reg);
void apuLenCycle();
bool apuUpdate();


typedef struct _envelope_t {
Expand Down
Loading

0 comments on commit 4da6b82

Please sign in to comment.