sim: new instructions, bugfix in others
[calu.git] / 3c_disasm / instr / sub.cpp
diff --git a/3c_disasm/instr/sub.cpp b/3c_disasm/instr/sub.cpp
new file mode 100644 (file)
index 0000000..d174735
--- /dev/null
@@ -0,0 +1,81 @@
+#include "../Iinstr.hpp"
+
+class Csub : public Iinstr {
+       public:
+               Csub();
+               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 Csub();
+}
+
+Iinstr* Csub::getNew() {
+       return new Csub();
+}
+
+/**
+ * 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;
+}
+
+Csub::Csub()
+{
+       opcode = B5(00001);
+       name = "sub";
+}
+
+void Csub::evalInstr()
+{
+       this->m_d = argbits[0];
+       this->m_c = argbits[1];
+       
+       argbits >>= 11;
+       m_rb = this->getRegister(argbits);
+
+       argbits >>= 4;
+       m_ra = this->getRegister(argbits);
+
+       argbits >>= 4;
+       m_rd = this->getRegister(argbits);
+}
+
+void Csub::execInstr()
+{
+       //cout << "should exec " << this->toString() << endl;
+       CDat ra = this->m_cpu->getRegister(m_ra);
+       CDat rb = this->m_cpu->getRegister(m_rb);
+       CDatd val = ra - rb;
+       this->m_cpu->setRegister(m_rd, val);
+       if(!this->m_d) {
+               this->m_cpu->updateFlags(val, ra, rb);
+       }
+}
+
+std::string Csub::toString()
+{
+       stringstream op;
+       op << this->getName();
+
+       if(m_d) op << 'D';
+       if(m_c) op << 'C';
+
+       op << " r" << m_rd << ", r" << m_ra << ", r" << m_rb;
+       return op.str();
+}