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

Subversion Repositories y80e

[/] [y80e/] [trunk/] [asm/] [ihex2vm.cpp] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 bsa
#include <iostream>
2
#include <fstream>
3
#include <iomanip>
4
#include <sstream>
5
#include <string>
6
#include <vector>
7
#include <cstring>
8
#include <cerrno>
9
 
10
int main(int argc, char *argv[])
11
{
12
        std::istream *src = &std::cin;
13
        std::ostream *dst = &std::cout;
14
        std::cerr << "Intel HEX to Verilog readmemh converter" << std::endl;
15
        if (argc > 1) {
16
                std::fstream *file = new std::fstream(argv[1], std::ios::in);
17
                src = file;
18
                if (!file->is_open()) {
19
                        const char *err = strerror(errno);
20
                        std::cerr << "Failed to open input file: " << err << std::endl;
21
                        return 1;
22
                }
23
                if (argc > 2) {
24
                        file = new std::fstream(argv[2], std::ios::out);
25
                        dst = file;
26
                        if (!file->is_open()) {
27
                                const char *err = strerror(errno);
28
                                std::cerr << "Failed to open output file: " << err << std::endl;
29
                                return 1;
30
                        }
31
                }
32
        } else
33
                std::cerr << "Usage:\n\t" << argv[0] << "[<input file> [<output file>]]\n" << "By default standard input and output are used" << std::endl;
34
 
35
        std::string line;
36
        std::vector<unsigned char> buffer;
37
        int nline = 0;
38
        *dst << std::uppercase;
39
        while(std::getline(*src, line)) {
40
                ++nline;
41
                if (line.empty())
42
                        continue;
43
                char c[3];
44
                std::istringstream str(line);
45
                str >> c[0];
46
                if (c[0] != ':') {
47
                        std::cerr << "Line " << nline << " has invalid format:\n'" << line << '\'' << std::endl;
48
                        return 1;
49
                }
50
                buffer.clear();
51
                unsigned crc = 0;
52
                str.clear();
53
                c[2] = '\0';
54
                c[0] = '\0';
55
                while(str >> c[1]) {
56
                        if (c[0] == '\0') {
57
                                c[0] = c[1];
58
                                continue;
59
                        }
60
                        std::istringstream str(c);
61
                        unsigned x;
62
                        if (!(str >> std::hex >> x)) {
63
                                std::cerr << "Invalid entry size at line " << nline << std::endl;
64
                                return 1;
65
                        }
66
                        buffer.push_back((unsigned char)x);
67
                        crc += x;
68
                        c[0] = '\0';
69
                }
70
                std::cerr << std::endl;
71
                if ((char)crc) {
72
                        std::cerr << "Invalid CRC of line " << nline << std::endl;
73
                        return 1;
74
                }
75
                if (buffer.size() < 5) {
76
                        std::cerr << "Invalid size of line " << nline << std::endl;
77
                        return 1;
78
                }
79
                if (buffer[3] == 1)     //end of file
80
                        break;
81
                int size = buffer[0];
82
                int address = (buffer[1] << 8) + buffer[2];
83
                *dst << '@' << std::hex << std::setw(4) << std::setfill('0') << address;
84
                for(std::vector<unsigned char>::iterator i = buffer.begin() + 4, e = buffer.end()-1; i != e; ++i)
85
                        *dst << ' ' << std::hex << std::setw(2) << std::setfill('0') << unsigned(*i);
86
                *dst << '\n';
87
        }
88
 
89
        if (dst != &std::cout)
90
                delete dst;
91
        if (src != &std::cin)
92
                delete src;
93
        return 0;
94
}

powered by: WebSVN 2.1.0

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