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"
21 #include "extensions/cprog.hpp"
22 #include "extensions/cuart.hpp"
26 #include "SReadline/SReadline.h"
27 using namespace swift;
29 #define RAM_END (0x3000)
30 #define PROG_END (0x3000)
31 #define REG_COUNT (16)
33 typedef boost::function<void (const vector<string> &)> Func;
34 typedef boost::tuple<string, Func, string> CompleterElement;
36 typedef list<CompleterElement> MyCompleterContainer;
41 // Creates a functor and memorises tokens
42 LookupFunctor(const vector<string>& tokens) : Tokens(tokens) {}
44 // Compares the first token only
45 bool operator()(const CompleterElement& ele) const
47 return (strncmp(Tokens.begin()->c_str(), ele.get<0>().c_str(), Tokens.begin()->size()) == 0);
51 const vector<string> &Tokens;
60 const MyCompleterContainer &m_completers;
63 CHelpExec(const MyCompleterContainer &cont) : m_completers(cont) {}
65 void operator() (const vector<string>&)
67 cout << "Available commands: " << endl;
68 for(auto iter = m_completers.begin(); iter != m_completers.end(); ++iter) {
69 cout << setw(19) << setfill(' ') << (*iter).get<0>() << ": " << (*iter).get<2>() << endl;
74 void close_prog(const std::vector<std::string> &);
78 disasm* Iinstr::m_disasm;
79 disasm* Iext::m_disasm;
81 CCpu* global_cpu = NULL;
83 vector<CDat> breakpoints;
88 multimap<int,string> dataCommentsStore, progCommentsStore, dataLabelStore, progLabelStore;
89 map<int,string> dataLineCommentStore, progLineCommentStore;
91 bool ignoreBreak = false;
93 bool exitProg = false;
95 void signalCpuBreak(int)
97 global_cpu->breakNext();
100 void doExit(const vector<string>&)
105 unsigned int convertStringToNum(const std::string& in)
107 if(in.substr(0,2) == "0x") {
108 return lexical_cast<uint32_from_hex>(in);
111 return lexical_cast<unsigned int>(in);
116 void execStep(const vector<string>& in)
121 count = convertStringToNum(in.back());
124 cerr << "given parameter to step is not a number" << endl;
129 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
130 if(breakp == breakpoints.end() || ignoreBreak) {
136 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
140 catch(std::string& e) {
147 void execRun(const vector<string>&)
151 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
152 if((breakp == breakpoints.end() || ignoreBreak) && !global_cpu->shouldBreak() ) {
158 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
162 catch(std::string& e) {
169 void setPC(const vector<string>& in)
174 addr = convertStringToNum(in.back());
177 cerr << "given parameter is not a number" << endl;
181 global_cpu->setNextPC(addr);
182 cout << "Set programcounter to 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << endl;
186 void printReg(const vector<string>& in)
188 int i, start = 0, end = REG_COUNT-1;
195 start = convertStringToNum(in[1]);
196 if(start < 0 || start > (REG_COUNT-1)) {
197 cerr << "start is out of range" << endl;
203 cerr << "given parameter is not a number" << endl;
210 end = convertStringToNum(in[2]);
211 if(start > end || end > (REG_COUNT-1)) {
212 cerr << "end is out of range or smaller than start" << endl;
217 cerr << "given parameter is not a number" << endl;
222 for(i = start; i <= end; i++) {
223 cout << setw(2) << setfill('0') << i << ": 0x";
224 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRegister(i) << " ";
225 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRegister(i) << " ";
226 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRegister(i) << endl;
230 void setReg(const vector<string>& in)
237 reg = convertStringToNum(in[1]);
238 if(reg < 0 || reg > (REG_COUNT-1)) {
239 cerr << "register is out of range" << endl;
243 val = convertStringToNum(in[2]);
245 cout << "Setting register " << reg << " to 0x" << std::hex << setw(8) << setfill('0') << val << std::dec << endl;
246 global_cpu->setRegister(reg,val);
249 cerr << "given parameter is not a number" << endl;
255 void printRAM(const vector<string>& in)
257 int i, start = 0, end = 15;
264 start = convertStringToNum(in[1]);
265 if(start < 0 || start > (RAM_END-1)) {
266 cerr << "start is out of range" << endl;
269 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
273 cerr << "given parameter is not a number" << endl;
280 end = convertStringToNum(in[2]);
281 if(start > end || end > (RAM_END-1)) {
282 cerr << "end is out of range or smaller than start" << endl;
285 if(end % BYTE_COUNT != 0) {
286 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
289 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
293 cerr << "given parameter is not a number" << endl;
297 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
299 auto range = dataLabelStore.equal_range(i);
300 for(auto iter = range.first; iter != range.second; ++iter) {
301 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
305 auto range = dataCommentsStore.equal_range(i);
306 for(auto iter = range.first; iter != range.second; ++iter) {
307 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
310 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": 0x";
311 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRAM(i) << " ";
312 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRAM(i) << " ";
313 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRAM(i);
315 auto iter = dataLineCommentStore.find(i);
316 if(iter != dataLineCommentStore.end()) {
317 cout << color(blue,black) << " ;" << iter->second << color(white,black);
324 void setRam(const vector<string>& in)
331 addr = convertStringToNum(in[1]);
332 if(addr < 0 || addr > (RAM_END-1)) {
333 cerr << "RAM-Address is out of range" << endl;
337 addr = (addr & (~(BYTE_COUNT-1))) / BYTE_COUNT;
338 val = convertStringToNum(in[2]);
342 cout << "Setting RAM-Address 0x" << std::hex << setw(8) << setfill('0') << addr;
343 cout << " to 0x" << setw(8) << setfill('0') << val << std::dec << endl;
344 global_cpu->setRAM(addr,val);
347 cerr << "given parameter is not a number" << endl;
352 void printPROG(const vector<string>& in)
354 int i, start = 0, end = 15;
359 if(in.size() >= 2 && in[1][0] == 'c') {
360 start = global_cpu->getCurPC() / BYTE_COUNT;
363 else if(in.size() >= 2) {
365 start = convertStringToNum(in[1]);
366 if(start < 0 || start > (PROG_END-1)) {
367 cerr << "start is out of range" << endl;
370 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
374 cerr << "given parameter is not a number" << endl;
381 end = convertStringToNum(in[2]);
382 if(start > end || end > (PROG_END-1)) {
383 cerr << "end is out of range or smaller than start" << endl;
386 if(end % BYTE_COUNT != 0) {
387 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
390 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
395 cerr << "given parameter is not a number" << endl;
401 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
403 auto range = progLabelStore.equal_range(i);
404 for(auto iter = range.first; iter != range.second; ++iter) {
405 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
409 auto range = progCommentsStore.equal_range(i);
410 for(auto iter = range.first; iter != range.second; ++iter) {
411 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
415 Iinstr* pi = global_cpu->getProg(i);
417 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP";
420 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": " << std::dec << global_cpu->colorifyInstr(pi->toString());
422 auto iter = progLineCommentStore.find(i);
423 if(iter != progLineCommentStore.end()) {
424 cout << color(blue,black) << " ;" << iter->second << color(white,black);
430 void setBreak(const vector<string>& in)
432 unsigned int addr = 0;
435 addr = convertStringToNum(in.back());
436 breakpoints.push_back(addr);
437 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << " set" << endl;
440 cerr << "Given parameter is not a valid address" << endl;
444 cerr << "Invalid parameter count!" << endl;
448 void listBreaks(const vector<string>&)
450 for(auto iter = breakpoints.begin(); iter != breakpoints.end(); ++iter) {
451 cout << "Breakpoint at 0x" << std::hex << setw(8) << setfill('0') << *iter << std::dec << endl;
455 void getPerf(const vector<string>&)
457 cout << "current perfcounter is " << std::dec << global_cpu->getPerf() << endl;
460 void resetPerf(const vector<string>&)
462 cout << "reset perfcounter" << endl;
463 global_cpu->setPerf(0);
466 void applyToExtensions(const vector<string>& in)
468 global_cpu->applyToExtensions(in);
472 void printStatus(const vector<string>&)
474 CDat stackp = global_cpu->getStack();
475 CDat stackd = global_cpu->getRAM(stackp);
476 cout << "Stack pointer: 0x" << std::hex << setw(8) << setfill('0') << stackp << " @stackpointer: 0x" << setw(8) << stackd << std::dec << " (" << stackd << ")" << endl;
477 cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
478 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;
482 using boost::lexical_cast;
483 using boost::bad_lexical_cast;
487 using namespace boost::program_options;
488 namespace po = boost::program_options;
490 std::string progName;
492 int main(int argc, char* argv[])
497 po::options_description desc("Allowed options");
499 ("help,h","produce help message")
500 ("file,f",value<string>(), "input file")
503 po::positional_options_description p;
506 po::variables_map vm;
507 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
510 if(vm.count("help")) {
511 cout << desc << endl;
515 if(vm.count("file")) {
517 cout << "going to open file " << vm["file"].as<string>() << endl;
519 inFile.open(vm["file"].as<string>(), ios::in);
521 cerr << "Error opening file " << vm["file"].as<string>() << endl;
526 cout << "not input file given!" << endl << endl;
527 cout << desc << endl;
531 catch(std::exception& ex) {
532 cout << ex.what() << endl;
536 string dir = "./instr/";
538 map<short, Iinstr*> instr;
540 CInstrFactory instrFab;
542 instrFab.loadLibsIntoMap(instr, dir);
544 catch(std::bad_alloc& e) {
545 cerr << progName << ": bad_alloc caught " << e.what() << endl;
548 catch(std::string& s) {
549 cerr << progName << ": " << s << endl;
553 CCpu cpu(REG_COUNT, RAM_END, PROG_END);
557 signal(SIGINT, signalCpuBreak);
558 Iinstr::setCPU(&cpu);
561 disasm disasm(instr);
563 Iinstr::setDisasm(&disasm);
564 Iext::setDisasm(&disasm);
566 global_cpu->registerExtension(new Cprog());
567 global_cpu->registerExtension(new Cuart());
569 vector<string> commentDefer;
570 vector<string> labelDefer;
571 std::string str = "";
573 boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
574 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
575 while(getline(inFile, str)) {
580 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
581 if(tok_iter == tokens.begin()) {
583 type = lexical_cast<int>(*tok_iter);
587 catch(bad_lexical_cast &) {
596 addr = lexical_cast<uint32_from_hex>(*tok_iter);
597 for(unsigned int i = 0; i < commentDefer.size(); i++) {
598 dataCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
600 for(unsigned int i = 0; i < labelDefer.size(); i++) {
601 dataLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
604 commentDefer.clear();
607 catch(bad_lexical_cast& e) {
608 cerr << e.what() << endl;
612 else if(count == 2) {
614 CDat data = lexical_cast<uint32_from_hex>(*tok_iter);
615 cpu.setRAM(addr, data);
617 catch(bad_lexical_cast& e) {
618 cerr << e.what() << endl;
622 else if(count == 4) {
623 if((*tok_iter).size() > 0) {
624 dataLabelStore.insert(pair<int,string>(addr, *tok_iter));
627 else if(count == 5) {
628 if((*tok_iter).size() > 0) {
629 dataLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
636 addr = lexical_cast<uint32_from_hex>(*tok_iter);
637 for(unsigned int i = 0; i < commentDefer.size(); i++) {
638 progCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
640 for(unsigned int i = 0; i < labelDefer.size(); i++) {
641 progLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
644 commentDefer.clear();
647 catch(bad_lexical_cast& e) {
648 cerr << e.what() << endl;
652 else if(count == 2) {
653 Iinstr *pi = disasm.decode(*tok_iter);
654 cpu.setProg(addr, pi);
656 else if(count == 4) {
657 if((*tok_iter).size() > 0) {
658 progLabelStore.insert(pair<int,string>(addr, *tok_iter));
661 else if(count == 5) {
662 if((*tok_iter).size() > 0) {
663 progLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
669 if((*tok_iter).size() > 0) {
670 commentDefer.push_back(*tok_iter);
674 if((*tok_iter).size() > 0) {
675 labelDefer.push_back(*tok_iter);
679 cerr << "i was to lazy to implement the other format types for now" << endl;
689 for(int i = 0; i <= 32; i += 4) {
690 Iinstr *pinstr = cpu.getProg(i);
692 cout << i << " : " << std::hex << i << std::dec << " " << pinstr->toString() << endl;
695 cout << "Null at " << i << " : " << std::hex << i << endl;
699 for(int i = 0; i <= 32; i += 4) {
700 CDat data = cpu.getRAM(i);
701 cout << i << " : " << std::hex << i << std::dec << " " << data << endl;
704 /* cpu.setRegister(1, 4);
705 cpu.setRegister(2, 0);
707 cpu.setRAM(4,0x66334455);
711 // following: job of the bootloader
714 //set return to nowhere for ret
719 MyCompleterContainer Completers;
721 CHelpExec HelpExec(Completers);
723 Completers.push_back(CompleterElement("help", boost::bind1st( boost::mem_fun( &CHelpExec::operator()), &HelpExec), "Prints this message"));
724 Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
725 Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
726 Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
727 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."));
728 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."));
729 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."));
730 Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
731 Completers.push_back(CompleterElement("listbreaks",&listBreaks, "Lists all breakpoints."));
732 Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
733 Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
734 Completers.push_back(CompleterElement("setreg [s] [num]",&setReg, "Sets Register s to num."));
735 Completers.push_back(CompleterElement("setdata [s] [num]",&setRam, "Sets RAM-Addr s to num."));
736 Completers.push_back(CompleterElement("status",&printStatus, "Prints status of CPU."));
737 Completers.push_back(CompleterElement("getperf",&getPerf, "Prints performance counter."));
738 Completers.push_back(CompleterElement("resetperf",&resetPerf, "Resets performance counter to 0."));
739 Completers.push_back(CompleterElement("extension",&applyToExtensions, "Write to extensions."));
740 Reader.RegisterCompletions(Completers);
743 vector<string> Tokens, lastTokens;
744 bool EndOfInput = false;
746 //tilab g++44 doesn't like auto here
747 MyCompleterContainer::iterator Found(Completers.end());
749 Func lastFunc = NULL;
753 UserInput = Reader.GetLine("> ", Tokens, EndOfInput);
758 if(!Tokens.empty()) {
759 Found = find_if(Completers.begin(), Completers.end(), LookupFunctor(Tokens));
761 if(Found != Completers.end()) {
762 if((*Found).get<1>() != 0) {
763 lastFunc = (*Found).get<1>();
766 //(*Found).get<1>()(Tokens);
771 cout << "Unknown command. 'help' displays help" << endl;
775 if(lastFunc != NULL) {
776 lastFunc(lastTokens);
779 cout << "Unknown command. 'help' displays help" << endl;
786 for(int i = 0; ; i++) {
789 cout << " reg0: " << cpu.getRegister(0) << " reg1: " << cpu.getRegister(1);
790 cout << " reg2: " << cpu.getRegister(2) << " reg3: " << cpu.getRegister(3);
791 cout << " reg4: " << cpu.getRegister(4) << " reg5: " << cpu.getRegister(5);
792 cout << endl << endl;