From 65bff8f91cfc8dcb8df6b220126c81c17d14ef56 Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Mon, 1 Nov 2010 12:08:41 +0100 Subject: [PATCH] sim: hex/dec inputs solved more userfriendly --- 3b_sim/sim.cpp | 60 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/3b_sim/sim.cpp b/3b_sim/sim.cpp index 4fe52b0..0a6f146 100644 --- a/3b_sim/sim.cpp +++ b/3b_sim/sim.cpp @@ -80,12 +80,23 @@ void doExit(const vector&) exit(EXIT_SUCCESS); } +unsigned int convertStringToNum(const std::string& in) +{ + if(in.substr(0,2) == "0x") { + return lexical_cast(in); + } + else { + return lexical_cast(in); + } +} + + void execStep(const vector& in) { int count = 1; if(in.size() == 2) { try { - count = lexical_cast(in.back()); + count = convertStringToNum(in.back()); } catch(bad_cast&) { cerr << "given parameter to step is not a number" << endl; @@ -100,7 +111,7 @@ void execStep(const vector& in) } else { ignoreBreak = true; - cout << "Breakpoint " << *breakp << " hit" << endl; + cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl; break; } } @@ -122,7 +133,7 @@ void execRun(const vector&) } else { ignoreBreak = true; - cout << "Breakpoint " << *breakp << " hit" << endl; + cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << *breakp << std::hex << " hit" << endl; return; } } @@ -138,7 +149,7 @@ void setPC(const vector& in) CDat addr = 0; if(in.size() == 2) { try { - addr = lexical_cast(in.back()); + addr = convertStringToNum(in.back()); } catch(bad_cast&) { cerr << "given parameter is not a number" << endl; @@ -146,7 +157,7 @@ void setPC(const vector& in) } } global_cpu->setNextPC(addr); - cout << "Set programcounter to " << addr << endl; + cout << "Set programcounter to 0x" << std::hex << setw(8) << setfill('0') << addr << std::dec << endl; } @@ -159,7 +170,7 @@ void printReg(const vector& in) if(in.size() >= 2) { try { - start = lexical_cast(in[1]); + start = convertStringToNum(in[1]); if(start < 0 || start > (REG_COUNT-1)) { cerr << "start is out of range" << endl; return; @@ -174,7 +185,7 @@ void printReg(const vector& in) if(in.size() >= 3) { try { - end = lexical_cast(in[2]); + end = convertStringToNum(in[2]); if(start > end || end > (REG_COUNT-1)) { cerr << "end is out of range or smaller than start" << endl; return; @@ -187,7 +198,7 @@ void printReg(const vector& in) } for(i = start; i <= end; i++) { - 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; + 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; } } @@ -200,11 +211,12 @@ void printRAM(const vector& in) if(in.size() >= 2) { try { - start = lexical_cast(in[1]); + start = convertStringToNum(in[1]); if(start < 0 || start > (RAM_END-1)) { cerr << "start is out of range" << endl; return; } + start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT; end = start; } catch(bad_cast&) { @@ -215,19 +227,24 @@ void printRAM(const vector& in) if(in.size() >= 3) { try { - end = lexical_cast(in[2]); + end = convertStringToNum(in[2]); if(start > end || end > (RAM_END-1)) { cerr << "end is out of range or smaller than start" << endl; return; } + if(end % BYTE_COUNT != 0) { + end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1; + } + else { + end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT); + } } catch(bad_cast&) { cerr << "given parameter is not a number" << endl; return; } } - - for(i = start*4; i <= end*4; i += 4) { + for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) { 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; } } @@ -241,11 +258,12 @@ void printPROG(const vector& in) if(in.size() >= 2) { try { - start = lexical_cast(in[1]); + start = convertStringToNum(in[1]); if(start < 0 || start > (PROG_END-1)) { cerr << "start is out of range" << endl; return; } + start = (start & (~(BYTE_COUNT-1))) / BYTE_COUNT; end = start; } catch(bad_cast&) { @@ -256,11 +274,18 @@ void printPROG(const vector& in) if(in.size() >= 3) { try { - end = lexical_cast(in[2]); + end = convertStringToNum(in[2]); if(start > end || end > (PROG_END-1)) { cerr << "end is out of range or smaller than start" << endl; return; } + if(end % BYTE_COUNT != 0) { + end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT)+1; + } + else { + end = ((end & (~(BYTE_COUNT-1))) / BYTE_COUNT); + } + } catch(bad_cast&) { cerr << "given parameter is not a number" << endl; @@ -268,7 +293,7 @@ void printPROG(const vector& in) } } - for(i = start*4; i <= end*4; i += 4) { + for(i = start*BYTE_COUNT; i <= end*BYTE_COUNT; i += BYTE_COUNT) { Iinstr* pi = global_cpu->getProg(i); if(pi == NULL) { cout << std::hex << "0x" << setw(8) << setfill('0') << i << ": NOP" << endl; @@ -281,11 +306,12 @@ void printPROG(const vector& in) void setBreak(const vector& in) { - int addr = 0; + unsigned int addr = 0; if(in.size() == 2) { try { - addr = lexical_cast(in.back()); + addr = convertStringToNum(in.back()); breakpoints.push_back(addr); + cout << "Breakpoint 0x" << std::hex << setw(8) << setfill('0') << addr << std::hex << " set" << endl; } catch(bad_cast&) { cerr << "Given parameter is not a valid address" << endl; -- 2.25.1