1 /* `Deep Thought', a softcore CPU implemented on a FPGA
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>
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.
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.
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/>. */
24 /* CDat m_pc, m_pc_next;
25 CMem m_regfile, m_ram;
28 //void registerExtension() {};
30 const char* expression = "(;.*)|(r0)|(rX)|(r1[0-5])|(r[1-5])|(r[6-9])|(0x[a-fA-F0-9]+)|([-]?\\d+)";
31 const char* format = "(?1$&)"
32 //Return-Register: violett
33 "(?2\033[0m\033[35m$&\033[0m\\3:)"
34 // Callee-saved Register: rot
35 "(?4\033[0m\033[31m$&\033[0m\\3:)"
36 //Argument-Register: gruen
37 "(?5\033[0m\033[32m$&\033[0m\\3:)"
38 // Temporary Register: gelb
39 "(?6\033[0m\033[33m$&\033[0m\\3:)"
40 // Zahlenwerte: tuerkis
41 "(?7\033[0m\033[36m$&\033[0m\\3:)"
42 "(?8\033[0m\033[36m$&\033[0m\\3:)";
45 string CCpu::colorifyInstr(string instr)
49 return boost::regex_replace(instr, e, format, boost::match_default | boost::format_all);
52 void CCpu::registerExtension(Iext* ext)
54 m_exts.push_back(ext);
57 void CCpu::applyToExtensions(const vector<string>& in)
59 for(auto iter = m_exts.begin(); iter != m_exts.end(); ++iter) {
60 (*iter)->parseInput(in);
64 void CCpu::breakNext()
69 bool CCpu::shouldBreak()
81 Iinstr* instr = this->getProg(m_pc);
84 throw string("Out of Instructions!");
87 for(auto iter = m_exts.begin(); iter != m_exts.end(); ++iter) {
91 if(this->conditionMet(instr->getCondition())) {
92 cout << color(green,black) << "Executing: " << color(white,black) << colorifyInstr(instr->toString()) << " (0x" << std::hex << m_pc << std::dec << ")" << endl;
94 this->incPerfBy(instr->getClockCount());
97 cout << color(red,black) << "Didn't Execute " << color(white,black) << colorifyInstr(instr->toString()) << "; condition wasn't met" << endl;
103 bool CCpu::conditionMet(short cond)
131 return (!this->m_C && !this->m_Z);
134 return (this->m_C || this->m_Z);
137 return (this->m_S == this->m_O);
140 return (this->m_S != this->m_O);
143 return (!this->m_Z && (this->m_S == this->m_O));
146 return (!this->m_Z || (this->m_S != this->m_O));
155 cerr << "What did you do? more than 16 values in 5 bits?!" << endl;
161 CDat CCpu::getNextPC() const
166 CDat CCpu::getCurPC() const
171 void CCpu::setNextPC(CDat val)
176 CDat CCpu::getFlags() const {
178 psw += (this->m_Z ? 1 : 0);
179 psw += (this->m_O ? 2 : 0);
180 psw += (this->m_C ? 4 : 0);
181 psw += (this->m_S ? 8 : 0);
185 void CCpu::setFlags(CDat psw) {
186 this->m_Z = ((psw & 0x1) != 0);
187 this->m_O = ((psw & 0x2) != 0);
188 this->m_C = ((psw & 0x4) != 0);
189 this->m_S = ((psw & 0x8) != 0);
192 void CCpu::updateFlags(CDat val) {
193 this->m_Z = (val == 0);
194 this->m_S = ((val >> (BIT_LEN-1)) & 0x1);
195 /* this->m_C = false;
196 this->m_O = false; */
199 void CCpu::updateFlags(CDatd val, CDat a, CDat b) {
200 this->m_Z = (val == 0);
201 this->m_S = ((val >> (BIT_LEN-1)) & 0x1);
202 this->m_C = ((val >> (BIT_LEN)) & 0x1);
203 bool a_31 = ((a >> (BIT_LEN-1)) & 0x1);
204 bool b_31 = ((b >> (BIT_LEN-1)) & 0x1);
205 bool val_31 = ((val >> (BIT_LEN-1)) & 0x1);
206 this->m_O = ((a_31 && b_31 && !val_31) || (!a_31 && !b_31 && val_31));
209 void CCpu::updateCarry(bool c)
214 bool CCpu::getCarry()
219 void CCpu::updateFlags(bool z, bool o, bool c, bool s)
227 CDat CCpu::getRegister(const int addr) const
229 return m_reg.getDirect(addr);
232 void CCpu::setRegister(const int addr, CDat data)
234 m_reg.setDirect(addr, data);
237 CDat CCpu::getRAM(const int addr) const
239 if((addr & EXT_MODEL_OFFSET) == 0) {
240 return m_ram.get(addr);
244 for(auto iter = m_exts.begin(); iter != m_exts.end(); ++iter) {
245 result = (*iter)->readData(addr);
254 void CCpu::setRAM(const int addr, CDat data)
256 if((addr & EXT_MODEL_OFFSET) == 0) {
257 m_ram.set(addr, data);
260 for(auto iter = m_exts.begin(); iter != m_exts.end(); ++iter) {
261 (*iter)->loadData(addr, data);
266 void CCpu::setProg(int addr, Iinstr* instr)
268 m_prog.set(addr, instr);
271 Iinstr* CCpu::getProg(const int addr) const
273 return m_prog.get(addr);
276 int CCpu::getStack() const
278 return this->m_stack;
281 void CCpu::setStack(const int val)
286 CDat CCpu::getPerf() const
291 void CCpu::setPerf(CDat val)
301 void CCpu::incPerfBy(short inc)
307 CCpu::CCpu(int regs, int ram, int prog) : m_Z(false), m_S(false), m_C(false), m_O(false), m_breakNext(0), m_pc(0), m_pc_next(0), m_perf(0), m_reg(regs), m_ram(ram), m_prog(prog), m_exts(0), m_stack(0)