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"
23 #include "SReadline/SReadline.h"
24 using namespace swift;
26 #define RAM_END (0x3000)
27 #define PROG_END (0x3000)
28 #define REG_COUNT (16)
30 typedef boost::function<void (const vector<string> &)> Func;
31 typedef boost::tuple<string, Func, string> CompleterElement;
33 typedef list<CompleterElement> MyCompleterContainer;
38 // Creates a functor and memorises tokens
39 LookupFunctor(const vector<string>& tokens) : Tokens(tokens) {}
41 // Compares the first token only
42 bool operator()(const CompleterElement& ele) const
44 return (strncmp(Tokens.begin()->c_str(), ele.get<0>().c_str(), Tokens.begin()->size()) == 0);
48 const vector<string> &Tokens;
57 const MyCompleterContainer &m_completers;
60 CHelpExec(const MyCompleterContainer &cont) : m_completers(cont) {}
62 void operator() (const vector<string>&)
64 cout << "Available commands: " << endl;
65 for(auto iter = m_completers.begin(); iter != m_completers.end(); ++iter) {
66 cout << setw(19) << setfill(' ') << (*iter).get<0>() << ": " << (*iter).get<2>() << endl;
71 void close_prog(const std::vector<std::string> &);
75 disasm* Iinstr::m_disasm;
76 disasm* Iext::m_disasm;
78 CCpu* global_cpu = NULL;
80 vector<CDat> breakpoints;
85 multimap<int,string> dataCommentsStore, progCommentsStore, dataLabelStore, progLabelStore;
86 map<int,string> dataLineCommentStore, progLineCommentStore;
88 bool ignoreBreak = false;
90 bool exitProg = false;
92 void doExit(const vector<string>&)
97 unsigned int convertStringToNum(const std::string& in)
99 if(in.substr(0,2) == "0x") {
100 return lexical_cast<uint32_from_hex>(in);
103 return lexical_cast<unsigned int>(in);
108 void execStep(const vector<string>& in)
113 count = convertStringToNum(in.back());
116 cerr << "given parameter to step is not a number" << endl;
121 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
122 if(breakp == breakpoints.end() || ignoreBreak) {
128 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
132 catch(std::string& e) {
139 void execRun(const vector<string>&)
143 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
144 if(breakp == breakpoints.end() || ignoreBreak) {
150 cout << color(white,red) << "Breakpoint" << color(white,black) << " 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
154 catch(std::string& e) {
161 void setPC(const vector<string>& in)
166 addr = convertStringToNum(in.back());
169 cerr << "given parameter is not a number" << endl;
173 global_cpu->setNextPC(addr);
174 cout << "Set programcounter to 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << endl;
178 void printReg(const vector<string>& in)
180 int i, start = 0, end = REG_COUNT-1;
187 start = convertStringToNum(in[1]);
188 if(start < 0 || start > (REG_COUNT-1)) {
189 cerr << "start is out of range" << endl;
195 cerr << "given parameter is not a number" << endl;
202 end = convertStringToNum(in[2]);
203 if(start > end || end > (REG_COUNT-1)) {
204 cerr << "end is out of range or smaller than start" << endl;
209 cerr << "given parameter is not a number" << endl;
214 for(i = start; i <= end; i++) {
215 cout << setw(2) << setfill('0') << i << ": 0x";
216 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRegister(i) << " ";
217 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRegister(i) << " ";
218 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRegister(i) << endl;
222 void setReg(const vector<string>& in)
229 reg = convertStringToNum(in[1]);
230 if(reg < 0 || reg > (REG_COUNT-1)) {
231 cerr << "register is out of range" << endl;
235 val = convertStringToNum(in[2]);
237 cout << "Setting register " << reg << " to 0x" << std::hex << setw(8) << setfill('0') << val << std::dec << endl;
238 global_cpu->setRegister(reg,val);
241 cerr << "given parameter is not a number" << endl;
247 void printRAM(const vector<string>& in)
249 int i, start = 0, end = 15;
256 start = convertStringToNum(in[1]);
257 if(start < 0 || start > (RAM_END-1)) {
258 cerr << "start is out of range" << endl;
261 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
265 cerr << "given parameter is not a number" << endl;
272 end = convertStringToNum(in[2]);
273 if(start > end || end > (RAM_END-1)) {
274 cerr << "end is out of range or smaller than start" << endl;
277 if(end % BYTE_COUNT != 0) {
278 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
281 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
285 cerr << "given parameter is not a number" << endl;
289 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
291 auto range = dataLabelStore.equal_range(i);
292 for(auto iter = range.first; iter != range.second; ++iter) {
293 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
297 auto range = dataCommentsStore.equal_range(i);
298 for(auto iter = range.first; iter != range.second; ++iter) {
299 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
302 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": 0x";
303 cout << std::hex << setw(8) << setfill('0') << global_cpu->getRAM(i) << " ";
304 cout << std::dec << setw(10) << setfill(' ') << global_cpu->getRAM(i) << " ";
305 cout << std::dec << setw(10) << setfill(' ') << (int)global_cpu->getRAM(i);
307 auto iter = dataLineCommentStore.find(i);
308 if(iter != dataLineCommentStore.end()) {
309 cout << color(blue,black) << " ;" << iter->second << color(white,black);
316 void setRam(const vector<string>& in)
323 addr = convertStringToNum(in[1]);
324 if(addr < 0 || addr > (RAM_END-1)) {
325 cerr << "RAM-Address is out of range" << endl;
329 addr = (addr & (~(BYTE_COUNT-1))) / BYTE_COUNT;
330 val = convertStringToNum(in[2]);
334 cout << "Setting RAM-Address 0x" << std::hex << setw(8) << setfill('0') << addr;
335 cout << " to 0x" << setw(8) << setfill('0') << val << std::dec << endl;
336 global_cpu->setRAM(addr,val);
339 cerr << "given parameter is not a number" << endl;
344 void printPROG(const vector<string>& in)
346 int i, start = 0, end = 15;
351 if(in.size() >= 2 && in[1][0] == 'c') {
352 start = global_cpu->getCurPC() / BYTE_COUNT;
355 else if(in.size() >= 2) {
357 start = convertStringToNum(in[1]);
358 if(start < 0 || start > (PROG_END-1)) {
359 cerr << "start is out of range" << endl;
362 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
366 cerr << "given parameter is not a number" << endl;
373 end = convertStringToNum(in[2]);
374 if(start > end || end > (PROG_END-1)) {
375 cerr << "end is out of range or smaller than start" << endl;
378 if(end % BYTE_COUNT != 0) {
379 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
382 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
387 cerr << "given parameter is not a number" << endl;
393 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
395 auto range = progLabelStore.equal_range(i);
396 for(auto iter = range.first; iter != range.second; ++iter) {
397 cout << color(yellow,black) << iter->second << ":" << color(white,black) << endl;
401 auto range = progCommentsStore.equal_range(i);
402 for(auto iter = range.first; iter != range.second; ++iter) {
403 cout << color(blue,black) << ";" << iter->second << color(white,black) << endl;
407 Iinstr* pi = global_cpu->getProg(i);
409 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP";
412 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": " << std::dec << global_cpu->colorifyInstr(pi->toString());
414 auto iter = progLineCommentStore.find(i);
415 if(iter != progLineCommentStore.end()) {
416 cout << color(blue,black) << " ;" << iter->second << color(white,black);
422 void setBreak(const vector<string>& in)
424 unsigned int addr = 0;
427 addr = convertStringToNum(in.back());
428 breakpoints.push_back(addr);
429 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << " set" << endl;
432 cerr << "Given parameter is not a valid address" << endl;
436 cerr << "Invalid parameter count!" << endl;
440 void listBreaks(const vector<string>&)
442 for(auto iter = breakpoints.begin(); iter != breakpoints.end(); ++iter) {
443 cout << "Breakpoint at 0x" << std::hex << setw(8) << setfill('0') << *iter << std::dec << endl;
447 void getPerf(const vector<string>&)
449 cout << "current perfcounter is " << std::dec << global_cpu->getPerf() << endl;
452 void resetPerf(const vector<string>&)
454 cout << "reset perfcounter" << endl;
455 global_cpu->setPerf(0);
458 void printStatus(const vector<string>&)
460 CDat stackp = global_cpu->getStack();
461 CDat stackd = global_cpu->getRAM(stackp);
462 cout << "Stack pointer: 0x" << std::hex << setw(8) << setfill('0') << stackp << " @stackpointer: 0x" << setw(8) << stackd << std::dec << " (" << stackd << ")" << endl;
463 cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
464 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;
468 using boost::lexical_cast;
469 using boost::bad_lexical_cast;
473 using namespace boost::program_options;
474 namespace po = boost::program_options;
476 std::string progName;
478 int main(int argc, char* argv[])
483 po::options_description desc("Allowed options");
485 ("help,h","produce help message")
486 ("file,f",value<string>(), "input file")
489 po::positional_options_description p;
492 po::variables_map vm;
493 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
496 if(vm.count("help")) {
497 cout << desc << endl;
501 if(vm.count("file")) {
503 cout << "going to open file " << vm["file"].as<string>() << endl;
505 inFile.open(vm["file"].as<string>(), ios::in);
507 cerr << "Error opening file " << vm["file"].as<string>() << endl;
512 cout << "not input file given!" << endl << endl;
513 cout << desc << endl;
517 catch(std::exception& ex) {
518 cout << ex.what() << endl;
522 string dir = "./instr/";
524 map<short, Iinstr*> instr;
526 CInstrFactory instrFab;
528 instrFab.loadLibsIntoMap(instr, dir);
530 catch(std::bad_alloc& e) {
531 cerr << progName << ": bad_alloc caught " << e.what() << endl;
534 catch(std::string& s) {
535 cerr << progName << ": " << s << endl;
539 CCpu cpu(REG_COUNT, RAM_END, PROG_END);
543 Iinstr::setCPU(&cpu);
546 disasm disasm(instr);
548 Iinstr::setDisasm(&disasm);
549 Iext::setDisasm(&disasm);
551 global_cpu->registerExtension(new Cprog());
553 vector<string> commentDefer;
554 vector<string> labelDefer;
555 std::string str = "";
557 boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
558 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
559 while(getline(inFile, str)) {
564 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
565 if(tok_iter == tokens.begin()) {
567 type = lexical_cast<int>(*tok_iter);
571 catch(bad_lexical_cast &) {
580 addr = lexical_cast<uint32_from_hex>(*tok_iter);
581 for(unsigned int i = 0; i < commentDefer.size(); i++) {
582 dataCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
584 for(unsigned int i = 0; i < labelDefer.size(); i++) {
585 dataLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
588 commentDefer.clear();
591 catch(bad_lexical_cast& e) {
592 cerr << e.what() << endl;
596 else if(count == 2) {
598 CDat data = lexical_cast<uint32_from_hex>(*tok_iter);
599 cpu.setRAM(addr, data);
601 catch(bad_lexical_cast& e) {
602 cerr << e.what() << endl;
606 else if(count == 4) {
607 if((*tok_iter).size() > 0) {
608 dataLabelStore.insert(pair<int,string>(addr, *tok_iter));
611 else if(count == 5) {
612 if((*tok_iter).size() > 0) {
613 dataLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
620 addr = lexical_cast<uint32_from_hex>(*tok_iter);
621 for(unsigned int i = 0; i < commentDefer.size(); i++) {
622 progCommentsStore.insert(pair<int,string>(addr, commentDefer.at(i)));
624 for(unsigned int i = 0; i < labelDefer.size(); i++) {
625 progLabelStore.insert(pair<int,string>(addr, labelDefer.at(i)));
628 commentDefer.clear();
631 catch(bad_lexical_cast& e) {
632 cerr << e.what() << endl;
636 else if(count == 2) {
637 Iinstr *pi = disasm.decode(*tok_iter);
638 cpu.setProg(addr, pi);
640 else if(count == 4) {
641 if((*tok_iter).size() > 0) {
642 progLabelStore.insert(pair<int,string>(addr, *tok_iter));
645 else if(count == 5) {
646 if((*tok_iter).size() > 0) {
647 progLineCommentStore.insert(pair<int,string>(addr, *tok_iter));
653 if((*tok_iter).size() > 0) {
654 commentDefer.push_back(*tok_iter);
658 if((*tok_iter).size() > 0) {
659 labelDefer.push_back(*tok_iter);
663 cerr << "i was to lazy to implement the other format types for now" << endl;
673 for(int i = 0; i <= 32; i += 4) {
674 Iinstr *pinstr = cpu.getProg(i);
676 cout << i << " : " << std::hex << i << std::dec << " " << pinstr->toString() << endl;
679 cout << "Null at " << i << " : " << std::hex << i << endl;
683 for(int i = 0; i <= 32; i += 4) {
684 CDat data = cpu.getRAM(i);
685 cout << i << " : " << std::hex << i << std::dec << " " << data << endl;
688 /* cpu.setRegister(1, 4);
689 cpu.setRegister(2, 0);
691 cpu.setRAM(4,0x66334455);
695 // following: job of the bootloader
698 //set return to nowhere for ret
703 MyCompleterContainer Completers;
705 CHelpExec HelpExec(Completers);
707 Completers.push_back(CompleterElement("help", boost::bind1st( boost::mem_fun( &CHelpExec::operator()), &HelpExec), "Prints this message"));
708 Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
709 Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
710 Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
711 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."));
712 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."));
713 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."));
714 Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
715 Completers.push_back(CompleterElement("listbreaks",&listBreaks, "Lists all breakpoints."));
716 Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
717 Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
718 Completers.push_back(CompleterElement("setreg [s] [num]",&setReg, "Sets Register s to num."));
719 Completers.push_back(CompleterElement("setdata [s] [num]",&setRam, "Sets RAM-Addr s to num."));
720 Completers.push_back(CompleterElement("status",&printStatus, "Prints status of CPU."));
721 Completers.push_back(CompleterElement("getperf",&getPerf, "Prints performance counter."));
722 Completers.push_back(CompleterElement("resetperf",&resetPerf, "Resets performance counter to 0."));
724 Reader.RegisterCompletions(Completers);
727 vector<string> Tokens, lastTokens;
728 bool EndOfInput = false;
730 //tilab g++44 doesn't like auto here
731 MyCompleterContainer::iterator Found(Completers.end());
733 Func lastFunc = NULL;
737 UserInput = Reader.GetLine("> ", Tokens, EndOfInput);
742 if(!Tokens.empty()) {
743 Found = find_if(Completers.begin(), Completers.end(), LookupFunctor(Tokens));
745 if(Found != Completers.end()) {
746 if((*Found).get<1>() != 0) {
747 lastFunc = (*Found).get<1>();
750 //(*Found).get<1>()(Tokens);
755 cout << "Unknown command. 'help' displays help" << endl;
759 if(lastFunc != NULL) {
760 lastFunc(lastTokens);
763 cout << "Unknown command. 'help' displays help" << endl;
770 for(int i = 0; ; i++) {
773 cout << " reg0: " << cpu.getRegister(0) << " reg1: " << cpu.getRegister(1);
774 cout << " reg2: " << cpu.getRegister(2) << " reg3: " << cpu.getRegister(3);
775 cout << " reg4: " << cpu.getRegister(4) << " reg5: " << cpu.getRegister(5);
776 cout << endl << endl;