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