bb35be2ded3e97e390ba2fbb723b21dc810332e9
[calu.git] / 3c_disasm / instr / ldb.cpp
1 #include "../Iinstr.hpp"
2
3
4 class Cldb : public Iinstr {
5         public:
6                 Cldb();
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 Cldb();
22 }
23
24 Iinstr* Cldb::getNew()
25 {
26     return new Cldb();
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 Cldb::Cldb()
41 {
42         opcode = B5(10010);
43         name = "ldb";
44 }
45
46 void Cldb::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 Cldb::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         val = this->m_cpu->getRAM(temp);
72         val = (val >> (offset*8)) & ( ((1<<8)-1));
73         CDat dest = this->m_cpu->getRegister(this->m_rd);
74         dest = dest & (~((1<<8)-1));
75         dest = dest | val;
76         this->m_cpu->setRegister(this->m_rd,dest);
77 }
78
79 std::string Cldb::toString()
80 {
81         stringstream op;
82         op << this->getName();
83
84         op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
85
86         return op.str();
87 }