From 5b00bcef56c20b43c6662f055712a1a449e79a79 Mon Sep 17 00:00:00 2001 From: Ry Date: Thu, 27 Oct 2022 15:30:50 -0700 Subject: [PATCH] Better argument parsing --- README.md | 7 ++++++- src/main.c | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a8f778b..104457c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,12 @@ Download the latest release or commit of [**fox32rom**](https://github.com/fox32 ### Usage -Passing files as arguments will mount those files as disks, in the order that the arguments were passed. The most common use case is passing the [**fox32os**](https://github.com/fox32-arch/fox32os) disk image as the first argument: `./fox32 fox32os.img` +The following arguments are valid: +- `--disk `: mount the specified file as a disk +- `--rom `: use the specified file as the boot ROM. if this argument is not specified then the embedded copy of **fox32rom** is used +- `--debug`: print a disassembly of each instruction as it runs + +The most common use case is passing the [**fox32os**](https://github.com/fox32-arch/fox32os) disk image as the first disk: `./fox32 --disk fox32os.img` See [encoding.md](docs/encoding.md) for information about the instruction set. diff --git a/src/main.c b/src/main.c index 37d36ef..142ffc9 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ struct timeval rtc_current_time; uint32_t rtc_uptime; void main_loop(void); +void load_rom(const char *filename); int main(int argc, char *argv[]) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -48,13 +49,33 @@ int main(int argc, char *argv[]) { vm.io_read = bus_io_read; vm.io_write = bus_io_write; vm.halted = false; - //vm.debug = true; + vm.debug = false; memcpy(vm.memory_rom, fox32rom, sizeof(fox32rom)); - if (argc > 1) { - for (int i = 0; i + 1 < argc; i++) { - new_disk(argv[i + 1], i); + size_t disk_id = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--disk") == 0) { + if (i + 1 < argc) { + new_disk(argv[i + 1], disk_id++); + i++; + } else { + fprintf(stderr, "no disk image specified\n"); + return 1; + } + } else if (strcmp(argv[i], "--rom") == 0) { + if (i + 1 < argc) { + load_rom(argv[i + 1]); + i++; + } else { + fprintf(stderr, "no rom image specified\n"); + return 1; + } + } else if (strcmp(argv[i], "--debug") == 0) { + vm.debug = true; + } else { + fprintf(stderr, "unrecognized option %s\n", argv[i]); + return 1; } } @@ -134,3 +155,17 @@ void main_loop(void) { ticks++; } + +void load_rom(const char *filename) { + FILE *rom; + rom = fopen(filename, "r"); + + if (!rom) { + fprintf(stderr, "couldn't open ROM file %s\n", filename); + return; + } + + printf("using %s as boot ROM\n", filename); + fread(&vm.memory_rom, sizeof(fox32rom), 1, rom); + fclose(rom); +}