#include "../Iinstr.hpp" class Cbranch : public Iinstr { private: bool m_taken; char m_typ; public: Cbranch(); void evalInstr(); void execInstr(); std::string toString(); Iinstr* getNew(); }; /** * Name: create_instruction * Purpose: if compiled as shared library, this functions creates the instruction object * Returns: pointer to instruction object */ extern "C" Iinstr* create_instruction() { return new Cbranch(); } Iinstr* Cbranch::getNew() { return new Cbranch(); } /** * Name: destroy_instruction * Purpose: if compiled as shared library, this functions destoys the instruction object * Parameter: IInstruction - the instruction object to delete */ extern "C" void destroy_instruction(Iinstr* p) { delete p; } Cbranch::Cbranch() : m_taken(1), m_typ(0) { opcode = B5(10110); name = "branch"; } void Cbranch::evalInstr() { this->m_s = argbits[0]; this->m_taken = argbits[1]; argbits >>= 2; dynamic_bitset<> type = argbits; type.resize(2); this->m_typ = type.to_ulong(); switch(this->m_typ) { case 0: this->name = "br"; break; case 1: this->name = "call"; break; case 2: this->name = "ret"; this->clockcount = 3; break; case 3: this->name = "reti"; this->clockcount = 3; break; default: cerr << "What have you done? 2 bits that have more than 4 values?!" << endl; } argbits >>= 5; dynamic_bitset<> immb = argbits; immb.resize(16); this->m_imm = this->generate16ImmSign(immb.to_ulong()); } void Cbranch::execInstr() { //cout << "should exec " << this->toString() << endl; CDat pc = this->m_cpu->getCurPC(); switch(this->m_typ) { case 1: { CDat sp = this->m_cpu->getStack(); sp -= 4; this->m_cpu->setRAM(sp, this->m_cpu->getNextPC()); this->m_cpu->setStack(sp); } case 0: this->m_cpu->setNextPC(pc+this->m_imm); break; case 2: case 3: this->m_cpu->setNextPC(this->m_cpu->getRAM(this->m_cpu->getStack())); this->m_cpu->setStack(this->m_cpu->getStack()+4); break; default: // nothing this->m_cpu->setNextPC(400); } } std::string Cbranch::toString() { stringstream op; op << this->getName(); if(m_s) op << 'S'; op << this->getConditionFlag() << (m_taken ? '+' : '-'); if(m_typ < 2) { op << " 0x" << std::hex << m_imm << "(" << std::dec << m_imm << ")"; } return op.str(); }