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

Subversion Repositories lxp32

[/] [lxp32/] [trunk/] [tools/] [src/] [lxp32dump/] [main.cpp] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 ring0_mipt
/*
2
 * Copyright (c) 2016 by Alex I. Kuznetsov.
3
 *
4
 * Part of the LXP32 CPU IP core.
5
 *
6
 * Main translation unit for the LXP32 disassembler.
7
 */
8
 
9
#ifdef _MSC_VER
10
        #define _CRT_SECURE_NO_WARNINGS
11
#endif
12
 
13
#include "disassembler.h"
14
 
15
#include <iostream>
16
#include <fstream>
17
#include <string>
18
#include <stdexcept>
19
#include <cstring>
20
#include <cstdlib>
21
#include <cstdio>
22
#include <ctime>
23
 
24
static void displayUsage(std::ostream &os,const char *program) {
25
        os<<std::endl;
26
        os<<"Usage:"<<std::endl;
27
        os<<"    "<<program<<" [ option(s) | input file ]"<<std::endl<<std::endl;
28
 
29
        os<<"Options:"<<std::endl;
30
        os<<"    -b <addr>    Base address (for comments only)"<<std::endl;
31
        os<<"    -f <fmt>     Input format (bin, textio, dec, hex), default: autodetect"<<std::endl;
32
        os<<"    -h, --help   Display a short help message"<<std::endl;
33
        os<<"    -o <file>    Output file name, default: standard output"<<std::endl;
34
        os<<"    --           Do not interpret subsequent arguments as options"<<std::endl;
35
}
36
 
37
static Disassembler::Format detectInputFormat(std::istream &in) {
38
        static const std::size_t Size=256;
39
        static const char *textio="01\r\n \t";
40
        static const char *dec="0123456789\r\n \t";
41
        static const char *hex="0123456789ABCDEFabcdef\r\n \t";
42
 
43
        char buf[Size];
44
        in.read(buf,Size);
45
        auto s=static_cast<std::size_t>(in.gcount());
46
        in.clear();
47
        in.seekg(0);
48
 
49
        Disassembler::Format fmt=Disassembler::Textio;
50
 
51
        for(std::size_t i=0;i<s;i++) {
52
                if(fmt==Disassembler::Textio&&!strchr(textio,buf[i])) fmt=Disassembler::Dec;
53
                if(fmt==Disassembler::Dec&&!strchr(dec,buf[i])) fmt=Disassembler::Hex;
54
                if(fmt==Disassembler::Hex&&!strchr(hex,buf[i])) {
55
                        fmt=Disassembler::Bin;
56
                        break;
57
                }
58
        }
59
 
60
        return fmt;
61
}
62
 
63
int main(int argc,char *argv[]) try {
64
        std::string inputFileName,outputFileName;
65
 
66
        std::cerr<<"LXP32 Platform Disassembler"<<std::endl;
67
        std::cerr<<"Copyright (c) 2016 by Alex I. Kuznetsov"<<std::endl;
68
 
69
        Disassembler::Format fmt=Disassembler::Bin;
70
        bool noMoreOptions=false;
71
        bool formatSpecified=false;
72
        Disassembler::Word base=0;
73
 
74
        if(argc<=1) {
75
                displayUsage(std::cout,argv[0]);
76
                return 0;
77
        }
78
 
79
        for(int i=1;i<argc;i++) {
80
                if(argv[i][0]!='-'||noMoreOptions) {
81
                        if(inputFileName.empty()) inputFileName=argv[i];
82
                        else throw std::runtime_error("Only one input file name can be specified");
83
                }
84
                else if(!strcmp(argv[i],"--")) noMoreOptions=true;
85
                else if(!strcmp(argv[i],"-b")) {
86
                        if(++i==argc) {
87
                                displayUsage(std::cerr,argv[0]);
88
                                return EXIT_FAILURE;
89
                        }
90
                        try {
91
                                base=std::stoul(argv[i],nullptr,0);
92
                                if(base%4!=0) throw std::exception();
93
                        }
94
                        catch(std::exception &) {
95
                                throw std::runtime_error("Invalid base address");
96
                        }
97
                }
98
                else if(!strcmp(argv[i],"-f")) {
99
                        if(++i==argc) {
100
                                displayUsage(std::cerr,argv[0]);
101
                                return EXIT_FAILURE;
102
                        }
103
                        if(!strcmp(argv[i],"bin")) fmt=Disassembler::Bin;
104
                        else if(!strcmp(argv[i],"textio")) fmt=Disassembler::Textio;
105
                        else if(!strcmp(argv[i],"dec")) fmt=Disassembler::Dec;
106
                        else if(!strcmp(argv[i],"hex")) fmt=Disassembler::Hex;
107
                        else throw std::runtime_error("Unrecognized input format");
108
                        formatSpecified=true;
109
                }
110
                else if(!strcmp(argv[i],"-h")||!strcmp(argv[i],"--help")) {
111
                        displayUsage(std::cout,argv[0]);
112
                        return 0;
113
                }
114
                else if(!strcmp(argv[i],"-o")) {
115
                        if(++i==argc) {
116
                                displayUsage(std::cerr,argv[0]);
117
                                return EXIT_FAILURE;
118
                        }
119
                        outputFileName=argv[i];
120
                }
121
                else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
122
        }
123
 
124
        if(!formatSpecified) { // auto-detect input file format
125
                std::ifstream in(inputFileName,std::ios_base::in|std::ios_base::binary);
126
                fmt=detectInputFormat(in);
127
        }
128
 
129
        std::ifstream in;
130
 
131
        if(fmt==Disassembler::Bin) in.open(inputFileName,std::ios_base::in|std::ios_base::binary);
132
        else in.open(inputFileName,std::ios_base::in);
133
        if(!in) throw std::runtime_error("Cannot open \""+inputFileName+"\"");
134
 
135
        std::ofstream out;
136
        std::ostream *os=&std::cout;
137
        if(!outputFileName.empty()) {
138
                out.open(outputFileName,std::ios_base::out);
139
                if(!out) throw std::runtime_error("Cannot open \""+outputFileName+"\"");
140
                os=&out;
141
        }
142
 
143
        auto t=std::time(NULL);
144
        char szTime[256];
145
        auto r=std::strftime(szTime,256,"%c",std::localtime(&t));
146
        if(r==0) szTime[0]='\0';
147
 
148
        *os<<"/*"<<std::endl;
149
        *os<<" * Input file: "<<inputFileName<<std::endl;
150
        *os<<" * Input format: ";
151
 
152
        switch(fmt) {
153
        case Disassembler::Bin:
154
                *os<<"bin";
155
                break;
156
        case Disassembler::Textio:
157
                *os<<"textio";
158
                break;
159
        case Disassembler::Dec:
160
                *os<<"dec";
161
                break;
162
        case Disassembler::Hex:
163
                *os<<"hex";
164
                break;
165
        default:
166
                break;
167
        }
168
 
169
        if(!formatSpecified) *os<<" (autodetected)";
170
        *os<<std::endl;
171
        *os<<" * Base address: 0x"<<Disassembler::hex(base)<<std::endl;
172
        *os<<" * Disassembled by lxp32dump at "<<szTime<<std::endl;
173
        *os<<" */"<<std::endl<<std::endl;
174
 
175
        Disassembler disasm(in,*os);
176
        disasm.setFormat(fmt);
177
        disasm.setBase(base);
178
 
179
        try {
180
                disasm.dump();
181
        }
182
        catch(std::exception &) {
183
                if(!outputFileName.empty()) {
184
                        out.close();
185
                        std::remove(outputFileName.c_str());
186
                }
187
                throw;
188
        }
189
}
190
catch(std::exception &ex) {
191
        std::cerr<<"Error: "<<ex.what()<<std::endl;
192
        return EXIT_FAILURE;
193
}

powered by: WebSVN 2.1.0

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