1e56cfa833a96b0c86fdb5917283596f64b44ed8
[calu.git] / 3c_disasm / instr / stackop.cpp
1 #include "../Iinstr.hpp"
2
3 class Cstackop : public Iinstr {
4                 char m_typ;
5         public:
6                 Cstackop();
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 Cstackop();
22 }
23
24 Iinstr* Cstackop::getNew()
25 {
26         return new Cstackop();
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 Cstackop::Cstackop() : m_typ(0)
40 {
41         opcode = B5(01011);
42         name = "stackop";
43 }
44
45 void Cstackop::evalInstr()
46 {
47         argbits >>= 17;
48
49         dynamic_bitset<> type = argbits;
50         type.resize(2);
51         this->m_typ = type.to_ulong();
52
53         switch(this->m_typ) {
54                 case 0:
55                         this->name = "pop";
56                         break;
57                 case 1:
58                         this->name = "disc";
59                         break;
60                 case 2:
61                         this->name = "fetch";
62                         break;
63                 case 3:
64                         this->name = "push";
65                         break;
66                 default:
67                         cerr << "What have you done? 2 bits that have more than 4 values?!" << endl;
68         }
69
70         argbits >>= 2;
71         m_rd = this->getRegister(argbits);
72 }
73
74 void Cstackop::execInstr()
75 {
76         CDat sp = this->m_cpu->getStack();
77
78         switch(this->m_typ) {
79                 case 0:
80                         this->m_cpu->setRegister(m_rd, this->m_cpu->getRAM(sp));
81                         sp += 4;
82                         break;
83                 case 1:
84                         sp += 4;
85                         break;
86                 case 2:
87                         this->m_cpu->setRegister(m_rd, this->m_cpu->getRAM(sp));
88                         break;
89                 case 3:
90                         sp -= 4;
91                         this->m_cpu->setRAM(sp, this->m_cpu->getRegister(m_rd));
92                         break;
93                 default:
94                         cerr << "What have you done? 2 bits that have more than 4 values?!" << endl;
95         }
96
97         this->m_cpu->setStack(sp);
98 }
99
100 std::string Cstackop::toString()
101 {
102         stringstream op;
103         op << this->getName();
104
105         op << this->getConditionFlag();
106         op << " r" << m_rd;
107         return op.str();
108 }