Add smart rumble to games without a rumblepak

This commit is contained in:
Lior Halphon 2020-04-29 16:06:11 +03:00
parent 151d58eb60
commit 6448a692e2
5 changed files with 52 additions and 8 deletions

View File

@ -493,7 +493,6 @@ void GB_apu_run(GB_gameboy_t *gb)
/* Step LFSR */ /* Step LFSR */
unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000;
/* Todo: is this formula is different on a GBA? */
bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1; bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1;
gb->apu.noise_channel.lfsr >>= 1; gb->apu.noise_channel.lfsr >>= 1;

View File

@ -200,13 +200,7 @@ static void display_vblank(GB_gameboy_t *gb)
} }
} }
} }
GB_handle_rumble(gb);
if (gb->rumble_callback) {
if (gb->rumble_on_cycles + gb->rumble_off_cycles) {
gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles));
gb->rumble_on_cycles = gb->rumble_off_cycles = 0;
}
}
if (gb->vblank_callback) { if (gb->vblank_callback) {
gb->vblank_callback(gb); gb->vblank_callback(gb);

View File

@ -22,6 +22,7 @@
#include "symbol_hash.h" #include "symbol_hash.h"
#include "sgb.h" #include "sgb.h"
#include "cheats.h" #include "cheats.h"
#include "rumble.h"
#define GB_STRUCT_VERSION 13 #define GB_STRUCT_VERSION 13

42
Core/rumble.c Normal file
View File

@ -0,0 +1,42 @@
#include "rumble.h"
#include "gb.h"
void GB_handle_rumble(GB_gameboy_t *gb)
{
if (gb->rumble_callback) {
if (gb->cartridge_type->has_rumble) {
if (gb->rumble_on_cycles + gb->rumble_off_cycles) {
gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles));
gb->rumble_on_cycles = gb->rumble_off_cycles = 0;
}
}
else {
unsigned volume = (gb->io_registers[GB_IO_NR50] & 7) + 1 + ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1;
unsigned ch4_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 8) + !!(gb->io_registers[GB_IO_NR51] & 0x80));
unsigned ch1_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 1) + !!(gb->io_registers[GB_IO_NR51] & 0x10));
double ch4_rumble = (MIN(gb->apu.noise_channel.sample_length * (gb->apu.noise_channel.narrow? 8 : 1) , 4096) * ((signed) gb->apu.noise_channel.current_volume * gb->apu.noise_channel.current_volume * ch4_volume / 32.0 - 50) - 2048) / 2048.0;
ch4_rumble = MIN(ch4_rumble, 1.0);
ch4_rumble = MAX(ch4_rumble, 0.0);
double ch1_rumble = 0;
if (gb->apu.sweep_enabled && ((gb->io_registers[GB_IO_NR10] >> 4) & 7)) {
double sweep_speed = (gb->io_registers[GB_IO_NR10] & 7) / (double)((gb->io_registers[GB_IO_NR10] >> 4) & 7);
ch1_rumble = gb->apu.square_channels[GB_SQUARE_1].current_volume * ch1_volume / 32.0 * sweep_speed / 8.0 - 0.5;
ch1_rumble = MIN(ch1_rumble, 1.0);
ch1_rumble = MAX(ch1_rumble, 0.0);
}
if (!gb->apu.is_active[GB_NOISE]) {
ch4_rumble = 0;
}
if (!gb->apu.is_active[GB_SQUARE_1]) {
ch1_rumble = 0;
}
gb->rumble_callback(gb, MIN(MAX(ch1_rumble / 2 + ch4_rumble, 0.0), 1.0));
}
}
}

8
Core/rumble.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef rumble_h
#define rumble_h
#include "gb_struct_def.h"
void GB_handle_rumble(GB_gameboy_t *gb);
#endif /* rumble_h */