// ----------------------------------------------------------------------- void log_log_dasm(unsigned component, unsigned level, int mod, int norm_arg, int short_arg, int16_t n) { int nb = ((log_cycle_sr & 0b0000000000100000) >> 5) * (log_cycle_sr & 0b0000000000001111); emdas_dasm(emd, nb, log_cycle_ic); if (norm_arg) { if (mod) { log_log_cpu(component, level, " %-20s N = 0x%x = %i, MOD = 0x%x = %i", dasm_buf, (uint16_t) n, n, mod, mod); } else { log_log_cpu(component, level, " %-20s N = 0x%x = %i", dasm_buf, (uint16_t) n, n); } } else if (short_arg) { if (mod) { log_log_cpu(component, level, " %-20s T = %i, MOD = 0x%x = %i", dasm_buf, n, mod, mod); } else { log_log_cpu(component, level, " %-20s T = %i", dasm_buf, n); } } else { log_log_cpu(component, level, " %-20s", dasm_buf); } }
// ----------------------------------------------------------------------- int main(int argc, char **argv) { FILE *fi, *fo; int res; int ret = -1; struct emdas *emd = NULL; struct emelf *e = NULL; struct stat sb; res = parse_args(argc, argv); if (res) { // wrong usage, parse_args() prints the error goto cleanup; } if (stat(input_file, &sb) == -1) { fprintf(stderr, "Cannot stat file '%s'.\n", input_file); goto cleanup; } if (sb.st_size > 2*(0x10000-base_addr)) { fprintf(stderr, "Warning: File is bigger than available address space (%li bytes > %i words). Output will be truncated.\n", sb.st_size, 0x10000-base_addr); } fi = fopen(input_file, "r"); if (!fi) { fprintf(stderr, "Cannot open input file '%s'.\n", input_file); goto cleanup; } emd = emdas_create(iset, memget); if (!emd) { fprintf(stderr, "Cannot setup disassembler: %s\n", emdas_get_error(emdas_error)); goto cleanup; } res = emdas_set_tabs(emd, TAB_LABEL, TAB_MNEMO, TAB_ARG, TAB_COMMENT); if (res != EMD_E_OK) { fprintf(stderr, "Cannot set tabulators: %s\n", emdas_get_error(res)); goto cleanup; } res = emdas_set_features(emd, features); if (res) { fprintf(stderr, "Cannot set disassembler features: %s\n", emdas_get_error(res)); goto cleanup; } // try as emelf first e = emelf_load(fi); if (e) { bin_size = e->image_size; mem = e->image; } else { mem = calloc(0x10000, sizeof(uint16_t)); if (!mem) { fclose(fi); fprintf(stderr, "Memory allocation error\n"); goto cleanup; } rewind(fi); bin_size = fread(mem+base_addr, sizeof(uint16_t), 0x10000-base_addr, fi); for (int i=base_addr ; i<base_addr+bin_size ; i++) { mem[i] = ntohs(mem[i]); } } if (bin_size < 0) { fprintf(stderr, "Cannot read input file '%s'.\n", input_file); fclose(fi); goto cleanup; } fclose(fi); if (output_file) { fo = fopen(output_file, "w"); } else { fo = stdout; } if (!fo) { fprintf(stderr, "Cannot open output file '%s' for writing.\n", output_file); goto cleanup; } // print CPU type fprintf(fo, "%*s.cpu%*s%s\n\n", TAB_MNEMO, "", TAB_ARG-TAB_MNEMO-4, "", iset == EMD_ISET_MX16 ? "mx16" : "mera400"); // disassemble and write output if (use_labels) { res = emdas_analyze(emd, 0, base_addr, bin_size); if (res != EMD_E_OK) { fprintf(stderr, "Cannot assign labels: %s\n", emdas_get_error(res)); goto cleanup; } } int ic = base_addr; int words; while (ic < base_addr+bin_size) { words = emdas_dasm(emd, 0, ic); if (words <= 0) { fprintf(stderr, "Disassembly error: %s\n", emdas_get_error(emdas_error)); } fprintf(fo, "%s", emdas_get_buf(emd)); ic += words; } if (output_file) { fclose(fo); } ret = 0; cleanup: if (e) emelf_destroy(e); else free(mem); emdas_destroy(emd); free(output_file); return ret; }