sim: call pushed current pc instead of next pc onto stack
[calu.git] / 3c_disasm / instr / branch.cpp
index 37682d7ed816ec78e316797d1eed407801363258..39b2e73c415348fcef9ed366893eb55902f6cd2f 100644 (file)
@@ -9,6 +9,7 @@ class Cbranch : public Iinstr {
                void evalInstr();
                void execInstr();
                std::string toString();
+               Iinstr* getNew();
 };
 
 /**
@@ -22,6 +23,10 @@ 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 
@@ -50,9 +55,9 @@ void Cbranch::evalInstr()
        type.resize(2);
        this->m_typ = type.to_ulong();
 
-       switch(m_typ) {
+       switch(this->m_typ) {
                case 0:
-                       this->name = "branch";
+                       this->name = "br";
                        break;
                case 1:
                        this->name = "call";
@@ -77,7 +82,28 @@ void Cbranch::evalInstr()
 
 void Cbranch::execInstr()
 {
-       cout << "should exec" << this->toString() << endl;
+       //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()