sim: fixed misc things
[calu.git] / 3b_sim / sim.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <boost/tokenizer.hpp>
4 #include <boost/program_options.hpp>
5 #include <boost/lexical_cast.hpp>
6 #include <string>
7 #include <map>
8
9 #include "disasm.h"
10 #include "ccpu.hpp"
11 #include "CInstrFactory.hpp"
12
13 using boost::lexical_cast;
14 using boost::bad_lexical_cast;
15
16 using namespace std;
17
18 using namespace boost::program_options;
19 namespace po = boost::program_options;
20
21 std::string progName;
22
23 int main(int argc, char* argv[])
24 {
25         progName = argv[0];
26         ifstream inFile;
27         try {
28                 po::options_description desc("Allowed options");
29                 desc.add_options()
30                 ("help,h","produce help message")
31                 ("file,f",value<string>(), "input file")
32                 ;
33
34                 po::positional_options_description p;
35                 p.add("file",1);
36
37                 po::variables_map vm;
38                 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
39                 po::notify(vm);
40
41                 if(vm.count("help")) {
42                         cout << desc << endl;
43                         return EXIT_FAILURE;
44                 }
45
46                 if(vm.count("file")) {
47 #ifdef DEBUG 
48                         cout << "going to open file " << vm["file"].as<string>() << endl;
49 #endif
50                         inFile.open(vm["file"].as<string>(), ios::in);
51                         if(!inFile) {
52                                 cerr << "Error opening file " << vm["file"].as<string>() << endl;
53                                 return EXIT_FAILURE;
54                         }
55                 }
56                 else {
57                         cout << "not input file given!" << endl << endl;
58                         cout << desc << endl;
59                         return EXIT_FAILURE;
60                 }
61         }
62         catch(std::exception& ex) {
63                 cout << ex.what() << endl;
64         }
65
66
67         string dir = "./instr/";
68
69         map<short, Iinstr*> instr;
70
71         CInstrFactory instrFab;
72         try {
73                 instrFab.loadLibsIntoMap(instr, dir);
74         }
75         catch(std::bad_alloc& e) {
76                 cerr << progName << ": bad_alloc caught " << e.what() << endl;
77                 exit(EXIT_FAILURE); 
78         }
79         catch(std::string& s) {
80                 cerr << progName << ": " << s << endl;
81                 exit(EXIT_FAILURE);
82         }
83
84
85         CCpu cpu;
86
87
88         std::string str = "";
89         int addr = 0;
90         boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
91         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
92         disasm disasm(instr);
93         while(getline(inFile, str)) {
94                 int count = 0;
95                 tokens.assign(str);
96                 stringstream out;
97                 int type = 0;
98                 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
99                         if(tok_iter == tokens.begin()) {
100                                 try {
101                                         type = lexical_cast<int>(*tok_iter);
102                                         count++;
103                                         continue;
104                                 }
105                                 catch(bad_lexical_cast &) {
106                                         break;
107                                 }
108                                 cout << endl;
109                         }
110                         switch(type) {
111                                 case 2:
112                                         if(count == 1) {
113                                                 cout << "; ";
114                                         }
115                                         cout << *tok_iter;
116                                         break;
117                                 case 3:
118                                         if((*tok_iter).size() > 0) {
119                                                 if(count > 1) {
120                                                         cout << endl;
121                                                 }
122                                                 cout << *tok_iter << ":";
123                                         }
124                                         break;
125                                 case 1:
126                                         if(count == 1) {
127                                                 out << "[0x" << *tok_iter << "]: ";
128                                                 addr = lexical_cast<int>(*tok_iter);
129                                         }
130                                         else if(count == 2) {
131                                                 cpu.setProg(addr, disasm.decode(*tok_iter));
132                                         }
133                                         else if(count == 3) {
134                                                 //code saved in hex-file
135                                                 //cout << *tok_iter ;
136                                         }
137                                         else if(count == 4) {
138                                                 /* label */
139                                                 if((*tok_iter).size() > 0) {
140                                                         cout << *tok_iter << ":" << endl;
141                                                 }
142                                                 cout << out.str();
143                                         }
144                                         else if(count == 5) {
145                                                 if((*tok_iter).size() > 0) {
146                                                         cout << " ;";
147                                                 }
148                                         }
149
150                                         if(count >= 5) {
151                                                 cout << *tok_iter;
152                                         }
153                                         break;
154                                 default:
155                                         cerr << "i was to lazy to implement the other format types for now" << endl;
156                         }
157                         count++;
158                 }
159                 if(type == 1 && count <= 4) {
160                         cout << out.str();
161                 }
162                 cout << endl;
163         }
164         inFile.close();
165
166         cout << endl;
167         return EXIT_SUCCESS;
168 }