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

Subversion Repositories common

[/] [common/] [trunk/] [220_convert_hex2ver_pli.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 bbeaver
#include <stdio.h>
2
#include <ctype.h>
3
#include <string.h>
4
#include "veriuser.h"
5
#include "acc_user.h"
6
 
7
#define TRUE 1 
8
#define FALSE 0 
9
#define MAX_BUFFER_SZ   2048
10
#define MAX_NAME_SZ             128
11
#define OUT_FILE_EXT    ".ver"
12
#define COLON                   ':'
13
#define OFFSET                  9
14
#define H10                             0x10L
15
#define AWORD                   8
16
#define WORDS_PER_LINE  8
17
#define MASK15                  0x000000FFL
18
#define EXT_STR                 ".ver"
19
 
20
typedef enum {
21
        OK              = 0,
22
        WARNING = 1,
23
        ERROR   = 2
24
}STATUS;
25
static int line_no = 0;
26
/********************************************************************
27
 * char* trimAlteraExt(char* oldName, char* newName)
28
 * oldName : original file name.
29
 * newName : new file name which doesnt have file extension.
30
 *
31
 * This function trims the file extension
32
 * Looks for first "." and trims the file name afterwords.
33
********************************************************************/
34
 
35
char* trimAlteraExt(oldName, newName)
36
char* oldName;
37
char* newName;
38
{
39
    char tempStr[MAX_BUFFER_SZ];
40
    char* tempPtr=NULL;
41
 
42
    newName[0]='\0';
43
    if(oldName[0]=='\0')
44
        return NULL;
45
    strcpy(tempStr, oldName);
46
    if(tempPtr = strstr(tempStr, "."))
47
    {
48
        *tempPtr = '\0';
49
    }
50
    strcpy(newName, tempStr);
51
    return newName;
52
}
53
 
54
/****************************************************************/
55
int display_msg(status, str, data_file)
56
STATUS status;
57
char *str;
58
char *data_file;
59
{
60
        switch(status)
61
        {
62
                case WARNING:
63
                         printf("WARNING: %s, %s\n\n", data_file, str);
64
                        break;
65
                case ERROR:
66
                         printf("ERROR:%s, line %d, %s\n\n", data_file, line_no, str);
67
                        break;
68
                default:
69
                        break;
70
        }
71
        return(TRUE);
72
}
73
/****************************************************************
74
  char *ltrim(str)
75
  char *str;
76
  Deletes leading blanks in string 'str'
77
****************************************************************/
78
char *ltrim(str)
79
char *str;
80
{
81
        int i = 0,j = 0;
82
 
83
        /* Illeagal application. Returns NULL. */
84
        if (str == 0) return(0);
85
        /* Deletes leading blanks */
86
        while (*(str + i) == ' ' || *(str + i) == '\n' || *(str + i) == '\t' || *(str + i) == '\b') i++;
87
        for (; *(str + i) != '\0'; i++,j++) *(str + j) = *(str + i);
88
        /* Appends a NULL character to the end of the string */
89
        *(str + j) = '\0';
90
 
91
        return(str);
92
}
93
 
94
/****************************************************************
95
 * Function: write_ver_data
96
 * Description: This function ensures that each char (in hex)
97
 *              represents a nibble of the data.
98
 * For example if (width<5) we need to output only 1 char hex.
99
****************************************************************/
100
void write_ver_data(ofp, width, data)
101
FILE *ofp;
102
int width;
103
char *data;
104
{
105
  int num_nibbles;
106
  int num_hex;
107
 
108
  num_nibbles = (width%4?(width/4 + 1):width/4);
109
  num_hex = num_nibbles/2;
110
 
111
  while (num_hex--)
112
  {
113
    fprintf(ofp, "%c%c", data[0], data[1]);
114
    data +=2;
115
  }
116
  /* NB: we skip the leading '0' in data[0]. */
117
  if (num_nibbles&1) fprintf(ofp, "%c", data[1]);
118
}
119
 
120
/****************************************************************
121
Function: write_data
122
Description: Write ROM data in the verilog format so that
123
        it can be read using readmemh(infile, rom)
124
Format: example data format for the 8 bit data
125
                @1
126
                01 02 03 04 05 06 07 08
127
                @a
128
                0a 0b 0c
129
****************************************************************/
130
int write_data(ofp, nn, aaaa, off_addr, dddd, width)
131
FILE *ofp;
132
long nn, aaaa, off_addr;
133
char *dddd;
134
int width;
135
{
136
        int count, i;
137
    char data[MAX_BUFFER_SZ +1];
138
        int num_hexs, nibbles ;
139
 
140
 
141
        if((width % AWORD) == 0)
142
        {
143
                num_hexs = width/AWORD;
144
        }
145
        else
146
        {
147
                num_hexs = (width/AWORD) + 1;
148
        }
149
 
150
        fprintf(ofp, "@%x\n", aaaa + off_addr);
151
 
152
        nibbles  = num_hexs*2;
153
        count = 1;
154
        while(nn > 0)
155
        {
156
                if(nn >= num_hexs)
157
                {
158
                        strncpy(data, dddd, nibbles);
159
                        data[nibbles] = '\0';
160
                }
161
                else
162
                {
163
                        for(i = 0; i < (num_hexs - nn); i++)
164
                        {
165
                                sprintf(data + i*2, "%s", "00");
166
                        }
167
                        strcat(data + (num_hexs - nn), dddd);
168
                }
169
                write_ver_data(ofp, width, data);
170
                if(count < WORDS_PER_LINE)
171
                {
172
                        count ++;
173
                }
174
                else
175
                {
176
                        fprintf(ofp, "\n");
177
                        count = 1;
178
                }
179
                dddd = dddd + nibbles;
180
                nn -= num_hexs;
181
        }
182
        if(count > 1)
183
                fprintf(ofp, "\n");
184
 
185
 
186
}
187
/****************************************************************/
188
/* Convert Intel-hex format data to verilog format data                 */
189
/*      Intel-hex format        :nnaaaaattddddcc                                                */
190
/****************************************************************/
191
convert_hex2ver()
192
{
193
    char buffer[MAX_BUFFER_SZ +1];
194
        char out_file[MAX_NAME_SZ +1];
195
        char init_filename[MAX_NAME_SZ +1];
196
    char dddd[MAX_BUFFER_SZ +1];
197
        char *in_file, *out_str;
198
 
199
        FILE *ifp, *ofp;
200
        int i ;
201
        int done = FALSE;
202
        int first_rec = FALSE;
203
        int last_rec = FALSE;
204
        int width ;
205
        long off_addr, nn, aaaa, tt, cc, aah, aal, dd, sum ;
206
        handle wrk;
207
        static s_setval_value user_s = {accStringVal};
208
        static s_setval_delay delay ;
209
 
210
        off_addr= nn= aaaa= tt= cc= aah= aal= dd= sum = 0;
211
 
212
        in_file = (char *)tf_getcstringp(1);
213
        width = tf_getp(2);
214
        trimAlteraExt(in_file, out_file);
215
        strcat(out_file, OUT_FILE_EXT);
216
    if ((ifp = fopen(in_file, "r")) == NULL)
217
    {
218
        printf("cannot read %s\n", in_file);
219
        fclose(ifp);
220
                return;
221
    }
222
    if ((ofp = fopen(out_file, "w")) == NULL)
223
    {
224
        printf("cannot write %s\n", out_file);
225
        fclose(ofp);
226
                return;
227
    }
228
        while(!done)
229
        {
230
        if(fgets(buffer, MAX_BUFFER_SZ, ifp) == NULL)
231
                {
232
                        if(!first_rec)
233
                                done = display_msg(WARNING, "Intel-hex data file is empty.", in_file);
234
                        else if(!last_rec)
235
                                done = display_msg(ERROR, "Missing the last record.", in_file);
236
                }
237
                else if(strlen(ltrim(buffer)) == 0)
238
                {
239
                        line_no++;
240
                }
241
                else if(buffer && (buffer[0] == COLON))
242
                {
243
                        line_no++;
244
                        first_rec = TRUE;
245
                        sscanf(buffer+1,"%02x%04x%02x", &nn, &aaaa, &tt);
246
                        if((tt == 2) && (nn != 2) )
247
                        {
248
                                done = display_msg(ERROR, "Invalid data record.", in_file);
249
                        }
250
                        else
251
                        {
252
                        sscanf(buffer+3,"%02x%02x", &aah, &aal);
253
                        sum = nn + aah + aal + tt ;
254
                        for(i = 0; i < nn; i++)
255
                        {
256
                                sscanf(buffer+OFFSET+i*2, "%02x", &dd);
257
                                sprintf(dddd+i*2, "%02x", dd);
258
                                sum += dd;
259
                        }
260
                        sscanf(buffer+OFFSET+i*2, "%02x\n", &cc);
261
 
262
                        switch(tt) {
263
                                case 0x00: /* normal_record */
264
                                        first_rec = TRUE;
265
                                        if(((~sum + 1)& MASK15) == cc)
266
                                        {
267
                                                write_data(ofp, nn, aaaa, off_addr, dddd,width);
268
                                                off_addr = 0;
269
                                        }
270
                                        else
271
                                        {
272
                                                done = display_msg(ERROR, "Invalid checksum.", in_file);
273
                                        }
274
                                break;
275
                                case 0x01: /* last record */
276
                                        last_rec = TRUE;
277
                                        if(((~sum+1)&MASK15) != cc)
278
                                        {
279
                                                display_msg(ERROR, "Invalid checksum.", in_file);
280
                                        }
281
                                        done = TRUE;
282
                                break;
283
                                case 0x02: /* address base record */
284
                                        sscanf(dddd,"%x\n", &off_addr);
285
                                        if(((~sum +1)& MASK15) == cc)
286
                                        {
287
                                                off_addr *=  H10;
288
                                        }
289
                                        else
290
                                        {
291
                                                done = display_msg(ERROR, "Invalid checksum.", in_file);
292
                                        }
293
                                break;
294
                                default:
295
                                        done = display_msg(ERROR, "Unknown record type.", in_file);
296
                                break;
297
                    } /* switch */
298
                        }
299
                }
300
                else
301
                {
302
                        line_no++;
303
                        display_msg(ERROR, "Invalid INTEL HEX record", in_file);
304
                        done = TRUE;
305
                }
306
        }
307
        fclose(ifp);
308
        fclose(ofp);
309
        /* append EXT_STR to the input string and pass it back in arg 1 */
310
        delay.model = accNoDelay;
311
        wrk = acc_handle_tfarg(3);
312
        trimAlteraExt(in_file, init_filename);
313
        strcat(init_filename, EXT_STR);
314
        user_s.value.str = init_filename;
315
        acc_set_value(wrk, &user_s, &delay);
316
 
317
        return(0);
318
}
319
 

powered by: WebSVN 2.1.0

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