aa2c1c3e80a167abc2855f1c76728d6a15f1c9f5
[calu.git] / 3c_disasm / instr / sth.cpp
1 #include "../Iinstr.hpp"
2
3
4 class Csth : public Iinstr {
5         public:
6                 Csth();
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 Csth();
22 }
23
24 Iinstr* Csth::getNew()
25 {
26     return new Csth();
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 Csth::Csth()
41 {
42         opcode = B5(10001);
43         name = "sth";
44 }
45
46 void Csth::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 Csth::execInstr()
65 {
66         //cout << "should exec " << this->toString() << endl;
67         CDat addr = this->m_cpu->getRegister(this->m_ra);
68         addr += m_imm;
69         MEMORY_ADDRESS temp = (addr & (~(BYTE_COUNT-1)));
70         CDat offset = addr-temp;
71         if(offset%2 == 1) {
72                 cerr << "you try to get an invalid halfword!" << endl;
73                 return;
74         }
75         CDat val = this->m_cpu->getRAM(temp);
76
77         val = val & ~( ((1<<16)-1) << (offset*8));
78
79         //data to insert
80         CDat source = this->m_cpu->getRegister(this->m_rd);
81         source = (source & (((1<<16)-1))) << (offset*8);
82
83         val += source;
84
85         this->m_cpu->setRAM(temp, val);
86 }
87
88 std::string Csth::toString()
89 {
90         stringstream op;
91         op << this->getName();
92
93         op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
94
95         return op.str();
96 }