Better argument parsing

This commit is contained in:
Ry 2022-10-27 15:30:50 -07:00
parent e402e96cfa
commit 5b00bcef56
2 changed files with 45 additions and 5 deletions

View File

@ -20,7 +20,12 @@ Download the latest release or commit of [**fox32rom**](https://github.com/fox32
### Usage ### 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 <file>`: mount the specified file as a disk
- `--rom <file>`: 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. See [encoding.md](docs/encoding.md) for information about the instruction set.

View File

@ -35,6 +35,7 @@ struct timeval rtc_current_time;
uint32_t rtc_uptime; uint32_t rtc_uptime;
void main_loop(void); void main_loop(void);
void load_rom(const char *filename);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) { 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_read = bus_io_read;
vm.io_write = bus_io_write; vm.io_write = bus_io_write;
vm.halted = false; vm.halted = false;
//vm.debug = true; vm.debug = false;
memcpy(vm.memory_rom, fox32rom, sizeof(fox32rom)); memcpy(vm.memory_rom, fox32rom, sizeof(fox32rom));
if (argc > 1) { size_t disk_id = 0;
for (int i = 0; i + 1 < argc; i++) { for (int i = 1; i < argc; i++) {
new_disk(argv[i + 1], 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++; 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);
}