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

Subversion Repositories light8080

[/] [light8080/] [trunk/] [tools/] [ihex2vlog/] [ihex.c] - Blame information for rev 65

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 65 motilito
/* 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
 
13
/* some ansi prototypes.. maybe ought to make a .h file */
14
 
15
/* this loads an intel hex file into the memory[] array */
16
int load_file(char *filename);
17
 
18
/* this writes a part of memory[] to an intel hex file */
19
void save_file(char *command);
20
 
21
/* this is used by load_file to get each line of intex hex */
22
int parse_hex_line(char *theline, int bytes[], int *addr, int *num, int *code);
23
 
24
/* this does the dirty work of writing an intel hex file */
25
/* caution, static buffering is used, so it is necessary */
26
/* to call it with end=1 when finsihed to flush the buffer */
27
/* and close the file */
28
void hexout(FILE *fhex, int byte, int memory_location, int end);
29
 
30
extern int      memory[65536];          /* the memory is global */
31
 
32
/* parses a line of intel hex code, stores the data in bytes[] */
33
/* and the beginning address in addr, and returns a 1 if the */
34
/* line was valid, or a 0 if an error occured.  The variable */
35
/* num gets the number of bytes that were stored into bytes[] */
36
 
37
int parse_hex_line(theline, bytes, addr, num, code)
38
char *theline;
39
int *addr, *num, *code, bytes[];
40
{
41
        int sum, len, cksum;
42
        char *ptr;
43
 
44
        *num = 0;
45
        if (theline[0] != ':') return 0;
46
        if (strlen(theline) < 11) return 0;
47
        ptr = theline+1;
48
        if (!sscanf(ptr, "%02x", &len)) return 0;
49
        ptr += 2;
50
        if ( strlen(theline) < (11 + (len * 2)) ) return 0;
51
        if (!sscanf(ptr, "%04x", addr)) return 0;
52
        ptr += 4;
53
          /* printf("Line: length=%d Addr=%d\n", len, *addr); */
54
        if (!sscanf(ptr, "%02x", code)) return 0;
55
        ptr += 2;
56
        sum = (len & 255) + ((*addr >> 8) & 255) + (*addr & 255) + (*code & 255);
57
        while(*num != len) {
58
                if (!sscanf(ptr, "%02x", &bytes[*num])) return 0;
59
                ptr += 2;
60
                sum += bytes[*num] & 255;
61
                (*num)++;
62
                if (*num >= 256) return 0;
63
        }
64
        if (!sscanf(ptr, "%02x", &cksum)) return 0;
65
        if ( ((sum & 255) + (cksum & 255)) & 255 ) return 0; /* checksum error */
66
        return 1;
67
}
68
 
69
/* loads an intel hex file into the global memory[] array */
70
/* filename is a string of the file to be opened */
71
// the function returns the number of bytes read from the hex file 
72
int load_file (char *filename)
73
{
74
        char line[1000];
75
        FILE *fin;
76
        int addr, n, status, bytes[256];
77
        int i, total=0, lineno=1;
78
        int minaddr=65536, maxaddr=0;
79
        int topaddr=0;
80
 
81
        if (strlen(filename) == 0) {
82
                printf("Can't load a file without the filename.");
83
                printf("  '?' for help\n");
84
                return 0;
85
        }
86
        fin = fopen(filename, "r");
87
        if (fin == NULL) {
88
                printf("Can't open file '%s' for reading.\n", filename);
89
                return 0;
90
        }
91
 
92
        while (!feof(fin) && !ferror(fin)) {
93
                line[0] = '\0';
94
                fgets(line, 1000, fin);
95
                if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
96
                if (line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0';
97
                if (parse_hex_line(line, bytes, &addr, &n, &status)) {
98
                        if (status == 0) {  /* data */
99
                                for(i=0; i<=(n-1); i++) {
100
                                        memory[addr] = bytes[i] & 255;
101
                                        total++;
102
                                        if (addr < minaddr) minaddr = addr;
103
                                        if (addr > maxaddr) maxaddr = addr;
104
                                        addr++;
105
                                }
106
                                if (addr >= topaddr) {
107
                                        topaddr = 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 (topaddr + 1);
115
                        }
116
                        if (status == 2) ;  /* begin of file */
117
                } else {
118
                        printf("   Error: '%s', line: %d\n", filename, lineno);
119
                }
120
                lineno++;
121
        }
122
        return (topaddr + 1);
123
}
124
 
125
 
126
/* the command string format is "S begin end filename" where */
127
/* "begin" and "end" are the locations to dump to the intel */
128
/* hex file, specified in hexidecimal. */
129
 
130
void save_file(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.