[SNSF] Replaced most uses of fill/copy with fill_n/copy_n, and a few other minor code cleanups.
[SNSF] Replaced most uses of fill/copy with fill_n/copy_n, and a few other minor code cleanups.

--- a/src/in_snsf/XSFPlayer_SNSF.cpp
+++ b/src/in_snsf/XSFPlayer_SNSF.cpp
@@ -93,7 +93,7 @@
 			return;
 		if (bytes > bleft)
 			bytes = bleft;
-		std::fill(&this->buf[this->fil], &this->buf[this->fil + bytes], 0);
+		std::fill_n(&this->buf[this->fil], bytes, 0);
 		S9xMixSamples(&this->buf[this->fil], bytes >> 1);
 		this->fil += bytes;
 	}
@@ -122,7 +122,7 @@
 		data.resize(finalSize, 0);
 	else if (data.size() < size + offset)
 		data.resize(offset + finalSize);
-	std::copy(&section[8], &section[8 + size], &data[offset]);
+	std::copy_n(&section[8], size, &data[offset]);
 }
 
 static bool Map2SF(XSFFile *xSF, int level)
@@ -148,7 +148,7 @@
 				if (size > 4 && loaderwork.sram.size() > offset)
 				{
 					auto len = std::min(size - 4, loaderwork.sram.size() - offset);
-					std::copy(&reservedSection[reservedPosition + 12], &reservedSection[reservedPosition + 12 + len], &loaderwork.sram[offset]);
+					std::copy_n(&reservedSection[reservedPosition + 12], len, &loaderwork.sram[offset]);
 				}
 			}
 			reservedPosition += size + 8;
@@ -276,7 +276,7 @@
 		unsigned len = remain;
 		if (len > bytes)
 			len = bytes;
-		std::copy(&buffer.buf[buffer.cur], &buffer.buf[buffer.cur + len], &buf[offset]);
+		std::copy_n(&buffer.buf[buffer.cur], len, &buf[offset]);
 		bytes -= len;
 		offset += len;
 		buffer.cur += len;

--- a/src/in_snsf/snes9x/apu/SNES_SPC.cpp
+++ b/src/in_snsf/snes9x/apu/SNES_SPC.cpp
@@ -63,9 +63,9 @@
 	{
 		this->m.rom_enabled = this->dsp.rom_enabled = enable;
 		if (enable)
-			std::copy(&this->m.ram.ram[rom_addr], &this->m.ram.ram[rom_addr + rom_size], &this->m.hi_ram[0]);
+			std::copy_n(&this->m.ram.ram[rom_addr], static_cast<int>(rom_size), &this->m.hi_ram[0]);
 		auto data = enable ? &this->m.rom[0] : &this->m.hi_ram[0];
-		std::copy(&data[0], &data[rom_size], &this->m.ram.ram[rom_addr]);
+		std::copy_n(&data[0], static_cast<int>(rom_size), &this->m.ram.ram[rom_addr]);
 		// TODO: ROM can still get overwritten when DSP writes to echo buffer
 	}
 }
@@ -173,10 +173,7 @@
 					t = this->run_timer(t, time);
 					t->enabled = enabled;
 					if (enabled)
-					{
-						t->divider = 0;
-						t->counter = 0;
-					}
+						t->divider = t->counter = 0;
 				}
 			}
 			this->enable_rom(!!(data & 0x80));
@@ -296,28 +293,6 @@
 
 //// Run
 
-// Prefix and suffix for CPU emulator function
-#define SPC_CPU_RUN_FUNC \
-uint8_t *SNES_SPC::run_until_(time_t end_time) \
-{ \
-	rel_time_t rel_time = this->m.spc_time - end_time; \
-	/*assert( rel_time <= 0 );*/ \
-	this->m.spc_time = end_time; \
-	this->m.dsp_time += rel_time; \
-	this->m.timers[0].next_time += rel_time; \
-	this->m.timers[1].next_time += rel_time; \
-	this->m.timers[2].next_time += rel_time;
-
-#define SPC_CPU_RUN_FUNC_END \
-	this->m.spc_time += rel_time; \
-	this->m.dsp_time -= rel_time; \
-	this->m.timers[0].next_time -= rel_time; \
-	this->m.timers[1].next_time -= rel_time; \
-	this->m.timers[2].next_time -= rel_time; \
-	/*assert( m.spc_time >= end_time );*/ \
-	return &this->m.smp_regs[0][r_cpuio0]; \
-}
-
 static const int cpu_lag_max = 12 - 1; // DIV YA,X takes 12 clocks
 
 void SNES_SPC::end_frame(time_t end_time)

--- a/src/in_snsf/snes9x/apu/SNES_SPC_misc.cpp
+++ b/src/in_snsf/snes9x/apu/SNES_SPC_misc.cpp
@@ -70,7 +70,7 @@
 
 void SNES_SPC::init_rom(const uint8_t in[rom_size])
 {
-	std::copy(&in[0], &in[rom_size], &this->m.rom[0]);
+	std::copy_n(&in[0], static_cast<int>(rom_size), &this->m.rom[0]);
 }
 
 void SNES_SPC::set_tempo(int t)
@@ -100,7 +100,7 @@
 // reset timer prescalers or dividers.
 void SNES_SPC::timers_loaded()
 {
-	for (int  i = 0; i < timer_count; ++i)
+	for (int i = 0; i < timer_count; ++i)
 	{
 		auto &t = this->m.timers[i];
 		t.period = IF_0_THEN_256(this->m.smp_regs[0][r_t0target + i]);
@@ -114,8 +114,8 @@
 // Loads registers from unified 16-byte format
 void SNES_SPC::load_regs(const uint8_t in[reg_count])
 {
-	std::copy(&in[0], &in[reg_count], &this->m.smp_regs[0][0]);
-	std::copy(&this->m.smp_regs[0][0], &this->m.smp_regs[0][reg_count], &this->m.smp_regs[1][0]);
+	std::copy_n(&in[0], static_cast<int>(reg_count), &this->m.smp_regs[0][0]);
+	std::copy_n(&this->m.smp_regs[0][0], static_cast<int>(reg_count), &this->m.smp_regs[1][0]);
 
 	// These always read back as 0
 	this->m.smp_regs[1][r_test] = this->m.smp_regs[1][r_control] = this->m.smp_regs[1][r_t0target] = this->m.smp_regs[1][r_t1target] = this->m.smp_regs[1][r_t2target] = 0;
@@ -145,12 +145,11 @@
 	this->m.spc_time = 0;
 	this->m.dsp_time = 0;
 
-	for (int i = 0; i < timer_count; ++i)
-	{
-		auto &t = this->m.timers[i];
+	std::for_each(&this->m.timers[0], &this->m.timers[timer_count], [](Timer &t)
+	{
 		t.next_time = 1;
 		t.divider = 0;
-	}
+	});
 
 	this->regs_loaded();
 
@@ -160,8 +159,7 @@
 
 void SNES_SPC::reset_common(int timer_counter_init)
 {
-	for (int i = 0; i < timer_count; ++i)
-		this->m.smp_regs[1][r_t0out + i] = timer_counter_init;
+	std::fill_n(&this->m.smp_regs[1][r_t0out], static_cast<int>(timer_count), timer_counter_init);
 
 	// Run IPL ROM
 	memset(&this->m.cpu_regs, 0, sizeof(this->m.cpu_regs));
@@ -169,7 +167,7 @@
 
 	this->m.smp_regs[0][r_test] = 0x0A;
 	this->m.smp_regs[0][r_control] = 0xB0; // ROM enabled, clear ports
-	std::fill(&this->m.smp_regs[1][r_cpuio0], &this->m.smp_regs[1][r_cpuio0 + port_count], 0);
+	std::fill_n(&this->m.smp_regs[1][r_cpuio0], static_cast<int>(port_count), 0);
 
 	this->reset_time_regs();
 }
@@ -186,7 +184,7 @@
 	this->m.cpu_regs.a = this->m.cpu_regs.x = this->m.cpu_regs.y = 0x00;
 	this->m.cpu_regs.psw = 0x02;
 	this->m.cpu_regs.sp = 0xEF;
-	std::fill(&this->m.ram.ram[0], &this->m.ram.ram[0x10000], 0);
+	std::fill_n(&this->m.ram.ram[0], 0x10000, 0);
 	this->ram_loaded();
 	this->reset_common(0x0F);
 	this->dsp.reset();

--- a/src/in_snsf/snes9x/apu/SPC_CPU.h
+++ b/src/in_snsf/snes9x/apu/SPC_CPU.h
@@ -38,8 +38,16 @@
 
 const int nz_neg_mask = 0x880; // either bit set indicates N flag set
 
-SPC_CPU_RUN_FUNC
+uint8_t *SNES_SPC::run_until_(time_t end_time)
 {
+	rel_time_t rel_time = this->m.spc_time - end_time;
+	/*assert( rel_time <= 0 );*/
+	this->m.spc_time = end_time;
+	this->m.dsp_time += rel_time;
+	this->m.timers[0].next_time += rel_time;
+	this->m.timers[1].next_time += rel_time;
+	this->m.timers[2].next_time += rel_time;
+
 	auto ram = this->m.ram.ram;
 	int a = this->m.cpu_regs.a;
 	int x = this->m.cpu_regs.x;
@@ -155,7 +163,6 @@
 inc_pc_loop:
 	++pc;
 loop:
-{
 	unsigned data;
 
 	unsigned opcode = *pc;
@@ -167,20 +174,6 @@
 #ifdef SPC_CPU_OPCODE_HOOK
 	SPC_CPU_OPCODE_HOOK(GET_PC(), opcode);
 #endif
-	/*
-	//SUB_CASE_COUNTER( 1 );
-	#define PROFILE_TIMER_LOOP( op, addr, len )\
-	if ( opcode == op )\
-	{\
-		int cond = (unsigned) ((addr) - 0xFD) < 3 &&\
-				pc [len] == 0xF0 && pc [len+1] == 0xFE - len;\
-		SUB_CASE_COUNTER( op && cond );\
-	}
-	
-	PROFILE_TIMER_LOOP( 0xEC, get_le16( pc + 1 ), 3 );
-	PROFILE_TIMER_LOOP( 0xEB, pc [1], 2 );
-	PROFILE_TIMER_LOOP( 0xE4, pc [1], 2 );
-	*/
 
 	// TODO: if PC is at end of memory, this will get wrong operand (very obscure)
 	data = *++pc;
@@ -268,7 +261,7 @@
 			++pc;
 			{
 				int i = dp + data;
-				ram [i] = static_cast<uint8_t>(a);
+				ram[i] = static_cast<uint8_t>(a);
 				i -= 0xF0;
 				if (static_cast<unsigned>(i) < 0x10) // 39%
 				{
@@ -411,23 +404,19 @@
 		// 3. 8-BIT DATA TRANSMISSIN COMMANDS, GROUP 3.
 
 		case 0x7D: // MOV A,X
-			a  = x;
-			nz = x;
+			a = nz = x;
 			goto loop;
 
 		case 0xDD: // MOV A,Y
-			a  = y;
-			nz = y;
+			a = nz = y;
 			goto loop;
 
 		case 0x5D: // MOV X,A
-			x  = a;
-			nz = a;
+			x = nz = a;
 			goto loop;
 
 		case 0xFD: // MOV Y,A
-			y  = a;
-			nz = a;
+			y = nz = a;
 			goto loop;
 
 		case 0x9D: // MOV X,SP
@@ -1157,7 +1146,7 @@
 	} // switch
 
 	assert(0); // catch any unhandled instructions
-}
+
 out_of_time:
 	rel_time -= this->m.cycle_table[*pc]; // undo partial execution of opcode
 stop:
@@ -1168,11 +1157,16 @@
 	m.cpu_regs.a = static_cast<uint8_t>(a);
 	m.cpu_regs.x = static_cast<uint8_t>(x);
 	m.cpu_regs.y = static_cast<uint8_t>(y);
-	{
-		int temp;
-		GET_PSW(temp);
-		m.cpu_regs.psw = static_cast<uint8_t>(temp);
-	}
+	int temp;
+	GET_PSW(temp);
+	m.cpu_regs.psw = static_cast<uint8_t>(temp);
+
+	this->m.spc_time += rel_time;
+	this->m.dsp_time -= rel_time;
+	this->m.timers[0].next_time -= rel_time;
+	this->m.timers[1].next_time -= rel_time;
+	this->m.timers[2].next_time -= rel_time;
+	/*assert( m.spc_time >= end_time );*/
+	return &this->m.smp_regs[0][r_cpuio0];
 }
-SPC_CPU_RUN_FUNC_END
-
+

--- a/src/in_snsf/snes9x/apu/SPC_DSP.cpp
+++ b/src/in_snsf/snes9x/apu/SPC_DSP.cpp
@@ -317,7 +317,8 @@
 }
 void SPC_DSP::misc_29()
 {
-	if ((this->m.every_other_sample = !this->m.every_other_sample))
+	this->m.every_other_sample = !this->m.every_other_sample;
+	if (this->m.every_other_sample)
 		this->m.new_kon &= ~this->m.kon; // clears KON 63 clocks after it was last read
 }
 void SPC_DSP::misc_30()
@@ -697,38 +698,38 @@
 
 // Voice      0      1      2      3      4      5      6      7
 #define GEN_DSP_TIMING \
-PHASE( 0)  V(V5,0)V(V2,1)\
-PHASE( 1)  V(V6,0)V(V3,1)\
-PHASE( 2)  V(V7_V4_V1,0)\
-PHASE( 3)  V(V8_V5_V2,0)\
-PHASE( 4)  V(V9_V6_V3,0)\
-PHASE( 5)         V(V7_V4_V1,1)\
-PHASE( 6)         V(V8_V5_V2,1)\
-PHASE( 7)         V(V9_V6_V3,1)\
-PHASE( 8)                V(V7_V4_V1,2)\
-PHASE( 9)                V(V8_V5_V2,2)\
-PHASE(10)                V(V9_V6_V3,2)\
-PHASE(11)                       V(V7_V4_V1,3)\
-PHASE(12)                       V(V8_V5_V2,3)\
-PHASE(13)                       V(V9_V6_V3,3)\
-PHASE(14)                              V(V7_V4_V1,4)\
-PHASE(15)                              V(V8_V5_V2,4)\
-PHASE(16)                              V(V9_V6_V3,4)\
-PHASE(17)  V(V1,0)                            V(V7,5)V(V4,6)\
-PHASE(18)                                     V(V8_V5_V2,5)\
-PHASE(19)                                     V(V9_V6_V3,5)\
-PHASE(20)         V(V1,1)                            V(V7,6)V(V4,7)\
-PHASE(21)                                            V(V8,6)V(V5,7)  V(V2,0)  /* t_brr_next_addr order dependency */\
-PHASE(22)  V(V3a,0)                                  V(V9,6)V(V6,7)  echo_22();\
-PHASE(23)                                                   V(V7,7)  echo_23();\
-PHASE(24)                                                   V(V8,7)  echo_24();\
-PHASE(25)  V(V3b,0)                                         V(V9,7)  echo_25();\
-PHASE(26)                                                            echo_26();\
-PHASE(27) misc_27();                                                 echo_27();\
-PHASE(28) misc_28();                                                 echo_28();\
-PHASE(29) misc_29();                                                 echo_29();\
-PHASE(30) misc_30();V(V3c,0)                                         echo_30();\
-PHASE(31)  V(V4,0)       V(V1,2)\
+PHASE(0) V(V5, 0) V(V2, 1) \
+PHASE(1) V(V6, 0) V(V3, 1) \
+PHASE(2) V(V7_V4_V1, 0) \
+PHASE(3) V(V8_V5_V2, 0) \
+PHASE(4) V(V9_V6_V3, 0) \
+PHASE(5) V(V7_V4_V1, 1) \
+PHASE(6) V(V8_V5_V2, 1) \
+PHASE(7) V(V9_V6_V3, 1) \
+PHASE(8) V(V7_V4_V1, 2) \
+PHASE(9) V(V8_V5_V2, 2) \
+PHASE(10) V(V9_V6_V3, 2) \
+PHASE(11) V(V7_V4_V1, 3) \
+PHASE(12) V(V8_V5_V2, 3) \
+PHASE(13) V(V9_V6_V3, 3) \
+PHASE(14) V(V7_V4_V1, 4) \
+PHASE(15) V(V8_V5_V2, 4) \
+PHASE(16) V(V9_V6_V3, 4) \
+PHASE(17) V(V1, 0) V(V7, 5) V(V4, 6) \
+PHASE(18) V(V8_V5_V2, 5) \
+PHASE(19) V(V9_V6_V3, 5) \
+PHASE(20) V(V1, 1) V(V7, 6) V(V4, 7) \
+PHASE(21) V(V8, 6) V(V5, 7) V(V2, 0) /* t_brr_next_addr order dependency */ \
+PHASE(22) V(V3a, 0) V(V9, 6) V(V6, 7) echo_22(); \
+PHASE(23) V(V7, 7) echo_23(); \
+PHASE(24) V(V8, 7) echo_24(); \
+PHASE(25) V(V3b, 0) V(V9, 7) echo_25(); \
+PHASE(26) echo_26(); \
+PHASE(27) misc_27(); echo_27(); \
+PHASE(28) misc_28(); echo_28(); \
+PHASE(29) misc_29(); echo_29(); \
+PHASE(30) misc_30(); V(V3c, 0) echo_30(); \
+PHASE(31) V(V4, 0) V(V1, 2)
 
 #if !SPC_DSP_CUSTOM_RUN
 void SPC_DSP::run(int clocks_remain)
@@ -748,7 +749,6 @@
 			goto loop;
 	}
 }
-
 #endif
 
 //// Setup
@@ -807,7 +807,7 @@
 
 void SPC_DSP::load(const uint8_t regs[register_count])
 {
-	std::copy(&regs[0], &regs[register_count], &this->m.regs[0]);
+	std::copy_n(&regs[0], static_cast<int>(register_count), &this->m.regs[0]);
 	memset(&this->m.regs[register_count], 0, offsetof(state_t, ram) - register_count);
 
 	// Internal state

--- a/src/in_snsf/snes9x/apu/apu.cpp
+++ b/src/in_snsf/snes9x/apu/apu.cpp
@@ -235,11 +235,10 @@
 
 static void EightBitize(uint8_t *buffer, int sample_count)
 {
-	uint8_t *buf8 = buffer;
 	int16_t *buf16 = reinterpret_cast<int16_t *>(buffer);
 
 	for (int i = 0; i < sample_count; ++i)
-		buf8[i] = static_cast<uint8_t>((buf16[i] / 256) + 128);
+		buffer[i] = static_cast<uint8_t>((buf16[i] / 256) + 128);
 }
 
 static void DeStereo(uint8_t *buffer, int sample_count)
@@ -287,7 +286,7 @@
 
 	if (Settings.Mute)
 	{
-		std::fill(&dest[0], &dest[sample_count << 1], 0);
+		std::fill_n(&dest[0], sample_count << 1, 0);
 		spc::resampler->clear();
 
 		return false;
@@ -302,7 +301,7 @@
 		}
 		else
 		{
-			std::fill(&buffer[0], &buffer[(sample_count << (Settings.SixteenBitSound ? 1 : 0)) >> (Settings.Stereo ? 0 : 1)], Settings.SixteenBitSound ? 0 : 128);
+			std::fill_n(&buffer[0], (sample_count << (Settings.SixteenBitSound ? 1 : 0)) >> (Settings.Stereo ? 0 : 1), Settings.SixteenBitSound ? 0 : 128);
 			if (!spc::lag)
 				spc::lag = spc::lag_master;
 
@@ -324,7 +323,7 @@
 		if (!Settings.SixteenBitSound)
 			EightBitize(dest, sample_count);
 
-		std::copy(&dest[0], &dest[sample_count << (Settings.SixteenBitSound ? 1 : 0)], &buffer[0]);
+		std::copy_n(&dest[0], sample_count << (Settings.SixteenBitSound ? 1 : 0), &buffer[0]);
 	}
 
 	return true;
@@ -349,12 +348,7 @@
 		}
 	}
 
-	if (!Settings.SoundSync || Settings.TurboMode || Settings.Mute)
-		spc::sound_in_sync = true;
-	else if (spc::resampler->space_empty() >= spc::resampler->space_filled())
-		spc::sound_in_sync = true;
-	else
-		spc::sound_in_sync = false;
+	spc::sound_in_sync = !Settings.SoundSync || Settings.TurboMode || Settings.Mute || spc::resampler->space_empty() >= spc::resampler->space_filled();
 
 	spc_core->set_output(reinterpret_cast<SNES_SPC::sample_t *>(spc::landing_buffer.get()), spc::buffer_size >> 1);
 }

--- a/src/in_snsf/snes9x/apu/ring_buffer.h
+++ b/src/in_snsf/snes9x/apu/ring_buffer.h
@@ -19,9 +19,7 @@
 	{
 		this->buffer_size = buffer_size;
 		this->buffer = new unsigned char[this->buffer_size];
-		std::fill(&this->buffer[0], &this->buffer[this->buffer_size], 0);
-
-		this->size = this->start = 0;
+		this->clear();
 	}
 
 	~ring_buffer()
@@ -37,7 +35,7 @@
 		int end = (this->start + this->size) % this->buffer_size;
 		int first_write_size = std::min(bytes, this->buffer_size - end);
 
-		std::copy(&src[0], &src[first_write_size], &this->buffer[end]);
+		std::copy_n(&src[0], first_write_size, &this->buffer[end]);
 
 		if (bytes > first_write_size)
 			std::copy(&src[first_write_size], &src[bytes], &this->buffer[0]);
@@ -60,7 +58,7 @@
 	void clear()
 	{
 		this->start = this->size = 0;
-		std::fill(&this->buffer[0], &this->buffer[this->buffer_size], 0);
+		std::fill_n(&this->buffer[0], this->buffer_size, 0);
 	}
 
 	void resize(int size)
@@ -68,9 +66,7 @@
 		delete[] this->buffer;
 		this->buffer_size = size;
 		this->buffer = new unsigned char[this->buffer_size];
-		std::fill(&this->buffer[0], &this->buffer[this->buffer_size], 0);
-
-		this->size = this->start = 0;
+		this->clear();
 	}
 };
 

--- a/src/in_snsf/snes9x/apu/sinc_resampler.h
+++ b/src/in_snsf/snes9x/apu/sinc_resampler.h
@@ -81,8 +81,8 @@
 	{
 		ring_buffer::clear();
 		this->r_frac = 1.0;
-		std::fill(&this->r_left[0], &this->r_left[SINC_WIDTH * 2], 0);
-		std::fill(&this->r_right[0], &this->r_right[SINC_WIDTH * 2], 0);
+		std::fill_n(&this->r_left[0], SINC_WIDTH * 2, 0);
+		std::fill_n(&this->r_right[0], SINC_WIDTH * 2, 0);
 	}
 
 	void read(short *data, int num_samples)
@@ -125,10 +125,10 @@
 
 			if (this->r_frac > 1.0)
 			{
-				std::copy(&this->r_left[1], &this->r_left[SINC_WIDTH * 2], &this->r_left[0]);
+				std::copy_n(&this->r_left[1], SINC_WIDTH * 2 - 1, &this->r_left[0]);
 				this->r_left[SINC_WIDTH * 2 - 1] = s_left;
 
-				std::copy(&this->r_right[1], &this->r_right[SINC_WIDTH * 2], &this->r_right[0]);
+				std::copy_n(&this->r_right[1], SINC_WIDTH * 2 - 1, &this->r_right[0]);
 				this->r_right[SINC_WIDTH * 2 - 1] = s_right;
 
 				this->r_frac -= 1.0;

--- a/src/in_snsf/snes9x/cpu.cpp
+++ b/src/in_snsf/snes9x/cpu.cpp
@@ -218,10 +218,7 @@
 	Timings.H_Max = Timings.H_Max_Master;
 	Timings.V_Max = Timings.V_Max_Master;
 	Timings.NMITriggerPos = 0xffff;
-	if (Model->_5A22 == 2)
-		Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v2;
-	else
-		Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v1;
+	Timings.WRAMRefreshPos = Model->_5A22 == 2 ? SNES_WRAM_REFRESH_HC_v2 : SNES_WRAM_REFRESH_HC_v1;
 
 	S9xSetPCBase(Registers.PC.xPBPC);
 
@@ -242,9 +239,9 @@
 
 void S9xReset()
 {
-	std::fill(&Memory.RAM[0], &Memory.RAM[0x20000], 0x55);
-	std::fill(&Memory.VRAM[0], &Memory.VRAM[0x10000], 0);
-	std::fill(&Memory.FillRAM[0], &Memory.FillRAM[0x8000], 0);
+	std::fill_n(&Memory.RAM[0], 0x20000, 0x55);
+	std::fill_n(&Memory.VRAM[0], 0x10000, 0);
+	std::fill_n(&Memory.FillRAM[0], 0x8000, 0);
 
 	S9xResetCPU();
 	S9xResetPPU();

--- a/src/in_snsf/snes9x/dma.cpp
+++ b/src/in_snsf/snes9x/dma.cpp
@@ -209,8 +209,7 @@
 
 bool S9xDoDMA(uint8_t Channel)
 {
-	CPU.InDMA = true;
-	CPU.InDMAorHDMA = true;
+	CPU.InDMA = CPU.InDMAorHDMA = true;
 	CPU.CurrentDMAorHDMAChannel = Channel;
 
 	SDMA *d = &DMA[Channel];
@@ -325,9 +324,7 @@
 			p += inc;
 			if (!addCyclesInDMA(Channel))
 			{
-				CPU.InDMA = false;
-				CPU.InDMAorHDMA = false;
-				CPU.InWRAMDMAorHDMA = false;
+				CPU.InDMA = CPU.InDMAorHDMA = CPU.InWRAMDMAorHDMA = false;
 				CPU.CurrentDMAorHDMAChannel = -1;
 				return false;
 			}
@@ -836,9 +833,7 @@
 			d->AAddress += inc;
 			if (!addCyclesInDMA(Channel))
 			{
-				CPU.InDMA = false;
-				CPU.InDMAorHDMA = false;
-				CPU.InWRAMDMAorHDMA = false;
+				CPU.InDMA = CPU.InDMAorHDMA = CPU.InWRAMDMAorHDMA = false;
 				CPU.CurrentDMAorHDMAChannel = -1;
 				return false;
 			}
@@ -1148,8 +1143,7 @@
 
 	PPU.HDMAEnded = 0;
 
-	CPU.InHDMA = true;
-	CPU.InDMAorHDMA = true;
+	CPU.InHDMA = CPU.InDMAorHDMA = true;
 	int32_t tmpch = CPU.CurrentDMAorHDMAChannel;
 
 	// XXX: Not quite right...
@@ -1186,8 +1180,7 @@
 
 	int d = 0;
 
-	CPU.InHDMA = true;
-	CPU.InDMAorHDMA = true;
+	CPU.InHDMA = CPU.InDMAorHDMA = true;
 	CPU.HDMARanInDMA = CPU.InDMA ? byte : 0;
 	bool temp = CPU.InWRAMDMAorHDMA;
 	int32_t tmpch = CPU.CurrentDMAorHDMAChannel;

--- a/src/in_snsf/snes9x/globals.cpp
+++ b/src/in_snsf/snes9x/globals.cpp
@@ -178,7 +178,6 @@
 #include "snes9x.h"
 #include "memmap.h"
 #include "dma.h"
-#include "apu/apu.h"
 
 SCPUState CPU;
 SICPU ICPU;

--- a/src/in_snsf/snes9x/memmap.cpp
+++ b/src/in_snsf/snes9x/memmap.cpp
@@ -270,10 +270,10 @@
 		return false;
 	}
 
-	std::fill(&this->RAM[0], &this->RAM[0x20000], 0);
-	std::fill(&this->SRAM[0], &this->SRAM[0x20000], 0);
-	std::fill(&this->VRAM[0], &this->VRAM[0x10000], 0);
-	std::fill(&this->RealROM[0], &this->RealROM[MAX_ROM_SIZE + 0x200 + 0x8000], 0);
+	std::fill_n(&this->RAM[0], 0x20000, 0);
+	std::fill_n(&this->SRAM[0], 0x20000, 0);
+	std::fill_n(&this->VRAM[0], 0x10000, 0);
+	std::fill_n(&this->RealROM[0], MAX_ROM_SIZE + 0x200 + 0x8000, 0);
 
 	// FillRAM uses first 32K of ROM image area, otherwise space just
 	// wasted. Might be read by the SuperFX code.
@@ -409,7 +409,7 @@
 {
 	int retry_count = 0;
 
-	std::fill(&this->ROM[0], &this->ROM[MAX_ROM_SIZE], 0);
+	std::fill_n(&this->ROM[0], static_cast<int>(MAX_ROM_SIZE), 0);
 
 again:
 	this->CalculatedSize = 0;
@@ -418,18 +418,17 @@
 	int32_t totalFileSize = std::min<int32_t>(MAX_ROM_SIZE, lromsize);
 	if (!totalFileSize)
 		return false;
-	std::copy(&lrombuf[0], &lrombuf[totalFileSize], &this->ROM[0]);
-	SNESGameFixes.SRAMInitialValue = 0xff;
-	std::fill(&this->SRAM[0], &this->SRAM[0x20000], SNESGameFixes.SRAMInitialValue);
+	std::copy_n(&lrombuf[0], totalFileSize, &this->ROM[0]);
+	std::fill_n(&this->SRAM[0], 0x20000, 0xff);
 	if (srambuf && sramsize)
-		std::copy(&srambuf[0], &srambuf[sramsize], &this->SRAM[0]);
+		std::copy_n(&srambuf[0], sramsize, &this->SRAM[0]);
 
 	int hi_score = this->ScoreHiROM(false);
 	int lo_score = this->ScoreLoROM(false);
 
 	if (((hi_score > lo_score && this->ScoreHiROM(true) > hi_score) || (hi_score <= lo_score && this->ScoreLoROM(true) > lo_score)))
 	{
-		memmove(&this->ROM[0], &this->ROM[512], totalFileSize - 512);
+		std::copy_n(&this->ROM[512], totalFileSize - 512, &this->ROM[0]);
 		totalFileSize -= 512;
 		// modifying ROM, so we need to rescore
 		hi_score = this->ScoreHiROM(false);
@@ -486,7 +485,6 @@
 
 		// ignore map type byte if not 0x2x or 0x3x
 		if ((RomHeader[0x7fd5] & 0xf0) == 0x20 || (RomHeader[0x7fd5] & 0xf0) == 0x30)
-		{
 			switch (RomHeader[0x7fd5] & 0xf)
 			{
 				case 1:
@@ -494,11 +492,8 @@
 					break;
 
 				case 5:
-					interleaved = true;
-					tales = true;
-					break;
+					interleaved = tales = true;
 			}
-		}
 	}
 	else
 	{
@@ -506,24 +501,19 @@
 		this->HiROM = true;
 
 		if ((RomHeader[0xffd5] & 0xf0) == 0x20 || (RomHeader[0xffd5] & 0xf0) == 0x30)
-		{
 			switch (RomHeader[0xffd5] & 0xf)
 			{
 				case 0:
 				case 3:
 					interleaved = true;
-					break;
 			}
-		}
 	}
 
 	// this two games fail to be detected
 	if (!strncmp(reinterpret_cast<char *>(&this->ROM[0x7fc0]), "YUYU NO QUIZ DE GO!GO!", 22) || !strncmp(reinterpret_cast<char *>(&this->ROM[0xffc0]), "BATMAN--REVENGE JOKER",  21))
 	{
 		this->LoROM = true;
-		this->HiROM = false;
-		interleaved = false;
-		tales = false;
+		this->HiROM = interleaved = tales = false;
 	}
 
 	if (!Settings.ForceNotInterleaved && interleaved)
@@ -567,13 +557,12 @@
 	if (tales)
 	{
 		auto tmp = std::vector<uint8_t>(this->CalculatedSize - 0x400000);
-		memmove(&tmp[0], &this->ROM[0], this->CalculatedSize - 0x400000);
-		memmove(&this->ROM[0], &this->ROM[this->CalculatedSize - 0x400000], 0x400000);
-		memmove(&this->ROM[0x400000], &tmp[0], this->CalculatedSize - 0x400000);
+		std::copy_n(&this->ROM[0], this->CalculatedSize - 0x400000, &tmp[0]);
+		std::copy_n(&this->ROM[this->CalculatedSize - 0x400000], 0x400000, &this->ROM[0]);
+		std::copy_n(&tmp[0], this->CalculatedSize - 0x400000, &this->ROM[0x400000]);
 	}
 
 	memset(&SNESGameFixes, 0, sizeof(SNESGameFixes));
-	SNESGameFixes.SRAMInitialValue = 0x60;
 
 	this->InitROM();
 
@@ -626,14 +615,14 @@
 	this->ROMType = RomHeader[0x26];
 	this->ROMRegion = RomHeader[0x29];
 
-	std::copy(&RomHeader[0x02], &RomHeader[0x06], &this->ROMId[0]);
+	std::copy_n(&RomHeader[0x02], 4, &this->ROMId[0]);
 }
 
 void CMemory::InitROM()
 {
 	//// Parse ROM header and read ROM informatoin
 
-	std::fill(&this->ROMId[0], &this->ROMId[5], 0);
+	std::fill_n(&this->ROMId[0], 5, 0);
 
 	uint8_t *RomHeader = &this->ROM[0x7FB0];
 	if (this->ExtendedFormat == BIGFIRST)
@@ -817,7 +806,6 @@
 
 void CMemory::map_index(uint32_t bank_s, uint32_t bank_e, uint32_t addr_s, uint32_t addr_e, int index, int type)
 {
-	bool isROM = !(type == MAP_TYPE_I_O || type == MAP_TYPE_RAM);
 	bool isRAM = type != MAP_TYPE_I_O;
 
 	for (uint32_t c = bank_s; c <= bank_e; ++c)
@@ -825,7 +813,7 @@
 		{
 			uint32_t p = (c << 4) | (i >> 12);
 			this->Map[p] = reinterpret_cast<uint8_t *>(index);
-			this->BlockIsROM[p] = isROM;
+			this->BlockIsROM[p] = false;
 			this->BlockIsRAM[p] = isRAM;
 		}
 }
@@ -862,7 +850,7 @@
 
 void CMemory::map_WriteProtectROM()
 {
-	std::copy(&this->Map[0], &this->Map[0x1000], &this->WriteMap[0]);
+	std::copy_n(&this->Map[0], 0x1000, &this->WriteMap[0]);
 
 	for (int c = 0; c < 0x1000; ++c)
 		if (this->BlockIsROM[c])
@@ -1119,17 +1107,6 @@
 			SRAMMask = ((1 << (SRAMSize + 3)) * 128) - 1;
 		}
 
-		// SRAM value fixes
-		if (this->match_na("SUPER DRIFT OUT") || // Super Drift Out
-			this->match_na("SATAN IS OUR FATHER!") ||
-			this->match_na("goemon 4")) // Ganbare Goemon Kirakira Douchuu
-			SNESGameFixes.SRAMInitialValue = 0x00;
-
-		// Additional game fixes by sanmaiwashi ...
-		// XXX: unnecessary?
-		if (this->match_na("SFX \xC5\xB2\xC4\xB6\xDE\xDD\xC0\xDE\xD1\xD3\xC9\xB6\xDE\xC0\xD8 1")) // SD Gundam Gaiden - Knight Gundam Monogatari
-			SNESGameFixes.SRAMInitialValue = 0x6b;
-
 		// others: BS and ST-01x games are 0x00.
 
 		//// OAM hacks :(

--- a/src/in_snsf/snes9x/ppu.cpp
+++ b/src/in_snsf/snes9x/ppu.cpp
@@ -229,15 +229,11 @@
 		// write_port will run the APU until given clock before writing value
 		S9xAPUWritePort(Address & 3, Byte);
 	else if (Address <= 0x2183)
-	{
 		switch (Address)
 		{
 			case 0x2100: // INIDISP
-				if (Byte != Memory.FillRAM[0x2100])
-				{
-					if ((Memory.FillRAM[0x2100] & 0x80) != (Byte & 0x80))
-						PPU.ForcedBlanking = !!((Byte >> 7) & 1);
-				}
+				if (Byte != Memory.FillRAM[0x2100] && (Memory.FillRAM[0x2100] & 0x80) != (Byte & 0x80))
+					PPU.ForcedBlanking = !!((Byte >> 7) & 1);
 
 				if ((Memory.FillRAM[0x2100] & 0x80) && CPU.V_Counter == PPU.ScreenHeight + FIRST_VISIBLE_LINE)
 				{
@@ -434,10 +430,7 @@
 					PPU.WRAM |= Byte << 16;
 					PPU.WRAM &= 0x1ffff;
 				}
-
-				break;
 		}
-	}
 
 	Memory.FillRAM[Address] = Byte;
 }
@@ -658,7 +651,7 @@
 				DMA[d].UnusedBit43x0 = !!(Byte & 0x20);
 				DMA[d].AAddressDecrement = !!(Byte & 0x10);
 				DMA[d].AAddressFixed = !!(Byte & 0x08);
-				DMA[d].TransferMode = (Byte & 7);
+				DMA[d].TransferMode = Byte & 7;
 				return;
 
 			case 0x1: // 0x43x1: BBADx
@@ -775,9 +768,9 @@
 				uint16_t div = Byte ? a / Byte : 0xffff;
 				uint16_t rem = Byte ? a % Byte : a;
 				// FIXME: The update occurs 16 machine cycles after $4206 is set.
-				Memory.FillRAM[0x4214] = static_cast<uint8_t>(div);
+				Memory.FillRAM[0x4214] = div & 0xff;
 				Memory.FillRAM[0x4215] = div >> 8;
-				Memory.FillRAM[0x4216] = static_cast<uint8_t>(rem);
+				Memory.FillRAM[0x4216] = rem & 0xff;
 				Memory.FillRAM[0x4217] = rem >> 8;
 				break;
 			}
@@ -844,12 +837,7 @@
 
 			case 0x420d: // MEMSEL
 				if ((Byte & 1) != (Memory.FillRAM[0x420d] & 1))
-				{
-					if (Byte & 1)
-						CPU.FastROMSpeed = ONE_CYCLE;
-					else
-						CPU.FastROMSpeed = SLOW_ONE_CYCLE;
-				}
+					CPU.FastROMSpeed = Byte & 1 ? ONE_CYCLE : SLOW_ONE_CYCLE;
 
 				break;
 
@@ -948,8 +936,7 @@
 
 			case 0x4211: // TIMEUP
 				byte = CPU.IRQLine ? 0x80 : 0;
-				CPU.IRQLine = false;
-				CPU.IRQTransition = false;
+				CPU.IRQLine = CPU.IRQTransition = false;
 				return byte | (OpenBus & 0x7f);
 
 			case 0x4212: // HVBJOY
@@ -1011,7 +998,7 @@
 	PPU.OAMPriorityRotation = false;
 	PPU.OAMFlip = 0;
 	PPU.OAMWriteRegister = 0;
-	std::fill(&PPU.OAMData[0], &PPU.OAMData[512 + 32], 0);
+	std::fill_n(&PPU.OAMData[0], sizeof(PPU.OAMData), 0);
 
 	PPU.FirstSprite = 0;
 
@@ -1038,12 +1025,12 @@
 	IPPU.Interlace = false;
 
 	for (int c = 0; c < 0x8000; c += 0x100)
-		std::fill(&Memory.FillRAM[c], &Memory.FillRAM[c + 0x100], c >> 8);
-	std::fill(&Memory.FillRAM[0x2100], &Memory.FillRAM[0x2200], 0);
-	std::fill(&Memory.FillRAM[0x4200], &Memory.FillRAM[0x4300], 0);
-	std::fill(&Memory.FillRAM[0x4000], &Memory.FillRAM[0x4100], 0);
+		std::fill_n(&Memory.FillRAM[c], 0x100, c >> 8);
+	std::fill_n(&Memory.FillRAM[0x2100], 0x0100, 0);
+	std::fill_n(&Memory.FillRAM[0x4200], 0x0100, 0);
+	std::fill_n(&Memory.FillRAM[0x4000], 0x0100, 0);
 	// For BS Suttehakkun 2...
-	std::fill(&Memory.FillRAM[0x1000], &Memory.FillRAM[0x2000], 0);
+	std::fill_n(&Memory.FillRAM[0x1000], 0x1000, 0);
 
 	Memory.FillRAM[0x4201] = Memory.FillRAM[0x4213] = 0xff;
 }

--- a/src/in_snsf/snes9x/sdd1.cpp
+++ b/src/in_snsf/snes9x/sdd1.cpp
@@ -178,7 +178,6 @@
 #include <algorithm>
 #include "snes9x.h"
 #include "memmap.h"
-#include "sdd1.h"
 
 void S9xSetSDD1MemoryMap(uint32_t bank, uint32_t value)
 {
@@ -188,18 +187,14 @@
 	for (int c = 0; c < 0x100; c += 16)
 	{
 		uint8_t *block = &Memory.ROM[value + (c << 12)];
-		for (int i = c; i < c + 16; ++i)
-			Memory.Map[i + bank] = block;
+		std::fill_n(&Memory.Map[bank + c], 16, block);
 	}
 }
 
 void S9xResetSDD1()
 {
-	std::fill(&Memory.FillRAM[0x4800], &Memory.FillRAM[0x4804], 0);
-	for (int i = 0; i < 4; ++i)
-	{
-		Memory.FillRAM[0x4804 + i] = i;
-		S9xSetSDD1MemoryMap(i, i);
-	}
+	std::fill_n(&Memory.FillRAM[0x4800], 4, 0);
+	int i = 0;
+	std::generate_n(&Memory.FillRAM[0x4804], 4, [&]() -> int { S9xSetSDD1MemoryMap(i, i); return i++; });
 }
 

--- a/src/in_snsf/snes9x/snes9x.h
+++ b/src/in_snsf/snes9x/snes9x.h
@@ -299,7 +299,6 @@
 
 struct SSNESGameFixes
 {
-	uint8_t SRAMInitialValue;
 	bool Uniracers;
 };