diff --git a/src/apps/gbdb.c b/src/apps/gbdb.c index 090bf4b..b312560 100644 --- a/src/apps/gbdb.c +++ b/src/apps/gbdb.c @@ -37,7 +37,6 @@ static int64_t parse_val(const char *str); static const char prompt[] = "gbdb > "; static const char usage[] = "Available commands:\n" - "load : loads the given file as the gameboy cartridge\n" "run: runs the CPU until a breakpoint or halt is hit\n" "break : Adds a breakpoint for the given addess\n" "step : executes \"cycle\" instructions (default 1)\n" @@ -108,13 +107,13 @@ static void strip_newline(char *string) } } -static void init(void) +static void init(const char *bootrom, const char *rom) { cpu_ops.undef_d3 = breakpoint_cb; lr35902_init(&cpu, memory, &cpu_ops); gb_video_init(&video, memory); - gb_mem_init(memory, &video); + gb_mem_init(memory, &video, bootrom, rom); config.log_fd = -1; config.trace_fd = -1; @@ -570,31 +569,6 @@ static void mem(struct tokens **tokens) } } -static void load(struct tokens **tokens) -{ - char *token = token_next(tokens); - if (token == NULL) { - gb_error("usage: load \n"); - return; - } - - gb_log("loading %s\n", token); - - gb_memory_load_file(memory, GB_MEMORY_CART, token); -} - -static void load_bootrom(struct tokens **tokens) -{ - char *token = token_next(tokens); - if (token == NULL) { - gb_error("usage: bootrom \n"); - return; - } - - gb_memory_load_file(memory, GB_MEMORY_BOOTROM, token); -} - - static void quit(struct tokens **tokens) { exit(0); @@ -869,9 +843,10 @@ static int load_initfile(const char *initfile_path, struct tri *commands) return 0; } -int main(int argc, char **argv) +int main(int argc, const char **argv) { struct tri commands; + const char *rom, *bootrom; tri_init(&commands); tri_add_string(&commands, "#", comment); @@ -882,8 +857,6 @@ int main(int argc, char **argv) tri_add_string(&commands, "stats", stats); tri_add_string(&commands, "exit", quit); tri_add_string(&commands, "quit", quit); - tri_add_string(&commands, "load", load); - tri_add_string(&commands, "bootrom", load_bootrom); tri_add_string(&commands, "mem", mem); /* FIXME */ /* tri_add_string(&commands, "dump", mem_dump); */ @@ -897,11 +870,22 @@ int main(int argc, char **argv) tri_add_string(&commands, "set", set); tri_add_string(&commands, "echo", echo); - init(); - if (argc > 1) { - if (load_initfile(argv[1], &commands)) { + if (argc < 3) { + gb_error("usage: %s INIT_SCRIPT\n", argv[0]); + exit(1); + } + + bootrom = argv[1]; + rom = argv[2]; + + init(bootrom, rom); + + if (argc > 3) { + gb_log("%s\n", argv[0]); + if (load_initfile(argv[3], &commands)) { gb_error("Failed to load initfile\n"); + exit(1); } } diff --git a/src/gbemu/memory.c b/src/gbemu/memory.c index f349d66..4843320 100644 --- a/src/gbemu/memory.c +++ b/src/gbemu/memory.c @@ -25,11 +25,38 @@ static unsigned char bootrom[0x100] = {0}; static unsigned char cart_low[0x100] = {0}; static int bootrom_mapped = 1; -void gb_mem_init(struct gb_memory *memory, struct gb_video *v) +static void load_file_to_buffer(const char *filename, uint8_t *buffer, size_t size) +{ + //TODO: Check error codes like a real programmer :P + FILE* prog_file; + long filesize; + + prog_file = fopen(filename, "r"); + if(prog_file < 0) { + printf("Failed to load game file: %d\n", errno); + return; + } + + fseek(prog_file, 0, SEEK_END); + filesize = ftell(prog_file); + fseek(prog_file, 0, SEEK_SET); + + if (fread(buffer, MIN(filesize, size), 1, prog_file) < 0) { + printf("Failed to read filed: %d\n", errno); + } + fclose(prog_file); +} + +void gb_mem_init(struct gb_memory *memory, struct gb_video *v, + const char *bootrom_file, const char *rom_file) { video = v; memset(&ram, 0, MAX_RAM_LEN); bootrom_mapped = 1; + load_file_to_buffer(rom_file, cart_low, sizeof(cart_low)); + load_file_to_buffer(rom_file, ram, sizeof(ram)); + load_file_to_buffer(bootrom_file, bootrom, sizeof(bootrom)); + load_file_to_buffer(bootrom_file, ram, sizeof(ram)); } static uint8_t ser_byte = 0; @@ -169,67 +196,3 @@ void gb_mem_force_write(struct gb_memory *memory, uint16_t addr, uint8_t val) } } - -static void load_file_to_buffer(char *filename, uint8_t *buffer, size_t size) -{ - //TODO: Check error codes like a real programmer :P - FILE* prog_file; - long filesize; - - prog_file = fopen(filename, "r"); - if(prog_file < 0) { - printf("Failed to load game file: %d\n", errno); - return; - } - - fseek(prog_file, 0, SEEK_END); - filesize = ftell(prog_file); - fseek(prog_file, 0, SEEK_SET); - - if (fread(buffer, MIN(filesize, size), 1, prog_file) < 0) { - printf("Failed to read filed: %d\n", errno); - } - fclose(prog_file); - -} - -int gb_memory_load_file(struct gb_memory *memory, enum gb_memory_range destination, char *filename) -{ - //TODO: Check error codes like a real programmer :P - FILE* prog_file; - long filesize; - - switch (destination) { - case GB_MEMORY_BOOTROM: - load_file_to_buffer(filename, bootrom, sizeof(bootrom)); - load_file_to_buffer(filename, ram, sizeof(bootrom)); - - break; - case GB_MEMORY_CART: - load_file_to_buffer(filename, ram, sizeof(ram)); - load_file_to_buffer(filename, cart_low, sizeof(cart_low)); - break; - default: - return -1; - } -} - -/* static void mem_dump(char *filename) */ -/* { */ -/* //TODO: Check error codes like a real programmer :P */ -/* FILE* dump_file; */ -/* char *token = strtok(filename, " "); */ - -/* if (token == NULL) { */ -/* gb_log("usage: load \n"); */ -/* return; */ -/* } */ -/* strip_newline(token); */ -/* dump_file = fopen(token, "w"); */ -/* if(dump_file == NULL) { */ -/* gb_log("Failed to open mem dump file: %d\n", errno); */ -/* return; */ -/* } */ -/* fwrite(ram, MAX_RAM_LEN, 1, dump_file); */ -/* fclose(dump_file); */ -/* } */ diff --git a/src/gbemu/memory.h b/src/gbemu/memory.h index 53bbc6f..5a8f43f 100644 --- a/src/gbemu/memory.h +++ b/src/gbemu/memory.h @@ -6,16 +6,8 @@ struct gb_memory; struct gb_video; -enum gb_memory_range { - GB_MEMORY_BOOTROM, - GB_MEMORY_CART, -}; - -void gb_mem_init(struct gb_memory *memory, struct gb_video *v); - -int gb_memory_load_file(struct gb_memory *memory, - enum gb_memory_range destination, - char *filename); +void gb_mem_init(struct gb_memory *memory, struct gb_video *v, + const char *bootrom, const char *rom); uint8_t gb_mem_read(struct gb_memory *mem, uint16_t addr);