sim: hex/dec inputs solved more userfriendly
authorMartin Perner <martin@perner.cc>
Mon, 1 Nov 2010 11:08:41 +0000 (12:08 +0100)
committerMartin Perner <martin@perner.cc>
Mon, 1 Nov 2010 13:52:22 +0000 (14:52 +0100)
3b_sim/sim.cpp

index 4fe52b06f5091ed974b4c88e7b0ae3040e0b3fda..0a6f14613ba573f1dc38391044b013e89644e475 100644 (file)
@@ -80,12 +80,23 @@ void doExit(const vector<string>&)
        exit(EXIT_SUCCESS);
 }
 
+unsigned int convertStringToNum(const std::string& in)
+{
+       if(in.substr(0,2) == "0x") {
+               return lexical_cast<uint32_from_hex>(in);
+       }
+       else {
+               return lexical_cast<unsigned int>(in);
+       }
+}
+
+
 void execStep(const vector<string>& in)
 {
        int count = 1;
        if(in.size() == 2) {
                try {
-                       count = lexical_cast<int>(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<string>& 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<string>&)
                        }
                        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<string>& in)
        CDat addr = 0;
        if(in.size() == 2) {
                try {
-                       addr = lexical_cast<uint32_from_hex>(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<string>& 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<string>& in)
 
        if(in.size() >= 2) {
                try {
-                       start = lexical_cast<int>(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<string>& in)
 
        if(in.size() >= 3) {
                try {
-                       end = lexical_cast<int>(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<string>& 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<string>& in)
 
        if(in.size() >= 2) {
                try {
-                       start = lexical_cast<int>(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<string>& in)
 
        if(in.size() >= 3) {
                try {
-                       end = lexical_cast<int>(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<string>& in)
 
        if(in.size() >= 2) {
                try {
-                       start = lexical_cast<int>(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<string>& in)
 
        if(in.size() >= 3) {
                try {
-                       end = lexical_cast<int>(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<string>& 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<string>& in)
 
 void setBreak(const vector<string>& in)
 {
-       int addr = 0;
+       unsigned int addr = 0;
        if(in.size() == 2) {
                try {
-                       addr = lexical_cast<uint32_from_hex>(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;