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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [sound/] [hex2hex.h] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * This file is a part of configure.c
3
 *
4
 * hex2hex reads an input file in Intel HEX format and produces
5
 * an (unsigned char) array which contains the bytes and writes it to the
6
 * output file using C syntax
7
 */
8
 
9
#define MAX_SIZE (256*1024)
10
#define ABANDON(why) { \
11
                fprintf(stderr, "%s: " why "\n", source); \
12
                return 0; \
13
                }
14
 
15
int loadhex(FILE *inf, unsigned char *buf, char *source)
16
{
17
        int l=0, c, i;
18
 
19
        while ((c=getc(inf))!=EOF)
20
        {
21
                if (c == ':')   /* Sync with beginning of line */
22
                {
23
                        int n, check;
24
                        unsigned char sum;
25
                        int addr;
26
                        int linetype;
27
 
28
                        if (fscanf(inf, "%02x", &n) != 1)
29
                           ABANDON("File format error");
30
                        sum = n;
31
 
32
                        if (fscanf(inf, "%04x", &addr) != 1)
33
                           ABANDON("File format error");
34
                        sum += addr/256;
35
                        sum += addr%256;
36
 
37
                        if (fscanf(inf, "%02x", &linetype) != 1)
38
                           ABANDON("File format error");
39
                        sum += linetype;
40
 
41
                        if (linetype != 0)
42
                           continue;
43
 
44
                        for (i=0;i<n;i++)
45
                        {
46
                                if (fscanf(inf, "%02x", &c) != 1)
47
                                   ABANDON("File format error");
48
                                if (addr >= MAX_SIZE)
49
                                   ABANDON("File too large");
50
                                buf[addr++] = c;
51
                                if (addr > l)
52
                                   l = addr;
53
                                sum += c;
54
                        }
55
 
56
                        if (fscanf(inf, "%02x", &check) != 1)
57
                           ABANDON("File format error");
58
 
59
                        sum = ~sum + 1;
60
                        if (check != sum)
61
                           ABANDON("Line checksum error");
62
                }
63
        }
64
 
65
        return l;
66
}
67
 
68
int hex2hex(char *source, char *target, char *varline)
69
{
70
        FILE *inf, *outf;
71
 
72
        int i,l;
73
        unsigned char buf[MAX_SIZE];
74
 
75
        if ((inf=fopen(source, "r"))==NULL)
76
        {
77
                perror(source);
78
                return 0;
79
        }
80
 
81
        if ((outf=fopen(target, "w"))==NULL)
82
        {
83
                perror(target);
84
                fclose(inf);
85
                return 0;
86
        }
87
 
88
        l=loadhex(inf, buf, source);
89
        if (l<=0)
90
        {
91
           fclose(inf);
92
           fclose(outf);
93
           return l;
94
        }
95
 
96
 
97
        fprintf(outf, "/*\n *\t Computer generated file. Do not edit.\n */\n");
98
        fprintf(outf, "static int %s_len = %d;\n", varline, l);
99
        fprintf(outf, "static unsigned char %s[] = {\n", varline);
100
 
101
        for (i=0;i<l;i++)
102
        {
103
                if (i) fprintf(outf, ",");
104
                if (i && !(i % 16)) fprintf(outf, "\n");
105
                fprintf(outf, "0x%02x", buf[i]);
106
        }
107
 
108
        fprintf(outf, "\n};\n\n");
109
        fclose(inf);
110
        fclose(outf);
111
        return 1;
112
}

powered by: WebSVN 2.1.0

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