sim: added sth/ldh
authorMartin Perner <martin@perner.cc>
Wed, 3 Nov 2010 14:13:58 +0000 (15:13 +0100)
committerMartin Perner <martin@perner.cc>
Wed, 3 Nov 2010 14:13:58 +0000 (15:13 +0100)
3c_disasm/instr/ldh.cpp [new file with mode: 0644]
3c_disasm/instr/sth.cpp [new file with mode: 0644]

diff --git a/3c_disasm/instr/ldh.cpp b/3c_disasm/instr/ldh.cpp
new file mode 100644 (file)
index 0000000..8998572
--- /dev/null
@@ -0,0 +1,91 @@
+#include "../Iinstr.hpp"
+
+
+class Cldh : public Iinstr {
+       public:
+               Cldh();
+               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 Cldh();
+}
+
+Iinstr* Cldh::getNew()
+{
+    return new Cldh();
+}
+
+/**
+ * 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;
+}
+
+Cldh::Cldh()
+{
+       opcode = B5(10000);
+       name = "ldh";
+}
+
+void Cldh::evalInstr()
+{
+       this->m_s = true;
+
+       dynamic_bitset<> immb = argbits;
+       immb.resize(15);
+       this->m_imm = this->generate15ImmSign(immb.to_ulong());
+
+       argbits >>= 15;
+       m_ra = this->getRegister(argbits);
+       argbits >>= 4;
+       m_rd = this->getRegister(argbits);
+
+}
+
+#include <iomanip>
+#define AS_HEX std::hex << setw(8) << setfill('0')
+
+void Cldh::execInstr()
+{
+       //cout << "should exec " << this->toString() << endl;
+       CDat val = this->m_cpu->getRegister(this->m_ra);
+       val += m_imm;
+       MEMORY_ADDRESS temp = (val & (~(BYTE_COUNT-1)));
+       CDat offset = val-temp;
+       if(offset%2 == 1) {
+               cerr << "you try to get an invalid halfword!" << endl;
+               return;
+       }
+       val = this->m_cpu->getRAM(temp);
+       val = (val >> (offset*8)) & ( ((1<<16)-1));
+       CDat dest = this->m_cpu->getRegister(this->m_rd);
+       dest = dest & (~((1<<16)-1));
+       dest = dest | val;
+       this->m_cpu->setRegister(this->m_rd,dest);
+}
+
+std::string Cldh::toString()
+{
+       stringstream op;
+       op << this->getName();
+
+       op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
+
+       return op.str();
+}
diff --git a/3c_disasm/instr/sth.cpp b/3c_disasm/instr/sth.cpp
new file mode 100644 (file)
index 0000000..aa2c1c3
--- /dev/null
@@ -0,0 +1,96 @@
+#include "../Iinstr.hpp"
+
+
+class Csth : public Iinstr {
+       public:
+               Csth();
+               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 Csth();
+}
+
+Iinstr* Csth::getNew()
+{
+    return new Csth();
+}
+
+/**
+ * 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;
+}
+
+Csth::Csth()
+{
+       opcode = B5(10001);
+       name = "sth";
+}
+
+void Csth::evalInstr()
+{
+       this->m_s = true;
+
+       dynamic_bitset<> immb = argbits;
+       immb.resize(15);
+       this->m_imm = this->generate15ImmSign(immb.to_ulong());
+
+       argbits >>= 15;
+       m_ra = this->getRegister(argbits);
+       argbits >>= 4;
+       m_rd = this->getRegister(argbits);
+
+}
+
+#include <iomanip>
+#define AS_HEX std::hex << setw(8) << setfill('0')
+
+void Csth::execInstr()
+{
+       //cout << "should exec " << this->toString() << endl;
+       CDat addr = this->m_cpu->getRegister(this->m_ra);
+       addr += m_imm;
+       MEMORY_ADDRESS temp = (addr & (~(BYTE_COUNT-1)));
+       CDat offset = addr-temp;
+       if(offset%2 == 1) {
+               cerr << "you try to get an invalid halfword!" << endl;
+               return;
+       }
+       CDat val = this->m_cpu->getRAM(temp);
+
+       val = val & ~( ((1<<16)-1) << (offset*8));
+
+       //data to insert
+       CDat source = this->m_cpu->getRegister(this->m_rd);
+       source = (source & (((1<<16)-1))) << (offset*8);
+
+       val += source;
+
+       this->m_cpu->setRAM(temp, val);
+}
+
+std::string Csth::toString()
+{
+       stringstream op;
+       op << this->getName();
+
+       op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
+
+       return op.str();
+}