bsnes/higan/emulator/random.hpp

97 lines
2.2 KiB
C++
Raw Normal View History

#pragma once
namespace Emulator {
struct Random {
enum class Entropy : uint { None, Low, High };
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
auto operator()() -> uint64 {
return random();
}
auto entropy(Entropy entropy) -> void {
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
_entropy = entropy;
seed();
}
auto seed(maybe<uint32> seed = nothing, maybe<uint32> sequence = nothing) -> void {
if(!seed) seed = (uint32)clock();
if(!sequence) sequence = 0;
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
_state = 0;
_increment = sequence() << 1 | 1;
step();
_state += seed();
step();
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
auto random() -> uint64 {
if(_entropy == Entropy::None) return 0;
return (uint64)step() << 32 | (uint64)step() << 0;
}
auto bias(uint64 bias) -> uint64 {
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
if(_entropy == Entropy::None) return bias;
return random();
}
auto bound(uint64 bound) -> uint64 {
uint64 threshold = -bound % bound;
while(true) {
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
uint64 result = random();
if(result >= threshold) return result % bound;
}
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
auto array(uint8* data, uint32 size) -> void {
if(_entropy == Entropy::None) {
memory::fill(data, size);
return;
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
if(_entropy == Entropy::High) {
for(uint32 address : range(size)) {
data[address] = random();
}
return;
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
//Entropy::Low
uint lobit = random() & 3;
uint hibit = (lobit + 8 + (random() & 3)) & 15;
uint lovalue = random() & 255;
uint hivalue = random() & 255;
if((random() & 3) == 0) lovalue = 0;
if((random() & 1) == 0) hivalue = ~lovalue;
for(uint32 address : range(size)) {
uint8 value = address.bit(lobit) ? lovalue : hivalue;
if(address.bit(hibit)) value = ~value;
if((random() & 511) == 0) value.bit(random() & 7) ^= 1;
if((random() & 2047) == 0) value.bit(random() & 7) ^= 1;
data[address] = value;
}
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
auto serialize(serializer& s) -> void {
s.integer((uint&)_entropy);
s.integer(_state);
s.integer(_increment);
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
private:
auto step() -> uint32 {
uint64 state = _state;
_state = state * 6364136223846793005ull + _increment;
uint32 xorshift = (state >> 18 ^ state) >> 27;
uint32 rotate = state >> 59;
return xorshift >> rotate | xorshift << (-rotate & 31);
}
Update to v104r05 release. byuu says: Changelog: - emulator/random: new array function with more realistic RAM initializations - emulator/random: both low and high entropy register initializations now use PCG - gba/player: rumble will time out and disable after being left on for 500ms; fixes Pokemon Pinball issue - ruby/input/udev: fixed rumble effects [ma\_rysia] - sfc/system: default to low-entropy randomization of memory The low-entropy memory randomization is modeled after one of my SHVC 2/1/3 systems. It generates striped patterns in memory, using random inputs (biased to 0x00/0xff), and has a random chance of corrupting 1-2 bits of random values in the pool of memory (to prevent easy emulator detection and to match observed results on hardware.) The reasoning for using PCG on register initializations, is that I don't believe they're going to have repeating patterns like RAM does anyway. And register initializations are way more vital. I want to have the new low-entropy RAM mode tested, so at least for the next few WIPs, I've set the SNES randomization over to low-entropy. We'll have to have a long discussion and decide whether we want official releases to use high-entropy or low-entropy. Also, I figured out the cause of the Prince of Persia distortion ... I had the volume under the audio settings tab set to 200%. I didn't realize there were SNES games that clipped so easily, given how incredibly weak SNES audio is compared to every other sound source on my PC. So with no entropy or low-entropy, indeed the game now sounds just fine. I can't actually test the udev fixes, so I guess we'll see how that goes for Screwtape and ma\_rysia.
2017-08-24 14:24:34 +00:00
Entropy _entropy = Entropy::High;
uint64 _state;
uint64 _increment;
};
}