From 827be7304a4c40b6465951e55195caab1d666f86 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 21 Jun 2018 15:55:20 -0400 Subject: add test_execute_steps(), Makefile --- .gitignore | 3 ++ Makefile | 19 ++++++++++ test.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 113 insertions(+), 26 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 2e1f048..599400f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ gb.c *.o +test +*.gb +out.png diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..79cc3c5 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +OBJS=gb.o test.o lodepng.o +CFLAGS=-W -Wall -pedantic -Werror -Wextra -std=c11 -O3 +APP=./test + +.PHONY=all clean test + +all: $(APP) + +gb.c: ops.yaml + ./gen.rb > gb.c + +$(APP): $(OBJS) + $(CC) -o $(APP) $(OBJS) + +%.o: %.c gb.h + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) $(OBJS) $(APP) diff --git a/test.c b/test.c index 32f0474..247529c 100644 --- a/test.c +++ b/test.c @@ -10,6 +10,7 @@ #include "gb.h" #define NUM_FRAMES 600 +#define NUM_STEPS 100 static uint32_t file_size( @@ -60,35 +61,99 @@ load( // buffer for rendered frames static uint8_t frames[NUM_FRAMES * GB_FRAME_SIZE]; -int main(int argc, char *argv[]) { - for (int i = 1; i < argc; i++) { - uint32_t rom_size = 0; - const uint8_t *rom_data = load(argv[i], &rom_size); - fprintf(stderr, "loaded \"%s\" (%u bytes)\n", argv[i], rom_size); - - gb_t ctx; - gb_init(&ctx, rom_data, rom_size); - fprintf(stderr, "gb context initialized\n"); - - // render several frames - for (size_t j = 0; j < NUM_FRAMES; j++) { - // render frame - gb_frame(&ctx); - - // copy frame to buffer - memcpy(frames + GB_FRAME_SIZE * j, gb_get_rgb_frame(&ctx), GB_FRAME_SIZE); - } +static void +test_render_frames( + const char * const png_path, + const char * const rom_path +) { + // load rom data + uint32_t rom_size = 0; + const uint8_t *rom_data = load(rom_path, &rom_size); + fprintf(stderr, "loaded \"%s\" (%u bytes)\n", rom_path, rom_size); + + // init context + gb_t ctx; + gb_init(&ctx, rom_data, rom_size); + fprintf(stderr, "gb context initialized\n"); + + // render frames + for (size_t j = 0; j < NUM_FRAMES; j++) { + // render frame + gb_frame(&ctx); + + // copy frame to buffer + memcpy(frames + GB_FRAME_SIZE * j, gb_get_rgb_frame(&ctx), GB_FRAME_SIZE); + } - // save png - if (lodepng_encode24_file("out.png", frames, 160, 144 * NUM_FRAMES)) { - fprintf(stderr, "lode_png_encode24_file() failed\n"); - return EXIT_FAILURE; - } + // save png + if (lodepng_encode24_file(png_path, frames, 160, 144 * NUM_FRAMES)) { + fprintf(stderr, "lode_png_encode24_file() failed\n"); + exit(EXIT_FAILURE); + } + fprintf(stderr, "%d frames rendered\n", NUM_FRAMES); - fprintf(stderr, "%d frames rendered\n", NUM_FRAMES); + // free rom data + free((void*) rom_data); +} - // free rom data - free((void*) rom_data); +const char * const RWS[] = { + "AF", + "BC", + "DE", + "HL", + "SP", + "PC", + NULL +}; + +static void +gb_dump_context( + const gb_t * const ctx +) { + for (int i = 0; RWS[i]; i++) { + printf("%s%s: 0x%04x", (!i) ? "" : ", ", RWS[i], ctx->cpu.rs[i]); + } + printf("\n"); +} + +static void +test_execute_steps( + const char * const rom_path +) { + // load rom data + uint32_t rom_size = 0; + const uint8_t *rom_data = load(rom_path, &rom_size); + fprintf(stderr, "loaded \"%s\" (%u bytes)\n", rom_path, rom_size); + + // init context + gb_t ctx; + gb_init(&ctx, rom_data, rom_size); + fprintf(stderr, "gb context initialized\n"); + + // render frames + for (size_t j = 0; j < NUM_STEPS; j++) { + // render frame + gb_step(&ctx); + + // render frame + gb_dump_context(&ctx); + } + + // free rom data + free((void*) rom_data); +} + +int main(int argc, char *argv[]) { + // render 600 frames + for (int i = 2; i < argc; i++) { + if (argv[1][0] == 'f') { + test_render_frames("out.png", argv[i]); + } else if (argv[1][0] == 'e') { + test_execute_steps(argv[i]); + } else { + fprintf(stderr, "unknown test: %s\n", argv[i]); + exit(EXIT_FAILURE); + } } // return success -- cgit v1.2.3