8998572f3486732a63212f0762564a088026ed2b
[calu.git] / 3c_disasm / instr / ldh.cpp
1 #include "../Iinstr.hpp"
2
3
4 class Cldh : public Iinstr {
5         public:
6                 Cldh();
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 Cldh();
22 }
23
24 Iinstr* Cldh::getNew()
25 {
26     return new Cldh();
27 }
28
29 /**
30  * Name:      destroy_instruction
31  * Purpose:   if compiled as shared library, this functions destoys the 
32               instruction object
33
34  * Parameter: IInstruction - the instruction object to delete
35  */
36 extern "C" void destroy_instruction(Iinstr* p) {
37     delete p;
38 }
39
40 Cldh::Cldh()
41 {
42         opcode = B5(10000);
43         name = "ldh";
44 }
45
46 void Cldh::evalInstr()
47 {
48         this->m_s = true;
49
50         dynamic_bitset<> immb = argbits;
51         immb.resize(15);
52         this->m_imm = this->generate15ImmSign(immb.to_ulong());
53
54         argbits >>= 15;
55         m_ra = this->getRegister(argbits);
56         argbits >>= 4;
57         m_rd = this->getRegister(argbits);
58
59 }
60
61 #include <iomanip>
62 #define AS_HEX std::hex << setw(8) << setfill('0')
63
64 void Cldh::execInstr()
65 {
66         //cout << "should exec " << this->toString() << endl;
67         CDat val = this->m_cpu->getRegister(this->m_ra);
68         val += m_imm;
69         MEMORY_ADDRESS temp = (val & (~(BYTE_COUNT-1)));
70         CDat offset = val-temp;
71         if(offset%2 == 1) {
72                 cerr << "you try to get an invalid halfword!" << endl;
73                 return;
74         }
75         val = this->m_cpu->getRAM(temp);
76         val = (val >> (offset*8)) & ( ((1<<16)-1));
77         CDat dest = this->m_cpu->getRegister(this->m_rd);
78         dest = dest & (~((1<<16)-1));
79         dest = dest | val;
80         this->m_cpu->setRegister(this->m_rd,dest);
81 }
82
83 std::string Cldh::toString()
84 {
85         stringstream op;
86         op << this->getName();
87
88         op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
89
90         return op.str();
91 }