diff --git a/src/mem.c b/src/mem.c index 9f4f779..7a3de01 100644 --- a/src/mem.c +++ b/src/mem.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "cpu.h" #include "mem.h" @@ -186,3 +187,52 @@ void mem_mod_byte_indx(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) { void mem_mod_byte_indy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) { mem_set_byte_indy(cpu, addr, op(cpu, mem_get_byte_indy(cpu, addr))); } + +// For parsing --memory options + +struct ewm_memory_option_t *parse_memory_option(char *s) { + char *type = strsep(&s, ":"); + if (type == NULL) { // || (strcmp(type, "ram") && strcmp(type, "rom"))) { + printf("type fail\n"); + return NULL; + } + + char *address = strsep(&s, ":"); + if (address == NULL) { + printf("address fail\n"); + return NULL; + } + + char *path = strsep(&s, ":"); + if (path == NULL) { + printf("path fail\n"); + return NULL; + } + + struct ewm_memory_option_t *m = (struct ewm_memory_option_t*) malloc(sizeof(struct ewm_memory_option_t)); + m->type = strcmp(type, "ram") ? EWM_MEMORY_TYPE_RAM : EWM_MEMORY_TYPE_ROM; + m->path = path; + m->address = atoi(address); + m->next = NULL; + + return m; +} + +int cpu_add_memory_from_options(struct cpu_t *cpu, struct ewm_memory_option_t *m) { + while (m != NULL) { + fprintf(stderr, "[EWM] Adding %s $%.4X %s\n", m->type == EWM_MEMORY_TYPE_RAM ? "RAM" : "ROM", m->address, m->path); + if (m->type == EWM_MEMORY_TYPE_RAM) { + if (cpu_add_ram_file(cpu, m->address, m->path) == NULL) { + fprintf(stderr, "[MEM] Failed to add RAM from %s\n", m->path); + return -1; + } + } else { + if (cpu_add_rom_file(cpu, m->address, m->path) == NULL) { + fprintf(stderr, "[MEM] Failed to add ROM from %s\n", m->path); + return -1; + } + } + m = m->next; + } + return 0; +} diff --git a/src/mem.h b/src/mem.h index 55a0743..b3b7417 100644 --- a/src/mem.h +++ b/src/mem.h @@ -63,4 +63,19 @@ void mem_mod_byte_indy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op); uint16_t mem_get_word(struct cpu_t *cpu, uint16_t addr); void mem_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v); +// For parsing --memory options + +#define EWM_MEMORY_TYPE_RAM (0) +#define EWM_MEMORY_TYPE_ROM (1) + +struct ewm_memory_option_t { + int type; + char *path; + uint16_t address; + struct ewm_memory_option_t *next; +}; + +struct ewm_memory_option_t *parse_memory_option(char *s); +int cpu_add_memory_from_options(struct cpu_t *cpu, struct ewm_memory_option_t *m); + #endif diff --git a/src/one.c b/src/one.c index c666b45..4a16d2f 100644 --- a/src/one.c +++ b/src/one.c @@ -178,22 +178,24 @@ static bool ewm_one_step_cpu(struct ewm_one_t *one, int cycles) { return true; } -#define EWM_ONE_OPT_MODEL 0 +#define EWM_ONE_OPT_MODEL (0) +#define EWM_ONE_OPT_MEMORY (1) static struct option one_options[] = { { "model", required_argument, NULL, EWM_ONE_OPT_MODEL }, - { NULL, 0, NULL, 0 } + { "memory", required_argument, NULL, EWM_ONE_OPT_MEMORY }, + { NULL, 0, NULL, 0 } }; int ewm_one_main(int argc, char **argv) { // Parse Apple 1 specific options - int model = EWM_ONE_MODEL_DEFAULT; + struct ewm_memory_option_t *extra_memory = NULL; char ch; while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) { switch (ch) { - case EWM_ONE_OPT_MODEL: + case EWM_ONE_OPT_MODEL: { if (strcmp(optarg, "apple1") == 0) { model = EWM_ONE_MODEL_APPLE1; } else if (strcmp(optarg, "replica1") == 0) { @@ -203,6 +205,16 @@ int ewm_one_main(int argc, char **argv) { exit(1); } break; + } + case EWM_ONE_OPT_MEMORY: { + struct ewm_memory_option_t *m = parse_memory_option(optarg); + if (m == NULL) { + exit(1); + } + m->next = extra_memory; + extra_memory = m; + break; + } } } @@ -237,6 +249,14 @@ int ewm_one_main(int argc, char **argv) { return 1; } + // Add extra memory, if any + + if (extra_memory != NULL) { + if (cpu_add_memory_from_options(one->cpu, extra_memory) != 0) { + exit(1); + } + } + cpu_reset(one->cpu); // Main loop diff --git a/src/two.c b/src/two.c index eb7f523..4ca337e 100644 --- a/src/two.c +++ b/src/two.c @@ -496,12 +496,14 @@ static bool ewm_two_step_cpu(struct ewm_two_t *two, int cycles) { #define EWM_TWO_OPT_DRIVE2 (1) #define EWM_TWO_OPT_COLOR (2) #define EWM_TWO_OPT_FPS (3) +#define EWM_TWO_OPT_MEMORY (4) static struct option one_options[] = { { "drive1", required_argument, NULL, EWM_TWO_OPT_DRIVE1 }, { "drive2", required_argument, NULL, EWM_TWO_OPT_DRIVE2 }, { "color", no_argument, NULL, EWM_TWO_OPT_COLOR }, { "fps", required_argument, NULL, EWM_TWO_OPT_FPS }, + { "memory", required_argument, NULL, EWM_TWO_OPT_MEMORY }, { NULL, 0, NULL, 0 } }; @@ -512,6 +514,7 @@ int ewm_two_main(int argc, char **argv) { char *drive2 = NULL; bool color = false; int fps = EWM_TWO_FPS_DEFAULT; + struct ewm_memory_option_t *extra_memory = NULL; char ch; while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) { @@ -528,6 +531,15 @@ int ewm_two_main(int argc, char **argv) { case EWM_TWO_OPT_FPS: fps = atoi(optarg); break; + case EWM_TWO_OPT_MEMORY: { + struct ewm_memory_option_t *m = parse_memory_option(optarg); + if (m == NULL) { + exit(1); + } + m->next = extra_memory; + extra_memory = m; + break; + } } } @@ -585,6 +597,14 @@ int ewm_two_main(int argc, char **argv) { } } + // Add extra memory, if any + + if (extra_memory != NULL) { + if (cpu_add_memory_from_options(two->cpu, extra_memory) != 0) { + exit(1); + } + } + cpu_reset(two->cpu); //