sim: added [st|ld]x
authorMartin Perner <martin@perner.cc>
Wed, 3 Nov 2010 18:43:57 +0000 (19:43 +0100)
committerMartin Perner <martin@perner.cc>
Wed, 3 Nov 2010 18:44:12 +0000 (19:44 +0100)
3b_sim/.gitignore
3b_sim/sim.cpp
3c_disasm/Iinstr.hpp
3c_disasm/dasm.cpp
3c_disasm/disasm.cpp
3c_disasm/disasm.h
3c_disasm/instr/ldx.cpp [new file with mode: 0644]
3c_disasm/instr/stx.cpp [new file with mode: 0644]

index 6eedf3a65670c4fda3cd036426ed05a1d48c6b33..e260184a3b128e761abff324860236940630ecf3 100644 (file)
@@ -1,4 +1,5 @@
 *.o
 *.so
 *.d
+*.dthex
 sim
index 0343edec265c8e74adfc381b29141bce88de9a4e..1fd383403321a016e2ac7db23f9f6404be438609 100644 (file)
@@ -68,6 +68,7 @@ class CHelpExec
 void close_prog(const std::vector<std::string> &);
 
 CCpu* Iinstr::m_cpu;
+disasm* Iinstr::m_disasm;
 
 CCpu* global_cpu = NULL;
 
@@ -480,12 +481,14 @@ int main(int argc, char* argv[])
 
        Iinstr::setCPU(&cpu);
 
+       disasm disasm(instr);
+
+       Iinstr::setDisasm(&disasm);
 
        std::string str = "";
        int addr = 0;
        boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
        boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
-       disasm disasm(instr);
        while(getline(inFile, str)) {
                int count = 0;
                tokens.assign(str);
index d94c88f494e878a22c37f5e4a9aa67eb2baea438..2cc17df5e1f4d5093e10a75d4eb5f3b03db25e41 100644 (file)
@@ -10,6 +10,10 @@ class CCpu;
 
 #include "ccpu.hpp"
 
+class disasm;
+
+#include "disasm.h"
+
 
 /* concept from https://groups.google.com/group/comp.arch.embedded/msg/9d430b6d3da12c8f */
 #define to_HEX__(x) 0x##x##LU
@@ -54,7 +58,8 @@ class Iinstr {
                CDat m_imm;
                short m_cond;
                boost::dynamic_bitset<> argbits;
-               Iinstr() : opcode(0), name(""), m_ra(0), m_rb(0), m_rd(0), m_c(0), m_d(0), m_hl(0), m_f(0), m_s(0), m_imm(0), m_cond(ALWAYS), argbits(32) {}
+               CDat hexdump;
+               Iinstr() : opcode(0), name(""), m_ra(0), m_rb(0), m_rd(0), m_c(0), m_d(0), m_hl(0), m_f(0), m_s(0), m_imm(0), m_cond(ALWAYS), argbits(32), hexdump(0) {}
 
                CDat generate16ImmFill(const CDat value) const {
                        CDat i = value;
@@ -114,20 +119,24 @@ class Iinstr {
                }
 
                static CCpu* m_cpu;
+               static disasm* m_disasm;
 
        public:
 
                static void setCPU(CCpu* cpu) { m_cpu = cpu; }
+               static void setDisasm(disasm* dasm) { m_disasm = dasm; }
 
                virtual ~Iinstr() {}
                virtual short getOpcode() { return this->opcode; }
                virtual std::string getName() { return this->name; }
-               virtual void loadBits(boost::dynamic_bitset<> bits) { argbits = bits; }
+               virtual void loadBits(boost::dynamic_bitset<> bits) { argbits = bits; this->constructHex(); }
                virtual void evalInstr() = 0;
                virtual void execInstr() = 0;
                virtual std::string toString() = 0;
                virtual Iinstr* getNew() = 0;
-                       
+
+               unsigned long toNum() { return this->hexdump; }
+
                short getCondition() {
                        return m_cond;
                }
@@ -143,6 +152,15 @@ class Iinstr {
 
        protected:
 
+               void constructHex()
+               {
+                       hexdump = this->m_cond;
+                       hexdump <<= 5;
+                       hexdump += this->opcode;
+                       hexdump <<= 23;
+                       hexdump += this->argbits.to_ulong();
+               }
+
                std::string getConditionFlag()
                {
                        stringstream cond;
index a11d4f51cc6c5082e9a98cf2e6ac836ffed1b692..254644106ce3da0cde6287054ba376d7383c5d7b 100644 (file)
@@ -22,6 +22,7 @@ std::string progName;
 
 
 CCpu* Iinstr::m_cpu;
+disasm* Iinstr::m_disasm;
 
 int main(int argc, char* argv[])
 {
index 0c6c5f09119e6dde572aa5e20ec2cf3f831b3f73..c117bb047730a625063677756586cef00999d3d8 100644 (file)
@@ -10,8 +10,13 @@ Iinstr* disasm::decode(std::string str)
        string hex = "0x";
        hex.append(str);
 
-       unsigned int val =  lexical_cast<uint32_from_hex>(hex);
+       CDat val =  lexical_cast<uint32_from_hex>(hex);
+       return this->decodeNum(val);
+}
+
 
+Iinstr* disasm::decodeNum(CDat val)
+{
        dynamic_bitset<> bits(32,val), opcode(32,val), condition(9), args(32);
 
        args = opcode;
index 078bd17ba3806f0d3f5c45d97a26b271fcd0efb8..403e38d3c333b6b6b5e76e4dd4f70a1e0ad1c9ef 100644 (file)
@@ -1,8 +1,14 @@
+#ifndef __DISASM_H__
+#define __DISASM_H__
+
 #include <iostream>
 #include <string>
 #include <map>
 
 #include "uint32_from_hex.hpp"
+
+class Iinstr;
+
 #include "Iinstr.hpp"
 
 using namespace std;
@@ -18,6 +24,8 @@ class disasm {
        public:
                disasm(std::map<short,Iinstr*> map) : instrs(map) {};
                Iinstr* decode(std::string);
+               Iinstr* decodeNum(CDat);
                std::string decodeToString(std::string str);
 };
 
+#endif
diff --git a/3c_disasm/instr/ldx.cpp b/3c_disasm/instr/ldx.cpp
new file mode 100644 (file)
index 0000000..b978bd5
--- /dev/null
@@ -0,0 +1,77 @@
+#include "../Iinstr.hpp"
+
+
+class Cldx : public Iinstr {
+       public:
+               Cldx();
+               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 Cldx();
+}
+
+Iinstr* Cldx::getNew()
+{
+    return new Cldx();
+}
+
+/**
+ * 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;
+}
+
+Cldx::Cldx()
+{
+       opcode = B5(10100);
+       name = "ldx";
+}
+
+void Cldx::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);
+
+}
+
+void Cldx::execInstr()
+{
+       //cout << "should exec " << this->toString() << endl;
+       CDat val = this->m_cpu->getRegister(this->m_ra);
+       val += m_imm;
+       this->m_cpu->setRegister(this->m_rd, this->m_cpu->getProg(val)->toNum());
+}
+
+std::string Cldx::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/stx.cpp b/3c_disasm/instr/stx.cpp
new file mode 100644 (file)
index 0000000..9f5058e
--- /dev/null
@@ -0,0 +1,77 @@
+#include "../Iinstr.hpp"
+
+
+class Cstx : public Iinstr {
+       public:
+               Cstx();
+               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 Cstx();
+}
+
+Iinstr* Cstx::getNew()
+{
+    return new Cstx();
+}
+
+/**
+ * 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;
+}
+
+Cstx::Cstx()
+{
+       opcode = B5(10101);
+       name = "stx";
+}
+
+void Cstx::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);
+
+}
+
+void Cstx::execInstr()
+{
+       //cout << "should exec " << this->toString() << endl;
+       CDat val = this->m_cpu->getRegister(this->m_ra);
+       val += m_imm;
+       this->m_cpu->setProg(val, m_disasm->decodeNum(this->m_cpu->getRegister(this->m_rd)));
+}
+
+std::string Cstx::toString()
+{
+       stringstream op;
+       op << this->getName();
+
+       op << this->getConditionFlag() << " r" << m_rd << ", " << m_imm << "(r" <<  m_ra << ")";
+
+       return op.str();
+}