OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [ihex2mif/] [ihex.c] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 alirezamon
/* Intel HEX read/write functions, Paul Stoffregen, paul@ece.orst.edu */
2
/* This code is in the public domain.  Please retain my name and */
3
/* email address in distributed copies, and let me know about any bugs */
4
 
5
/* I, Paul Stoffregen, give no warranty, expressed or implied for */
6
/* this software and/or documentation provided, including, without */
7
/* limitation, warranty of merchantability and fitness for a */
8
/* particular purpose. */
9
 
10
 
11
#include <stdio.h>
12
#include <string.h>
13
#include <stdlib.h>
14 25 alirezamon
#include <ctype.h>
15 19 alirezamon
 
16 25 alirezamon
#ifndef MAX_MEMORY_SIZE
17
        #define MAX_MEMORY_SIZE         65535
18
#endif
19
 
20 19 alirezamon
/* some ansi prototypes.. maybe ought to make a .h file */
21
 
22
/* this loads an intel hex file into the memory[] array */
23 45 alirezamon
int load_file(char *filename);
24 19 alirezamon
 
25
/* this writes a part of memory[] to an intel hex file */
26
void save_file(char *command);
27
 
28
/* this is used by load_file to get each line of intex hex */
29
int parse_hex_line(char *theline, int bytes[], int *addr, int *num, int *code);
30
 
31
/* this does the dirty work of writing an intel hex file */
32
/* caution, static buffering is used, so it is necessary */
33
/* to call it with end=1 when finsihed to flush the buffer */
34
/* and close the file */
35
void hexout(FILE *fhex, int byte, int memory_location, int end);
36
 
37
 
38 25 alirezamon
extern int      memory[MAX_MEMORY_SIZE+1];              /* the memory is global */
39 19 alirezamon
 
40
/* parses a line of intel hex code, stores the data in bytes[] */
41
/* and the beginning address in addr, and returns a 1 if the */
42
/* line was valid, or a 0 if an error occured.  The variable */
43
/* num gets the number of bytes that were stored into bytes[] */
44
 
45
int parse_hex_line(theline, bytes, addr, num, code)
46
char *theline;
47
int *addr, *num, *code, bytes[];
48
{
49
        int sum, len, cksum;
50
        char *ptr;
51
 
52
        *num = 0;
53
        if (theline[0] != ':') return 0;
54
        if (strlen(theline) < 11) return 0;
55
        ptr = theline+1;
56
        if (!sscanf(ptr, "%02x", &len)) return 0;
57
        ptr += 2;
58
        if ( strlen(theline) < (11 + (len * 2)) ) return 0;
59
        if (!sscanf(ptr, "%04x", addr)) return 0;
60
        ptr += 4;
61
          /* printf("Line: length=%d Addr=%d\n", len, *addr); */
62
        if (!sscanf(ptr, "%02x", code)) return 0;
63
        ptr += 2;
64
        sum = (len & 255) + ((*addr >> 8) & 255) + (*addr & 255) + (*code & 255);
65
        while(*num != len) {
66
                if (!sscanf(ptr, "%02x", &bytes[*num])) return 0;
67
                ptr += 2;
68
                sum += bytes[*num] & 255;
69
                (*num)++;
70
                if (*num >= 256) return 0;
71
        }
72
        if (!sscanf(ptr, "%02x", &cksum)) return 0;
73
        if ( ((sum & 255) + (cksum & 255)) & 255 ) return 0; /* checksum error */
74
        return 1;
75
}
76
 
77
/* loads an intel hex file into the global memory[] array */
78
/* filename is a string of the file to be opened */
79
 
80 45 alirezamon
int load_file(filename)
81 19 alirezamon
char *filename;
82
{
83
        char line[1000];
84
        FILE *fin;
85
        int addr, n, status, bytes[256];
86
        int i, total=0, lineno=1;
87
        int minaddr=65536, maxaddr=0;
88
 
89
        if (strlen(filename) == 0) {
90
                printf("   Can't load a file without the filename.");
91
                printf("  '?' for help\n");
92 45 alirezamon
                return 0;
93 19 alirezamon
        }
94
        fin = fopen(filename, "r");
95
        if (fin == NULL) {
96
                printf("   Can't open file '%s' for reading.\n", filename);
97
                //return;
98
                exit(1);
99
        }
100
        while (!feof(fin) && !ferror(fin)) {
101
                line[0] = '\0';
102
                fgets(line, 1000, fin);
103
                if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
104
                if (line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0';
105
                if (parse_hex_line(line, bytes, &addr, &n, &status)) {
106
                        if (status == 0) {  /* data */
107
                                for(i=0; i<=(n-1); i++) {
108
                                        memory[addr] = bytes[i] & 255;
109
                                        total++;
110
                                        if (addr < minaddr) minaddr = addr;
111
                                        if (addr > maxaddr) maxaddr = addr;
112
                                        addr++;
113
                                }
114
                        }
115
                        if (status == 1) {  /* end of file */
116
                                fclose(fin);
117
                                printf("   Loaded %d bytes between:", total);
118
                                printf(" %04X to %04X\n", minaddr, maxaddr);
119 45 alirezamon
 
120
                                return maxaddr;
121 19 alirezamon
                        }
122
                        if (status == 2) ;  /* begin of file */
123
                } else {
124
                        printf("   Error: '%s', line: %d\n", filename, lineno);
125
                }
126
                lineno++;
127
        }
128 45 alirezamon
        return maxaddr;//it should not reach here
129 19 alirezamon
}
130
 
131
 
132
/* the command string format is "S begin end filename" where */
133
/* "begin" and "end" are the locations to dump to the intel */
134
/* hex file, specified in hexidecimal. */
135
 
136
void save_file(command)
137
char *command;
138
{
139
        int begin, end, addr;
140
        char *ptr, filename[200];
141
        FILE *fhex;
142
 
143
        ptr = command+1;
144
        while (isspace(*ptr)) ptr++;
145
        if (*ptr == '\0') {
146
                printf("   Must specify address range and filename\n");
147
                return;
148
                }
149
        if (sscanf(ptr, "%x%x%s", &begin, &end, filename) < 3) {
150
                printf("   Invalid addresses or filename,\n");
151
                printf("    usage: S begin_addr end_addr filename\n");
152
                printf("    the addresses must be hexidecimal format\n");
153
                return;
154
        }
155 25 alirezamon
        begin &= MAX_MEMORY_SIZE;
156
        end &= MAX_MEMORY_SIZE;
157 19 alirezamon
        if (begin > end) {
158
                printf("   Begin address must be less than end address.\n");
159
                return;
160
        }
161
        fhex = fopen(filename, "w");
162
        if (fhex == NULL) {
163
                printf("   Can't open '%s' for writing.\n", filename);
164
                return;
165
        }
166
        for (addr=begin; addr <= end; addr++)
167
                hexout(fhex, memory[addr], addr, 0);
168
        hexout(fhex, 0, 0, 1);
169
        printf("Memory %04X to %04X written to '%s'\n", begin, end, filename);
170
}
171
 
172
 
173
/* produce intel hex file output... call this routine with */
174
/* each byte to output and it's memory location.  The file */
175
/* pointer fhex must have been opened for writing.  After */
176
/* all data is written, call with end=1 (normally set to 0) */
177
/* so it will flush the data from its static buffer */
178
 
179
#define MAXHEXLINE 32   /* the maximum number of bytes to put in one line */
180
 
181
void hexout(fhex, byte, memory_location, end)
182
FILE *fhex;  /* the file to put intel hex into */
183
int byte, memory_location, end;
184
{
185
        static int byte_buffer[MAXHEXLINE];
186
        static int last_mem, buffer_pos, buffer_addr;
187
        static int writing_in_progress=0;
188
        register int i, sum;
189
 
190
        if (!writing_in_progress) {
191
                /* initial condition setup */
192
                last_mem = memory_location-1;
193
                buffer_pos = 0;
194
                buffer_addr = memory_location;
195
                writing_in_progress = 1;
196
                }
197
 
198
        if ( (memory_location != (last_mem+1)) || (buffer_pos >= MAXHEXLINE) \
199
         || ((end) && (buffer_pos > 0)) ) {
200
                /* it's time to dump the buffer to a line in the file */
201
                fprintf(fhex, ":%02X%04X00", buffer_pos, buffer_addr);
202
                sum = buffer_pos + ((buffer_addr>>8)&255) + (buffer_addr&255);
203
                for (i=0; i < buffer_pos; i++) {
204
                        fprintf(fhex, "%02X", byte_buffer[i]&255);
205
                        sum += byte_buffer[i]&255;
206
                }
207
                fprintf(fhex, "%02X\n", (-sum)&255);
208
                buffer_addr = memory_location;
209
                buffer_pos = 0;
210
        }
211
 
212
        if (end) {
213
                fprintf(fhex, ":00000001FF\n");  /* end of file marker */
214
                fclose(fhex);
215
                writing_in_progress = 0;
216
        }
217
 
218
        last_mem = memory_location;
219
        byte_buffer[buffer_pos] = byte & 255;
220
        buffer_pos++;
221
}
222
 
223
 

powered by: WebSVN 2.1.0

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