From 7f40b333ce625e630cc8daf691672b1f9958c1ef Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Mon, 1 Nov 2010 14:30:18 +0100 Subject: [PATCH] sim: added branchreg and stackop both not tested! --- 3c_disasm/instr/branch.cpp | 2 +- 3c_disasm/instr/branchreg.cpp | 92 +++++++++++++++++++++++++++++ 3c_disasm/instr/stackop.cpp | 108 ++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 3c_disasm/instr/branchreg.cpp create mode 100644 3c_disasm/instr/stackop.cpp diff --git a/3c_disasm/instr/branch.cpp b/3c_disasm/instr/branch.cpp index 405acc0..28ff4eb 100644 --- a/3c_disasm/instr/branch.cpp +++ b/3c_disasm/instr/branch.cpp @@ -88,8 +88,8 @@ void Cbranch::execInstr() case 1: { CDat sp = this->m_cpu->getStack(); - this->m_cpu->setRAM(sp, pc); sp -= 4; + this->m_cpu->setRAM(sp, pc); this->m_cpu->setStack(sp); } case 0: diff --git a/3c_disasm/instr/branchreg.cpp b/3c_disasm/instr/branchreg.cpp new file mode 100644 index 0000000..16a251f --- /dev/null +++ b/3c_disasm/instr/branchreg.cpp @@ -0,0 +1,92 @@ +#include "../Iinstr.hpp" + +class Cbranchreg : public Iinstr { + private: + bool m_taken; + char m_typ; + public: + Cbranchreg(); + 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 Cbranchreg(); +} + +Iinstr* Cbranchreg::getNew() +{ + return new Cbranchreg(); +} +/** + * 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; +} + +Cbranchreg::Cbranchreg() : m_taken(1), m_typ(0) +{ + opcode = B5(10111); + name = "branchreg"; +} + +void Cbranchreg::evalInstr() +{ + argbits >>= 2; + + dynamic_bitset<> type = argbits; + type.resize(1); + this->m_typ = type.to_ulong(); + + switch(this->m_typ) { + case 0: + this->name = "brr"; + break; + case 1: + this->name = "callr"; + break; + default: + cerr << "What have you done? 1 bits that have more than 2 values?!" << endl; + } + + argbits >>= 17; + m_rd = this->getRegister(argbits); +} + +void Cbranchreg::execInstr() +{ + //cout << "should exec " << this->toString() << endl; + CDat pc = this->m_cpu->getRegister(this->m_rd); + + if(this->m_typ == 1) { + CDat sp = this->m_cpu->getStack(); + sp -= 4; + this->m_cpu->setRAM(sp, this->m_cpu->getCurPC()); + this->m_cpu->setStack(sp); + } + this->m_cpu->setNextPC(pc); +} + +std::string Cbranchreg::toString() +{ + stringstream op; + op << this->getName(); + + op << this->getConditionFlag(); + op << " r" << m_rd; + return op.str(); +} diff --git a/3c_disasm/instr/stackop.cpp b/3c_disasm/instr/stackop.cpp new file mode 100644 index 0000000..4cac8be --- /dev/null +++ b/3c_disasm/instr/stackop.cpp @@ -0,0 +1,108 @@ +#include "../Iinstr.hpp" + +class Cstackop : public Iinstr { + char m_typ; + public: + Cstackop(); + 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 Cstackop(); +} + +Iinstr* Cstackop::getNew() +{ + return new Cstackop(); +} +/** + * 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; +} + +Cstackop::Cstackop() : m_typ(0) +{ + opcode = B5(01011); + name = "stackop"; +} + +void Cstackop::evalInstr() +{ + argbits >>= 17; + + dynamic_bitset<> type = argbits; + type.resize(2); + this->m_typ = type.to_ulong(); + + switch(this->m_typ) { + case 0: + this->name = "pop"; + break; + case 1: + this->name = "disc"; + break; + case 2: + this->name = "fetch"; + break; + case 3: + this->name = "push"; + break; + default: + cerr << "What have you done? 2 bits that have more than 4 values?!" << endl; + } + + argbits >>= 2; + m_rd = this->getRegister(argbits); +} + +void Cstackop::execInstr() +{ + CDat sp = this->m_cpu->getStack(); + + switch(this->m_typ) { + case 0: + this->m_cpu->setRegister(m_rd, this->m_cpu->getRAM(sp)); + sp += 4; + break; + case 1: + sp += 4; + break; + case 2: + this->m_cpu->setRegister(m_rd, this->m_cpu->getRAM(sp)); + break; + case 3: + this->m_cpu->setRAM(sp, this->m_cpu->getRegister(m_rd)); + sp -= 4; + break; + default: + cerr << "What have you done? 2 bits that have more than 4 values?!" << endl; + } + + this->m_cpu->setStack(sp); +} + +std::string Cstackop::toString() +{ + stringstream op; + op << this->getName(); + + op << this->getConditionFlag(); + op << " r" << m_rd; + return op.str(); +} -- 2.25.1