commit 6646982373405f9be03c675d56c0de3c39e24dcc
parent 278aaf87f55d7e0cb52a28e52854ee738c790e96
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 30 May 2020 02:28:03 +0300
added error macro, minor changes
Diffstat:
7 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/Makefile b/Makefile
@@ -13,7 +13,7 @@ MKDIR_P = mkdir -p
CC = gcc
CPPFLAGS += -Iinclude -pedantic
-CFLAGS += -Wall -std=c99
+CFLAGS += -Wall -std=c99 -O3
LDFLAGS += -Llib
LDLIBS += -lSDL2
diff --git a/bin/chip8 b/bin/chip8
Binary files differ.
diff --git a/obj/chip8.o b/obj/chip8.o
Binary files differ.
diff --git a/obj/main.o b/obj/main.o
Binary files differ.
diff --git a/src/chip8.c b/src/chip8.c
@@ -59,7 +59,7 @@ load(Chip8 *chip8, const char *fpath)
FILE *rom = fopen(fpath, "rb");
if (rom == NULL)
{
- fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath);
+ ERROR("Error loading ROM (%s). Exiting. . .", fpath);
return FALSE;
}
fseek(rom, 0, SEEK_END);
@@ -69,14 +69,14 @@ load(Chip8 *chip8, const char *fpath)
char *buf = (char *)malloc(romsize * sizeof(char));
if (buf == NULL)
{
- fprintf(stderr, "Cannot allocate memory. Exiting. . .\n");
+ ERROR("%s", "Cannot allocate memory. Exiting. . .");
return FALSE;
}
size_t res = fread(buf, sizeof(char), (size_t)romsize, rom);
if (res != romsize)
{
- fprintf(stderr, "Error reading ROM. Exiting. . .\n");
+ ERROR("%s", "Error reading ROM. Exiting. . .");
return FALSE;
}
@@ -86,7 +86,7 @@ load(Chip8 *chip8, const char *fpath)
memory[i + 512] = (uint8_t)buf[i];
else
{
- fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n");
+ ERROR("%s", "ROM can't fit into memory. Exiting. . .");
return FALSE;
}
@@ -132,7 +132,7 @@ decode(Chip8 *chip8)
pc = stack[--sp];
break;
default:
- fprintf(stderr, "Unknown opcode: %x\n", opcode);
+ ERROR("Unknown opcode: %x", opcode);
return FALSE;
}
break;
@@ -193,7 +193,7 @@ decode(Chip8 *chip8)
V[(opcode & 0x0F00) >> 8] <<= 1;
break;
default:
- fprintf(stderr, "Unknown opcode: %x\n", opcode);
+ ERROR("Unknown opcode: %x", opcode);
return FALSE;
}
break;
@@ -245,7 +245,7 @@ decode(Chip8 *chip8)
if (keys[V[(opcode & 0x0F00) >> 8]] == 0) pc += 2;
break;
default:
- fprintf(stderr, "Unknown opcode: %x\n", opcode);
+ ERROR("Unknown opcode: %x", opcode);
return FALSE;
}
break;
@@ -299,12 +299,12 @@ decode(Chip8 *chip8)
I += ((opcode & 0x0F00) >> 8) + 1;
break;
default:
- fprintf(stderr, "Unknown opcode: %x\n", opcode);
+ ERROR("Unknown opcode: %x", opcode);
return FALSE;
}
break;
default:
- fprintf(stderr, "Unimplemented opcode\n");
+ ERROR("%s", "Unimplemented opcode");
return FALSE;
}
diff --git a/src/chip8.h b/src/chip8.h
@@ -10,6 +10,8 @@
#define TRUE 1
#define FALSE 0
+#define ERROR(format, ...) (fprintf(stderr, format"\n", __VA_ARGS__))
+
typedef struct {
uint8_t memory[4096];
uint16_t stack[16];
diff --git a/src/main.c b/src/main.c
@@ -24,13 +24,13 @@ main(int argc, char **argv)
if (argc != 2)
{
- fprintf(stderr, "Usage: ./chip8 [ROM]\n");
+ ERROR("%s", "Usage: ./chip8 [ROM]");
return EXIT_FAILURE;
}
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
- fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n");
+ ERROR("%s", "Cannot initialize SDL. Exiting. . .");
return EXIT_FAILURE;
}
@@ -39,8 +39,7 @@ main(int argc, char **argv)
SDL_WINDOW_SHOWN);
if (win == NULL)
{
- fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n",
- SDL_GetError());
+ ERROR("Cannot create SDL window. Exiting. . .\n%s", SDL_GetError());
return EXIT_FAILURE;
}
@@ -62,10 +61,10 @@ main(int argc, char **argv)
int i;
while (SDL_PollEvent(&e))
{
- if (e.type == SDL_QUIT) return EXIT_FAILURE;
+ if (e.type == SDL_QUIT) return EXIT_SUCCESS;
if (e.type == SDL_KEYDOWN)
{
- if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_FAILURE;
+ if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_SUCCESS;
for (i = 0; i < 16; i++)
if (e.key.keysym.sym == keymap[i])
chip8.keys[i] = 1;