/* `Deep Thought', a softcore CPU implemented on a FPGA Copyright (C) 2010 Markus Hofstaetter Copyright (C) 2010 Martin Perner Copyright (C) 2010 Stefan Rebernig Copyright (C) 2010 Manfred Schwarz Copyright (C) 2010 Bernhard Urban This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #include #include #include "disasm.h" #include "CInstrFactory.hpp" using boost::lexical_cast; using boost::bad_lexical_cast; using namespace std; using namespace boost::program_options; namespace po = boost::program_options; std::string progName; CCpu* Iinstr::m_cpu; disasm* Iinstr::m_disasm; int main(int argc, char* argv[]) { progName = argv[0]; ifstream inFile; try { po::options_description desc("Allowed options"); desc.add_options() ("help,h","produce help message") ("file,f",value(), "input file") ; po::positional_options_description p; p.add("file",1); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); po::notify(vm); if(vm.count("help")) { cout << desc << endl; return EXIT_FAILURE; } if(vm.count("file")) { #ifdef DEBUG cout << "going to open file " << vm["file"].as() << endl; #endif inFile.open(vm["file"].as(), ios::in); if(!inFile) { cerr << "Error opening file " << vm["file"].as() << endl; return EXIT_FAILURE; } } else { cout << "not input file given!" << endl << endl; cout << desc << endl; return EXIT_FAILURE; } } catch(std::exception& ex) { cout << ex.what() << endl; } string dir = "./instr/"; map instr; CInstrFactory instrFab; try { instrFab.loadLibsIntoMap(instr, dir); } catch(std::bad_alloc& e) { cerr << progName << ": bad_alloc caught " << e.what() << endl; exit(EXIT_FAILURE); } std::string str = ""; boost::char_separator sep(";", "", boost::keep_empty_tokens); boost::tokenizer > tokens(str, sep); disasm disasm(instr); while(getline(inFile, str)) { int count = 0; tokens.assign(str); stringstream out; int type = 0; for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) { if(tok_iter == tokens.begin()) { try { type = lexical_cast(*tok_iter); count++; continue; } catch(bad_lexical_cast &) { break; } cout << endl; } switch(type) { case 2: if(count == 1) { cout << "; "; } cout << *tok_iter; break; case 3: if((*tok_iter).size() > 0) { if(count > 1) { cout << endl; } cout << *tok_iter << ":"; } break; case 1: if(count == 1) { out << "[0x" << *tok_iter << "]: "; } else if(count == 2) { out << disasm.decodeToString(*tok_iter); } else if(count == 3) { //code saved in hex-file //cout << *tok_iter ; } else if(count == 4) { /* label */ if((*tok_iter).size() > 0) { cout << *tok_iter << ":" << endl; } cout << out.str(); } else if(count == 5) { if((*tok_iter).size() > 0) { cout << " ;"; } } if(count >= 5) { cout << *tok_iter; } break; default: cerr << "i was to lazy to implement the other format types for now" << endl; } count++; } if(type == 1 && count <= 4) { cout << out.str(); } cout << endl; } inFile.close(); cout << endl; return EXIT_SUCCESS; }