Use memset and memcpy instead of loops

This commit is contained in:
Franck STAUFFER 2020-08-03 15:34:33 +02:00
parent ef80e1ae34
commit 77848b630a
Signed by: franck.stauffer
GPG Key ID: AAF5A94045CEC261

View File

@ -50,15 +50,11 @@ init_chip8(void)
{
srand(time(NULL));
for (register uint_fast8_t t = 0; t < 16; ++t) {
v[t] = 0;
stack[t] = 0;
key[t] = 0;
}
for (register uint_fast8_t x = 0; x < 64; ++x)
for (register uint_fast8_t y = 0; y < 32; ++y)
gfx[x][y] = 0;
memset(v, 0, 16 * sizeof(byte));
memset(stack, 0, 16 * sizeof(word));
memset(key, 0, 16 * sizeof(byte));
memset(gfx, 0, 32 * 64 * sizeof(byte));
memset(oldgfx, 0, 32 * 64 * sizeof(byte));
const byte fontset[80] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
@ -79,11 +75,8 @@ init_chip8(void)
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
for (register uint_fast8_t t = 0; t < 80; ++t)
memory[t] = fontset[t];
for (register uint_fast16_t t = 80; t < 4096; ++t)
memory[t] = 0;
memcpy(memory, fontset, 80 * sizeof(byte));
memset(memory + 80, 0, 4016);
}
void
@ -332,9 +325,7 @@ op_handler(void)
{
switch (op) {
case 0x00E0:
for (register uint_fast8_t x = 0; x < 64; ++x)
for (register uint_fast8_t y = 0; y < 32; ++y)
gfx[x][y] = 0;
memset(gfx, 0, 32 * 63 * sizeof(byte));
SDL_Rect r;
r.h = 32 * SCALE;
@ -500,12 +491,7 @@ op_handler(void)
pc += 2;
return 1;
case 0xD000:
for (register uint_fast8_t x = 0; x < 64;
++x)
for (register uint_fast8_t y = 0;
y < 32;
++y)
oldgfx[x][y] = gfx[x][y];
memcpy(oldgfx, gfx, 32 * 64 * sizeof(byte));
v[0xF] = 0;