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

Subversion Repositories amber

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

Go to most recent revision | 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
char s_clk_count[12], s_from_addr[12], s_to_addr[12], s_r0[12];
97
unsigned int from_addr, to_addr, clk_count;
98
unsigned int x;
99
 
100
unsigned int slen;
101
 
102
if (argc < 3)
103
    {
104
    printf("errer agrc = %d - need jums file name, func_names file name\n", argc);
105
    return 1;
106
    }
107
 
108
jumps_file = fopen (argv[1], "r");
109
func_names_file = fopen (argv[2], "r");
110
 
111
 
112
if (jumps_file==NULL)
113
    {
114
    printf("jumps file open failed\n");
115
    return 1;
116
    }
117
 
118
if (func_names_file==NULL)
119
    {
120
    printf("jumps file open failed\n");
121
    return 1;
122
    }
123
 
124
 
125
/* Read the function names file into a structure */
126
/* reads a line */
127
line_buffer  = (char *) malloc (nbytes + 1);
128
 
129
i=0;
130
while (((bytes_read = getline (&line_buffer, &nbytes, func_names_file)) > 0) && i < NAMES_SIZE )
131
    {
132
    sscanf(line_buffer, "%s %s", a, n);
133
    if ( !conv_hstring(a, &addr) )
134
        {
135
        fprintf(stderr,"ERROR: conv_hstring error in func_names file with i = %d\n", i);
136
        return 1;
137
        }
138
    strcpy(func_names[i].name, n);
139
    func_names[i++].address = addr;
140
    }
141
 
142
 
143
if ( i == NAMES_SIZE )
144
    {
145
    fprintf(stderr, "WARNING: ran out of space in the function array, can only hold %d entries\n", i);
146
    return 1;
147
    }
148
 
149
num_func_names = i;
150
 
151
 
152
 
153
 
154
/* Assign names to jumps */
155
while ( (bytes_read = getline (&line_buffer, &nbytes, jumps_file)) > 0)
156
    {
157
    sscanf(line_buffer, "%s %s %s %s", s_clk_count, s_from_addr, s_to_addr, s_r0);
158
 
159
    if ( !conv_hstring(s_from_addr, &from_addr) )
160
        {
161
        fprintf(stderr,"ERROR: conv_hstring error in jumps file, from_addr, with i = %d\n", i);
162
        return 1;
163
        }
164
 
165
    if ( !conv_hstring(s_to_addr, &to_addr) )
166
        {
167
        fprintf(stderr,"ERROR: conv_hstring error in jumps file, to_addr, with i = %d\n", i);
168
        return 1;
169
        }
170
 
171
    if ( !conv_dstring(s_clk_count, &clk_count) )
172
        {
173
        fprintf(stderr,"ERROR: conv_dstring error in jumps file, r0, with i = %d\n", i);
174
        return 1;
175
        }
176
 
177
    /* find matching function name using binary search */
178
    found       = 0;
179
    exhausted   = 0;
180
    exact       = 0;
181
    start       = 0;
182
    end         = num_func_names-1;
183
 
184
    while ( !found && !exhausted )
185
        {
186
        mid = (start + end) / 2;
187
 
188
        if ( to_addr >= func_names[mid].address &&
189
             to_addr <  func_names[mid+1].address )
190
            {
191
            found = 1;
192
            to_func_num = mid;
193
            if ( to_addr == func_names[mid].address ) exact = 1;
194
            }
195
        else
196
            {
197
            if ( start == end ) exhausted = 1;
198
            else if ( start+1 == end )
199
                start += 1;
200
            else if (to_addr > func_names[mid].address)
201
                start = mid;
202
            else
203
                end = mid;
204
            }
205
        }
206
 
207
    /*
208
       now assign a function to the from_address
209
       this just assigns a function within the range
210
    */
211
    if (found)
212
        {
213
        found =0;
214
 
215
        start = 0;
216
        end = num_func_names-1;
217
 
218
        while (!found)
219
            {
220
            mid = (start + end) / 2;
221
 
222
            if ( from_addr >= func_names[mid].address &&
223
                 from_addr <  func_names[mid+1].address )
224
                {
225
                found = 1;
226
                if ( strcmp ( func_names[mid].name, func_names[to_func_num].name ) )
227
                    {
228
 
229
                    if ( exact ) {
230
                        if ( func_names[to_func_num].address < 0xc0000000 )
231
                            printf("%9d u %s ->", clk_count, func_names[mid].name);
232
                        else
233
                            printf("%9d   %s ->", clk_count, func_names[mid].name);
234
 
235
                        slen = 35 - strlen ( func_names[mid].name );
236
                        if ( slen > 0 ) {
237
                            for (x=0;x<slen;x++) printf(" ");
238
                            }
239
 
240
 
241
                        if ( func_names[to_func_num].address < 0xc0000000 )
242
                            printf("( %s ) %s u\n",
243
                                s_r0,
244
                                func_names[to_func_num].name);
245
                        else
246
                            printf("( %s ) %s\n",
247
                                s_r0,
248
                                func_names[to_func_num].name);
249
 
250
                        }
251
                    else {
252
                        printf("%9d   %s <-",
253
                                clk_count,
254
                                func_names[to_func_num].name);
255
 
256
                        slen = 35 - strlen ( func_names[to_func_num].name );
257
                        if ( slen > 0 ) {
258
                            for (x=0;x<slen;x++) printf(" ");
259
                            }
260
 
261
 
262
                        printf("( %s )\n",
263
                                s_r0);
264
                        }
265
                    }
266
                }
267
            else
268
                {
269
                if ( start == end )
270
                    found = 1;
271
                else if ( start+1 == end )
272
                    start += 1;
273
                else if (from_addr > func_names[mid].address)
274
                    start = mid;
275
                else
276
                    end = mid;
277
                }
278
            }
279
 
280
        }
281
 
282
    }
283
 
284
fclose(func_names_file);
285
fclose(jumps_file);
286
 
287
 
288
}
289
 
290
 

powered by: WebSVN 2.1.0

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