/* * Copyright (c) 1998 Miloslaw Smyk * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Miloslaw Smyk * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * * 18/06/2020 Trev Jackson Added 24bit word support, renamed test.c to d56.c * Removed make_masks and make_masks2. * Added command line options. */ #include #include #include #include #include "5600x_disasm.h" int main(int argc, char **argv) { struct disasm_data dis, *d = &dis; char mem[6]; unsigned char *memory; unsigned char *data; FILE *fh, *fh2; // fpos_t size; int size, advance, c; unsigned int memory_address_option = 0; unsigned int word_length; char word_length_string[2] = "32"; char *input_file; char *output_file = NULL; char *usage = "\nDSP56000 disassembler, usage:\n\n d56 [-w nn] [-m] [-o output-file] source-file\n\nOption Summary\n-w nn where nn is the length of words in source file, available options are -w 24 or -w 32 (default)\n-m outputs without memory address for each opcode\n-o output-file output defaults to stdout, but can be sent to output-file with this option\n\nexample:\n\n d56 -w 32 -o example.a56 example.bin disassembles example.bin containing 32bit words, output to example.a56\n\n"; while((c = getopt(argc, argv, "w:mo:")) != -1) switch (c) { case 'w': { strncpy(word_length_string, optarg, 2); break; } case 'm': memory_address_option++; break; case 'o': output_file = optarg; break; case '?': default: { fprintf(stderr,usage); exit(1); } } input_file = argv[optind++]; if(input_file == NULL) { fprintf(stderr, usage); exit(1); } word_length = (((strncmp(word_length_string, "24",2))==0) ? 24 : 32); if (!(output_file == NULL)) { if(!(fh2 = fopen(output_file, "w"))) output_file = NULL; } if(fh = fopen(input_file, "r")) { /* get the code's size */ if(fseek(fh, 0, SEEK_END)) exit(1); else { size = ftell(fh); rewind(fh); } if(memory = data = malloc(size)) { /* read the entire file into memory */ if(fread(data, size, 1, fh) == 1) { /* important part begins here */ while((memory - data) < size) { /* The library needs 24-bit words, so we have to throw away ** upper 8 bits of every instruction's code. Since disassembly ** processes up to two words per step, we need to prepare ** two words. */ memcpy(mem, memory + (int)((word_length/8)-3), 3); memcpy(mem + 3, memory + (int)((word_length/4)-3), 3); /* tell the library where is the data to disassemble */ d->memory = mem; /* call the line-disassembly routine and note how many ** words to advance forwards */ advance = disassemble_opcode(d); /* display disassembled line */ if(output_file == NULL) { if (!(memory_address_option)) printf("%04x:\t", ((memory - data) / (word_length/8))); printf("%s\n", d->line_buf); } else { if (!(memory_address_option)) fprintf(fh2,"%04x:\t", ((memory - data) / (word_length/8))); fprintf(fh2,"%s\n", d->line_buf); } /* advance code pointer */ memory += advance * (word_length/8); } } free(data); } else exit(10); fclose(fh); if (!(output_file == NULL)) fclose(fh2); } }