From 103dfaf8417bd388a5674ed67b6ae01efaca8bcf Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 23 Mar 2015 03:08:12 -0400 Subject: [PATCH] Stub out assembler/disassembler code. --- crater.c | 25 +++++++++++++++++-------- src/assembler.c | 16 ++++++++++++++++ src/assembler.h | 13 +++++++++++++ src/config.c | 4 +++- src/disassembler.c | 16 ++++++++++++++++ src/disassembler.h | 13 +++++++++++++ src/logging.h | 26 +++++++++++++------------- src/rom.c | 3 +-- 8 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 src/assembler.c create mode 100644 src/assembler.h create mode 100644 src/disassembler.c create mode 100644 src/disassembler.h diff --git a/crater.c b/crater.c index 4e50b5c..fb3333f 100644 --- a/crater.c +++ b/crater.c @@ -2,8 +2,11 @@ Released under the terms of the MIT License. See LICENSE for details. */ #include +#include +#include "src/assembler.h" #include "src/config.h" +#include "src/disassembler.h" #include "src/logging.h" #include "src/rom.h" @@ -13,7 +16,7 @@ int main(int argc, char *argv[]) { Config *config; - int retval; + int retval = EXIT_SUCCESS; retval = config_create(&config, argc, argv); if (retval != CONFIG_OK) @@ -23,10 +26,15 @@ int main(int argc, char *argv[]) config_dump_args(config); #endif - if (config->assemble) { - printf("Running assembler: %s -> %s.\n", config->src_path, config->dst_path); - } else if (config->disassemble) { - printf("Running disassembler: %s -> %s.\n", config->src_path, config->dst_path); + if (config->assemble || config->disassemble) { + printf("crater: running %s: %s -> %s\n", + config->assemble ? "assembler" : "disassembler", + config->src_path, config->dst_path); + if (config->assemble) + retval = assemble(config->src_path, config->dst_path); + else + retval = disassemble(config->src_path, config->dst_path); + retval = retval ? EXIT_SUCCESS : EXIT_FAILURE; } else { ROM *rom; @@ -37,12 +45,13 @@ int main(int argc, char *argv[]) else FATAL_ERRNO("couldn't load ROM image '%s'", config->rom_path) } - printf("Loaded ROM image: %s.\n", rom->name); + printf("Loaded ROM image: %s\n", rom->name); + + // TODO: emulate game here... - // TODO: start from here rom_close(rom); } config_destroy(config); - return EXIT_SUCCESS; + return retval; } diff --git a/src/assembler.c b/src/assembler.c new file mode 100644 index 0000000..ae5446f --- /dev/null +++ b/src/assembler.c @@ -0,0 +1,16 @@ +/* Copyright (C) 2014-2015 Ben Kurtovic + Released under the terms of the MIT License. See LICENSE for details. */ + +#include "assembler.h" + +/* + Assemble the z80 source code at the input path into a binary. + + Return true if the operation was a success and false if it was a failure. + Errors are printed to STDOUT; if the operation was successful then nothing + is printed. +*/ +bool assemble(const char* src_path, const char* dst_path) +{ + return true; +} diff --git a/src/assembler.h b/src/assembler.h new file mode 100644 index 0000000..51f9266 --- /dev/null +++ b/src/assembler.h @@ -0,0 +1,13 @@ +/* Copyright (C) 2014-2015 Ben Kurtovic + Released under the terms of the MIT License. See LICENSE for details. */ + +#pragma once +#include + +/* Structs */ + +// ... + +/* Functions */ + +bool assemble(const char*, const char*); diff --git a/src/config.c b/src/config.c index 8b18090..d7e5bac 100644 --- a/src/config.c +++ b/src/config.c @@ -312,7 +312,9 @@ static bool sanity_check(Config* config) Create a new config object and load default values into it. Return value is one of CONFIG_OK, CONFIG_EXIT_SUCCESS, CONFIG_EXIT_FAILURE - and indicates how the caller should proceed. + and indicates how the caller should proceed. If the caller should exit, + then the config object should *not* be freed; otherwise it should be freed + with config_destroy() when the caller is ready. */ int config_create(Config** config_ptr, int argc, char* argv[]) { diff --git a/src/disassembler.c b/src/disassembler.c new file mode 100644 index 0000000..2f4fb7f --- /dev/null +++ b/src/disassembler.c @@ -0,0 +1,16 @@ +/* Copyright (C) 2014-2015 Ben Kurtovic + Released under the terms of the MIT License. See LICENSE for details. */ + +#include "disassembler.h" + +/* + Disassemble the binary file at the input path into z80 source code. + + Return true if the operation was a success and false if it was a failure. + Errors are printed to STDOUT; if the operation was successful then nothing + is printed. +*/ +bool disassemble(const char* src_path, const char* dst_path) +{ + return true; +} diff --git a/src/disassembler.h b/src/disassembler.h new file mode 100644 index 0000000..a30ee51 --- /dev/null +++ b/src/disassembler.h @@ -0,0 +1,13 @@ +/* Copyright (C) 2014-2015 Ben Kurtovic + Released under the terms of the MIT License. See LICENSE for details. */ + +#pragma once +#include + +/* Structs */ + +// ... + +/* Functions */ + +bool disassemble(const char*, const char*); diff --git a/src/logging.h b/src/logging.h index 179a7c6..f5df66e 100644 --- a/src/logging.h +++ b/src/logging.h @@ -10,26 +10,26 @@ /* Internal usage only */ -#define LOG_MSG(level, extra, tail, after, ...) { \ - fprintf(stderr, level ": " __VA_ARGS__); \ - extra; \ - fprintf(stderr, tail ? ".\n" : "\n"); \ - after; \ +#define LOG_MSG(level, extra, after, ...) { \ + fprintf(stderr, level ": " __VA_ARGS__); \ + extra; \ + fprintf(stderr, "\n"); \ + after; \ } #define PRINT_ERRNO() fprintf(stderr, ": %s", strerror(errno)) /* Public logging macros */ -#define FATAL(...) LOG_MSG("Error", {}, 1, exit(EXIT_FAILURE), __VA_ARGS__) -#define FATAL_ERRNO(...) LOG_MSG("Error", PRINT_ERRNO(), 1, exit(EXIT_FAILURE), __VA_ARGS__) -#define ERROR(...) LOG_MSG("Error", {}, 1, {}, __VA_ARGS__) -#define ERROR_ERRNO(...) LOG_MSG("Error", PRINT_ERRNO(), 1, {}, __VA_ARGS__) -#define WARN(...) LOG_MSG("Warning", {}, 1, {}, __VA_ARGS__) -#define WARN_ERRNO(...) LOG_MSG("Warning", PRINT_ERRNO(), 1, {}, __VA_ARGS__) +#define FATAL(...) LOG_MSG("Error", {}, exit(EXIT_FAILURE), __VA_ARGS__) +#define FATAL_ERRNO(...) LOG_MSG("Error", PRINT_ERRNO(), exit(EXIT_FAILURE), __VA_ARGS__) +#define ERROR(...) LOG_MSG("Error", {}, {}, __VA_ARGS__) +#define ERROR_ERRNO(...) LOG_MSG("Error", PRINT_ERRNO(), {}, __VA_ARGS__) +#define WARN(...) LOG_MSG("Warning", {}, {}, __VA_ARGS__) +#define WARN_ERRNO(...) LOG_MSG("Warning", PRINT_ERRNO(), {}, __VA_ARGS__) #ifdef DEBUG_MODE -#define DEBUG(...) LOG_MSG("[DEBUG]", {}, 0, {}, __VA_ARGS__) -#define DEBUG_ERRNO(...) LOG_MSG("[DEBUG]", PRINT_ERRNO(), 0, {}, __VA_ARGS__) +#define DEBUG(...) LOG_MSG("[DEBUG]", {}, {}, __VA_ARGS__) +#define DEBUG_ERRNO(...) LOG_MSG("[DEBUG]", PRINT_ERRNO(), {}, __VA_ARGS__) #endif #define OUT_OF_MEMORY() FATAL("couldn't allocate enough memory") diff --git a/src/rom.c b/src/rom.c index 2ba40fe..e684be6 100644 --- a/src/rom.c +++ b/src/rom.c @@ -27,8 +27,7 @@ ROM* rom_open(const char *path) return NULL; } if (!(s.st_mode & S_IFREG)) { - if (s.st_mode & S_IFDIR) - errno = EISDIR; + errno = (s.st_mode & S_IFDIR) ? EISDIR : ENOENT; fclose(fp); return NULL; }