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

Subversion Repositories aor3000

[/] [aor3000/] [trunk/] [syn/] [soc/] [firmware/] [hex2mif.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * This file is subject to the terms and conditions of the BSD License. See
3
 * the file "LICENSE" in the main directory of this archive for more details.
4
 *
5
 * Copyright (C) 2014 Aleksander Osman
6
 */
7
 
8
/* This is a very simple converter from a ihex file generated by the GNU
9
 * toolchain to a Altera MIF file.
10
 */
11
 
12
#include <cstdio>
13
#include <cstring>
14
#include <cstdlib>
15
 
16
int main() {
17
    char line[256];
18
 
19
    int depth = 1024;
20
 
21
    printf("WIDTH=32;\n");
22
    printf("DEPTH=%d;\n", depth);
23
    printf("ADDRESS_RADIX=HEX;\n");
24
    printf("DATA_RADIX=HEX;\n");
25
    printf("CONTENT BEGIN\n");
26
 
27
    int address = 0;
28
    while(1) {
29
        char *ret_ptr = fgets(line, sizeof(line), stdin);
30
        if(ret_ptr == NULL) break;
31
 
32
        int len = strlen(line);
33
        if(len > 0 && line[0] == ':') { //hex line
34
            int hex_len, type;
35
            sscanf(line+1, "%02x", &hex_len);
36
            sscanf(line+7, "%02x", &type);
37
 
38
            if(type == 0) { //data type
39
                if((hex_len % 4) != 0) {
40
                    fprintf(stderr, "Error: data entry size not multiple of 4.\n");
41
                    return -1;
42
                }
43
                for(int i=0; i<hex_len/4; i++) {
44
                    unsigned int val;
45
                    sscanf(line+(9+i*8), "%08x", &val);
46
                    val = ((val >> 24) & 0xFF) | ((val >> 8) & 0xFF00) | ((val << 8) & 0xFF0000) | ((val << 24) & 0xFF000000);
47
                    printf("%03x : %08x;\n", address++, val);
48
                }
49
                while((hex_len/4) < 4) {
50
                    printf("%03x : %08x;\n", address++, 0);
51
                    hex_len += 4;
52
                }
53
            }
54
        }
55
    }
56
    while(address < depth) printf("%03x : %08x;\n", address++, 0);
57
    printf("END;\n");
58
    return 0;
59
}
60
 

powered by: WebSVN 2.1.0

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