mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-04-02 10:42:14 -04:00
byuu says: Changelog: - gb/mbc6: mapper is now functional, but Net de Get has some text corruption¹ - gb/mbc7: mapper is now functional² - gb/cpu: HDMA syncs other components after each byte transfer now - gb/ppu: LY,LX forced to zero when LCDC.d7 is lowered (eg disabled), not when it's raised (eg enabled) - gb/ppu: the LCD does not run at all when LCDC.d7 is clear³ - fixes graphical corruption between scene transitions in Legend of Zelda - Oracle of Ages - thanks to Cydrak, Shonumi, gekkio for their input on the cause of this issue - md/controller: renamed "Gamepad" to "Control Pad" per official terminology - md/controller: added "Fighting Pad" (6-button controller) emulation [hex\_usr] - processor/m68k: fixed TAS to set data.d7 when EA.mode==DataRegisterDirect; fixes Asterix - hiro/windows: removed carriage returns from mouse.cpp and desktop.cpp - ruby/audio/alsa: added device driver selection [SuperMikeMan] - ruby/audio/ao: set format.matrix=nullptr to prevent a crash on some systems [SuperMikeMan] - ruby/video/cgl: rename term() to terminate() to fix a crash on macOS [Sintendo] ¹: The observation that this mapper split $4000-7fff into two banks came from MAME's implementation. But their implementation was quite broken and incomplete, so I didn't actually use any of it. The observation that this mapper split $a000-bfff into two banks came from Tauwasser, and I did directly use that information, plus the knowledge that $0400/$0800 are the RAM bank select registers. The text corruption is due to a race condition with timing. The game is transferring font letters via HDMA, but the game code ends up setting the bank# with the font a bit too late after the HDMA has already occurred. I'm not sure how to fix this ... as a whole, I assumed my Game Boy timing was pretty good, but apparently it's not that good. ²: The entire design of this mapper comes from endrift's notes. endrift gets full credit for higan being able to emulate this mapper. Note that the accelerometer implementation is still not tested, and probably won't work right until I tweak the sensitivity a lot. ³: So the fun part of this is ... it breaks the strict 60fps rate of the Game Boy. This was always inevitable: certain timing conditions can stretch frames, too. But this is pretty much an absolute deal breaker for something like Vsync timing. This pretty much requires adaptive sync to run well without audio stuttering during the transition. There's currently one very important detail missing: when the LCD is turned off, presumably the image on the screen fades to white. I do not know how long this process takes, or how to really go about emulating it. Right now as an incomplete patch, I'm simply leaving the last displayed image on the screen until the LCD is turned on again. But I will have to output white, as well as add code to break out of the emulation loop periodically when the LCD is left off eg indefinitely, or bad things would happen. I'll work something out and then implement. Another detail is I'm not sure how long it takes for the LCD to start rendering again once enabled. Right now, it's immediate. I've heard it's as long as 1/60th of a second, but that really seems incredibly excessive? I'd like to know at least a reasonably well-supported estimate before I implement that.
176 lines
5.2 KiB
C++
176 lines
5.2 KiB
C++
#include <alsa/asoundlib.h>
|
|
|
|
struct AudioALSA : Audio {
|
|
AudioALSA() { initialize(); }
|
|
~AudioALSA() { terminate(); }
|
|
|
|
auto ready() -> bool { return _ready; }
|
|
|
|
auto information() -> Information {
|
|
Information information;
|
|
information.devices = queryDevices();
|
|
information.frequencies = {44100.0, 48000.0, 96000.0};
|
|
information.latencies = {20, 40, 60, 80, 100};
|
|
information.channels = {2};
|
|
return information;
|
|
}
|
|
|
|
auto device() -> string { return _device; }
|
|
auto blocking() -> bool { return _blocking; }
|
|
auto channels() -> uint { return 2; }
|
|
auto frequency() -> double { return _frequency; }
|
|
auto latency() -> uint { return _latency; }
|
|
|
|
auto setDevice(string device) -> bool {
|
|
if(_device == device) return true;
|
|
_device = device;
|
|
return initialize();
|
|
}
|
|
|
|
auto setBlocking(bool blocking) -> bool {
|
|
if(_blocking == blocking) return true;
|
|
_blocking = blocking;
|
|
return true;
|
|
}
|
|
|
|
auto setFrequency(double frequency) -> bool {
|
|
if(_frequency == frequency) return true;
|
|
_frequency = frequency;
|
|
return initialize();
|
|
}
|
|
|
|
auto setLatency(uint latency) -> bool {
|
|
if(_latency == latency) return true;
|
|
_latency = latency;
|
|
return initialize();
|
|
}
|
|
|
|
auto output(const double samples[]) -> void {
|
|
if(!ready()) return;
|
|
|
|
_buffer[_offset++] = uint16_t(samples[0] * 32768.0) << 0 | uint16_t(samples[1] * 32768.0) << 16;
|
|
if(_offset < _periodSize) return;
|
|
|
|
snd_pcm_sframes_t available;
|
|
do {
|
|
available = snd_pcm_avail_update(_interface);
|
|
if(available < 0) snd_pcm_recover(_interface, available, 1);
|
|
if(available < _offset) {
|
|
if(!_blocking) {
|
|
_offset = 0;
|
|
return;
|
|
}
|
|
int error = snd_pcm_wait(_interface, -1);
|
|
if(error < 0) snd_pcm_recover(_interface, error, 1);
|
|
}
|
|
} while(available < _offset);
|
|
|
|
uint32_t* output = _buffer;
|
|
int i = 4;
|
|
|
|
while(_offset > 0 && i--) {
|
|
snd_pcm_sframes_t written = snd_pcm_writei(_interface, output, _offset);
|
|
if(written < 0) {
|
|
//no samples written
|
|
snd_pcm_recover(_interface, written, 1);
|
|
} else if(written <= _offset) {
|
|
_offset -= written;
|
|
output += written;
|
|
}
|
|
}
|
|
|
|
if(i < 0) {
|
|
if(_buffer == output) {
|
|
_offset--;
|
|
output++;
|
|
}
|
|
memory::move(_buffer, output, _offset * sizeof(uint32_t));
|
|
}
|
|
}
|
|
|
|
private:
|
|
auto initialize() -> bool {
|
|
terminate();
|
|
|
|
string device = "default";
|
|
if(queryDevices().find(_device)) device = _device;
|
|
if(snd_pcm_open(&_interface, device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return terminate(), false;
|
|
|
|
uint rate = (uint)_frequency;
|
|
uint bufferTime = _latency * 1000;
|
|
uint periodTime = _latency * 1000 / 4;
|
|
|
|
snd_pcm_hw_params_t* hardwareParameters;
|
|
snd_pcm_hw_params_alloca(&hardwareParameters);
|
|
if(snd_pcm_hw_params_any(_interface, hardwareParameters) < 0) return terminate(), false;
|
|
|
|
if(snd_pcm_hw_params_set_access(_interface, hardwareParameters, SND_PCM_ACCESS_RW_INTERLEAVED) < 0
|
|
|| snd_pcm_hw_params_set_format(_interface, hardwareParameters, SND_PCM_FORMAT_S16_LE) < 0
|
|
|| snd_pcm_hw_params_set_channels(_interface, hardwareParameters, 2) < 0
|
|
|| snd_pcm_hw_params_set_rate_near(_interface, hardwareParameters, &rate, 0) < 0
|
|
|| snd_pcm_hw_params_set_period_time_near(_interface, hardwareParameters, &periodTime, 0) < 0
|
|
|| snd_pcm_hw_params_set_buffer_time_near(_interface, hardwareParameters, &bufferTime, 0) < 0
|
|
) return terminate(), false;
|
|
|
|
if(snd_pcm_hw_params(_interface, hardwareParameters) < 0) return terminate(), false;
|
|
if(snd_pcm_get_params(_interface, &_bufferSize, &_periodSize) < 0) return terminate(), false;
|
|
|
|
snd_pcm_sw_params_t* softwareParameters;
|
|
snd_pcm_sw_params_alloca(&softwareParameters);
|
|
if(snd_pcm_sw_params_current(_interface, softwareParameters) < 0) return terminate(), false;
|
|
if(snd_pcm_sw_params_set_start_threshold(_interface, softwareParameters,
|
|
(_bufferSize / _periodSize) * _periodSize) < 0
|
|
) return terminate(), false;
|
|
if(snd_pcm_sw_params(_interface, softwareParameters) < 0) return terminate(), false;
|
|
|
|
_buffer = new uint32_t[_periodSize]();
|
|
_offset = 0;
|
|
return _ready = true;
|
|
}
|
|
|
|
auto terminate() -> void {
|
|
_ready = false;
|
|
|
|
if(_interface) {
|
|
//snd_pcm_drain(_interface); //prevents popping noise; but causes multi-second lag
|
|
snd_pcm_close(_interface);
|
|
_interface = nullptr;
|
|
}
|
|
|
|
if(_buffer) {
|
|
delete[] _buffer;
|
|
_buffer = nullptr;
|
|
}
|
|
}
|
|
|
|
auto queryDevices() -> string_vector {
|
|
string_vector devices;
|
|
|
|
const char** list;
|
|
if(snd_device_name_hint(-1, "pcm", (void***)&list) == 0) {
|
|
uint index = 0;
|
|
while(list[index]) {
|
|
const char* deviceName = snd_device_name_get_hint(list[index], "NAME");
|
|
if(deviceName) devices.append(deviceName);
|
|
free(deviceName);
|
|
index++;
|
|
}
|
|
}
|
|
|
|
snd_device_name_free_hint((void**)list);
|
|
return devices;
|
|
}
|
|
|
|
bool _ready = false;
|
|
string _device;
|
|
bool _blocking = true;
|
|
double _frequency = 48000.0;
|
|
uint _latency = 40;
|
|
|
|
snd_pcm_t* _interface = nullptr;
|
|
snd_pcm_uframes_t _bufferSize;
|
|
snd_pcm_uframes_t _periodSize;
|
|
|
|
uint32_t* _buffer = nullptr;
|
|
uint _offset = 0;
|
|
};
|