sim: added cmds
[calu.git] / 3b_sim / sim.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4 #include <boost/tokenizer.hpp>
5 #include <boost/program_options.hpp>
6 #include <boost/lexical_cast.hpp>
7 #include <string>
8 #include <map>
9 #include <list>
10
11 #include <boost/function.hpp>
12 #include <boost/functional.hpp>
13 #include <boost/tuple/tuple.hpp>
14
15 #include "disasm.h"
16 #include "ccpu.hpp"
17 #include "CInstrFactory.hpp"
18 #include "uint32_from_hex.hpp"
19
20 #include "SReadline/SReadline.h"
21 using namespace swift;
22
23 #define RAM_END (1000)
24 #define PROG_END (1000)
25 #define REG_COUNT (16)
26
27 typedef boost::function<void (const vector<string> &)> Func;
28 typedef boost::tuple<string, Func, string> CompleterElement;
29
30 typedef list<CompleterElement> MyCompleterContainer;
31
32 class LookupFunctor
33 {
34         public:
35                 // Creates a functor and memorises tokens
36                 LookupFunctor(const vector<string>&  tokens) : Tokens(tokens) {}
37
38                 // Compares the first token only
39                 bool operator()(const CompleterElement& ele) const
40                 {
41                         return (strncmp(Tokens.begin()->c_str(), ele.get<0>().c_str(), Tokens.begin()->size()) == 0);
42                 }
43
44         private:
45                 const vector<string> &Tokens;
46 };
47
48
49
50
51 class CHelpExec
52 {
53         private:
54                 const MyCompleterContainer &m_completers;
55
56         public:
57                 CHelpExec(const MyCompleterContainer &cont) : m_completers(cont) {}
58
59                 void operator() (const vector<string>&)
60                 {
61                         cout << "Available commands: " << endl;
62                         for(auto iter = m_completers.begin(); iter != m_completers.end(); ++iter) {
63                                 cout << setw(15) << (*iter).get<0>() << ": " << (*iter).get<2>() << endl;
64                         }
65                 }
66 };
67
68 void close_prog(const std::vector<std::string> &);
69
70 CCpu* Iinstr::m_cpu;
71
72 CCpu* global_cpu = NULL;
73
74 vector<CDat> breakpoints;
75
76 bool ignoreBreak = false;
77
78 void doExit(const vector<string>&)
79 {
80         exit(EXIT_SUCCESS);
81 }
82
83 void execStep(const vector<string>& in)
84 {
85         int count = 1;
86         if(in.size() == 2) {
87                 try {
88                         count = lexical_cast<int>(in.back());
89                 }
90                 catch(bad_cast&) {
91                         cerr << "given parameter to step is not a number" << endl;
92                 }
93         }
94         while(count > 0) {
95                 try {
96                         auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
97                         if(breakp == breakpoints.end() || ignoreBreak) {
98                                 global_cpu->tick();
99                                 ignoreBreak = false;
100                         }
101                         else {
102                                 ignoreBreak = true;
103                                 cout << "Breakpoint " << *breakp << " hit" << endl;
104                                 break;
105                         }
106                 }
107                 catch(std::string& e) {
108                         cerr << e << endl;
109                 }
110                 count--;
111         }
112 }
113
114 void execRun(const vector<string>&)
115 {
116         while(1) {
117                 try {
118                         auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
119                         if(breakp == breakpoints.end() || ignoreBreak) {
120                                 global_cpu->tick();
121                                 ignoreBreak = false;
122                         }
123                         else {
124                                 ignoreBreak = true;
125                                 cout << "Breakpoint " << *breakp << " hit" << endl;
126                                 return;
127                         }
128                 }
129                 catch(std::string& e) {
130                         cerr << e << endl;
131                         return;
132                 }
133         }
134 }
135
136 void setPC(const vector<string>& in)
137 {
138         CDat addr = 0;
139         if(in.size() == 2) {
140                 try {
141                         addr = lexical_cast<uint32_from_hex>(in.back());
142                 }
143                 catch(bad_cast&) {
144                         cerr << "given parameter is not a number" << endl;
145                         return;
146                 }
147         }
148         global_cpu->setNextPC(addr);
149         cout << "Set programcounter to " << addr << endl;
150 }
151
152
153 void printReg(const vector<string>& in)
154 {
155         int i, start = 0, end = REG_COUNT-1;
156         /* Todo: 
157          * 1) make 2 columns
158          */
159
160         if(in.size() >= 2) {
161                 try {
162                         start = lexical_cast<int>(in[1]);
163                         if(start < 0 || start > (REG_COUNT-1)) {
164                                 cerr << "start is out of range" << endl;
165                                 return;
166                         }
167                         end = start;
168                 }
169                 catch(bad_cast&) {
170                         cerr << "given parameter is not a number" << endl;
171                         return;
172                 }
173         }
174
175         if(in.size() >= 3) {
176                 try {
177                         end = lexical_cast<int>(in[2]);
178                         if(start > end || end > (REG_COUNT-1)) {
179                                 cerr << "end is out of range or smaller than start" << endl;
180                                 return;
181                         }
182                 }
183                 catch(bad_cast&) {
184                         cerr << "given parameter is not a number" << endl;
185                         return;
186                 }
187         }
188         
189         for(i = start; i <= end; i++) {
190                 cout << setw(2) << i << ": 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getRegister(i) << " " << std::dec << setw(10) << setfill(' ') << global_cpu->getRegister(i) << endl;
191         }
192 }
193
194 void printRAM(const vector<string>& in)
195 {
196         int i, start = 0, end = 15;
197         /* Todo: 
198          * 1) make 2 columns
199          */
200
201         if(in.size() >= 2) {
202                 try {
203                         start = lexical_cast<int>(in[1]);
204                         if(start < 0 || start > (RAM_END-1)) {
205                                 cerr << "start is out of range" << endl;
206                                 return;
207                         }
208                         end = start;
209                 }
210                 catch(bad_cast&) {
211                         cerr << "given parameter is not a number" << endl;
212                         return;
213                 }
214         }
215
216         if(in.size() >= 3) {
217                 try {
218                         end = lexical_cast<int>(in[2]);
219                         if(start > end || end > (RAM_END-1)) {
220                                 cerr << "end is out of range or smaller than start" << endl;
221                                 return;
222                         }
223                 }
224                 catch(bad_cast&) {
225                         cerr << "given parameter is not a number" << endl;
226                         return;
227                 }
228         }
229         
230         for(i = start*4; i <= end*4; i += 4) {
231                 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getRAM(i) << " " << std::dec << setw(10) << setfill(' ') << global_cpu->getRAM(i) << endl;
232         }
233 }
234
235 void printPROG(const vector<string>& in)
236 {
237         int i, start = 0, end = 15;
238         /* Todo: 
239          * 1) make 2 columns
240          */
241
242         if(in.size() >= 2) {
243                 try {
244                         start = lexical_cast<int>(in[1]);
245                         if(start < 0 || start > (PROG_END-1)) {
246                                 cerr << "start is out of range" << endl;
247                                 return;
248                         }
249                         end = start;
250                 }
251                 catch(bad_cast&) {
252                         cerr << "given parameter is not a number" << endl;
253                         return;
254                 }
255         }
256
257         if(in.size() >= 3) {
258                 try {
259                         end = lexical_cast<int>(in[2]);
260                         if(start > end || end > (PROG_END-1)) {
261                                 cerr << "end is out of range or smaller than start" << endl;
262                                 return;
263                         }
264                 }
265                 catch(bad_cast&) {
266                         cerr << "given parameter is not a number" << endl;
267                         return;
268                 }
269         }
270         
271         for(i = start*4; i <= end*4; i += 4) {
272                 Iinstr* pi = global_cpu->getProg(i);
273                 if(pi == NULL) {
274                         cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP" << endl;
275                 }
276                 else {
277                         cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": " << std::dec << pi->toString() << endl;
278                 }
279         }
280 }
281
282 void setBreak(const vector<string>& in)
283 {
284         int addr = 0;
285         if(in.size() == 2) {
286                 try {
287                         addr = lexical_cast<uint32_from_hex>(in.back());
288                         breakpoints.push_back(addr);
289                 }
290                 catch(bad_cast&) {
291                         cerr << "Given parameter is not a valid address" << endl;
292                 }
293         }
294         else {
295                 cerr << "Invalid parameter count!" << endl;
296         }
297 }
298
299 void printStatus(const vector<string>&)
300 {
301         CDat stackp = global_cpu->getStack();
302         CDat stackd = global_cpu->getRAM(stackp);
303         cout << "Stack pointer: " << stackp << " @stackpointer: " << stackd << endl;
304         cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
305
306 }
307
308 using boost::lexical_cast;
309 using boost::bad_lexical_cast;
310
311 using namespace std;
312
313 using namespace boost::program_options;
314 namespace po = boost::program_options;
315
316 std::string progName;
317
318 int main(int argc, char* argv[])
319 {
320         progName = argv[0];
321         ifstream inFile;
322         try {
323                 po::options_description desc("Allowed options");
324                 desc.add_options()
325                 ("help,h","produce help message")
326                 ("file,f",value<string>(), "input file")
327                 ;
328
329                 po::positional_options_description p;
330                 p.add("file",1);
331
332                 po::variables_map vm;
333                 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
334                 po::notify(vm);
335
336                 if(vm.count("help")) {
337                         cout << desc << endl;
338                         return EXIT_FAILURE;
339                 }
340
341                 if(vm.count("file")) {
342 #ifdef DEBUG 
343                         cout << "going to open file " << vm["file"].as<string>() << endl;
344 #endif
345                         inFile.open(vm["file"].as<string>(), ios::in);
346                         if(!inFile) {
347                                 cerr << "Error opening file " << vm["file"].as<string>() << endl;
348                                 return EXIT_FAILURE;
349                         }
350                 }
351                 else {
352                         cout << "not input file given!" << endl << endl;
353                         cout << desc << endl;
354                         return EXIT_FAILURE;
355                 }
356         }
357         catch(std::exception& ex) {
358                 cout << ex.what() << endl;
359         }
360
361
362         string dir = "./instr/";
363
364         map<short, Iinstr*> instr;
365
366         CInstrFactory instrFab;
367         try {
368                 instrFab.loadLibsIntoMap(instr, dir);
369         }
370         catch(std::bad_alloc& e) {
371                 cerr << progName << ": bad_alloc caught " << e.what() << endl;
372                 exit(EXIT_FAILURE); 
373         }
374         catch(std::string& s) {
375                 cerr << progName << ": " << s << endl;
376                 exit(EXIT_FAILURE);
377         }
378
379         CCpu cpu(REG_COUNT, RAM_END, PROG_END);
380
381         global_cpu = &cpu;
382
383         Iinstr::setCPU(&cpu);
384
385
386         std::string str = "";
387         int addr = 0;
388         boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
389         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
390         disasm disasm(instr);
391         while(getline(inFile, str)) {
392                 int count = 0;
393                 tokens.assign(str);
394                 stringstream out;
395                 int type = 0;
396                 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
397                         if(tok_iter == tokens.begin()) {
398                                 try {
399                                         type = lexical_cast<int>(*tok_iter);
400                                         count++;
401                                         continue;
402                                 }
403                                 catch(bad_lexical_cast &) {
404                                         break;
405                                 }
406                                 cout << endl;
407                         }
408                         switch(type) {
409                                 case 0:
410                                         if(count == 1) {
411                                                 try {
412                                                         addr = lexical_cast<uint32_from_hex>(*tok_iter);
413                                                 }
414                                                 catch(bad_lexical_cast& e) {
415                                                         cerr << e.what() << endl;
416                                                         exit(EXIT_FAILURE);
417                                                 }
418                                         }
419                                         else if(count == 2) {
420                                                 try {
421                                                         CDat data = lexical_cast<uint32_from_hex>(*tok_iter);
422                                                         cpu.setRAM(addr, data);
423                                                 }
424                                                 catch(bad_lexical_cast& e) {
425                                                         cerr << e.what() << endl;
426                                                         exit(EXIT_FAILURE);
427                                                 }
428                                         }
429                                         break;
430                                 case 1:
431                                         if(count == 1) {
432                                                 try {
433                                                         addr = lexical_cast<uint32_from_hex>(*tok_iter);
434                                                 }
435                                                 catch(bad_lexical_cast& e) {
436                                                         cerr << e.what() << endl;
437                                                         exit(EXIT_FAILURE);
438                                                 }
439                                         }
440                                         else if(count == 2) {
441                                                 Iinstr *pi = disasm.decode(*tok_iter);
442                                                 cpu.setProg(addr, pi);
443                                         }
444                                         break;
445                                 case 2:
446                                 case 3:
447                                         cerr << "ignoring labels and comments for now" << endl;
448                                 default:
449                                         cerr << "i was to lazy to implement the other format types for now" << endl;
450                         }
451                         count++;
452                 }
453         }
454         inFile.close();
455
456         cout << endl;
457
458 /*
459         for(int i = 0; i <= 32; i += 4) {
460                 Iinstr *pinstr = cpu.getProg(i);
461                 if(pinstr != NULL) {
462                         cout << i << " : " << std::hex << i << std::dec << " " << pinstr->toString() << endl;
463                 }
464                 else {
465                         cout << "Null at " << i << " : " << std::hex << i << endl;
466                 }
467         }
468
469         for(int i = 0; i <= 32; i += 4) {
470                 CDat data = cpu.getRAM(i);
471                 cout << i << " : " << std::hex << i << std::dec << " " << data << endl;
472         }
473 */
474         cpu.setRegister(1, 4);
475         cpu.setRegister(2, 0);
476         cpu.setRAM(0,5);
477         cpu.setRAM(4,50);
478         cpu.setRAM(8,32);
479         cpu.setRAM(12,45);
480
481         // following: job of the bootloader
482         //set stackpointer
483         cpu.setStack(500);
484         //set return to nowhere for ret
485         cpu.setRAM(500,50);
486
487         SReadline Reader;
488
489         MyCompleterContainer Completers;
490
491         CHelpExec HelpExec(Completers);
492
493         Completers.push_back(CompleterElement("help", boost::bind1st( boost::mem_fun( &CHelpExec::operator()), &HelpExec), "Prints this message"));
494         Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
495         Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
496         Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
497         Completers.push_back(CompleterElement("dre [s] [e]",&printReg, "Prints registers. if s is given, only register s is printed. if s and e are given the registers from s to e are printed. if omitted all registers a printed."));
498         Completers.push_back(CompleterElement("dra [s] [e]",&printRAM, "Prints RAM. if s is given, only RAM-Addr. s is printed. if s and e are given the RAM-Addrs from s to e are printed. if omitted the first 16 RAM-Addrs are printed."));
499         Completers.push_back(CompleterElement("dpr [s] [e]",&printPROG, "Prints program. if s is given, only Prog-Addr. s is printed. if s and e are given the Prog-Addrs from s to e are printed. if omitted the first 16 Prog-Addrs are printed."));
500         Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
501         Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
502         Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
503         Completers.push_back(CompleterElement("status",&printStatus, "Prints status of CPU."));
504
505         Reader.RegisterCompletions(Completers);
506
507         string UserInput;
508         vector<string> Tokens, lastTokens;
509         bool EndOfInput = false;
510
511         auto Found(Completers.end());
512
513         Func lastFunc = NULL;
514
515
516         while(1) {
517                 UserInput = Reader.GetLine("> ", Tokens, EndOfInput);
518                 if(EndOfInput) {
519                         break;
520                 }
521
522                 if(!Tokens.empty()) {
523                         Found = find_if(Completers.begin(), Completers.end(), LookupFunctor(Tokens));
524
525                         if(Found != Completers.end()) {
526                                 if((*Found).get<1>() != 0) {
527                                         lastFunc = (*Found).get<1>();
528                                         lastFunc(Tokens);
529                                         lastTokens = Tokens;
530                                         //(*Found).get<1>()(Tokens);
531                                 }
532                         }
533                         else {
534                                 lastFunc = NULL;
535                                 cout << "Unknown command. 'help' displays help" << endl;
536                         }
537                 }
538                 else {
539                         if(lastFunc != NULL) {
540                                 lastFunc(lastTokens);
541                         }
542                         else {
543                                 cout << "Unknown command. 'help' displays help" << endl;
544                         }
545                 }
546         }
547
548
549 /*
550         for(int i = 0; ; i++) {
551                 try {
552                         cpu.tick();
553                         cout << " reg0: " << cpu.getRegister(0) <<  " reg1: " << cpu.getRegister(1);
554                         cout << " reg2: " << cpu.getRegister(2) <<  " reg3: " << cpu.getRegister(3);
555                         cout << " reg4: " << cpu.getRegister(4) <<  " reg5: " << cpu.getRegister(5);
556                         cout << endl << endl;
557
558                 }
559                 catch(string& e) {
560                         cerr << e << endl;
561                         break;
562                 }
563         }
564 */
565         return EXIT_SUCCESS;
566 }