0c6c5f09119e6dde572aa5e20ec2cf3f831b3f73
[calu.git] / 3c_disasm / disasm.cpp
1 #include <boost/lexical_cast.hpp>
2 #include <boost/dynamic_bitset.hpp>
3 #include "disasm.h"
4
5 using namespace boost;
6
7 Iinstr* disasm::decode(std::string str)
8 {
9         /* we need 0x prefix */
10         string hex = "0x";
11         hex.append(str);
12
13         unsigned int val =  lexical_cast<uint32_from_hex>(hex);
14
15         dynamic_bitset<> bits(32,val), opcode(32,val), condition(9), args(32);
16
17         args = opcode;
18         args.resize(23);
19
20         opcode >>= 23;
21         condition = opcode;
22         opcode.resize(5);
23
24         condition >>= 5;
25         condition.resize(4);
26
27         //cout << "<" << hex << "> is in int  " << val << "\t is binary " << bits << " opcode?" << opcode << " condition " << condition << endl;
28         try {
29                 Iinstr* instr = decodeOpcode(opcode.to_ulong());
30                 instr->decodeCondition(condition.to_ulong());
31                 instr->loadBits(args);
32                 instr->evalInstr();
33                 return instr;
34         }
35         catch(std::string &e) {
36                 cerr << " Error: " << e << endl;
37         }
38         return NULL;
39 }
40
41 std::string disasm::decodeToString(std::string str)
42 {
43         return this->decode(str)->toString();
44 }
45
46 Iinstr* disasm::decodeOpcode(short opcode)
47 {
48         auto iter = instrs.find(opcode);
49         if(iter != instrs.end()) {
50                 Iinstr* p = (iter->second)->getNew();
51                 return p;
52         }
53         else {
54                 stringstream err;
55                 err << "opcode not found" << endl;
56                 throw err.str();
57         }
58 }