3c_disasm: quickfix for deps in Makefile
[calu.git] / 3c_disasm / instr / branch.cpp
1 #include "../Iinstr.hpp"
2
3 class Cbranch : public Iinstr {
4         private:
5                 bool m_taken;
6                 char m_typ;
7         public:
8                 Cbranch();
9                 void evalInstr();
10                 void execInstr();
11                 std::string toString();
12 };
13
14 /**
15  * Name:      create_instruction
16  * Purpose:   if compiled as shared library, this functions creates the 
17               instruction object
18
19  * Returns:   pointer to instruction object
20  */
21 extern "C" Iinstr* create_instruction() {
22     return new Cbranch();
23 }
24
25 /**
26  * Name:      destroy_instruction
27  * Purpose:   if compiled as shared library, this functions destoys the 
28               instruction object
29
30  * Parameter: IInstruction - the instruction object to delete
31  */
32 extern "C" void destroy_instruction(Iinstr* p) {
33     delete p;
34 }
35
36 Cbranch::Cbranch() : m_taken(1), m_typ(0)
37 {
38         opcode = B5(10110);
39         name = "branch";
40 }
41
42 void Cbranch::evalInstr()
43 {
44         this->m_s = argbits[0];
45         this->m_taken = argbits[1];
46
47         argbits >>= 2;
48
49         dynamic_bitset<> type = argbits;
50         type.resize(2);
51         this->m_typ = type.to_ulong();
52
53         switch(m_typ) {
54                 case 0:
55                         this->name = "branch";
56                         break;
57                 case 1:
58                         this->name = "call";
59                         break;
60                 case 2:
61                         this->name = "ret";
62                         break;
63                 case 3:
64                         this->name = "reti";
65                         break;
66                 default:
67                         cerr << "What have you done? 2 bits that have more than 4 values?!" << endl;
68         }
69
70         argbits >>= 5;
71
72         dynamic_bitset<> immb = argbits;
73         immb.resize(16);
74         this->m_imm = this->generate16ImmSign(immb.to_ulong());
75
76 }
77
78 void Cbranch::execInstr()
79 {
80         cout << "should exec" << this->toString() << endl;
81 }
82
83 std::string Cbranch::toString()
84 {
85         stringstream op;
86         op << this->getName();
87
88         if(m_s) op << 'S';
89
90         op << this->getConditionFlag() << (m_taken ? '+' : '-');
91         if(m_typ < 2) {
92                 op << " 0x" << std::hex << m_imm << "(" << std::dec << m_imm << ")";
93         }
94         return op.str();
95 }