Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Jul 25, 2015
1 parent 99ed9e6 commit 0b7b177
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 71 deletions.
3 changes: 3 additions & 0 deletions libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ static void update_variables(void)
#endif
}

extern bool lagged;

void retro_run(void)
{
bool updated = false;
Expand Down Expand Up @@ -475,6 +477,7 @@ void retro_run(void)

has_frame = 0;
UpdateJoypad();
lagged = false;
do
{
CPULoop();
Expand Down
59 changes: 18 additions & 41 deletions src/gba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif

bool cheatsEnabled = true;
bool lagged;

/*============================================================
GBA INLINE
Expand Down Expand Up @@ -492,6 +493,8 @@ static INLINE u32 CPUReadMemory(u32 address)
break;
case 0x04:
/* I/O registers */
if (address == 0x4000130)
lagged = false;
if((address < 0x4000400) && ioReadable[address & 0x3fc])
{
if(ioReadable[(address & 0x3fc) + 2])
Expand Down Expand Up @@ -576,6 +579,8 @@ static INLINE u32 CPUReadHalfWord(u32 address)
value = READ16LE(((u16 *)&internalRAM[address & 0x7ffe]));
break;
case 4:
if (address == 0x4000130)
lagged = false;
if((address < 0x4000400) && ioReadable[address & 0x3fe])
{
value = READ16LE(((u16 *)&ioMem[address & 0x3fe]));
Expand Down Expand Up @@ -672,6 +677,8 @@ static INLINE u8 CPUReadByte(u32 address)
case 3:
return internalRAM[address & 0x7fff];
case 4:
if (address == 0x4000130 || address == 0x4000131)
lagged = false;
if((address < 0x4000400) && ioReadable[address & 0x3ff])
return ioMem[address & 0x3ff];
else goto unreadable;
Expand Down Expand Up @@ -4423,11 +4430,7 @@ static int armExecute (void)

cpuTotalTicks += clockTicks;

#ifdef USE_SWITICKS
} while (cpuTotalTicks<cpuNextEvent && armState && !holdState && !SWITicks);
#else
} while ((cpuTotalTicks < cpuNextEvent) & armState & ~holdState);
#endif
return 1;
}

Expand Down Expand Up @@ -6092,11 +6095,7 @@ static int thumbExecute (void)
cpuTotalTicks += clockTicks;


#ifdef USE_SWITICKS
} while (cpuTotalTicks < cpuNextEvent && !armState && !holdState && !SWITicks);
#else
} while ((cpuTotalTicks < cpuNextEvent) & ~armState & ~holdState);
#endif
return 1;
}

Expand Down Expand Up @@ -8183,9 +8182,6 @@ static INLINE int CPUUpdateTicks (void)
{
int cpuLoopTicks = graphics.lcdTicks;

if(soundTicks < cpuLoopTicks)
cpuLoopTicks = soundTicks;

if(timer0On && (timer0Ticks < cpuLoopTicks))
cpuLoopTicks = timer0Ticks;

Expand All @@ -8198,14 +8194,6 @@ static INLINE int CPUUpdateTicks (void)
if(timer3On && !(io_registers[REG_TM3CNT] & 4) && (timer3Ticks < cpuLoopTicks))
cpuLoopTicks = timer3Ticks;

#ifdef USE_SWITICKS
if (SWITicks)
{
if (SWITicks < cpuLoopTicks)
cpuLoopTicks = SWITicks;
}
#endif

if (IRQTicks)
{
if (IRQTicks < cpuLoopTicks)
Expand Down Expand Up @@ -12226,15 +12214,6 @@ void CPULoop (void)
if(cpuTotalTicks >= cpuNextEvent) {
int remainingTicks = cpuTotalTicks - cpuNextEvent;

#ifdef USE_SWITICKS
if (SWITicks)
{
SWITicks-=clockTicks;
if (SWITicks<0)
SWITicks = 0;
}
#endif

clockTicks = cpuNextEvent;
cpuTotalTicks = 0;

Expand All @@ -12249,6 +12228,8 @@ void CPULoop (void)

graphics.lcdTicks -= clockTicks;

soundTicksUp += clockTicks;

if(graphics.lcdTicks <= 0)
{
if(io_registers[REG_DISPSTAT] & 1)
Expand Down Expand Up @@ -12311,6 +12292,9 @@ void CPULoop (void)
}
CPUCheckDMA(1, 0x0f);
systemDrawScreen();

process_sound_tick_fn();

framedone = true;
}

Expand Down Expand Up @@ -12351,12 +12335,9 @@ void CPULoop (void)
// we shouldn't be doing sound in stop state, but we lose synchronization
// if sound is disabled, so in stop state, soundTick will just produce
// mute sound
soundTicks -= clockTicks;
if(!soundTicks)
{
process_sound_tick_fn();
soundTicks += SOUND_CLOCK_TICKS;
}

// moving this may have consequences; we'll see
//soundTicksUp += clockTicks;

if(!stopState) {
if(timer0On) {
Expand Down Expand Up @@ -12475,7 +12456,7 @@ void CPULoop (void)
cpuDmaTicksToUpdate -= clockTicks;
if(cpuDmaTicksToUpdate < 0)
cpuDmaTicksToUpdate = 0;
goto updateLoop;
goto skipIRQ;
}

if(io_registers[REG_IF] && (io_registers[REG_IME] & 1) && armIrqEnable)
Expand Down Expand Up @@ -12512,15 +12493,11 @@ void CPULoop (void)
}
}

#ifdef USE_SWITICKS
// Stops the SWI Ticks emulation if an IRQ is executed
//(to avoid problems with nested IRQ/SWI)
if (SWITicks)
SWITicks = 0;
#endif
}
}

skipIRQ:

if(remainingTicks > 0) {
if(remainingTicks > cpuNextEvent)
clockTicks = cpuNextEvent;
Expand Down
52 changes: 24 additions & 28 deletions src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,9 @@ INLINE void Gb_Wave::write( unsigned addr, int data )
wave_bank[index] = data;;
}

static int16_t soundFinalWave [1600];
static int16_t soundFinalWave [2048];
long soundSampleRate = 22050;
int SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_;
int soundTicks = SOUND_CLOCK_TICKS_;
int soundTicksUp; // counts up from 0 being the last time the blips were emptied

static int soundEnableFlag = 0x3ff; /* emulator channels enabled*/
static float const apu_vols [4] = { -0.25f, -0.5f, -1.0f, -0.25f };
Expand Down Expand Up @@ -374,7 +373,7 @@ static void gba_pcm_apply_control( int pcm_idx, int idx )
if ( pcm[pcm_idx].pcm.output != out )
{
if ( pcm[pcm_idx].pcm.output )
pcm_synth.offset( SOUND_CLOCK_TICKS - soundTicks, -pcm[pcm_idx].pcm.last_amp, pcm[pcm_idx].pcm.output );
pcm_synth.offset( soundTicksUp, -pcm[pcm_idx].pcm.last_amp, pcm[pcm_idx].pcm.output );
pcm[pcm_idx].pcm.last_amp = 0;
pcm[pcm_idx].pcm.output = out;
}
Expand Down Expand Up @@ -1476,8 +1475,7 @@ Blip_Buffer::Blip_Buffer()

Blip_Buffer::~Blip_Buffer()
{
if (buffer_)
free(buffer_);
free(buffer_);
}

void Blip_Buffer::clear( void)
Expand Down Expand Up @@ -1722,7 +1720,7 @@ static void pcm_fifo_write_control( int data, int data2)

if(pcm[0].pcm.output)
{
int time = SOUND_CLOCK_TICKS - soundTicks;
int time = soundTicksUp;

pcm[0].dac = (int8_t)pcm[0].dac >> pcm[0].pcm.shift;
int delta = pcm[0].dac - pcm[0].pcm.last_amp;
Expand Down Expand Up @@ -1751,7 +1749,7 @@ static void pcm_fifo_write_control( int data, int data2)

if(pcm[1].pcm.output)
{
int time = SOUND_CLOCK_TICKS - soundTicks;
int time = soundTicksUp;

pcm[1].dac = (int8_t)pcm[1].dac >> pcm[1].pcm.shift;
int delta = pcm[1].dac - pcm[1].pcm.last_amp;
Expand Down Expand Up @@ -1838,7 +1836,7 @@ static void gba_pcm_fifo_timer_overflowed( unsigned pcm_idx )

if(pcm[pcm_idx].pcm.output)
{
int time = SOUND_CLOCK_TICKS - soundTicks;
int time = soundTicksUp;

pcm[pcm_idx].dac = (int8_t)pcm[pcm_idx].dac >> pcm[pcm_idx].pcm.shift;
int delta = pcm[pcm_idx].dac - pcm[pcm_idx].pcm.last_amp;
Expand All @@ -1856,7 +1854,7 @@ void soundEvent_u8_parallel(int gb_addr[], uint32_t address[], uint8_t data[])
for(uint32_t i = 0; i < 2; i++)
{
ioMem[address[i]] = data[i];
gb_apu_write_register( SOUND_CLOCK_TICKS - soundTicks, gb_addr[i], data[i] );
gb_apu_write_register( soundTicksUp, gb_addr[i], data[i] );

if ( address[i] == NR52 )
{
Expand All @@ -1870,7 +1868,7 @@ void soundEvent_u8_parallel(int gb_addr[], uint32_t address[], uint8_t data[])
void soundEvent_u8(int gb_addr, uint32_t address, uint8_t data)
{
ioMem[address] = data;
gb_apu_write_register( SOUND_CLOCK_TICKS - soundTicks, gb_addr, data );
gb_apu_write_register( soundTicksUp, gb_addr, data );

if ( address == NR52 )
{
Expand Down Expand Up @@ -1940,32 +1938,34 @@ void soundTimerOverflow(int timer)
void process_sound_tick_fn (void)
{
// Run sound hardware to present
pcm[0].pcm.last_time -= SOUND_CLOCK_TICKS;
pcm[0].pcm.last_time -= soundTicksUp;
if ( pcm[0].pcm.last_time < -2048 )
pcm[0].pcm.last_time = -2048;

pcm[1].pcm.last_time -= SOUND_CLOCK_TICKS;
pcm[1].pcm.last_time -= soundTicksUp;
if ( pcm[1].pcm.last_time < -2048 )
pcm[1].pcm.last_time = -2048;

/* Emulates sound hardware up to a specified time, ends current time
frame, then starts a new frame at time 0 */

if(SOUND_CLOCK_TICKS > gb_apu.last_time)
gb_apu_run_until_( SOUND_CLOCK_TICKS );
if(soundTicksUp > gb_apu.last_time)
gb_apu_run_until_( soundTicksUp );

gb_apu.frame_time -= SOUND_CLOCK_TICKS;
gb_apu.last_time -= SOUND_CLOCK_TICKS;
gb_apu.frame_time -= soundTicksUp;
gb_apu.last_time -= soundTicksUp;

bufs_buffer[2].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[2].factor_;
bufs_buffer[1].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[1].factor_;
bufs_buffer[0].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[0].factor_;
bufs_buffer[2].offset_ += soundTicksUp * bufs_buffer[2].factor_;
bufs_buffer[1].offset_ += soundTicksUp * bufs_buffer[1].factor_;
bufs_buffer[0].offset_ += soundTicksUp * bufs_buffer[0].factor_;


// dump all the samples available
// VBA will only ever store 1 frame worth of samples
int numSamples = stereo_buffer_read_samples( (int16_t*) soundFinalWave, stereo_buffer_samples_avail());
systemOnWriteDataToSoundBuffer(soundFinalWave, numSamples);

soundTicksUp = 0;
}

static void apply_muting (void)
Expand Down Expand Up @@ -2006,7 +2006,7 @@ static void remake_stereo_buffer (void)

stereo_buffer_clear();

soundTicks = SOUND_CLOCK_TICKS;
soundTicksUp = 0;

apply_muting();

Expand All @@ -2023,18 +2023,14 @@ void soundReset (void)

stereo_buffer_clear();

soundTicks = SOUND_CLOCK_TICKS;
//End of Reset APU

SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_;
soundTicks = SOUND_CLOCK_TICKS_;
soundTicksUp = 0;

// Sound Event (NR52)
int gb_addr = table[NR52 - 0x60];
if ( gb_addr )
{
ioMem[NR52] = 0x80;
gb_apu_write_register( SOUND_CLOCK_TICKS - soundTicks, gb_addr, 0x80 );
gb_apu_write_register( soundTicksUp, gb_addr, 0x80 );

gba_pcm_apply_control(0, 0 );
gba_pcm_apply_control(1, 1 );
Expand Down Expand Up @@ -2134,7 +2130,7 @@ void soundReadGameMem(const uint8_t *& in_data, int)

stereo_buffer_clear();

soundTicks = SOUND_CLOCK_TICKS;
soundTicksUp = 0;
//End of Reset APU

gb_apu_save_state( &state.apu );
Expand Down
3 changes: 1 addition & 2 deletions src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ void process_sound_tick_fn (void);
void soundSaveGameMem(uint8_t *& data);
void soundReadGameMem(const uint8_t *& data, int version);

extern int SOUND_CLOCK_TICKS; // Number of 16.8 MHz clocks between calls to soundTick()
extern int soundTicks; // Number of 16.8 MHz clocks until soundTick() will be called
extern int soundTicksUp; // Number of 16.8 MHz clocks until soundTick() will be called

#endif // SOUND_H

0 comments on commit 0b7b177

Please sign in to comment.