bb18c286dc97e49510a408a08d17973de9c5e4db
[calu.git] / 3c_disasm / instr / ldi.cpp
1 #include "../Iinstr.hpp"
2
3
4 class Cldi : public Iinstr {
5         public:
6                 Cldi();
7                 void evalInstr();
8                 void execInstr();
9                 std::string toString();
10                 Iinstr* getNew();
11 };
12
13 /**
14  * Name:      create_instruction
15  * Purpose:   if compiled as shared library, this functions creates the 
16               instruction object
17
18  * Returns:   pointer to instruction object
19  */
20 extern "C" Iinstr* create_instruction() {
21     return new Cldi();
22 }
23
24 Iinstr* Cldi::getNew()
25 {
26     return new Cldi();
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 Cldi::Cldi()
40 {
41         opcode = B5(11010);
42         name = "ldi";
43 }
44
45 void Cldi::evalInstr()
46 {
47         this->m_s = argbits[2];
48         this->m_hl = argbits[1];
49
50         argbits >>= 3;
51         dynamic_bitset<> immb = argbits;
52         immb.resize(16);
53         this->m_imm = this->generate16ImmSign(immb.to_ulong());
54
55         argbits >>= 16;
56         m_rd = this->getRegister(argbits);
57 }
58
59 void Cldi::execInstr()
60 {
61         //cout << "should exec " << this->toString() << endl;
62         CDat val = this->m_cpu->getRegister(this->m_rd);
63         if(this->m_s == false) {
64                 if(this->m_hl == false) {
65                         val &= 0xFFFF0000;
66                 }
67                 else {
68                         val &= 0x0000FFFF;
69                 }
70                 this->m_cpu->setRegister(this->m_rd, val + this->m_imm);
71         }
72         else {
73                 this->m_cpu->setRegister(this->m_rd, this->m_imm);
74         }
75 }
76
77 std::string Cldi::toString()
78 {
79         stringstream op;
80         op << this->getName();
81
82         if(m_hl) op << 'H';
83         if(m_s) op << 'S';
84
85         op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm;
86
87         return op.str();
88 }