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 (1000)
24 #define PROG_END (1000)
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(15) << (*iter).get<0>() << ": " << (*iter).get<2>() << endl;
68 void close_prog(const std::vector<std::string> &);
72 CCpu* global_cpu = NULL;
74 vector<CDat> breakpoints;
76 bool ignoreBreak = false;
78 void doExit(const vector<string>&)
83 unsigned int convertStringToNum(const std::string& in)
85 if(in.substr(0,2) == "0x") {
86 return lexical_cast<uint32_from_hex>(in);
89 return lexical_cast<unsigned int>(in);
94 void execStep(const vector<string>& in)
99 count = convertStringToNum(in.back());
102 cerr << "given parameter to step is not a number" << endl;
107 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
108 if(breakp == breakpoints.end() || ignoreBreak) {
114 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
118 catch(std::string& e) {
125 void execRun(const vector<string>&)
129 auto breakp = find(breakpoints.begin(), breakpoints.end(), global_cpu->getNextPC());
130 if(breakp == breakpoints.end() || ignoreBreak) {
136 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl;
140 catch(std::string& e) {
147 void setPC(const vector<string>& in)
152 addr = convertStringToNum(in.back());
155 cerr << "given parameter is not a number" << endl;
159 global_cpu->setNextPC(addr);
160 cout << "Set programcounter to 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << endl;
164 void printReg(const vector<string>& in)
166 int i, start = 0, end = REG_COUNT-1;
173 start = convertStringToNum(in[1]);
174 if(start < 0 || start > (REG_COUNT-1)) {
175 cerr << "start is out of range" << endl;
181 cerr << "given parameter is not a number" << endl;
188 end = convertStringToNum(in[2]);
189 if(start > end || end > (REG_COUNT-1)) {
190 cerr << "end is out of range or smaller than start" << endl;
195 cerr << "given parameter is not a number" << endl;
200 for(i = start; i <= end; i++) {
201 cout << setw(2) << setfill('0') << i << ": 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getRegister(i) << " " << std::dec << setw(10) << setfill(' ') << global_cpu->getRegister(i) << endl;
205 void printRAM(const vector<string>& in)
207 int i, start = 0, end = 15;
214 start = convertStringToNum(in[1]);
215 if(start < 0 || start > (RAM_END-1)) {
216 cerr << "start is out of range" << endl;
219 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
223 cerr << "given parameter is not a number" << endl;
230 end = convertStringToNum(in[2]);
231 if(start > end || end > (RAM_END-1)) {
232 cerr << "end is out of range or smaller than start" << endl;
235 if(end % BYTE_COUNT != 0) {
236 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
239 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
243 cerr << "given parameter is not a number" << endl;
247 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
248 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;
252 void printPROG(const vector<string>& in)
254 int i, start = 0, end = 15;
261 start = convertStringToNum(in[1]);
262 if(start < 0 || start > (PROG_END-1)) {
263 cerr << "start is out of range" << endl;
266 start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT;
270 cerr << "given parameter is not a number" << endl;
277 end = convertStringToNum(in[2]);
278 if(start > end || end > (PROG_END-1)) {
279 cerr << "end is out of range or smaller than start" << endl;
282 if(end % BYTE_COUNT != 0) {
283 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1;
286 end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT);
291 cerr << "given parameter is not a number" << endl;
296 for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) {
297 Iinstr* pi = global_cpu->getProg(i);
299 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP" << endl;
302 cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": " << std::dec << pi->toString() << endl;
307 void setBreak(const vector<string>& in)
309 unsigned int addr = 0;
312 addr = convertStringToNum(in.back());
313 breakpoints.push_back(addr);
314 cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << " set" << endl;
317 cerr << "Given parameter is not a valid address" << endl;
321 cerr << "Invalid parameter count!" << endl;
325 void listBreaks(const vector<string>&)
327 for(auto iter = breakpoints.begin(); iter != breakpoints.end(); ++iter) {
328 cout << "Breakpoint at 0x" << std::hex << setw(8) << setfill('0') << *iter << std::dec << endl;
332 void printStatus(const vector<string>&)
334 CDat stackp = global_cpu->getStack();
335 CDat stackd = global_cpu->getRAM(stackp);
336 cout << "Stack pointer: " << stackp << " @stackpointer: " << stackd << endl;
337 cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
338 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;
342 using boost::lexical_cast;
343 using boost::bad_lexical_cast;
347 using namespace boost::program_options;
348 namespace po = boost::program_options;
350 std::string progName;
352 int main(int argc, char* argv[])
357 po::options_description desc("Allowed options");
359 ("help,h","produce help message")
360 ("file,f",value<string>(), "input file")
363 po::positional_options_description p;
366 po::variables_map vm;
367 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
370 if(vm.count("help")) {
371 cout << desc << endl;
375 if(vm.count("file")) {
377 cout << "going to open file " << vm["file"].as<string>() << endl;
379 inFile.open(vm["file"].as<string>(), ios::in);
381 cerr << "Error opening file " << vm["file"].as<string>() << endl;
386 cout << "not input file given!" << endl << endl;
387 cout << desc << endl;
391 catch(std::exception& ex) {
392 cout << ex.what() << endl;
396 string dir = "./instr/";
398 map<short, Iinstr*> instr;
400 CInstrFactory instrFab;
402 instrFab.loadLibsIntoMap(instr, dir);
404 catch(std::bad_alloc& e) {
405 cerr << progName << ": bad_alloc caught " << e.what() << endl;
408 catch(std::string& s) {
409 cerr << progName << ": " << s << endl;
413 CCpu cpu(REG_COUNT, RAM_END, PROG_END);
417 Iinstr::setCPU(&cpu);
420 std::string str = "";
422 boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
423 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
424 disasm disasm(instr);
425 while(getline(inFile, str)) {
430 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
431 if(tok_iter == tokens.begin()) {
433 type = lexical_cast<int>(*tok_iter);
437 catch(bad_lexical_cast &) {
446 addr = lexical_cast<uint32_from_hex>(*tok_iter);
448 catch(bad_lexical_cast& e) {
449 cerr << e.what() << endl;
453 else if(count == 2) {
455 CDat data = lexical_cast<uint32_from_hex>(*tok_iter);
456 cpu.setRAM(addr, data);
458 catch(bad_lexical_cast& e) {
459 cerr << e.what() << endl;
467 addr = lexical_cast<uint32_from_hex>(*tok_iter);
469 catch(bad_lexical_cast& e) {
470 cerr << e.what() << endl;
474 else if(count == 2) {
475 Iinstr *pi = disasm.decode(*tok_iter);
476 cpu.setProg(addr, pi);
481 cerr << "ignoring labels and comments for now" << endl;
484 cerr << "i was to lazy to implement the other format types for now" << endl;
494 for(int i = 0; i <= 32; i += 4) {
495 Iinstr *pinstr = cpu.getProg(i);
497 cout << i << " : " << std::hex << i << std::dec << " " << pinstr->toString() << endl;
500 cout << "Null at " << i << " : " << std::hex << i << endl;
504 for(int i = 0; i <= 32; i += 4) {
505 CDat data = cpu.getRAM(i);
506 cout << i << " : " << std::hex << i << std::dec << " " << data << endl;
509 cpu.setRegister(1, 4);
510 cpu.setRegister(2, 0);
516 // following: job of the bootloader
519 //set return to nowhere for ret
524 MyCompleterContainer Completers;
526 CHelpExec HelpExec(Completers);
528 Completers.push_back(CompleterElement("help", boost::bind1st( boost::mem_fun( &CHelpExec::operator()), &HelpExec), "Prints this message"));
529 Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
530 Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
531 Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
532 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."));
533 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."));
534 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."));
535 Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
536 Completers.push_back(CompleterElement("listbreaks",&listBreaks, "Lists all breakpoints."));
537 Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
538 Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
539 Completers.push_back(CompleterElement("status",&printStatus, "Prints status of CPU."));
541 Reader.RegisterCompletions(Completers);
544 vector<string> Tokens, lastTokens;
545 bool EndOfInput = false;
547 auto Found(Completers.end());
549 Func lastFunc = NULL;
553 UserInput = Reader.GetLine("> ", Tokens, EndOfInput);
558 if(!Tokens.empty()) {
559 Found = find_if(Completers.begin(), Completers.end(), LookupFunctor(Tokens));
561 if(Found != Completers.end()) {
562 if((*Found).get<1>() != 0) {
563 lastFunc = (*Found).get<1>();
566 //(*Found).get<1>()(Tokens);
571 cout << "Unknown command. 'help' displays help" << endl;
575 if(lastFunc != NULL) {
576 lastFunc(lastTokens);
579 cout << "Unknown command. 'help' displays help" << endl;
586 for(int i = 0; ; i++) {
589 cout << " reg0: " << cpu.getRegister(0) << " reg1: " << cpu.getRegister(1);
590 cout << " reg2: " << cpu.getRegister(2) << " reg3: " << cpu.getRegister(3);
591 cout << " reg4: " << cpu.getRegister(4) << " reg5: " << cpu.getRegister(5);
592 cout << endl << endl;