OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [tools/] [amber-func-jumps.c] - Blame information for rev 61

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  amber-func-jumps.c                                          //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Creates a little database of all the function names and     //
10
//  addresses is a given disassembly file and then uses it      //
11
//  to list out the jumps in the Amber disassembly log file.    //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Conor Santifort, csantifort.amber@gmail.com           //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
----------------------------------------------------------------*/
42
 
43
#include <stdio.h>
44
#include <stdlib.h> 
45
#include <string.h> 
46
#define NAMES_SIZE 20000
47
 
48
struct func_name
49
{
50
    char name[48];
51
    unsigned int address;
52
};
53
 
54
int conv_hstring ( char *string, unsigned int * addr)
55
{
56
int pos = 0;
57
*addr = 0;
58
 
59
while (((string[pos] >= '0' && string[pos] <= '9') ||
60
       (string[pos] >= 'a' && string[pos] <= 'f')) && pos < 9) {
61
    if (string[pos] >= '0' && string[pos] <= '9')
62
        *addr =  (*addr << 4) + ( string[pos++] - '0' );
63
    else
64
        *addr =  (*addr << 4) + ( string[pos++] - 'a' ) + 10;
65
    }
66
 
67
return pos;
68
}
69
 
70
 
71
unsigned int conv_dstring ( char *string, unsigned int * addr)
72
{
73
unsigned int pos = 0;
74
*addr = 0;
75
 
76
while (((string[pos] >= '0' && string[pos] <= '9')) && pos < 9) {
77
    *addr =  (*addr * 10) + ( string[pos++] - '0' );
78
    }
79
 
80
return pos;
81
}
82
 
83
 
84
int main (int argc, char **argv)
85
{
86
FILE *jumps_file;
87
FILE *func_names_file;
88
int i, num_func_names, to_func_num, found, exhausted, exact, start, mid, end;
89
int bytes_read;
90
size_t nbytes = 100;
91
char *line_buffer;
92
unsigned int addr;
93
 
94
struct func_name func_names [NAMES_SIZE];
95
char a[12], n[48];
96 61 csantifort
char s_clk_count[12], s_from_addr[12], s_to_addr[12], s_r0[12], s_r1[12];
97 2 csantifort
unsigned int from_addr, to_addr, clk_count;
98
unsigned int x;
99 61 csantifort
char current_func_name [48] = "none";
100 2 csantifort
 
101
unsigned int slen;
102
 
103
if (argc < 3)
104
    {
105
    printf("errer agrc = %d - need jums file name, func_names file name\n", argc);
106
    return 1;
107
    }
108
 
109
jumps_file = fopen (argv[1], "r");
110
func_names_file = fopen (argv[2], "r");
111
 
112
 
113
if (jumps_file==NULL)
114
    {
115
    printf("jumps file open failed\n");
116
    return 1;
117
    }
118
 
119
if (func_names_file==NULL)
120
    {
121
    printf("jumps file open failed\n");
122
    return 1;
123
    }
124
 
125
 
126
/* Read the function names file into a structure */
127
/* reads a line */
128
line_buffer  = (char *) malloc (nbytes + 1);
129
 
130
i=0;
131
while (((bytes_read = getline (&line_buffer, &nbytes, func_names_file)) > 0) && i < NAMES_SIZE )
132
    {
133
    sscanf(line_buffer, "%s %s", a, n);
134
    if ( !conv_hstring(a, &addr) )
135
        {
136
        fprintf(stderr,"ERROR: conv_hstring error in func_names file with i = %d\n", i);
137
        return 1;
138
        }
139
    strcpy(func_names[i].name, n);
140
    func_names[i++].address = addr;
141
    }
142
 
143
 
144
if ( i == NAMES_SIZE )
145
    {
146
    fprintf(stderr, "WARNING: ran out of space in the function array, can only hold %d entries\n", i);
147
    return 1;
148
    }
149
 
150
num_func_names = i;
151
 
152
 
153
 
154
 
155
/* Assign names to jumps */
156
while ( (bytes_read = getline (&line_buffer, &nbytes, jumps_file)) > 0)
157
    {
158 61 csantifort
    sscanf(line_buffer, "%s %s %s %s %s", s_clk_count, s_from_addr, s_to_addr, s_r0, s_r1);
159 2 csantifort
 
160
    if ( !conv_hstring(s_from_addr, &from_addr) )
161
        {
162
        fprintf(stderr,"ERROR: conv_hstring error in jumps file, from_addr, with i = %d\n", i);
163
        return 1;
164
        }
165
 
166
    if ( !conv_hstring(s_to_addr, &to_addr) )
167
        {
168
        fprintf(stderr,"ERROR: conv_hstring error in jumps file, to_addr, with i = %d\n", i);
169
        return 1;
170
        }
171
 
172
    if ( !conv_dstring(s_clk_count, &clk_count) )
173
        {
174
        fprintf(stderr,"ERROR: conv_dstring error in jumps file, r0, with i = %d\n", i);
175
        return 1;
176
        }
177
 
178
    /* find matching function name using binary search */
179
    found       = 0;
180
    exhausted   = 0;
181
    exact       = 0;
182
    start       = 0;
183
    end         = num_func_names-1;
184
 
185
    while ( !found && !exhausted )
186
        {
187
        mid = (start + end) / 2;
188
 
189 61 csantifort
        if (  to_addr >= func_names[mid].address &&
190
             (to_addr <  func_names[mid+1].address || mid == end) )
191 2 csantifort
            {
192
            found = 1;
193
            to_func_num = mid;
194
            if ( to_addr == func_names[mid].address ) exact = 1;
195
            }
196
        else
197
            {
198
            if ( start == end ) exhausted = 1;
199
            else if ( start+1 == end )
200
                start += 1;
201
            else if (to_addr > func_names[mid].address)
202
                start = mid;
203
            else
204
                end = mid;
205
            }
206
        }
207 61 csantifort
 
208
 
209
    if (!found)
210
        fprintf(stderr,"WARNING: to_addr 0x%08x not found\n", to_addr);
211
 
212 2 csantifort
    /*
213
       now assign a function to the from_address
214
       this just assigns a function within the range
215
    */
216
    if (found)
217
        {
218
        found =0;
219
 
220
        start = 0;
221
        end = num_func_names-1;
222
 
223
        while (!found)
224
            {
225
            mid = (start + end) / 2;
226
 
227 61 csantifort
            if (  from_addr >= func_names[mid].address &&
228
                 (from_addr <  func_names[mid+1].address || mid == end) )
229 2 csantifort
                {
230
                found = 1;
231
                if ( strcmp ( func_names[mid].name, func_names[to_func_num].name ) )
232
                    {
233
 
234
                    if ( exact ) {
235 61 csantifort
                        printf("%9d %s ->", clk_count, func_names[mid].name);
236 2 csantifort
 
237
                        slen = 35 - strlen ( func_names[mid].name );
238
                        if ( slen > 0 ) {
239
                            for (x=0;x<slen;x++) printf(" ");
240
                            }
241
 
242 61 csantifort
                        printf("( r0 %s, r1 %s ) %s\n",
243
                            s_r0,
244
                            s_r1,
245
                            func_names[to_func_num].name);
246 2 csantifort
 
247 61 csantifort
                        strcpy(current_func_name, func_names[to_func_num].name);
248 2 csantifort
 
249
                        }
250 61 csantifort
                    else if ( strcmp(func_names[to_func_num].name, current_func_name)) {
251
                        printf("%9d %s <-",
252 2 csantifort
                                clk_count,
253
                                func_names[to_func_num].name);
254
 
255
                        slen = 35 - strlen ( func_names[to_func_num].name );
256
                        if ( slen > 0 ) {
257
                            for (x=0;x<slen;x++) printf(" ");
258
                            }
259
 
260
 
261 61 csantifort
                        printf("( r0 %s, r1 %s )\n",
262
                                s_r0, s_r1);
263 2 csantifort
                        }
264
                    }
265
                }
266
            else
267
                {
268
                if ( start == end )
269
                    found = 1;
270
                else if ( start+1 == end )
271
                    start += 1;
272
                else if (from_addr > func_names[mid].address)
273
                    start = mid;
274
                else
275
                    end = mid;
276
                }
277
            }
278
 
279
        }
280
 
281
    }
282
 
283
fclose(func_names_file);
284
fclose(jumps_file);
285
 
286
 
287
}
288
 
289
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.