5aab94c3933ae4d0f6dcb37aa9ff185105461bf1
[calu.git] / 3c_disasm / instr / addi.cpp
1 #include "../Iinstr.hpp"
2
3 class Caddi : public Iinstr {
4         public:
5                 Caddi();
6                 void evalInstr();
7                 void execInstr();
8                 std::string toString();
9                 Iinstr* getNew();
10 };
11
12 /**
13  * Name:      create_instruction
14  * Purpose:   if compiled as shared library, this functions creates the 
15               instruction object
16
17  * Returns:   pointer to instruction object
18  */
19 extern "C" Iinstr* create_instruction() {
20     return new Caddi();
21 }
22
23 Iinstr* Caddi::getNew() {
24         return new Caddi();
25 }
26
27
28 /**
29  * Name:      destroy_instruction
30  * Purpose:   if compiled as shared library, this functions destoys the 
31               instruction object
32
33  * Parameter: IInstruction - the instruction object to delete
34  */
35 extern "C" void destroy_instruction(Iinstr* p) {
36     delete p;
37 }
38
39 Caddi::Caddi()
40 {
41         opcode = B5(00010);
42         name = "addi";
43 }
44
45 void Caddi::evalInstr()
46 {
47         this->m_s = argbits[2];
48         this->m_c = argbits[1];
49         this->m_d = argbits[0];
50
51         argbits >>= 3;
52         dynamic_bitset<> immb = argbits;
53         immb.resize(12);
54         this->m_imm = this->generate12ImmSign(immb.to_ulong());
55
56         argbits >>= 12;
57         m_ra = this->getRegister(argbits);
58
59         argbits >>= 4;
60         m_rd = this->getRegister(argbits);
61 }
62
63 void Caddi::execInstr()
64 {
65         //cout << "should exec " << this->toString() << endl;
66         CDat ra = this->m_cpu->getRegister(m_ra);
67         CDatd reg = ra + this->m_imm;
68         this->m_cpu->setRegister(m_rd, reg);
69         if(!this->m_d) {
70                 this->m_cpu->updateFlags(reg, ra, this->m_imm);
71         }
72 }
73
74 std::string Caddi::toString()
75 {
76         stringstream op;
77         op << this->getName();
78
79         if(m_d) op << 'D';
80         if(m_s) op << 'S';
81         if(m_c) op << 'C';
82
83         op << this->getConditionFlag() << " r" << m_rd << ", r" << m_ra << ", " << m_imm;
84
85         return op.str();
86 }