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/] [ihex2bin/] [bin2ihex.c] - Blame information for rev 25

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 25 alirezamon
/*
2
 * bin2ihex.c: Read binary data, output in Intel HEX format.
3
 *
4
 * By default reads from stdin and writes to stdout. Input and
5
 * output files can be specified with arguments `-i` and `-o`,
6
 * respectively. Initial address offset can be set with option
7
 * `-a` (also, `-a 0` forces output of the initial offset even
8
 * though it is the default zero).
9
 *
10
 * Copyright (c) 2013-2015 Kimmo Kulovesi, http://arkku.com
11
 * Provided with absolutely no warranty, use at your own risk only.
12
 * Distribute freely, mark modified copies as such.
13
 */
14
 
15
#include "kk_ihex_write.h"
16
#include <stdbool.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <errno.h>
20
 
21
#ifdef IHEX_EXTERNAL_WRITE_BUFFER
22
char *ihex_write_buffer = NULL;
23
#endif
24
 
25
//#define IHEX_WRITE_INITIAL_EXTENDED_ADDRESS_RECORD
26
 
27
static FILE *outfile;
28
 
29
int
30
main (int argc, char *argv[]) {
31
    struct ihex_state ihex;
32
    FILE *infile = stdin;
33
    ihex_address_t initial_address = 0;
34
    bool write_initial_address = 0;
35
    bool debug_enabled = 0;
36
    ihex_count_t count;
37
    uint8_t buf[1024];
38
 
39
    outfile = stdout;
40
 
41
    // spaghetti parser of args: -o outfile -i infile -a initial_address
42
    while (--argc) {
43
        char *arg = *(++argv);
44
        if (arg[0] == '-' && arg[1] && arg[2] == '\0') {
45
            switch (arg[1]) {
46
            case 'a':
47
                if (--argc == 0) {
48
                    goto invalid_argument;
49
                }
50
                ++argv;
51
                errno = 0;
52
                initial_address = (ihex_address_t) strtoul(*argv, &arg, 0);
53
                if (errno || arg == *argv) {
54
                    errno = errno ? errno : EINVAL;
55
                    goto argument_error;
56
                }
57
                write_initial_address = 1;
58
                break;
59
            case 'i':
60
                if (--argc == 0) {
61
                    goto invalid_argument;
62
                }
63
                ++argv;
64
                if (!(infile = fopen(*argv, "rb"))) {
65
                    goto argument_error;
66
                }
67
                break;
68
            case 'o':
69
                if (--argc == 0) {
70
                    goto invalid_argument;
71
                }
72
                ++argv;
73
                if (!(outfile = fopen(*argv, "w"))) {
74
                    goto argument_error;
75
                }
76
                break;
77
            case 'v':
78
                debug_enabled = 1;
79
                break;
80
            case 'h':
81
            case '?':
82
                arg = NULL;
83
                goto usage;
84
            default:
85
                goto invalid_argument;
86
            }
87
            continue;
88
        }
89
invalid_argument:
90
        (void) fprintf(stderr, "Invalid argument: %s\n", arg);
91
usage:
92
        (void) fprintf(stderr, "kk_ihex " KK_IHEX_VERSION
93
                               " - Copyright (c) 2013-2015 Kimmo Kulovesi\n");
94
        (void) fprintf(stderr, "Usage: bin2ihex [-a <address_offset>]"
95
                               " [-o <out.hex>] [-i <in.bin>] [-v]\n");
96
        return arg ? EXIT_FAILURE : EXIT_SUCCESS;
97
argument_error:
98
        perror(*argv);
99
        return EXIT_FAILURE;
100
    }
101
 
102
    {
103
#ifdef IHEX_EXTERNAL_WRITE_BUFFER
104
        // How to provide an external write buffer with limited duration:
105
        char buffer[IHEX_WRITE_BUFFER_LENGTH];
106
        ihex_write_buffer = buffer;
107
#endif
108
        ihex_init(&ihex);
109
        ihex_write_at_address(&ihex, initial_address);
110
        if (write_initial_address) {
111
            if (debug_enabled) {
112
                (void) fprintf(stderr, "Address offset: 0x%lx\n",
113
                        (unsigned long) ihex.address);
114
            }
115
            ihex.flags |= IHEX_FLAG_ADDRESS_OVERFLOW;
116
        }
117
        while ((count = (ihex_count_t) fread(buf, 1, sizeof(buf), infile))) {
118
            ihex_write_bytes(&ihex, buf, count);
119
        }
120
        ihex_end_write(&ihex);
121
    }
122
 
123
    if (outfile != stdout) {
124
        (void) fclose(outfile);
125
    }
126
    if (infile != stdout) {
127
        (void) fclose(infile);
128
    }
129
 
130
    if (debug_enabled) {
131
        (void) fprintf(stderr, "%lu bytes read\n",
132
                (unsigned long) ihex.address - initial_address);
133
    }
134
 
135
    return EXIT_SUCCESS;
136
}
137
 
138
#pragma clang diagnostic ignored "-Wunused-parameter"
139
 
140
void
141
ihex_flush_buffer(struct ihex_state *ihex, char *buffer, char *eptr) {
142
    *eptr = '\0';
143
    (void) fputs(buffer, outfile);
144
}

powered by: WebSVN 2.1.0

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