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 19

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

powered by: WebSVN 2.1.0

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