4 #include <boost/tokenizer.hpp>
5 #include <boost/program_options.hpp>
6 #include <boost/lexical_cast.hpp>
11 #include <boost/function.hpp>
12 #include <boost/functional.hpp>
13 #include <boost/tuple/tuple.hpp>
17 #include "CInstrFactory.hpp"
18 #include "uint32_from_hex.hpp"
20 #include "SReadline/SReadline.h"
21 using namespace swift;
23 #define RAM_END (0x3000)
24 #define PROG_END (0x3000)
25 #define REG_COUNT (16)
27 typedef boost::function<void (const vector<string> &)> Func;
28 typedef boost::tuple<string, Func, string> CompleterElement;
30 typedef list<CompleterElement> MyCompleterContainer;
35 // Creates a functor and memorises tokens
36 LookupFunctor(const vector<string>& tokens) : Tokens(tokens) {}
38 // Compares the first token only
39 bool operator()(const CompleterElement& ele) const
41 return (strncmp(Tokens.begin()->c_str(), ele.get<0>().c_str(), Tokens.begin()->size()) == 0);
45 const vector<string> &Tokens;
54 const MyCompleterContainer &m_completers;
57 CHelpExec(const MyCompleterContainer &cont) : m_completers(cont) {}
59 void operator() (const vector<string>&)
61 cout << "Available commands: " << endl;
62 for(auto iter = m_completers.begin(); iter != m_completers.end(); ++iter) {
63 cout << setw(19) << setfill(' ') << (*iter).get<0>() << ": " << (*iter).get<2>() << endl;
68 void close_prog(const std::vector<std::string> &);
71 disasm* Iinstr::m_disasm;
73 CCpu* global_cpu = NULL;
75 vector<CDat> breakpoints;
80 multimap<int,string> dataCommentsStore, progCommentsStore, dataLabelStore, progLabelStore;
81 map<int,string> dataLineCommentStore, progLineCommentStore;
83 bool ignoreBreak = false;
85 bool exitProg = false;
87 void doExit(const vector<string>&)
92 unsigned int convertStringToNum(const std::string& in)
94 if(in.substr(0,2) == "0x") {
95 return lexical_cast<uint32_from_hex>(in);
98 return lexical_cast<unsigned int>(in);
103 void execStep(const vector<string>& in)
108 count = convertStringToNum(in.back());
111 cerr << "given parameter to step is not a number" << endl;
116 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
117 if(breakp == breakpoints.end() || ignoreBreak) {
123 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
127 catch(std::string& e) {
134 void execRun(const vector<string>&)
138 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
139 if(breakp == breakpoints.end() || ignoreBreak) {
145 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
149 catch(std::string& e) {
156 void setPC(const vector<string>& in)
161 addr = convertStringToNum(in.back());
164 cerr << "given parameter is not a number" << endl;
168 global_cpu->setNextPC(addr);
169 cout << "Set programcounter to 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << endl;
173 void printReg(const vector<string>& in)
175 int i, start = 0, end = REG_COUNT-1;
182 start = convertStringToNum(in[1]);
183 if(start < 0 || start > (REG_COUNT-1)) {
184 cerr << "start is out of range" << endl;
190 cerr << "given parameter is not a number" << endl;
197 end = convertStringToNum(in[2]);
198 if(start > end || end > (REG_COUNT-1)) {
199 cerr << "end is out of range or smaller than start" << endl;
204 cerr << "given parameter is not a number" << endl;
209 for(i = start; i <= end; i++) {
210 cout << setw(2) << setfill('0') << i << ": 0x";
211 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRegister(i) << " ";
212 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRegister(i) << " ";
213 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRegister(i) << endl;
217 void setReg(const vector<string>& in)
224 reg = convertStringToNum(in[1]);
225 if(reg < 0 || reg > (REG_COUNT-1)) {
226 cerr << "register is out of range" << endl;
230 val = convertStringToNum(in[2]);
232 cout << "Setting register " << reg << " to 0x" << std::hex << setw(8) << setfill('0') << val << std::dec << endl;
233 global_cpu->setRegister(reg,val);
236 cerr << "given parameter is not a number" << endl;
242 void printRAM(const vector<string>& in)
244 int i, start = 0, end = 15;
251 start = convertStringToNum(in[1]);
252 if(start < 0 || start > (RAM_END-1)) {
253 cerr << "start is out of range" << endl;
256 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
260 cerr << "given parameter is not a number" << endl;
267 end = convertStringToNum(in[2]);
268 if(start > end || end > (RAM_END-1)) {
269 cerr << "end is out of range or smaller than start" << endl;
272 if(end % BYTE_COUNT != 0) {
273 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
276 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
280 cerr << "given parameter is not a number" << endl;
284 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
286 auto range = dataLabelStore.equal_range(i);
287 for(auto iter = range.first; iter != range.second; ++iter) {
288 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
292 auto range = dataCommentsStore.equal_range(i);
293 for(auto iter = range.first; iter != range.second; ++iter) {
294 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
297 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": 0x";
298 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRAM(i) << " ";
299 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRAM(i) << " ";
300 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRAM(i);
302 auto iter = dataLineCommentStore.find(i);
303 if(iter != dataLineCommentStore.end()) {
304 cout << color(blue,black) << " ;" << iter->second << color(white,black);
311 void setRam(const vector<string>& in)
318 addr = convertStringToNum(in[1]);
319 if(addr < 0 || addr > (RAM_END-1)) {
320 cerr << "RAM-Address is out of range" << endl;
324 addr = (addr & (~(BYTE_COUNT-1))) / BYTE_COUNT;
325 val = convertStringToNum(in[2]);
329 cout << "Setting RAM-Address 0x" << std::hex << setw(8) << setfill('0') << addr;
330 cout << " to 0x" << setw(8) << setfill('0') << val << std::dec << endl;
331 global_cpu->setRAM(addr,val);
334 cerr << "given parameter is not a number" << endl;
339 void printPROG(const vector<string>& in)
341 int i, start = 0, end = 15;
346 if(in.size() >= 2 && in[1][0] == 'c') {
347 start = global_cpu->getCurPC() / BYTE_COUNT;
350 else if(in.size() >= 2) {
352 start = convertStringToNum(in[1]);
353 if(start < 0 || start > (PROG_END-1)) {
354 cerr << "start is out of range" << endl;
357 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
361 cerr << "given parameter is not a number" << endl;
368 end = convertStringToNum(in[2]);
369 if(start > end || end > (PROG_END-1)) {
370 cerr << "end is out of range or smaller than start" << endl;
373 if(end % BYTE_COUNT != 0) {
374 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
377 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
382 cerr << "given parameter is not a number" << endl;
388 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
390 auto range = progLabelStore.equal_range(i);
391 for(auto iter = range.first; iter != range.second; ++iter) {
392 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
396 auto range = progCommentsStore.equal_range(i);
397 for(auto iter = range.first; iter != range.second; ++iter) {
398 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
402 Iinstr* pi = global_cpu->getProg(i);
404 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP";
407 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": " << std::dec << global_cpu->colorifyInstr(pi->toString());
409 auto iter = progLineCommentStore.find(i);
410 if(iter != progLineCommentStore.end()) {
411 cout << color(blue,black) << " ;" << iter->second << color(white,black);
417 void setBreak(const vector<string>& in)
419 unsigned int addr = 0;
422 addr = convertStringToNum(in.back());
423 breakpoints.push_back(addr);
424 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << " set" << endl;
427 cerr << "Given parameter is not a valid address" << endl;
431 cerr << "Invalid parameter count!" << endl;
435 void listBreaks(const vector<string>&)
437 for(auto iter = breakpoints.begin(); iter != breakpoints.end(); ++iter) {
438 cout << "Breakpoint at 0x" << std::hex << setw(8) << setfill('0') << *iter << std::dec << endl;
442 void getPerf(const vector<string>&)
444 cout << "current perfcounter is " << std::dec << global_cpu->getPerf() << endl;
447 void resetPerf(const vector<string>&)
449 cout << "reset perfcounter" << endl;
450 global_cpu->setPerf(0);
453 void printStatus(const vector<string>&)
455 CDat stackp = global_cpu->getStack();
456 CDat stackd = global_cpu->getRAM(stackp);
457 cout << "Stack pointer: 0x" << std::hex << setw(8) << setfill('0') << stackp << " @stackpointer: 0x" << setw(8) << stackd << std::dec << " (" << stackd << ")" << endl;
458 cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
459 cout << "cur PC: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getCurPC() << " next PC: 0x" << setw(8) << setfill('0') << global_cpu->getNextPC() << std::dec << endl;
463 using boost::lexical_cast;
464 using boost::bad_lexical_cast;
468 using namespace boost::program_options;
469 namespace po = boost::program_options;
471 std::string progName;
473 int main(int argc, char* argv[])
478 po::options_description desc("Allowed options");
480 ("help,h","produce help message")
481 ("file,f",value<string>(), "input file")
484 po::positional_options_description p;
487 po::variables_map vm;
488 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
491 if(vm.count("help")) {
492 cout << desc << endl;
496 if(vm.count("file")) {
498 cout << "going to open file " << vm["file"].as<string>() << endl;
500 inFile.open(vm["file"].as<string>(), ios::in);
502 cerr << "Error opening file " << vm["file"].as<string>() << endl;
507 cout << "not input file given!" << endl << endl;
508 cout << desc << endl;
512 catch(std::exception& ex) {
513 cout << ex.what() << endl;
517 string dir = "./instr/";
519 map<short, Iinstr*> instr;
521 CInstrFactory instrFab;
523 instrFab.loadLibsIntoMap(instr, dir);
525 catch(std::bad_alloc& e) {
526 cerr << progName << ": bad_alloc caught " << e.what() << endl;
529 catch(std::string& s) {
530 cerr << progName << ": " << s << endl;
534 CCpu cpu(REG_COUNT, RAM_END, PROG_END);
538 Iinstr::setCPU(&cpu);
540 disasm disasm(instr);
542 Iinstr::setDisasm(&disasm);
544 vector<string> commentDefer;
545 vector<string> labelDefer;
546 std::string str = "";
548 boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
549 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
550 while(getline(inFile, str)) {
555 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
556 if(tok_iter == tokens.begin()) {
558 type = lexical_cast<int>(*tok_iter);
562 catch(bad_lexical_cast &) {
571 addr = lexical_cast<uint32_from_hex>(*tok_iter);
572 for(unsigned int i = 0; i < commentDefer.size(); i++) {
573 dataCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
575 for(unsigned int i = 0; i < labelDefer.size(); i++) {
576 dataLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
579 commentDefer.clear();
582 catch(bad_lexical_cast& e) {
583 cerr << e.what() << endl;
587 else if(count == 2) {
589 CDat data = lexical_cast<uint32_from_hex>(*tok_iter);
590 cpu.setRAM(addr, data);
592 catch(bad_lexical_cast& e) {
593 cerr << e.what() << endl;
597 else if(count == 4) {
598 if((*tok_iter).size() > 0) {
599 dataLabelStore.insert(pair<int,string>(addr, *tok_iter));
602 else if(count == 5) {
603 if((*tok_iter).size() > 0) {
604 dataLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
611 addr = lexical_cast<uint32_from_hex>(*tok_iter);
612 for(unsigned int i = 0; i < commentDefer.size(); i++) {
613 progCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
615 for(unsigned int i = 0; i < labelDefer.size(); i++) {
616 progLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
619 commentDefer.clear();
622 catch(bad_lexical_cast& e) {
623 cerr << e.what() << endl;
627 else if(count == 2) {
628 Iinstr *pi = disasm.decode(*tok_iter);
629 cpu.setProg(addr, pi);
631 else if(count == 4) {
632 if((*tok_iter).size() > 0) {
633 progLabelStore.insert(pair<int,string>(addr, *tok_iter));
636 else if(count == 5) {
637 if((*tok_iter).size() > 0) {
638 progLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
644 if((*tok_iter).size() > 0) {
645 commentDefer.push_back(*tok_iter);
649 if((*tok_iter).size() > 0) {
650 labelDefer.push_back(*tok_iter);
654 cerr << "i was to lazy to implement the other format types for now" << endl;
664 for(int i = 0; i <= 32; i += 4) {
665 Iinstr *pinstr = cpu.getProg(i);
667 cout << i << " : " << std::hex << i << std::dec << " " << pinstr->toString() << endl;
670 cout << "Null at " << i << " : " << std::hex << i << endl;
674 for(int i = 0; i <= 32; i += 4) {
675 CDat data = cpu.getRAM(i);
676 cout << i << " : " << std::hex << i << std::dec << " " << data << endl;
679 /* cpu.setRegister(1, 4);
680 cpu.setRegister(2, 0);
682 cpu.setRAM(4,0x66334455);
686 // following: job of the bootloader
689 //set return to nowhere for ret
694 MyCompleterContainer Completers;
696 CHelpExec HelpExec(Completers);
698 Completers.push_back(CompleterElement("help", boost::bind1st( boost::mem_fun( &CHelpExec::operator()), &HelpExec), "Prints this message"));
699 Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
700 Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
701 Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
702 Completers.push_back(CompleterElement("dreg [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."));
703 Completers.push_back(CompleterElement("ddata [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."));
704 Completers.push_back(CompleterElement("dprog [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."));
705 Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
706 Completers.push_back(CompleterElement("listbreaks",&listBreaks, "Lists all breakpoints."));
707 Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
708 Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
709 Completers.push_back(CompleterElement("setreg [s] [num]",&setReg, "Sets Register s to num."));
710 Completers.push_back(CompleterElement("setdata [s] [num]",&setRam, "Sets RAM-Addr s to num."));
711 Completers.push_back(CompleterElement("status",&printStatus, "Prints status of CPU."));
712 Completers.push_back(CompleterElement("getperf",&getPerf, "Prints performance counter."));
713 Completers.push_back(CompleterElement("resetperf",&resetPerf, "Resets performance counter to 0."));
715 Reader.RegisterCompletions(Completers);
718 vector<string> Tokens, lastTokens;
719 bool EndOfInput = false;
721 //tilab g++44 doesn't like auto here
722 MyCompleterContainer::iterator Found(Completers.end());
724 Func lastFunc = NULL;
728 UserInput = Reader.GetLine("> ", Tokens, EndOfInput);
733 if(!Tokens.empty()) {
734 Found = find_if(Completers.begin(), Completers.end(), LookupFunctor(Tokens));
736 if(Found != Completers.end()) {
737 if((*Found).get<1>() != 0) {
738 lastFunc = (*Found).get<1>();
741 //(*Found).get<1>()(Tokens);
746 cout << "Unknown command. 'help' displays help" << endl;
750 if(lastFunc != NULL) {
751 lastFunc(lastTokens);
754 cout << "Unknown command. 'help' displays help" << endl;
761 for(int i = 0; ; i++) {
764 cout << " reg0: " << cpu.getRegister(0) << " reg1: " << cpu.getRegister(1);
765 cout << " reg2: " << cpu.getRegister(2) << " reg3: " << cpu.getRegister(3);
766 cout << " reg4: " << cpu.getRegister(4) << " reg5: " << cpu.getRegister(5);
767 cout << endl << endl;