sim: arith. fixes, optimized mem access
authorMartin Perner <martin@perner.cc>
Sun, 31 Oct 2010 21:44:29 +0000 (22:44 +0100)
committerMartin Perner <martin@perner.cc>
Sun, 31 Oct 2010 22:32:48 +0000 (23:32 +0100)
3b_sim/Makefile
3b_sim/ccpu.cpp
3b_sim/ccpu.hpp
3b_sim/cdat.hpp
3b_sim/cmem.cpp
3b_sim/cmem.hpp
3b_sim/cpmem.cpp
3b_sim/cpmem.hpp
3b_sim/sim.cpp
3c_disasm/Iinstr.hpp
3c_disasm/Makefile

index e6d4b04abd171d7d9263931dede0dc7b8b3009cd..c4272a60358f072324672d0c1052f2fdfc0a9081 100644 (file)
@@ -13,7 +13,7 @@ $(PROG): $(sources:.cpp=.o)
 -include $(sources:.cpp=.d)
 .PHONY: libs
 libs:   
-#      $(MAKE) -C instr all
+       $(MAKE) -C instr all
 
 $(sources:.cpp=.o):
        $(CC) $(CPPFLAGS) -c  -o ${@} ${@:.o=.cpp}
@@ -21,7 +21,7 @@ $(sources:.cpp=.o):
 .PHONY:clean
 clean:  
        rm -rf $(PROG) $(sources:.cpp=.o) $(sources:.cpp=.d)
-#      $(MAKE) -C instr clean
+       $(MAKE) -C instr clean
 
 .PHONY: run
 run: $(PROG)
index 148fe8170198214c7b3af4d8034cf6de54094196..4974fcc64d78dfbf4fc7b30f7bc9d1eef1748e90 100644 (file)
@@ -112,8 +112,8 @@ CDat CCpu::getFlags() const {
 void CCpu::updateFlags(CDat val) {
        this->m_Z = (val == 0);
        this->m_S = ((val >> (BIT_LEN-1)) & 0x1);
-       this->m_C = false;
-       this->m_O = false;
+/*     this->m_C = false;
+       this->m_O = false; */
 }
 
 void CCpu::updateFlags(CDatd val, CDat a, CDat b) {
@@ -134,12 +134,12 @@ void CCpu::updateFlags(bool z, bool o, bool c, bool s)
 
 CDat CCpu::getRegister(const int addr) const
 {
-       return m_reg.get(addr);
+       return m_reg.getDirect(addr);
 }
 
 void CCpu::setRegister(const int addr, CDat data)
 {
-       m_reg.set(addr, data);
+       m_reg.setDirect(addr, data);
 }
 
 CDat CCpu::getRAM(const int addr) const
index bef4f9e692905e91c3b8fdbc53d0dabeb1996e30..24f6f31b073da00fe23c1c1adfa35ee70a4323f4 100644 (file)
@@ -39,8 +39,11 @@ class CCpu {
                void setNextPC(CDat);
 
                CDat getFlags() const;
+               /* will only change zero and sign */
                void updateFlags(CDat);
+               /* will change all flags */
                void updateFlags(CDatd, CDat, CDat);
+               /* will change all flags */
                void updateFlags(bool z, bool o, bool c, bool s);
        
 
index 39ea074a4e251a420d162a220c740369fef4c4c2..b5d212b152f9a66fa8c61bc55194b55e1beb0a05 100644 (file)
@@ -7,7 +7,9 @@
 typedef __UINT32_TYPE__ CDat;
 typedef __UINT64_TYPE__ CDatd;
 
-#define BIT_LEN 32
-static_assert(sizeof(CDat) == 4, "The size of the datatype for int is NOT 4 bytes (32 Bit!)");
-static_assert(sizeof(CDatd) > 4, "The size of the datatype for double int is NOT bigger than 4 bytes (32 Bit!)");
+#define BYTE_COUNT 4
+
+#define BIT_LEN (BYTE_COUNT * 8)
+static_assert(sizeof(CDat) == BYTE_COUNT, "The size of the datatype for int is NOT 4 bytes (32 Bit!)");
+static_assert(sizeof(CDatd) > BYTE_COUNT, "The size of the datatype for double int is NOT bigger than 4 bytes (32 Bit!)");
 #endif
index 4b49a081b5560d49871f68786e5f728c1c1dc94d..3b8010dcc6f6ef399a98dd3975600b51e078036f 100644 (file)
@@ -11,7 +11,7 @@ void CMem<T>::set(const MEMORY_ADDRESS address, const T data)
                throw out_of_range(error.str());
        }
 
-       MEMORY_ADDRESS temp = address;
+       MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT;
        auto iter = m_memory.begin();
        while(temp > 0) {
                ++iter;
@@ -25,6 +25,39 @@ void CMem<T>::set(const MEMORY_ADDRESS address, const T data)
 
 template <typename T>
 T CMem<T>::get(const MEMORY_ADDRESS address) const
+{
+       if(address >= MAX_MEMORY) {
+               stringstream error;
+               error << "memoryaddress " << address << " out of range";
+               throw out_of_range(error.str());
+       }
+       MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT;
+       return m_memory[temp];
+}
+
+template <typename T>
+void CMem<T>::setDirect(const MEMORY_ADDRESS address, const T data)
+{
+       if(address >= MAX_MEMORY) {
+               stringstream error;
+               error << "memoryaddress " << address << " out of range";
+               throw out_of_range(error.str());
+       }
+
+       MEMORY_ADDRESS temp = address;
+       auto iter = m_memory.begin();
+       while(temp > 0) {
+               ++iter;
+               temp--;
+       }
+
+       iter = m_memory.insert(iter, data);
+       ++iter;
+       m_memory.erase(iter);
+}
+
+template <typename T>
+T CMem<T>::getDirect(const MEMORY_ADDRESS address) const
 {
        if(address >= MAX_MEMORY) {
                stringstream error;
index 05739dbcb037b31f2445de3ddca4ac7a756ec7f0..8e348c7f7f49a203280c249c2665367e853dca89 100644 (file)
@@ -21,8 +21,13 @@ private:
        const int MAX_MEMORY;
        std::vector<T> m_memory;
 public:
+       /* aligns to BIT_LEN words, aka. does calc direct memorycell from address */
        void set(const MEMORY_ADDRESS address, const T data);
        T get(const MEMORY_ADDRESS address) const;
+
+       /* doesn't align, user has to do */
+       void setDirect(const MEMORY_ADDRESS address, const T data);
+       T getDirect(const MEMORY_ADDRESS address) const;
        CMem(int size) : MAX_MEMORY(size), m_memory(size) {};
 };
 
index 31205e61f17a75e557c4ac2f730fe7871362d0b7..7972aa8f061344cf4a5f6c4cd5d4b9dd895c71ae 100644 (file)
@@ -18,7 +18,7 @@ void CPMem<T>::set(const MEMORY_ADDRESS address, const T data)
                throw out_of_range(error.str());
        }
 
-       MEMORY_ADDRESS temp = address;
+       MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT;
        auto iter = m_memory.begin();
        while(temp > 0) {
                ++iter;
@@ -38,7 +38,8 @@ T CPMem<T>::get(const MEMORY_ADDRESS address) const
                error << "memoryaddress " << address << " out of range";
                throw out_of_range(error.str());
        }
-       return m_memory[address];
+       MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT;
+       return m_memory[temp];
 }
 
 template <typename T>
index fd09f3cd567a41489349a6ab0aa9108a3dd73de2..6b64a0e221a69ee0f6e3d04196d0bca568123354 100644 (file)
@@ -21,7 +21,8 @@ template <typename T>
 class CPMem
 {
 private:
-       //MAX_MEMORY-1 zugreifbare Speicherzellen
+       //MAX_MEMORY-1 zugreifbare Speicherzellen aus BYTE_COUNT Bytes
+       //adressierung erfolgt aber byteweise
        const int MAX_MEMORY;
        std::vector<T> m_memory;
 public:
@@ -30,6 +31,7 @@ public:
        void set(const MEMORY_ADDRESS address, const T data);
        //retuniert referenz eines cdat objekts mit dem Wert von address
        T get(const MEMORY_ADDRESS address) const;
+       T getByte(const MEMORY_ADDRESS address) const;
        CPMem(int size) : MAX_MEMORY(size), m_memory(size, NULL) {};
        ~CPMem();
 };
index bd7f5d0f48edbc341835885e7d3f0b5a759289af..4fe52b06f5091ed974b4c88e7b0ae3040e0b3fda 100644 (file)
@@ -302,6 +302,7 @@ void printStatus(const vector<string>&)
        CDat stackd = global_cpu->getRAM(stackp);
        cout << "Stack pointer: " << stackp << " @stackpointer: " << stackd << endl;
        cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl;
+       cout << "cur PC: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getCurPC() << " next PC: 0x" << setw(8) << setfill('0') << global_cpu->getNextPC() << std::dec << endl;
 
 }
 
@@ -445,6 +446,7 @@ int main(int argc, char* argv[])
                                case 2:
                                case 3:
                                        cerr << "ignoring labels and comments for now" << endl;
+                                       break;
                                default:
                                        cerr << "i was to lazy to implement the other format types for now" << endl;
                        }
@@ -494,9 +496,9 @@ int main(int argc, char* argv[])
        Completers.push_back(CompleterElement("quit", &doExit, "Exits program"));
        Completers.push_back(CompleterElement("exit", &doExit, "Exits program"));
        Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed."));
-       Completers.push_back(CompleterElement("dre [s] [e]",&printReg, "Prints registers. if s is given, only register s is printed. if s and e are given the registers from s to e are printed. if omitted all registers a printed."));
-       Completers.push_back(CompleterElement("dra [s] [e]",&printRAM, "Prints RAM. if s is given, only RAM-Addr. s is printed. if s and e are given the RAM-Addrs from s to e are printed. if omitted the first 16 RAM-Addrs are printed."));
-       Completers.push_back(CompleterElement("dpr [s] [e]",&printPROG, "Prints program. if s is given, only Prog-Addr. s is printed. if s and e are given the Prog-Addrs from s to e are printed. if omitted the first 16 Prog-Addrs are printed."));
+       Completers.push_back(CompleterElement("dreg [s] [e]",&printReg, "Prints registers. if s is given, only register s is printed. if s and e are given the registers from s to e are printed. if omitted all registers a printed."));
+       Completers.push_back(CompleterElement("ddata [s] [e]",&printRAM, "Prints RAM. if s is given, only RAM-Addr. s is printed. if s and e are given the RAM-Addrs from s to e are printed. if omitted the first 16 RAM-Addrs are printed."));
+       Completers.push_back(CompleterElement("dprog [s] [e]",&printPROG, "Prints program. if s is given, only Prog-Addr. s is printed. if s and e are given the Prog-Addrs from s to e are printed. if omitted the first 16 Prog-Addrs are printed."));
        Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr."));
        Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program."));
        Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used."));
index 416db47a869cb6041163165d5e96d968b6477e78..d94c88f494e878a22c37f5e4a9aa67eb2baea438 100644 (file)
@@ -51,13 +51,13 @@ class Iinstr {
                std::string name;
                short m_ra, m_rb, m_rd;
                bool m_c, m_d, m_hl, m_f, m_s;
-               int m_imm;
+               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) {}
 
-               int generate16ImmFill(const int value) const {
-                       int i = value;
+               CDat generate16ImmFill(const CDat value) const {
+                       CDat i = value;
                        if(m_hl == true && m_f == true) {
                                i <<= 16;
                                i |= 0x0000FFFF;
@@ -76,8 +76,8 @@ class Iinstr {
                        return i; 
                }
 
-               int generate16ImmSign(const int value) const {
-                       int i = value;
+               CDat generate16ImmSign(const CDat value) const {
+                       CDat i = value;
                        if(m_hl == true) {
                                i <<= 16;
                        }
@@ -88,8 +88,8 @@ class Iinstr {
                        return i; 
                }
 
-               int generate12ImmSign(const int value) const {
-                       int i = value;
+               CDat generate12ImmSign(const CDat value) const {
+                       CDat i = value;
                        if(m_s == true && (i & 0x0800) != 0) {
                                        i |= 0xFFFFF000;
                        }
@@ -97,8 +97,8 @@ class Iinstr {
                        return i; 
                }
 
-               int generate15ImmSign(const int value) const {
-                       int i = value;
+               CDat generate15ImmSign(const CDat value) const {
+                       CDat i = value;
                        if(m_s == true && (i & 0x4000) != 0) {
                                        i |= 0xFFFF8000;
                        }
index 960717edeeb115a2b921ed1432b9cb2e7d3c19fb..062b6fe854559c856cbe7a1119658f725c710216 100644 (file)
@@ -24,7 +24,7 @@ clean:
        $(MAKE) -C instr clean
 
 .PHONY: run
-run: $(PROG)
+run: all
        ./$(PROG) sum.dthex
 
 %.d: %.cpp