1 // license:BSD-3-Clause
2 // copyright-holders:Tony La Porta
3 	/**************************************************************************\
4 	*               Texas Instruments TMS32010 DSP Disassembler                *
5 	*                                                                          *
6 	*                 Copyright Tony La Porta                                  *
7 	*                                                                          *
8 	*      Notes : Data is expected to be read from source file as MSB first.  *
9 	*              This is a word based microcontroller, with addressing       *
10 	*                  architecture based on the Harvard addressing scheme.    *
11 	*                                                                          *
12 	\**************************************************************************/
13 
14 #include <cstdio>
15 #include <cstring>
16 
17 #include "32010dsm.c"
18 
19 
20 unsigned char *Buffer;
21 
22 
main(int argc,char * argv[])23 int main(int argc,char *argv[])
24 {
25 	int  length=0, length_to_dump=0, offset=0, disasm_words=0;
26 	int  filelength=0, bytes_read;
27 	int  Counter=0;
28 
29 	FILE *F;
30 	char *String_Output;
31 
32 	if(argc<2)
33 	{
34 		printf("\n");
35 		printf("TMS32010 Disassembler 1.1 by Tony La Porta (C)1999-2002+\n\n");
36 		printf("Usage: dis32010 <input-file> [ <start-addr> [ <num-of-addr> ] ]\n");
37 		printf("                <input-file>  source file data must be MSB first\n");
38 		printf("                <start-addr>  starting address to disassemble from (decimal)\n");
39 		printf("                <num-of-addr> number of addresses to disassemble (decimal)\n");
40 		printf("                              Precede values with 0x if HEX values preffered\n");
41 		exit(1);
42 	}
43 
44 	if(!(F=fopen(argv[1],"rb")))
45 	{
46 		printf("\n%s: Can't open file %s\n",argv[0],argv[1]);
47 		exit(2);
48 	}
49 	argv++; argc--;
50 	if (argv[1])
51 	{
52 		offset = strtol(argv[1],nullptr,0);
53 		argv++; argc--;
54 	}
55 	if (argv[1])
56 	{
57 		length = strtol(argv[1],nullptr,0);
58 		argv++; argc--;
59 	}
60 
61 	fseek(F,0, SEEK_END);
62 	filelength = ftell(F);
63 
64 	length *= 2;
65 
66 	if ((length > (filelength - (offset*2))) || (length == 0)) length = filelength - (offset*2);
67 	printf("Length=%04Xh(words)  Offset=$%04Xh  filelength=%04Xh(words) %04Xh(bytes)\n",length/2,offset,filelength/2,filelength);
68 	length_to_dump = length;
69 	printf("Starting from %d, dumping %d opcodes (word size)\n",offset,length/2);
70 	Buffer = calloc((filelength+1),sizeof(char));
71 	if (Buffer==nullptr)
72 	{
73 		printf("Out of Memory !!!");
74 		fclose(F);
75 		exit(3);
76 	}
77 	String_Output = calloc(80,sizeof(char));
78 	if (String_Output==nullptr)
79 	{
80 		printf("Out of Memory !!!");
81 		free(Buffer);
82 		fclose(F);
83 		exit(4);
84 	}
85 
86 	if (fseek(F,0,SEEK_SET) != 0)
87 	{
88 		printf("Error seeking to beginning of file\n");
89 		free(String_Output);
90 		free(Buffer);
91 		fclose(F);
92 		exit(5);
93 	}
94 
95 	Counter = offset;
96 	bytes_read = fread(Buffer,sizeof(char),filelength,F);
97 	if (bytes_read >= length)
98 	{
99 		for (; length > 0; length -= (disasm_words*2))
100 		{
101 			int ii;
102 			disasm_words = Dasm32010(String_Output,Counter);
103 			printf("$%04lX: ",Counter);
104 			for (ii = 0; ii < disasm_words; ii++)
105 			{
106 				if (((Counter*2) + ii) > filelength)    /* Past end of length to dump ? */
107 				{
108 					sprintf(String_Output,"???? dw %02.2X%02.2Xh (Past end of disassembly !)",Buffer[((Counter-1)*2)],Buffer[((Counter-1)*2)+1]);
109 				}
110 				else
111 				{
112 					printf("%02.2x%02.2x ",Buffer[(Counter*2)],Buffer[(Counter*2) + 1]);
113 				}
114 				Counter++ ;
115 			}
116 			for (; ii < 4; ii++)
117 			{
118 				printf("   ");
119 			}
120 			printf("\t%s\n",String_Output);
121 		}
122 	}
123 	else
124 	{
125 		printf("ERROR length to dump was %d ", length_to_dump/2);
126 		printf(", but bytes read from file were %d\n", bytes_read/2);
127 		free(String_Output);
128 		free(Buffer);
129 		fclose(F);
130 		exit(7);
131 	}
132 	free(String_Output);
133 	free(Buffer);
134 	fclose(F);
135 	return(0);
136 }
137