diff --git a/src/chr.c b/src/chr.c index 342250f..c55b8c9 100644 --- a/src/chr.c +++ b/src/chr.c @@ -60,16 +60,6 @@ static int _load_rom_data(char *rom_path, uint8_t rom_data[2048]) { return 0; } -struct ewm_chr_t* ewm_chr_create(char *rom_path, int rom_type, SDL_Renderer *renderer) { - struct ewm_chr_t *chr = (struct ewm_chr_t*) malloc(sizeof(struct ewm_chr_t)); - int ret = ewm_chr_init(chr, rom_path, rom_type, renderer); - if (ret != 0) { - free(chr); - chr = NULL; - } - return chr; -} - static void _set_pixel(SDL_Surface * surface, int x, int y, Uint32 color) { uint32_t *pixel = (uint32_t*) ((uint8_t*) surface->pixels + (y * surface->pitch) + (x * sizeof(uint32_t))); *pixel = color; @@ -106,7 +96,7 @@ static SDL_Texture *_generate_texture(SDL_Renderer *renderer, uint8_t rom_data[2 return texture; } -int ewm_chr_init(struct ewm_chr_t *chr, char *rom_path, int rom_type, SDL_Renderer *renderer) { +static int ewm_chr_init(struct ewm_chr_t *chr, char *rom_path, int rom_type, SDL_Renderer *renderer) { if (rom_type != EWM_CHR_ROM_TYPE_2716) { return -1; } @@ -144,6 +134,16 @@ int ewm_chr_init(struct ewm_chr_t *chr, char *rom_path, int rom_type, SDL_Render return 0; } +struct ewm_chr_t* ewm_chr_create(char *rom_path, int rom_type, SDL_Renderer *renderer) { + struct ewm_chr_t *chr = (struct ewm_chr_t*) malloc(sizeof(struct ewm_chr_t)); + int ret = ewm_chr_init(chr, rom_path, rom_type, renderer); + if (ret != 0) { + free(chr); + chr = NULL; + } + return chr; +} + #if 0 int main() { struct ewm_chr_t *chr = ewm_chr_create("rom/3410036.bin", EWM_CHR_ROM_TYPE_2716); diff --git a/src/chr.h b/src/chr.h index 50ca07f..a36d8b3 100644 --- a/src/chr.h +++ b/src/chr.h @@ -32,6 +32,5 @@ struct ewm_chr_t { }; struct ewm_chr_t* ewm_chr_create(char *rom_path, int rom_type, SDL_Renderer *renderer); -int ewm_chr_init(struct ewm_chr_t *chr, char *rom_path, int rom_type, SDL_Renderer *renderer); #endif diff --git a/src/cpu.c b/src/cpu.c index aeef933..73fb70d 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -189,7 +189,7 @@ static void cpu_initialize() { } } -int cpu_init(struct cpu_t *cpu, int model) { +static int cpu_init(struct cpu_t *cpu, int model) { if (!cpu_initialized) { cpu_initialize(); cpu_initialized = true; diff --git a/src/cpu.h b/src/cpu.h index 6b93dc5..ada06a3 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -87,7 +87,6 @@ uint8_t _cpu_stack_used(struct cpu_t *cpu); uint8_t _cpu_get_status(struct cpu_t *cpu); void _cpu_set_status(struct cpu_t *cpu, uint8_t status); -int cpu_init(struct cpu_t *cpu, int model); struct cpu_t *cpu_create(int model); void cpu_destroy(struct cpu_t *cpu); diff --git a/src/cpu_test.c b/src/cpu_test.c index 416863a..1b012b1 100644 --- a/src/cpu_test.c +++ b/src/cpu_test.c @@ -31,13 +31,12 @@ #include "mem.h" int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) { - struct cpu_t cpu; - cpu_init(&cpu, model); - cpu_add_ram_file(&cpu, 0x0000, rom_path); - cpu_reset(&cpu); - cpu.state.pc = start_addr; + struct cpu_t *cpu = cpu_create(model); + cpu_add_ram_file(cpu, 0x0000, rom_path); + cpu_reset(cpu); + cpu->state.pc = start_addr; - uint16_t last_pc = cpu.state.pc; + uint16_t last_pc = cpu->state.pc; struct timespec start; if (clock_gettime(CLOCK_REALTIME, &start) != 0) { @@ -46,12 +45,12 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) } while (true) { - int ret = cpu_step(&cpu); + int ret = cpu_step(cpu); if (ret < 0) { switch (ret) { case EWM_CPU_ERR_UNIMPLEMENTED_INSTRUCTION: fprintf(stderr, "TEST Unimplemented instruction 0x%.2x at 0x%.4x\n", - mem_get_byte(&cpu, cpu.state.pc), cpu.state.pc); + mem_get_byte(cpu, cpu->state.pc), cpu->state.pc); return -1; default: fprintf(stderr, "TEST Unexpected error %d\n", ret); @@ -62,7 +61,7 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) // End of the tests is at 0x3399. This is hard coded and not // ideal. Is there a better way to detect this? - if (cpu.state.pc == success_addr) { + if (cpu->state.pc == success_addr) { struct timespec now; if (clock_gettime(CLOCK_REALTIME, &now) != 0) { perror("Cannot get time"); @@ -75,10 +74,10 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) uint64_t duration_ms = (now.tv_sec * 1000 + (now.tv_nsec / 1000000)) - (start.tv_sec * 1000 + (start.tv_nsec / 1000000)); double duration = (double) duration_ms / 1000.0; - double mhz = (double) cpu.counter * (1.0 / duration) / 1000000.0; + double mhz = (double) cpu->counter * (1.0 / duration) / 1000000.0; fprintf(stderr, "TEST Success; executed %" PRIu64 " cycles in %.4f at %.4f MHz\n", - cpu.counter, duration, mhz); + cpu->counter, duration, mhz); return 0; } @@ -87,17 +86,17 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) // which we can easily detect by remembering the previous pc and // then looking at what we are about to execute. - if (cpu.state.pc == last_pc) { - uint8_t i = mem_get_byte(&cpu, cpu.state.pc); + if (cpu->state.pc == last_pc) { + uint8_t i = mem_get_byte(cpu, cpu->state.pc); if (i == 0x10 || i == 0x30 || i == 0x50 || i == 0x70 || i == 0x90 || i == 0xb0 || i == 0xd0 || i == 0xf0) { - if (mem_get_byte(&cpu, cpu.state.pc + 1) == 0xfe) { - fprintf(stderr, "TEST Failure at 0x%.4x \n", cpu.state.pc); + if (mem_get_byte(cpu, cpu->state.pc + 1) == 0xfe) { + fprintf(stderr, "TEST Failure at 0x%.4x \n", cpu->state.pc); return -1; } } } - last_pc = cpu.state.pc; + last_pc = cpu->state.pc; } } diff --git a/src/dsk.c b/src/dsk.c index 9a99aa9..8fcef36 100644 --- a/src/dsk.c +++ b/src/dsk.c @@ -413,7 +413,7 @@ static struct ewm_dsk_track_t dsk_convert_track(struct ewm_dsk_t *disk, struct e // Public -int ewm_dsk_init(struct ewm_dsk_t *dsk, struct cpu_t *cpu) { +static int ewm_dsk_init(struct ewm_dsk_t *dsk, struct cpu_t *cpu) { memset(dsk, 0x00, sizeof(struct ewm_dsk_t)); dsk->rom = cpu_add_rom_data(cpu, 0xc600, 0xc6ff, dsk_rom); dsk->rom->description = "rom/dsk/$C600"; diff --git a/src/dsk.h b/src/dsk.h index e0ae176..e86db75 100644 --- a/src/dsk.h +++ b/src/dsk.h @@ -66,7 +66,6 @@ struct ewm_dsk_t { #define EWM_DSK_TYPE_DO (0) #define EWM_DSK_TYPE_PO (1) -int ewm_dsk_init(struct ewm_dsk_t *dsk, struct cpu_t *cpu); struct ewm_dsk_t *ewm_dsk_create(struct cpu_t *cpu); int ewm_dsk_set_disk_data(struct ewm_dsk_t *dsk, uint8_t index, bool readonly, void *data, size_t length, int type); int ewm_dsk_set_disk_file(struct ewm_dsk_t *dsk, uint8_t index, bool readonly, char *path); diff --git a/src/one.c b/src/one.c index ae201b4..24820a4 100644 --- a/src/one.c +++ b/src/one.c @@ -32,15 +32,6 @@ #include "tty.h" #include "one.h" -struct ewm_one_t *ewm_one_create(int model, SDL_Renderer *renderer) { - struct ewm_one_t *one = (struct ewm_one_t*) malloc(sizeof(struct ewm_one_t)); - if (ewm_one_init(one, model, renderer) != 0) { - free(one); - one = NULL; - } - return one; -} - static void ewm_one_pia_callback(struct ewm_pia_t *pia, void *obj, uint8_t ddr, uint8_t v) { struct ewm_one_t *one = (struct ewm_one_t*) obj; if (one->model == EWM_ONE_MODEL_APPLE1) { @@ -49,7 +40,7 @@ static void ewm_one_pia_callback(struct ewm_pia_t *pia, void *obj, uint8_t ddr, ewm_tty_write(one->tty, v); } -int ewm_one_init(struct ewm_one_t *one, int model, SDL_Renderer *renderer) { +static int ewm_one_init(struct ewm_one_t *one, int model, SDL_Renderer *renderer) { memset(one, 0, sizeof(struct ewm_one_t)); one->model = model; switch (model) { @@ -322,3 +313,11 @@ int ewm_one_main(int argc, char **argv) { return 0; } +struct ewm_one_t *ewm_one_create(int model, SDL_Renderer *renderer) { + struct ewm_one_t *one = (struct ewm_one_t*) malloc(sizeof(struct ewm_one_t)); + if (ewm_one_init(one, model, renderer) != 0) { + free(one); + one = NULL; + } + return one; +} diff --git a/src/one.h b/src/one.h index d583326..9876fa4 100644 --- a/src/one.h +++ b/src/one.h @@ -44,7 +44,6 @@ struct ewm_one_t { }; struct ewm_one_t *ewm_one_create(int type, SDL_Renderer *renderer); -int ewm_one_init(struct ewm_one_t *one, int type, SDL_Renderer *renderer); void ewm_one_destroy(struct ewm_one_t *one); int ewm_one_main(int argc, char **argv); diff --git a/src/pia.c b/src/pia.c index 67e2843..bc732ea 100644 --- a/src/pia.c +++ b/src/pia.c @@ -102,6 +102,12 @@ static void pia_write(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr, uint8 } } +static int ewm_pia_init(struct ewm_pia_t *pia, struct cpu_t *cpu) { + memset(pia, 0, sizeof(struct ewm_pia_t)); + cpu_add_iom(cpu, EWM_A1_PIA6820_ADDR, EWM_A1_PIA6820_ADDR + EWM_A1_PIA6820_LENGTH - 1, pia, pia_read, pia_write); + return 0; +} + struct ewm_pia_t *ewm_pia_create(struct cpu_t *cpu) { struct ewm_pia_t *pia = (struct ewm_pia_t*) malloc(sizeof(struct ewm_pia_t)); if (ewm_pia_init(pia, cpu) != 0) { @@ -111,12 +117,6 @@ struct ewm_pia_t *ewm_pia_create(struct cpu_t *cpu) { return pia; } -int ewm_pia_init(struct ewm_pia_t *pia, struct cpu_t *cpu) { - memset(pia, 0, sizeof(struct ewm_pia_t)); - cpu_add_iom(cpu, EWM_A1_PIA6820_ADDR, EWM_A1_PIA6820_ADDR + EWM_A1_PIA6820_LENGTH - 1, pia, pia_read, pia_write); - return 0; -} - void ewm_pia_destroy(struct ewm_pia_t *pia) { free(pia); } diff --git a/src/pia.h b/src/pia.h index 1e6fca8..9df36d2 100644 --- a/src/pia.h +++ b/src/pia.h @@ -57,7 +57,6 @@ struct ewm_pia_t { }; struct ewm_pia_t *ewm_pia_create(struct cpu_t *cpu); -int ewm_pia_init(struct ewm_pia_t *pia, struct cpu_t *cpu); void ewm_pia_destroy(struct ewm_pia_t *pia); void ewm_pia_set_outa(struct ewm_pia_t *pia, uint8_t v); diff --git a/src/scr.c b/src/scr.c index 686681c..8cb3fdf 100644 --- a/src/scr.c +++ b/src/scr.c @@ -285,9 +285,7 @@ inline static void scr_render_hgr_screen(struct scr_t *scr, bool flash) { } } -// TODO This is the only actual API exposed - -int ewm_scr_init(struct scr_t *scr, struct ewm_two_t *two, SDL_Renderer *renderer) { +static int ewm_scr_init(struct scr_t *scr, struct ewm_two_t *two, SDL_Renderer *renderer) { memset(scr, 0x00, sizeof(struct scr_t)); scr->two = two; scr->renderer = renderer; diff --git a/src/scr.h b/src/scr.h index 8c1f44d..0534c55 100644 --- a/src/scr.h +++ b/src/scr.h @@ -43,7 +43,6 @@ struct scr_t { }; struct scr_t *ewm_scr_create(struct ewm_two_t *two, SDL_Renderer *renderer); -int ewm_scr_init(struct scr_t *scr, struct ewm_two_t *two, SDL_Renderer *renderer); void ewm_scr_destroy(struct scr_t *scr); void ewm_scr_update(struct scr_t *scr, int phase, int fps); void ewm_scr_set_color_scheme(struct scr_t *scr, int color_scheme);