copyleft: gplv3 added and set repo to public
[calu.git] / 3c_disasm / instr / shift.cpp
1 /*   `Deep Thought', a softcore CPU implemented on a FPGA
2
3     Copyright (C) 2010 Markus Hofstaetter <markus.manrow@gmx.at>
4     Copyright (C) 2010 Martin Perner <e0725782@student.tuwien.ac.at>
5     Copyright (C) 2010 Stefan Rebernig <stefan.rebernig@gmail.com>
6     Copyright (C) 2010 Manfred Schwarz <e0725898@student.tuwien.ac.at>
7     Copyright (C) 2010 Bernhard Urban <lewurm@gmail.com>
8
9     This program is free software: you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation, either version 3 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "../Iinstr.hpp"
23
24 class Cshift : public Iinstr {
25                 char m_typ;
26         public:
27                 Cshift();
28                 void evalInstr();
29                 void execInstr();
30                 std::string toString();
31                 Iinstr* getNew();
32 };
33
34 /**
35  * Name:      create_instruction
36  * Purpose:   if compiled as shared library, this functions creates the 
37               instruction object
38
39  * Returns:   pointer to instruction object
40  */
41 extern "C" Iinstr* create_instruction() {
42     return new Cshift();
43 }
44
45 Iinstr* Cshift::getNew()
46 {
47         return new Cshift();
48 }
49 /**
50  * Name:      destroy_instruction
51  * Purpose:   if compiled as shared library, this functions destoys the 
52               instruction object
53
54  * Parameter: IInstruction - the instruction object to delete
55  */
56 extern "C" void destroy_instruction(Iinstr* p) {
57     delete p;
58 }
59
60 Cshift::Cshift() : m_typ(0)
61 {
62         opcode = B5(01010);
63         name = "shift";
64 }
65
66 void Cshift::evalInstr()
67 {
68         this->m_d = argbits[0];
69         this->m_c = argbits[1];
70         argbits >>= 2;
71
72         dynamic_bitset<> type = argbits;
73         type.resize(2);
74         this->m_typ = type.to_ulong();
75
76         switch(this->m_typ) {
77                 case 1:
78                         cerr << "INVALID shift type, using logic left shift!" << endl;
79                         this->m_typ = 0;
80                 case 0:
81                         this->name = "lls";
82                         break;
83                 case 2:
84                         this->name = "lrs";
85                         break;
86                 case 3:
87                         this->name = "ars";
88                         break;
89                 default:
90                         cerr << "What have you done? 2 bits that have more than 4 values?!" << endl;
91         }
92
93         argbits >>= 8;
94         dynamic_bitset<> immb = argbits;
95         immb.resize(5);
96         this->m_imm = immb.to_ulong();
97         argbits >>= 5;
98         m_ra = this->getRegister(argbits);
99         argbits >>= 4;
100         m_rd = this->getRegister(argbits);
101 }
102
103 void Cshift::execInstr()
104 {
105         CDatd val = this->m_cpu->getRegister(m_ra);
106         switch(this->m_typ) {
107                 case 0:
108                 case 1:
109 //                      this->name = "lls";
110                         for(unsigned int i = 0; i < m_imm; i++) {
111                                 val = val << 1;
112                         }
113                         //carry
114                         if(m_c) {
115                                 this->m_cpu->updateCarry(((val >> BIT_LEN) & 0x1) != 0);
116                         }
117                         break;
118                 case 2:
119 //                      this->name = "lrs";
120                         {
121                                 bool carry = this->m_cpu->getCarry();
122                                 for(unsigned int i = 0; i < m_imm; i++) {
123                                         val = val >> 1;
124
125                                         //carry!
126                                         //if user wants carry, and carry is set
127                                         if(this->m_c && carry) {
128                                                 //set highest bit 1
129                                                 val |= (CDat)(1<<(BIT_LEN-1));
130                                         }
131                                         else {
132                                                 //set highest bit 0
133                                                 val &= (~(1<<(BIT_LEN-1)));
134                                         }
135                                 }
136                         }
137                         break;
138                 case 3:
139 //                      this->name = "ars";
140                         //>> is a ars, but val is CDatd => we have to set the MSB manually
141                         {
142                                 bool msb = (val & (1<<(BIT_LEN-1)) >> (BIT_LEN-1));
143                                 for(unsigned int i = 0; i < m_imm; i++) {
144                                         val = val >> 1;
145                                         if(msb) {
146                                                 //set highest bit 1
147                                                 val |= (CDat)(1<<(BIT_LEN-1));
148                                         }
149                                         else {
150                                         //set highest bit 0
151                                         val &= (~(1<<(BIT_LEN-1)));
152                                         }
153                                 }
154                         }
155                         break;
156                 default:
157                         cerr << "What have you done? 2 bits that have more than 4 values?!" << endl;
158         }
159         this->m_cpu->setRegister(m_rd, val);
160 }
161
162 std::string Cshift::toString()
163 {
164         stringstream op;
165         op << this->getName();
166         if(m_c) op << "C";
167         if(m_d) op << "D";
168
169         op << this->getConditionFlag();
170         op << " r" << m_rd << ", r" << m_ra << ", " << m_imm;
171         return op.str();
172 }