@@ -2,8 +2,11 @@ | |||||
Released under the terms of the MIT License. See LICENSE for details. */ | Released under the terms of the MIT License. See LICENSE for details. */ | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | |||||
#include "src/assembler.h" | |||||
#include "src/config.h" | #include "src/config.h" | ||||
#include "src/disassembler.h" | |||||
#include "src/logging.h" | #include "src/logging.h" | ||||
#include "src/rom.h" | #include "src/rom.h" | ||||
@@ -13,7 +16,7 @@ | |||||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||||
{ | { | ||||
Config *config; | Config *config; | ||||
int retval; | |||||
int retval = EXIT_SUCCESS; | |||||
retval = config_create(&config, argc, argv); | retval = config_create(&config, argc, argv); | ||||
if (retval != CONFIG_OK) | if (retval != CONFIG_OK) | ||||
@@ -23,10 +26,15 @@ int main(int argc, char *argv[]) | |||||
config_dump_args(config); | config_dump_args(config); | ||||
#endif | #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 { | } else { | ||||
ROM *rom; | ROM *rom; | ||||
@@ -37,12 +45,13 @@ int main(int argc, char *argv[]) | |||||
else | else | ||||
FATAL_ERRNO("couldn't load ROM image '%s'", config->rom_path) | 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); | rom_close(rom); | ||||
} | } | ||||
config_destroy(config); | config_destroy(config); | ||||
return EXIT_SUCCESS; | |||||
return retval; | |||||
} | } |
@@ -0,0 +1,16 @@ | |||||
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
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; | |||||
} |
@@ -0,0 +1,13 @@ | |||||
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
Released under the terms of the MIT License. See LICENSE for details. */ | |||||
#pragma once | |||||
#include <stdbool.h> | |||||
/* Structs */ | |||||
// ... | |||||
/* Functions */ | |||||
bool assemble(const char*, const char*); |
@@ -312,7 +312,9 @@ static bool sanity_check(Config* config) | |||||
Create a new config object and load default values into it. | Create a new config object and load default values into it. | ||||
Return value is one of CONFIG_OK, CONFIG_EXIT_SUCCESS, CONFIG_EXIT_FAILURE | 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[]) | int config_create(Config** config_ptr, int argc, char* argv[]) | ||||
{ | { | ||||
@@ -0,0 +1,16 @@ | |||||
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
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; | |||||
} |
@@ -0,0 +1,13 @@ | |||||
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
Released under the terms of the MIT License. See LICENSE for details. */ | |||||
#pragma once | |||||
#include <stdbool.h> | |||||
/* Structs */ | |||||
// ... | |||||
/* Functions */ | |||||
bool disassemble(const char*, const char*); |
@@ -10,26 +10,26 @@ | |||||
/* Internal usage only */ | /* 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)) | #define PRINT_ERRNO() fprintf(stderr, ": %s", strerror(errno)) | ||||
/* Public logging macros */ | /* 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 | #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 | #endif | ||||
#define OUT_OF_MEMORY() FATAL("couldn't allocate enough memory") | #define OUT_OF_MEMORY() FATAL("couldn't allocate enough memory") |
@@ -27,8 +27,7 @@ ROM* rom_open(const char *path) | |||||
return NULL; | return NULL; | ||||
} | } | ||||
if (!(s.st_mode & S_IFREG)) { | if (!(s.st_mode & S_IFREG)) { | ||||
if (s.st_mode & S_IFDIR) | |||||
errno = EISDIR; | |||||
errno = (s.st_mode & S_IFDIR) ? EISDIR : ENOENT; | |||||
fclose(fp); | fclose(fp); | ||||
return NULL; | return NULL; | ||||
} | } | ||||