Merge pull request #62 from retro-wertz/gb_timing_updates

Gb timing updates
This commit is contained in:
Twinaphex 2019-07-22 20:19:27 +02:00 committed by GitHub
commit 2e96f59ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 128 deletions

View File

@ -1232,7 +1232,7 @@ void gbWriteMemory(uint16_t address, uint8_t value)
case 0x3e:
case 0x3f:
// Sound registers handled by blargg
gbSoundEvent(address, value);
gbSoundEvent(soundTicks, address, value);
//gbMemory[address] = value;
return;
@ -1967,7 +1967,7 @@ uint8_t gbReadMemory(uint16_t address)
case 0x3e:
case 0x3f:
// Sound registers read
return gbSoundRead(address);
return gbSoundRead(soundTicks, address);
case 0x40:
return register_LCDC;
case 0x41:
@ -4535,12 +4535,37 @@ void gbDrawLine()
}
}
static void gbUpdateJoypads(bool readSensors)
{
if (systemReadJoypads()) {
// read joystick
if (gbSgbMode && gbSgbMultiplayer) {
if (gbSgbFourPlayers) {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
gbJoymask[2] = systemReadJoypad(2);
gbJoymask[3] = systemReadJoypad(3);
} else {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
}
} else {
gbJoymask[0] = systemReadJoypad(-1);
}
}
if (readSensors && gbRomType == 0x22) {
systemUpdateMotionSensor();
}
}
void gbEmulate(int ticksToStop)
{
gbRegister tempRegister;
uint8_t tempValue;
int8_t offset;
clockTicks = 0;
gbDmaTicks = 0;
@ -4549,6 +4574,9 @@ void gbEmulate(int ticksToStop)
int opcode1 = 0;
int opcode2 = 0;
bool execute = false;
bool frameDone = false;
gbUpdateJoypads(true);
while (1) {
uint16_t oldPCW = PC.W;
@ -4661,6 +4689,8 @@ void gbEmulate(int ticksToStop)
}
ticksToStop -= clockTicks;
soundTicks += clockTicks;
if (!gbSpeed) soundTicks += clockTicks;
// DIV register emulation
gbDivTicks -= clockTicks;
@ -4930,6 +4960,7 @@ void gbEmulate(int ticksToStop)
gbFrameCount++;
systemFrame();
gbSoundTick(soundTicks);
if ((gbFrameCount % 10) == 0)
system10Frames(60);
@ -4944,28 +4975,8 @@ void gbEmulate(int ticksToStop)
gbFrameCount = 0;
}
if (systemReadJoypads()) {
// read joystick
if (gbSgbMode && gbSgbMultiplayer) {
if (gbSgbFourPlayers) {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
gbJoymask[2] = systemReadJoypad(2);
gbJoymask[3] = systemReadJoypad(3);
} else {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
}
} else {
gbJoymask[0] = systemReadJoypad(-1);
}
}
int newmask = gbJoymask[0] & 255;
if (gbRomType == 0x22) {
systemUpdateMotionSensor();
}
if (newmask) {
gbMemory[0xff0f] = register_IF |= 16;
}
@ -5003,6 +5014,8 @@ void gbEmulate(int ticksToStop)
} else
gbFrameSkipCount++;
frameDone = true;
} else {
// go the the OAM being accessed mode
gbLcdTicksDelayed += GBLCD_MODE_2_CLOCK_TICKS;
@ -5151,25 +5164,11 @@ void gbEmulate(int ticksToStop)
ticksToStop = 0;
}
}
if (systemReadJoypads()) {
// read joystick
if (gbSgbMode && gbSgbMultiplayer) {
if (gbSgbFourPlayers) {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
gbJoymask[2] = systemReadJoypad(2);
gbJoymask[3] = systemReadJoypad(3);
} else {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
}
} else {
gbJoymask[0] = systemReadJoypad(-1);
}
}
gbFrameCount++;
systemFrame();
gbSoundTick(soundTicks);
if ((gbFrameCount % 10) == 0)
system10Frames(60);
@ -5183,6 +5182,7 @@ void gbEmulate(int ticksToStop)
gbLastTime = currentTime;
gbFrameCount = 0;
}
frameDone = true;
}
}
}
@ -5279,15 +5279,14 @@ void gbEmulate(int ticksToStop)
#endif
}
#endif
soundTicks -= clockTicks;
if (!gbSpeed)
soundTicks -= clockTicks;
while (soundTicks < 0) {
soundTicks += SOUND_CLOCK_TICKS;
gbSoundTick();
// TODO: evaluate and fix this
// On VBA-M (gb core running twice as fast?), each vblank is uses 35112 cycles.
// on some cases no vblank is generated causing sound ticks to keep accumulating causing core to crash.
// This forces core to flush sound buffers when expected sound ticks has passed and no frame is done yet.which then ends cpuloop
if ((soundTicks > SOUND_CLOCK_TICKS) && !frameDone) {
int last_st = soundTicks;
gbSoundTick(soundTicks);
soundTicks = (last_st - SOUND_CLOCK_TICKS);
}
// timer emulation
@ -5413,25 +5412,7 @@ void gbEmulate(int ticksToStop)
gbBlackScreen = false;
if ((ticksToStop <= 0)) {
if (!(register_LCDC & 0x80)) {
if (systemReadJoypads()) {
// read joystick
if (gbSgbMode && gbSgbMultiplayer) {
if (gbSgbFourPlayers) {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
gbJoymask[2] = systemReadJoypad(2);
gbJoymask[3] = systemReadJoypad(3);
} else {
gbJoymask[0] = systemReadJoypad(0);
gbJoymask[1] = systemReadJoypad(1);
}
} else {
gbJoymask[0] = systemReadJoypad(-1);
}
}
}
if (ticksToStop <= 0 || frameDone) { // Stop loop
return;
}
}
@ -5821,7 +5802,7 @@ struct EmulatedSystem GBSystem = {
false,
// emuCount
#ifdef FINAL_VERSION
70000 / 4,
72000,
#else
1000,
#endif

View File

@ -24,25 +24,20 @@ static bool declicking = false;
int const chan_count = 4;
int const ticks_to_time = 2 * GB_APU_OVERCLOCK;
static inline blip_time_t blip_time()
{
return (SOUND_CLOCK_TICKS - soundTicks) * ticks_to_time;
}
uint8_t gbSoundRead(uint16_t address)
uint8_t gbSoundRead(int st, uint16_t address)
{
if (gb_apu && address >= NR10 && address <= 0xFF3F)
return gb_apu->read_register(blip_time(), address);
return gb_apu->read_register((blip_time_t)(st * ticks_to_time), address);
return gbMemory[address];
}
void gbSoundEvent(uint16_t address, int data)
void gbSoundEvent(int st, uint16_t address, int data)
{
gbMemory[address] = data;
if (gb_apu && address >= NR10 && address <= 0xFF3F)
gb_apu->write_register(blip_time(), address, data);
gb_apu->write_register((blip_time_t)(st * ticks_to_time), address, data);
}
static void end_frame(blip_time_t time)
@ -83,11 +78,11 @@ static void apply_volume()
gb_apu->volume(soundVolume_);
}
void gbSoundTick()
void gbSoundTick(int st)
{
if (gb_apu && stereo_buffer) {
// Run sound hardware to present
end_frame(SOUND_CLOCK_TICKS * ticks_to_time);
end_frame((blip_time_t)(st * ticks_to_time));
flush_samples(stereo_buffer);
@ -100,6 +95,8 @@ void gbSoundTick()
if (soundVolume_ != soundGetVolume())
apply_volume();
}
soundTicks = 0;
}
static void reset_apu()
@ -115,7 +112,7 @@ static void reset_apu()
if (stereo_buffer)
stereo_buffer->clear();
soundTicks = SOUND_CLOCK_TICKS;
soundTicks = 0;
}
static void remake_stereo_buffer()
@ -171,42 +168,42 @@ bool gbSoundGetDeclicking()
void gbSoundReset()
{
SOUND_CLOCK_TICKS = 20000; // 1/100 second
SOUND_CLOCK_TICKS = 35112;
remake_stereo_buffer();
reset_apu();
soundPaused = 1;
gbSoundEvent(0xff10, 0x80);
gbSoundEvent(0xff11, 0xbf);
gbSoundEvent(0xff12, 0xf3);
gbSoundEvent(0xff14, 0xbf);
gbSoundEvent(0xff16, 0x3f);
gbSoundEvent(0xff17, 0x00);
gbSoundEvent(0xff19, 0xbf);
gbSoundEvent(0, 0xff10, 0x80);
gbSoundEvent(0, 0xff11, 0xbf);
gbSoundEvent(0, 0xff12, 0xf3);
gbSoundEvent(0, 0xff14, 0xbf);
gbSoundEvent(0, 0xff16, 0x3f);
gbSoundEvent(0, 0xff17, 0x00);
gbSoundEvent(0, 0xff19, 0xbf);
gbSoundEvent(0xff1a, 0x7f);
gbSoundEvent(0xff1b, 0xff);
gbSoundEvent(0xff1c, 0xbf);
gbSoundEvent(0xff1e, 0xbf);
gbSoundEvent(0, 0xff1a, 0x7f);
gbSoundEvent(0, 0xff1b, 0xff);
gbSoundEvent(0, 0xff1c, 0xbf);
gbSoundEvent(0, 0xff1e, 0xbf);
gbSoundEvent(0xff20, 0xff);
gbSoundEvent(0xff21, 0x00);
gbSoundEvent(0xff22, 0x00);
gbSoundEvent(0xff23, 0xbf);
gbSoundEvent(0xff24, 0x77);
gbSoundEvent(0xff25, 0xf3);
gbSoundEvent(0, 0xff20, 0xff);
gbSoundEvent(0, 0xff21, 0x00);
gbSoundEvent(0, 0xff22, 0x00);
gbSoundEvent(0, 0xff23, 0xbf);
gbSoundEvent(0, 0xff24, 0x77);
gbSoundEvent(0, 0xff25, 0xf3);
if (gbHardware & 0x4)
gbSoundEvent(0xff26, 0xf0);
gbSoundEvent(0, 0xff26, 0xf0);
else
gbSoundEvent(0xff26, 0xf1);
gbSoundEvent(0, 0xff26, 0xf1);
/* workaround for game Beetlejuice */
if (gbHardware & 0x1) {
gbSoundEvent(0xff24, 0x77);
gbSoundEvent(0xff25, 0xf3);
gbSoundEvent(0, 0xff24, 0x77);
gbSoundEvent(0, 0xff25, 0xf3);
}
int addr = 0xff30;
@ -399,7 +396,6 @@ static void gbSoundReadGameOld(int version, gzFile gzFile)
#endif
// New state format
static variable_desc gb_state[] = {
LOAD(int, state.version), // room_for_expansion will be used by later versions
@ -427,7 +423,8 @@ static variable_desc gb_state[] = {
SKIP(int[13], room_for_expansion),
// Emulator
SKIP(int[16], room_for_expansion),
LOAD(int, soundTicks),
SKIP(int[15], room_for_expansion),
{ NULL, 0 }
};
@ -445,7 +442,7 @@ void gbSoundSaveGame(gzFile out)
state.version = 1;
#ifdef __LIBRETRO__
utilWriteDataMem(out, gb_state);
utilWriteDataMem(out, gb_state);
#else
utilWriteData(out, gb_state);
#endif
@ -461,10 +458,10 @@ void gbSoundReadGame(int version, gzFile in)
reset_apu();
gb_apu->save_state(&state.apu);
if (version > 11)
#ifdef __LIBRETRO__
utilReadDataMem(in, gb_state);
utilReadDataMem(in, gb_state);
#else
if (version > 11)
utilReadData(in, gb_state);
else
gbSoundReadGameOld(version, in);

View File

@ -56,14 +56,14 @@ extern gb_effects_config_t gb_effects_config; // current configuration
void gbSoundReset();
// Emulates write to sound hardware
void gbSoundEvent(uint16_t address, int data);
void gbSoundEvent(int st, uint16_t address, int data);
#define SOUND_EVENT gbSoundEvent
// Emulates read from sound hardware
uint8_t gbSoundRead(uint16_t address);
uint8_t gbSoundRead(int st, uint16_t address);
// Notifies emulator that SOUND_CLOCK_TICKS clocks have passed
void gbSoundTick();
void gbSoundTick(int st);
extern int SOUND_CLOCK_TICKS; // Number of 16.8 MHz clocks between calls to gbSoundTick()
extern int soundTicks; // Number of 16.8 MHz clocks until gbSoundTick() will be called

View File

@ -718,8 +718,8 @@ static variable_desc gba_state[] = {
// Emulator
LOAD(int, soundEnableFlag),
SKIP(int[15], room_for_expansion),
LOAD(int, soundTicks),
SKIP(int[14], room_for_expansion),
{ NULL, 0 }
};
@ -806,10 +806,10 @@ void soundReadGame(gzFile in, int version)
reset_apu();
gb_apu->save_state(&state.apu);
if (version > SAVE_GAME_VERSION_9)
#ifdef __LIBRETRO__
utilReadDataMem(in, gba_state);
utilReadDataMem(in, gba_state);
#else
if (version > SAVE_GAME_VERSION_9)
utilReadData(in, gba_state);
else
soundReadGameOld(in, version);

View File

@ -1689,6 +1689,9 @@ int main(int argc, char** argv)
gb_effects_config.surround = false;
gb_effects_config.enabled = false;
ReadOpts(argc, argv);
LoadConfig(); // Parse command line arguments (overrides ini)
inputSetKeymap(PAD_1, KEY_LEFT, ReadPrefHex("Joy0_Left"));
inputSetKeymap(PAD_1, KEY_RIGHT, ReadPrefHex("Joy0_Right"));
inputSetKeymap(PAD_1, KEY_UP, ReadPrefHex("Joy0_Up"));
@ -1750,9 +1753,6 @@ int main(int argc, char** argv)
inputSetMotionKeymap(KEY_UP, ReadPrefHex("Motion_Up"));
inputSetMotionKeymap(KEY_DOWN, ReadPrefHex("Motion_Down"));
LoadConfig(); // Parse command line arguments (overrides ini)
ReadOpts(argc, argv);
if (!sdlCheckDirectory(screenShotDir))
screenShotDir = NULL;
if (!sdlCheckDirectory(saveDir))

View File

@ -9,6 +9,7 @@ struct wxSDLJoyState {
SDL_Joystick* dev;
int nax, nhat, nbut;
short* curval;
short* initial_val;
bool is_valid;
~wxSDLJoyState()
{
@ -16,12 +17,15 @@ struct wxSDLJoyState {
SDL_JoystickClose(dev);
if (curval)
delete[] curval;
if (initial_val)
delete[] initial_val;
}
wxSDLJoyState()
{
dev = NULL;
nax = nhat = nbut = 0;
curval = NULL;
initial_val = NULL;
is_valid = true;
}
};
@ -70,17 +74,19 @@ wxSDLJoy::wxSDLJoy(bool analog)
nctrl += joystate[i].nhat = hats < 0 ? 0 : hats;
nctrl += joystate[i].nbut = buttons < 0 ? 0 : buttons;
joystate[i].curval = new short[nctrl]{0};
joystate[i].curval = new short[nctrl]{};
// clear controls
memset(joystate[i].curval, 0, sizeof(short) * nctrl);
// initialize axis previous value to initial state
// set initial values array
// see below for 360 trigger handling
#if SDL_VERSION_ATLEAST(2, 0, 6)
joystate[i].initial_val = new short[nctrl]{};
for (int j = 0; j < joystate[i].nax; j++) {
int16_t initial_state = 0;
SDL_JoystickGetAxisInitialState(dev, j, &initial_state);
joystate[i].curval[j] = initial_state;
joystate[i].initial_val[j] = initial_state;
// curval is 0 which is what initial state maps to ATM
}
#endif
}
@ -181,6 +187,13 @@ void wxSDLJoy::Notify()
val = 0;
}
// This is for the 360 and similar triggers which return a
// value on initial state of the trigger axis. This hack may be
// insufficient, we may need to expand the joy event API to
// support initial values.
if (joystate[i].initial_val && val == joystate[i].initial_val[j])
val = 0;
if (handler && val != joystate[i].curval[j]) {
wxSDLJoyEvent ev(wxEVT_SDLJOY, GetId());
ev.joy = i;

View File

@ -172,7 +172,7 @@ DISTS=$DISTS'
ninja https://github.com/ninja-build/ninja/archive/v1.8.2.tar.gz bin/ninja
meson https://github.com/mesonbuild/meson/releases/download/0.44.0/meson-0.44.0.tar.gz bin/meson
glib https://github.com/GNOME/glib/archive/2.58.1.tar.gz lib/libglib-2.0.a
libgpg-error https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.27.tar.bz2 lib/libgpg-error.a
libgpg-error https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.36.tar.bz2 lib/libgpg-error.a
libgcrypt https://www.gnupg.org/ftp/gcrypt/libgcrypt/libgcrypt-1.8.2.tar.bz2 lib/libgcrypt.a
libsecret http://ftp.gnome.org/pub/gnome/sources/libsecret/0.18/libsecret-0.18.5.tar.xz lib/libsecret-1.a
sdl2 https://www.libsdl.org/release/SDL2-2.0.9.tar.gz lib/libSDL2.a
@ -241,6 +241,7 @@ DIST_PATCHES=$DIST_PATCHES'
python2 https://gist.githubusercontent.com/rkitover/2d9e5baff1f1cc4f2618dee53083bd35/raw/7f33fcf5470a9f1013ac6ae7bb168368a98fe5a0/python-2.7.14-custom-static-openssl.patch https://gist.githubusercontent.com/rkitover/afab7ed3ac7ce1860c43a258571c8ae1/raw/6f5fc90a7acf5f5c3ffda2edf402b28f469a4b3b/python-2.7.14-static-libintl.patch
python3 https://gist.githubusercontent.com/rkitover/93d89a679705875c59275fb0a8f22b45/raw/6149e7fa3920d6c674c79448c5a4c9313620e06c/python-3.6.3-custom-static-openssl.patch https://gist.githubusercontent.com/rkitover/b18f19eafda3775a9652cc9cdf3ec914/raw/ed14c34bf9f205ccc3a4684dbdb83f8620162b98/python-3.6.3-static-libintl.patch
intltool https://gist.githubusercontent.com/rkitover/d638882f52e5d5f8e392cbf6842cd6d0/raw/dcfbe358bbb8b89f88b40a9c3402494552fd33f8/intltool-0.51.0.patch
libgpg-error https://raw.githubusercontent.com/gentoo/gentoo/master/dev-libs/libgpg-error/files/libgpg-error-1.36-gawk5-support.patch
'
DIST_TAR_ARGS="$DIST_TAR_ARGS
@ -259,6 +260,7 @@ DIST_CONFIGURE_TYPES="$DIST_CONFIGURE_TYPES
graphviz autoreconf
docbook2x autoreconf
libvorbis autoreconf
libgpg-error autoreconf
"
DIST_RELOCATION_TYPES="$DIST_RELOCATION_TYPES
@ -421,6 +423,7 @@ DIST_MAKE_INSTALL_ARGS="$DIST_MAKE_INSTALL_ARGS
"
DIST_EXTRA_LDFLAGS="$DIST_EXTRA_LDFLAGS
glib -liconv
graphviz -lpcreposix
doxygen -lintl -liconv
ffmpeg -lm -llzma -lpthread
@ -688,12 +691,12 @@ msys2_install_core_deps() {
pacman -Sy
set --
for p in binutils curl crt-git gcc gcc-libs headers-git tools-git windows-default-manifest libmangle-git; do
for p in binutils curl crt-git gcc gcc-libs headers-git tools-git windows-default-manifest libmangle-git meson; do
set -- "$@" "${target}-${p}"
done
# install
pacman --noconfirm --needed -S make tar patch diffutils ccache perl msys2-w32api-headers msys2-runtime-devel gcc gcc-libs mpfr windows-default-manifest python2 "$@"
pacman --noconfirm --needed -S make tar patch diffutils ccache perl msys2-w32api-headers msys2-runtime-devel gcc gcc-libs mpfr windows-default-manifest python python2 "$@"
# make sure msys perl takes precedence over mingw perl if the latter is installed
mkdir -p "$BUILD_ROOT/root/bin"

View File

@ -60,7 +60,7 @@ perl_dists="$perl_dists XML-NamespaceSupport XML-SAX-Base XML-SAX"
perl_dists=$(list_remove_duplicates $perl_dists)
host_dists="$host_dists autoconf autoconf-archive automake m4 gsed bison \
flex-2.6.3 flex c2man docbook2x ccache"
flex-2.6.3 flex c2man docbook2x ccache ninja"
host_dists=$(list_remove_duplicates $host_dists)
both_dists="$both_dists openssl zlib bzip2 libiconv"
@ -200,7 +200,7 @@ for dist in $both_dists; do
table_line_replace DIST_POST_BUILD $dist "eval \"\$(target_env)\"; $(table_line DIST_POST_BUILD $dist)"
done
remove_dists='graphviz python2 python3 swig libxml2-python doxygen bakefile setuptools pip meson XML-Parser intltool ninja libsecret shared-mime-info'
remove_dists='graphviz python2 python3 meson swig libxml2-python doxygen bakefile setuptools pip XML-Parser intltool libsecret shared-mime-info'
for dist in $remove_dists; do
if ! list_contains $dist $do_not_remove_dists; then
@ -340,8 +340,9 @@ table_line_append DIST_ARGS gettext "--enable-threads=windows"
table_line_append DIST_ARGS glib "--with-threads=posix --disable-libelf"
table_line_append DIST_PATCHES glib "\
https://raw.githubusercontent.com/Alexpux/MINGW-packages/master/mingw-w64-glib2/0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch \
https://raw.githubusercontent.com/msys2/MINGW-packages/master/mingw-w64-glib2/0001-Update-g_fopen-g_open-and-g_creat-to-open-with-FILE_.patch \
https://raw.githubusercontent.com/Alexpux/MINGW-packages/master/mingw-w64-glib2/0001-win32-Make-the-static-build-work-with-MinGW-when-pos.patch \
https://raw.githubusercontent.com/msys2/MINGW-packages/master/mingw-w64-glib2/0001-disable-some-tests-when-static.patch \
https://gist.githubusercontent.com/rkitover/2edaf9583fb3068bb14016571e6f7d01/raw/ece80116d5618f372464f02392a9bcab670ce6c1/glib-mingw-no-strerror_s.patch \
"